diff options
author | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2023-04-16 21:46:50 +1000 |
---|---|---|
committer | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2023-04-16 21:46:50 +1000 |
commit | 164fcc25f3dd0059cd630a6967d3fe199277d8f6 (patch) | |
tree | 878916e9353f9b5b71cc71ee7c6eff30ea8d9fde | |
parent | fix style action (diff) | |
download | server-164fcc25f3dd0059cd630a6967d3fe199277d8f6.tar.xz |
Fix body-parser errors not being filtered in ErrorHandler middleware
-rw-r--r-- | patches/body-parser+1.20.1.patch | 11 | ||||
-rw-r--r-- | src/api/middlewares/ErrorHandler.ts | 7 |
2 files changed, 16 insertions, 2 deletions
diff --git a/patches/body-parser+1.20.1.patch b/patches/body-parser+1.20.1.patch index 054e24f7..11d1a9d1 100644 --- a/patches/body-parser+1.20.1.patch +++ b/patches/body-parser+1.20.1.patch @@ -1,5 +1,5 @@ diff --git a/node_modules/body-parser/lib/types/json.js b/node_modules/body-parser/lib/types/json.js -index c2745be..7104cfa 100644 +index c2745be..17c2cfe 100644 --- a/node_modules/body-parser/lib/types/json.js +++ b/node_modules/body-parser/lib/types/json.js @@ -18,6 +18,7 @@ var createError = require('http-errors') @@ -28,3 +28,12 @@ index c2745be..7104cfa 100644 } catch (e) { return normalizeJsonSyntaxError(e, { message: e.message.replace('#', char), +@@ -216,7 +217,7 @@ function normalizeJsonSyntaxError (error, obj) { + } + + // replace stack before message for Node.js 0.10 and below +- error.stack = obj.stack.replace(error.message, obj.message) ++ error.stack = obj.stack?.replace(error.message, obj.message) + error.message = obj.message + + return error diff --git a/src/api/middlewares/ErrorHandler.ts b/src/api/middlewares/ErrorHandler.ts index b8a73298..c417e64f 100644 --- a/src/api/middlewares/ErrorHandler.ts +++ b/src/api/middlewares/ErrorHandler.ts @@ -22,7 +22,7 @@ import { ApiError, FieldError } from "@spacebar/util"; const EntityNotFoundErrorRegex = /"(\w+)"/; export function ErrorHandler( - error: Error, + error: Error & { type?: string }, req: Request, res: Response, next: NextFunction, @@ -50,6 +50,11 @@ export function ErrorHandler( code = Number(error.code); message = error.message; errors = error.errors; + } else if (error?.type == "entity.parse.failed") { + // body-parser failed + httpcode = 400; + code = 50109; + message = "The request body contains invalid JSON."; } else { console.error( `[Error] ${code} ${req.url}\n`, |