dd-trace 5.136.0 → 5.138.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 (190) 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/index.d.ts +56 -0
  8. package/initialize.mjs +5 -3
  9. package/package.json +36 -32
  10. package/packages/datadog-esbuild/index.js +8 -15
  11. package/packages/datadog-esbuild/src/log.js +2 -29
  12. package/packages/datadog-instrumentations/src/ai.js +88 -122
  13. package/packages/datadog-instrumentations/src/aws-lambda.js +9 -0
  14. package/packages/datadog-instrumentations/src/cypress-config.js +20 -9
  15. package/packages/datadog-instrumentations/src/helpers/bundler-esm-exports.js +23 -1
  16. package/packages/datadog-instrumentations/src/helpers/bundler-log.js +38 -0
  17. package/packages/datadog-instrumentations/src/helpers/bundler-transform.js +9 -6
  18. package/packages/datadog-instrumentations/src/helpers/fetch.js +51 -6
  19. package/packages/datadog-instrumentations/src/helpers/hooks.js +0 -1
  20. package/packages/datadog-instrumentations/src/helpers/instrument.js +1 -3
  21. package/packages/datadog-instrumentations/src/helpers/rewriter/index.js +6 -0
  22. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentation-registry.js +12 -1
  23. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/ai.js +2 -0
  24. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/graphql-jit.js +5 -3
  25. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/modelcontextprotocol-client.js +30 -0
  26. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/modelcontextprotocol-server.js +23 -0
  27. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/next.js +21 -0
  28. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/postgres.js +33 -4
  29. package/packages/datadog-instrumentations/src/helpers/rewriter/target-patterns.json +30 -0
  30. package/packages/datadog-instrumentations/src/helpers/rewriter/targets.json +6 -0
  31. package/packages/datadog-instrumentations/src/helpers/rewriter/transforms/postgres.js +122 -0
  32. package/packages/datadog-instrumentations/src/microgateway-core.js +1 -2
  33. package/packages/datadog-instrumentations/src/modelcontextprotocol-client.js +21 -0
  34. package/packages/datadog-instrumentations/src/modelcontextprotocol-sdk.js +32 -2
  35. package/packages/datadog-instrumentations/src/modelcontextprotocol-server.js +43 -0
  36. package/packages/datadog-instrumentations/src/next.js +184 -20
  37. package/packages/datadog-instrumentations/src/path-to-regexp.js +11 -11
  38. package/packages/datadog-instrumentations/src/router.js +52 -13
  39. package/packages/datadog-instrumentations/src/vitest-main-no-worker-init.js +1 -1
  40. package/packages/datadog-instrumentations/src/vitest-main.js +10 -2
  41. package/packages/datadog-plugin-aws-lambda/src/arn.js +41 -0
  42. package/packages/datadog-plugin-aws-lambda/src/context/events.js +167 -0
  43. package/packages/datadog-plugin-aws-lambda/src/context/extractor.js +153 -0
  44. package/packages/datadog-plugin-aws-lambda/src/context/http.js +50 -0
  45. package/packages/datadog-plugin-aws-lambda/src/context/span-context.js +26 -0
  46. package/packages/datadog-plugin-aws-lambda/src/context/step-functions.js +142 -0
  47. package/packages/datadog-plugin-aws-lambda/src/context/types.d.ts +17 -0
  48. package/packages/datadog-plugin-aws-lambda/src/context/xray.js +67 -0
  49. package/packages/datadog-plugin-aws-lambda/src/event-type-guards.js +170 -0
  50. package/packages/datadog-plugin-aws-lambda/src/index.js +52 -4
  51. package/packages/datadog-plugin-aws-lambda/src/trigger.js +339 -0
  52. package/packages/datadog-plugin-aws-sdk/src/services/dynamodb.js +7 -0
  53. package/packages/datadog-plugin-aws-sdk/src/services/kinesis.js +32 -10
  54. package/packages/datadog-plugin-aws-sdk/src/services/lambda.js +10 -1
  55. package/packages/datadog-plugin-aws-sdk/src/services/sqs.js +4 -0
  56. package/packages/datadog-plugin-aws-sdk/src/services/stepfunctions.js +3 -0
  57. package/packages/datadog-plugin-azure-functions/src/index.js +68 -12
  58. package/packages/datadog-plugin-cypress/src/cypress-plugin.js +30 -4
  59. package/packages/datadog-plugin-cypress/src/support.mjs +1353 -0
  60. package/packages/datadog-plugin-express/src/code_origin.js +2 -2
  61. package/packages/datadog-plugin-fastify/src/code_origin.js +2 -2
  62. package/packages/datadog-plugin-fetch/src/index.js +7 -0
  63. package/packages/datadog-plugin-google-cloud-pubsub/src/consumer.js +10 -0
  64. package/packages/datadog-plugin-google-cloud-pubsub/src/producer.js +2 -0
  65. package/packages/datadog-plugin-google-cloud-pubsub/src/pubsub-push-subscription.js +7 -0
  66. package/packages/datadog-plugin-google-cloud-pubsub/src/resource-names.js +24 -0
  67. package/packages/datadog-plugin-graphql/src/tools/transforms.js +1 -0
  68. package/packages/datadog-plugin-http/src/client.js +14 -1
  69. package/packages/datadog-plugin-http/src/server.js +18 -4
  70. package/packages/datadog-plugin-http2/src/client.js +2 -1
  71. package/packages/datadog-plugin-modelcontextprotocol-sdk/src/tracing.js +28 -2
  72. package/packages/datadog-plugin-modelcontextprotocol-sdk/src/utils.js +1 -0
  73. package/packages/datadog-plugin-mongodb-core/src/query.js +17 -1
  74. package/packages/datadog-plugin-postgres/src/index.js +118 -41
  75. package/packages/datadog-plugin-supabase/src/supabase-auth-js-gotrueclient-getuser.js +18 -8
  76. package/packages/datadog-plugin-supabase/src/supabase-functions-js-functionsclient-invoke.js +49 -13
  77. package/packages/datadog-plugin-supabase/src/supabase-storage-js-handle-request.js +34 -9
  78. package/packages/datadog-plugin-vitest/src/index.js +3 -2
  79. package/packages/datadog-plugin-ws/src/close.js +3 -5
  80. package/packages/datadog-plugin-ws/src/producer.js +1 -4
  81. package/packages/datadog-plugin-ws/src/receiver.js +3 -5
  82. package/packages/datadog-plugin-ws/src/server.js +44 -13
  83. package/packages/datadog-vite/index.js +537 -0
  84. package/packages/datadog-vite/src/log.js +5 -0
  85. package/packages/datadog-webpack/src/log.js +2 -29
  86. package/packages/dd-trace/src/aiguard/client.js +7 -9
  87. package/packages/dd-trace/src/aiguard/evaluation.js +7 -2
  88. package/packages/dd-trace/src/aiguard/index.js +1 -1
  89. package/packages/dd-trace/src/aiguard/integrations/index.js +6 -5
  90. package/packages/dd-trace/src/aiguard/messages/utils.js +2 -2
  91. package/packages/dd-trace/src/aiguard/sdk.js +7 -2
  92. package/packages/dd-trace/src/appsec/api_security/normalized-route.js +273 -167
  93. package/packages/dd-trace/src/appsec/blocking/index.js +17 -1
  94. package/packages/dd-trace/src/appsec/channels.js +8 -0
  95. package/packages/dd-trace/src/appsec/downstream_requests.js +105 -0
  96. package/packages/dd-trace/src/appsec/graphql.js +10 -13
  97. package/packages/dd-trace/src/appsec/handlers/auth.js +1 -3
  98. package/packages/dd-trace/src/appsec/handlers/http-request.js +60 -65
  99. package/packages/dd-trace/src/appsec/handlers/http-response.js +61 -7
  100. package/packages/dd-trace/src/appsec/handlers/payments.js +23 -27
  101. package/packages/dd-trace/src/appsec/iast/taint-tracking/plugin.js +21 -14
  102. package/packages/dd-trace/src/appsec/index.js +9 -0
  103. package/packages/dd-trace/src/appsec/lambda.js +17 -17
  104. package/packages/dd-trace/src/appsec/rasp/command_injection.js +4 -4
  105. package/packages/dd-trace/src/appsec/rasp/index.js +64 -3
  106. package/packages/dd-trace/src/appsec/rasp/lfi.js +2 -2
  107. package/packages/dd-trace/src/appsec/rasp/sql_injection.js +7 -17
  108. package/packages/dd-trace/src/appsec/rasp/ssrf.js +80 -16
  109. package/packages/dd-trace/src/appsec/rasp/utils.js +47 -3
  110. package/packages/dd-trace/src/appsec/recommended.json +70 -3
  111. package/packages/dd-trace/src/appsec/reporter.js +9 -0
  112. package/packages/dd-trace/src/appsec/sdk/set_user.js +3 -3
  113. package/packages/dd-trace/src/appsec/sdk/track_event.js +4 -4
  114. package/packages/dd-trace/src/appsec/sdk/user_blocking.js +1 -1
  115. package/packages/dd-trace/src/appsec/user_tracking.js +6 -8
  116. package/packages/dd-trace/src/appsec/waf/index.js +65 -6
  117. package/packages/dd-trace/src/appsec/waf/waf_context_wrapper.js +33 -38
  118. package/packages/dd-trace/src/appsec/waf/waf_manager.js +18 -2
  119. package/packages/dd-trace/src/carrier.js +7 -1
  120. package/packages/dd-trace/src/ci-visibility/dynamic-instrumentation/index.js +4 -2
  121. package/packages/dd-trace/src/ci-visibility/dynamic-instrumentation/worker/index.js +31 -19
  122. package/packages/dd-trace/src/config/generated-config-types.d.ts +14 -5
  123. package/packages/dd-trace/src/config/index.js +73 -27
  124. package/packages/dd-trace/src/config/parsers.js +60 -0
  125. package/packages/dd-trace/src/config/remote-config-layers.js +38 -0
  126. package/packages/dd-trace/src/config/remote_config.js +2 -1
  127. package/packages/dd-trace/src/config/supported-configurations.json +41 -13
  128. package/packages/dd-trace/src/datastreams/fnv.js +27 -0
  129. package/packages/dd-trace/src/datastreams/http.js +57 -0
  130. package/packages/dd-trace/src/datastreams/index.js +24 -0
  131. package/packages/dd-trace/src/datastreams/pathway.js +142 -28
  132. package/packages/dd-trace/src/datastreams/processor.js +27 -7
  133. package/packages/dd-trace/src/debugger/config.js +1 -1
  134. package/packages/dd-trace/src/debugger/devtools_client/request-options.js +7 -9
  135. package/packages/dd-trace/src/debugger/devtools_client/send.js +1 -1
  136. package/packages/dd-trace/src/debugger/devtools_client/state.js +3 -1
  137. package/packages/dd-trace/src/debugger/devtools_client/tags.js +1 -1
  138. package/packages/dd-trace/src/debugger/index.js +2 -2
  139. package/packages/dd-trace/src/debugger/inspect-segment.js +3 -2
  140. package/packages/dd-trace/src/debugger/pause-duration-histogram.js +4 -1
  141. package/packages/dd-trace/src/debugger/probe_sampler.js +39 -17
  142. package/packages/dd-trace/src/debugger/unsupported-runtime.js +2 -0
  143. package/packages/dd-trace/src/git_metadata.js +2 -1
  144. package/packages/dd-trace/src/index.js +6 -3
  145. package/packages/dd-trace/src/lambda/facade.js +6 -5
  146. package/packages/dd-trace/src/lambda/handler-paths.js +1 -1
  147. package/packages/dd-trace/src/llmobs/constants/tags.js +1 -0
  148. package/packages/dd-trace/src/llmobs/plugins/modelcontextprotocol-sdk/index.js +86 -3
  149. package/packages/dd-trace/src/llmobs/plugins/modelcontextprotocol-sdk/utils.js +182 -1
  150. package/packages/dd-trace/src/llmobs/span_processor.js +3 -0
  151. package/packages/dd-trace/src/llmobs/tagger.js +9 -0
  152. package/packages/dd-trace/src/noop/span.js +0 -4
  153. package/packages/dd-trace/src/openfeature/agentless_configuration_source.js +3 -0
  154. package/packages/dd-trace/src/openfeature/configuration_source.js +20 -0
  155. package/packages/dd-trace/src/openfeature/flagging_provider.js +17 -0
  156. package/packages/dd-trace/src/opentelemetry/logs/otlp_transformer.js +2 -1
  157. package/packages/dd-trace/src/opentelemetry/metrics/otlp_transformer.js +2 -1
  158. package/packages/dd-trace/src/opentelemetry/otlp/otlp_transformer_base.js +16 -1
  159. package/packages/dd-trace/src/opentelemetry/trace/index.js +5 -4
  160. package/packages/dd-trace/src/opentelemetry/trace/otlp_http_trace_exporter.js +10 -6
  161. package/packages/dd-trace/src/opentelemetry/trace/otlp_transformer.js +42 -26
  162. package/packages/dd-trace/src/opentracing/propagation/text_map.js +4 -4
  163. package/packages/dd-trace/src/plugin_manager.js +2 -0
  164. package/packages/dd-trace/src/plugins/ci_plugin.js +104 -9
  165. package/packages/dd-trace/src/plugins/index.js +2 -0
  166. package/packages/dd-trace/src/plugins/util/ci.js +34 -0
  167. package/packages/dd-trace/src/plugins/util/http-otel-semantics.js +9 -14
  168. package/packages/dd-trace/src/plugins/util/inferred_proxy.js +3 -5
  169. package/packages/dd-trace/src/plugins/util/test.js +28 -1
  170. package/packages/dd-trace/src/plugins/util/url.js +9 -6
  171. package/packages/dd-trace/src/plugins/util/user-provided-git.js +4 -4
  172. package/packages/dd-trace/src/process-tags/index.js +1 -1
  173. package/packages/dd-trace/src/profiler.js +74 -12
  174. package/packages/dd-trace/src/profiling/profiler.js +30 -2
  175. package/packages/dd-trace/src/profiling/profilers/space.js +3 -1
  176. package/packages/dd-trace/src/profiling/profilers/wall.js +4 -4
  177. package/packages/dd-trace/src/profiling/remote-config.js +354 -0
  178. package/packages/dd-trace/src/profiling/runtime-capabilities.js +11 -0
  179. package/packages/dd-trace/src/propagation-hash/index.js +7 -0
  180. package/packages/dd-trace/src/proxy.js +13 -1
  181. package/packages/dd-trace/src/register-loader.js +43 -0
  182. package/register.js +10 -44
  183. package/register.mjs +27 -0
  184. package/vendor/dist/@opentelemetry/core/LICENSE +11 -0
  185. package/vendor/dist/@opentelemetry/core/index.js +1 -1
  186. package/vite.d.ts +7 -0
  187. package/vite.js +3 -0
  188. package/lambda.d.ts +0 -39
  189. package/lambda.js +0 -3
  190. package/packages/datadog-plugin-cypress/src/support.js +0 -1348
