dd-trace 6.25.0 → 6.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/README.md +3 -0
  2. package/ci/cypress/support.js +2 -1
  3. package/ci/vitest-no-worker-init-node-setup.mjs +2 -1
  4. package/ci/vitest-no-worker-init-runner-setup.mjs +2 -1
  5. package/ci/vitest-no-worker-init-setup.mjs +729 -724
  6. package/ci/vitest-no-worker-init-v5-setup.mjs +2 -1
  7. package/package.json +33 -30
  8. package/packages/datadog-esbuild/index.js +8 -15
  9. package/packages/datadog-esbuild/src/log.js +2 -29
  10. package/packages/datadog-instrumentations/src/ai.js +6 -1
  11. package/packages/datadog-instrumentations/src/cypress-config.js +20 -9
  12. package/packages/datadog-instrumentations/src/helpers/bundler-esm-exports.js +23 -1
  13. package/packages/datadog-instrumentations/src/helpers/bundler-log.js +38 -0
  14. package/packages/datadog-instrumentations/src/helpers/bundler-transform.js +9 -6
  15. package/packages/datadog-instrumentations/src/helpers/fetch.js +51 -6
  16. package/packages/datadog-instrumentations/src/helpers/rewriter/index.js +6 -0
  17. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentation-registry.js +8 -0
  18. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/modelcontextprotocol-client.js +30 -0
  19. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/modelcontextprotocol-server.js +23 -0
  20. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/next.js +21 -0
  21. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/postgres.js +33 -4
  22. package/packages/datadog-instrumentations/src/helpers/rewriter/target-patterns.json +30 -0
  23. package/packages/datadog-instrumentations/src/helpers/rewriter/targets.json +6 -0
  24. package/packages/datadog-instrumentations/src/helpers/rewriter/transforms/postgres.js +122 -0
  25. package/packages/datadog-instrumentations/src/microgateway-core.js +1 -2
  26. package/packages/datadog-instrumentations/src/modelcontextprotocol-client.js +21 -0
  27. package/packages/datadog-instrumentations/src/modelcontextprotocol-sdk.js +7 -1
  28. package/packages/datadog-instrumentations/src/modelcontextprotocol-server.js +41 -0
  29. package/packages/datadog-instrumentations/src/next.js +184 -20
  30. package/packages/datadog-instrumentations/src/router.js +35 -12
  31. package/packages/datadog-instrumentations/src/vitest-main.js +10 -2
  32. package/packages/datadog-plugin-aws-lambda/src/arn.js +41 -0
  33. package/packages/datadog-plugin-aws-lambda/src/event-type-guards.js +170 -0
  34. package/packages/datadog-plugin-aws-lambda/src/trigger.js +339 -0
  35. package/packages/datadog-plugin-aws-sdk/src/services/dynamodb.js +7 -0
  36. package/packages/datadog-plugin-aws-sdk/src/services/kinesis.js +32 -10
  37. package/packages/datadog-plugin-aws-sdk/src/services/lambda.js +10 -1
  38. package/packages/datadog-plugin-aws-sdk/src/services/sqs.js +4 -0
  39. package/packages/datadog-plugin-aws-sdk/src/services/stepfunctions.js +3 -0
  40. package/packages/datadog-plugin-cypress/src/cypress-plugin.js +30 -4
  41. package/packages/datadog-plugin-cypress/src/support.mjs +1353 -0
  42. package/packages/datadog-plugin-express/src/code_origin.js +2 -2
  43. package/packages/datadog-plugin-fastify/src/code_origin.js +2 -2
  44. package/packages/datadog-plugin-google-cloud-pubsub/src/consumer.js +10 -0
  45. package/packages/datadog-plugin-google-cloud-pubsub/src/producer.js +2 -0
  46. package/packages/datadog-plugin-google-cloud-pubsub/src/pubsub-push-subscription.js +7 -0
  47. package/packages/datadog-plugin-google-cloud-pubsub/src/resource-names.js +24 -0
  48. package/packages/datadog-plugin-graphql/src/tools/transforms.js +1 -0
  49. package/packages/datadog-plugin-http/src/client.js +2 -1
  50. package/packages/datadog-plugin-http2/src/client.js +2 -1
  51. package/packages/datadog-plugin-modelcontextprotocol-sdk/src/tracing.js +28 -2
  52. package/packages/datadog-plugin-modelcontextprotocol-sdk/src/utils.js +1 -0
  53. package/packages/datadog-plugin-mongodb-core/src/query.js +17 -1
  54. package/packages/datadog-plugin-postgres/src/index.js +118 -41
  55. package/packages/datadog-plugin-supabase/src/supabase-auth-js-gotrueclient-getuser.js +18 -8
  56. package/packages/datadog-plugin-supabase/src/supabase-functions-js-functionsclient-invoke.js +49 -13
  57. package/packages/datadog-plugin-supabase/src/supabase-storage-js-handle-request.js +34 -9
  58. package/packages/datadog-plugin-vitest/src/index.js +3 -2
  59. package/packages/datadog-plugin-ws/src/close.js +3 -5
  60. package/packages/datadog-plugin-ws/src/producer.js +1 -4
  61. package/packages/datadog-plugin-ws/src/receiver.js +3 -5
  62. package/packages/datadog-plugin-ws/src/server.js +44 -13
  63. package/packages/datadog-vite/index.js +537 -0
  64. package/packages/datadog-vite/src/log.js +5 -0
  65. package/packages/datadog-webpack/src/log.js +2 -29
  66. package/packages/dd-trace/src/aiguard/index.js +1 -1
  67. package/packages/dd-trace/src/aiguard/integrations/index.js +6 -5
  68. package/packages/dd-trace/src/appsec/blocking/index.js +17 -1
  69. package/packages/dd-trace/src/appsec/channels.js +7 -0
  70. package/packages/dd-trace/src/appsec/downstream_requests.js +105 -0
  71. package/packages/dd-trace/src/appsec/graphql.js +10 -13
  72. package/packages/dd-trace/src/appsec/handlers/auth.js +1 -3
  73. package/packages/dd-trace/src/appsec/handlers/http-request.js +60 -65
  74. package/packages/dd-trace/src/appsec/handlers/http-response.js +61 -7
  75. package/packages/dd-trace/src/appsec/handlers/payments.js +23 -27
  76. package/packages/dd-trace/src/appsec/index.js +6 -0
  77. package/packages/dd-trace/src/appsec/lambda.js +17 -17
  78. package/packages/dd-trace/src/appsec/rasp/command_injection.js +4 -4
  79. package/packages/dd-trace/src/appsec/rasp/index.js +8 -1
  80. package/packages/dd-trace/src/appsec/rasp/lfi.js +2 -2
  81. package/packages/dd-trace/src/appsec/rasp/sql_injection.js +7 -17
  82. package/packages/dd-trace/src/appsec/rasp/ssrf.js +80 -16
  83. package/packages/dd-trace/src/appsec/rasp/utils.js +33 -3
  84. package/packages/dd-trace/src/appsec/recommended.json +70 -3
  85. package/packages/dd-trace/src/appsec/reporter.js +9 -0
  86. package/packages/dd-trace/src/appsec/sdk/set_user.js +3 -3
  87. package/packages/dd-trace/src/appsec/sdk/track_event.js +4 -4
  88. package/packages/dd-trace/src/appsec/sdk/user_blocking.js +1 -1
  89. package/packages/dd-trace/src/appsec/user_tracking.js +6 -8
  90. package/packages/dd-trace/src/appsec/waf/index.js +65 -6
  91. package/packages/dd-trace/src/appsec/waf/waf_context_wrapper.js +33 -38
  92. package/packages/dd-trace/src/appsec/waf/waf_manager.js +18 -2
  93. package/packages/dd-trace/src/config/generated-config-types.d.ts +4 -2
  94. package/packages/dd-trace/src/config/index.js +46 -26
  95. package/packages/dd-trace/src/config/parsers.js +35 -0
  96. package/packages/dd-trace/src/config/remote-config-layers.js +38 -0
  97. package/packages/dd-trace/src/config/remote_config.js +2 -1
  98. package/packages/dd-trace/src/config/supported-configurations.json +14 -6
  99. package/packages/dd-trace/src/datastreams/fnv.js +27 -0
  100. package/packages/dd-trace/src/datastreams/pathway.js +113 -28
  101. package/packages/dd-trace/src/datastreams/processor.js +5 -4
  102. package/packages/dd-trace/src/debugger/config.js +1 -1
  103. package/packages/dd-trace/src/debugger/devtools_client/request-options.js +7 -9
  104. package/packages/dd-trace/src/debugger/devtools_client/send.js +1 -1
  105. package/packages/dd-trace/src/debugger/devtools_client/state.js +3 -1
  106. package/packages/dd-trace/src/debugger/devtools_client/tags.js +1 -1
  107. package/packages/dd-trace/src/debugger/index.js +2 -2
  108. package/packages/dd-trace/src/debugger/inspect-segment.js +3 -2
  109. package/packages/dd-trace/src/debugger/pause-duration-histogram.js +4 -1
  110. package/packages/dd-trace/src/debugger/probe_sampler.js +39 -17
  111. package/packages/dd-trace/src/debugger/unsupported-runtime.js +2 -0
  112. package/packages/dd-trace/src/git_metadata.js +2 -1
  113. package/packages/dd-trace/src/index.js +6 -3
  114. package/packages/dd-trace/src/lambda/facade.js +6 -5
  115. package/packages/dd-trace/src/llmobs/plugins/modelcontextprotocol-sdk/index.js +54 -3
  116. package/packages/dd-trace/src/llmobs/plugins/modelcontextprotocol-sdk/utils.js +81 -1
  117. package/packages/dd-trace/src/openfeature/agentless_configuration_source.js +3 -0
  118. package/packages/dd-trace/src/openfeature/configuration_source.js +20 -0
  119. package/packages/dd-trace/src/plugins/ci_plugin.js +104 -9
  120. package/packages/dd-trace/src/plugins/index.js +2 -0
  121. package/packages/dd-trace/src/plugins/util/ci.js +34 -0
  122. package/packages/dd-trace/src/plugins/util/http-otel-semantics.js +9 -14
  123. package/packages/dd-trace/src/plugins/util/test.js +28 -1
  124. package/packages/dd-trace/src/plugins/util/url.js +8 -5
  125. package/packages/dd-trace/src/plugins/util/user-provided-git.js +4 -4
  126. package/packages/dd-trace/src/process-tags/index.js +1 -1
  127. package/packages/dd-trace/src/profiler.js +74 -12
  128. package/packages/dd-trace/src/profiling/profiler.js +30 -2
  129. package/packages/dd-trace/src/profiling/profilers/space.js +3 -1
  130. package/packages/dd-trace/src/profiling/profilers/wall.js +4 -4
  131. package/packages/dd-trace/src/profiling/remote-config.js +354 -0
  132. package/packages/dd-trace/src/profiling/runtime-capabilities.js +11 -0
  133. package/packages/dd-trace/src/propagation-hash/index.js +7 -0
  134. package/packages/dd-trace/src/proxy.js +13 -1
  135. package/vendor/dist/@opentelemetry/core/LICENSE +11 -0
  136. package/vendor/dist/@opentelemetry/core/index.js +1 -1
  137. package/vite.d.ts +7 -0
  138. package/vite.js +3 -0
  139. package/lambda.d.ts +0 -39
  140. package/lambda.js +0 -3
  141. package/packages/datadog-plugin-cypress/src/support.js +0 -1348
