Skip to content

Fix LDAPi vulnerability location when using ldapjs-promise #3593

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 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict'
const InjectionAnalyzer = require('./injection-analyzer')
const { LDAP_INJECTION } = require('../vulnerabilities')
const { getNodeModulesPaths } = require('../path-line')

const EXCLUDED_PATHS = getNodeModulesPaths('ldapjs-promise')

class LdapInjectionAnalyzer extends InjectionAnalyzer {
constructor () {
Expand All @@ -10,6 +13,10 @@ class LdapInjectionAnalyzer extends InjectionAnalyzer {
onConfigure () {
this.addSub('datadog:ldapjs:client:search', ({ base, filter }) => this.analyzeAll(base, filter))
}

_getExcludedPaths () {
return EXCLUDED_PATHS
}
}

module.exports = new LdapInjectionAnalyzer()
2 changes: 1 addition & 1 deletion packages/dd-trace/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function globMatch (pattern, subject) {
function calculateDDBasePath (dirname) {
const dirSteps = dirname.split(path.sep)
const packagesIndex = dirSteps.lastIndexOf('packages')
return dirSteps.slice(0, packagesIndex).join(path.sep) + path.sep
return dirSteps.slice(0, packagesIndex + 1).join(path.sep) + path.sep
}

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
'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')
Expand Down Expand Up @@ -165,4 +169,53 @@ describe('ldap-injection-analyzer with ldapjs', () => {
})
})
})

withVersions('ldapjs', 'ldapjs-promise', promiseVersion => {
prepareTestServerForIast('ldapjs-promise', (testThatRequestHasVulnerability, testThatRequestHasNoVulnerability) => {
const srcFilePath = path.join(__dirname, 'resources', 'ldap-injection-methods.js')
const dstFilePath = path.join(os.tmpdir(), 'ldap-injection-methods.js')
let ldapMethods

beforeEach(async () => {
await agent.load('ldapjs')
vulnerabilityReporter.clearCache()
const ldapjs = require(`../../../../../../versions/ldapjs-promise@${promiseVersion}`).get()
client = ldapjs.createClient({
url: 'ldap://localhost:1389'
})

fs.copyFileSync(srcFilePath, dstFilePath)
ldapMethods = require(dstFilePath)

return client.bind(`cn=admin,${base}`, 'adminpassword')
})

afterEach(async () => {
fs.unlinkSync(dstFilePath)
await client.unbind()
})

describe('has vulnerability', () => {
testThatRequestHasVulnerability(() => {
const store = storage.getStore()
const iastCtx = iastContextFunctions.getIastContext(store)

let filter = '(objectClass=*)'
filter = newTaintedString(iastCtx, filter, 'param', 'Request')

return ldapMethods.executeSearch(client, base, filter)
}, 'LDAP_INJECTION', {
occurrences: 1,
location: { path: 'ldap-injection-methods.js' }
})
})

describe('has no vulnerability', () => {
testThatRequestHasNoVulnerability(() => {
const filter = '(objectClass=*)'
return ldapMethods.executeSearch(client, base, filter)
}, 'LDAP_INJECTION')
})
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

function executeSearch (client, base, filter) {
return client.search(base, filter)
}

module.exports = { executeSearch }
9 changes: 5 additions & 4 deletions packages/dd-trace/test/appsec/iast/path-line.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CallSiteMock {
}

describe('path-line', function () {
const PATH_LINE_PATH = path.join('packages', 'dd-trace', 'src', 'appsec', 'iast', 'path-line.js')
const PATH_LINE_PATH = path.join('dd-trace', 'src', 'appsec', 'iast', 'path-line.js')

const tmpdir = os.tmpdir()
const firstSep = tmpdir.indexOf(path.sep)
Expand Down Expand Up @@ -50,19 +50,20 @@ describe('path-line', function () {

describe('calculateDDBasePath', () => {
it('/node_modules/dd-trace', () => {
const basePath = path.join(rootPath, 'node_modules', 'dd-trace', path.sep)
const basePath = path.join(rootPath, 'node_modules', 'dd-trace', 'packages', path.sep)
const result = pathLine.calculateDDBasePath(path.join(basePath, PATH_LINE_PATH))
expect(result).to.be.equals(basePath)
})

it('/packages/project/path/node_modules/dd-trace', () => {
const basePath = path.join(rootPath, 'packages', 'project', 'path', 'node_modules', 'dd-trace', path.sep)
const basePath =
path.join(rootPath, 'packages', 'project', 'path', 'node_modules', 'dd-trace', 'packages', path.sep)
const result = pathLine.calculateDDBasePath(path.join(basePath, PATH_LINE_PATH))
expect(result).to.be.equals(basePath)
})

it('/project/path/node_modules/dd-trace', () => {
const basePath = path.join(rootPath, 'project', 'path', 'node_modules', 'dd-trace', path.sep)
const basePath = path.join(rootPath, 'project', 'path', 'node_modules', 'dd-trace', 'packages', path.sep)
const result = pathLine.calculateDDBasePath(path.join(basePath, PATH_LINE_PATH))
expect(result).to.be.equals(basePath)
})
Expand Down
10 changes: 10 additions & 0 deletions packages/dd-trace/test/plugins/externals.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@
"versions": ["6.1.0"]
}
],
"ldapjs": [
{
"name": "ldapjs",
"versions": [">= 2"]
},
{
"name": "ldapjs-promise",
"versions": [">=2"]
}
],
"mariadb": [
{
"name": "mariadb",
Expand Down