Skip to content

Commit 2b0c24a

Browse files
authored
Validate PATCH /line/:lineid/bounds requests (#452)
* First pass at validating the xywh * changes while reviewing
1 parent ae3aea9 commit 2b0c24a

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

classes/Line/Line.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,11 @@ export default class Line {
180180
}
181181

182182
async updateBounds({x, y, w, h}, options = {}) {
183-
if (!x || !y || !w || !h) {
184-
throw new Error('Bounds ({x,y,w,h}) must be provided')
183+
const isValidBound = v => (Number.isInteger(v) && v >= 0) || (typeof v === 'string' && /^\d+$/.test(v))
184+
if (!isValidBound(x) || !isValidBound(y) || !isValidBound(w) || !isValidBound(h)) {
185+
throw new Error('Bounds ({x,y,w,h}) must be non-negative integers')
185186
}
187+
x = parseInt(x, 10); y = parseInt(y, 10); w = parseInt(w, 10); h = parseInt(h, 10)
186188
if (options.creator) this.creator = options.creator
187189
this.target ??= ''
188190
const newTarget = this.updateTargetXYWH(this.target, x, y, w, h)

line/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,11 @@ router.patch('/:lineId/bounds', auth0Middleware(), async (req, res) => {
270270
if (!(await projectObj.checkUserAccess(user._id, ACTIONS.UPDATE, SCOPES.SELECTOR, ENTITIES.LINE))) {
271271
return respondWithError(res, 403, 'You do not have permission to update line bounds in this project')
272272
}
273-
if (typeof req.body !== 'object' || !req.body.x || !req.body.y || !req.body.w || !req.body.h) {
274-
return respondWithError(res, 400, 'Invalid request body. Expected an object with x, y, w, and h properties.')
273+
const isValidBound = v => (Number.isInteger(v) && v >= 0) || (typeof v === 'string' && /^\d+$/.test(v))
274+
if (!req.body || typeof req.body !== 'object' || Array.isArray(req.body) || !isValidBound(req.body.x) || !isValidBound(req.body.y) || !isValidBound(req.body.w) || !isValidBound(req.body.h)) {
275+
return respondWithError(res, 400, 'Invalid request body. Expected an object with x, y, w, and h as non-negative integers.')
275276
}
277+
const bounds = { x: parseInt(req.body.x, 10), y: parseInt(req.body.y, 10), w: parseInt(req.body.w, 10), h: parseInt(req.body.h, 10) }
276278
const project = await getProjectById(req.params.projectId)
277279
const page = await findPageById(req.params.pageId, req.params.projectId)
278280
const findOldLine = page.items?.find(l => l.id.split('/').pop() === req.params.lineId?.split('/').pop())
@@ -283,7 +285,7 @@ router.patch('/:lineId/bounds', auth0Middleware(), async (req, res) => {
283285
let oldLine = await fetch(findOldLine.id).then(res => res.json())
284286
delete oldLine.label
285287
const line = new Line(oldLine)
286-
const updatedLine = await line.updateBounds(req.body, { creator: user._id })
288+
const updatedLine = await line.updateBounds(bounds, { creator: user._id })
287289
const lineIndex = page.items.findIndex(l => l.id.split('/').pop() === req.params.lineId?.split('/').pop())
288290
page.items[lineIndex] = updatedLine
289291

0 commit comments

Comments
 (0)