-
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 #137 from AikidoSec/AIK-2617
Aik 2617
- Loading branch information
Showing
18 changed files
with
641 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,7 @@ generated_docs | |
# Credentials | ||
.env | ||
credentials.json | ||
|
||
# Test files | ||
library/test.txt | ||
library/test2.txt |
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,116 @@ | ||
const t = require("tap"); | ||
const { spawn } = require("child_process"); | ||
const { resolve } = require("path"); | ||
const timeout = require("../timeout"); | ||
|
||
const pathToApp = resolve( | ||
__dirname, | ||
"../../sample-apps/express-path-traversal", | ||
"app.js" | ||
); | ||
|
||
t.test("it blocks in blocking mode", (t) => { | ||
const server = spawn(`node`, [pathToApp, "4000"], { | ||
env: { ...process.env, AIKIDO_DEBUG: "true", AIKIDO_BLOCKING: "true" }, | ||
}); | ||
|
||
server.on("close", () => { | ||
t.end(); | ||
}); | ||
|
||
server.on("error", (err) => { | ||
t.fail(err.message); | ||
}); | ||
|
||
let stdout = ""; | ||
server.stdout.on("data", (data) => { | ||
stdout += data.toString(); | ||
}); | ||
|
||
let stderr = ""; | ||
server.stderr.on("data", (data) => { | ||
stderr += data.toString(); | ||
}); | ||
|
||
// Wait for the server to start | ||
timeout(2000) | ||
.then(() => { | ||
return Promise.all([ | ||
fetch( | ||
"http://localhost:4000/?content=blablabla&filename=/../TestDoc.txt", | ||
{ | ||
signal: AbortSignal.timeout(5000), | ||
} | ||
), | ||
fetch( | ||
"http://localhost:4000/?content=blablabla&filename=/TestDoc.txt", | ||
{ | ||
signal: AbortSignal.timeout(5000), | ||
} | ||
), | ||
]); | ||
}) | ||
.then(([pathTraversal, normalSearch]) => { | ||
t.equal(pathTraversal.status, 500); | ||
t.equal(normalSearch.status, 200); | ||
t.match(stdout, /Starting agent/); | ||
t.match(stderr, /Aikido runtime has blocked a Path traversal/); | ||
}) | ||
.catch((error) => { | ||
t.fail(error.message); | ||
}) | ||
.finally(() => { | ||
server.kill(); | ||
}); | ||
}); | ||
|
||
t.test("it does not block in dry mode", (t) => { | ||
const server = spawn(`node`, [pathToApp, "4001"], { | ||
env: { ...process.env, AIKIDO_DEBUG: "true" }, | ||
}); | ||
|
||
server.on("close", () => { | ||
t.end(); | ||
}); | ||
|
||
let stdout = ""; | ||
server.stdout.on("data", (data) => { | ||
stdout += data.toString(); | ||
}); | ||
|
||
let stderr = ""; | ||
server.stderr.on("data", (data) => { | ||
stderr += data.toString(); | ||
}); | ||
|
||
// Wait for the server to start | ||
timeout(2000) | ||
.then(() => | ||
Promise.all([ | ||
fetch( | ||
"http://localhost:4001/?content=blablabla&filename=/../TestDoc.txt", | ||
{ | ||
signal: AbortSignal.timeout(5000), | ||
} | ||
), | ||
fetch( | ||
"http://localhost:4001/?content=blablabla&filename=/TestDoc.txt", | ||
{ | ||
signal: AbortSignal.timeout(5000), | ||
} | ||
), | ||
]) | ||
) | ||
.then(([pathTraversal, normalSearch]) => { | ||
t.equal(pathTraversal.status, 200); | ||
t.equal(normalSearch.status, 200); | ||
t.match(stdout, /Starting agent/); | ||
t.notMatch(stderr, /Aikido runtime has blocked a Path traversal/); | ||
}) | ||
.catch((error) => { | ||
t.fail(error.message); | ||
}) | ||
.finally(() => { | ||
server.kill(); | ||
}); | ||
}); |
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,126 @@ | ||
import * as t from "tap"; | ||
import { Agent } from "../agent/Agent"; | ||
import { APIForTesting } from "../agent/api/APIForTesting"; | ||
import { Context, runWithContext } from "../agent/Context"; | ||
import { LoggerNoop } from "../agent/logger/LoggerNoop"; | ||
import { FileSystem } from "./FileSystem"; | ||
|
||
const unsafeContext: Context = { | ||
remoteAddress: "::1", | ||
method: "POST", | ||
url: "http://localhost:4000", | ||
query: {}, | ||
headers: {}, | ||
body: { | ||
file: { | ||
matches: "../test.txt", | ||
}, | ||
}, | ||
cookies: {}, | ||
source: "express", | ||
}; | ||
|
||
function throws(fn: () => void, wanted: string | RegExp) { | ||
const error = t.throws(fn); | ||
if (error instanceof Error) { | ||
t.match(error.message, wanted); | ||
} | ||
} | ||
|
||
t.test("it works", async (t) => { | ||
const agent = new Agent( | ||
true, | ||
new LoggerNoop(), | ||
new APIForTesting(), | ||
undefined, | ||
"lambda" | ||
); | ||
|
||
agent.start([new FileSystem()]); | ||
|
||
const { writeFile, writeFileSync, rename } = require("fs"); | ||
const { writeFile: writeFilePromise } = require("fs/promises"); | ||
|
||
const runCommandsWithInvalidArgs = () => { | ||
throws(() => writeFile(), /Received undefined/); | ||
throws(() => writeFileSync(), /Received undefined/); | ||
}; | ||
|
||
runCommandsWithInvalidArgs(); | ||
|
||
runWithContext(unsafeContext, () => { | ||
runCommandsWithInvalidArgs(); | ||
}); | ||
|
||
const runSafeCommands = async () => { | ||
writeFile( | ||
"./test.txt", | ||
"some file content to test with", | ||
{ encoding: "utf-8" }, | ||
(err) => {} | ||
); | ||
writeFileSync("./test.txt", "some other file content to test with", { | ||
encoding: "utf-8", | ||
}); | ||
await writeFilePromise( | ||
"./test.txt", | ||
"some other file content to test with", | ||
{ encoding: "utf-8" } | ||
); | ||
rename("./test.txt", "./test2.txt", (err) => {}); | ||
}; | ||
|
||
await runSafeCommands(); | ||
|
||
await runWithContext(unsafeContext, async () => { | ||
await runSafeCommands(); | ||
}); | ||
|
||
await runWithContext(unsafeContext, async () => { | ||
throws( | ||
() => | ||
writeFile( | ||
"../../test.txt", | ||
"some file content to test with", | ||
{ encoding: "utf-8" }, | ||
(err) => {} | ||
), | ||
"Aikido runtime has blocked a Path traversal: fs.writeFile(...) originating from body.file.matches" | ||
); | ||
|
||
throws( | ||
() => | ||
writeFileSync( | ||
"../../test.txt", | ||
"some other file content to test with", | ||
{ encoding: "utf-8" } | ||
), | ||
"Aikido runtime has blocked a Path traversal: fs.writeFileSync(...) originating from body.file.matches" | ||
); | ||
|
||
const error = await t.rejects(() => | ||
writeFilePromise( | ||
"../../test.txt", | ||
"some other file content to test with", | ||
{ encoding: "utf-8" } | ||
) | ||
); | ||
|
||
if (error instanceof Error) { | ||
t.match( | ||
error.message, | ||
"Aikido runtime has blocked a Path traversal: fs.writeFile(...) originating from body.file.matches" | ||
); | ||
} | ||
|
||
throws( | ||
() => rename("../../test.txt", "./test2.txt", (err) => {}), | ||
"Aikido runtime has blocked a Path traversal: fs.rename(...) originating from body.file.matches" | ||
); | ||
|
||
throws( | ||
() => rename("./test.txt", "../../test.txt", (err) => {}), | ||
"Aikido runtime has blocked a Path traversal: fs.rename(...) originating from body.file.matches" | ||
); | ||
}); | ||
}); |
Oops, something went wrong.