Skip to content

Commit

Permalink
Sanitize workpad before sending to API (#57704) (#58240)
Browse files Browse the repository at this point in the history
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
Corey Robertson and elasticmachine authored Feb 21, 2020
1 parent 64215c5 commit 369004b
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions x-pack/legacy/plugins/canvas/public/lib/workpad_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,35 @@ import {
} from '../../common/lib/constants';
import { fetch } from '../../common/lib/fetch';
import { getCoreStart } from '../legacy';
/*
Remove any top level keys from the workpad which will be rejected by validation
*/
const validKeys = [
'@created',
'@timestamp',
'assets',
'colors',
'css',
'height',
'id',
'isWriteable',
'name',
'page',
'pages',
'width',
];

const sanitizeWorkpad = function(workpad) {
const workpadKeys = Object.keys(workpad);

for (const key of workpadKeys) {
if (!validKeys.includes(key)) {
delete workpad[key];
}
}

return workpad;
};

const getApiPath = function() {
const basePath = getCoreStart().http.basePath.get();
Expand All @@ -29,7 +58,10 @@ const getApiPathAssets = function() {
};

export function create(workpad) {
return fetch.post(getApiPath(), { ...workpad, assets: workpad.assets || {} });
return fetch.post(getApiPath(), {
...sanitizeWorkpad({ ...workpad }),
assets: workpad.assets || {},
});
}

export function get(workpadId) {
Expand All @@ -41,11 +73,11 @@ export function get(workpadId) {

// TODO: I think this function is never used. Look into and remove the corresponding route as well
export function update(id, workpad) {
return fetch.put(`${getApiPath()}/${id}`, workpad);
return fetch.put(`${getApiPath()}/${id}`, sanitizeWorkpad({ ...workpad }));
}

export function updateWorkpad(id, workpad) {
return fetch.put(`${getApiPathStructures()}/${id}`, workpad);
return fetch.put(`${getApiPathStructures()}/${id}`, sanitizeWorkpad({ ...workpad }));
}

export function updateAssets(id, workpadAssets) {
Expand Down

0 comments on commit 369004b

Please sign in to comment.