Skip to content

Commit 1613435

Browse files
committed
Handle invalid object paths in map multipart field entries.
Fixes #154 .
1 parent 9b49be4 commit 1613435

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# graphql-upload changelog
22

3+
## Next
4+
5+
### Patch
6+
7+
- Handle invalid object paths in `map` multipart field entries, fixing [#154](https://github.com/jaydenseric/graphql-upload/issues/154).
8+
39
## 8.0.6
410

511
### Patch

src/processRequest.mjs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,16 @@ export const processRequest = (
271271
)
272272
)
273273

274-
operationsPath.set(path, map.get(fieldName).promise)
274+
try {
275+
operationsPath.set(path, map.get(fieldName).promise)
276+
} catch (error) {
277+
return exit(
278+
createError(
279+
400,
280+
`Invalid object path for the ‘map’ multipart field entry key ‘${fieldName}’ array index ‘${index}’ value ‘${path}’ (${SPEC_URL}).`
281+
)
282+
)
283+
}
275284
}
276285
}
277286

src/test.mjs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,53 @@ t.test('Invalid ‘map’ entry array item type.', async t => {
537537
})
538538
})
539539

540+
t.test('Invalid ‘map’ entry object path.', async t => {
541+
const sendRequest = async (t, port) => {
542+
const body = new FormData()
543+
544+
body.append('operations', '{ "variables": "" }')
545+
body.append('map', '{ "1": ["variables.file"] }')
546+
body.append('1', 'a', { filename: 'a.txt' })
547+
548+
const { status } = await fetch(`http://localhost:${port}`, {
549+
method: 'POST',
550+
body
551+
})
552+
553+
t.equal(status, 400, 'Response status.')
554+
}
555+
556+
await t.test('Koa middleware.', async t => {
557+
t.plan(2)
558+
559+
const app = new Koa()
560+
.on('error', error =>
561+
t.matchSnapshot(snapshotError(error), 'Middleware throws.')
562+
)
563+
.use(graphqlUploadKoa())
564+
565+
const port = await startServer(t, app)
566+
567+
await sendRequest(t, port)
568+
})
569+
570+
await t.test('Express middleware.', async t => {
571+
t.plan(2)
572+
573+
const app = express()
574+
.use(graphqlUploadExpress())
575+
.use((error, request, response, next) => {
576+
if (response.headersSent) return next(error)
577+
t.matchSnapshot(snapshotError(error), 'Middleware throws.')
578+
response.send()
579+
})
580+
581+
const port = await startServer(t, app)
582+
583+
await sendRequest(t, port)
584+
})
585+
})
586+
540587
t.test('Handles unconsumed uploads.', async t => {
541588
const sendRequest = async port => {
542589
const body = new FormData()

tap-snapshots/lib-test-TAP.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,26 @@ exports[`lib/test TAP Invalid ‘map’ entry array item type. Koa middleware. >
347347
}
348348
`
349349

350+
exports[`lib/test TAP Invalid ‘map’ entry object path. Express middleware. > Middleware throws. 1`] = `
351+
{
352+
"name": "BadRequestError",
353+
"message": "Invalid object path for the ‘map’ multipart field entry key ‘1’ array index ‘0’ value ‘variables.file’ (https://github.com/jaydenseric/graphql-multipart-request-spec).",
354+
"status": 400,
355+
"statusCode": 400,
356+
"expose": true
357+
}
358+
`
359+
360+
exports[`lib/test TAP Invalid ‘map’ entry object path. Koa middleware. > Middleware throws. 1`] = `
361+
{
362+
"name": "BadRequestError",
363+
"message": "Invalid object path for the ‘map’ multipart field entry key ‘1’ array index ‘0’ value ‘variables.file’ (https://github.com/jaydenseric/graphql-multipart-request-spec).",
364+
"status": 400,
365+
"statusCode": 400,
366+
"expose": true
367+
}
368+
`
369+
350370
exports[`lib/test TAP Invalid ‘map’ entry type. Express middleware. > Middleware throws. 1`] = `
351371
{
352372
"name": "BadRequestError",

0 commit comments

Comments
 (0)