-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathextractStringsFromUserInput.ts
More file actions
57 lines (49 loc) · 1.77 KB
/
extractStringsFromUserInput.ts
File metadata and controls
57 lines (49 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { isPlainObject } from "./isPlainObject";
import { safeDecodeURIComponent } from "./safeDecodeURIComponent";
import { tryDecodeAsJWT } from "./tryDecodeAsJWT";
type UserString = string;
export function extractStringsFromUserInput(obj: unknown): Set<UserString> {
const results: Set<UserString> = new Set();
if (isPlainObject(obj)) {
for (const key in obj) {
results.add(key);
extractStringsFromUserInput(obj[key]).forEach((value) => {
results.add(value);
});
}
}
if (Array.isArray(obj)) {
for (let i = 0; i < obj.length; i++) {
extractStringsFromUserInput(obj[i]).forEach((value) =>
results.add(value)
);
}
// Add array as string to results
// This prevents bypassing the firewall by HTTP Parameter Pollution
// Example: ?param=value1¶m=value2 will be treated as array by express
// If its used inside a string, it will be converted to a comma separated string
results.add(obj.join());
}
if (typeof obj == "string") {
results.add(obj);
if (obj.includes("%") && obj.length >= 3) {
const r = safeDecodeURIComponent(obj);
if (r && r !== obj) {
// Only add if the decoded value is different from the original, to avoid duplicates in results
// This improves the performance of all injection tests
results.add(r);
}
}
const jwt = tryDecodeAsJWT(obj);
if (jwt.jwt) {
// Do not add the issuer of the JWT as a string because it can contain a domain / url and produce false positives
if (jwt.object && typeof jwt.object === "object" && "iss" in jwt.object) {
delete jwt.object.iss;
}
extractStringsFromUserInput(jwt.object).forEach((value) => {
results.add(value);
});
}
}
return results;
}