dd-trace 5.0.0-pre-ca96946 → 5.0.0-pre-40740ae

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 (52) hide show
  1. package/package.json +2 -2
  2. package/packages/datadog-instrumentations/src/helpers/hooks.js +1 -0
  3. package/packages/datadog-instrumentations/src/http/client.js +2 -1
  4. package/packages/datadog-instrumentations/src/http/server.js +14 -0
  5. package/packages/datadog-instrumentations/src/http2/client.js +4 -0
  6. package/packages/datadog-instrumentations/src/playwright.js +1 -1
  7. package/packages/datadog-instrumentations/src/sequelize.js +51 -0
  8. package/packages/datadog-plugin-cypress/src/plugin.js +117 -27
  9. package/packages/datadog-plugin-cypress/src/support.js +5 -2
  10. package/packages/datadog-plugin-memcached/src/index.js +2 -3
  11. package/packages/datadog-plugin-pg/src/index.js +1 -1
  12. package/packages/datadog-plugin-redis/src/index.js +2 -13
  13. package/packages/dd-trace/src/appsec/blocked_templates.js +2 -101
  14. package/packages/dd-trace/src/appsec/blocking.js +60 -11
  15. package/packages/dd-trace/src/appsec/channels.js +3 -2
  16. package/packages/dd-trace/src/appsec/iast/analyzers/analyzers.js +7 -5
  17. package/packages/dd-trace/src/appsec/iast/analyzers/index.js +3 -0
  18. package/packages/dd-trace/src/appsec/iast/analyzers/insecure-cookie-analyzer.js +31 -0
  19. package/packages/dd-trace/src/appsec/iast/analyzers/set-cookies-header-interceptor.js +47 -0
  20. package/packages/dd-trace/src/appsec/iast/analyzers/sql-injection-analyzer.js +30 -5
  21. package/packages/dd-trace/src/appsec/iast/analyzers/ssrf-analyzer.js +26 -0
  22. package/packages/dd-trace/src/appsec/iast/analyzers/vulnerability-analyzer.js +35 -3
  23. package/packages/dd-trace/src/appsec/iast/path-line.js +14 -7
  24. package/packages/dd-trace/src/appsec/iast/taint-tracking/plugin.js +19 -4
  25. package/packages/dd-trace/src/appsec/iast/vulnerabilities-formatter/evidence-redaction/sensitive-analyzers/sql-sensitive-analyzer.js +25 -2
  26. package/packages/dd-trace/src/appsec/iast/vulnerabilities-formatter/evidence-redaction/sensitive-analyzers/url-sensitive-analyzer.js +49 -0
  27. package/packages/dd-trace/src/appsec/iast/vulnerabilities-formatter/evidence-redaction/sensitive-handler.js +2 -0
  28. package/packages/dd-trace/src/appsec/iast/vulnerabilities.js +7 -5
  29. package/packages/dd-trace/src/appsec/iast/vulnerability-reporter.js +0 -33
  30. package/packages/dd-trace/src/appsec/recommended.json +45 -46
  31. package/packages/dd-trace/src/appsec/remote_config/capabilities.js +3 -1
  32. package/packages/dd-trace/src/appsec/remote_config/index.js +4 -0
  33. package/packages/dd-trace/src/appsec/rule_manager.js +49 -6
  34. package/packages/dd-trace/src/ci-visibility/intelligent-test-runner/get-skippable-suites.js +10 -4
  35. package/packages/dd-trace/src/config.js +9 -1
  36. package/packages/dd-trace/src/plugins/cache.js +7 -0
  37. package/packages/dd-trace/src/plugins/ci_plugin.js +2 -0
  38. package/packages/dd-trace/src/plugins/database.js +2 -2
  39. package/packages/dd-trace/src/plugins/storage.js +2 -0
  40. package/packages/dd-trace/src/plugins/tracing.js +4 -4
  41. package/packages/dd-trace/src/plugins/util/ci.js +1 -1
  42. package/packages/dd-trace/src/service-naming/index.js +4 -12
  43. package/packages/dd-trace/src/service-naming/schemas/definition.js +9 -9
  44. package/packages/dd-trace/src/service-naming/schemas/util.js +5 -0
  45. package/packages/dd-trace/src/service-naming/schemas/v0/index.js +5 -0
  46. package/packages/dd-trace/src/service-naming/schemas/v0/messaging.js +64 -0
  47. package/packages/dd-trace/src/service-naming/schemas/v0/storage.js +33 -0
  48. package/packages/dd-trace/src/service-naming/schemas/v1/index.js +5 -0
  49. package/packages/dd-trace/src/service-naming/schemas/v1/messaging.js +52 -0
  50. package/packages/dd-trace/src/service-naming/schemas/v1/storage.js +21 -0
  51. package/packages/dd-trace/src/service-naming/schemas/v0.js +0 -66
  52. package/packages/dd-trace/src/service-naming/schemas/v1.js +0 -58
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dd-trace",
3
- "version": "5.0.0-pre-ca96946",
3
+ "version": "5.0.0-pre-40740ae",
4
4
  "description": "Datadog APM tracing client for JavaScript",