@@ -41,43 +41,43 @@ function onLambdaStartInvocation (data) {
41
41
  '_dd.runtime_family': 'nodejs',
42
42
  })
43
43
 
44
- const persistent = {}
44
+ const requestData = {}
45
45
 
46
46
  if (path) {
47
- persistent[addresses.HTTP_INCOMING_URL] = path
47
+ requestData[addresses.HTTP_INCOMING_URL] = path
48
48
  }
49
49
 
50
50
  if (method) {
51
- persistent[addresses.HTTP_INCOMING_METHOD] = method
51
+ requestData[addresses.HTTP_INCOMING_METHOD] = method
52
52
  }
53
53
 
54
54
  if (headers) {
55
55
  // Cookie header is already stripped by the Lambda layer's event-data-extractor
56
- persistent[addresses.HTTP_INCOMING_HEADERS] = headers
56
+ requestData[addresses.HTTP_INCOMING_HEADERS] = headers
57
57
  }
58
58
 
59
59
  if (clientIp) {
60
60
  span.setTag(HTTP_CLIENT_IP, clientIp)
61
- persistent[addresses.HTTP_CLIENT_IP] = clientIp
61
+ requestData[addresses.HTTP_CLIENT_IP] = clientIp
62
62
  }
63
63
 
64
64
  if (query) {
65
- persistent[addresses.HTTP_INCOMING_QUERY] = query
65
+ requestData[addresses.HTTP_INCOMING_QUERY] = query
66
66
  }
