@synopackageland/cli 0.54.9 → 0.54.10

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
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.54.10 (2026-11-21)
4
+
5
+ - `check <spk>` and `inspect` scan public `ui/` files over 64 MiB instead of rejecting them with `SPK_UI_UNSAFE`: a larger file (such as a Go CGI) is streamed in 16 MiB windows with 1 MiB overlap, so a match longer than 1 MiB is still flagged but reported by its first 1 MiB. Executables keep their binary-pattern scanning.
6
+
3
7
  ## 0.54.9 (2026-11-18)
4
8
 
5
9
  - Use SDK 0.53.51 by default.
@@ -3,10 +3,12 @@ import { posix } from "node:path";
3
3
  import * as tar from "tar";
4
4
  import { ARCH_ELF_MACHINE } from "./dsm-spec.js";
5
5
  import { archiveInventory, formatArchiveIssue } from './archive-inventory.js';
6
- import { applyUiIgnores, logicalUiFile, publicUiReport, resolveUiSecretIgnores, staleUiIgnores, uiFileProblems } from "../standalone/public-ui.js";
6
+ import { applyUiIgnores, logicalUiFile, publicUiReport, resolveUiSecretIgnores, staleUiIgnores, uiFileProblems, UiStreamScanner } from "../standalone/public-ui.js";
7
7
  const XZ_MAGIC = Buffer.from([0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00]);
8
8
  const ELF_MAGIC = Buffer.from("\x7fELF", "latin1");
9
9
  const MAX_TEXT = 1024 * 1024;
10
+ /** ui/ files up to this size are scanned whole; larger ones (e.g. a Go CGI) are streamed in windows. */
11
+ const MAX_UI_IN_MEMORY = 64 * MAX_TEXT;
10
12
  const HEAD_BYTES = 20;
11
13
  /** `section:key`: a DSM string reference rather than literal text. */
12
14
  const STRING_REF = /^([A-Za-z0-9_]+):([A-Za-z0-9_]+)$/;
@@ -40,16 +42,24 @@ async function scanPayload(payloadPath, wanted, findings) {
40
42
  if (entry.type !== "File")
41
43
  return;
42
44
  const file = { path, head: Buffer.alloc(0) };
43
- const keep = wanted(path) && (entry.size ?? 0) <= (path.startsWith("ui/") ? 64 * MAX_TEXT : MAX_TEXT);
45
+ const ui = path.startsWith("ui/");
46
+ const keep = wanted(path) && (entry.size ?? 0) <= (ui ? MAX_UI_IN_MEMORY : MAX_TEXT);
47
+ // Only the payload's own ui/ files need a secret scan at any size.
48
+ const stream = !keep && ui && wanted(path) ? new UiStreamScanner(path.slice(3)) : undefined;
44
49
  const chunks = [];
45
50
  entry.on("data", (chunk) => {
46
51
  if (file.head.length < HEAD_BYTES)
47
52
  file.head = Buffer.concat([file.head, chunk]).subarray(0, HEAD_BYTES);
48
53
  if (keep)
49
54
  chunks.push(chunk);
55
+ stream?.write(chunk);
56
+ });
57
+ entry.on("end", () => {
58
+ if (keep)
59
+ file.content = Buffer.concat(chunks);
60
+ if (stream)
61
+ file.problems = stream.end();
50
62
  });
51
- entry.on("end", () => { if (keep)
52
- file.content = Buffer.concat(chunks); });
53
63
  files.push(file);
54
64
  },
55
65
  });
@@ -174,11 +184,12 @@ export async function checkPayload(payloadPath, info, findings, policy = {}) {
174
184
  }
175
185
  const scanned = [];
