@vtex/api 6.52.1 → 6.52.5

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.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,57 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [6.52.5]
11
+
12
+ ### Security
13
+
14
+ - Error objects assigned to the response body no longer expose the request
15
+ configuration, credentials, connection agents or, outside link mode, the
16
+ stack. If the response cannot be sanitized it is withheld. A body the walk cannot
17
+ inspect completely is sent as it is, and the limit is reported.
18
+ - String response bodies built from an error (`` `failed: ${JSON.stringify(error)}` ``) no longer
19
+ expose its credentials: the request's tokens, the value of credential headers (`authorization`,
20
+ `proxy-authorization`, `VtexIdclientAutCookie`, `x-vtex-api-*`), the VTEX ID cookie and `Bearer`
21
+ tokens are replaced with `[Redacted]`. The same names are replaced when the request is
22
+ embedded as header lines (`Cookie`, `Set-Cookie`, `X-Vtex-Session`), not only as JSON keys.
23
+ - The body the runtime builds from a thrown value goes through the same sanitizer. An error that is
24
+ a property of the thrown one (`originalError`), or sits in a thrown object, no longer exposes its
25
+ request configuration, request or stack, and the upstream response headers are no longer
26
+ included. Linked apps keep the stack but no longer get the configuration in the body.
27
+ - Errors printed by the runtime no longer include the request configuration.
28
+ - GraphQL error responses no longer include the stack outside link mode.
29
+
30
+ ## [6.52.4]
31
+ ### Fixed
32
+ - `yarn test` no longer fails to even load `src/service/logger` and its transitive
33
+ dependents under `jest@25`: that resolver predates the package `exports` field, so
34
+ it couldn't resolve `@vtex/diagnostics-nodejs@0.1.0-beta.10`'s OpenTelemetry OTLP
35
+ exporter chain (reached via `@opentelemetry/otlp-exporter-base/node-http`), which is
36
+ loaded at module-eval time by nearly every service module. Stubbed the package for
37
+ tests via `jest.config.js` `moduleNameMapper` (see `jest/stubs/diagnosticsNodejs.js`);
38
+ the real telemetry/log-client paths are lazy and error-guarded, so this has no
39
+ behavioural effect on the code under test.
40
+ - `TestServer.closeServer()`'s `new Promise((resolve) => ...)` failed to type-check
41
+ under strict `Promise<void>` typing; typed the executor explicitly.
42
+ - The `axiosTracing.test.ts` "forcing ECONNREFUSED" suite pointed at `localhost`, which
43
+ Node's `autoSelectFamily` (Happy Eyeballs) resolves to both loopback addresses; since
44
+ both connections are refused, Node throws an `AggregateError` with an empty top-level
45
+ `message` instead of the plain `ECONNREFUSED` error the test asserts on. Uses the
46
+ literal `127.0.0.1` instead.
47
+
48
+ ## [6.52.2]
49
+ ### Fixed
50
+ - TSLint violations across `src/axios.d.ts`, `src/caches/MultilayeredCache.ts`,
51
+ `src/HttpClient/middlewares/cache.ts`, `src/HttpClient/middlewares/request/index.ts`,
52
+ `src/HttpClient/middlewares/request/setupAxios/interceptors/tracing/spanSetup.ts`,
53
+ `src/service/logger/client.ts`, `src/service/logger/logger.ts`,
54
+ `src/service/telemetry/client.ts`,
55
+ `src/service/worker/runtime/graphql/schema/schemaDirectives/Auth.ts`,
56
+ `src/tracing/utils.test.ts` and `src/utils/buildFullPath.ts` (semicolon usage,
57
+ member ordering, `==` vs `===`, unsorted object keys, import ordering, and
58
+ other `tslint-config-vtex` rule failures) that were blocking the CI lint gate
59
+ on unrelated PRs against the 6.x line.
60
+
10
61
  ## [6.51.0] - 2026-06-23
11
62
  ### Added
12
63
  - Base `IOClients` getter `janusCatalogSystem` (Janus Catalog) and
@@ -73,7 +73,7 @@ const cacheMiddleware = ({ type, storage, asyncSet }) => {
73
73
  ["key-with-segment" /* HttpCacheLogFields.KEY_WITH_SEGMENT */]: keyWithSegment,
74
74
  });
75
75
  const cacheReadSpan = createCacheSpan(cacheType, 'read', tracer, span);
