-
Notifications
You must be signed in to change notification settings - Fork 354
Detect SQL injection with sequelize #3154
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
115f0c6
Detect vulnerabilities in sequelize and ignore when sql is already va…
uurien c4fe603
Add sequelize test
uurien 2e9f0d1
Support ORACLE in redactions
uurien 57b8335
Fix test
uurien a7fb46d
Improve taint-tracking plugin tests
uurien 8beda88
Remove dummy lines
uurien 579ac6c
Comment in PR
uurien 738d5cb
Delete doble check of sequelizeParentStore
uurien b35a77d
Update packages/dd-trace/src/appsec/iast/taint-tracking/plugin.js
uurien 5611d5c
Fix lint
uurien da59b75
Change function declaration to help understanding
uurien 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
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,51 @@ | ||
| 'use strict' | ||
|
|
||
| const { | ||
| channel, | ||
| addHook, | ||
| AsyncResource | ||
| } = require('./helpers/instrument') | ||
|
|
||
| const shimmer = require('../../datadog-shimmer') | ||
|
|
||
| addHook({ name: 'sequelize', versions: ['>=4'] }, Sequelize => { | ||
| const startCh = channel('datadog:sequelize:query:start') | ||
| const finishCh = channel('datadog:sequelize:query:finish') | ||
|
|
||
| shimmer.wrap(Sequelize.prototype, 'query', query => { | ||
| return function (sql) { | ||
| if (!startCh.hasSubscribers) { | ||
| return query.apply(this, arguments) | ||
| } | ||
|
|
||
| const asyncResource = new AsyncResource('bound-anonymous-fn') | ||
|
|
||
| let dialect | ||
| if (this.options && this.options.dialect) { | ||
| dialect = this.options.dialect | ||
| } else if (this.dialect && this.dialect.name) { | ||
| dialect = this.dialect.name | ||
| } | ||
|
|
||
| function onFinish () { | ||
| asyncResource.bind(function () { | ||
| finishCh.publish() | ||
| }, this).apply(this) | ||
| } | ||
|
|
||
| return asyncResource.bind(function () { | ||
| startCh.publish({ | ||
| sql, | ||
| dialect | ||
| }) | ||
|
|
||
| const promise = query.apply(this, arguments) | ||
| promise.then(onFinish, onFinish) | ||
|
|
||
| return promise | ||
| }, this).apply(this, arguments) | ||
| } | ||
| }) | ||
|
|
||
| return Sequelize | ||
| }) |
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
70 changes: 70 additions & 0 deletions
70
packages/dd-trace/test/appsec/iast/analyzers/sql-injection-analyzer.sequelize.plugin.spec.js
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,70 @@ | ||
| 'use strict' | ||
|
|
||
| const fs = require('fs') | ||
| const os = require('os') | ||
| const path = require('path') | ||
| const { prepareTestServerForIast } = require('../utils') | ||
| const { storage } = require('../../../../../datadog-core') | ||
| const iastContextFunctions = require('../../../../src/appsec/iast/iast-context') | ||
| const { newTaintedString } = require('../../../../src/appsec/iast/taint-tracking/operations') | ||
| const vulnerabilityReporter = require('../../../../src/appsec/iast/vulnerability-reporter') | ||
|
|
||
| describe('sql-injection-analyzer with sequelize', () => { | ||
| withVersions('sequelize', 'sequelize', sequelizeVersion => { | ||
| withVersions('mysql2', 'mysql2', (mysqlVersion) => { | ||
| let sequelize | ||
|
|
||
| prepareTestServerForIast('sequelize + mysql2', | ||
| (testThatRequestHasVulnerability, testThatRequestHasNoVulnerability) => { | ||
| beforeEach(() => { | ||
| const Sequelize = require(`../../../../../../versions/sequelize@${sequelizeVersion}`).get() | ||
| sequelize = new Sequelize('db', 'root', '', { | ||
| host: '127.0.0.1', | ||
| dialect: 'mysql' | ||
| }) | ||
| vulnerabilityReporter.clearCache() | ||
| return sequelize.authenticate() | ||
| }) | ||
|
|
||
| testThatRequestHasVulnerability(() => { | ||
| const store = storage.getStore() | ||
| const iastCtx = iastContextFunctions.getIastContext(store) | ||
|
|
||
| let sql = 'SELECT 1' | ||
| sql = newTaintedString(iastCtx, sql, 'param', 'Request') | ||
|
|
||
| return sequelize.query(sql) | ||
| }, 'SQL_INJECTION', { occurrences: 1 }) | ||
|
|
||
| const externalFileContent = `'use strict' | ||
|
|
||
| module.exports = function (sequelize, sql) { | ||
| return sequelize.query(sql) | ||
| } | ||
| ` | ||
| testThatRequestHasVulnerability(() => { | ||
| const filepath = path.join(os.tmpdir(), 'test-sequelize-sqli.js') | ||
| fs.writeFileSync(filepath, externalFileContent) | ||
|
|
||
| const store = storage.getStore() | ||
| const iastCtx = iastContextFunctions.getIastContext(store) | ||
|
|
||
| let sql = 'SELECT 1' | ||
| sql = newTaintedString(iastCtx, sql, 'param', 'Request') | ||
|
|
||
| return require(filepath)(sequelize, sql) | ||
| }, 'SQL_INJECTION', { | ||
| occurrences: 1, | ||
| location: { | ||
| path: 'test-sequelize-sqli.js', | ||
| line: 4 | ||
| } | ||
| }) | ||
|
|
||
| testThatRequestHasNoVulnerability(() => { | ||
| return sequelize.query('SELECT 1') | ||
| }, 'SQL_INJECTION') | ||
| }) | ||
| }) | ||
| }) | ||
| }) |
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
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.
This data doesn't seem to ever get cleared from the store so it may have some unintended side-effects. 🤔
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.
This is restored on
datadog:sequelize:query:finishThe purpose of this lines is prevent double detections of the same vulnerability. As sequelize library uses
pgormysql2libraries, we need a way to set in store that we are already in the query. For that reason we are entering with new store in:startand restoring the old one in:finish.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.
Ah, true. Do you know if sequelize could trigger these in a nested way? Setting on the store rather than storing on the span could make things leak beyond where it should. 🤔
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.
Good point. I've reviewed it after your comment. The answer is not, sequelize.query method does not call internally again to the same query method.
sequelize.query method calls to the query method of the select dialect (pg, sqlite, mysql...)
With sequelize we are not creating new span, so current span is the parent span, and the parent span can have multiple calls to
sequelize.querymethod, that the reason that we should save the data in AsyncResource (that is new asyncresource, created in the instrumentation point) and not in the parent span.