http.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import { _optionalChain } from '@sentry/utils';
  2. import { URL } from 'url';
  3. import { NODE_VERSION } from '../../nodeVersion.js';
  4. /**
  5. * Assembles a URL that's passed to the users to filter on.
  6. * It can include raw (potentially PII containing) data, which we'll allow users to access to filter
  7. * but won't include in spans or breadcrumbs.
  8. *
  9. * @param requestOptions RequestOptions object containing the component parts for a URL
  10. * @returns Fully-formed URL
  11. */
  12. // TODO (v8): This function should include auth, query and fragment (it's breaking, so we need to wait for v8)
  13. function extractRawUrl(requestOptions) {
  14. const { protocol, hostname, port } = parseRequestOptions(requestOptions);
  15. const path = requestOptions.path ? requestOptions.path : '/';
  16. return `${protocol}//${hostname}${port}${path}`;
  17. }
  18. /**
  19. * Assemble a URL to be used for breadcrumbs and spans.
  20. *
  21. * @param requestOptions RequestOptions object containing the component parts for a URL
  22. * @returns Fully-formed URL
  23. */
  24. function extractUrl(requestOptions) {
  25. const { protocol, hostname, port } = parseRequestOptions(requestOptions);
  26. const path = requestOptions.pathname || '/';
  27. // always filter authority, see https://develop.sentry.dev/sdk/data-handling/#structuring-data
  28. const authority = requestOptions.auth ? redactAuthority(requestOptions.auth) : '';
  29. return `${protocol}//${authority}${hostname}${port}${path}`;
  30. }
  31. function redactAuthority(auth) {
  32. const [user, password] = auth.split(':');
  33. return `${user ? '[Filtered]' : ''}:${password ? '[Filtered]' : ''}@`;
  34. }
  35. /**
  36. * Handle various edge cases in the span description (for spans representing http(s) requests).
  37. *
  38. * @param description current `description` property of the span representing the request
  39. * @param requestOptions Configuration data for the request
  40. * @param Request Request object
  41. *
  42. * @returns The cleaned description
  43. */
  44. function cleanSpanDescription(
  45. description,
  46. requestOptions,
  47. request,
  48. ) {
  49. // nothing to clean
  50. if (!description) {
  51. return description;
  52. }
  53. // eslint-disable-next-line prefer-const
  54. let [method, requestUrl] = description.split(' ');
  55. // superagent sticks the protocol in a weird place (we check for host because if both host *and* protocol are missing,
  56. // we're likely dealing with an internal route and this doesn't apply)
  57. if (requestOptions.host && !requestOptions.protocol) {
  58. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
  59. requestOptions.protocol = _optionalChain([(request ), 'optionalAccess', _ => _.agent, 'optionalAccess', _2 => _2.protocol]); // worst comes to worst, this is undefined and nothing changes
  60. // This URL contains the filtered authority ([filtered]:[filtered]@example.com) but no fragment or query params
  61. requestUrl = extractUrl(requestOptions);
  62. }
  63. // internal routes can end up starting with a triple slash rather than a single one
  64. if (_optionalChain([requestUrl, 'optionalAccess', _3 => _3.startsWith, 'call', _4 => _4('///')])) {
  65. requestUrl = requestUrl.slice(2);
  66. }
  67. return `${method} ${requestUrl}`;
  68. }
  69. // the node types are missing a few properties which node's `urlToOptions` function spits out
  70. /**
  71. * Convert a URL object into a RequestOptions object.
  72. *
  73. * Copied from Node's internals (where it's used in http(s).request() and http(s).get()), modified only to use the
  74. * RequestOptions type above.
  75. *
  76. * See https://github.com/nodejs/node/blob/master/lib/internal/url.js.
  77. */
  78. function urlToOptions(url) {
  79. const options = {
  80. protocol: url.protocol,
  81. hostname:
  82. typeof url.hostname === 'string' && url.hostname.startsWith('[') ? url.hostname.slice(1, -1) : url.hostname,
  83. hash: url.hash,
  84. search: url.search,
  85. pathname: url.pathname,
  86. path: `${url.pathname || ''}${url.search || ''}`,
  87. href: url.href,
  88. };
  89. if (url.port !== '') {
  90. options.port = Number(url.port);
  91. }
  92. if (url.username || url.password) {
  93. options.auth = `${url.username}:${url.password}`;
  94. }
  95. return options;
  96. }
  97. /**
  98. * Normalize inputs to `http(s).request()` and `http(s).get()`.
  99. *
  100. * Legal inputs to `http(s).request()` and `http(s).get()` can take one of ten forms:
  101. * [ RequestOptions | string | URL ],
  102. * [ RequestOptions | string | URL, RequestCallback ],
  103. * [ string | URL, RequestOptions ], and
  104. * [ string | URL, RequestOptions, RequestCallback ].
  105. *
  106. * This standardizes to one of two forms: [ RequestOptions ] and [ RequestOptions, RequestCallback ]. A similar thing is
  107. * done as the first step of `http(s).request()` and `http(s).get()`; this just does it early so that we can interact
  108. * with the args in a standard way.
  109. *
  110. * @param requestArgs The inputs to `http(s).request()` or `http(s).get()`, as an array.
  111. *
  112. * @returns Equivalent args of the form [ RequestOptions ] or [ RequestOptions, RequestCallback ].
  113. */
  114. function normalizeRequestArgs(
  115. httpModule,
  116. requestArgs,
  117. ) {
  118. let callback, requestOptions;
  119. // pop off the callback, if there is one
  120. if (typeof requestArgs[requestArgs.length - 1] === 'function') {
  121. callback = requestArgs.pop() ;
  122. }
  123. // create a RequestOptions object of whatever's at index 0
  124. if (typeof requestArgs[0] === 'string') {
  125. requestOptions = urlToOptions(new URL(requestArgs[0]));
  126. } else if (requestArgs[0] instanceof URL) {
  127. requestOptions = urlToOptions(requestArgs[0]);
  128. } else {
  129. requestOptions = requestArgs[0];
  130. try {
  131. const parsed = new URL(
  132. requestOptions.path || '',
  133. `${requestOptions.protocol || 'http:'}//${requestOptions.hostname}`,
  134. );
  135. requestOptions = {
  136. pathname: parsed.pathname,
  137. search: parsed.search,
  138. hash: parsed.hash,
  139. ...requestOptions,
  140. };
  141. } catch (e) {
  142. // ignore
  143. }
  144. }
  145. // if the options were given separately from the URL, fold them in
  146. if (requestArgs.length === 2) {
  147. requestOptions = { ...requestOptions, ...requestArgs[1] };
  148. }
  149. // Figure out the protocol if it's currently missing
  150. if (requestOptions.protocol === undefined) {
  151. // Worst case we end up populating protocol with undefined, which it already is
  152. /* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any */
  153. // NOTE: Prior to Node 9, `https` used internals of `http` module, thus we don't patch it.
  154. // Because of that, we cannot rely on `httpModule` to provide us with valid protocol,
  155. // as it will always return `http`, even when using `https` module.
  156. //
  157. // See test/integrations/http.test.ts for more details on Node <=v8 protocol issue.
  158. if (NODE_VERSION.major > 8) {
  159. requestOptions.protocol =
  160. _optionalChain([(_optionalChain([httpModule, 'optionalAccess', _5 => _5.globalAgent]) ), 'optionalAccess', _6 => _6.protocol]) ||
  161. _optionalChain([(requestOptions.agent ), 'optionalAccess', _7 => _7.protocol]) ||
  162. _optionalChain([(requestOptions._defaultAgent ), 'optionalAccess', _8 => _8.protocol]);
  163. } else {
  164. requestOptions.protocol =
  165. _optionalChain([(requestOptions.agent ), 'optionalAccess', _9 => _9.protocol]) ||
  166. _optionalChain([(requestOptions._defaultAgent ), 'optionalAccess', _10 => _10.protocol]) ||
  167. _optionalChain([(_optionalChain([httpModule, 'optionalAccess', _11 => _11.globalAgent]) ), 'optionalAccess', _12 => _12.protocol]);
  168. }
  169. /* eslint-enable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any */
  170. }
  171. // return args in standardized form
  172. if (callback) {
  173. return [requestOptions, callback];
  174. } else {
  175. return [requestOptions];
  176. }
  177. }
  178. function parseRequestOptions(requestOptions)
  179. {
  180. const protocol = requestOptions.protocol || '';
  181. const hostname = requestOptions.hostname || requestOptions.host || '';
  182. // Don't log standard :80 (http) and :443 (https) ports to reduce the noise
  183. // Also don't add port if the hostname already includes a port
  184. const port =
  185. !requestOptions.port || requestOptions.port === 80 || requestOptions.port === 443 || /^(.*):(\d+)$/.test(hostname)
  186. ? ''
  187. : `:${requestOptions.port}`;
  188. return { protocol, hostname, port };
  189. }
  190. export { cleanSpanDescription, extractRawUrl, extractUrl, normalizeRequestArgs, urlToOptions };
  191. //# sourceMappingURL=http.js.map