@percy/core 1.32.4 → 1.32.5-beta.1

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/dist/utils.js CHANGED
@@ -54,6 +54,104 @@ export function isHttpOrHttpsUrl(urlString) {
54
54
  }
55
55
  }
56
56
 
57
+ // Cloud instance-metadata endpoints. These are never legitimate snapshot
58
+ // targets, but they hand out short-lived cloud credentials to anything that
59
+ // can reach them — so a page (or a subresource it requests) that navigates
60
+ // Chromium to one of these can exfiltrate credentials via SSRF. We block
61
+ // these specific targets only; loopback and general RFC1918 hosts stay
62
+ // allowed so that snapshotting http://localhost:3000 and internal staging
63
+ // hosts keeps working.
64
+ const METADATA_IPS = new Set(['169.254.169.254',
65
+ // AWS/Azure/GCP IMDS
66
+ '169.254.170.2',
67
+ // AWS ECS task metadata
68
+ '100.100.100.200',
69
+ // Alibaba Cloud
70
+ 'fd00:ec2::254' // AWS IMDS over IPv6
71
+ ].map(canonicalHost));
72
+ const METADATA_HOSTNAMES = new Set(['metadata.google.internal', 'metadata.goog']);
73
+
74
+ // Canonicalizes a host for comparison: lowercases, strips a trailing dot and
75
+ // IPv6 brackets, normalizes IPv6 addresses to their compressed form (so e.g.
76
+ // fd00:ec2:0:0:0:0:0:254 and fd00:ec2::254 compare equal), and unwraps
77
+ // IPv4-mapped IPv6 addresses to their dotted-quad form (so e.g.
78
+ // ::ffff:169.254.169.254 compares equal to the literal 169.254.169.254 — the
79
+ // OS routes it to the IPv4 service, so it must not slip past the IP set).
80
+ function canonicalHost(host) {
81
+ /* istanbul ignore next -- @preserve: defensive guard; the sole caller
82
+ matchMetadataHost already returns on a falsy host before calling this,
83
+ so this branch is unreachable via the public API and cannot be exercised
84
+ without widening the export surface. */
85
+ if (!host) return host;
86
+ let h = String(host).toLowerCase().replace(/\.$/, '');
87
+ let bare = h.replace(/^\[/, '').replace(/\]$/, '');
88
+ if (bare.includes(':')) {
89
+ try {
90
+ let canon = new URL(`http://[${bare}]/`).hostname.replace(/^\[/, '').replace(/\]$/, '');
91
+ // The URL parser renders IPv4-mapped IPv6 as ::ffff:wwww:xxxx (hex);
92
+ // fold those 32 bits back into dotted-quad so mapped metadata IPs match.
93
+ let mapped = /^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/.exec(canon);
94
+ if (mapped) {
95
+ let hi = parseInt(mapped[1], 16);
96
+ let lo = parseInt(mapped[2], 16);
97
+ return `${hi >> 8}.${hi & 0xff}.${lo >> 8}.${lo & 0xff}`;
98
+ }
99
+ return canon;
100
+ } catch {
101
+ return bare;
102
+ }
103
+ }
104
+ return bare;
105
+ }
106
+
107
+ // The single decision point for the metadata block: returns the given host
108
+ // (hostname or IP literal) when it canonicalizes to a known cloud
109
+ // instance-metadata endpoint, otherwise null. Purely synchronous and DNS-free —
110
+ // every outbound path (the request-time literal pre-check, the response-stage
111
+ // remoteIPAddress gate, and the direct-fetch socket-address gate) funnels
112
+ // through here so the block behaves identically everywhere.
113
+ function matchMetadataHost(host) {
114
+ if (!host) return null;
115
+ let canon = canonicalHost(host);
116
+ return METADATA_IPS.has(canon) || METADATA_HOSTNAMES.has(canon) ? host : null;
117
+ }
118
+
119
+ // Cheap, synchronous first-line pre-check on a URL's literal host: blocks only
120
+ // literal metadata IPs and metadata hostnames. It does NOT resolve DNS and so
121
+ // cannot defend against DNS rebinding on its own — an attacker's hostname can
122
+ // resolve benignly here and then to a metadata IP for the actual connection.
123
+ // The DNS-rebinding leg is closed at the response stage by isMetadataIP, which
124
+ // gates on response.remoteIPAddress — the IP the request actually connected to.
125
+ export function isMetadataTarget(rawUrl) {
126
+ let host;
127
+ try {
128
+ host = new URL(rawUrl).hostname;
129
+ } catch {
130
+ // Unparseable URLs are handled elsewhere; they are not our concern here.
131
+ return null;
132
+ }
133
+ return matchMetadataHost(host);
134
+ }
135
+
136
+ // Response/connection-stage gate: given the IP a request actually connected to
137
+ // (response.remoteIPAddress from CDP, or a direct fetch's socket.remoteAddress),
138
+ // returns the offending IP when it is a metadata target, otherwise null. This
139
+ // is the authoritative enforcement point — it inspects the real connected IP,
140
+ // so a hostname that rebinds to a metadata IP after the request-time pre-check
141
+ // is still blocked here. No DNS needed: the input is already an IP literal.
142
+ export function isMetadataIP(remoteIP) {
143
+ return matchMetadataHost(remoteIP);
144
+ }
145
+
146
+ // Throws when the URL points at a cloud instance-metadata endpoint. Used to
147
+ // refuse navigating the top-level snapshot URL to such a target.
148
+ export function assertNotMetadataTarget(rawUrl) {
149
+ let host = isMetadataTarget(rawUrl);
150
+ if (host) {
151
+ throw new Error(`Refusing to navigate to cloud metadata endpoint: ${host}`);
152
+ }
153
+ }
154
+
57
155
  // Rewrites localhost/127.0.0.1 origins to Percy's internal render host so
