-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from AikidoSec/patch-body-headers
Send body and headers
- Loading branch information
Showing
8 changed files
with
124 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export type PathPart = | ||
| { type: "jwt" } | ||
| { type: "object"; key: string } | ||
| { type: "array"; index: number }; | ||
|
||
export function buildPathToPayload(pathToPayload: PathPart[]): string { | ||
if (pathToPayload.length === 0) { | ||
return "."; | ||
} | ||
|
||
return pathToPayload.reduce((acc, part) => { | ||
if (part.type === "jwt") { | ||
return `${acc}<jwt>`; | ||
} | ||
|
||
if (part.type === "object") { | ||
return `${acc}.${part.key}`; | ||
} | ||
|
||
if (part.type === "array") { | ||
return `${acc}.[${part.index}]`; | ||
} | ||
|
||
return acc; | ||
}, ""); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as t from "tap"; | ||
import { convertRequestBodyToString } from "./convertRequestBodyToString"; | ||
|
||
t.test("it converts object body to JSON string", async (t) => { | ||
t.same( | ||
convertRequestBodyToString({ a: 1, b: 2, c: 3 }), | ||
JSON.stringify({ a: 1, b: 2, c: 3 }, null, 2) | ||
); | ||
}); | ||
|
||
t.test("it converts string body to string", async (t) => { | ||
t.same(convertRequestBodyToString("hello"), "hello"); | ||
}); | ||
|
||
t.test("it returns undefined for non-plain object", async (t) => { | ||
t.same(convertRequestBodyToString(new Date()), undefined); | ||
}); | ||
|
||
t.test("it limits length to maxLength", async (t) => { | ||
t.same(convertRequestBodyToString("a".repeat(16385)), "a".repeat(16384)); | ||
}); | ||
|
||
t.test("it returns undefined for circular object", async (t) => { | ||
const obj: Record<string, unknown> = {}; | ||
obj.a = obj; | ||
|
||
t.same(convertRequestBodyToString(obj), undefined); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { isPlainObject } from "./isPlainObject"; | ||
|
||
export function convertRequestBodyToString( | ||
body: unknown, | ||
maxLength = 16384 | ||
): string | undefined { | ||
if (typeof body === "string") { | ||
return body.length > maxLength ? body.slice(0, maxLength) : body; | ||
} | ||
|
||
if (isPlainObject(body)) { | ||
try { | ||
const serialized = JSON.stringify(body, null, 2); | ||
|
||
return convertRequestBodyToString(serialized, maxLength); | ||
} catch { | ||
return undefined; | ||
} | ||
} | ||
|
||
return undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as t from "tap"; | ||
import { filterEmptyRequestHeaders } from "./filterEmptyRequestHeaders"; | ||
|
||
t.test("it filters empty headers", async (t) => { | ||
t.same( | ||
filterEmptyRequestHeaders({ | ||
string: "value", | ||
array: ["a", "b"], | ||
emptyArray: [], | ||
undefined: undefined, | ||
}), | ||
{ | ||
string: "value", | ||
array: ["a", "b"], | ||
} | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Context } from "../agent/Context"; | ||
|
||
export function filterEmptyRequestHeaders( | ||
headers: Context["headers"] | ||
): Record<string, string | string[]> { | ||
const normalized: Record<string, string | string[]> = {}; | ||
for (const key in headers) { | ||
const value = headers[key]; | ||
|
||
if (Array.isArray(value) && value.length > 0) { | ||
normalized[key] = value; | ||
} | ||
|
||
if (typeof value === "string" && value.length > 0) { | ||
normalized[key] = value; | ||
} | ||
} | ||
|
||
return normalized; | ||
} |
32 changes: 3 additions & 29 deletions
32
library/src/vulnerabilities/nosql-injection/detectNoSQLInjection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters