-
Notifications
You must be signed in to change notification settings - Fork 309
/
web.js
579 lines (450 loc) · 14 KB
/
web.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
'use strict'
const uniq = require('../../../../datadog-core/src/utils/src/uniq')
const analyticsSampler = require('../../analytics_sampler')
const FORMAT_HTTP_HEADERS = 'http_headers'
const log = require('../../log')
const tags = require('../../../../../ext/tags')
const types = require('../../../../../ext/types')
const kinds = require('../../../../../ext/kinds')
const urlFilter = require('./urlfilter')
const { extractIp } = require('./ip_extractor')
const { ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../../constants')
const WEB = types.WEB
const SERVER = kinds.SERVER
const RESOURCE_NAME = tags.RESOURCE_NAME
const SERVICE_NAME = tags.SERVICE_NAME
const SPAN_TYPE = tags.SPAN_TYPE
const SPAN_KIND = tags.SPAN_KIND
const ERROR = tags.ERROR
const HTTP_METHOD = tags.HTTP_METHOD
const HTTP_URL = tags.HTTP_URL
const HTTP_STATUS_CODE = tags.HTTP_STATUS_CODE
const HTTP_ROUTE = tags.HTTP_ROUTE
const HTTP_REQUEST_HEADERS = tags.HTTP_REQUEST_HEADERS
const HTTP_RESPONSE_HEADERS = tags.HTTP_RESPONSE_HEADERS
const HTTP_USERAGENT = tags.HTTP_USERAGENT
const HTTP_CLIENT_IP = tags.HTTP_CLIENT_IP
const MANUAL_DROP = tags.MANUAL_DROP
const HTTP2_HEADER_AUTHORITY = ':authority'
const HTTP2_HEADER_SCHEME = ':scheme'
const HTTP2_HEADER_PATH = ':path'
const contexts = new WeakMap()
const ends = new WeakMap()
const web = {
// Ensure the configuration has the correct structure and defaults.
normalizeConfig (config) {
const headers = getHeadersToRecord(config)
const validateStatus = getStatusValidator(config)
const hooks = getHooks(config)
const filter = urlFilter.getFilter(config)
const middleware = getMiddlewareSetting(config)
const queryStringObfuscation = getQsObfuscator(config)
return {
...config,
headers,
validateStatus,
hooks,
filter,
middleware,
queryStringObfuscation
}
},
setFramework (req, name, config) {
const context = this.patch(req)
const span = context.span
if (!span) return
span.context()._name = `${name}.request`
span.context()._tags.component = name
web.setConfig(req, config)
},
setConfig (req, config) {
const context = contexts.get(req)
const span = context.span
context.config = config
if (!config.filter(req.url)) {
span.setTag(MANUAL_DROP, true)
span.context()._trace.isRecording = false
}
if (config.service) {
span.setTag(SERVICE_NAME, config.service)
}
analyticsSampler.sample(span, config.measured, true)
},
startSpan (tracer, config, req, res, name) {
const context = this.patch(req)
let span
if (context.span) {
context.span.context()._name = name
span = context.span
} else {
span = web.startChildSpan(tracer, name, req.headers)
}
context.tracer = tracer
context.span = span
context.res = res
this.setConfig(req, config)
addRequestTags(context)
return span
},
wrap (req) {
const context = contexts.get(req)
if (!context.instrumented) {
this.wrapEnd(context)
context.instrumented = true
}
},
// Start a span and activate a scope for a request.
instrument (tracer, config, req, res, name, callback) {
const span = this.startSpan(tracer, config, req, res, name)
this.wrap(req)
return callback && tracer.scope().activate(span, () => callback(span))
},
// Reactivate the request scope in case it was changed by a middleware.
reactivate (req, fn) {
return reactivate(req, fn)
},
// Add a route segment that will be used for the resource name.
enterRoute (req, path) {
if (typeof path === 'string') {
contexts.get(req).paths.push(path)
}
},
setRoute (req, path) {
const context = contexts.get(req)
if (!context) return
context.paths = [path]
},
// Remove the current route segment.
exitRoute (req) {
contexts.get(req).paths.pop()
},
// Start a new middleware span and activate a new scope with the span.
wrapMiddleware (req, middleware, name, fn) {
if (!this.active(req)) return fn()
const context = contexts.get(req)
const tracer = context.tracer
const childOf = this.active(req)
const config = context.config
if (config.middleware === false) return this.bindAndWrapMiddlewareErrors(fn, req, tracer, childOf)
const span = tracer.startSpan(name, { childOf })
analyticsSampler.sample(span, config.measured)
span.addTags({
[RESOURCE_NAME]: middleware._name || middleware.name || '<anonymous>'
})
context.middleware.push(span)
return tracer.scope().activate(span, fn)
},
// catch errors and apply to active span
bindAndWrapMiddlewareErrors (fn, req, tracer, activeSpan) {
try {
return tracer.scope().bind(fn, activeSpan).apply(this, arguments)
} catch (e) {
web.addError(req, e) // TODO: remove when error formatting is moved to Span
throw e
}
},
// Finish the active middleware span.
finish (req, error) {
if (!this.active(req)) return
const context = contexts.get(req)
const span = context.middleware.pop()
if (span) {
if (error) {
span.addTags({
[ERROR_TYPE]: error.name,
[ERROR_MESSAGE]: error.message,
[ERROR_STACK]: error.stack
})
}
span.finish()
}
},
// Register a callback to run before res.end() is called.
beforeEnd (req, callback) {
contexts.get(req).beforeEnd.push(callback)
},
// Prepare the request for instrumentation.
patch (req) {
let context = contexts.get(req)
if (context) return context
context = req.stream && contexts.get(req.stream)
if (context) {
contexts.set(req, context)
return context
}
context = {
req,
span: null,
paths: [],
middleware: [],
beforeEnd: [],
config: {}
}
contexts.set(req, context)
return context
},
// Return the request root span.
root (req) {
const context = contexts.get(req)
return context ? context.span : null
},
// Return the active span.
active (req) {
const context = contexts.get(req)
if (!context) return null
if (context.middleware.length === 0) return context.span || null
return context.middleware.slice(-1)[0]
},
// Extract the parent span from the headers and start a new span as its child
startChildSpan (tracer, name, headers) {
const childOf = tracer.extract(FORMAT_HTTP_HEADERS, headers)
const span = tracer.startSpan(name, { childOf })
return span
},
// Validate a request's status code and then add error tags if necessary
addStatusError (req, statusCode) {
const context = contexts.get(req)
const span = context.span
const error = context.error
const hasExistingError = span.context()._tags.error || span.context()._tags[ERROR_MESSAGE]
if (!hasExistingError && !context.config.validateStatus(statusCode)) {
span.setTag(ERROR, error || true)
}
},
// Add an error to the request
addError (req, error) {
if (error instanceof Error) {
const context = contexts.get(req)
if (context) {
context.error = error
}
}
},
finishMiddleware (context) {
if (context.finished) return
let span
while ((span = context.middleware.pop())) {
span.finish()
}
},
finishSpan (context) {
const { req, res } = context
if (context.finished && !req.stream) return
addRequestTags(context)
addResponseTags(context)
context.config.hooks.request(context.span, req, res)
addResourceTag(context)
context.span.finish()
context.finished = true
},
finishAll (context) {
for (const beforeEnd of context.beforeEnd) {
beforeEnd()
}
web.finishMiddleware(context)
web.finishSpan(context)
},
obfuscateQs (config, url) {
const { queryStringObfuscation } = config
if (queryStringObfuscation === false) return url
const i = url.indexOf('?')
if (i === -1) return url
const path = url.slice(0, i)
if (queryStringObfuscation === true) return path
let qs = url.slice(i + 1)
qs = qs.replace(queryStringObfuscation, '<redacted>')
return `${path}?${qs}`
},
wrapWriteHead (context) {
const { req, res } = context
const writeHead = res.writeHead
return function (statusCode, statusMessage, headers) {
headers = typeof statusMessage === 'string' ? headers : statusMessage
headers = Object.assign(res.getHeaders(), headers)
if (req.method.toLowerCase() === 'options' && isOriginAllowed(req, headers)) {
addAllowHeaders(req, res, headers)
}
return writeHead.apply(this, arguments)
}
},
getContext (req) {
return contexts.get(req)
},
wrapRes (context, req, res, end) {
return function () {
web.finishAll(context)
return end.apply(res, arguments)
}
},
wrapEnd (context) {
const scope = context.tracer.scope()
const req = context.req
const res = context.res
const end = res.end
res.writeHead = web.wrapWriteHead(context)
ends.set(res, this.wrapRes(context, req, res, end))
Object.defineProperty(res, 'end', {
configurable: true,
get () {
return ends.get(this)
},
set (value) {
ends.set(this, scope.bind(value, context.span))
}
})
}
}
function addAllowHeaders (req, res, headers) {
const allowHeaders = splitHeader(headers['access-control-allow-headers'])
const requestHeaders = splitHeader(req.headers['access-control-request-headers'])
const contextHeaders = [
'x-datadog-origin',
'x-datadog-parent-id',
'x-datadog-sampled', // Deprecated, but still accept it in case it's sent.
'x-datadog-sampling-priority',
'x-datadog-trace-id',
'x-datadog-tags'
]
for (const header of contextHeaders) {
if (~requestHeaders.indexOf(header)) {
allowHeaders.push(header)
}
}
if (allowHeaders.length > 0) {
res.setHeader('access-control-allow-headers', uniq(allowHeaders).join(','))
}
}
function isOriginAllowed (req, headers) {
const origin = req.headers.origin
const allowOrigin = headers['access-control-allow-origin']
return origin && (allowOrigin === '*' || allowOrigin === origin)
}
function splitHeader (str) {
return typeof str === 'string' ? str.split(/\s*,\s*/) : []
}
function reactivate (req, fn) {
const context = contexts.get(req)
return context
? context.tracer.scope().activate(context.span, fn)
: fn()
}
function addRequestTags (context) {
const { req, span, config } = context
const url = extractURL(req)
span.addTags({
[HTTP_URL]: web.obfuscateQs(config, url),
[HTTP_METHOD]: req.method,
[SPAN_KIND]: SERVER,
[SPAN_TYPE]: WEB,
[HTTP_USERAGENT]: req.headers['user-agent']
})
// if client ip has already been set by appsec, no need to run it again
if (config.clientIpEnabled && !span.context()._tags.hasOwnProperty(HTTP_CLIENT_IP)) {
const clientIp = extractIp(config, req)
if (clientIp) {
span.setTag(HTTP_CLIENT_IP, clientIp)
}
}
addHeaders(context)
}
function addResponseTags (context) {
const { req, res, paths, span } = context
if (paths.length > 0) {
span.setTag(HTTP_ROUTE, paths.join(''))
}
span.addTags({
[HTTP_STATUS_CODE]: res.statusCode
})
web.addStatusError(req, res.statusCode)
}
function addResourceTag (context) {
const { req, span } = context
const tags = span.context()._tags
if (tags['resource.name']) return
const resource = [req.method, tags[HTTP_ROUTE]]
.filter(val => val)
.join(' ')
span.setTag(RESOURCE_NAME, resource)
}
function addHeaders (context) {
const { req, res, config, span } = context
config.headers.forEach(([key, tag]) => {
const reqHeader = req.headers[key]
const resHeader = res.getHeader(key)
if (reqHeader) {
span.setTag(tag || `${HTTP_REQUEST_HEADERS}.${key}`, reqHeader)
}
if (resHeader) {
span.setTag(tag || `${HTTP_RESPONSE_HEADERS}.${key}`, resHeader)
}
})
}
function extractURL (req) {
const headers = req.headers
if (req.stream) {
return `${headers[HTTP2_HEADER_SCHEME]}://${headers[HTTP2_HEADER_AUTHORITY]}${headers[HTTP2_HEADER_PATH]}`
} else {
const protocol = getProtocol(req)
return `${protocol}://${req.headers.host}${req.originalUrl || req.url}`
}
}
function getProtocol (req) {
if (req.socket && req.socket.encrypted) return 'https'
if (req.connection && req.connection.encrypted) return 'https'
return 'http'
}
function getHeadersToRecord (config) {
if (Array.isArray(config.headers)) {
try {
return config.headers
.map(h => h.split(':'))
.map(([key, tag]) => [key.toLowerCase(), tag])
} catch (err) {
log.error(err)
}
} else if (config.hasOwnProperty('headers')) {
log.error('Expected `headers` to be an array of strings.')
}
return []
}
function getStatusValidator (config) {
if (typeof config.validateStatus === 'function') {
return config.validateStatus
} else if (config.hasOwnProperty('validateStatus')) {
log.error('Expected `validateStatus` to be a function.')
}
return code => code < 500
}
function getHooks (config) {
const noop = () => {}
const request = (config.hooks && config.hooks.request) || noop
return { request }
}
function getMiddlewareSetting (config) {
if (config && typeof config.middleware === 'boolean') {
return config.middleware
} else if (config && config.hasOwnProperty('middleware')) {
log.error('Expected `middleware` to be a boolean.')
}
return true
}
function getQsObfuscator (config) {
const obfuscator = config.queryStringObfuscation
if (typeof obfuscator === 'boolean') {
return obfuscator
}
if (typeof obfuscator === 'string') {
if (obfuscator === '') return false // disable obfuscator
if (obfuscator === '.*') return true // optimize full redact
try {
return new RegExp(obfuscator, 'gi')
} catch (err) {
log.error(err)
}
}
if (config.hasOwnProperty('queryStringObfuscation')) {
log.error('Expected `queryStringObfuscation` to be a regex string or boolean.')
}
return true
}
module.exports = web