-
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 branch 'main' into clean-stacktraces
- Loading branch information
Showing
34 changed files
with
1,475 additions
and
107 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
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,129 @@ | ||
const t = require("tap"); | ||
const { spawn } = require("child_process"); | ||
const { resolve, join } = require("path"); | ||
const timeout = require("../timeout"); | ||
const { promisify } = require("util"); | ||
const { exec: execCb } = require("child_process"); | ||
|
||
const execAsync = promisify(execCb); | ||
|
||
const appDir = resolve(__dirname, "../../sample-apps/hono-prisma"); | ||
const pathToApp = join(appDir, "app.js"); | ||
|
||
process.env.DATABASE_URL = "file:./dev.db"; | ||
|
||
t.before(async (t) => { | ||
// Generate prismajs client | ||
const { stdout, stderr } = await execAsync( | ||
"npx prisma migrate reset --force", // Generate prisma client, reset db and apply migrations | ||
{ | ||
cwd: appDir, | ||
} | ||
); | ||
|
||
if (stderr) { | ||
t.fail(stderr); | ||
} | ||
}); | ||
|
||
t.test("it blocks in blocking mode", (t) => { | ||
const server = spawn(`node`, [pathToApp, "4002"], { | ||
env: { ...process.env, AIKIDO_DEBUG: "true", AIKIDO_BLOCKING: "true" }, | ||
}); | ||
|
||
server.on("close", () => { | ||
t.end(); | ||
}); | ||
|
||
server.on("error", (err) => { | ||
t.fail(err); | ||
}); | ||
|
||
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://127.0.0.1:4002/posts/Test" OR 1=1 -- C', { | ||
method: "GET", | ||
signal: AbortSignal.timeout(5000), | ||
}), | ||
fetch("http://127.0.0.1:4002/posts/Happy", { | ||
method: "GET", | ||
signal: AbortSignal.timeout(5000), | ||
}), | ||
]); | ||
}) | ||
.then(([sqlInjection, normalAdd]) => { | ||
t.equal(sqlInjection.status, 500); | ||
t.equal(normalAdd.status, 200); | ||
t.match(stdout, /Starting agent/); | ||
t.match(stderr, /Zen has blocked an SQL injection/); | ||
}) | ||
.catch((error) => { | ||
t.fail(error); | ||
}) | ||
.finally(() => { | ||
server.kill(); | ||
}); | ||
}); | ||
|
||
t.test("it does not block in non-blocking mode", (t) => { | ||
const server = spawn(`node`, [pathToApp, "4002"], { | ||
env: { ...process.env, AIKIDO_DEBUG: "true" }, | ||
}); | ||
|
||
server.on("close", () => { | ||
t.end(); | ||
}); | ||
|
||
server.on("error", (err) => { | ||
t.fail(err); | ||
}); | ||
|
||
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://127.0.0.1:4002/posts/Test" OR 1=1 -- C', { | ||
method: "GET", | ||
signal: AbortSignal.timeout(5000), | ||
}), | ||
fetch("http://127.0.0.1:4002/posts/Happy", { | ||
method: "GET", | ||
signal: AbortSignal.timeout(5000), | ||
}), | ||
]); | ||
}) | ||
.then(([sqlInjection, normalAdd]) => { | ||
t.equal(sqlInjection.status, 200); | ||
t.equal(normalAdd.status, 200); | ||
t.match(stdout, /Starting agent/); | ||
t.notMatch(stderr, /Zen has blocked an SQL injection/); | ||
}) | ||
.catch((error) => { | ||
t.fail(error); | ||
}) | ||
.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { resolve } from "path"; | ||
import { cleanupStackTrace } from "../../helpers/cleanupStackTrace"; | ||
import { escapeHTML } from "../../helpers/escapeHTML"; | ||
import type { Agent } from "../Agent"; | ||
import { attackKindHumanName } from "../Attack"; | ||
import { getContext, updateContext } from "../Context"; | ||
import type { InterceptorResult } from "./InterceptorResult"; | ||
import type { WrapPackageInfo } from "./WrapPackageInfo"; | ||
|
||
// Used for cleaning up the stack trace | ||
const libraryRoot = resolve(__dirname, "../.."); | ||
|
||
export function onInspectionInterceptorResult( | ||
context: ReturnType<typeof getContext>, | ||
agent: Agent, | ||
result: InterceptorResult, | ||
pkgInfo: WrapPackageInfo, | ||
start: number | ||
) { | ||
const end = performance.now(); | ||
agent.getInspectionStatistics().onInspectedCall({ | ||
sink: pkgInfo.name, | ||
attackDetected: !!result, | ||
blocked: agent.shouldBlock(), | ||
durationInMs: end - start, | ||
withoutContext: !context, | ||
}); | ||
|
||
const isAllowedIP = | ||
context && | ||
context.remoteAddress && | ||
agent.getConfig().isAllowedIP(context.remoteAddress); | ||
|
||
if (result && context && !isAllowedIP) { | ||
// Flag request as having an attack detected | ||
updateContext(context, "attackDetected", true); | ||
|
||
agent.onDetectedAttack({ | ||
module: pkgInfo.name, | ||
operation: result.operation, | ||
kind: result.kind, | ||
source: result.source, | ||
blocked: agent.shouldBlock(), | ||
stack: cleanupStackTrace(new Error().stack!, libraryRoot), | ||
paths: result.pathsToPayload, | ||
metadata: result.metadata, | ||
request: context, | ||
payload: result.payload, | ||
}); | ||
|
||
if (agent.shouldBlock()) { | ||
throw new Error( | ||
`Zen has blocked ${attackKindHumanName(result.kind)}: ${result.operation}(...) originating from ${result.source}${escapeHTML((result.pathsToPayload || []).join())}` | ||
); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.