Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
344 changes: 344 additions & 0 deletions packages/tooling/vis/__tests__/native/attest-parity.test.ts

Large diffs are not rendered by default.

41 changes: 25 additions & 16 deletions packages/tooling/vis/__tests__/native/info-dlx-parity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ import { resolveNativeBinary } from "../../bin/resolve-binary.mjs";
* side. The environment the manager sees is compared in full, except for the variables the native launcher
* contract adds on its own (`LAUNCHER_VARIABLES`): they are named, not hidden.
*
* The first-run gate of `dlx` (package metadata over the network, a prompt) is never exercised: every native
* case switches it off, and every case that would run it only has to be handed to Node.
* The first-run gate of `dlx` skips itself, with no network work, in CI, off a terminal and with `--yes`, and
* these runs have neither a terminal nor a registry: the cases cover that fast path, with and without `--no-info`.
* The gate's panel and prompt (a terminal, an unapproved version) are Node's, and the lookup that tells an approved
* version from a new one runs only on a terminal; it is covered by the unit tests in `dlx.rs`, against a registry stub.
* `--info` and a value of `VIS_DLX_YES` or `VIS_DLX_NO_INFO` that is not ASCII are handed to Node.
*
* Needs the native `vis` binary, the built CLI (`dist/bin.js`) and the NAPI addon; skipped otherwise.
*/
Expand Down Expand Up @@ -449,6 +452,15 @@ describe.skipIf(!ready)("vis info and vis dlx: native vs Node", () => {
}

const DLX_SHAPES: DlxShape[] = [
// The gate skips itself: no terminal, and `--yes`.
{ args: ["dlx", "cowsay"] },
{ args: ["dlx", "cowsay", "hi"] },
{ args: ["dlx", "@scope/pkg@1", "a"] },
{ args: ["dlx", "--yes", "cowsay"] },
{ args: ["dlx", "-y", "cowsay"] },
{ args: ["dlx", "--silent", "cowsay"] },
{ args: ["dlx", "-p", "lolcatjs", "-s", "cowsay"] },
{ args: ["dlx", "./local.js", "-p", "cowsay", "-s"] },
{ args: ["dlx", "--no-info", "cowsay"] },
{ args: ["dlx", "cowsay", "--no-info"] },
{ args: ["dlx", "--no-info", "cowsay", "hi", "there"] },
Expand All @@ -473,6 +485,10 @@ describe.skipIf(!ready)("vis info and vis dlx: native vs Node", () => {
// Shell mode vets only the `-p` packages, so with none the gate is out without `--no-info`.
{ args: ["dlx", "-c", "echo hi"], needs: "shell" },
{ args: ["dlx", "--shell-mode", "echo hi && echo there", "--silent"], needs: "shell" },
{ args: ["dlx", "-p", "lolcatjs", "-c", "echo hi"], needs: "shell" },
{ args: ["dlx", "-c", "echo hi", "-p", "lolcatjs", "-s"], needs: "shell" },
{ args: ["dlx", "-p", "lolcatjs", "-c", "./local.js"], needs: "shell" },
{ args: ["dlx", "--offline", "cowsay"], needs: "offline" },
// Paths, URLs and aliases are no registry specs.
{ args: ["dlx", "./local.js"] },
{ args: ["dlx", "../tools/run.js", "a", "b"] },
Expand All @@ -498,6 +514,10 @@ describe.skipIf(!ready)("vis info and vis dlx: native vs Node", () => {
...["1", "true", "TRUE", "yes", "on", "anything", " 1 ", "2"].map((value) => { return { args: ["dlx", "cowsay", "hi"], env: { VIS_DLX_NO_INFO: value }, fixture: "pnpm-single", label: `VIS_DLX_NO_INFO=${JSON.stringify(value)}` }; }),
// With the gate out the other variable changes nothing.
{ args: ["dlx", "--no-info", "cowsay"], env: { VIS_DLX_YES: "1" }, fixture: "pnpm-single", label: "VIS_DLX_YES beside --no-info" },
// The gate skips itself whatever these say, because no stream is a terminal.
...["0", "false", "", "no", " NO "].map((value) => { return { args: ["dlx", "cowsay"], env: { VIS_DLX_NO_INFO: value }, fixture: "pnpm-single", label: `VIS_DLX_NO_INFO=${JSON.stringify(value)} with a registry spec` }; }),
...["1", "true", "yes", "on", " 1 ", "0", ""].map((value) => { return { args: ["dlx", "cowsay"], env: { VIS_DLX_YES: value }, fixture: "pnpm-single", label: `VIS_DLX_YES=${JSON.stringify(value)}` }; }),
{ args: ["dlx", "cowsay"], ci: false, env: { VIS_NO_UPDATE_CHECK: UNSET }, fixture: "pnpm-single", label: "a registry spec outside CI, with the update check on" },
// A falsy value leaves the gate on, which only a non-registry spec makes irrelevant.
...["", "0", "false", "no", " NO "].map((value) => { return { args: ["dlx", "./local.js"], env: { VIS_DLX_NO_INFO: value }, fixture: "pnpm-single", label: `a path with VIS_DLX_NO_INFO=${JSON.stringify(value)}` }; }),
// The lean entry runs the manager in the working directory, with no workspace root and no config.
Expand Down Expand Up @@ -529,25 +549,14 @@ describe.skipIf(!ready)("vis info and vis dlx: native vs Node", () => {
];

const DLX_DELEGATED_CASES: DelegatedCase[] = [
// The first-run gate may run (it skips itself in CI, without a terminal and with `--yes`, which the binary does not decide): Node's before any output.
// `--info` shows the panel on every run, and a value JavaScript and Rust could trim differently is Node's.
...[
["dlx", "cowsay"],
["dlx", "cowsay", "hi"],
["dlx", "--yes", "cowsay"],
["dlx", "-y", "cowsay"],
["dlx", "--info", "cowsay"],
["dlx", "--offline", "cowsay"],
["dlx", "--silent", "cowsay"],
["dlx", "@scope/pkg@1", "a"],
["dlx", "-p", "lolcatjs", "-c", "echo hi"],
["dlx", "-c", "echo hi", "-p", "lolcatjs", "-s"],
["dlx", "-p", "lolcatjs", "-c", "./local.js"],
["dlx", "./local.js", "-p", "cowsay", "-s"],
["dlx", "cowsay", "--info"],
].map((args) => { return { args, fixture: "pnpm-single", label: `vis ${args.join(" ")}` }; }),
...[{ VIS_DLX_YES: "1" }, { VIS_DLX_NO_INFO: "0" }, { VIS_DLX_NO_INFO: "false" }, { VIS_DLX_NO_INFO: "" }, { VIS_DLX_NO_INFO: "no" }, { VIS_DLX_NO_INFO: " 1" }, { VIS_DLX_NO_INFO: " " }].map((env) => {
...[{ VIS_DLX_YES: "\u00A01" }, { VIS_DLX_YES: " " }, { VIS_DLX_NO_INFO: "\u00A01" }, { VIS_DLX_NO_INFO: " " }].map((env) => {
return { args: ["dlx", "cowsay"], env, fixture: "pnpm-single", label: `environment ${JSON.stringify(env)}` };
}),
{ args: ["dlx", "cowsay"], ci: false, fixture: "pnpm-single", label: "outside CI" },
// Shapes of the command line.
...[
["dlx"],
Expand Down
29 changes: 8 additions & 21 deletions packages/tooling/vis/__tests__/native/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ describe("launcher routing (bin/route.mjs)", () => {
[["dlx", "git+https://example.test/a.git"]],
[["dlx", "npm:cowsay@1"]],
[["dlx", "-p", "./local.tgz", "-c", "echo hi"]],
// The first-run gate is the binary's to decide: it does nothing with `--yes`, in CI and off a terminal, and on a
// terminal it answers only for a version that was approved.
[["dlx", "cowsay"]],
[["dlx", "--yes", "cowsay"]],
[["dlx", "cowsay", "-y"]],
[["dlx", "-p", "lolcatjs", "-c", "echo hi"]],
[["dlx", "-p", "lolcatjs", "-c", "./local.js"]],
])("routes `vis %j`, whatever the terminal and CI", (args: string[]) => {
expect.assertions(3);

Expand All @@ -201,12 +208,8 @@ describe("launcher routing (bin/route.mjs)", () => {
});

it.each([
// The first-run gate runs, which Node answers before any output.
[["dlx", "cowsay"]],
[["dlx", "--yes", "cowsay"]],
// `--info` shows the panel on every run, which Node renders.
[["dlx", "--info", "cowsay"]],
[["dlx", "-p", "lolcatjs", "-c", "echo hi"]],
[["dlx", "-p", "lolcatjs", "-c", "./local.js"]],
// Shapes of the command line.
[["dlx"]],
[["dlx", "--no-info"]],
Expand All @@ -226,22 +229,6 @@ describe("launcher routing (bin/route.mjs)", () => {
expect(routesToNative(args, "linux", {}, "/work/app", false)).toBe(false);
});

it("follows VIS_DLX_NO_INFO the way isTruthyEnv does, and leaves a non-ASCII value to Node", () => {
expect.assertions(11);

const dlx = ["dlx", "cowsay"];

for (const value of ["1", "true", "YES", " on ", "anything"]) {
expect(routesToNative(dlx, "linux", { VIS_DLX_NO_INFO: value }, "/work/app", false)).toBe(true);
}

for (const value of ["", "0", "false", "no", " No "]) {
expect(routesToNative(dlx, "linux", { VIS_DLX_NO_INFO: value }, "/work/app", false)).toBe(false);
}

expect(routesToNative(dlx, "linux", { VIS_DLX_NO_INFO: "\u00A01" }, "/work/app", false)).toBe(false);
});

it("keeps the dlx in Node on Windows and where the environment changes the installer or Node's output", () => {
expect.assertions(8);

Expand Down
137 changes: 105 additions & 32 deletions packages/tooling/vis/bin/route.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -727,35 +727,12 @@ const dlxShape = (rest) => {
return shape.words.length > 0 ? shape : undefined;
};

// `isRegistrySpec` of the first-run gate (`src/dlx/first-run.ts`): not a path, URL or protocol alias.
const PATH_SPEC = /^[./~]/;

const isRegistrySpec = (spec) => {
const bare = spec.startsWith("@") ? spec.slice(1) : spec;

return spec !== "" && !PATH_SPEC.test(spec) && !bare.includes(":");
};

// `isTruthyEnv` for an ASCII value; the binary leaves any other to Node, where `trim` and Rust's differ.
const isAscii = (text) => [...text].every((character) => character.codePointAt(0) <= 0x7F);

const dlxGateIsSwitchedOff = (environment, shape) => {
const value = environment.VIS_DLX_NO_INFO;

if (value !== undefined && !isAscii(value)) {
return false;
}

const noInfoFromEnvironment = value !== undefined && !["", "0", "false", "no"].includes(value.trim().toLowerCase());
const vetted = [...shape.flags.has("-c") ? [] : shape.words.slice(0, 1), ...shape.packages];

return shape.flags.has("--no-info") || noInfoFromEnvironment || !vetted.some((spec) => isRegistrySpec(spec));
};

// Whether `vis dlx` can pass the binary's native gate (`plan` in `dlx.rs`): only the runs where the first-run gate does
// nothing, the shapes above, and an environment that leaves the installer and Node's output alone. The lean entry reads
// no config and no workspace root, and prints nothing of its own, so a terminal and a missing `CI` make no difference.
// The binary re-checks everything, including the lockfiles, `packageManager` pins and `aube` on `PATH`.
// Whether `vis dlx` can pass the binary's native gate (`plan` in `dlx.rs`): the shapes above, and an environment that
// leaves the installer and Node's output alone. The first-run gate is the binary's to decide (it does nothing with
// `--no-info`, `--yes`, CI or a stream that is not a terminal, and on a terminal it looks the package up and answers only
// for a version already approved), and so are the lockfiles, `packageManager` pins and `aube` on `PATH`; this only skips
// the extra process for a shape the binary would delegate anyway. The lean entry reads no config and no workspace root,
// and prints nothing of its own.
const dlxMayRunNatively = (platform, environment, args) => {
if (
platform === "win32"
Expand All @@ -766,9 +743,7 @@ const dlxMayRunNatively = (platform, environment, args) => {
return false;
}

const shape = dlxShape(args.slice(1));

return shape !== undefined && dlxGateIsSwitchedOff(environment, shape);
return dlxShape(args.slice(1)) !== undefined;
};

// Whether `vis sync ci-workflow` can pass the binary's native gate (`native_root` and `parse` in
Expand Down Expand Up @@ -1107,6 +1082,102 @@ const keysRefreshArgsAreNative = (args) => {
return (flags.length === 1 && flags[0] === "--json") || (flags.length === 2 && flags.includes("--json") && flags.includes("--clear"));
};

// The flags of `vis attest` (`parse` in `attest/mod.rs`): `--name value` or `--name=value` for the values, `--name` for the
// switches, each at most once, and plain words. A value that is empty or starts with a dash, any other flag and `--` are not.
const attestFlag = (shape, rest, index, values, switches) => {
const [name, ...inline] = rest[index].slice(2).split("=");

if (switches.includes(name)) {
if (inline.length > 0 || shape.switches.has(name)) {
return undefined;
}

shape.switches.add(name);

return index + 1;
}

const value = inline.length > 0 ? inline.join("=") : rest[index + 1];

if (!values.includes(name) || shape.values.has(name) || value === undefined || value === "" || value.startsWith("-")) {
return undefined;
}

shape.values.set(name, value);

return inline.length > 0 ? index + 1 : index + 2;
};

const attestShape = (rest, values, switches) => {
const shape = { switches: new Set(), values: new Map(), words: [] };
let index = 0;

while (index < rest.length) {
const token = rest[index];

if (!token.startsWith("-")) {
shape.words.push(token);
index += 1;

continue;
}

index = token.startsWith("--") ? attestFlag(shape, rest, index, values, switches) : undefined;

if (index === undefined) {
return undefined;
}
}

return shape;
};

const ATTEST_FORMATS = ["json", "ndjson", "table"];

// Whether `vis attest <subject>` can pass the binary's native gate (`answer` in `attest/emit.rs`): the run that has no ambient OIDC
// token, so Node hashes the artifact and skips signing. A run that signs, `--require-signing` and the failures stay in Node. The
// binary re-checks everything, including the subject and the workspace.
const attestEmitShapeIsNative = (environment, rest) => {
const shape = attestShape(rest, ["format", "output", "predicate"], ["require-signing"]);

return shape !== undefined
&& shape.words.length === 1
&& shape.words[0] !== "verify"
&& !shape.switches.has("require-signing")
&& ["slsaProvenance", undefined].includes(shape.values.get("predicate"))
&& [undefined, ...ATTEST_FORMATS].includes(shape.values.get("format"))
&& environment.CI !== "true"
&& environment.ACTIONS_ID_TOKEN_REQUEST_URL === undefined
&& environment.SIGSTORE_ID_TOKEN === undefined;
};

// Whether `vis attest verify` can pass the binary's native gate (`answer` in `attest/verify.rs`): no `--prod-only`, no word, no
// `MARSHALL_*` or `VIS_NPM_KEYS_URL`. The binary re-checks everything, including the lockfile, the registry and the keys.
const attestVerifyShapeIsNative = (environment, rest) => {
const shape = attestShape(rest, ["format", "fail-on", "allowlist"], []);

return shape !== undefined
&& shape.words.length === 0
&& [undefined, ...ATTEST_FORMATS].includes(shape.values.get("format"))
&& ["error", undefined, "warning"].includes(shape.values.get("fail-on"))
&& environment.VIS_NPM_KEYS_URL === undefined
&& !Object.keys(environment).some((key) => key.startsWith("MARSHALL_"));
};

// `vis attest <subject>` and `vis attest verify`, in a CI context off a terminal at the root of a pnpm workspace, with no proxy,
// CA or Node option in the environment.
const attestMayRunNatively = (platform, environment, args, cwd, interactive, holdsMarker) => {
if (platform === "win32" || interactive || !inCi(environment) || !cwdIsRoot(environment, cwd, holdsMarker) || !pailOutputIsDecidable(environment, args)) {
return false;
}

if (!outputSettingsAreDefault(environment) || !networkEnvironmentIsPlain(environment)) {
return false;
}

return args[1] === "verify" ? attestVerifyShapeIsNative(environment, args.slice(2)) : attestEmitShapeIsNative(environment, args.slice(1));
};

const keysRefreshMayRunNatively = (platform, environment, args, cwd, interactive, holdsMarker) => {
if (platform === "win32" || interactive || !inCi(environment) || !cwdIsRoot(environment, cwd, holdsMarker) || nodeWarnsAboutColour(environment)) {
return false;
Expand Down Expand Up @@ -1425,6 +1496,8 @@ const NATIVE_SHAPE_GATES = new Map([
["add", ({ args, environment, interactive, platform }) => helpOrNative(args, () => addMayRunNatively(platform, environment, args, interactive))],
// `affected <target> --sparse-checkout`.
["affected", ({ args, environment, interactive, platform }) => helpOrNative(args, () => affectedMayRunNatively(platform, environment, args, interactive))],
// `attest <subject>` for a run with no OIDC token (Node skips signing) and `attest verify` (JSON, NDJSON, and a table of no findings).
["attest", ({ args, cwd, environment, holdsMarker, interactive, platform }) => helpOrNative(args, () => attestMayRunNatively(platform, environment, args, cwd, interactive, holdsMarker))],
// `audit --format json`, answered only when the scan finds nothing.
["audit", ({ args, cwd, environment, holdsMarker, interactive, platform }) => helpOrNative(args, () => auditMayRunNatively(platform, environment, args, cwd, interactive, holdsMarker))],
// `cache list`, `cache size` (`cache.rs`), `cache clean` (`cache_clean.rs`), `cache prune` (`cache_prune.rs`), `cache hash` (`cache_hash.rs`) and `cache why` (`cache_why.rs`); the binary hands every other shape to Node itself.
Expand Down
1 change: 1 addition & 0 deletions packages/tooling/vis/native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/tooling/vis/native/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ ureq = { version = "3.5", default-features = false, features = [
"rustls",
"gzip"
] }
# `vis attest verify`: the ECDSA P-256 check of the registry's signatures (src/attest/signature.rs). The crate `rustls`
# already links as its crypto provider.
ring = "0.17"
# Already in the tree through `ureq`; named here to read its errors apart (src/http/error.rs) and, in the tests, to
# serve TLS. Same features as `ureq` turns on.
rustls = { version = "0.23", default-features = false, features = [
Expand Down
Loading
Loading