-
Notifications
You must be signed in to change notification settings - Fork 13
Block SQL queries that fail tokenization #408
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
Open
hansott
wants to merge
8
commits into
main
Choose a base branch
from
block-invalid-sql-queries
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.
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4dd8d88
Block SQL queries that fail tokenization
hansott 38bf257
Add server tests for invalid SQL blocking (AIKIDO_BLOCK_INVALID_SQL) …
PopoviciMarian e66aa59
bump zen-internals version to 0.1.61
tudor-timcu e70837f
Refactor SQL injection detection constants to improve clarity and mai…
tudor-timcu 36ea90c
Enhance SQL injection detection logic to handle tokenization failures…
tudor-timcu 9a1f3d8
Update documentation for invalid SQL query blocking feature
tudor-timcu 5c6f42e
Update default behavior for invalid SQL query blocking feature
tudor-timcu 38080f8
Update default value for BlockInvalidSql in AikidoConfigData and rela…
tudor-timcu 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,8 @@ | ||
| # Blocking invalid SQL queries | ||
|
|
||
| Zen blocks SQL queries that it can't tokenize when they contain user input. This prevents attackers from bypassing SQL injection detection with malformed queries. For example, ClickHouse ignores invalid SQL after `;`, and SQLite runs queries before an unclosed `/*` comment. | ||
|
|
||
| This is on by default. In blocking mode, these queries are blocked. In detection-only mode, they are reported but still executed. | ||
|
|
||
| If you see false positives (legitimate queries being blocked), set the | ||
| `AIKIDO_BLOCK_INVALID_SQL` environment variable to `false`. |
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
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,6 @@ | ||
| { | ||
| "AIKIDO_BLOCK": "1", | ||
| "AIKIDO_BLOCK_INVALID_SQL": "1", | ||
| "AIKIDO_LOCALHOST_ALLOWED_BY_DEFAULT": "0", | ||
| "AIKIDO_FEATURE_COLLECT_API_SCHEMA": "1" | ||
| } |
31 changes: 31 additions & 0 deletions
31
tests/server/test_sql_injection_invalid_sql_blocked/expect_detection_blocked.json
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,31 @@ | ||
| { | ||
| "type": "detected_attack", | ||
| "request": { | ||
| "method": "GET", | ||
| "source": "php", | ||
| "route": "/testDetection" | ||
| }, | ||
| "attack": { | ||
| "kind": "sql_injection", | ||
| "operation": "pdo->query", | ||
| "module": "PDO", | ||
| "blocked": true, | ||
| "source": "query", | ||
| "path": ".id", | ||
| "stack": "tests/server/test_sql_injection_invalid_sql_blocked/index.php(18): PDO->query()", | ||
| "payload": "1 /*", | ||
| "metadata": { | ||
| "dialect": "sqlite", | ||
| "sql": "SELECT * FROM cats WHERE id = 1 /*", | ||
| "failedToTokenize": "true" | ||
| }, | ||
| "user": { | ||
| "id": "12345", | ||
| "name": "Test User" | ||
| } | ||
| }, | ||
| "agent": { | ||
| "dryMode": false, | ||
| "library": "firewall-php" | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
tests/server/test_sql_injection_invalid_sql_blocked/index.php
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,24 @@ | ||
| <?php | ||
|
|
||
| \aikido\set_user("12345", "Test User"); | ||
|
|
||
| try { | ||
| $pdo = new PDO('sqlite::memory:'); | ||
| $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
|
|
||
| $pdo->exec("CREATE TABLE IF NOT EXISTS cats ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| name TEXT NOT NULL, | ||
| age INTEGER NOT NULL | ||
| )"); | ||
|
|
||
| $pdo->exec("INSERT INTO cats (name, age) VALUES ('n', 1)"); | ||
|
|
||
| $id = $_GET['id']; | ||
| $pdo->query("SELECT * FROM cats WHERE id = $id"); | ||
|
|
||
| echo "Query executed!"; | ||
|
|
||
| } catch (PDOException $e) { | ||
| echo "Error: " . $e->getMessage(); | ||
| } |
10 changes: 10 additions & 0 deletions
10
tests/server/test_sql_injection_invalid_sql_blocked/start_config.json
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,10 @@ | ||
| { | ||
| "success": true, | ||
| "serviceId": 1, | ||
| "heartbeatIntervalInMS": 600000, | ||
| "endpoints": [], | ||
| "blockedUserIds": [], | ||
| "allowedIPAddresses": [], | ||
| "receivedAnyStats": true, | ||
| "block": true | ||
| } |
24 changes: 24 additions & 0 deletions
24
tests/server/test_sql_injection_invalid_sql_blocked/test.py
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,24 @@ | ||
| from testlib import * | ||
|
|
||
| """ | ||
| Invalid SQL (failed tokenization) with user input is blocked when AIKIDO_BLOCK=1 and | ||
| AIKIDO_BLOCK_INVALID_SQL=1. Uses PDO SQLite and an unclosed block comment in the query. | ||
| """ | ||
|
|
||
|
|
||
| def run_test(): | ||
| response = php_server_get("/testDetection?id=1+%2F*") | ||
| assert_response_code_is(response, 500) | ||
| assert_response_body_contains(response, "") | ||
|
|
||
| mock_server_wait_for_new_events(5) | ||
|
|
||
| events = mock_server_get_events() | ||
| assert_events_length_is(events, 2) | ||
| assert_started_event_is_valid(events[0]) | ||
| assert_event_contains_subset_file(events[1], "expect_detection_blocked.json") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| load_test_args() | ||
| run_test() |
6 changes: 6 additions & 0 deletions
6
tests/server/test_sql_injection_invalid_sql_not_blocked/env.json
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,6 @@ | ||
| { | ||
| "AIKIDO_BLOCK": "1", | ||
| "AIKIDO_BLOCK_INVALID_SQL": "0", | ||
| "AIKIDO_LOCALHOST_ALLOWED_BY_DEFAULT": "0", | ||
| "AIKIDO_FEATURE_COLLECT_API_SCHEMA": "1" | ||
| } |
24 changes: 24 additions & 0 deletions
24
tests/server/test_sql_injection_invalid_sql_not_blocked/index.php
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,24 @@ | ||
| <?php | ||
|
|
||
| \aikido\set_user("12345", "Test User"); | ||
|
|
||
| try { | ||
| $pdo = new PDO('sqlite::memory:'); | ||
| $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
|
|
||
| $pdo->exec("CREATE TABLE IF NOT EXISTS cats ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| name TEXT NOT NULL, | ||
| age INTEGER NOT NULL | ||
| )"); | ||
|
|
||
| $pdo->exec("INSERT INTO cats (name, age) VALUES ('n', 1)"); | ||
|
|
||
| $id = $_GET['id']; | ||
| $pdo->query("SELECT * FROM cats WHERE id = $id"); | ||
|
|
||
| echo "Query executed!"; | ||
|
|
||
| } catch (PDOException $e) { | ||
| echo "Error: " . $e->getMessage(); | ||
| } |
10 changes: 10 additions & 0 deletions
10
tests/server/test_sql_injection_invalid_sql_not_blocked/start_config.json
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,10 @@ | ||
| { | ||
| "success": true, | ||
| "serviceId": 1, | ||
| "heartbeatIntervalInMS": 600000, | ||
| "endpoints": [], | ||
| "blockedUserIds": [], | ||
| "allowedIPAddresses": [], | ||
| "receivedAnyStats": true, | ||
| "block": true | ||
| } |
19 changes: 19 additions & 0 deletions
19
tests/server/test_sql_injection_invalid_sql_not_blocked/test.py
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,19 @@ | ||
| import time | ||
| from testlib import * | ||
|
|
||
| """ | ||
| With AIKIDO_BLOCK=1 but AIKIDO_BLOCK_INVALID_SQL=0, queries that only fail tokenization | ||
| (result 3) are not blocked and no attack event is reported. | ||
| """ | ||
|
|
||
|
|
||
| def run_test(): | ||
| response = php_server_get("/testDetection?id=1+%2F*") | ||
| assert_response_code_is(response, 200) | ||
| assert_response_body_contains(response, "Error: SQLSTATE[HY000]") | ||
| assert_events_length_is(mock_server_get_events(), 1) # No attack event is reported. | ||
| assert_started_event_is_valid(mock_server_get_events()[0]) | ||
|
|
||
| if __name__ == "__main__": | ||
| load_test_args() | ||
| run_test() |
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.
Two near-identical InterceptorResult constructions were added; consolidate the repeated result-building to avoid duplicated logic.
Details
✨ AI Reasoning
The change added a new branch that returns an InterceptorResult with the same structure as the existing SQLInjectionDetected branch, differing only by one metadata key (failedToTokenize). Both branches build and return the same type with identical fields (Operation, Kind, Source, PathToPayload, Metadata contains sql and dialect, and Payload). This duplicates substantial, non-trivial logic in the same function and would require parallel updates if the result shape changes.
🔧 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