Skip to content

Commit 5296dc3

Browse files
committed
fix(release): bound postpublish dist scans
1 parent a447f9a commit 5296dc3

2 files changed

Lines changed: 102 additions & 24 deletions

File tree

‎scripts/openclaw-npm-postpublish-verify.ts‎

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
existsSync,
66
lstatSync,
77
mkdtempSync,
8+
opendirSync,
89
readdirSync,
910
readFileSync,
1011
realpathSync,
@@ -86,6 +87,10 @@ const OPTIONAL_OR_EXTERNALIZED_RUNTIME_IMPORTS = new Set([
8687
const require = createRequire(import.meta.url);
8788
const acorn = require("acorn") as typeof import("acorn");
8889

90+
type DistJavaScriptFileListResult =
91+
| { files: string[]; limitExceeded: false }
92+
| { files: string[]; limit: number; limitExceeded: true };
93+
8994
export type PublishedInstallScenario = {
9095
name: string;
9196
installSpecs: string[];
@@ -179,11 +184,14 @@ export function normalizeInstalledBinaryVersion(output: string): string {
179184

180185
function listDistJavaScriptFiles(
181186
packageRoot: string,
182-
opts: { skipRelativePath?: (relativePath: string) => boolean } = {},
183-
): string[] {
187+
opts: {
188+
maxFiles?: number;
189+
skipRelativePath?: (relativePath: string) => boolean;
190+
} = {},
191+
): DistJavaScriptFileListResult {
184192
const distDir = join(packageRoot, "dist");
185193
if (!existsSync(distDir)) {
186-
return [];
194+
return { files: [], limitExceeded: false };
187195
}
188196

189197
const pending = [distDir];
@@ -193,28 +201,56 @@ function listDistJavaScriptFiles(
193201
if (!currentDir) {
194202
continue;
195203
}
196-
for (const entry of readdirSync(currentDir, { withFileTypes: true })) {
197-
const entryPath = join(currentDir, entry.name);
198-
const relativePath = relative(distDir, entryPath).replaceAll("\\", "/");
199-
if (opts.skipRelativePath?.(relativePath)) {
200-
continue;
201-
}
202-
if (entry.isDirectory()) {
203-
pending.push(entryPath);
204-
continue;
205-
}
206-
if (entry.isFile() && ROOT_DIST_JAVASCRIPT_MODULE_FILE_RE.test(entry.name)) {
207-
files.push(entryPath);
204+
const dir = opendirSync(currentDir);
205+
try {
206+
while (true) {
207+
const entry = dir.readSync();
208+
if (!entry) {
209+
break;
210+
}
211+
212+
const entryPath = join(currentDir, entry.name);
213+
const relativePath = relative(distDir, entryPath).replaceAll("\\", "/");
214+
if (opts.skipRelativePath?.(relativePath)) {
215+
continue;
216+
}
217+
if (entry.isDirectory()) {
218+
pending.push(entryPath);
219+
continue;
220+
}
221+
if (entry.isFile() && ROOT_DIST_JAVASCRIPT_MODULE_FILE_RE.test(entry.name)) {
222+
files.push(entryPath);
223+
if (opts.maxFiles !== undefined && files.length > opts.maxFiles) {
224+
return {
225+
files,
226+
limit: opts.maxFiles,
227+
limitExceeded: true,
228+
};
229+
}
230+
}
208231
}
232+
} finally {
233+
dir.closeSync();
209234
}
210235
}
211236

212-
return files;
237+
return { files, limitExceeded: false };
238+
}
239+
240+
function formatInstalledDistFileScanLimitError(scope: string, limit: number): string {
241+
return `installed package ${scope} contains more than ${limit} JavaScript files; refusing to scan unbounded package contents.`;
213242
}
214243

215244
export function collectInstalledContextEngineRuntimeErrors(packageRoot: string): string[] {
216245
const errors: string[] = [];
217-
for (const filePath of listDistJavaScriptFiles(packageRoot)) {
246+
const distFiles = listDistJavaScriptFiles(packageRoot, {
247+
maxFiles: MAX_INSTALLED_ROOT_DIST_JS_FILES,
248+
});
249+
if (distFiles.limitExceeded) {
250+
return [formatInstalledDistFileScanLimitError("dist", distFiles.limit)];
251+
}
252+
253+
for (const filePath of distFiles.files) {
218254
const contents = readFileSync(filePath, "utf8");
219255
if (contents.includes(LEGACY_CONTEXT_ENGINE_UNRESOLVED_RUNTIME_MARKER)) {
220256
errors.push(
@@ -345,9 +381,11 @@ export function collectInstalledPluginSdkDeclarationErrors(packageRoot: string):
345381
return errors;
346382
}
347383

348-
function listInstalledRootDistJavaScriptFiles(packageRoot: string): string[] {
384+
function listInstalledRootDistJavaScriptFiles(packageRoot: string): DistJavaScriptFileListResult {
349385
return listDistJavaScriptFiles(packageRoot, {
350-
skipRelativePath: (relativePath) => relativePath.startsWith("extensions/"),
386+
maxFiles: MAX_INSTALLED_ROOT_DIST_JS_FILES,
387+
skipRelativePath: (relativePath) =>
388+
relativePath === "extensions" || relativePath.startsWith("extensions/"),
351389
});
352390
}
353391

@@ -450,16 +488,14 @@ export function collectInstalledRootDependencyManifestErrors(packageRoot: string
450488
...Object.keys(rootPackageJson.optionalDependencies ?? {}),
451489
]);
452490
const distFiles = listInstalledRootDistJavaScriptFiles(packageRoot);
453-
if (distFiles.length > MAX_INSTALLED_ROOT_DIST_JS_FILES) {
454-
return [
455-
`installed package root dist contains ${distFiles.length} JavaScript files, exceeding the ${MAX_INSTALLED_ROOT_DIST_JS_FILES} file scan limit.`,
456-
];
491+
if (distFiles.limitExceeded) {
492+
return [formatInstalledDistFileScanLimitError("root dist", distFiles.limit)];
457493
}
458494
const missingImporters = new Map<string, Set<string>>();
459495
const bundledExtensionRuntimeDependencyOwners =
460496
collectBundledExtensionRuntimeDependencyOwners(packageRoot);
461497

462-
for (const filePath of distFiles) {
498+
for (const filePath of distFiles.files) {
463499
const fileStat = lstatSync(filePath);
464500
if (!fileStat.isFile() || fileStat.size > MAX_INSTALLED_ROOT_DIST_JS_BYTES) {
465501
const relativePath = relative(join(packageRoot, "dist"), filePath).replaceAll("\\", "/");

‎test/openclaw-npm-postpublish-verify.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ import {
1616
resolveInstalledBinaryPath,
1717
} from "../scripts/openclaw-npm-postpublish-verify.ts";
1818

19+
const INSTALLED_ROOT_DIST_JS_FILE_SCAN_LIMIT = 10_000;
20+
21+
function writeDistJavaScriptFiles(packageRoot: string, count: number): void {
22+
const distDir = join(packageRoot, "dist");
23+
mkdirSync(distDir, { recursive: true });
24+
for (let index = 0; index < count; index += 1) {
25+
writeFileSync(join(distDir, `chunk-${index}.js`), "export {};\n", "utf8");
26+
}
27+
}
28+
1929
describe("buildPublishedInstallScenarios", () => {
2030
it("uses a single fresh scenario for plain stable releases", () => {
2131
expect(buildPublishedInstallScenarios("2026.3.23")).toEqual([
@@ -147,6 +157,20 @@ describe("collectInstalledContextEngineRuntimeErrors", () => {
147157
rmSync(packageRoot, { recursive: true, force: true });
148158
}
149159
});
160+
161+
it("refuses unbounded packaged dist scans", () => {
162+
const packageRoot = makeInstalledPackageRoot();
163+
164+
try {
165+
writeDistJavaScriptFiles(packageRoot, INSTALLED_ROOT_DIST_JS_FILE_SCAN_LIMIT + 1);
166+
167+
expect(collectInstalledContextEngineRuntimeErrors(packageRoot)).toEqual([
168+
`installed package dist contains more than ${INSTALLED_ROOT_DIST_JS_FILE_SCAN_LIMIT} JavaScript files; refusing to scan unbounded package contents.`,
169+
]);
170+
} finally {
171+
rmSync(packageRoot, { recursive: true, force: true });
172+
}
173+
});
150174
});
151175

152176
describe("collectInstalledPluginSdkZodArtifactErrors", () => {
@@ -493,4 +517,22 @@ describe("collectInstalledRootDependencyManifestErrors", () => {
493517
rmSync(packageRoot, { recursive: true, force: true });
494518
}
495519
});
520+
521+
it("refuses unbounded root dist dependency scans", () => {
522+
const packageRoot = makeInstalledPackageRoot();
523+
524+
try {
525+
writePackageFile(packageRoot, "package.json", {
526+
version: "2026.4.22",
527+
dependencies: {},
528+
});
529+
writeDistJavaScriptFiles(packageRoot, INSTALLED_ROOT_DIST_JS_FILE_SCAN_LIMIT + 1);
530+
531+
expect(collectInstalledRootDependencyManifestErrors(packageRoot)).toEqual([
532+
`installed package root dist contains more than ${INSTALLED_ROOT_DIST_JS_FILE_SCAN_LIMIT} JavaScript files; refusing to scan unbounded package contents.`,
533+
]);
534+
} finally {
535+
rmSync(packageRoot, { recursive: true, force: true });
536+
}
537+
});
496538
});

0 commit comments

Comments
 (0)