From 164fcc25f3dd0059cd630a6967d3fe199277d8f6 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Sun, 16 Apr 2023 21:46:50 +1000 Subject: [PATCH 1/3] Fix body-parser errors not being filtered in ErrorHandler middleware --- patches/body-parser+1.20.1.patch | 11 ++++++++++- 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`, From 9e6a465f6687f6069db5effd00873e1bdaf35327 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Tue, 18 Apr 2023 21:22:12 +1000 Subject: [PATCH 2/3] update schemas/openapi --- assets/openapi.json | Bin 253823 -> 255370 bytes assets/schemas.json | Bin 1549271 -> 1559711 bytes 2 files changed, 0 insertions(+), 0 deletions(-) diff --git a/assets/openapi.json b/assets/openapi.json index 4e4d50a8b1d71770b655e2cfa774a45d66990369..eff3df07cf97aeb024fcc3c0f9dfe273a76e58e5 100644 GIT binary patch delta 121 zcmV-<0EYknz7LAa4}i1*LU5C)fxZ~^HblR#Dm|k#*HFWa2m+aH$E?^Pf{I*Lc4lcZT-OGFgZ~G<>#!azs z$?5mgnb@~2XDmrX5#rh|%*kYq&;?TVVw#J9;PimWOl;HDS2Jo(|FDu#Z2E*AA)D{w5 z<=(FJm*s0RLJ3f;B>}|U4h(}6>DxENFwNqENr!Fk(_&i`f+`rgy|tcAJQQ8<;}y2q z2zO2wn8MDpUDt%&56L+|F&l)E?GHb(Ya?8@ePIB{oGh5lq1!L*<_JdO?o#7K_UN(} zP75TXiXU@kBNbghzqD#AD0Zu2ii1Tk2FowdNROt_xyGnWboAWYqE z$1E6!NUhrqQw3`g`nNkD6}$=Y8z@U`->54ji=;QXTj&}R_uNY%WdwIRkjt^X&s{hM zA-27Ksc-=jw}n}x7{LX*c0!^^7?K034~js-3mjM51?5FM5S|5U;NEW8AUXvRaFe$= zNpG*cFRH+}-GGNlDjMPMXbUkHCNx1%vSizyJW1>ohH~yVV$U!H*Sm_(fV DAtUVL delta 1888 zcmai!OK1~O6o#3}WL`;=Op|m3iH3Gj0Qlni2+LdT=(oIcj&7-E6q(`NKQdEd*Nr;y++X7i0XWg|oa|fbWR4uceMIj6gGD~MMDcPT5 z1(;r7E}v@^B12wekDxi4E?eXWBOe3*mMD#2R-nFOAEC9t(H=_y%>jWKOB&aTo0ege zlKLIa3XKpq8AD6cLn>_y-tKTa=P?5_$H1tyIEsZp-wSI#MT#L#gXKf~{WInX(Eu@IFC(tk7igx*g$5;jb$J@u$Hf{ul(Oqt6U17FWNE#m0y1ut@=~mQlZ9+&! My+v)ce_f6J2M%P%bN~PV From 1d19db52ebe86d3fcbb8f3837407100e5c15ad73 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Tue, 18 Apr 2023 21:22:55 +1000 Subject: [PATCH 3/3] REALLY bad server testing --- package.json | 1 + scripts/test.js | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 scripts/test.js diff --git a/package.json b/package.json index 16f3c87f..a6017c8a 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "start:cdn": "node dist/cdn/start.js", "start:gateway": "node dist/gateway/start.js", "build": "tsc -p .", + "test": "node scripts/test.js", "lint": "eslint .", "setup": "npm run build && npm run generate:schema", "sync:db": "npm run build && node scripts/syncronise.js", diff --git a/scripts/test.js b/scripts/test.js new file mode 100644 index 00000000..28ac3778 --- /dev/null +++ b/scripts/test.js @@ -0,0 +1,50 @@ +/* + Spacebar: A FOSS re-implementation and extension of the Discord.com backend. + Copyright (C) 2023 Spacebar and Spacebar Contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +/* + Super simple script to check if the server starts at all, for use in gh actions. + Not a proper test framework by any means. +*/ + +const { spawn } = require("child_process"); +const path = require("path"); + +const server = spawn("node", [ + path.join(__dirname, "..", "dist", "bundle", "start.js"), +]); + +server.stdout.on("data", (data) => { + process.stdout.write(data); + + if (data.toString().toLowerCase().includes("listening")) { + // we good :) + console.log("we good"); + process.exit(); + } +}); + +server.stderr.on("data", (err) => { + process.stdout.write(err); + // we bad :( + process.kill(1); +}); + +server.on("close", (code) => { + console.log("closed with code", code); + process.exit(code); +});