Skip to content

Commit 8199622

Browse files
committed
Ignore user input that is just alphanumeric
1 parent 00bae33 commit 8199622

File tree

5 files changed

+6
-66
lines changed

5 files changed

+6
-66
lines changed

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as t from "tap";
22
import {
33
SQL_DANGEROUS_IN_STRING,
4-
COMMON_SQL_KEYWORDS,
54
SQL_ESCAPE_SEQUENCES,
65
SQL_KEYWORDS,
76
SQL_OPERATORS,
@@ -20,18 +19,6 @@ t.test("SQL_KEYWORDS are uppercase", async () => {
2019
});
2120
});
2221

23-
t.test("COMMON_SQL_KEYWORDS are not empty", async () => {
24-
COMMON_SQL_KEYWORDS.forEach((keyword) => {
25-
t.ok(keyword.length > 0);
26-
});
27-
});
28-
29-
t.test("COMMON_SQL_KEYWORDS are uppercase", async () => {
30-
COMMON_SQL_KEYWORDS.forEach((keyword) => {
31-
t.same(keyword, keyword.toUpperCase());
32-
});
33-
});
34-
3522
t.test("SQL_OPERATORS are not empty", async () => {
3623
SQL_OPERATORS.forEach((operator) => {
3724
t.ok(operator.length > 0);

library/vulnerabilities/sql-injection/config.ts

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -75,49 +75,6 @@ export const SQL_KEYWORDS = [
7575
"IS",
7676
];
7777

78-
// This is a list of common SQL keywords that are not dangerous by themselves
79-
// They will appear in almost any SQL query
80-
// e.g. SELECT * FROM table WHERE column = 'value' LIMIT 1
81-
// If a query parameter is ?LIMIT=1 it would be blocked
82-
// If the body contains "LIMIT" or "SELECT" it would be blocked
83-
export const COMMON_SQL_KEYWORDS = [
84-
"SELECT",
85-
"INSERT",
86-
"FROM",
87-
"WHERE",
88-
"DELETE",
89-
"GROUP",
90-
"BY",
91-
"ORDER",
92-
"LIMIT",
93-
"OFFSET",
94-
"HAVING",
95-
"COUNT",
96-
"SUM",
97-
"AVG",
98-
"MIN",
99-
"MAX",
100-
"DISTINCT",
101-
"AS",
102-
"AND",
103-
"OR",
104-
"NOT",
105-
"IN",
106-
"LIKE",
107-
"BETWEEN",
108-
"IS",
109-
"NULL",
110-
"ALL",
111-
"ANY",
112-
"EXISTS",
113-
"UNIQUE",
114-
"UPDATE",
115-
"INTO",
116-
"KEY",
117-
"VALUES",
118-
"VIEW",
119-
];
120-
12178
export const SQL_OPERATORS = [
12279
"=",
12380
"!",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { detectSQLInjection } from "./detectSQLInjection";
33
import { SQLDialectSQLite } from "./dialects/SQLDialectSQLite";
44

55
t.test("It flags the VACUUM command as SQL injection", async () => {
6-
isSqlInjection("VACUUM;", "VACUUM");
6+
isNotSQLInjection("VACUUM;", "VACUUM");
77
});
88

99
t.test("It flags the ATTACH command as SQL injection", async () => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ const IS_NOT_INJECTION = [
5858
["SELECT * FROM table", "*"],
5959
[`"COPY/*"`, "COPY/*"], // String encapsulated but dangerous chars
6060
[`'union' is not "UNION--"`, "UNION--"], // String encapsulated but dangerous chars
61+
[`'union' is not UNION`, "UNION"], // String not always encapsulated
6162
];
6263

6364
const IS_INJECTION = [
64-
[`'union' is not UNION`, "UNION"], // String not always encapsulated
6565
[`UNTER;`, "UNTER;"], // String not encapsulated and dangerous char (;)
6666
];
6767

library/vulnerabilities/sql-injection/userInputContainsSQLSyntax.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import { escapeStringRegexp } from "../../helpers/escapeStringRegexp";
2-
import {
3-
COMMON_SQL_KEYWORDS,
4-
SQL_DANGEROUS_IN_STRING,
5-
SQL_KEYWORDS,
6-
SQL_OPERATORS,
7-
} from "./config";
2+
import { SQL_DANGEROUS_IN_STRING, SQL_KEYWORDS, SQL_OPERATORS } from "./config";
83
import { SQLDialect } from "./dialects/SQLDialect";
94

105
const cachedRegexes = new Map<string, RegExp>();
6+
const alphaNumeric = /^[a-z0-9]+$/i;
117

128
/**
139
* This function is the first check in order to determine if a SQL injection is happening,
@@ -21,8 +17,8 @@ export function userInputContainsSQLSyntax(
2117
// e.g. SELECT * FROM table WHERE column = 'value' LIMIT 1
2218
// If a query parameter is ?LIMIT=1 it would be blocked
2319
// If the body contains "LIMIT" or "SELECT" it would be blocked
24-
// These are common SQL keywords and appear in almost any SQL query
25-
if (COMMON_SQL_KEYWORDS.includes(userInput.toUpperCase())) {
20+
// If the user input is just alphanumeric, we can safely ignore it
21+
if (alphaNumeric.test(userInput)) {
2622
return false;
2723
}
2824

0 commit comments

Comments
 (0)