5
5
  "main": "index.js",
6
6
  "typings": "index.d.ts",
@@ -66,7 +66,7 @@
66
66
  "node": ">=16"
67
67
  },
68
68
  "dependencies": {
69
- "@datadog/native-appsec": "^3.1.0",
69
+ "@datadog/native-appsec": "^3.2.0",
70
70
  "@datadog/native-iast-rewriter": "2.0.1",
71
71
  "@datadog/native-iast-taint-tracking": "^1.4.1",
72
72
  "@datadog/native-metrics": "^2.0.0",
@@ -80,6 +80,7 @@ module.exports = {
80
80
  'rhea': () => require('../rhea'),
81
81
  'router': () => require('../router'),
82
82
  'sharedb': () => require('../sharedb'),
83
+ 'sequelize': () => require('../sequelize'),
83
84
  'tedious': () => require('../tedious'),
84
85
  'when': () => require('../when'),
85
86
  'winston': () => require('../winston')
@@ -101,6 +101,7 @@ function patch (http, methodName) {
101
101
  }
102
102
 
103
103
  function normalizeArgs (inputURL, inputOptions, cb) {
104
+ const originalUrl = inputURL
104
105
  inputURL = normalizeOptions(inputURL)
105
106
 
106
107
  const [callback, inputOptionsNormalized] = normalizeCallback(inputOptions, cb, inputURL)
@@ -108,7 +109,7 @@ function patch (http, methodName) {
108
109
  normalizeHeaders(options)
109
110
  const uri = url.format(options)
110
111
 
111
- return { uri, options, callback }
112
+ return { uri, options, callback, originalUrl }
112
113
  }
113
114
 
114
115
  function combineOptions (inputURL, inputOptions) {
@@ -11,6 +11,7 @@ const startServerCh = channel('apm:http:server:request:start')
11
11
  const exitServerCh = channel('apm:http:server:request:exit')
12
12
  const errorServerCh = channel('apm:http:server:request:error')
13
13
  const finishServerCh = channel('apm:http:server:request:finish')
14
+ const finishSetHeaderCh = channel('datadog:http:server:response:set-header:finish')
14
15
 
15
16
  const requestFinishedSet = new WeakSet()
16
17
 
@@ -58,6 +59,9 @@ function wrapEmit (emit) {
58
59
  // TODO: should this always return true ?
59
60
  return this.listenerCount(eventName) > 0
60
61
  }
62
+ if (finishSetHeaderCh.hasSubscribers) {
63
+ wrapSetHeader(res)
64
+ }
61
65
  return emit.apply(this, arguments)
62
66
  } catch (err) {
63
67
  errorServerCh.publish(err)
@@ -70,3 +74,13 @@ function wrapEmit (emit) {
70
74
  return emit.apply(this, arguments)
71
75
  }
72
76
  }
77
+
78
+ function wrapSetHeader (res) {
79
+ shimmer.wrap(res, 'setHeader', setHeader => {
80
+ return function (name, value) {
81
+ const setHeaderResult = setHeader.apply(this, arguments)
82
+ finishSetHeaderCh.publish({ name, value, res })
83
+ return setHeaderResult
84
+ }
85
+ })
86
+ }
@@ -3,6 +3,7 @@
3
3
  const shimmer = require('../../../datadog-shimmer')
4
4
  const { addHook, channel, AsyncResource } = require('../helpers/instrument')
5
5
 
6
+ const connectChannel = channel('apm:http2:client:connect:start')
6
7
  const startChannel = channel('apm:http2:client:request:start')
7
8
  const finishChannel = channel('apm:http2:client:request:finish')
8
9
  const errorChannel = channel('apm:http2:client:request:error')
@@ -52,6 +53,9 @@ function createWrapRequest (authority, options) {
52
53
 
53
54
  function wrapConnect (connect) {
54
55
  return function (authority, options) {
56
+ if (connectChannel.hasSubscribers) {
57
+ connectChannel.publish({ authority })
58
+ }
55
59
  const session = connect.apply(this, arguments)
56
60
 
57
61
  shimmer.wrap(session, 'request', createWrapRequest(authority, options))
@@ -248,7 +248,7 @@ function runnerHook (runnerExport, playwrightVersion) {
248
248
  addHook({
249
249
  name: '@playwright/test',
250
250
  file: 'lib/runner.js',
251
- versions: ['>=1.18.0 <1.30.0']
251
+ versions: ['>=1.18.0 <=1.30.0']
252
252
  }, runnerHook)
253
253
 
254
254
  addHook({
@@ -0,0 +1,51 @@
1
+ 'use strict'
2
+
3
+ const {
4
+ channel,
5
+ addHook,
6
+ AsyncResource
7
+ } = require('./helpers/instrument')
8
+
9
+ const shimmer = require('../../datadog-shimmer')
10
+
11
+ addHook({ name: 'sequelize', versions: ['>=4'] }, Sequelize => {
12
+ const startCh = channel('datadog:sequelize:query:start')
13
+ const finishCh = channel('datadog:sequelize:query:finish')
14
+
15
+ shimmer.wrap(Sequelize.prototype, 'query', query => {
16
+ return function (sql) {
17
+ if (!startCh.hasSubscribers) {
18
+ return query.apply(this, arguments)
19
+ }
20
+
21
+ const asyncResource = new AsyncResource('bound-anonymous-fn')
22
+
23
+ let dialect
24
+ if (this.options && this.options.dialect) {
25
+ dialect = this.options.dialect
26
+ } else if (this.dialect && this.dialect.name) {
27
+ dialect = this.dialect.name
28
+ }
29
+
30
+ function onFinish () {
31
+ asyncResource.bind(function () {
32
+ finishCh.publish()
33
+ }, this).apply(this)
34
+ }
35
+
36
+ return asyncResource.bind(function () {
37
+ startCh.publish({
38
+ sql,
39
+ dialect
40
+ })
41
+
42
+ const promise = query.apply(this, arguments)
43
+ promise.then(onFinish, onFinish)
44
+
45
+ return promise
46
+ }, this).apply(this, arguments)
47
+ }
48
+ })
49
+
50
+ return Sequelize
51
+ })
@@ -19,13 +19,14 @@ const {
19
19
  TEST_SOURCE_START,
20
20
  finishAllTraceSpans,
21
21
  getCoveredFilenamesFromCoverage,
22
- getTestSuitePath
22
+ getTestSuitePath,
23
+ addIntelligentTestRunnerSpanTags
23
24
  } = require('../../dd-trace/src/plugins/util/test')
25
+ const { ORIGIN_KEY, COMPONENT } = require('../../dd-trace/src/constants')
26
+ const log = require('../../dd-trace/src/log')
24
27
 
25
28
  const TEST_FRAMEWORK_NAME = 'cypress'
26
29
 
27
- const { ORIGIN_KEY, COMPONENT } = require('../../dd-trace/src/constants')
28
-
29
30
  const CYPRESS_STATUS_TO_TEST_STATUS = {
30
31
  passed: 'pass',
31
32
  failed: 'fail',
@@ -56,7 +57,7 @@ function getCypressVersion (details) {
56
57
 
57
58
  function getRootDir (details) {
58
59
  if (details && details.config) {
59
- return details.config.repoRoot || details.config.projectRoot || process.cwd()
60
+ return details.config.projectRoot || details.config.repoRoot || process.cwd()
60
61
  }
61
62
  return process.cwd()
62
63
  }
@@ -88,10 +89,62 @@ function getSuiteStatus (suiteStats) {
88
89
  return 'pass'
89
90
  }
90
91
 
92
+ function getItrConfig (tracer, testConfiguration) {
93
+ return new Promise(resolve => {
94
+ if (!tracer._tracer._exporter || !tracer._tracer._exporter.getItrConfiguration) {
95
+ return resolve({ err: new Error('CI Visibility was not initialized correctly') })
96
+ }
97
+
98
+ tracer._tracer._exporter.getItrConfiguration(testConfiguration, (err, itrConfig) => {
99
+ resolve({ err, itrConfig })
100
+ })
101
+ })
102
+ }
103
+
104
+ function getSkippableTests (isSuitesSkippingEnabled, tracer, testConfiguration) {
105
+ if (!isSuitesSkippingEnabled) {
106
+ return Promise.resolve({ skippableTests: [] })
107
+ }
108
+ return new Promise(resolve => {
109
+ if (!tracer._tracer._exporter || !tracer._tracer._exporter.getItrConfiguration) {
110
+ return resolve({ err: new Error('CI Visibility was not initialized correctly') })
111
+ }
112
+ tracer._tracer._exporter.getSkippableSuites(testConfiguration, (err, skippableTests) => {
113
+ resolve({
114
+ err,
115
+ skippableTests
116
+ })
117
+ })
118
+ })
119
+ }
120
+
91
121
  module.exports = (on, config) => {
92
122
  const tracer = require('../../dd-trace')
93
123
  const testEnvironmentMetadata = getTestEnvironmentMetadata(TEST_FRAMEWORK_NAME)
94
124
 
125
+ const {
126
+ 'git.repository_url': repositoryUrl,
127
+ 'git.commit.sha': sha,
128
+ 'os.version': osVersion,
129
+ 'os.platform': osPlatform,
130
+ 'os.architecture': osArchitecture,
131
+ 'runtime.name': runtimeName,
132
+ 'runtime.version': runtimeVersion,
133
+ 'git.branch': branch
134
+ } = testEnvironmentMetadata
135
+
136
+ const testConfiguration = {
137
+ repositoryUrl,
138
+ sha,
139
+ osVersion,
140
+ osPlatform,
141
+ osArchitecture,
142
+ runtimeName,
143
+ runtimeVersion,
144
+ branch,
145
+ testLevel: 'test'
146
+ }
147
+
95
148
  const codeOwnersEntries = getCodeOwnersFileEntries()
96
149
 
97
150
  let activeSpan = null
@@ -101,32 +154,53 @@ module.exports = (on, config) => {
101
154
  let command = null
102
155
  let frameworkVersion
103
156
  let rootDir
157
+ let isSuitesSkippingEnabled = false
158
+ let isCodeCoverageEnabled = false
159
+ let testsToSkip = []
104
160
 
105
161
  on('before:run', (details) => {
106
- const childOf = getTestParentSpan(tracer)
162
+ return getItrConfig(tracer, testConfiguration).then(({ err, itrConfig }) => {
163
+ if (err) {
164
+ log.error(err)
165
+ } else {
166
+ isSuitesSkippingEnabled = itrConfig.isSuitesSkippingEnabled
167
+ isCodeCoverageEnabled = itrConfig.isCodeCoverageEnabled
168
+ }
107
169
 
108
- rootDir = getRootDir(details)
109
- command = getCypressCommand(details)
110
- frameworkVersion = getCypressVersion(details)
170
+ getSkippableTests(isSuitesSkippingEnabled, tracer, testConfiguration).then(({ err, skippableTests }) => {
171
+ if (err) {
172
+ log.error(err)
173
+ } else {
174
+ testsToSkip = skippableTests || []
175
+ }
111
176
 
112
- const testSessionSpanMetadata = getTestSessionCommonTags(command, frameworkVersion, TEST_FRAMEWORK_NAME)
113
- const testModuleSpanMetadata = getTestModuleCommonTags(command, frameworkVersion, TEST_FRAMEWORK_NAME)
177
+ const childOf = getTestParentSpan(tracer)
178
+ rootDir = getRootDir(details)
114
179
 
115
- testSessionSpan = tracer.startSpan(`${TEST_FRAMEWORK_NAME}.test_session`, {
116
- childOf,
117
- tags: {
118
- [COMPONENT]: TEST_FRAMEWORK_NAME,
119
- ...testEnvironmentMetadata,
120
- ...testSessionSpanMetadata
121
- }
122
- })
123
- testModuleSpan = tracer.startSpan(`${TEST_FRAMEWORK_NAME}.test_module`, {
124
- childOf: testSessionSpan,
125
- tags: {
126
- [COMPONENT]: TEST_FRAMEWORK_NAME,
127
- ...testEnvironmentMetadata,
128
- ...testModuleSpanMetadata
129
- }
180
+ command = getCypressCommand(details)
181
+ frameworkVersion = getCypressVersion(details)
182
+
183
+ const testSessionSpanMetadata = getTestSessionCommonTags(command, frameworkVersion, TEST_FRAMEWORK_NAME)
184
+ const testModuleSpanMetadata = getTestModuleCommonTags(command, frameworkVersion, TEST_FRAMEWORK_NAME)
185
+
186
+ testSessionSpan = tracer.startSpan(`${TEST_FRAMEWORK_NAME}.test_session`, {
187
+ childOf,
188
+ tags: {
189
+ [COMPONENT]: TEST_FRAMEWORK_NAME,
190
+ ...testEnvironmentMetadata,
191
+ ...testSessionSpanMetadata
192
+ }
193
+ })
194
+ testModuleSpan = tracer.startSpan(`${TEST_FRAMEWORK_NAME}.test_module`, {
195
+ childOf: testSessionSpan,
196
+ tags: {
197
+ [COMPONENT]: TEST_FRAMEWORK_NAME,
198
+ ...testEnvironmentMetadata,
199
+ ...testModuleSpanMetadata
200
+ }
201
+ })
202
+ return details
203
+ })
130
204
  })
131
205
  })
132
206
 
@@ -136,6 +210,16 @@ module.exports = (on, config) => {
136
210
  testModuleSpan.setTag(TEST_STATUS, testStatus)
137
211
  testSessionSpan.setTag(TEST_STATUS, testStatus)
138
212
 
213
+ addIntelligentTestRunnerSpanTags(
214
+ testSessionSpan,
215
+ testModuleSpan,
216
+ {
217
+ isSuitesSkipped: !!testsToSkip.length,
218
+ isSuitesSkippingEnabled,
219
+ isCodeCoverageEnabled
220
+ }
221
+ )
222
+
139
223
  testModuleSpan.finish()
140
224
  testSessionSpan.finish()
141
225
 
@@ -181,6 +265,12 @@ module.exports = (on, config) => {
181
265
  },
182
266
  'dd:beforeEach': (test) => {
183
267
  const { testName, testSuite } = test
268
+ // skip test
269
+ if (testsToSkip.find(test => {
270
+ return testName === test.name && testSuite === test.suite
271
+ })) {
272
+ return { shouldSkip: true }
273
+ }
184
274
 
185
275
  const testSuiteTags = {
186
276
  [TEST_COMMAND]: command,
@@ -219,12 +309,12 @@ module.exports = (on, config) => {
219
309
  }
220
310
  })
221
311
  }
222
- return activeSpan ? activeSpan.context().toTraceId() : null
312
+ return activeSpan ? { traceId: activeSpan.context().toTraceId() } : {}
223
313
  },
224
314
  'dd:afterEach': ({ test, coverage }) => {
225
315
  const { state, error, isRUMActive, testSourceLine } = test
226
316
  if (activeSpan) {
227
- if (coverage && tracer._tracer._exporter.exportCoverage) {
317
+ if (coverage && tracer._tracer._exporter.exportCoverage && isCodeCoverageEnabled) {
228
318
  const coverageFiles = getCoveredFilenamesFromCoverage(coverage)
229
319
  const relativeCoverageFiles = coverageFiles.map(file => getTestSuitePath(file, rootDir))
230
320
  const { _traceId, _spanId } = testSuiteSpan.context()
@@ -1,10 +1,13 @@
1
1
  /* eslint-disable */
2
- beforeEach(() => {
2
+ beforeEach(function () {
3
3
  cy.task('dd:beforeEach', {
4
4
  testName: Cypress.mocha.getRunner().suite.ctx.currentTest.fullTitle(),
5
5
  testSuite: Cypress.mocha.getRootSuite().file
6
- }).then(traceId => {
6
+ }).then(({ traceId, shouldSkip }) => {
7
7
  Cypress.env('traceId', traceId)
8
+ if (shouldSkip) {
9
+ this.skip()
10
+ }
8
11
  })
9
12
  })
10
13
 
@@ -9,11 +9,10 @@ class MemcachedPlugin extends CachePlugin {
9
9
  start ({ client, server, query }) {
10
10
  const address = getAddress(client, server, query)
11
11
 
12
- this.startSpan('memcached.command', {
13
- service: this.config.service,
12
+ this.startSpan({
13
+ service: this.serviceName(this.config, this.system),
14
14
  resource: query.type,
15
15
  type: 'memcached',
16
- kind: 'client',
17
16
  meta: {
18
17
  'memcached.command': query.command,
19
18
  'out.host': address[0],
@@ -27,7 +27,7 @@ class PGPlugin extends DatabasePlugin {
27
27
  }
28
28
  })
29
29
 
30
- query.text = this.injectDbmQuery(query.text, service)
30
+ query.text = this.injectDbmQuery(query.text, service, !!query.name)
31
31
  }
32
32
  }
33
33
 
@@ -13,11 +13,10 @@ class RedisPlugin extends CachePlugin {
13
13
  const normalizedCommand = command.toUpperCase()
14
14
  if (!this.config.filter(normalizedCommand)) return this.skip()
15
15
 
16
- this.startSpan('redis.command', {
17
- service: getService(this.config, connectionName),
16
+ this.startSpan({
18
17
  resource,
18
+ service: this.serviceName(this.config, this.system, connectionName),
19
19
  type: 'redis',
20
- kind: 'client',
21
20
  meta: {
22
21
  'db.type': 'redis',
23
22
  'db.name': db || '0',
@@ -33,16 +32,6 @@ class RedisPlugin extends CachePlugin {
33
32
  }
34
33
  }
35
34
 
36
- function getService (config, connectionName) {
37
- if (config.splitByInstance && connectionName) {
38
- return config.service
39
- ? `${config.service}-${connectionName}`
40
- : connectionName
41
- }
42
-
43
- return config.service
44
- }
45
-
46
35
  function formatCommand (command, args) {
47
36
  if (!args || command === 'AUTH') return command
48
37
 
@@ -1,108 +1,9 @@
1
1
  /* eslint-disable max-len */
2
2
  'use strict'
3
3
 
4
- const html = `<!-- Sorry, you’ve been blocked -->
5
- <!DOCTYPE html>
6
- <html lang="en">
4
+ const html = `<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>You've been blocked</title><style>a,body,div,html,span{margin:0;padding:0;border:0;font-size:100%;font:inherit;vertical-align:baseline}body{background:-webkit-radial-gradient(26% 19%,circle,#fff,#f4f7f9);background:radial-gradient(circle at 26% 19%,#fff,#f4f7f9);display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-ms-flex-line-pack:center;align-content:center;width:100%;min-height:100vh;line-height:1;flex-direction:column}p{display:block}main{text-align:center;flex:1;display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;-webkit-box-align:center;-ms-flex-align:center;align-items:center;-ms-flex-line-pack:center;align-content:center;flex-direction:column}p{font-size:18px;line-height:normal;color:#646464;font-family:sans-serif;font-weight:400}a{color:#4842b7}footer{width:100%;text-align:center}footer p{font-size:16px}</style></head><body><main><p>Sorry, you cannot access this page. Please contact the customer service team.</p></main><footer><p>Security provided by <a href="https://www.datadoghq.com/product/security-platform/application-security-monitoring/" target="_blank">Datadog</a></p></footer></body></html>`
7
5
 
8
- <head>
9
- <meta charset="UTF-8">
10
- <meta name="viewport" content="width=device-width,initial-scale=1">
11
- <title>You've been blocked</title>
12
- <style>
13
- a,
14
- body,
15
- div,
16
- html,
17
- span {
18
- margin: 0;
19
- padding: 0;
20
- border: 0;
21
- font-size: 100%;
22
- font: inherit;
23
- vertical-align: baseline
24
- }
25
-
26
- body {
27
- background: -webkit-radial-gradient(26% 19%, circle, #fff, #f4f7f9);
28
- background: radial-gradient(circle at 26% 19%, #fff, #f4f7f9);
29
- display: -webkit-box;
30
- display: -ms-flexbox;
31
- display: flex;
32
- -webkit-box-pack: center;
33
- -ms-flex-pack: center;
34
- justify-content: center;
35
- -webkit-box-align: center;
36
- -ms-flex-align: center;
37
- align-items: center;
38
- -ms-flex-line-pack: center;
39
- align-content: center;
40
- width: 100%;
41
- min-height: 100vh;
42
- line-height: 1;
43
- flex-direction: column
44
- }
45
-
46
- p {
47
- display: block
48
- }
49
-
50
-
51
- main {
52
- text-align: center;
53
- flex: 1;
54
- display: -webkit-box;
55
- display: -ms-flexbox;
56
- display: flex;
57
- -webkit-box-pack: center;
58
- -ms-flex-pack: center;
59
- justify-content: center;
60
- -webkit-box-align: center;
61
- -ms-flex-align: center;
62
- align-items: center;
63
- -ms-flex-line-pack: center;
64
- align-content: center;
65
- flex-direction: column
66
- }
67
-
68
- p {
69
- font-size: 18px;
70
- line-height: normal;
71
- color: #646464;
72
- font-family: sans-serif;
73
- font-weight: 400
74
- }
75
-
76
- a {
77
- color: #4842b7
78
- }
79
-
80
- footer {
81
- width: 100%;
82
- text-align: center
83
- }
84
-
85
- footer p {
86
- font-size: 16px
87
- }
88
- </style>
89
- </head>
90
-
91
- <body>
92
- <main>
93
- <p>Sorry, you cannot access this page. Please contact the customer service team.</p>
94
- </main>
95
- <footer>
96
- <p>Security provided by <a
97
- href="https://www.datadoghq.com/product/security-platform/application-security-monitoring/"
98
- target="_blank">Datadog</a></p>
99
- </footer>
100
- </body>
101
-
102
- </html>
103
- `
104
-
105
- const json = `{"errors": [{"title": "You've been blocked", "detail": "Sorry, you cannot access this page. Please contact the customer service team. Security provided by Datadog."}]}`
6
+ const json = `{"errors":[{"title":"You've been blocked","detail":"Sorry, you cannot access this page. Please contact the customer service team. Security provided by Datadog."}]}`
106
7
 
107
8
  module.exports = {
108
9
  html,
@@ -5,32 +5,62 @@ const blockedTemplates = require('./blocked_templates')
5
5
 
6
6
  let templateHtml = blockedTemplates.html
7
7
  let templateJson = blockedTemplates.json
8
+ let blockingConfiguration
8
9
 
9
- function block (req, res, rootSpan, abortController) {
10
- if (res.headersSent) {
11
- log.warn('Cannot send blocking response when headers have already been sent')
12
- return
10
+ function blockWithRedirect (res, rootSpan, abortController) {
11
+ rootSpan.addTags({
12
+ 'appsec.blocked': 'true'
13
+ })
14
+
15
+ let statusCode = blockingConfiguration.parameters.status_code
16
+ if (!statusCode || statusCode < 300 || statusCode >= 400) {
17
+ statusCode = 303
13
18
  }
14
19
 
20
+ res.writeHead(statusCode, {
21
+ 'Location': blockingConfiguration.parameters.location
22
+ }).end()
23
+
24
+ if (abortController) {
25
+ abortController.abort()
26
+ }
27
+ }
28
+
29
+ function blockWithContent (req, res, rootSpan, abortController) {
15
30
  let type
16
31
  let body
17
32
 
18
33
  // parse the Accept header, ex: Accept: text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8
19
34
  const accept = req.headers.accept && req.headers.accept.split(',').map((str) => str.split(';', 1)[0].trim())
20
35
 
21
- if (accept && accept.includes('text/html') && !accept.includes('application/json')) {
22
- type = 'text/html; charset=utf-8'
23
- body = templateHtml
36
+ if (!blockingConfiguration || blockingConfiguration.parameters.type === 'auto') {
37
+ if (accept && accept.includes('text/html') && !accept.includes('application/json')) {
38
+ type = 'text/html; charset=utf-8'
39
+ body = templateHtml
40
+ } else {
41
+ type = 'application/json'
42
+ body = templateJson
43
+ }
24
44
  } else {
25
- type = 'application/json'
26
- body = templateJson
45
+ if (blockingConfiguration.parameters.type === 'html') {
46
+ type = 'text/html; charset=utf-8'
47
+ body = templateHtml
48
+ } else {
49
+ type = 'application/json'
50
+ body = templateJson
51
+ }
27
52
  }
28
53
 
29
54
  rootSpan.addTags({
30
55
  'appsec.blocked': 'true'
31
56
  })
32
57
 
33
- res.statusCode = 403
58
+ if (blockingConfiguration && blockingConfiguration.type === 'block_request' &&
59
+ blockingConfiguration.parameters.status_code) {
60
+ res.statusCode = blockingConfiguration.parameters.status_code
61
+ } else {
62
+ res.statusCode = 403
63
+ }
34
64
  res.setHeader('Content-Type', type)
35
65
  res.setHeader('Content-Length', Buffer.byteLength(body))
36
66
  res.end(body)
@@ -40,6 +70,20 @@ function block (req, res, rootSpan, abortController) {
40
70
  }
41
71
  }
42
72
 
73
+ function block (req, res, rootSpan, abortController) {
74
+ if (res.headersSent) {
75
+ log.warn('Cannot send blocking response when headers have already been sent')
76
+ return
77
+ }
78
+
79
+ if (blockingConfiguration && blockingConfiguration.type === 'redirect_request' &&
80
+ blockingConfiguration.parameters.location) {
81
+ blockWithRedirect(res, rootSpan, abortController)
82
+ } else {
83
+ blockWithContent(req, res, rootSpan, abortController)
84
+ }
85
+ }
86
+
43
87
  function setTemplates (config) {
44
88
  if (config.appsec.blockedTemplateHtml) {
45
89
  templateHtml = config.appsec.blockedTemplateHtml
@@ -49,7 +93,12 @@ function setTemplates (config) {
49
93
  }
50
94
  }
51
95
 
96
+ function updateBlockingConfiguration (newBlockingConfiguration) {
97
+ blockingConfiguration = newBlockingConfiguration
98
+ }
99
+
52
100
  module.exports = {
53
101
  block,
54
- setTemplates
102
+ setTemplates,
103
+ updateBlockingConfiguration
55
104
  }
@@ -4,8 +4,9 @@ const dc = require('../../../diagnostics_channel')
4
4
 
5
5
  // TODO: use TBD naming convention
6
6
  module.exports = {
7
+ bodyParser: dc.channel('datadog:body-parser:read:finish'),
7
8
  incomingHttpRequestStart: dc.channel('dd-trace:incomingHttpRequestStart'),
8
9
  incomingHttpRequestEnd: dc.channel('dd-trace:incomingHttpRequestEnd'),
9
- bodyParser: dc.channel('datadog:body-parser:read:finish'),
10
- queryParser: dc.channel('datadog:query:read:finish')
10
+ queryParser: dc.channel('datadog:query:read:finish'),
11
+ setCookieChannel: dc.channel('datadog:iast:set-cookie')
11
12
  }