-
Notifications
You must be signed in to change notification settings - Fork 39
Add WAF rule engine prototype using wirefilter #968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
hansott
wants to merge
1
commit into
main
Choose a base branch
from
waf-wirefilter-prototype
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,136 @@ | ||
| import { spawn } from "child_process"; | ||
| import { resolve } from "path"; | ||
| import { test } from "node:test"; | ||
| import { equal } from "node:assert"; | ||
| import { getRandomPort } from "./utils/get-port.mjs"; | ||
| import { timeout } from "./utils/timeout.mjs"; | ||
|
|
||
| const pathToAppDir = resolve( | ||
| import.meta.dirname, | ||
| "../../sample-apps/hono-xml" | ||
| ); | ||
|
|
||
| const testServerUrl = "http://localhost:5874"; | ||
|
|
||
| test("it blocks requests matching WAF path rule", async () => { | ||
| const port = await getRandomPort(); | ||
|
|
||
| const response = await fetch(`${testServerUrl}/api/runtime/apps`, { | ||
| method: "POST", | ||
| }); | ||
| const body = await response.json(); | ||
| const token = body.token; | ||
|
|
||
| const configResp = await fetch(`${testServerUrl}/api/runtime/config`, { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| Authorization: token, | ||
| }, | ||
| body: JSON.stringify({ | ||
| wafRules: [ | ||
| { | ||
| id: "block-admin", | ||
| expression: 'http.request.uri.path contains "/admin"', | ||
| action: "block", | ||
| }, | ||
| ], | ||
| }), | ||
| }); | ||
| equal(configResp.status, 200); | ||
|
|
||
| const server = spawn(`node`, ["./app.js", port], { | ||
| cwd: pathToAppDir, | ||
| env: { | ||
| ...process.env, | ||
| AIKIDO_DEBUG: "true", | ||
| AIKIDO_BLOCKING: "true", | ||
| AIKIDO_TOKEN: token, | ||
| AIKIDO_ENDPOINT: testServerUrl, | ||
| AIKIDO_REALTIME_ENDPOINT: testServerUrl, | ||
| }, | ||
| }); | ||
|
|
||
| try { | ||
| await timeout(2000); | ||
|
|
||
| const blocked = await fetch(`http://127.0.0.1:${port}/admin`, { | ||
| headers: { "X-Forwarded-For": "1.2.3.4" }, | ||
| signal: AbortSignal.timeout(5000), | ||
| }); | ||
| equal(blocked.status, 403); | ||
| equal(await blocked.text(), "You are blocked by Zen."); | ||
|
|
||
| const allowed = await fetch(`http://127.0.0.1:${port}/`, { | ||
| headers: { "X-Forwarded-For": "1.2.3.4" }, | ||
| signal: AbortSignal.timeout(5000), | ||
| }); | ||
| equal(allowed.status, 200); | ||
| } finally { | ||
| server.kill(); | ||
| } | ||
| }); | ||
|
|
||
| test("it blocks requests matching user agent WAF rule", async () => { | ||
| const port = await getRandomPort(); | ||
|
|
||
| const response = await fetch(`${testServerUrl}/api/runtime/apps`, { | ||
| method: "POST", | ||
| }); | ||
| const body = await response.json(); | ||
| const token = body.token; | ||
|
|
||
| const configResp = await fetch(`${testServerUrl}/api/runtime/config`, { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| Authorization: token, | ||
| }, | ||
| body: JSON.stringify({ | ||
| wafRules: [ | ||
| { | ||
| id: "block-scanners", | ||
| expression: 'http.user_agent matches "(?i)(sqlmap|nikto)"', | ||
| action: "block", | ||
| }, | ||
| ], | ||
| }), | ||
| }); | ||
| equal(configResp.status, 200); | ||
|
|
||
| const server = spawn(`node`, ["./app.js", port], { | ||
| cwd: pathToAppDir, | ||
| env: { | ||
| ...process.env, | ||
| AIKIDO_DEBUG: "true", | ||
| AIKIDO_BLOCKING: "true", | ||
| AIKIDO_TOKEN: token, | ||
| AIKIDO_ENDPOINT: testServerUrl, | ||
| AIKIDO_REALTIME_ENDPOINT: testServerUrl, | ||
| }, | ||
| }); | ||
|
|
||
| try { | ||
| await timeout(2000); | ||
|
|
||
| const blocked = await fetch(`http://127.0.0.1:${port}/`, { | ||
| headers: { | ||
| "User-Agent": "sqlmap/1.0", | ||
| "X-Forwarded-For": "1.2.3.4", | ||
| }, | ||
| signal: AbortSignal.timeout(5000), | ||
| }); | ||
| equal(blocked.status, 403); | ||
|
|
||
| const allowed = await fetch(`http://127.0.0.1:${port}/`, { | ||
| headers: { | ||
| "User-Agent": "Mozilla/5.0", | ||
| "X-Forwarded-For": "1.2.3.4", | ||
| }, | ||
| signal: AbortSignal.timeout(5000), | ||
| }); | ||
| equal(allowed.status, 200); | ||
| } finally { | ||
| server.kill(); | ||
| } | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,15 @@ | ||
| export type WafRuleAction = "block"; | ||
|
|
||
| export type WafRule = { | ||
| id: string; | ||
| expression: string; | ||
| action?: WafRuleAction; | ||
| }; | ||
|
|
||
| export type WafSetRulesResult = | ||
| | { success: true } | ||
| | { success: false; error: string; rule_id?: string }; | ||
|
|
||
| export type WafEvaluateResult = | ||
| | { matched: false; error?: string } | ||
| | { matched: true; rule_id: string; action: string }; |
This file contains hidden or 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,59 @@ | ||
| import { | ||
| wasm_waf_set_rules, | ||
| wasm_waf_evaluate, | ||
| } from "../internals/zen_internals"; | ||
| import type { Context } from "../agent/Context"; | ||
| import type { WafRule, WafSetRulesResult, WafEvaluateResult } from "./WafRule"; | ||
|
|
||
| export function setWafRules(rules: WafRule[]): WafSetRulesResult { | ||
| return wasm_waf_set_rules(JSON.stringify(rules)); | ||
| } | ||
|
|
||
| export function evaluateWafRules(context: Context): WafEvaluateResult { | ||
| if (!context.remoteAddress) { | ||
| return { matched: false, error: "Missing remote address" }; | ||
| } | ||
|
|
||
| const url = context.url || "/"; | ||
| let path = url; | ||
| let query = ""; | ||
| const queryIndex = url.indexOf("?"); | ||
| if (queryIndex !== -1) { | ||
| path = url.substring(0, queryIndex); | ||
| query = url.substring(queryIndex); | ||
| } | ||
|
|
||
| const host = | ||
| (typeof context.headers?.host === "string" | ||
| ? context.headers.host | ||
| : undefined) || ""; | ||
|
|
||
| const requestData = { | ||
| host: host, | ||
| method: context.method || "GET", | ||
| path: path, | ||
| query: query, | ||
| uri: url, | ||
| full_uri: `${host}${url}`, | ||
| user_agent: | ||
| typeof context.headers?.["user-agent"] === "string" | ||
| ? context.headers["user-agent"] | ||
| : undefined, | ||
| cookie: | ||
| typeof context.headers?.cookie === "string" | ||
| ? context.headers.cookie | ||
| : undefined, | ||
| referer: | ||
| typeof context.headers?.referer === "string" | ||
| ? context.headers.referer | ||
| : undefined, | ||
| x_forwarded_for: | ||
| typeof context.headers?.["x-forwarded-for"] === "string" | ||
| ? context.headers["x-forwarded-for"] | ||
| : undefined, | ||
| body: typeof context.body === "string" ? context.body : undefined, | ||
| ip_src: context.remoteAddress, | ||
| }; | ||
|
|
||
| return wasm_waf_evaluate(JSON.stringify(requestData)); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Header extraction for user-agent, cookie, referer and x-forwarded-for repeats the same ternary logic; extract a small helper (e.g., getHeaderString(headers, key)) to avoid duplication.
Details
✨ AI Reasoning
Multiple header fields are extracted using the same ternary pattern and repeated inline: this repeats identical logic (check typeof header key is string ? use it : undefined) for user-agent, cookie, referer, and x-forwarded-for. Consolidating into a small helper would reduce repetitive code and the chance of inconsistent changes when adding more headers.
🔧 How do I fix it?
Delete extra code. Extract repeated code sequences into reusable functions or methods. Use loops or data structures to eliminate repetitive patterns.
Reply
@AikidoSec feedback: [FEEDBACK]to get better review comments in the future.Reply
@AikidoSec ignore: [REASON]to ignore this issue.More info