Skip to content

Commit

Permalink
[ASM] Ssrf handle request options (DataDog#4791)
Browse files Browse the repository at this point in the history
* Handle request options

* Remove .only

* clean up

* suggestion
  • Loading branch information
iunanua authored Oct 18, 2024
1 parent 83468b8 commit 4f62b5a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/dd-trace/src/appsec/rasp/ssrf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { format } = require('url')
const { httpClientRequestStart } = require('../channels')
const { storage } = require('../../../../datadog-core')
const addresses = require('../addresses')
Expand All @@ -20,12 +21,12 @@ function disable () {
function analyzeSsrf (ctx) {
const store = storage.getStore()
const req = store?.req
const url = ctx.args.uri
const outgoingUrl = (ctx.args.options?.uri && format(ctx.args.options.uri)) ?? ctx.args.uri

if (!req || !url) return
if (!req || !outgoingUrl) return

const persistent = {
[addresses.HTTP_OUTGOING_URL]: url
[addresses.HTTP_OUTGOING_URL]: outgoingUrl
}

const result = waf.run({ persistent }, req, RULE_TYPES.SSRF)
Expand Down
43 changes: 43 additions & 0 deletions packages/dd-trace/test/appsec/rasp/ssrf.express.plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,49 @@ describe('RASP - ssrf', () => {
})
})
})

describe('Test using request', () => {
withVersions('express', 'request', requestVersion => {
let requestToTest

beforeEach(() => {
requestToTest = require(`../../../../../versions/request@${requestVersion}`).get()
})

it('Should not detect threat', async () => {
app = (req, res) => {
requestToTest.get(`https://${req.query.host}`).on('response', () => {
res.end('end')
})
}

axios.get('/?host=www.datadoghq.com')

return checkRaspExecutedAndNotThreat(agent)
})

it('Should detect threat doing a GET request', async () => {
app = async (req, res) => {
try {
requestToTest.get(`https://${req.query.host}`)
.on('error', (e) => {
if (e.message === 'DatadogRaspAbortError') {
res.writeHead(500)
}
res.end('end')
})
} catch (e) {
if (e.cause.message === 'DatadogRaspAbortError') {
res.writeHead(500)
}
res.end('end')
}
}

await testBlockingRequest()
})
})
})
})
})

Expand Down
4 changes: 4 additions & 0 deletions packages/dd-trace/test/plugins/externals.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
{
"name": "cookie-parser",
"versions": [">=1.4.6"]
},
{
"name": "request",
"versions": ["2.88.2"]
}
],
"express-mongo-sanitize": [
Expand Down

0 comments on commit 4f62b5a

Please sign in to comment.