Skip to content

Commit 35e8941

Browse files
committed
Ignore full ai generated sql queries
1 parent 33c541f commit 35e8941

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

library/vulnerabilities/sql-injection/checkContextForSqlInjection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function checkContextForSqlInjection({
2828
}
2929

3030
for (const str of userInput) {
31-
if (detectSQLInjection(sql, str, dialect)) {
31+
if (detectSQLInjection(sql, str, dialect, source)) {
3232
return {
3333
operation: operation,
3434
kind: "sql_injection",

library/vulnerabilities/sql-injection/detectSQLInjection.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,59 @@ t.test("It does not match GROUP keyword", async () => {
259259
isNotSqlInjection(query, "ASC");
260260
});
261261

262+
t.test(
263+
"it ignores full SQL queries from the source aiToolParams",
264+
async (t) => {
265+
const generic = new SQLDialectGeneric();
266+
t.same(
267+
detectSQLInjection(
268+
"SELECT * FROM 'test';",
269+
"SELECT * FROM 'test';",
270+
generic,
271+
"body"
272+
),
273+
true
274+
);
275+
t.same(
276+
detectSQLInjection(
277+
"SELECT * FROM 'test';",
278+
"'test';",
279+
generic,
280+
"aiToolParams"
281+
),
282+
true
283+
);
284+
t.same(
285+
detectSQLInjection(
286+
"SELECT * FROM 'test'; DELETE FROM 'test'; -- ';",
287+
"test'; DELETE FROM 'test'; -- ';",
288+
generic,
289+
"aiToolParams"
290+
),
291+
true
292+
);
293+
294+
t.same(
295+
detectSQLInjection(
296+
"SELECT * FROM 'test';",
297+
"SELECT * FROM 'test';",
298+
generic,
299+
"aiToolParams"
300+
),
301+
false
302+
);
303+
t.same(
304+
detectSQLInjection(
305+
"DELETE FROM 'test';",
306+
"DELETE FROM 'test';",
307+
generic,
308+
"aiToolParams"
309+
),
310+
false
311+
);
312+
}
313+
);
314+
262315
const files = [
263316
// Taken from https://github.com/payloadbox/sql-injection-payload-list/tree/master
264317
join(__dirname, "payloads", "Auth_Bypass.txt"),

library/vulnerabilities/sql-injection/detectSQLInjection.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,29 @@ import { SQLDialect } from "./dialects/SQLDialect";
22
import { shouldReturnEarly } from "./shouldReturnEarly";
33
// eslint-disable-next-line camelcase
44
import { wasm_detect_sql_injection } from "../../internals/zen_internals";
5+
import type { Source } from "../../agent/Source";
56

67
export function detectSQLInjection(
78
query: string,
89
userInput: string,
9-
dialect: SQLDialect
10+
dialect: SQLDialect,
11+
source: Source | undefined = undefined
1012
) {
1113
if (shouldReturnEarly(query, userInput)) {
1214
return false;
1315
}
1416

17+
// Ignore full SQL queries from the source aiToolParams
18+
// This is to prevent false positives when the AI tool is generating SQL queries
19+
// It was already checked in shouldReturnEarly that the query includes user input
20+
if (
21+
source &&
22+
source === "aiToolParams" &&
23+
query.length === userInput.length
24+
) {
25+
return false;
26+
}
27+
1528
return wasm_detect_sql_injection(
1629
query.toLowerCase(),
1730
userInput.toLowerCase(),

0 commit comments

Comments
 (0)