fix(scripts): check-manifest takes no path argument
SonarCloud's second pass still flagged S8707: the directory to check came from argv and flowed into file reads, and a startsWith() allowlist did not count as sanitizing it. The CLI now only ever checks the repository's own .output/chrome-mv3; the logic is an exported checkExtension(dir) (typed by check-manifest.d.mts) that the tests call directly, so no command-line path reaches a file read at all. Path resolution moved inside the entry point: under Vitest's jsdom environment import.meta.url is not a file: URL, so resolving it at module load threw on import. The two Node-script suites now run in the node environment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1bd6aca93c
commit
83decd92cc
4 files changed
+61
-49
No files matched your search
@@ -0,0 +1,5 @@
|
||||
/**
|
||||
* Types for scripts/check-manifest.mjs, so its tests can call it directly.
|
||||
* The script itself is plain JavaScript checked with `// @ts-check`.
|
||||
*/
|
||||
export function checkExtension(outDir: string): { problems: string[]; readError?: string };
|
||||
+35
-36
@@ -16,16 +16,15 @@
|
||||
* (WXT drops `_locales/` and `icon/` silently when publicDir is wrong);
|
||||
* 3. the bundle: no network API anywhere in the shipped JavaScript.
|
||||
*
|
||||
* Usage: node scripts/check-manifest.mjs [<extension dir>]
|
||||
* default: <repo>/.output/chrome-mv3 (resolved from this file, not the cwd)
|
||||
* Usage: node scripts/check-manifest.mjs — checks <repo>/.output/chrome-mv3,
|
||||
* resolved from this file, not the cwd. It takes no arguments on purpose: a
|
||||
* path from the command line flowed straight into file reads (SonarCloud
|
||||
* S8707). Its tests call `checkExtension(dir)` directly instead.
|
||||
*/
|
||||
import { existsSync, readdirSync, readFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join, relative, resolve, sep } from 'node:path';
|
||||
import { join, relative, resolve } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const REPO = resolve(fileURLToPath(new URL('..', import.meta.url)));
|
||||
|
||||
/** Exactly this, in any order. Adding one is a minor-version event (docs/09 §4). */
|
||||
const ALLOWED_PERMISSIONS = ['bookmarks', 'storage'];
|
||||
|
||||
@@ -102,20 +101,6 @@ const NETWORK_IN_BUNDLE =
|
||||
/** @param {string} a @param {string} b */
|
||||
const byName = (a, b) => a.localeCompare(b, 'en');
|
||||
|
||||
/**
|
||||
* The directory to check. An argument is accepted only inside this repository
|
||||
* or the OS temp directory (where its own tests build fake extensions): the
|
||||
* script reads every file below it, so it must never be pointed elsewhere.
|
||||
*
|
||||
* @param {string | undefined} arg
|
||||
* @returns {string | undefined}
|
||||
*/
|
||||
function outDirFrom(arg) {
|
||||
const dir = resolve(arg ?? join(REPO, '.output', 'chrome-mv3'));
|
||||
const bases = [REPO, resolve(tmpdir())];
|
||||
return bases.some((base) => dir === base || dir.startsWith(base + sep)) ? dir : undefined;
|
||||
}
|
||||
|
||||
/** @param {string} dir @returns {string[]} */
|
||||
function listFiles(dir) {
|
||||
return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
|
||||
@@ -126,16 +111,13 @@ function listFiles(dir) {
|
||||
|
||||
/**
|
||||
* @param {string} outDir
|
||||
* @returns {Record<string, unknown> | undefined}
|
||||
* @returns {Record<string, unknown> | string} the manifest, or why it could not be read
|
||||
*/
|
||||
function readManifest(outDir) {
|
||||
const path = join(outDir, 'manifest.json');
|
||||
try {
|
||||
return JSON.parse(readFileSync(path, 'utf8'));
|
||||
return JSON.parse(readFileSync(join(outDir, 'manifest.json'), 'utf8'));
|
||||
} catch (cause) {
|
||||
console.error(`check-manifest: cannot read ${path} — run \`npm run build\` first.`);
|
||||
console.error(` ${cause instanceof Error ? cause.message : cause}`);
|
||||
return undefined;
|
||||
return `cannot read manifest.json (${cause instanceof Error ? cause.message : cause})`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,17 +192,32 @@ function checkBundle(outDir, problems) {
|
||||
}
|
||||
}
|
||||
|
||||
/** @param {string} outDir @returns {number} the exit code */
|
||||
function run(outDir) {
|
||||
/**
|
||||
* Check one built extension directory.
|
||||
*
|
||||
* @param {string} outDir
|
||||
* @returns {{ problems: string[], readError?: string }} no problems and no read
|
||||
* error means the build is clean
|
||||
*/
|
||||
export function checkExtension(outDir) {
|
||||
const manifest = readManifest(outDir);
|
||||
if (manifest === undefined) return 1;
|
||||
|
||||
if (typeof manifest === 'string') return { problems: [], readError: manifest };
|
||||
/** @type {string[]} */
|
||||
const problems = [];
|
||||
checkManifest(manifest, problems);
|
||||
checkInventory(outDir, manifest, problems);
|
||||
checkBundle(outDir, problems);
|
||||
return { problems };
|
||||
}
|
||||
|
||||
/** @returns {number} the exit code */
|
||||
function main() {
|
||||
const repo = resolve(fileURLToPath(new URL('..', import.meta.url)));
|
||||
const { problems, readError } = checkExtension(join(repo, '.output', 'chrome-mv3'));
|
||||
if (readError !== undefined) {
|
||||
console.error(`check-manifest: ${readError} — run \`npm run build\` first.`);
|
||||
return 1;
|
||||
}
|
||||
if (problems.length > 0) {
|
||||
for (const problem of problems) {
|
||||
console.error(`::error::check-manifest: ${problem}`);
|
||||
@@ -237,11 +234,13 @@ function run(outDir) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const outDir = outDirFrom(process.argv[2]);
|
||||
if (outDir === undefined) {
|
||||
console.error('check-manifest: refusing to read outside this repository or the temp directory.');
|
||||
process.exitCode = 1;
|
||||
} else {
|
||||
// Run only as the entry point. Under a test runner `import.meta.url` may not
|
||||
// even be a file: URL, so it is not resolved until it is known to be one.
|
||||
const isEntry =
|
||||
import.meta.url.startsWith('file:') &&
|
||||
process.argv[1] !== undefined &&
|
||||
resolve(process.argv[1]) === fileURLToPath(import.meta.url);
|
||||
if (isEntry) {
|
||||
// exitCode, not exit(): exit() can cut off piped stderr before CI reads it.
|
||||
process.exitCode = run(outDir);
|
||||
process.exitCode = main();
|
||||
}
|
||||
@@ -1,13 +1,16 @@
|
||||
// @vitest-environment node
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { afterEach, describe, expect, it } from 'vitest';
|
||||
import { checkExtension } from '../../../scripts/check-manifest.mjs';
|
||||
|
||||
/**
|
||||
* scripts/check-manifest.mjs gates the BUILT extension (docs/08 §2–§3). It ran
|
||||
* in CI and on every tag with no test of its own; these build a fake
|
||||
* `.output/chrome-mv3` tree per case and run the real script against it.
|
||||
* in CI and on every tag with no test of its own. These build a fake
|
||||
* `.output/chrome-mv3` tree per case and call the script's `checkExtension`
|
||||
* on it — the CLI itself takes no path argument (SonarCloud S8707).
|
||||
*/
|
||||
|
||||
const SCRIPT = join(process.cwd(), 'scripts', 'check-manifest.mjs');
|
||||
@@ -58,9 +61,10 @@ function build(manifest: unknown, overrides: Record<string, string | null> = {})
|
||||
return dir;
|
||||
}
|
||||
|
||||
function check(dir: string): { status: number | null; output: string } {
|
||||
const result = spawnSync(process.execPath, [SCRIPT, dir], { encoding: 'utf8' });
|
||||
return { status: result.status, output: `${result.stdout}${result.stderr}` };
|
||||
function check(dir: string): { status: number; output: string } {
|
||||
const { problems, readError } = checkExtension(dir);
|
||||
const failed = readError !== undefined || problems.length > 0;
|
||||
return { status: failed ? 1 : 0, output: [readError ?? '', ...problems].join('\n') };
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
@@ -71,7 +75,6 @@ describe('check-manifest.mjs', () => {
|
||||
it('passes a correct build', () => {
|
||||
const { status, output } = check(build(MANIFEST));
|
||||
expect(status, output).toBe(0);
|
||||
expect(output).toContain('check-manifest: clean');
|
||||
});
|
||||
|
||||
it.each([
|
||||
@@ -134,17 +137,21 @@ describe('check-manifest.mjs', () => {
|
||||
expect(output).toContain(`network API "${api}`);
|
||||
});
|
||||
|
||||
it('refuses to read a directory outside the repository and the temp directory', () => {
|
||||
const { status, output } = check(process.platform === 'win32' ? 'C:Windows' : '/etc');
|
||||
expect(status).toBe(1);
|
||||
expect(output).toContain('refusing to read outside');
|
||||
});
|
||||
|
||||
it('fails clearly when there is no build at all', () => {
|
||||
const empty = mkdtempSync(join(tmpdir(), 'check-manifest-empty-'));
|
||||
dirs.push(empty);
|
||||
const { status, output } = check(empty);
|
||||
expect(status).toBe(1);
|
||||
expect(output).toContain('run `npm run build` first');
|
||||
expect(output).toContain('cannot read manifest.json');
|
||||
});
|
||||
|
||||
it('runs as a CLI with no arguments, against the repository build', () => {
|
||||
// Whatever state .output/ is in, the CLI must exit cleanly or explain why —
|
||||
// never crash, and never read a path it was handed.
|
||||
const result = spawnSync(process.execPath, [SCRIPT, '/etc'], { encoding: 'utf8' });
|
||||
const output = `${result.stdout}${result.stderr}`;
|
||||
expect([0, 1]).toContain(result.status);
|
||||
expect(output).toMatch(/check-manifest: (clean|cannot read|.*permissions)/);
|
||||
expect(output).not.toContain('/etc');
|
||||
});
|
||||
});
|
||||
@@ -1,3 +1,4 @@
|
||||
// @vitest-environment node
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
|
||||
Reference in new issue
Block a user