67
67
 
68
68
  if (body !== undefined && body !== null) {
69
- persistent[addresses.HTTP_INCOMING_BODY] = body
69
+ requestData[addresses.HTTP_INCOMING_BODY] = body
70
70
  }
71
71
 
72
72
  if (pathParams) {
73
- persistent[addresses.HTTP_INCOMING_PARAMS] = pathParams
73
+ requestData[addresses.HTTP_INCOMING_PARAMS] = pathParams
74
74
  }
75
75
 
76
76
  if (cookies) {
77
- persistent[addresses.HTTP_INCOMING_COOKIES] = cookies
77
+ requestData[addresses.HTTP_INCOMING_COOKIES] = cookies
78
78
  }
79
79
 
80
- waf.run({ persistent }, req, undefined, span)
80
+ waf.run(requestData, req, undefined, span)
81
81
  } catch (err) {
82
82
  log.error('[ASM] Error in Lambda start-invocation handler', err)
83
83
  }
@@ -108,16 +108,16 @@ function onLambdaEndInvocation (data) {
108
108
  activeInvocations.delete(span)
109
109
 
110
110
  try {
111
- const persistent = {}
111
+ const requestData = {}
112
112
 
113
113
  if (statusCode) {
114
- persistent[addresses.HTTP_INCOMING_RESPONSE_CODE] = String(statusCode)
114
+ requestData[addresses.HTTP_INCOMING_RESPONSE_CODE] = String(statusCode)
115
115
  }
116
116
 
117
117
  if (responseHeaders) {
118
118
  const filteredHeaders = { ...responseHeaders }
119
119
  delete filteredHeaders['set-cookie']
120
- persistent[addresses.HTTP_INCOMING_RESPONSE_HEADERS] = filteredHeaders
120
+ requestData[addresses.HTTP_INCOMING_RESPONSE_HEADERS] = filteredHeaders
121
121
  }
122
122
 
123
123
  const samplingDecision = apiSecurity.sampleRootSpanRequest(span, {
@@ -129,17 +129,17 @@ function onLambdaEndInvocation (data) {
129
129
  }, true)
130
130
 
131
131
  if (samplingDecision === apiSecurity.SamplingDecision.SAMPLE) {
132
- persistent[addresses.WAF_CONTEXT_PROCESSOR] = { 'extract-schema': true }
132
+ requestData[addresses.WAF_CONTEXT_PROCESSOR] = { 'extract-schema': true }
133
133
 
134
134
  const parsedBody = parseResponseBody(responseBody, responseHeaders, isBase64Encoded)
135
135
  if (parsedBody !== undefined) {
136
- persistent[addresses.HTTP_INCOMING_RESPONSE_BODY] = parsedBody
136
+ requestData[addresses.HTTP_INCOMING_RESPONSE_BODY] = parsedBody
137
137
  }
138
138
  }
139
139
 
140
140
  let wafResult
141
- if (!isEmpty(persistent)) {
142
- wafResult = waf.run({ persistent }, req, undefined, span)
141
+ if (!isEmpty(requestData)) {
142
+ wafResult = waf.run(requestData, req, undefined, span)
143
143
  }
144
144
 
145
145
  apiSecurity.reportRootSpanRequest(span, samplingDecision, wafResult)
@@ -31,20 +31,20 @@ function analyzeCommandInjection ({ file, fileArgs, shell, abortController }) {
31
31
  const req = getActiveRequest()
32
32
  if (!req) return
33
33
 
34
- const ephemeral = {}
34
+ const subcontextData = {}
35
35
  const raspRule = { type: RULE_TYPES.COMMAND_INJECTION }
36
36
  const params = fileArgs ? [file, ...fileArgs] : file
37
37
 
38
38
  if (shell) {
39
- ephemeral[addresses.SHELL_COMMAND] = params
39
+ subcontextData[addresses.SHELL_COMMAND] = params
40
40
  raspRule.variant = 'shell'
41
41
  } else {
42
42
  const commandParams = Array.isArray(params) ? params : [params]
43
- ephemeral[addresses.EXEC_COMMAND] = commandParams
43
+ subcontextData[addresses.EXEC_COMMAND] = commandParams
44
44
  raspRule.variant = 'exec'
45
45
  }
46
46
 
47
- const result = waf.run({ ephemeral }, req, raspRule)
47
+ const result = waf.runSubContext(subcontextData, raspRule)
48
48
 
49
49
  handleResult(result, req, web.getContext(req)?.res, abortController, config, raspRule)
50
50
  }
@@ -6,6 +6,7 @@ const {
6
6
  expressMiddlewareError,
7
7
  fastifyMiddlewareError,
8
8
  routerMiddlewareError,
9
+ nextRequestStart,
9
10
  } = require('../channels')
10
11
  const { block, registerBlockDelegation, isBlocked } = require('../blocking')
11
12
  const { updateRaspRuleMatchMetricTags } = require('../telemetry')
@@ -14,7 +15,7 @@ const sqli = require('./sql_injection')
14
15
  const lfi = require('./lfi')
15
16
  const cmdi = require('./command_injection')
16
17
 
17
- const { DatadogRaspAbortError } = require('./utils')
18
+ const { DatadogRaspAbortError, markToBlockOnDetection } = require('./utils')
18
19
 
19
20
  function removeAllListeners (emitter, event) {
20
21
  const listeners = emitter.listeners(event)
@@ -106,6 +107,10 @@ function blockOnDatadogRaspAbortError ({ error, isTopLevel }) {
106
107
  return true
107
108
  }
108
109
 
110
+ function onNextRequestStart ({ req, ignoreError }) {
111
+ markToBlockOnDetection(req, ignoreError)
112
+ }
113
+
109
114
  function enable (config) {
110
115
  ssrf.enable(config)
111
116
  sqli.enable(config)
@@ -117,6 +122,7 @@ function enable (config) {
117
122
  expressMiddlewareError.subscribe(blockOnDatadogRaspAbortError)
118
123
  fastifyMiddlewareError.subscribe(blockOnDatadogRaspAbortError)
119
124
  routerMiddlewareError.subscribe(blockOnDatadogRaspAbortError)
125
+ nextRequestStart.subscribe(onNextRequestStart)
120
126
  }
121
127
 
122
128
  function disable () {
@@ -130,6 +136,7 @@ function disable () {
130
136
  expressMiddlewareError.unsubscribe(blockOnDatadogRaspAbortError)
131
137
  fastifyMiddlewareError.unsubscribe(blockOnDatadogRaspAbortError)
132
138
  routerMiddlewareError.unsubscribe(blockOnDatadogRaspAbortError)
139
+ nextRequestStart.unsubscribe(onNextRequestStart)
133
140
  }
134
141
 
135
142
  module.exports = {
@@ -77,13 +77,13 @@ function analyzeLfi (ctx) {
77
77
  }
78
78
 
79
79
  function analyzeLfiPath (path, req, res, abortController) {
80
- const ephemeral = {
80
+ const subcontextData = {
81
81
  [FS_OPERATION_PATH]: path,
82
82
  }
83
83
 
84
84
  const raspRule = { type: RULE_TYPES.LFI }
85
85
 
86
- const result = waf.run({ ephemeral }, req, raspRule)
86
+ const result = waf.runSubContext(subcontextData, raspRule, undefined, req)
87
87
  handleResult(result, req, res, abortController, config, raspRule)
88
88
  }
89
89
 
@@ -65,37 +65,27 @@ function analyzeSqlInjection (query, dbSystem, abortController) {
65
65
  }
66
66
  executedQueries.add(query)
67
67
 
68
- const ephemeral = {
68
+ const subcontextData = {
69
69
  [addresses.DB_STATEMENT]: query,
70
70
  [addresses.DB_SYSTEM]: dbSystem,
71
71
  }
72
72
 
73
73
  const raspRule = { type: RULE_TYPES.SQL_INJECTION }
74
74
 
75
- const result = waf.run({ ephemeral }, req, raspRule)
75
+ const result = waf.runSubContext(subcontextData, raspRule)
76
76
 
77
77
  handleResult(result, req, web.getContext(req)?.res, abortController, config, raspRule)
78
78
  }
79
79
 
80
- function hasInputAddress (payload) {
81
- return hasAddressesObjectInputAddress(payload.ephemeral) || hasAddressesObjectInputAddress(payload.persistent)
82
- }
83
-
84
- function hasAddressesObjectInputAddress (addressesObject) {
85
- return addressesObject && Object.keys(addressesObject)
86
- .some(address => address.startsWith('server.request') || address.startsWith('graphql.server'))
87
- }
88
-
89
- function clearQuerySet ({ payload }) {
90
- if (!payload) return
91
-
92
- const req = getActiveRequest()
93
- if (!req) return
80
+ function clearQuerySet ({ req, payload }) {
81
+ if (!req || !payload) return
94
82
 
95
83
  const executedQueries = reqQueryMap.get(req)
96
84
  if (!executedQueries) return
97
85
 
98
- if (hasInputAddress(payload)) {
86
+ const hasInput = Object.keys(payload)
87
+ .some(address => address.startsWith('server.request') || address.startsWith('graphql.server'))
88
+ if (hasInput) {
99
89
  executedQueries.clear()
100
90
  }
101
91
  }
@@ -2,7 +2,10 @@
2
2
 
3
3
  const { format } = require('url')
4
4
  const {
5
+ fetchRequestStart,
5
6
  httpClientRequestStart,
7
+ httpClientRequestError,
8
+ httpClientRequestFinish,
6
9
  httpClientResponseStart,
7
10
  httpClientResponseFinish,
8
11
  } = require('../channels')
@@ -21,17 +24,23 @@ function enable (_config) {
21
24
  config = _config
22
25
  downstream.enable(_config)
23
26
 
27
+ httpClientRequestFinish.subscribe(handleRequestFinish)
28
+ httpClientRequestError.subscribe(handleRequestError)
24
29
  httpClientRequestStart.subscribe(analyzeSsrf)
25
30
  httpClientResponseStart.subscribe(planResponseBodyCollection)
26
31
  httpClientResponseFinish.subscribe(handleResponseFinish)
32
+ fetchRequestStart.subscribe(analyzeFetchSsrf)
27
33
  }
28
34
 
29
35
  function disable () {
30
36
  downstream.disable()
31
37
 
38
+ httpClientRequestFinish.unsubscribe(handleRequestFinish)
39
+ httpClientRequestError.unsubscribe(handleRequestError)
32
40
  if (httpClientRequestStart.hasSubscribers) httpClientRequestStart.unsubscribe(analyzeSsrf)
33
41
  if (httpClientResponseStart.hasSubscribers) httpClientResponseStart.unsubscribe(planResponseBodyCollection)
34
42
  if (httpClientResponseFinish.hasSubscribers) httpClientResponseFinish.unsubscribe(handleResponseFinish)
43
+ if (fetchRequestStart.hasSubscribers) fetchRequestStart.unsubscribe(analyzeFetchSsrf)
35
44
  }
36
45
 
37
46
  function analyzeSsrf (ctx) {
@@ -40,33 +49,85 @@ function analyzeSsrf (ctx) {
40
49
 
41
50
  if (!req || !outgoingUrl) return
42
51
 
43
- const requestAddresses = downstream.extractRequestData(ctx)
52
+ // The subcontext stays on ctx for the response evaluation of the same outgoing request.
53
+ runRequestEvaluation(req, outgoingUrl, downstream.extractRequestData(ctx), ctx.abortController, ctx)
54
+ }
55
+
56
+ /**
57
+ * Evaluates an outgoing request of the global fetch before it is sent.
58
+ * @param {{ req: Request, init?: import('undici-types').RequestInit, abortController: AbortController }} ctx fetch
59
+ * instrumentation context, where `init` holds the options that fetch applies over `req`.
60
+ */
61
+ function analyzeFetchSsrf ({ req: request, init, abortController }) {
62
+ const req = getActiveRequest()
63
+ if (!req) return
44
64
 
45
- const ephemeral = {
65
+ // Fetch responses are not evaluated, so the request runs in a subcontext released right away.
66
+ runRequestEvaluation(req, request.url, downstream.extractFetchRequestData(request, init), abortController)
67
+ }
68
+
69
+ /**
70
+ * Runs the request variant of SSRF RASP and aborts the outgoing request when a rule blocks it.
71
+ * @param {import('http').IncomingMessage} req originating request.
72
+ * @param {string} outgoingUrl URL of the outgoing request.
73
+ * @param {object} requestAddresses method and headers of the outgoing request.
74
+ * @param {AbortController} abortController aborts the outgoing request.
75
+ * @param {import('../waf').SubcontextState} [subcontextState] keeps the WAF subcontext for later evaluations.
76
+ */
77
+ function runRequestEvaluation (req, outgoingUrl, requestAddresses, abortController, subcontextState) {
78
+ const subcontextData = {
46
79
  [addresses.HTTP_OUTGOING_URL]: outgoingUrl,
47
80
  ...requestAddresses,
48
81
  }
49
82
 
50
83
  const raspRule = { type: RULE_TYPES.SSRF, variant: 'request' }
51
84
 
52
- const result = waf.run({ ephemeral }, req, raspRule)
85
+ const result = waf.runSubContext(subcontextData, raspRule, subcontextState)
53
86
 
54
- handleResult(result, req, web.getContext(req)?.res, ctx.abortController, config, raspRule)
87
+ handleResult(result, req, web.getContext(req)?.res, abortController, config, raspRule)
55
88
 
56
89
  downstream.incrementDownstreamAnalysisCount(req)
57
90
  }
58
91
 
59
92
  /**
60
- * Channel handler: plans downstream response body capture once response headers are available.
93
+ * Evaluates redirects before the next hop, or plans response body capture.
61
94
  * @param {{ ctx: object, res: import('http').IncomingMessage }} payload channel payload.
62
95
  */
63
96
  function planResponseBodyCollection ({ ctx, res }) {
64
- const originatingRequest = getActiveRequest()
97
+ const originatingRequest = waf.getRequest(ctx)
65
98
  if (!originatingRequest || !res) return
66
99
 
100
+ if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
101
+ // Record the redirect before application code can start the next hop.
102
+ ctx.ssrfResponseEvaluated = true
103
+ runResponseEvaluation(res, originatingRequest, null, ctx)
104
+ return
105
+ }
106
+
67
107
  downstream.planResponseBodyCollection(originatingRequest, res, ctx)
68
108
  }
69
109
 
110
+ /** @param {{ error?: Error, wafSubcontext?: import('../waf/waf_context_wrapper') }} ctx HTTP request context. */
111
+ function handleRequestError (ctx) {
112
+ // The error channel also publishes timeouts without an error; those can still receive a response.
113
+ if (ctx.error) waf.disposeSubContext(ctx)
114
+ }
115
+
116
+ /**
117
+ * @param {import('../waf').SubcontextState & { req?: import('http').ClientRequest,
118
+ * res?: import('http').IncomingMessage }} ctx HTTP request context.
119
+ */
120
+ function handleRequestFinish (ctx) {
121
+ // Ordinary responses still need their response-finish evaluation, including truncated bodies.
122
+ if (!waf.getRequest(ctx) || (ctx.res && !ctx.res.upgrade)) return
123
+ // Request finish is emitted only once and can precede a response after a timeout.
124
+ if (!ctx.res && ctx.req && !ctx.req.destroyed) {
125
+ ctx.req.once('close', () => handleRequestFinish(ctx))
126
+ return
127
+ }
128
+ waf.disposeSubContext(ctx)
129
+ }
130
+
70
131
  /**
71
132
  * Finalizes body collection for the response and triggers RASP analysis.
72
133
  * @param {object} params event payload from the channel.
@@ -75,14 +136,16 @@ function planResponseBodyCollection ({ ctx, res }) {
75
136
  * @param {string|Buffer|null} params.body collected body.
76
137
  */
77
138
  function handleResponseFinish ({ ctx, res, body }) {
78
- // downstream response object
79
- if (!res) return
80
-
81
- const originatingRequest = getActiveRequest()
82
- if (!originatingRequest) return
83
-
84
- const responseBody = ctx.shouldCollectBody ? body : null
85
- runResponseEvaluation(res, originatingRequest, responseBody)
139
+ const req = waf.getRequest(ctx)
140
+ if (!req) return
141
+
142
+ try {
143
+ if (!res || ctx.ssrfResponseEvaluated) return
144
+ const responseBody = ctx.shouldCollectBody ? body : null
145
+ runResponseEvaluation(res, req, responseBody, ctx)
146
+ } finally {
147
+ waf.disposeSubContext(ctx)
148
+ }
86
149
  }
87
150
 
88
151
  /**
@@ -90,14 +153,15 @@ function handleResponseFinish ({ ctx, res, body }) {
90
153
  * @param {import('http').IncomingMessage} res incoming response from downstream service.
91
154
  * @param {import('http').IncomingMessage} req originating request.
92
155
  * @param {string|Buffer|null} responseBody collected downstream response body.
156
+ * @param {object} ctx - Outgoing HTTP instrumentation context.
93
157
  */
94
- function runResponseEvaluation (res, req, responseBody) {
158
+ function runResponseEvaluation (res, req, responseBody, ctx) {
95
159
  const responseAddresses = downstream.extractResponseData(res, responseBody)
96
160
 
97
161
  if (isEmpty(responseAddresses)) return
98
162
 
99
163
  const raspRule = { type: RULE_TYPES.SSRF, variant: 'response' }
100
- const result = waf.run({ ephemeral: responseAddresses }, req, raspRule)
164
+ const result = waf.runSubContext(responseAddresses, raspRule, ctx)
101
165
 
102
166
  // TODO: this should be done in the waf functions directly instead of calling it everywhere
103
167
  const ruleTriggered = !!result?.events?.length
@@ -2,7 +2,7 @@
2
2
 
3
3
  const web = require('../../plugins/util/web')
4
4
  const { getCallsiteFrames, reportStackTrace, canReportStackTrace } = require('../stack_trace')
5
- const { getBlockingAction } = require('../blocking')
5
+ const { block, getBlockingAction, isBlocked } = require('../blocking')
6
6
  const log = require('../../log')
7
7
  const { updateRaspRuleMatchMetricTags } = require('../telemetry')
8
8
 
@@ -24,6 +24,18 @@ const ALLOWED_ROOTSPAN_NAMES = new Set([
24
24
  'fastify.request',
25
25
  ])
26
26
 
27
+ /** @type {WeakMap<import('http').IncomingMessage, ((error: Error) => void) | undefined>} */
28
+ const requestsToBlockOnDetection = new WeakMap()
29
+
30
+ /**
31
+ * @param {import('http').IncomingMessage} req
32
+ * @param {(error: Error) => void} [ignoreError] Keeps the abort error of a blocked request out of the framework's
33
+ * request errors.
34
+ */
35
+ function markToBlockOnDetection (req, ignoreError) {
36
+ requestsToBlockOnDetection.set(req, ignoreError)
37
+ }
38
+
27
39
  class DatadogRaspAbortError extends Error {
28
40
  constructor (req, res, blockingAction, raspRule, ruleTriggered) {
29
41
  super('DatadogRaspAbortError')
@@ -67,10 +79,27 @@ function handleResult (result, req, res, abortController, config, raspRule) {
67
79
 
68
80
  if (abortController && !abortOnUncaughtException) {
69
81
  const blockingAction = getBlockingAction(result?.actions)
70
- const rootSpanName = rootSpan?.context?.()?._name
82
+ const shouldBlockOnDetection = requestsToBlockOnDetection.has(req)
71
83
 
72
- if (blockingAction && ALLOWED_ROOTSPAN_NAMES.has(rootSpanName)) {
84
+ if (blockingAction && (shouldBlockOnDetection || ALLOWED_ROOTSPAN_NAMES.has(rootSpan?.context?.()?._name))) {
73
85
  const abortError = new DatadogRaspAbortError(req, res, blockingAction, raspRule, ruleTriggered)
86
+
87
+ if (shouldBlockOnDetection) {
88
+ // The blocking response is sent before the abort: once the operation fails, the framework can destroy the
89
+ // response before writing anything, as Next.js does when a stream fails before its first chunk.
90
+ let blocked = false
91
+ if (res) {
92
+ blocked = isBlocked(res) || block(req, res, rootSpan, null, blockingAction)
93
+ }
94
+ if (blocked) {
95
+ const ignoreError = requestsToBlockOnDetection.get(req)
96
+ ignoreError?.(abortError)
97
+ }
98
+ if (ruleTriggered) {
99
+ updateRaspRuleMatchMetricTags(req, raspRule, true, blocked)
100
+ }
101
+ }
102
+
74
103
  abortController.abort(abortError)
75
104
 
76
105
  return
@@ -84,6 +113,7 @@ function handleResult (result, req, res, abortController, config, raspRule) {
84
113
 
85
114
  module.exports = {
86
115
  handleResult,
116
+ markToBlockOnDetection,
87
117
  RULE_TYPES,
88
118
  DatadogRaspAbortError,
89
119
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": "2.2",
3
3
  "metadata": {
4
- "rules_version": "1.19.0"
4
+ "rules_version": "1.20.0"
5
5
  },
6
6
  "rules": [
7
7
  {
@@ -6012,6 +6012,39 @@
6012
6012
  "keys_only"
6013
6013
  ]
6014
6014
  },
6015
+ {
6016
+ "id": "dog-920-210",
6017
+ "name": "Java4Shell - Attempts to exploit Java's base64 misdecoding (CVE-2026-114938)",
6018
+ "tags": {
6019
+ "type": "exploit_detection",
6020
+ "category": "attack_attempt",
6021
+ "cwe": "416",
6022
+ "capec": "1000/152/242",
6023
+ "confidence": "0",
6024
+ "module": "waf"
6025
+ },
6026
+ "conditions": [
6027
+ {
6028
+ "parameters": {
6029
+ "inputs": [
6030
+ {
6031
+ "address": "server.request.body"
6032
+ },
6033
+ {
6034
+ "address": "server.request.headers.no_cookies"
6035
+ }
6036
+ ],
6037
+ "regex": "^(?i:basic\\s+)?(?:(?:[A-Za-z0-9+/]{64})+|(?:[A-Za-z0-9_-]{64})+)(?:[A-Za-z0-9+/_-]?={1,2}|[A-Za-z0-9+/_-]{2}=)$",
6038
+ "options": {
6039
+ "case_sensitive": true,
6040
+ "min_length": 65
6041
+ }
6042
+ },
6043
+ "operator": "match_regex"
6044
+ }
6045
+ ],
6046
+ "transformers": []
6047
+ },
6015
6048
  {
6016
6049
  "id": "dog-931-001",
6017
6050
  "name": "RFI: URL Payload to well known RFI target",
@@ -6233,9 +6266,9 @@
6233
6266
  "address": "graphql.server.resolver"
6234
6267
  }
6235
6268
  ],
6236
- "regex": "{[{%]-?(?:(?:\\s*\\d+\\s*[*+-]\\s*\\d+\\s*)|(?:[\\s\\S]*__(?:init|class|globals|builtins|import|mro|bases?|subclasses|dict|getitem|getattribute)__[\\s\\S]*)|(?:[\\s\\S]*\\|\\s*(?:reverse|join|format|attr)\\b[\\s\\S]*))-?[}%]}",
6269
+ "regex": "{[{%]-?(?:(?:\\s*\\d+\\s*[*+-]\\s*\\d+\\s*)|(?:[\\s\\S]*__(?:init|class|globals|builtins|import|mro|bases?|subclasses|dict|getitem|getattribute)__[\\s\\S]*)|(?:(?:{}|[^}])*\\|\\s*(?:reverse|join|format|attr)\\b[\\s\\S]*))-?[}%]}",
6237
6270
  "options": {
6238
- "case_sensitive": false,
6271
+ "case_sensitive": true,
6239
6272
  "min_length": 7
6240
6273
  }
6241
6274
  },
@@ -10095,6 +10128,40 @@
10095
10128
  ],
10096
10129
  "transformers": []
10097
10130
  },
10131
+ {
10132
+ "id": "ua0-600-71x",
10133
+ "name": "Artex",
10134
+ "tags": {
10135
+ "type": "attack_tool",
10136
+ "category": "attack_attempt",
10137
+ "cwe": "200",
10138
+ "capec": "1000/118/169",
10139
+ "tool_name": "Artex",
10140
+ "confidence": "0",
10141
+ "module": "waf"
10142
+ },
10143
+ "conditions": [
10144
+ {
10145
+ "parameters": {
10146
+ "inputs": [
10147
+ {
10148
+ "address": "server.request.headers.no_cookies",
10149
+ "key_path": [
10150
+ "user-agent"
10151
+ ]
10152
+ }
10153
+ ],
10154
+ "regex": "\\bartex(?:[\\W_]|$)",
10155
+ "options": {
10156
+ "case_sensitive": false,
10157
+ "min_length": 5
10158
+ }
10159
+ },
10160
+ "operator": "match_regex"
10161
+ }
10162
+ ],
10163
+ "transformers": []
10164
+ },
10098
10165
  {
10099
10166
  "id": "ua0-600-7xx",
10100
10167
  "name": "SQLmap",
@@ -26,6 +26,14 @@ const { DIAGNOSTIC_KEYS } = require('./waf/diagnostics')
26
26
  const REQUEST_HEADER_TAG_PREFIX = 'http.request.headers.'
27
27
  const RESPONSE_HEADER_TAG_PREFIX = 'http.response.headers.'
28
28
 
29
+ // Preserve the first redirect target across independently evaluated outgoing requests.
30
+ const REDIRECT_TARGET_ATTRIBUTES = new Set([
31
+ 'appsec.api.redirection.move_target',
32
+ 'appsec.api.redirection.redirect_target',
33
+ 'api.security.redirection.move_target',
34
+ 'api.security.redirection.redirect_target',
35
+ ])
36
+
29
37
  const COLLECTED_REQUEST_BODY_MAX_STRING_LENGTH = 4096
30
38
  const COLLECTED_REQUEST_BODY_MAX_DEPTH = 20
31
39
  const COLLECTED_REQUEST_BODY_MAX_ELEMENTS_PER_NODE = 256
@@ -560,6 +568,7 @@ function reportAttributes (attributes, req, rootSpan) {
560
568
 
561
569
  const tags = {}
562
570
  for (let [tag, value] of Object.entries(attributes)) {
571
+ if (REDIRECT_TARGET_ATTRIBUTES.has(tag) && rootSpan.context().hasTag(tag)) continue
563
572
  if (isSchemaAttribute(tag)) {
564
573
  const gzippedValue = zlib.gzipSync(JSON.stringify(value))
565
574
  value = gzippedValue.toString('base64')
@@ -27,15 +27,15 @@ function setUser (tracer, user) {
27
27
 
28
28
  setUserTags(user, rootSpan)
29
29
 
30
- const persistent = {
30
+ const requestData = {
31
31
  [addresses.USER_ID]: String(user.id),
32
32
  }
33
33
 
34
34
  if (user.session_id && typeof user.session_id === 'string') {
35
- persistent[addresses.USER_SESSION_ID] = user.session_id
35
+ requestData[addresses.USER_SESSION_ID] = user.session_id
36
36
  }
37
37
 
38
- waf.run({ persistent })
38
+ waf.run(requestData)
39
39
  }
40
40
 
41
41
  module.exports = {
@@ -211,19 +211,19 @@ function trackEvent (eventName, fields, sdkMethodName, rootSpan) {
211
211
  }
212
212
 
213
213
  function runWaf (eventName, user) {
214
- const persistent = {
214
+ const requestData = {
215
215
  [`server.business_logic.${eventName}`]: null,
216
216
  }
217
217
 
218
218
  if (user?.id) {
219
- persistent[addresses.USER_ID] = String(user.id)
219
+ requestData[addresses.USER_ID] = String(user.id)
220
220
  }
221
221
 
222
222
  if (user?.login) {
223
- persistent[addresses.USER_LOGIN] = String(user.login)
223
+ requestData[addresses.USER_LOGIN] = String(user.login)
224
224
  }
225
225
 
226
- waf.run({ persistent })
226
+ waf.run(requestData)
227
227
  }
228
228
 
229
229
  module.exports = {
@@ -10,7 +10,7 @@ const { setUserTags } = require('./set_user')
10
10
  const { getRootSpan } = require('./utils')
11
11
 
12
12
  function isUserBlocked (user) {
13
- const results = waf.run({ persistent: { [USER_ID]: user.id } })
13
+ const results = waf.run({ [USER_ID]: user.id })
14
14
  return !!getBlockingAction(results?.actions)
15
15
  }
16
16