This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
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.
Merge pull request #10 from webschik/tsr-detect-sql-literal-injection
Added tsr-detect-sql-literal-injection rule
- Loading branch information
Showing
8 changed files
with
156 additions
and
7 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
function createMainKeywordsPattern(keyword: string): RegExp { | ||
return new RegExp(`(^|\\s)(${keyword})`); | ||
} | ||
|
||
const selectKeyword: RegExp = createMainKeywordsPattern('SELECT'); | ||
const deleteKeyword: RegExp = createMainKeywordsPattern('DELETE'); | ||
const insertKeyword: RegExp = createMainKeywordsPattern('INSERT'); | ||
const updateKeyword: RegExp = createMainKeywordsPattern('UPDATE'); | ||
const dropKeyword: RegExp = createMainKeywordsPattern('DROP'); | ||
const createKeyword: RegExp = createMainKeywordsPattern('CREATE'); | ||
const alterKeyword: RegExp = createMainKeywordsPattern('ALTER'); | ||
|
||
/** | ||
* @description Basic SQL query detection | ||
* @param {string} q | ||
* @returns {boolean} | ||
*/ | ||
export function isSqlQuery(q: string): boolean { | ||
// detect the shortest sql query | ||
if (!q[11]) { | ||
return false; | ||
} | ||
|
||
const query: string = q.toUpperCase(); | ||
|
||
if (selectKeyword.test(query) && (query.includes(' FROM ') || query.includes('*FROM '))) { | ||
return true; | ||
} | ||
|
||
if (insertKeyword.test(query) && query.includes(' INTO ')) { | ||
return true; | ||
} | ||
|
||
if (updateKeyword.test(query) && query.includes(' SET ')) { | ||
return true; | ||
} | ||
|
||
if (deleteKeyword.test(query) && query.includes(' FROM ')) { | ||
return true; | ||
} | ||
|
||
if (dropKeyword.test(query) && (query.includes(' TABLE ') || query.includes(' DATABASE '))) { | ||
return true; | ||
} | ||
|
||
if ( | ||
createKeyword.test(query) && | ||
(query.includes(' INDEX ') || query.includes(' TABLE ') || query.includes(' DATABASE ')) | ||
) { | ||
return true; | ||
} | ||
|
||
if (alterKeyword.test(query) && query.includes(' TABLE ')) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} |
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,37 @@ | ||
import * as ts from 'typescript'; | ||
import * as Lint from 'tslint'; | ||
import {isSqlQuery} from '../is-sql-query'; | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithWalker(new RuleWalker(sourceFile, this.getOptions())); | ||
} | ||
} | ||
|
||
const stringLiteralKinds: number[] = [ts.SyntaxKind.NoSubstitutionTemplateLiteral, ts.SyntaxKind.StringLiteral]; | ||
const generalErrorMessage: string = 'Found possible SQL injection'; | ||
|
||
class RuleWalker extends Lint.RuleWalker { | ||
visitTemplateExpression(node: ts.TemplateExpression) { | ||
const {parent} = node; | ||
|
||
if ( | ||
(!parent || parent.kind !== ts.SyntaxKind.TaggedTemplateExpression) && | ||
isSqlQuery(node.getText().slice(1, -1)) | ||
) { | ||
this.addFailureAtNode(node, generalErrorMessage); | ||
} | ||
|
||
super.visitTemplateExpression(node); | ||
} | ||
|
||
visitBinaryExpression(node: ts.BinaryExpression) { | ||
const {left} = node; | ||
|
||
if (left && stringLiteralKinds.includes(left.kind) && isSqlQuery(left.getText().slice(1, -1))) { | ||
this.addFailureAtNode(left, generalErrorMessage); | ||
} | ||
|
||
super.visitBinaryExpression(node); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
test/rules/tsr-detect-sql-literal-injection/default/test.ts.lint
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,22 @@ | ||
const userId = 1; | ||
let query = `SELECT * FROM users WHERE id = ${userId}`; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Found possible SQL injection] | ||
query = `SELECT *FROM users WHERE id = ` + userId; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Found possible SQL injection] | ||
query = ' SELECT * FROM users WHERE id =' + userId; | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Found possible SQL injection] | ||
|
||
db.query(query); | ||
|
||
const columns = 'id, name'; | ||
Users.query( ` SELECT ${columns} FROM users` ); | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Found possible SQL injection] | ||
|
||
|
||
const query = sql`SELECT * FROM users WHERE id = ${userId}`; | ||
db.query(query); | ||
|
||
Users.query(`SELECT id, name FROM users`); | ||
|
||
const punctuation = '!'; | ||
console.log(`Not SQL${punctuation}`); |
6 changes: 6 additions & 0 deletions
6
test/rules/tsr-detect-sql-literal-injection/default/tslint.json
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,6 @@ | ||
{ | ||
"rulesDirectory": "../../../../dist/rules", | ||
"rules": { | ||
"tsr-detect-sql-literal-injection": true | ||
} | ||
} |