76
- let cached = undefined;
76
+ let cached;
77
77
  try {
78
78
  const cacheHasWithSegment = await storage.has(keyWithSegment);
79
79
  cached = cacheHasWithSegment ? await storage.get(keyWithSegment) : await storage.get(key);
@@ -114,7 +114,7 @@ const cacheMiddleware = ({ type, storage, asyncSet }) => {
114
114
  if (cachedEtag && validateStatus(response.status)) {
115
115
  ctx.config.headers = {
116
116
  ...ctx.config.headers,
117
- 'if-none-match': cachedEtag
117
+ 'if-none-match': cachedEtag,
118
118
  };
119
119
  ctx.config.validateStatus = validateStatus;
120
120
  }
@@ -4,9 +4,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.requestMiddleware = exports.routerCacheMiddleware = exports.defaultsMiddleware = void 0;
7
- const buildFullPath_1 = __importDefault(require("../../../utils/buildFullPath"));
8
7
  const qs_1 = require("qs");
9
8
  const ramda_1 = require("ramda");
9
+ const buildFullPath_1 = __importDefault(require("../../../utils/buildFullPath"));
10
10
  const Tags_1 = require("../../../tracing/Tags");
11
11
  const renameBy_1 = require("../../../utils/renameBy");
12
12
  const setupAxios_1 = require("./setupAxios");
@@ -4,10 +4,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.injectResponseInfoOnSpan = exports.injectRequestInfoOnSpan = void 0;
7
- const buildFullPath_1 = __importDefault(require("../../../../../../utils/buildFullPath"));
8
7
  const constants_1 = require("../../../../../../constants");
9
8
  const Tags_1 = require("../../../../../../tracing/Tags");
10
9
  const utils_1 = require("../../../../../../tracing/utils");
10
+ const buildFullPath_1 = __importDefault(require("../../../../../../utils/buildFullPath"));
11
11
  const injectRequestInfoOnSpan = (span, http, config) => {
12
12
  span === null || span === void 0 ? void 0 : span.addTags({
13
13
  [Tags_1.OpentracingTags.SPAN_KIND]: Tags_1.OpentracingTags.SPAN_KIND_RPC_CLIENT,
@@ -8,7 +8,7 @@ class MultilayeredCache {
8
8
  this.hits = 0;
9
9
  this.total = 0;
10
10
  this.get = async (key, fetcher) => {
11
- let value = undefined;
11
+ let value;
12
12
  let maxAge;
13
13
  let successIndex = await this.findIndex(async (cache) => {
14
14
  const [getValue, hasKey] = await Promise.all([cache.get(key), cache.has(key)]);
@@ -5,7 +5,7 @@ const diagnostics_nodejs_1 = require("@vtex/diagnostics-nodejs");
5
5
  const telemetry_1 = require("../telemetry");
6
6
  let logClient;
7
7
  let isInitializing = false;
8
- let initPromise = undefined;
8
+ let initPromise;
9
9
  async function getLogClient(account, workspace, appName) {
10
10
  if (logClient) {
11
11
  return logClient;
@@ -23,11 +23,11 @@ async function initializeClient(account, workspace, appName) {
23
23
  const telemetryClient = await (0, telemetry_1.getTelemetryClient)();
24
24
  const logsConfig = diagnostics_nodejs_1.Exporters.CreateLogsExporterConfig({
25
25
  endpoint: process.env.OTEL_EXPORTER_OTLP_ENDPOINT,
26
+ headers: { 'Content-Type': 'application/json' },
27
+ interval: 5,
26
28
  path: process.env.OTEL_EXPORTER_OTLP_PATH || '/v1/logs',
27
29
  protocol: 'http',
28
- interval: 5,
29
30
  timeoutSeconds: 5,
30
- headers: { 'Content-Type': 'application/json' },
31
31
  });
32
32
  const logsExporter = diagnostics_nodejs_1.Exporters.CreateExporter(logsConfig, 'otlp');
33
33
  await logsExporter.initialize();
@@ -9,10 +9,10 @@ export declare class Logger {
9
9
  private logClient;
10
10
  private clientInitPromise;
11
11
  constructor(ctx: LoggerContext);
12
- private initLogClient;
13
12
  debug: (message: any) => void;
14
13
  info: (message: any) => void;
15
14
  warn: (warning: any) => void;
16
15
  error: (error: any) => void;
17
16
  log: (message: any, level: LogLevel) => void;
17
+ private initLogClient;
18
18
  }
@@ -4,8 +4,8 @@ exports.Logger = void 0;
4
4
  const constants_1 = require("../../constants");
5
5
  const error_1 = require("../../utils/error");
6
6
  const log_1 = require("../../utils/log");
7
- const loggerTypes_1 = require("./loggerTypes");
8
7
  const client_1 = require("./client");
8
+ const loggerTypes_1 = require("./loggerTypes");
9
9
  const app = constants_1.APP.ID;
10
10
  const EMPTY_MESSAGE = 'Logger.log was called with null or undefined message';
11
11
  class Logger {
@@ -42,7 +42,7 @@ class Logger {
42
42
  }
43
43
  if (this.logClient) {
44
44
  try {
45
- let logMessage = typeof data === 'string' ? data : JSON.stringify(data);
45
+ const logMessage = typeof data === 'string' ? data : JSON.stringify(data);
46
46
  switch (level) {
47
47
  case loggerTypes_1.LogLevel.Debug:
48
48
  this.logClient.debug(logMessage, inflatedLog);
@@ -90,7 +90,7 @@ class Logger {
90
90
  });
91
91
  this.logClient = await Promise.race([
92
92
  (0, client_1.getLogClient)(this.account, this.workspace, constants_1.APP.NAME),
93
- timeoutPromise
93
+ timeoutPromise,
94
94
  ]);
95
95
  return this.logClient;
96
96
  }
@@ -4,21 +4,35 @@ exports.resetTelemetryClient = exports.getTelemetryClient = void 0;
4
4
  const diagnostics_nodejs_1 = require("@vtex/diagnostics-nodejs");
5
5
  const constants_1 = require("../../constants");
6
6
  class TelemetryClientSingleton {
7
- constructor() {
8
- this.initializationPromise = undefined;
9
- }
10
7
  static getInstance() {
11
8
  if (!TelemetryClientSingleton.instance) {
12
9
  TelemetryClientSingleton.instance = new TelemetryClientSingleton();
13
10
  }
14
11
  return TelemetryClientSingleton.instance;
15
12
  }
13
+ constructor() {
14
+ this.initializationPromise = undefined;
15
+ }
16
+ async getClient() {
17
+ if (this.telemetryClient) {
18
+ return this.telemetryClient;
19
+ }
20
+ if (this.initializationPromise) {
21
+ return this.initializationPromise;
22
+ }
23
+ this.initializationPromise = this.initTelemetryClient();
24
+ return this.initializationPromise;
25
+ }
26
+ reset() {
27
+ this.telemetryClient = undefined;
28
+ this.initializationPromise = undefined;
29
+ }
16
30
  async initTelemetryClient() {
17
31
  try {
18
32
  const telemetryClient = await (0, diagnostics_nodejs_1.NewTelemetryClient)('node-vtex-api', constants_1.APP.ID || 'vtex-app', {
19
33
  additionalAttrs: {
20
- 'version': constants_1.APP.VERSION || '',
21
34
  'environment': process.env.VTEX_WORKSPACE || 'development',
35
+ 'version': constants_1.APP.VERSION || '',
22
36
  },
23
37
  });
24
38
  this.telemetryClient = telemetryClient;
@@ -32,20 +46,6 @@ class TelemetryClientSingleton {
32
46
  this.initializationPromise = undefined;
33
47
  }
34
48
  }
35
- async getClient() {
36
- if (this.telemetryClient) {
37
- return this.telemetryClient;
38
- }
39
- if (this.initializationPromise) {
40
- return this.initializationPromise;
41
- }
42
- this.initializationPromise = this.initTelemetryClient();
43
- return this.initializationPromise;
44
- }
45
- reset() {
46
- this.telemetryClient = undefined;
47
- this.initializationPromise = undefined;
48
- }
49
49
  }
50
50
  async function getTelemetryClient() {
51
51
  return TelemetryClientSingleton.getInstance().getClient();
@@ -26,6 +26,7 @@ const graphql_1 = require("./runtime/graphql");
26
26
  const http_2 = require("./runtime/http");
27
27
  const error_1 = require("./runtime/http/middlewares/error");
28
28
  const rateLimit_1 = require("./runtime/http/middlewares/rateLimit");
29
+ const sanitizeResponse_1 = require("./runtime/http/middlewares/sanitizeResponse");
29
30
  const router_2 = require("./runtime/http/router");
30
31
  const routes_1 = require("./runtime/http/routes");
31
32
  const statusTrack_1 = require("./runtime/statusTrack");
@@ -163,7 +164,8 @@ const startWorker = (serviceJSON) => {
163
164
  .use((0, middlewares_1.addMetricsLoggerMiddleware)())
164
165
  .use((0, rateLimit_1.concurrentRateLimiter)((_a = serviceJSON === null || serviceJSON === void 0 ? void 0 : serviceJSON.rateLimitPerReplica) === null || _a === void 0 ? void 0 : _a.concurrent))
165
166
  .use((0, koa_compress_1.default)())
166
- .use(middlewares_1.recorderMiddleware);
167
+ .use(middlewares_1.recorderMiddleware)
168
+ .use(sanitizeResponse_1.sanitizeResponse);
167
169
  const service = (0, loaders_1.getService)();
168
170
  const { config: { clients } } = service;
169
171
  if (clients) {
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.addProcessListeners = exports.logger = void 0;
4
4
  const os_1 = require("os");
5
5
  const RequestCancelledError_1 = require("../../errors/RequestCancelledError");
6
+ const responseSanitizer_1 = require("../../utils/responseSanitizer");
6
7
  const logger_1 = require("../logger");
7
8
  exports.logger = new logger_1.Logger({ account: 'unhandled', workspace: 'unhandled', requestId: 'unhandled', operationId: 'unhandled', production: process.env.VTEX_PRODUCTION === 'true' });
8
9
  let watched;
@@ -15,18 +16,19 @@ const addProcessListeners = () => {
15
16
  return;
16
17
  }
17
18
  watched = process.on('uncaughtException', (err) => {
18
- console.error('uncaughtException', err);
19
+ console.error('uncaughtException', (0, responseSanitizer_1.toLoggableError)(err));
19
20
  if (err && exports.logger) {
20
21
  err.type = 'uncaughtException';
21
22
  exports.logger.error(err);
22
23
  }
23
24
  process.exit(420);
24
25
  });
25
- process.on('unhandledRejection', (reason, promise) => {
26
+ process.on('unhandledRejection', (reason) => {
26
27
  if (reason instanceof RequestCancelledError_1.RequestCancelledError) {
27
28
  return;
28
29
  }
29
- console.error('unhandledRejection', reason, promise);
30
+ // The promise is not printed: inspecting it shows the rejection reason again, unsanitized.
31
+ console.error('unhandledRejection', (0, responseSanitizer_1.toLoggableError)(reason));
30
32
  if (reason && exports.logger) {
31
33
  reason.type = 'unhandledRejection';
32
34
  exports.logger.error(reason);
@@ -36,8 +36,14 @@ const createLogErrorToSplunk = (vtex) => (err) => {
36
36
  logger.log(log, level);
37
37
  }
38
38
  finally {
39
- if (!constants_1.LINKED && ((_f = (_e = err === null || err === void 0 ? void 0 : err.extensions) === null || _e === void 0 ? void 0 : _e.exception) === null || _f === void 0 ? void 0 : _f.sensitive)) {
40
- delete err.extensions.exception.sensitive;
39
+ if (!constants_1.LINKED) {
40
+ // Already logged above. The response must not carry them.
41
+ if ((_f = (_e = err === null || err === void 0 ? void 0 : err.extensions) === null || _e === void 0 ? void 0 : _e.exception) === null || _f === void 0 ? void 0 : _f.sensitive) {
42
+ delete err.extensions.exception.sensitive;
43
+ }
44
+ if (err) {
45
+ delete err.stack;
46
+ }
41
47
  }
42
48
  }
43
49
  };
@@ -50,7 +50,7 @@ async function auth(ctx, authArgs) {
50
50
  }
51
51
  }
52
52
  function parseArgs(authArgs) {
53
- if (authArgs.scope == 'PUBLIC') {
53
+ if (authArgs.scope === 'PUBLIC') {
54
54
  return authArgs;
55
55
  }
56
56
  if (!authArgs.productCode || !authArgs.resourceCode) {
@@ -63,7 +63,7 @@ class Auth extends graphql_tools_1.SchemaDirectiveVisitor {
63
63
  const { resolve = graphql_1.defaultFieldResolver } = field;
64
64
  field.resolve = async (root, args, ctx, info) => {
65
65
  const authArgs = parseArgs(this.args);
66
- if (!authArgs.scope || authArgs.scope == 'PRIVATE') {
66
+ if (!authArgs.scope || authArgs.scope === 'PRIVATE') {
67
67
  await auth(ctx, authArgs);
68
68
  }
69
69
  return resolve(root, args, ctx, info);
@@ -5,7 +5,9 @@ const constants_1 = require("../../../../../constants");
5
5
  const RequestCancelledError_1 = require("../../../../../errors/RequestCancelledError");
6
6
  const TooManyRequestsError_1 = require("../../../../../errors/TooManyRequestsError");
7
7
  const error_1 = require("../../../../../utils/error");
8
+ const responseSanitizer_1 = require("../../../../../utils/responseSanitizer");
8
9
  const logger_1 = require("../../../../logger");
10
+ const sanitizeResponse_1 = require("./sanitizeResponse");
9
11
  const CACHE_CONTROL_HEADER = 'cache-control';
10
12
  const META_HEADER = 'x-vtex-meta';
11
13
  const ETAG_HEADER = 'etag';
@@ -45,6 +47,19 @@ const logAndRemoveSensitiveData = (ctx, err) => {
45
47
  });
46
48
  }
47
49
  };
50
+ // What is sent to the client for a thrown value. It is built with the sanitizer that covers the
51
+ // bodies the app sets, so both paths expose the same: `cleanError` alone removes credentials only
52
+ // at the top level, and by a short list of header names, so an error that is a property of the
53
+ // thrown value (`originalError`) kept its config, request and stack.
54
+ const buildResponseBody = (thrown) => {
55
+ try {
56
+ return (0, error_1.cleanError)((0, responseSanitizer_1.sanitizeErrors)(thrown, { linked: constants_1.LINKED }).value);
57
+ }
58
+ catch (sanitizeError) {
59
+ console.error('[node-vtex-api error] the thrown value could not be sanitized', sanitizeError === null || sanitizeError === void 0 ? void 0 : sanitizeError.name);
60
+ return sanitizeResponse_1.WITHHELD_BODY;
61
+ }
62
+ };
48
63
  async function error(ctx, next) {
49
64
  var _a;
50
65
  try {
@@ -59,7 +74,8 @@ async function error(ctx, next) {
59
74
  ctx.status = TooManyRequestsError_1.tooManyRequestsStatus;
60
75
  return;
61
76
  }
62
- console.error('[node-vtex-api error]', e);
77
+ // The raw error carries the request config, whose headers include credentials.
78
+ console.error('[node-vtex-api error]', (0, responseSanitizer_1.toLoggableError)(e));
63
79
  const err = (0, error_1.cleanError)(e);
64
80
  // Add response
65
81
  ctx.status = (e === null || e === void 0 ? void 0 : e.status) && e.status >= 400 && e.status <= 599
@@ -78,8 +94,10 @@ async function error(ctx, next) {
78
94
  else {
79
95
  ctx.set(CACHE_CONTROL_HEADER, `no-cache, no-store`);
80
96
  }
97
+ // `err` is what is logged, which keeps the config and the request to debug with. It is not the
98
+ // body: the sanitized value is.
81
99
  logAndRemoveSensitiveData(ctx, err);
82
- ctx.body = ctx.body || err;
100
+ ctx.body = ctx.body || buildResponseBody(e);
83
101
  }
84
102
  }
85
103
  exports.error = error;
@@ -0,0 +1,37 @@
1
+ import { ServiceContext } from '../../typings';
2
+ /**
3
+ * Setting this environment variable to `off` disables the sanitizer.
4
+ */
5
+ export declare const RESPONSE_SANITIZER_SWITCH = "VTEX_IO_RESPONSE_SANITIZER";
6
+ /**
7
+ * Sent when the body could not be sanitized: it is not known whether it is safe, so it is withheld.
8
+ */
9
+ export declare const WITHHELD_BODY: {
10
+ code: string;
11
+ message: string;
12
+ };
13
+ export declare enum SanitizerReason {
14
+ ErrorRemoved = "error_object_removed",
15
+ SecretRemoved = "secret_removed",
16
+ LimitReached = "limit_reached",
17
+ Failed = "sanitizer_failed"
18
+ }
19
+ /**
20
+ * Removes error objects (`Error`, `AxiosError`) from the response body, so apps that write a caught
21
+ * error to `ctx.body` do not expose the request configuration (credentials), the stack and the
22
+ * connection internals to the caller.
23
+ *
24
+ * String bodies are also covered, for errors that were turned into text: the credentials in them
25
+ * (the ones of the request, the credential headers, `Bearer` tokens) are replaced.
26
+ *
27
+ * It must be registered after `compress()`, which serializes the body: it needs to see the object,
28
+ * and to change it before it is serialized.
29
+ *
30
+ * The body is also sanitized when the downstream handlers throw after having set it, as the error
31
+ * middleware keeps an existing body. If the sanitizer itself fails the body is withheld. If it
32
+ * cannot inspect the whole body, the response is sent and the limit is reported. It never throws.
33
+ *
34
+ * It is a backstop for apps that return errors by mistake. It is not a boundary against an app that
35
+ * wants to disclose its own credentials, which has them.
36
+ */
37
+ export declare function sanitizeResponse(ctx: ServiceContext, next: () => Promise<void>): Promise<void>;
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sanitizeResponse = exports.SanitizerReason = exports.WITHHELD_BODY = exports.RESPONSE_SANITIZER_SWITCH = void 0;
4
+ const constants_1 = require("../../../../../constants");
5
+ const responseSanitizer_1 = require("../../../../../utils/responseSanitizer");
6
+ const stringRedactor_1 = require("../../../../../utils/stringRedactor");
7
+ /**
8
+ * Setting this environment variable to `off` disables the sanitizer.
9
+ */
10
+ exports.RESPONSE_SANITIZER_SWITCH = 'VTEX_IO_RESPONSE_SANITIZER';
11
+ /**
12
+ * Sent when the body could not be sanitized: it is not known whether it is safe, so it is withheld.
13
+ */
14
+ exports.WITHHELD_BODY = {
15
+ code: 'E_RESPONSE_WITHHELD',
16
+ message: 'The response could not be safely returned.',
17
+ };
18
+ var SanitizerReason;
19
+ (function (SanitizerReason) {
20
+ SanitizerReason["ErrorRemoved"] = "error_object_removed";
21
+ SanitizerReason["SecretRemoved"] = "secret_removed";
22
+ SanitizerReason["LimitReached"] = "limit_reached";
23
+ SanitizerReason["Failed"] = "sanitizer_failed";
24
+ })(SanitizerReason = exports.SanitizerReason || (exports.SanitizerReason = {}));
25
+ // Only the class of the error is reported: its message may carry what is being kept out.
26
+ const errorName = (err) => err === null || err === void 0 ? void 0 : err.name;
27
+ const warn = (ctx, reason, fields = {}) => {
28
+ var _a, _b, _c;
29
+ const payload = {
30
+ message: `response-sanitizer: ${reason}`,
31
+ reason,
32
+ routeId: (_b = (_a = ctx.vtex) === null || _a === void 0 ? void 0 : _a.route) === null || _b === void 0 ? void 0 : _b.id,
33
+ ...fields,
34
+ };
35
+ try {
36
+ if ((_c = ctx.vtex) === null || _c === void 0 ? void 0 : _c.logger) {
37
+ ctx.vtex.logger.warn(payload);
38
+ }
39
+ else {
40
+ console.warn(JSON.stringify(payload));
41
+ }
42
+ }
43
+ catch (err) {
44
+ // Logging must never break the response: the event goes to the process output instead.
45
+ console.warn(JSON.stringify({ ...payload, loggerErrorName: errorName(err) }));
46
+ }
47
+ };
48
+ const withholdBody = (ctx) => {
49
+ try {
50
+ if (ctx.status < 400) {
51
+ ctx.status = 500;
52
+ }
53
+ ctx.body = exports.WITHHELD_BODY;
54
+ }
55
+ catch (err) {
56
+ // There is nothing else that can be done safely: as a last resort the response is emptied.
57
+ ctx.body = null;
58
+ warn(ctx, SanitizerReason.Failed, { withholdErrorName: errorName(err) });
59
+ }
60
+ };
61
+ // A string body can hold an error that was turned into text (`JSON.stringify(error)`), which has
62
+ // no error object left to find. What can be told apart is the credential itself.
63
+ const redactStringBody = (ctx, body) => {
64
+ var _a;
65
+ const { authToken, adminUserAuthToken, storeUserAuthToken } = (_a = ctx.vtex) !== null && _a !== void 0 ? _a : {};
66
+ const { value, redacted } = (0, stringRedactor_1.redactSecrets)(body, [authToken, adminUserAuthToken, storeUserAuthToken]);
67
+ if (redacted === 0) {
68
+ return;
69
+ }
70
+ // Koa keeps the type already set when the body is a string, and updates the length.
71
+ ctx.body = value;
72
+ warn(ctx, SanitizerReason.SecretRemoved, { redactions: redacted });
73
+ };
74
+ const sanitizeCtxBody = (ctx) => {
75
+ try {
76
+ if (process.env[exports.RESPONSE_SANITIZER_SWITCH] === 'off') {
77
+ return;
78
+ }
79
+ const body = ctx.body;
80
+ if (typeof body === 'string') {
81
+ redactStringBody(ctx, body);
82
+ return;
83
+ }
84
+ if (body === null || typeof body !== 'object' || Buffer.isBuffer(body) || typeof body.pipe === 'function') {
85
+ return;
86
+ }
87
+ // Most bodies have no error. A scan decides that without the copying walk; the walk runs only
88
+ // once an error has been found, or the scan could not finish.
89
+ const scan = (0, responseSanitizer_1.scanForErrors)(body);
90
+ if (scan === 'clean') {
91
+ return;
92
+ }
93
+ const { value, replaced, truncated } = (0, responseSanitizer_1.sanitizeErrors)(body, { linked: constants_1.LINKED });
94
+ if (truncated) {
95
+ // What is beyond the limit is left as it is, outside an error. The response is still sent:
96
+ // withholding it would break the large legitimate responses. It is reported to be measured.
97
+ warn(ctx, SanitizerReason.LimitReached);
98
+ }
99
+ if (value === body) {
100
+ return;
101
+ }
102
+ // Koa sets `Content-Type: application/json` whenever an object is assigned to the body, which
103
+ // would override a type chosen by the app.
104
+ const contentType = ctx.response.get('Content-Type');
105
+ ctx.body = value;
106
+ if (contentType) {
107
+ ctx.set('Content-Type', contentType);
108
+ }
109
+ warn(ctx, SanitizerReason.ErrorRemoved, { errors: replaced });
110
+ }
111
+ catch (err) {
112
+ // Fails closed: whatever made it fail may also be hiding a credential.
113
+ withholdBody(ctx);
114
+ warn(ctx, SanitizerReason.Failed, { errorName: errorName(err) });
115
+ }
116
+ };
117
+ /**
118
+ * Removes error objects (`Error`, `AxiosError`) from the response body, so apps that write a caught
119
+ * error to `ctx.body` do not expose the request configuration (credentials), the stack and the
120
+ * connection internals to the caller.
121
+ *
122
+ * String bodies are also covered, for errors that were turned into text: the credentials in them
123
+ * (the ones of the request, the credential headers, `Bearer` tokens) are replaced.
124
+ *
125
+ * It must be registered after `compress()`, which serializes the body: it needs to see the object,
126
+ * and to change it before it is serialized.
127
+ *
128
+ * The body is also sanitized when the downstream handlers throw after having set it, as the error
129
+ * middleware keeps an existing body. If the sanitizer itself fails the body is withheld. If it
130
+ * cannot inspect the whole body, the response is sent and the limit is reported. It never throws.
131
+ *
132
+ * It is a backstop for apps that return errors by mistake. It is not a boundary against an app that
133
+ * wants to disclose its own credentials, which has them.
134
+ */
135
+ async function sanitizeResponse(ctx, next) {
136
+ try {
137
+ await next();
138
+ }
139
+ finally {
140
+ sanitizeCtxBody(ctx);
141
+ }
142
+ }
143
+ exports.sanitizeResponse = sanitizeResponse;
@@ -19,8 +19,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
19
19
  * @returns {string} The combined full path
20
20
  */
21
21
  function default_1(baseURL, requestedURL, allowAbsoluteUrls) {
22
- let isRelativeUrl = !isAbsoluteURL(requestedURL);
23
- if (baseURL && (isRelativeUrl || allowAbsoluteUrls == false)) {
22
+ const isRelativeUrl = !isAbsoluteURL(requestedURL);
23
+ if (baseURL && (isRelativeUrl || allowAbsoluteUrls === false)) {
24
24
  return combineURLs(baseURL, requestedURL);
25
25
  }
26
26
  return requestedURL;
@@ -0,0 +1,52 @@
1
+ export interface SanitizeErrorsOptions {
2
+ /**
3
+ * When true (linked apps in development, and logs) the stack is kept to help debugging.
4
+ */
5
+ linked: boolean;
6
+ maxDepth?: number;
7
+ maxNodes?: number;
8
+ /**
9
+ * Total length of the arrays and objects visited, so that the cost of a long flat array is
10
+ * bounded the same as the number of containers.
11
+ */
12
+ maxValues?: number;
13
+ }
14
+ export interface SanitizeErrorsResult<T> {
15
+ /**
16
+ * The same reference received when nothing had to change.
17
+ */
18
+ value: T;
19
+ /**
20
+ * Number of error instances replaced by their safe representation.
21
+ */
22
+ replaced: number;
23
+ /**
24
+ * True when a limit (depth, containers, values) stopped the walk early. Outside an error the part
25
+ * not visited is left as it is. Inside an error it is replaced by a marker.
26
+ */
27
+ truncated: boolean;
28
+ }
29
+ /**
30
+ * Finds `Error` instances (including `AxiosError`) in a response body, at any depth of plain
31
+ * objects and arrays, and replaces each one by a safe representation: the message, name, code and
32
+ * status are kept, while the request config, the Node request, the agents, the upstream headers
33
+ * and, unless linked, the stack are removed.
34
+ *
35
+ * Strings, buffers, streams and class instances other than errors are returned untouched.
36
+ */
37
+ export declare const sanitizeErrors: <T>(value: T, options: SanitizeErrorsOptions) => SanitizeErrorsResult<T>;
38
+ export type ErrorScan = 'clean' | 'has-error' | 'truncated';
39
+ /**
40
+ * Says whether `value` contains an `Error`, without copying it. The copying walk is only needed
41
+ * when this returns `has-error`. `truncated` means a budget (depth, containers, or the values they
42
+ * hold) was spent before the value was fully inspected, so whatever was not visited must not be sent.
43
+ */
44
+ export declare const scanForErrors: (value: object, options?: Pick<SanitizeErrorsOptions, 'maxDepth' | 'maxNodes' | 'maxValues'>) => ErrorScan;
45
+ /**
46
+ * The same as `sanitizeErrors` for what is written to the process output, where the stack is
47
+ * useful and kept. A thrown `Error` stays an error, so it is printed as one (stack, then its
48
+ * properties), without the request config, the request and the agents.
49
+ *
50
+ * It never throws.
51
+ */
52
+ export declare const toLoggableError: (value: unknown) => unknown;
@@ -0,0 +1,354 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toLoggableError = exports.scanForErrors = exports.sanitizeErrors = void 0;
4
+ const error_1 = require("./error");
5
+ /**
6
+ * Properties of an error that hold HTTP-client internals: the request config (headers carry
7
+ * credentials), the Node request object, and the connection agents. They are never serialized.
8
+ * `response` is rebuilt separately, so that its headers and request are left out too.
9
+ */
10
+ const INTERNAL_ERROR_KEYS = new Set(['config', 'request', 'httpAgent', 'httpsAgent', 'response']);
11
+ const DEFAULT_MAX_DEPTH = 32;
12
+ const DEFAULT_MAX_NODES = 200000;
13
+ // What the containers hold, counted by their length. About 35 ms to go through on a laptop, which
14
+ // is far past a legitimate response.
15
+ const DEFAULT_MAX_VALUES = 5000000;
16
+ const TRUNCATED = '[Truncated]';
17
+ const CIRCULAR = '[Circular]';
18
+ // `instanceof` is not enough for errors that come from another realm (a `vm` context), which
19
+ // `Object.prototype.toString` still identifies.
20
+ const isError = (node) => node instanceof Error || Object.prototype.toString.call(node) === '[object Error]';
21
+ // Buffers and typed arrays, which have one entry per byte when walked as objects.
22
+ const isBinary = (node) => Buffer.isBuffer(node) || ArrayBuffer.isView(node) || node instanceof ArrayBuffer;
23
+ const isPlainObject = (node) => {
24
+ const proto = Object.getPrototypeOf(node);
25
+ return proto === Object.prototype || proto === null;
26
+ };
27
+ /**
28
+ * What is shown instead of a value that is kept out of an error: binary data, streams and
29
+ * instances of classes, which can hold large or circular graphs (sockets, agents, handles).
30
+ */
31
+ const describeOpaque = (node) => {
32
+ var _a;
33
+ if (node instanceof Date) {
34
+ return Number.isNaN(node.getTime()) ? '[Invalid Date]' : node.toISOString();
35
+ }
36
+ const name = ((_a = node === null || node === void 0 ? void 0 : node.constructor) === null || _a === void 0 ? void 0 : _a.name) || 'Object';
37
+ if (isBinary(node)) {
38
+ return `[${name} ${node.byteLength} bytes]`;
39
+ }
40
+ return `[${name}]`;
41
+ };
42
+ class ErrorSanitizer {
43
+ constructor({ linked, maxDepth, maxNodes, maxValues }) {
44
+ this.replaced = 0;
45
+ this.truncated = false;
46
+ this.nodes = 0;
47
+ this.values = 0;
48
+ this.insideError = 0;
49
+ // The containers being walked, from the root. At most `maxDepth` + 1 of them, so searching it is
50
+ // cheaper than maintaining a set.
51
+ this.ancestors = [];
52
+ this.linked = linked;
53
+ this.maxDepth = maxDepth !== null && maxDepth !== void 0 ? maxDepth : DEFAULT_MAX_DEPTH;
54
+ this.maxNodes = maxNodes !== null && maxNodes !== void 0 ? maxNodes : DEFAULT_MAX_NODES;
55
+ this.maxValues = maxValues !== null && maxValues !== void 0 ? maxValues : DEFAULT_MAX_VALUES;
56
+ }
57
+ /**
58
+ * Copy-on-write walk: containers are copied only when something below them changed, and the
59
+ * received objects are never mutated (they may be frozen, cached or still in use by the app).
60
+ */
61
+ walk(node, depth) {
62
+ if (node === null || typeof node !== 'object') {
63
+ return node;
64
+ }
65
+ if (this.ancestors.includes(node)) {
66
+ // A cycle. Inside an error it must not lead back to the received error, which has the config
67
+ // and the request. In a body, serializing it fails regardless of this walk.
68
+ return this.insideError > 0 ? CIRCULAR : node;
69
+ }
70
+ if (depth > this.maxDepth || this.nodes >= this.maxNodes) {
71
+ // Not inspected. Inside an error it must not be returned (it may hold the config). Outside
72
+ // one it is left as it is: withholding the whole body would break the large responses.
73
+ this.truncated = true;
74
+ return this.insideError > 0 ? TRUNCATED : node;
75
+ }
76
+ this.nodes++;
77
+ // Arrays and plain objects are by far the most common, and cannot be errors, binary data or
78
+ // streams: they are checked first.
79
+ if (Array.isArray(node)) {
80
+ return this.walkArray(node, depth);
81
+ }
82
+ if (isPlainObject(node)) {
83
+ return this.walkObject(node, depth);
84
+ }
85
+ return this.walkOther(node, depth);
86
+ }
87
+ // Charged per container, when it is entered, so the loops that visit the values have no counter.
88
+ spend(values) {
89
+ this.values += values;
90
+ if (this.values > this.maxValues) {
91
+ this.truncated = true;
92
+ return false;
93
+ }
94
+ return true;
95
+ }
96
+ walkArray(node, depth) {
97
+ if (!this.spend(node.length)) {
98
+ return this.insideError > 0 ? TRUNCATED : node;
99
+ }
100
+ this.ancestors.push(node);
101
+ let result = node;
102
+ for (let i = 0; i < node.length; i++) {
103
+ const value = node[i];
104
+ const safe = this.walkValue(value, depth);
105
+ if (safe !== value) {
106
+ result = result === node ? node.slice() : result;
107
+ result[i] = safe;
108
+ }
109
+ }
110
+ this.ancestors.pop();
111
+ return result;
112
+ }
113
+ walkObject(node, depth) {
114
+ const keys = Object.keys(node);
115
+ if (!this.spend(keys.length)) {
116
+ return this.insideError > 0 ? TRUNCATED : node;
117
+ }
118
+ this.ancestors.push(node);
119
+ let result = node;
120
+ for (const key of keys) {
121
+ const value = node[key];
122
+ const safe = this.walkValue(value, depth);
123
+ if (safe !== value) {
124
+ result = result === node ? { ...node } : result;
125
+ result[key] = safe;
126
+ }
127
+ }
128
+ this.ancestors.pop();
129
+ return result;
130
+ }
131
+ // Most values are primitives: skipping them here avoids a call per value.
132
+ walkValue(value, depth) {
133
+ return value === null || typeof value !== 'object' ? value : this.walk(value, depth + 1);
134
+ }
135
+ // Errors, and what is neither a plain object nor an array: buffers, streams, class instances.
136
+ walkOther(node, depth) {
137
+ if (isError(node)) {
138
+ this.replaced++;
139
+ return this.toSafeError(node, depth);
140
+ }
141
+ return this.insideError > 0 ? describeOpaque(node) : node;
142
+ }
143
+ /**
144
+ * Builds the object that is exposed instead of the error. It is made from a shallow copy that
145
+ * leaves out the HTTP-client internals, so those are never traversed nor serialized, and goes
146
+ * through `cleanError` to keep the output consistent with the one of the global error handler
147
+ * (circular references, private and function properties, long strings).
148
+ *
149
+ * What reaches `cleanError` is bounded: values beyond the limits, binary data and class
150
+ * instances were replaced by markers while walking, because `cleanError` has no limits of its own.
151
+ */
152
+ toSafeError(error, depth) {
153
+ this.ancestors.push(error);
154
+ this.insideError++;
155
+ const source = error;
156
+ const copy = this.copyErrorProperties(source, depth);
157
+ const { response } = source;
158
+ if (response !== null && typeof response === 'object') {
159
+ copy.response = this.copyResponse(response, depth);
160
+ }
161
+ const safe = (0, error_1.cleanError)(copy);
162
+ // `cleanError` picks a fixed set of response properties, which leaves `statusText` out.
163
+ if (safe.response && typeof (response === null || response === void 0 ? void 0 : response.statusText) === 'string') {
164
+ safe.response.statusText = response.statusText;
165
+ }
166
+ this.insideError--;
167
+ this.ancestors.pop();
168
+ return safe;
169
+ }
170
+ copyErrorProperties(source, depth) {
171
+ const copy = {};
172
+ for (const key of Object.keys(source)) {
173
+ if (!INTERNAL_ERROR_KEYS.has(key)) {
174
+ copy[key] = this.walk(source[key], depth + 1);
175
+ }
176
+ }
177
+ for (const property of ['name', 'message', 'code']) {
178
+ if (typeof source[property] === 'string') {
179
+ copy[property] = source[property];
180
+ }
181
+ }
182
+ if (this.linked && typeof source.stack === 'string') {
183
+ copy.stack = source.stack;
184
+ }
185
+ return copy;
186
+ }
187
+ // Only what the upstream answered. Its headers (cookies, tokens) and the request that originated
188
+ // it are not exposed.
189
+ copyResponse(response, depth) {
190
+ return {
191
+ data: this.walk(response.data, depth + 1),
192
+ status: response.status,
193
+ };
194
+ }
195
+ }
196
+ /**
197
+ * Finds `Error` instances (including `AxiosError`) in a response body, at any depth of plain
198
+ * objects and arrays, and replaces each one by a safe representation: the message, name, code and
199
+ * status are kept, while the request config, the Node request, the agents, the upstream headers
200
+ * and, unless linked, the stack are removed.
201
+ *
202
+ * Strings, buffers, streams and class instances other than errors are returned untouched.
203
+ */
204
+ const sanitizeErrors = (value, options) => {
205
+ const sanitizer = new ErrorSanitizer(options);
206
+ const sanitized = sanitizer.walk(value, 0);
207
+ return {
208
+ replaced: sanitizer.replaced,
209
+ truncated: sanitizer.truncated,
210
+ value: sanitized,
211
+ };
212
+ };
213
+ exports.sanitizeErrors = sanitizeErrors;
214
+ const isPrimitiveValue = (value) => value === null || typeof value !== 'object';
215
+ const enter = (budget, values) => {
216
+ if (budget.nodes >= budget.maxNodes) {
217
+ return false;
218
+ }
219
+ budget.nodes++;
220
+ budget.values += values;
221
+ return budget.values <= budget.maxValues;
222
+ };
223
+ // How far the linear pass may nest, in arrays and in objects alike. Deeper bodies fall through to
224
+ // the scan below, which also enforces the depth limit.
225
+ const SHALLOW_DEPTH = 4;
226
+ // Lists of plain records, and an object wrapping them, are the common large payload. One pass
227
+ // proves they hold no Error, which is cheaper than the copying walk and stops at the same budget.
228
+ const shallowValue = (value, budget, depth) => {
229
+ // Arrays and plain objects first: they are the common case and cannot be errors.
230
+ if (Array.isArray(value)) {
231
+ return shallowArray(value, budget, depth);
232
+ }
233
+ if (isPlainObject(value)) {
234
+ return shallowRecord(value, budget, depth);
235
+ }
236
+ if (isError(value)) {
237
+ return 'has-error';
238
+ }
239
+ return enter(budget, 0) ? 'clean' : 'truncated';
240
+ };
241
+ // What is below a container that is not a primitive. Recursion stops at the cutoff, whatever the
242
+ // container is, so its depth is bounded.
243
+ const shallowChild = (child, budget, depth) => depth >= SHALLOW_DEPTH ? undefined : shallowValue(child, budget, depth + 1);
244
+ const shallowArray = (items, budget, depth) => {
245
+ if (!enter(budget, items.length)) {
246
+ return 'truncated';
247
+ }
248
+ for (const item of items) {
249
+ if (isPrimitiveValue(item)) {
250
+ continue;
251
+ }
252
+ const found = shallowChild(item, budget, depth);
253
+ if (found !== 'clean') {
254
+ return found;
255
+ }
256
+ }
257
+ return 'clean';
258
+ };
259
+ const shallowRecord = (record, budget, depth) => {
260
+ const keys = Object.keys(record);
261
+ if (!enter(budget, keys.length)) {
262
+ return 'truncated';
263
+ }
264
+ for (const key of keys) {
265
+ const value = record[key];
266
+ if (isPrimitiveValue(value)) {
267
+ continue;
268
+ }
269
+ const found = shallowChild(value, budget, depth);
270
+ if (found !== 'clean') {
271
+ return found;
272
+ }
273
+ }
274
+ return 'clean';
275
+ };
276
+ const scanChildren = (node, depth, state) => {
277
+ const isArray = Array.isArray(node);
278
+ const record = node;
279
+ const keys = isArray ? undefined : Object.keys(record);
280
+ const length = keys ? keys.length : node.length;
281
+ state.values += length;
282
+ if (state.values > state.maxValues) {
283
+ state.truncated = true;
284
+ return;
285
+ }
286
+ state.ancestors.push(node);
287
+ for (let i = 0; i < length && !state.found && !state.truncated; i++) {
288
+ const child = keys ? record[keys[i]] : node[i];
289
+ if (child !== null && typeof child === 'object') {
290
+ scanNode(child, depth + 1, state);
291
+ }
292
+ }
293
+ state.ancestors.pop();
294
+ };
295
+ const scanNode = (node, depth, state) => {
296
+ if (state.found || state.truncated || state.ancestors.includes(node)) {
297
+ return;
298
+ }
299
+ if (depth > state.maxDepth || state.nodes >= state.maxNodes) {
300
+ state.truncated = true;
301
+ return;
302
+ }
303
+ state.nodes++;
304
+ if (Array.isArray(node) || isPlainObject(node)) {
305
+ scanChildren(node, depth, state);
306
+ }
307
+ else if (isError(node)) {
308
+ state.found = true;
309
+ }
310
+ };
311
+ const deepScan = (value, maxDepth, maxNodes, maxValues) => {
312
+ const state = { ancestors: [], found: false, maxDepth, maxNodes, maxValues, nodes: 0, truncated: false, values: 0 };
313
+ scanNode(value, 0, state);
314
+ if (state.truncated) {
315
+ return 'truncated';
316
+ }
317
+ return state.found ? 'has-error' : 'clean';
318
+ };
319
+ /**
320
+ * Says whether `value` contains an `Error`, without copying it. The copying walk is only needed
321
+ * when this returns `has-error`. `truncated` means a budget (depth, containers, or the values they
322
+ * hold) was spent before the value was fully inspected, so whatever was not visited must not be sent.
323
+ */
324
+ const scanForErrors = (value, options = {}) => {
325
+ var _a, _b, _c;
326
+ const maxNodes = (_a = options.maxNodes) !== null && _a !== void 0 ? _a : DEFAULT_MAX_NODES;
327
+ const maxValues = (_b = options.maxValues) !== null && _b !== void 0 ? _b : DEFAULT_MAX_VALUES;
328
+ const shallow = shallowValue(value, { maxNodes, maxValues, nodes: 0, values: 0 }, 0);
329
+ if (shallow !== undefined) {
330
+ return shallow;
331
+ }
332
+ return deepScan(value, (_c = options.maxDepth) !== null && _c !== void 0 ? _c : DEFAULT_MAX_DEPTH, maxNodes, maxValues);
333
+ };
334
+ exports.scanForErrors = scanForErrors;
335
+ /**
336
+ * The same as `sanitizeErrors` for what is written to the process output, where the stack is
337
+ * useful and kept. A thrown `Error` stays an error, so it is printed as one (stack, then its
338
+ * properties), without the request config, the request and the agents.
339
+ *
340
+ * It never throws.
341
+ */
342
+ const toLoggableError = (value) => {
343
+ try {
344
+ const { value: safe } = (0, exports.sanitizeErrors)(value, { linked: true });
345
+ if (safe !== value && value !== null && typeof value === 'object' && isError(value)) {
346
+ return Object.assign(Object.create(Object.getPrototypeOf(value)), safe);
347
+ }
348
+ return safe;
349
+ }
350
+ catch (err) {
351
+ return `[error omitted: it could not be sanitized (${err === null || err === void 0 ? void 0 : err.name})]`;
352
+ }
353
+ };
354
+ exports.toLoggableError = toLoggableError;
@@ -0,0 +1,19 @@
1
+ export declare const REDACTED = "[Redacted]";
2
+ export interface RedactResult {
3
+ value: string;
4
+ /**
5
+ * How many occurrences were replaced. Zero means `value` is the received string.
6
+ */
7
+ redacted: number;
8
+ }
9
+ /**
10
+ * Replaces credentials in a text: the given secrets wherever they appear, the value of the
11
+ * credential headers in JSON text and in header lines, `Cookie` and `Set-Cookie` lines, the VTEX
12
+ * ID cookie, and `Bearer` tokens.
13
+ * The rest of the text is kept.
14
+ *
15
+ * It is for text that was built from an error, where the error object is no longer there to
16
+ * detect. It does not inspect anything else: a credential that is neither one of the secrets nor
17
+ * under one of those names is not found.
18
+ */
19
+ export declare const redactSecrets: (text: string, secrets?: Array<string | undefined>) => RedactResult;
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.redactSecrets = exports.REDACTED = void 0;
4
+ exports.REDACTED = '[Redacted]';
5
+ // Shorter values are not credentials, and replacing them everywhere would damage ordinary text.
6
+ const MIN_SECRET_LENGTH = 16;
7
+ /**
8
+ * Names under which credentials travel in HTTP headers. When an error is turned into text with
9
+ * `JSON.stringify`, its request config, and the headers in it, end up in the text under these keys.
10
+ */
11
+ const SENSITIVE_KEYS = [
12
+ 'authorization',
13
+ 'cookie',
14
+ 'proxy-authorization',
15
+ 'vtexidclientautcookie',
16
+ 'x-vtex-api-appkey',
17
+ 'x-vtex-api-apptoken',
18
+ 'x-vtex-credential',
19
+ 'x-vtex-session',
20
+ ];
21
+ // `"key":"value"`, also when the text was serialized twice (`\"key\":\"value\"`). Captures
22
+ // everything up to the value, which is what is replaced. The value is a run of characters that are
23
+ // not a quote, where a backslash only escapes the character after it. The two alternatives of the
24
+ // run exclude each other, so the match is linear.
25
+ const KEYED_VALUE = new RegExp(String.raw `(\\{0,2}"(?:${SENSITIVE_KEYS.join('|')})\\{0,2}"\s*:\s*\\{0,2}")(?:[^"\\]|\\[^"])+`, 'gi');
26
+ // A header written as a line of the request (`Proxy-Authorization: value`), which is how a
27
+ // serialized request object (`_header`) holds it. Short values are not credentials. `Cookie` is
28
+ // not in this list: the word opens ordinary sentences, so its lines are matched separately.
29
+ const HEADER_LINE_NAMES = [
30
+ 'authorization',
31
+ 'proxy-authorization',
32
+ 'vtexidclientautcookie',
33
+ 'x-vtex-api-appkey',
34
+ 'x-vtex-api-apptoken',
35
+ 'x-vtex-credential',
36
+ 'x-vtex-session',
37
+ ];
38
+ // The name starts a line. In a serialized request the line break is the two characters `\n`, so a
39
+ // word boundary never fires there: the `n` and the name are both word characters.
40
+ const AT_LINE_START = String.raw `(?<=^|\\n|\\r|\n|\r)`;
41
+ const HEADER_LINE = new RegExp(String.raw `${AT_LINE_START}((?:${HEADER_LINE_NAMES.join('|')})[ \t]*:[ \t]*)[^\s"\\][^\r\n"\\]{7,}`, 'gi');
42
+ // A `Cookie` or `Set-Cookie` line. The value has to be cookie pairs (`name=value`), so a sentence
43
+ // that happens to start with "Cookie:" is left as it is.
44
+ const COOKIE_LINE = new RegExp(String.raw `${AT_LINE_START}((?:set-)?cookie[ \t]*:[ \t]*)[^\s=;"\\]+=[^\r\n"\\]{6,}`, 'gi');
45
+ // The VTEX ID cookie, in a `Cookie` header or a `Set-Cookie` line.
46
+ const VTEX_ID_COOKIE = /\b(VtexIdclientAutCookie(?:_[a-z0-9-]+)?=)[^;\s"\\]+/gi;
47
+ // An `Authorization` value that is not under a key, as in a message or a copied header line.
48
+ const BEARER_TOKEN = /\b(Bearer\s+)[a-z0-9._~+/=-]{16,}/gi;
49
+ const redactKnown = (text, secrets) => {
50
+ let value = text;
51
+ let redacted = 0;
52
+ for (const secret of secrets) {
53
+ if (!secret || secret.length < MIN_SECRET_LENGTH || !value.includes(secret)) {
54
+ continue;
55
+ }
56
+ const parts = value.split(secret);
57
+ redacted += parts.length - 1;
58
+ value = parts.join(exports.REDACTED);
59
+ }
60
+ return { redacted, value };
61
+ };
62
+ const redactPattern = (text, pattern) => {
63
+ let redacted = 0;
64
+ const value = text.replace(pattern, (match, prefix) => {
65
+ if (match === `${prefix}${exports.REDACTED}`) {
66
+ return match;
67
+ }
68
+ redacted++;
69
+ return `${prefix}${exports.REDACTED}`;
70
+ });
71
+ return { redacted, value: redacted > 0 ? value : text };
72
+ };
73
+ /**
74
+ * Replaces credentials in a text: the given secrets wherever they appear, the value of the
75
+ * credential headers in JSON text and in header lines, `Cookie` and `Set-Cookie` lines, the VTEX
76
+ * ID cookie, and `Bearer` tokens.
77
+ * The rest of the text is kept.
78
+ *
79
+ * It is for text that was built from an error, where the error object is no longer there to
80
+ * detect. It does not inspect anything else: a credential that is neither one of the secrets nor
81
+ * under one of those names is not found.
82
+ */
83
+ const redactSecrets = (text, secrets = []) => {
84
+ const known = redactKnown(text, secrets);
85
+ let value = known.value;
86
+ let redacted = known.redacted;
87
+ for (const pattern of [KEYED_VALUE, HEADER_LINE, COOKIE_LINE, VTEX_ID_COOKIE, BEARER_TOKEN]) {
88
+ const result = redactPattern(value, pattern);
89
+ value = result.value;
90
+ redacted += result.redacted;
91
+ }
92
+ return { redacted, value: redacted > 0 ? value : text };
93
+ };
94
+ exports.redactSecrets = redactSecrets;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vtex/api",
3
- "version": "6.52.1",
3
+ "version": "6.52.5",
4
4
  "description": "VTEX I/O API client",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",