176
186
  for (const file of ui) {
177
- if (file.content === undefined) {
178
- findings.add("SPK_UI_UNSAFE", "Public UI file exceeds the 64 MiB scan limit.", at(file.path));
187
+ const raw = file.content !== undefined ? uiFileProblems(file.path.slice(3), file.content) : file.problems;
188
+ if (raw === undefined) {
189
+ // Not reached for a fully read tar; fail closed if a ui/ file was never scanned.
190
+ findings.add("SPK_UI_UNSAFE", "Public UI file could not be scanned.", at(file.path));
179
191
  continue;
180
192
  }
181
- const raw = uiFileProblems(file.path.slice(3), file.content);
182
193
  scanned.push(...raw);
183
194
  // Inspection may run on SPKs from others: report patterns, never the flagged text.
184
195
  const problems = applyUiIgnores(raw, ignores, policy.moduleVersion);
@@ -190,5 +201,8 @@ export async function checkPayload(payloadPath, info, findings, policy = {}) {
190
201
  }
191
202
  for (const stale of staleUiIgnores(scanned, ignores, policy.moduleVersion))
192
203
  findings.add("SPK_UI_UNSAFE", "A recorded ui_secret_ignores match no longer occurs in this file; rebuild the SPK.", at(`ui/${stale.file}`));
193
- return publicUiReport(ui.filter(file => file.content !== undefined).map(file => ({ file: file.path, content: file.content.toString("utf8") })));
204
+ const report = publicUiReport(ui.filter(file => file.content !== undefined).map(file => ({ file: file.path, content: file.content.toString("utf8") })));
205
+ // Streamed files are not minify-checked, but a source map in one is still reported.
206
+ const streamedMaps = ui.filter(file => file.problems?.some(problem => ["source-map-file", "sourceMappingURL"].includes(problem.pattern))).map(file => file.path);
207
+ return streamedMaps.length ? { ...report, sourceMapFree: false, sourceMaps: [...report.sourceMaps, ...streamedMaps] } : report;
194
208
  }
@@ -43,6 +43,24 @@ export declare function uiFiles(root: string): string[];
43
43
  /** Run before computing content addresses or mobile asset inventories. */
44
44
  export declare function minifyUiTree(root: string, skip?: string[]): void;
45
45
  export declare function uiFileProblems(file: string, input: string | Buffer): UiProblem[];
46
+ /** Bytes of each streamed window that own match starts; the rest is context. */
47
+ export declare const UI_STREAM_CHUNK: number;
48
+ /** Trailing context: a match that starts in a chunk is seen whole if it is at most this long. */
49
+ export declare const UI_STREAM_OVERLAP: number;
50
+ /**
51
+ * uiFileProblems for a file too large to hold in memory, fed in arbitrary pieces.
52
+ * The file is cut into chunks; each is scanned with UI_STREAM_LEAD bytes before it and
53
+ * UI_STREAM_OVERLAP bytes after it, and a window keeps only the matches that start
54
+ * inside its own chunk, so every match is counted once and seen whole unless it is
55
+ * longer than the overlap (then it is still flagged, by its first UI_STREAM_OVERLAP bytes).
56
+ * Cuts never split a UTF-8 sequence, so text decodes as it would in one piece.
57
+ */
58
+ export declare class UiStreamScanner {
59
+ #private;
60
+ constructor(file: string, chunk?: number, overlap?: number);
61
+ write(piece: Buffer): void;
62
+ end(): UiProblem[];
63
+ }
46
64
  export declare function logicalUiFile(file: string, moduleVersion?: string): string;
47
65
  /**
48
66
  * `ignores` must already be resolved to exact logical files (see resolveUiSecretIgnores).
@@ -156,26 +156,47 @@ const PASSWORD_CAPTIONS = new Set([
156
156
  "passwort", "benutzerpasswort", "mot de passe", "mot de passe utilisateur",
157
157
  "contraseña", "contraseña de usuario", "password utente",
158
158
  ]);
159
- function credentialLiterals(content, bundle) {
159
+ /** The raw regex hits behind credentialLiterals, with their positions; render with `literal`. */
160
+ function credentialLiteralMatches(content, bundle, from = 0, to = content.length) {
160
161
  const found = [];
161
162
  const literals = /(?:^|[,{])\s*(?:"(password|secret)"|'(password|secret)'|(password|secret))\s*:\s*(?:"([^"\r\n]+)"|'([^'\r\n]+)'|`([^`$\r\n]+)`|(\d+(?:\.\d+)?)\b)/gi;
162
- for (const match of content.matchAll(literals)) {
163
+ for (const match of matchesIn(literals, content, from, to)) {
163
164
  const key = match[1] ?? match[2] ?? match[3];
164
165
  const value = match[4] ?? match[5] ?? match[6] ?? match[7];
165
166
  const caption = value.replace(/\\(?:u([0-9a-f]{4})|x([0-9a-f]{2}))/gi, (_escape, unicode, hex) => String.fromCharCode(parseInt(unicode ?? hex, 16))).toLowerCase();
166
167
  if (key.toLowerCase() === "password" && PASSWORD_CAPTIONS.has(caption))
167
168
  continue;
168
- found.push(match[0].replace(/^[,{]?\s*/, ""));
169
+ found.push(match);
169
170
  }
170
171
  const assignments = /(?<![\w$"'`])(?:password|secret)\s*=\s*(?:"[^"\r\n]+"|'[^'\r\n]+'|`[^`$\r\n]+`|\d+(?:\.\d+)?\b)|["'`](?:password|secret)=[^"'`\s]+/gi;
171
- for (const match of content.matchAll(assignments))
172
- found.push(match[0]);
172
+ for (const match of matchesIn(assignments, content, from, to))
173
+ found.push(match);
173
174
  // Unquoted values in environment/config/HTML text; JS variable assignments are not literals.
174
175
  if (!bundle)
175
- for (const match of content.matchAll(/(?<![\w$])(?:password|secret)\s*=\s*[^\s"'`<>{};,]+/gi))
176
- found.push(match[0]);
176
+ for (const match of matchesIn(/(?<![\w$])(?:password|secret)\s*=\s*[^\s"'`<>{};,]+/gi, content, from, to))
177
+ found.push(match);
178
+ return found;
179
+ }
180
+ /**
181
+ * Global matches that start in [from, to). Searching begins at `from`, so lookbehinds
182
+ * still see the text before it while `^` and earlier matches cannot reach into it.
183
+ */
184
+ function matchesIn(pattern, content, from = 0, to = content.length) {
185
+ const re = new RegExp(pattern.source, pattern.flags.includes("g") ? pattern.flags : `${pattern.flags}g`);
186
+ const found = [];
187
+ re.lastIndex = from;
188
+ for (let match = re.exec(content); match && match.index < to; match = re.exec(content)) {
189
+ found.push(match);
190
+ if (match[0] === "")
191
+ re.lastIndex++;
192
+ }
177
193
  return found;
178
194
  }
195
+ /** Only the object-literal form starts with its delimiter; reports omit it. */
196
+ const literal = (match) => match[0].replace(/^[,{]?\s*/, "");
197
+ function credentialLiterals(content, bundle) {
198
+ return credentialLiteralMatches(content, bundle).map(literal);
199
+ }
179
200
  const unique = (items) => [...new Set(items)];
180
201
  const allMatches = (pattern, content) => unique([...content.matchAll(new RegExp(pattern.source, pattern.flags.includes("g") ? pattern.flags : `${pattern.flags}g`))].map(match => match[0]));
181
202
  export function uiFileProblems(file, input) {
@@ -200,6 +221,100 @@ export function uiFileProblems(file, input) {
200
221
  found.push({ file, pattern: "credential-literal", matches: literals });
201
222
  return found;
202
223
  }
224
+ /** Bytes of each streamed window that own match starts; the rest is context. */
225
+ export const UI_STREAM_CHUNK = 16 * 1024 * 1024;
226
+ /** Trailing context: a match that starts in a chunk is seen whole if it is at most this long. */
227
+ export const UI_STREAM_OVERLAP = 1024 * 1024;
228
+ /** Leading context, so lookbehinds and `^` at a chunk start see the bytes before it. */
229
+ const UI_STREAM_LEAD = 4096;
230
+ /**
231
+ * uiFileProblems for a file too large to hold in memory, fed in arbitrary pieces.
232
+ * The file is cut into chunks; each is scanned with UI_STREAM_LEAD bytes before it and
233
+ * UI_STREAM_OVERLAP bytes after it, and a window keeps only the matches that start
234
+ * inside its own chunk, so every match is counted once and seen whole unless it is
235
+ * longer than the overlap (then it is still flagged, by its first UI_STREAM_OVERLAP bytes).
236
+ * Cuts never split a UTF-8 sequence, so text decodes as it would in one piece.
237
+ */
238
+ export class UiStreamScanner {
239
+ #file;
240
+ #chunk;
241
+ #overlap;
242
+ #pending = [];
243
+ #size = 0;
244
+ #lead = Buffer.alloc(0);
245
+ #executable;
246
+ #head = Buffer.alloc(0);
247
+ #found = new Map();
248
+ constructor(file, chunk = UI_STREAM_CHUNK, overlap = UI_STREAM_OVERLAP) {
249
+ if (!(chunk > 0 && overlap > 0))
250
+ throw new Error("UiStreamScanner needs a positive chunk and overlap");
251
+ this.#file = file;
252
+ this.#chunk = chunk;
253
+ this.#overlap = overlap;
254
+ }
255
+ write(piece) {
256
+ if (this.#executable === undefined) {
257
+ this.#head = Buffer.concat([this.#head, piece]);
258
+ // PE needs its header offset at 60 and the header behind it; 4 KiB covers real files.
259
+ if (this.#head.length < 4096) {
260
+ this.#pending.push(piece);
261
+ this.#size += piece.length;
262
+ return;
263
+ }
264
+ this.#executable = isExecutable(this.#head);
265
+ }
266
+ this.#pending.push(piece);
267
+ this.#size += piece.length;
268
+ while (this.#size >= this.#chunk + this.#overlap)
269
+ this.#scan(false);
270
+ }
271
+ end() {
272
+ if (this.#executable === undefined)
273
+ this.#executable = isExecutable(this.#head);
274
+ this.#scan(true);
275
+ return [...this.#found].map(([pattern, matches]) => ({ file: this.#file, pattern, ...(matches ? { matches: [...matches] } : {}) }));
276
+ }
277
+ #scan(last) {
278
+ const data = Buffer.concat(this.#pending);
279
+ // Own [0, cut); a continuation byte (10xxxxxx) never starts a cut.
280
+ let cut = last ? data.length : this.#chunk;
281
+ if (!this.#executable)
282
+ while (cut < data.length && (data[cut] & 0xc0) === 0x80)
283
+ cut++;
284
+ const decode = (bytes) => bytes.toString(this.#executable ? "latin1" : "utf8");
285
+ const lead = decode(this.#lead), owned = decode(data.subarray(0, cut));
286
+ const text = lead + owned + decode(data.subarray(cut));
287
+ const start = lead.length, end = start + owned.length;
288
+ const add = (pattern, matches) => {
289
+ if (!this.#found.has(pattern))
290
+ this.#found.set(pattern, matches ? new Set() : undefined);
291
+ for (const match of matches ?? [])
292
+ this.#found.get(pattern).add(match);
293
+ };
294
+ const name = basename(this.#file).toLowerCase();
295
+ if (name === ".env" || name.startsWith(".env."))
296
+ add("environment-file");
297
+ if (this.#file.toLowerCase().endsWith(".map"))
298
+ add("source-map-file");
299
+ if (matchesIn(SOURCE_MAP_COMMENT, text, start, end).length)
300
+ add("sourceMappingURL");
301
+ for (const [pattern, match] of this.#executable ? BINARY_SECRET_PATTERNS : SECRET_PATTERNS) {
302
+ const hits = matchesIn(match, text, start, end).map(hit => hit[0]);
303
+ if (hits.length)
304
+ add(pattern, hits);
305
+ }
306
+ if (!this.#executable) {
307
+ const hits = credentialLiteralMatches(text, isUiBundle(this.#file), start, end).map(literal);
308
+ if (hits.length)
309
+ add("credential-literal", hits);
310
+ }
311
+ const before = Buffer.concat([this.#lead, data.subarray(0, cut)]);
312
+ this.#lead = Buffer.from(before.subarray(Math.max(0, before.length - UI_STREAM_LEAD)));
313
+ const rest = data.subarray(cut);
314
+ this.#pending = rest.length ? [Buffer.from(rest)] : [];
315
+ this.#size = rest.length;
316
+ }
317
+ }
203
318
  export function logicalUiFile(file, moduleVersion) {
204
319
  const prefix = moduleVersion ? `app/${moduleVersion}/` : "";
205
320
  return prefix && file.startsWith(prefix) ? `app/${file.slice(prefix.length)}` : file;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@synopackageland/cli",
3
- "version": "0.54.9",
3
+ "version": "0.54.10",
4
4
  "description": "Syno Package Land Developer Kit CLI",
5
5
  "type": "module",
6
6
  "bin": {