58
156
  // serialized resources resolve consistently during rendering. Mirrors the
59
157
  // rewrite applied to serialized DOM resources in @percy/dom (serialize-cssom).
@@ -80,6 +178,11 @@ export function buildSyntheticFrameResourceUrl(rootUrl, percyElementId) {
80
178
  }
81
179
  }
82
180
 
181
+ // Returns a URL encoded string of nested query params
182
+ export function encodeURLSearchParams(subj, prefix) {
183
+ return typeof subj === 'object' ? Object.entries(subj).map(([key, value]) => encodeURLSearchParams(value, prefix ? `${prefix}[${key}]` : key)).join('&') : `${prefix}=${encodeURIComponent(subj)}`;
184
+ }
185
+
83
186
  // Process CORS iframes in a single domSnapshot object. `rootUrl` is the page
84
187
  // URL of the snapshot, used as the base for synthetic resource URLs.
85
188
  export function processCorsIframesInDomSnapshot(domSnapshot, rootUrl) {
@@ -612,21 +715,50 @@ export async function withRetries(fn, {
612
715
  }
613
716
  }
614
717
  }
615
- export function redactSecrets(data) {
616
- const filepath = path.resolve(url.fileURLToPath(import.meta.url), '../secretPatterns.yml');
617
- const secretPatterns = YAML.parse(readFileSync(filepath, 'utf-8'));
618
- if (Array.isArray(data)) {
619
- // Process each item in the array
620
- return data.map(item => redactSecrets(item));
621
- } else if (typeof data === 'object' && data !== null) {
622
- // Process each key-value pair in the object
623
- data.message = redactSecrets(data.message);
718
+
719
+ // Lazily load and compile the secret patterns once. The pattern file holds
720
+ // ~1.7k regexes; parsing the YAML and compiling every RegExp on each call made
721
+ // redactSecrets O(patterns) per string and re-read the file for every recursive
722
+ // call. Since redactSecrets now runs over the full CLI log array on egress
723
+ // (sendBuildLogs), that per-call cost is paid hundreds of times and could blow
724
+ // past test/runtime timeouts. Compile once and reuse.
725
+ let _compiledSecretPatterns;
726
+ function getSecretPatterns() {
727
+ if (!_compiledSecretPatterns) {
728
+ const filepath = path.resolve(url.fileURLToPath(import.meta.url), '../secretPatterns.yml');
729
+ const secretPatterns = YAML.parse(readFileSync(filepath, 'utf-8'));
730
+ // Regex sources come from the first-party, bundled secretPatterns.yml that
731
+ // ships in this package - never from remote or attacker-controlled input.
732
+ // nosemgrep: javascript.lang.security.audit.detect-non-literal-regexp.detect-non-literal-regexp
733
+ _compiledSecretPatterns = secretPatterns.patterns.map(p => new RegExp(p.pattern.regex, 'g'));
624
734
  }
735
+ return _compiledSecretPatterns;
736
+ }
737
+ export function redactSecrets(data) {
738
+ // Strings are redacted against every compiled secret pattern.
625
739
  if (typeof data === 'string') {
626
- for (const pattern of secretPatterns.patterns) {
627
- data = data.replace(new RegExp(pattern.pattern.regex, 'g'), '[REDACTED]');
740
+ for (const pattern of getSecretPatterns()) {
741
+ data = data.replace(pattern, '[REDACTED]');
628
742
  }
743
+ return data;
744
+ }
745
+ // Arrays (the CLI-log entry list) are redacted element-wise, in place.
746
+ if (Array.isArray(data)) {
747
+ for (let i = 0; i < data.length; i++) data[i] = redactSecrets(data[i]);
748
+ return data;
749
+ }
750
+ // Log entries: redact the human-readable `message` in place and return the
751
+ // same reference (sendBuildLogs reads the return value; the CI-log path reads
752
+ // the same memory-mode entry back — both see the redacted message). We do NOT
753
+ // recurse over `meta`: it holds structured instrumentation (e.g. a numeric
754
+ // `size`, ids) and a broad secret pattern matches plain digit strings, so a
755
+ // blanket meta sweep clobbers legitimate diagnostic data. Log-line secrets
756
+ // surface in `message`.
757
+ if (typeof data === 'object' && data !== null) {
758
+ data.message = redactSecrets(data.message);
759
+ return data;
629
760
  }
761
+ // Any other primitive (number, boolean, null, undefined) passes through.
630
762
  return data;
631
763
  }
632
764
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@percy/core",
3
- "version": "1.32.4",
3
+ "version": "1.32.5-beta.1",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -9,7 +9,7 @@
9
9
  },
10
10
  "publishConfig": {
11
11
  "access": "public",
12
- "tag": "latest"
12
+ "tag": "beta"
13
13
  },
14
14
  "engines": {
15
15
  "node": ">=14"
@@ -46,16 +46,16 @@
46
46
  "dependencies": {
47
47
  "@grpc/grpc-js": "^1.14.3",
48
48
  "@grpc/proto-loader": "^0.8.0",
49
- "@percy/client": "1.32.4",
50
- "@percy/config": "1.32.4",
51
- "@percy/dom": "1.32.4",
52
- "@percy/logger": "1.32.4",
53
- "@percy/monitoring": "1.32.4",
54
- "@percy/webdriver-utils": "1.32.4",
49
+ "@percy/client": "1.32.5-beta.1",
50
+ "@percy/config": "1.32.5-beta.1",
51
+ "@percy/dom": "1.32.5-beta.1",
52
+ "@percy/logger": "1.32.5-beta.1",
53
+ "@percy/monitoring": "1.32.5-beta.1",
54
+ "@percy/webdriver-utils": "1.32.5-beta.1",
55
+ "adm-zip": "^0.6.0",
55
56
  "busboy": "^1.6.0",
56
57
  "content-disposition": "^0.5.4",
57
58
  "cross-spawn": "^7.0.3",
58
- "extract-zip": "^2.0.1",
59
59
  "fast-glob": "^3.2.11",
60
60
  "fast-xml-parser": "^4.4.1",
61
61
  "micromatch": "^4.0.8",
@@ -67,7 +67,7 @@
67
67
  "yaml": "^2.4.1"
68
68
  },
69
69
  "optionalDependencies": {
70
- "@percy/cli-doctor": "1.32.4"
70
+ "@percy/cli-doctor": "1.32.5-beta.1"
71
71
  },
72
- "gitHead": "3cc47184f972f991e35b5cc58d3035ecca6cf221"
72
+ "gitHead": "ef91ddef69f7e693bff6cfa3d4136e38450d635a"
73
73
  }