-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Template injection vulnerability detection in handlebars and pug (#4827)
* Template injection vulnerability detection in handlebars * template injection vulnerability detection in pug * fix lint and naming issues * create separate job for template injection * add support to registerPartial function * add tests for pug render function
- Loading branch information
1 parent
59e9a2a
commit 985cb1d
Showing
13 changed files
with
297 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict' | ||
|
||
const shimmer = require('../../datadog-shimmer') | ||
const { channel, addHook } = require('./helpers/instrument') | ||
|
||
const handlebarsCompileCh = channel('datadog:handlebars:compile:start') | ||
const handlebarsRegisterPartialCh = channel('datadog:handlebars:register-partial:start') | ||
|
||
function wrapCompile (compile) { | ||
return function wrappedCompile (source) { | ||
if (handlebarsCompileCh.hasSubscribers) { | ||
handlebarsCompileCh.publish({ source }) | ||
} | ||
|
||
return compile.apply(this, arguments) | ||
} | ||
} | ||
|
||
function wrapRegisterPartial (registerPartial) { | ||
return function wrappedRegisterPartial (name, partial) { | ||
if (handlebarsRegisterPartialCh.hasSubscribers) { | ||
handlebarsRegisterPartialCh.publish({ partial }) | ||
} | ||
|
||
return registerPartial.apply(this, arguments) | ||
} | ||
} | ||
|
||
addHook({ name: 'handlebars', file: 'dist/cjs/handlebars/compiler/compiler.js', versions: ['>=4.0.0'] }, compiler => { | ||
shimmer.wrap(compiler, 'compile', wrapCompile) | ||
shimmer.wrap(compiler, 'precompile', wrapCompile) | ||
|
||
return compiler | ||
}) | ||
|
||
addHook({ name: 'handlebars', file: 'dist/cjs/handlebars/base.js', versions: ['>=4.0.0'] }, base => { | ||
shimmer.wrap(base.HandlebarsEnvironment.prototype, 'registerPartial', wrapRegisterPartial) | ||
|
||
return base | ||
}) |
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,23 @@ | ||
'use strict' | ||
|
||
const shimmer = require('../../datadog-shimmer') | ||
const { channel, addHook } = require('./helpers/instrument') | ||
|
||
const pugCompileCh = channel('datadog:pug:compile:start') | ||
|
||
function wrapCompile (compile) { | ||
return function wrappedCompile (source) { | ||
if (pugCompileCh.hasSubscribers) { | ||
pugCompileCh.publish({ source }) | ||
} | ||
|
||
return compile.apply(this, arguments) | ||
} | ||
} | ||
|
||
addHook({ name: 'pug', versions: ['>=2.0.4'] }, compiler => { | ||
shimmer.wrap(compiler, 'compile', wrapCompile) | ||
shimmer.wrap(compiler, 'compileClientWithDependenciesTracked', wrapCompile) | ||
|
||
return compiler | ||
}) |
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
18 changes: 18 additions & 0 deletions
18
packages/dd-trace/src/appsec/iast/analyzers/template-injection-analyzer.js
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,18 @@ | ||
'use strict' | ||
|
||
const InjectionAnalyzer = require('./injection-analyzer') | ||
const { TEMPLATE_INJECTION } = require('../vulnerabilities') | ||
|
||
class TemplateInjectionAnalyzer extends InjectionAnalyzer { | ||
constructor () { | ||
super(TEMPLATE_INJECTION) | ||
} | ||
|
||
onConfigure () { | ||
this.addSub('datadog:handlebars:compile:start', ({ source }) => this.analyze(source)) | ||
this.addSub('datadog:handlebars:register-partial:start', ({ partial }) => this.analyze(partial)) | ||
this.addSub('datadog:pug:compile:start', ({ source }) => this.analyze(source)) | ||
} | ||
} | ||
|
||
module.exports = new TemplateInjectionAnalyzer() |
File renamed without changes.
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
79 changes: 79 additions & 0 deletions
79
...dd-trace/test/appsec/iast/analyzers/template-injection-analyzer.handlebars.plugin.spec.js
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,79 @@ | ||
'use strict' | ||
|
||
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') | ||
|
||
describe('template-injection-analyzer with handlebars', () => { | ||
withVersions('handlebars', 'handlebars', version => { | ||
let source | ||
before(() => { | ||
source = '<p>{{name}}</p>' | ||
}) | ||
|
||
describe('compile', () => { | ||
prepareTestServerForIast('template injection analyzer', | ||
(testThatRequestHasVulnerability, testThatRequestHasNoVulnerability) => { | ||
let lib | ||
beforeEach(() => { | ||
lib = require(`../../../../../../versions/handlebars@${version}`).get() | ||
}) | ||
|
||
testThatRequestHasVulnerability(() => { | ||
const store = storage.getStore() | ||
const iastContext = iastContextFunctions.getIastContext(store) | ||
const template = newTaintedString(iastContext, source, 'param', 'Request') | ||
lib.compile(template) | ||
}, 'TEMPLATE_INJECTION') | ||
|
||
testThatRequestHasNoVulnerability(() => { | ||
lib.compile(source) | ||
}, 'TEMPLATE_INJECTION') | ||
}) | ||
}) | ||
|
||
describe('precompile', () => { | ||
prepareTestServerForIast('template injection analyzer', | ||
(testThatRequestHasVulnerability, testThatRequestHasNoVulnerability) => { | ||
let lib | ||
beforeEach(() => { | ||
lib = require(`../../../../../../versions/handlebars@${version}`).get() | ||
}) | ||
|
||
testThatRequestHasVulnerability(() => { | ||
const store = storage.getStore() | ||
const iastContext = iastContextFunctions.getIastContext(store) | ||
const template = newTaintedString(iastContext, source, 'param', 'Request') | ||
lib.precompile(template) | ||
}, 'TEMPLATE_INJECTION') | ||
|
||
testThatRequestHasNoVulnerability(() => { | ||
lib.precompile(source) | ||
}, 'TEMPLATE_INJECTION') | ||
}) | ||
}) | ||
|
||
describe('registerPartial', () => { | ||
prepareTestServerForIast('template injection analyzer', | ||
(testThatRequestHasVulnerability, testThatRequestHasNoVulnerability) => { | ||
let lib | ||
beforeEach(() => { | ||
lib = require(`../../../../../../versions/handlebars@${version}`).get() | ||
}) | ||
|
||
testThatRequestHasVulnerability(() => { | ||
const store = storage.getStore() | ||
const iastContext = iastContextFunctions.getIastContext(store) | ||
const partial = newTaintedString(iastContext, source, 'param', 'Request') | ||
|
||
lib.registerPartial('vulnerablePartial', partial) | ||
}, 'TEMPLATE_INJECTION') | ||
|
||
testThatRequestHasNoVulnerability(() => { | ||
lib.registerPartial('vulnerablePartial', source) | ||
}, 'TEMPLATE_INJECTION') | ||
}) | ||
}) | ||
}) | ||
}) |
100 changes: 100 additions & 0 deletions
100
packages/dd-trace/test/appsec/iast/analyzers/template-injection-analyzer.pug.plugin.spec.js
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,100 @@ | ||
'use strict' | ||
|
||
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') | ||
|
||
describe('template-injection-analyzer with pug', () => { | ||
withVersions('pug', 'pug', version => { | ||
let source | ||
before(() => { | ||
source = 'string of pug' | ||
}) | ||
|
||
describe('compile', () => { | ||
prepareTestServerForIast('template injection analyzer', | ||
(testThatRequestHasVulnerability, testThatRequestHasNoVulnerability) => { | ||
let lib | ||
beforeEach(() => { | ||
lib = require(`../../../../../../versions/pug@${version}`).get() | ||
}) | ||
|
||
testThatRequestHasVulnerability(() => { | ||
const store = storage.getStore() | ||
const iastContext = iastContextFunctions.getIastContext(store) | ||
const template = newTaintedString(iastContext, source, 'param', 'Request') | ||
lib.compile(template) | ||
}, 'TEMPLATE_INJECTION') | ||
|
||
testThatRequestHasNoVulnerability(() => { | ||
const template = lib.compile(source) | ||
template() | ||
}, 'TEMPLATE_INJECTION') | ||
}) | ||
}) | ||
|
||
describe('compileClient', () => { | ||
prepareTestServerForIast('template injection analyzer', | ||
(testThatRequestHasVulnerability, testThatRequestHasNoVulnerability) => { | ||
let lib | ||
beforeEach(() => { | ||
lib = require(`../../../../../../versions/pug@${version}`).get() | ||
}) | ||
|
||
testThatRequestHasVulnerability(() => { | ||
const store = storage.getStore() | ||
const iastContext = iastContextFunctions.getIastContext(store) | ||
const template = newTaintedString(iastContext, source, 'param', 'Request') | ||
lib.compileClient(template) | ||
}, 'TEMPLATE_INJECTION') | ||
|
||
testThatRequestHasNoVulnerability(() => { | ||
lib.compileClient(source) | ||
}, 'TEMPLATE_INJECTION') | ||
}) | ||
}) | ||
|
||
describe('compileClientWithDependenciesTracked', () => { | ||
prepareTestServerForIast('template injection analyzer', | ||
(testThatRequestHasVulnerability, testThatRequestHasNoVulnerability) => { | ||
let lib | ||
beforeEach(() => { | ||
lib = require(`../../../../../../versions/pug@${version}`).get() | ||
}) | ||
|
||
testThatRequestHasVulnerability(() => { | ||
const store = storage.getStore() | ||
const iastContext = iastContextFunctions.getIastContext(store) | ||
const template = newTaintedString(iastContext, source, 'param', 'Request') | ||
lib.compileClientWithDependenciesTracked(template, {}) | ||
}, 'TEMPLATE_INJECTION') | ||
|
||
testThatRequestHasNoVulnerability(() => { | ||
lib.compileClient(source) | ||
}, 'TEMPLATE_INJECTION') | ||
}) | ||
}) | ||
|
||
describe('render', () => { | ||
prepareTestServerForIast('template injection analyzer', | ||
(testThatRequestHasVulnerability, testThatRequestHasNoVulnerability) => { | ||
let lib | ||
beforeEach(() => { | ||
lib = require(`../../../../../../versions/pug@${version}`).get() | ||
}) | ||
|
||
testThatRequestHasVulnerability(() => { | ||
const store = storage.getStore() | ||
const iastContext = iastContextFunctions.getIastContext(store) | ||
const str = newTaintedString(iastContext, source, 'param', 'Request') | ||
lib.render(str) | ||
}, 'TEMPLATE_INJECTION') | ||
|
||
testThatRequestHasNoVulnerability(() => { | ||
lib.render(source) | ||
}, 'TEMPLATE_INJECTION') | ||
}) | ||
}) | ||
}) | ||
}) |
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
Oops, something went wrong.