Skip to content

Commit 5565ef3

Browse files
authored
Use json reviver in express.json() middleware. (#913)
1 parent 5b445d2 commit 5565ef3

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/utils.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -515,10 +515,18 @@ function patchMatrixClientForRetry() {
515515
let isMatrixClientPatchedForPrototypePollution = false;
516516

517517
export function jsonReviver<T = unknown>(key: string, value: T): T | undefined {
518-
if (key === "__proto__" || key === "constructor") {
519-
return undefined;
520-
} else {
521-
return value;
518+
switch (key) {
519+
case "__proto__":
520+
case "constructor":
521+
case "prototype":
522+
case "toString":
523+
case "valueOf":
524+
case "hasOwnProperty":
525+
case "__defineGetter__":
526+
case "__defineSetter__":
527+
return undefined;
528+
default:
529+
return value;
522530
}
523531
}
524532

src/webapis/WebAPIs.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
} from "@the-draupnir-project/matrix-basic-types";
2222
import { Logger, Task } from "matrix-protection-suite";
2323
import { SynapseHttpAntispam } from "./SynapseHTTPAntispam/SynapseHttpAntispam";
24+
import { jsonReviver } from "../utils";
2425

2526
const log = new Logger("WebAPIs");
2627

@@ -41,7 +42,7 @@ export class WebAPIs {
4142
private readonly synapseHTTPAntispam: SynapseHttpAntispam | undefined
4243
) {
4344
// Setup JSON parsing.
44-
this.webController.use(express.json());
45+
this.webController.use(express.json({ reviver: jsonReviver }));
4546
this.synapseHTTPAntispam?.register(this.webController);
4647
}
4748

0 commit comments

Comments
 (0)