@@ -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
 
@@ -87,7 +87,7 @@ function trackLogin (framework, login, user, success, rootSpan) {
87
87
 
88
88
  let newTags
89
89
 
90
- const persistent = {
90
+ const requestData = {
91
91
  [addresses.USER_LOGIN]: login,
92
92
  }
93
93
 
@@ -116,11 +116,11 @@ function trackLogin (framework, login, user, success, rootSpan) {
116
116
 
117
117
  if (shouldSetTag('usr.id')) {
118
118
  newTags['usr.id'] = userId
119
- persistent[addresses.USER_ID] = userId
119
+ requestData[addresses.USER_ID] = userId
120
120
  }
121
121
  }
122
122
 
123
- persistent[addresses.LOGIN_SUCCESS] = null
123
+ requestData[addresses.LOGIN_SUCCESS] = null
124
124
  } else {
125
125
  newTags = {
126
126
  'appsec.events.users.login.failure.track': 'true',
@@ -146,14 +146,14 @@ function trackLogin (framework, login, user, success, rootSpan) {
146
146
  }
147
147
  */
148
148
 
149
- persistent[addresses.LOGIN_FAILURE] = null
149
+ requestData[addresses.LOGIN_FAILURE] = null
150
150
  }
151
151
 
152
152
  keepTrace(rootSpan, ASM)
153
153
 
154
154
  rootSpan.addTags(newTags)
155
155
 
156
- return waf.run({ persistent })
156
+ return waf.run(requestData)
157
157
  }
158
158
 
159
159
  function trackUser (user, rootSpan) {
@@ -177,9 +177,7 @@ function trackUser (user, rootSpan) {
177
177
  })
178
178
 
179
179
  return waf.run({
180
- persistent: {
181
- [addresses.USER_ID]: userId,
182
- },
180
+ [addresses.USER_ID]: userId,
183
181
  })
184
182
  }
