-
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.
- Loading branch information
Showing
4 changed files
with
54 additions
and
54 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
library/src/vulnerabilities/sql-injection/dangerousCharsInInput.ts
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,17 @@ | ||
import { SQL_DANGEROUS_IN_STRING } from "./config.json"; | ||
|
||
const dangerousInStringRegex = new RegExp( | ||
SQL_DANGEROUS_IN_STRING.join("|"), | ||
"im" | ||
); | ||
|
||
/** | ||
* This function is the second step to determine if an SQL Injection is happening, | ||
* If the user input contains characters that should never end up in a query, not | ||
* even in a string, this function returns true. | ||
* @param userInput The user input you want to check | ||
* @returns True if characters are present | ||
*/ | ||
export function dangerousCharsInInput(userInput: string): boolean { | ||
return dangerousInStringRegex.test(userInput); | ||
} |
2 changes: 1 addition & 1 deletion
2
library/src/vulnerabilities/sql-injection/detectSQLInjection.test.ts
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
32 changes: 32 additions & 0 deletions
32
library/src/vulnerabilities/sql-injection/userInputContainsSQLSyntax.ts
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,32 @@ | ||
import { SQL_KEYWORDS, SQL_OPERATORS } from "./config.json"; | ||
|
||
const matchSqlKeywords = | ||
"(?<![a-z])(" + // Lookbehind : if the keywords are preceded by one or more letters, it should not match | ||
SQL_KEYWORDS.join("|") + // Look for SQL Keywords | ||
")(?![a-z])"; // Lookahead : if the keywords are followed by one or more letters, it should not match | ||
|
||
const matchSqlOperators = `(${SQL_OPERATORS.join("|")})`; | ||
|
||
const matchSqlFunctions = | ||
"(?<=([\\s|.|" + // Lookbehind : A sql function should be preceded by spaces, dots, | ||
SQL_OPERATORS.join("|") + // Or sql operators | ||
"]|^)+)" + | ||
"([a-z0-9_-]+)" + // The name of a sql function can include letters, numbers, "_" and "-" | ||
"(?=[\\s]*\\()"; // Lookahead : A sql function should be followed by a "(" , spaces are allowed. | ||
|
||
const possibleSqlRegex = new RegExp( | ||
// Match one or more of : sql keywords, sql operators, sql functions | ||
`${matchSqlKeywords}|${matchSqlOperators}|${matchSqlFunctions}`, | ||
"im" | ||
); | ||
|
||
/** | ||
* This function is the first check in order to determine if a SQL injection is happening, | ||
* If the user input contains the necessary characters or words for a SQL injection, this | ||
* function returns true. | ||
* @param userInput The user input you want to check | ||
* @returns True when this is a possible SQL Injection | ||
*/ | ||
export function userInputContainsSQLSyntax(userInput: string): boolean { | ||
return possibleSqlRegex.test(userInput); | ||
} |