185
183
  }
@@ -37,6 +37,9 @@ const waf = {
37
37
  removeConfig,
38
38
  checkAsmDdFallback,
39
39
  run: noop,
40
+ runSubContext: noop,
41
+ disposeSubContext,
42
+ getRequest,
40
43
  disposeContext: noop,
41
44
  WafUpdateError,
42
45
  }
@@ -55,6 +58,7 @@ function init (rules, config) {
55
58
 
56
59
  waf.wafManager = new WAFManager(rules, config)
57
60
 
61
+ waf.runSubContext = runSubContext
58
62
  waf.run = run
59
63
  waf.disposeContext = disposeContext
60
64
  }
@@ -65,6 +69,7 @@ function destroy () {
65
69
  waf.wafManager = null
66
70
  }
67
71
 
72
+ waf.runSubContext = noop
68
73
  waf.run = noop
69
74
  waf.disposeContext = noop
70
75
  }
@@ -116,7 +121,7 @@ function removeConfig (configPath) {
116
121
  // @see packages/dd-trace/test/appsec/lambda.spec.js
117
122
 
118
123
  /**
119
- * @param {object} data - WAF address data ({ persistent, ephemeral })
124
+ * @param {object} data - Flat WAF address map
120
125
  * @param {object} req - Request key for WAF context lookup. May be an HTTP
121
126
  * IncomingMessage or a plain object (Lambda invocation key).
122
127
  * @param {string} [raspRule] - RASP rule identifier
@@ -132,7 +137,19 @@ function run (data, req, raspRule, rootSpan) {
132
137
  }
133
138
 
134
139
  const wafContext = waf.wafManager.getWAFContext(req)
135
- const result = wafContext.run(data, raspRule, req, rootSpan)
140
+ if (!wafContext) return
141
+ return runContext(data, wafContext, raspRule, rootSpan)
142
+ }
143
+
144
+ /**
145
+ * @param {object} data - Flat WAF address map.
146
+ * @param {import('./waf_context_wrapper')} wafContext - Context selected for this evaluation.
147
+ * @param {object} [raspRule] - RASP rule metadata.
148
+ * @param {object} [rootSpan] - Explicit Lambda root span.
149
+ */
150
+ function runContext (data, wafContext, raspRule, rootSpan) {
151
+ const req = wafContext.parent ? wafContext.parent.req : wafContext.req
152
+ const result = wafContext.run(data, raspRule, rootSpan)
136
153
 
137
154
  if (result?.keep) {
138
155
  if (limiter.isAllowed()) {
@@ -146,12 +163,54 @@ function run (data, req, raspRule, rootSpan) {
146
163
  return result
147
164
  }
148
165
 
149
- function disposeContext (req) {
150
- const wafContext = waf.wafManager.getWAFContext(req)
166
+ /**
167
+ * @typedef {{ wafSubcontext?: import('./waf_context_wrapper') }} SubcontextState
168
+ */
151
169
 
152
- if (wafContext && !wafContext.ddwafContext.disposed) {
153
- wafContext.dispose()
170
+ /**
171
+ * Evaluate an isolated check, or reuse a subcontext across calls carrying the same state.
172
+ * @param {object} data - Flat WAF address map.
173
+ * @param {object} [raspRule] - RASP rule metadata.
174
+ * @param {SubcontextState} [state] - Instrumentation context, released by calling disposeSubContext.
175
+ * @param {object} [req] - Explicit request when the publisher provides it.
176
+ */
177
+ function runSubContext (data, raspRule, state, req) {
178
+ let subcontext = state?.wafSubcontext
179
+ if (!subcontext) {
180
+ req ||= getActiveRequest()
181
+ if (!req) return
182
+ const context = waf.wafManager.getWAFContext(req)
183
+ if (!context) return
184
+ if (context.ddwafContext.disposed) return runContext(data, context, raspRule)
185
+ // Stateful checks can need response inputs even when no request inputs are known.
186
+ if (!state && (data === null || typeof data !== 'object' ||
187
+ !Object.keys(data).some(key => context.knownAddresses.has(key)))) return
188
+ subcontext = context.createSubcontext()
189
+ if (!subcontext) return
190
+ if (state) state.wafSubcontext = subcontext
154
191
  }
192
+ try {
193
+ return runContext(data, subcontext, raspRule)
194
+ } finally {
195
+ if (!state) subcontext.dispose()
196
+ }
197
+ }
198
+
199
+ /** @param {SubcontextState} state - Instrumentation context bound by runSubContext. */
200
+ function getRequest (state) {
201
+ return state.wafSubcontext?.parent.req
202
+ }
203
+
204
+ /** @param {SubcontextState} state - Instrumentation context whose evaluation is complete. */
205
+ function disposeSubContext (state) {
206
+ const subcontext = state.wafSubcontext
207
+ if (!subcontext) return
208
+ state.wafSubcontext = undefined
209
+ subcontext.dispose()
210
+ }
211
+
212
+ function disposeContext (req) {
213
+ waf.wafManager.disposeContext(req)
155
214
  }
156
215
 
157
216
  function noop () {}
@@ -6,14 +6,16 @@ const addresses = require('../addresses')
6
6
  const { getBlockingAction } = require('../blocking')
7
7
  const { wafRunFinished } = require('../channels')
8
8
 
9
- // TODO: remove once ephemeral addresses are implemented
9
+ // Some HTTP hooks can supply the same query more than once.
10
10
  const preventDuplicateAddresses = new Set([
11
11
  addresses.HTTP_INCOMING_QUERY,
12
12
  ])
13
13
 
14
14
  class WAFContextWrapper {
15
- constructor (ddwafContext, wafTimeout, wafVersion, rulesVersion, knownAddresses) {
15
+ constructor (ddwafContext, wafTimeout, wafVersion, rulesVersion, knownAddresses, req, parent) {
16
16
  this.ddwafContext = ddwafContext
17
+ this.parent = parent
18
+ this.req = req
17
19
  this.wafTimeout = wafTimeout
18
20
  this.wafVersion = wafVersion
19
21
  this.rulesVersion = rulesVersion
@@ -36,7 +38,7 @@ class WAFContextWrapper {
36
38
  }
37
39
  }
38
40
 
39
- run ({ persistent, ephemeral }, raspRule, req, rootSpan) {
41
+ run (data, raspRule, rootSpan) {
40
42
  if (this.ddwafContext.disposed) {
41
43
  log.warn('[ASM] Calling run on a disposed context')
42
44
  if (raspRule) {
@@ -46,9 +48,8 @@ class WAFContextWrapper {
46
48
  return
47
49
  }
48
50
 
49
- // SPECIAL CASE FOR USER_ID
50
- // TODO: make this universal
51
- const userId = persistent?.[addresses.USER_ID] || ephemeral?.[addresses.USER_ID]
51
+ const req = this.req
52
+ const userId = data?.[addresses.USER_ID]
52
53
  if (userId) {
53
54
  const cachedResults = this.cachedUserIdResults.get(userId)
54
55
  if (cachedResults) {
@@ -61,42 +62,17 @@ class WAFContextWrapper {
61
62
  // Cloned lazily; only the preventDuplicateAddresses branch below adds to it.
62
63
  let newAddressesToSkip
63
64
 
64
- if (persistent !== null && typeof persistent === 'object') {
65
- const persistentInputs = {}
66
-
67
- let hasPersistentInputs = false
68
- for (const key of Object.keys(persistent)) {
65
+ if (data !== null && typeof data === 'object') {
66
+ for (const key of Object.keys(data)) {
69
67
  if (!this.addressesToSkip.has(key) && this.knownAddresses.has(key)) {
70
- hasPersistentInputs = true
71
- persistentInputs[key] = persistent[key]
72
- if (preventDuplicateAddresses.has(key)) {
68
+ payloadHasData = true
69
+ payload[key] = data[key]
70
+ if (!this.parent && preventDuplicateAddresses.has(key)) {
73
71
  newAddressesToSkip ??= new Set(this.addressesToSkip)
74
72
  newAddressesToSkip.add(key)
75
73
  }
76
74
  }
77
75
  }
78
-
79
- if (hasPersistentInputs) {
80
- payload.persistent = persistentInputs
81
- payloadHasData = true
82
- }
83
- }
84
-
85
- if (ephemeral !== null && typeof ephemeral === 'object') {
86
- const ephemeralInputs = {}
87
-
88
- let hasEphemeral = false
89
- for (const key of Object.keys(ephemeral)) {
90
- if (this.knownAddresses.has(key)) {
91
- hasEphemeral = true
92
- ephemeralInputs[key] = ephemeral[key]
93
- }
94
- }
95
-
96
- if (hasEphemeral) {
97
- payload.ephemeral = ephemeralInputs
98
- payloadHasData = true
99
- }
100
76
  }
101
77
 
102
78
  if (!payloadHasData) return
@@ -168,7 +144,7 @@ class WAFContextWrapper {
168
144
  metrics.errorCode = err.errorCode ?? -127
169
145
  } finally {
170
146
  if (wafRunFinished.hasSubscribers) {
171
- wafRunFinished.publish({ payload })
147
+ wafRunFinished.publish({ payload, req, isSubcontext: !!this.parent })
172
148
  }
173
149
 
174
150
  Reporter.reportMetrics(metrics, raspRule, req, rootSpan)
@@ -195,8 +171,27 @@ class WAFContextWrapper {
195
171
  }
196
172
  }
197
173
 
174
+ createSubcontext () {
175
+ if (this.parent || this.ddwafContext.disposed) return
176
+ try {
177
+ const subcontext = new WAFContextWrapper(
178
+ this.ddwafContext.createSubcontext(), this.wafTimeout, this.wafVersion,
179
+ this.rulesVersion, this.knownAddresses, this.req, this
180
+ )
181
+ this.subcontexts ??= new Set()
182
+ this.subcontexts.add(subcontext)
183
+ return subcontext
184
+ } catch (error) {
185
+ log.error('[ASM] Error creating an AppSec WAF subcontext', error)
186
+ }
187
+ }
188
+
198
189
  dispose () {
199
- this.ddwafContext.dispose()
190
+ this.parent?.subcontexts.delete(this)
191
+ if (this.subcontexts) {
192
+ for (const subcontext of this.subcontexts) subcontext.dispose()
193
+ }
194
+ if (!this.ddwafContext.disposed) this.ddwafContext.dispose()
200
195
  }
201
196
  }
202
197
 
@@ -53,14 +53,19 @@ class WAFManager {
53
53
 
54
54
  getWAFContext (req) {
55
55
  let wafContext = contexts.get(req)
56
+ if (wafContext === null) return
56
57
 
57
58
  if (!wafContext) {
59
+ const context = this.ddwaf.createContext()
60
+ if (context === null) return
61
+
58
62
  wafContext = new WAFContextWrapper(
59
- this.ddwaf.createContext(),
63
+ context,
60
64
  this.wafTimeout,
61
65
  this.ddwafVersion,
62
66
  this.rulesVersion,
63
- this.ddwaf.knownAddresses
67
+ this.ddwaf.knownAddresses,
68
+ req
64
69
  )
65
70
  contexts.set(req, wafContext)
66
71
  }
@@ -68,6 +73,17 @@ class WAFManager {
68
73
  return wafContext
69
74
  }
70
75
 
76
+ /** @param {object} req Request whose evaluations have finished. */
77
+ disposeContext (req) {
78
+ const context = contexts.get(req)
79
+ if (context) {
80
+ context.dispose()
81
+ } else {
82
+ // No native context exists when all rules were removed; late callbacks must still stay disabled.
83
+ contexts.set(req, null)
84
+ }
85
+ }
86
+
71
87
  setRulesVersion () {
72
88
  if (this.ddwaf.diagnostics.ruleset_version) {
73
89
  this.rulesVersion = this.ddwaf.diagnostics.ruleset_version
@@ -8,6 +8,7 @@ const FIELD_WRITE = 2
8
8
  const FIELD_HAS = 4
9
9
  const FIELD_DELETE = 8
10
10
  const FIELD_NAME_LENGTH = 16
11
+ const FIELD_NAME = 32
11
12
 
12
13
  const b3FlagsHeader = 'x-b3-flags'
13
14
  const b3Header = 'b3'
@@ -36,6 +37,7 @@ const carrierOperations = {}
36
37
  * @property {unknown} [has]
37
38
  * @property {unknown} [delete]
38
39
  * @property {unknown} [nameLength]
40
+ * @property {unknown} [name]
39
41
  */
40
42
 
41
43
  /**
@@ -47,6 +49,7 @@ const carrierOperations = {}
47
49
  * @property {(carrier: Carrier) => void} delete
48
50
  * @property {(carrier: Carrier, target: Carrier) => void} copy
49
51
  * @property {number} nameLength
52
+ * @property {string} name
50
53
  */
51
54
 
52
55
  /**
@@ -63,6 +66,7 @@ function defineCapability (name, operations, capability) {
63
66
  if (operations & FIELD_HAS) carrierOperations[`has${suffix}`] = capability.has
64
67
  if (operations & FIELD_DELETE) carrierOperations[`delete${suffix}`] = capability.delete
65
68
  if (operations & FIELD_NAME_LENGTH) carrierOperations[`${name}NameLength`] = capability.nameLength
69
+ if (operations & FIELD_NAME) carrierOperations[`${name}Name`] = capability.name
66
70
  return capability
67
71
  }
68
72
 
@@ -109,6 +113,7 @@ function defineField (fieldName, headerName, read, operations) {
109
113
  },
110
114
 
111
115
  nameLength: headerName.length,
116
+ name: headerName,
112
117
  })
113
118
  }
114
119
 
@@ -277,7 +282,7 @@ const dsmBase64 = defineField(
277
282
  'dsmBase64',
278
283
  dsmBase64Header,
279
284
  readDsmBase64,
280
- FIELD_READ | FIELD_WRITE | FIELD_HAS | FIELD_NAME_LENGTH
285
+ FIELD_READ | FIELD_WRITE | FIELD_HAS | FIELD_NAME_LENGTH | FIELD_NAME
281
286
  )
282
287
  const dsmBinary = defineField('dsmBinary', dsmBinaryHeader, readDsmBinary, FIELD_READ | FIELD_HAS)
283
288
 
@@ -363,6 +368,7 @@ function pickDsm (carrier) {
363
368
  /**
364
369
  * @typedef {object} CarrierOperations
365
370
  * @property {typeof datadogParentId.delete} deleteDatadogParentId
371
+ * @property {typeof dsmBase64.name} dsmBase64Name
366
372
  * @property {typeof dsmBase64.nameLength} dsmBase64NameLength
367
373
  * @property {typeof dsmBase64.has} hasDsmBase64
368
374
  * @property {typeof dsmBinary.has} hasDsmBinary
@@ -203,7 +203,8 @@ class TestVisDynamicInstrumentation {
203
203
  // Allow the parent to exit even if the worker is still running
204
204
  this.worker.unref?.()
205
205
 
206
- this.breakpointSetChannel.port2.on('message', (probeId) => {
206
+ this.breakpointSetChannel.port2.on('message', ({ probeId, error }) => {
207
+ if (error) log.error('Test Optimization - Dynamic Instrumentation failed to set probe', error)
207
208
  const probeState = this.#probeStateById.get(probeId)
208
209
  if (probeState?.resolveSet) {
209
210
  probeState.resolveSet()
@@ -230,7 +231,8 @@ class TestVisDynamicInstrumentation {
230
231
  }
231
232
  }).unref?.()
232
233
 
233
- this.breakpointRemoveChannel.port2.on('message', (probeId) => {
234
+ this.breakpointRemoveChannel.port2.on('message', ({ probeId, error }) => {
235
+ if (error) log.error('Test Optimization - Dynamic Instrumentation failed to remove probe', error)
234
236
  const probeState = this.#probeStateById.get(probeId)
235
237
  if (probeState?.resolveRemove) {
236
238
  probeState.resolveRemove()
@@ -155,16 +155,32 @@ breakpointHitChannel.on('message', ({ drainRequestId }) => {
155
155
  scheduleBreakpointHitDrain()
156
156
  })
157
157
 
158
- breakpointRemoveChannel.on('message', async (probeId) => {
159
- await removeBreakpoint(probeId)
160
- breakpointRemoveChannel.postMessage(probeId)
158
+ breakpointRemoveChannel.on('message', (probeId) => {
159
+ acknowledge(breakpointRemoveChannel, probeId, removeBreakpoint(probeId))
161
160
  })
162
161
 
163
- breakpointSetChannel.on('message', async (probe) => {
164
- await addBreakpoint(probe)
165
- breakpointSetChannel.postMessage(probe.id)
162
+ breakpointSetChannel.on('message', (probe) => {
163
+ acknowledge(breakpointSetChannel, probe.id, addBreakpoint(probe))
166
164
  })
167
165
 
166
+ /**
167
+ * Acknowledges a probe operation once it settles, along with its error if it failed. Rejecting instead would end the
168
+ * worker, and only the main thread's logs reach telemetry.
169
+ *
170
+ * @param {import('node:worker_threads').MessagePort} channel - The channel that requested the operation
171
+ * @param {string} probeId - The ID of the probe the operation was for
172
+ * @param {Promise<void>} operation - The pending operation
173
+ */
174
+ async function acknowledge (channel, probeId, operation) {
175
+ let error
176
+ try {
177
+ await operation
178
+ } catch (err) {
179
+ error = err
180
+ }
181
+ channel.postMessage({ probeId, error })
182
+ }
183
+
168
184
  async function removeBreakpoint (probeId) {
169
185
  if (!sessionStarted) {
170
186
  // We should not get in this state, but abort if we do, so the code doesn't fail unexpected
@@ -212,20 +228,16 @@ async function addBreakpoint (probe) {
212
228
  }
213
229
  }
214
230
 
215
- try {
216
- const { breakpointId } = await session.post('Debugger.setBreakpoint', {
217
- location: {
218
- scriptId,
219
- lineNumber: lineNumber - 1,
220
- columnNumber,
221
- },
222
- })
231
+ const { breakpointId } = await session.post('Debugger.setBreakpoint', {
232
+ location: {
233
+ scriptId,
234
+ lineNumber: lineNumber - 1,
235
+ columnNumber,
236
+ },
237
+ })
223
238
 
224
- breakpointIdToProbe.set(breakpointId, probe)
225
- probeIdToBreakpointId.set(probe.id, breakpointId)
226
- } catch (e) {
227
- log.error('Error setting breakpoint at %s:%s', url, line, e)
228
- }
239
+ breakpointIdToProbe.set(breakpointId, probe)
240
+ probeIdToBreakpointId.set(probe.id, breakpointId)
229
241
  }
230
242
 
231
243
  async function start () {