diff --git a/.github/workflows/bazel-npm.yml b/.github/workflows/bazel-npm.yml new file mode 100644 index 000000000..ae9c89092 --- /dev/null +++ b/.github/workflows/bazel-npm.yml @@ -0,0 +1,132 @@ +name: Bazel patch compatibility (npm) + +# The npm flows of the real-Bazel tests (`npm::` in `e2e_bazel_build`): +# hosted and vendored mode for a package that aspect_rules_js links from a +# pnpm lock, on each Bazel release below. bazel-compatibility.yml holds the +# legs and what every ecosystem shares; this file says when the npm legs run +# and on which releases. +# +# Beyond what every leg needs, an npm leg downloads Node from nodejs.org and +# ruleset archives from codeload.github.com; its repository cache holds them +# and comes to about 100 MB. An npm tarball under test passes through that +# cache as well: Bazel's downloader fetches it and keeps it under its +# sha512. So a test builds the upstream tarball and each patched one anew, +# with an id of the run in the entry file, and writes the upstream sum into +# the lock of its workspace copy. No cache holds a tarball of the run, and +# the test counts the requests for the upstream one. +# +# Not required for merge: no `merge_group` trigger, and no job here is a +# `needs` of ci.yml's `ci-ok`. + +on: + pull_request: + types: [opened, synchronize, reopened, ready_for_review] + # What a pull request run covers: the npm flows and their fixture, the + # harness and the workflows that run it, the Bazel installer and its + # pins, and the npm-family files of socket-patch-core that the two + # flows run through (a coverage build of the binary showed which): the + # pnpm lock's model and hosted planner (formats/pnpm/), the pnpm + # vendored backend with the npm code beside it (vendor/pnpm_*, + # vendor/npm_*, the lock inventory), the `pnpm-workspace.yaml` lookup, + # the npm-family lock and config readers (vex/discover/npm.rs, + # patch/redirect/npmrc.rs), the upstream restore of `rollback`, the + # crawler every `scan` starts, and the `package.json` reader. The + # globs name that one crate, so no bench fixture is here, and nothing + # another ecosystem's flow owns: a change to one ecosystem starts that + # ecosystem's legs only. + # + # What it does not cover: shared engine code (patch/redirect/mod.rs, + # the rest of vendor/, hosted/, commands/), the shared test helpers and + # the toolchain files. A change there gets its run on the push to main + # (when the file is in the list below) or in the nightly run, not on the + # pull request: that is the price of not running every ecosystem's legs + # on every shared change. On demand, workflow_dispatch runs the legs on + # any branch. + paths: + - '.github/workflows/bazel-npm.yml' + - '.github/workflows/bazel-compatibility.yml' + - 'scripts/install-bazel.sh' + - 'scripts/bazel-pins.txt' + - 'crates/socket-patch-cli/tests/e2e_bazel_build/npm.rs' + - 'crates/socket-patch-cli/tests/e2e_bazel_build/harness.rs' + - 'crates/socket-patch-cli/tests/fixtures/bazel/rules_js/**' + - 'crates/socket-patch-core/src/**/*pnpm*' + - 'crates/socket-patch-core/src/**/*pnpm*/**' + - 'crates/socket-patch-core/src/vendor/npm_*' + - 'crates/socket-patch-core/src/vendor/lock_inventory/npm_family.rs' + - 'crates/socket-patch-core/src/patch/redirect/npmrc.rs' + - 'crates/socket-patch-core/src/patch/redirect/upstream/npm.rs' + - 'crates/socket-patch-core/src/vex/discover/npm.rs' + - 'crates/socket-patch-core/src/crawlers/npm_crawler.rs' + - 'crates/socket-patch-core/src/formats/package_json.rs' + push: + branches: [main] + # The pull request paths, and what only a push to main covers: the + # root of the test target (it changes when an ecosystem's module is + # added; ci.yml builds the target and runs its hermetic tests on every + # pull request), the shared engine code and test helpers the flows run + # through, and the toolchain files. A main push outside this list waits + # for the nightly run below. + paths: + - '.github/workflows/bazel-npm.yml' + - '.github/workflows/bazel-compatibility.yml' + - 'scripts/install-bazel.sh' + - 'scripts/bazel-pins.txt' + - 'crates/socket-patch-cli/tests/e2e_bazel_build/npm.rs' + - 'crates/socket-patch-cli/tests/e2e_bazel_build/harness.rs' + - 'crates/socket-patch-cli/tests/fixtures/bazel/rules_js/**' + - 'crates/socket-patch-core/src/**/*pnpm*' + - 'crates/socket-patch-core/src/**/*pnpm*/**' + - 'crates/socket-patch-core/src/vendor/npm_*' + - 'crates/socket-patch-core/src/vendor/lock_inventory/npm_family.rs' + - 'crates/socket-patch-core/src/patch/redirect/npmrc.rs' + - 'crates/socket-patch-core/src/patch/redirect/upstream/npm.rs' + - 'crates/socket-patch-core/src/vex/discover/npm.rs' + - 'crates/socket-patch-core/src/crawlers/npm_crawler.rs' + - 'crates/socket-patch-core/src/formats/package_json.rs' + - 'crates/socket-patch-cli/tests/e2e_bazel_build/main.rs' + - 'crates/socket-patch-core/src/patch/redirect/**' + - 'crates/socket-patch-core/src/vendor/**' + - 'crates/socket-patch-core/src/hosted/**' + - 'crates/socket-patch-cli/src/commands/**' + - 'crates/socket-patch-cli/tests/common/**' + - 'crates/socket-patch-cli/tests/hosted_memory_common/**' + - 'Cargo.lock' + - 'Cargo.toml' + - 'crates/*/Cargo.toml' + - 'rust-toolchain.toml' + - '.cargo/**' + schedule: + # Nightly, so a main change outside the push list still gets a run + # within a day. The macOS leg runs here and on demand only. + - cron: '27 4 * * *' + workflow_dispatch: + +permissions: + contents: read + +# The nightly run has a group of its own. A group holds one pending run, so +# in the group of main's pushes a second push would replace a nightly that +# waits behind the first, and the nightly is the only run with the macOS leg. +concurrency: + group: bazel-npm-${{ github.event.pull_request.number || github.ref }}${{ github.event_name == 'schedule' && '-nightly' || '' }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +jobs: + npm: + uses: ./.github/workflows/bazel-compatibility.yml + with: + eco: npm + fixtures: rules_js/bzlmod + # One leg per line: a Bazel release, and the era of the fixture that + # `npm::FIXTURE` gives it. + releases: >- + [ + {"bazel": "9.3.2", "era": "e9"}, + {"bazel": "8.8.2", "era": "e9"}, + {"bazel": "7.7.2", "era": "e9"} + ] + macos: >- + [ + {"bazel": "9.3.2", "era": "e9"} + ] diff --git a/crates/socket-patch-cli/tests/e2e_bazel_build/main.rs b/crates/socket-patch-cli/tests/e2e_bazel_build/main.rs index 23dd4ae28..1c061fc4c 100644 --- a/crates/socket-patch-cli/tests/e2e_bazel_build/main.rs +++ b/crates/socket-patch-cli/tests/e2e_bazel_build/main.rs @@ -49,3 +49,4 @@ mod hosted_memory_common; mod go; mod harness; +mod npm; diff --git a/crates/socket-patch-cli/tests/e2e_bazel_build/npm.rs b/crates/socket-patch-cli/tests/e2e_bazel_build/npm.rs new file mode 100644 index 000000000..e088c638d --- /dev/null +++ b/crates/socket-patch-cli/tests/e2e_bazel_build/npm.rs @@ -0,0 +1,602 @@ +//! Hosted and vendored mode for an npm package that aspect_rules_js links +//! from a pnpm lock: `npm.npm_translate_lock(pnpm_lock = "//:pnpm-lock.yaml")` +//! under Bzlmod, with a lock of `lockfileVersion: '9.0'`. +//! +//! rules_js reads the lock itself. Every entry of `packages:` becomes one +//! repository, which Bazel's downloader fetches from the entry's `tarball` +//! (the registry's URL for the name and version when it has none, a path +//! beside the lock for a `file:` value) and holds to its `integrity`. No +//! pnpm runs, no `node_modules` of the checkout is read, and the machine +//! needs no Node: the ruleset downloads the one that runs the consumer. +//! +//! Two flows, one test each, on the Bazel the run is given ([`FIXTURE`] +//! says which ruleset goes with its release), each in a fresh output base: +//! +//! - (a) `bazel build` and `bazel run //:consumer` print the unpatched +//! marker, and Bazel fetched the upstream tarball from the stand-in +//! registry. +//! - (b) The one command of the mode writes its files and no other. +//! `scan --mode hosted --yes` gives the package's entry of +//! `pnpm-lock.yaml` the integrity of the patched tarball and its URL on +//! the patch host, and creates `pnpm-workspace.yaml` with the +//! `trustLockfile: true` that pnpm 11 and later ask for. +//! `scan --mode vendored --yes` downloads the patched tarball to +//! `.socket/vendor/npm//`, records it in the ledger +//! `.socket/vendor/state.json`, and points the lock's entry and a pnpm +//! override (in `package.json` and in a new `pnpm-workspace.yaml`) at the +//! tarball. +//! - (c) The same two Bazel commands, with no step in between, print the +//! patched marker; Bazel changed no file of the checkout, and a +//! `--lockfile_mode=error` build passes. Hosted: Bazel fetched the +//! tarball from the stand-in patch host. Vendored: the scan did, and +//! Bazel read the committed file without a request to the stand-in. +//! - (d) The undo (`rollback --yes`, or `vendor --revert`): every file of +//! the checkout is the one of before, byte for byte, and the build prints +//! the unpatched marker again. +//! - (e) On a second machine (a new output base and, on Bazel 9, a new +//! contents cache) the command runs against a second patch, and the +//! pinned place holds other bytes: the patch host serves them (hosted), +//! or they replace the committed file (vendored). The build fails on the +//! checksum and does not take the registry's tarball in its place. +//! +//! A marker is read from what Bazel built. Bazel's downloader keeps every +//! file it fetches in the repository cache, under its checksum, and hands it +//! out again without a request; the tests of a leg share that cache, and CI +//! restores it from earlier runs. So every tarball of a run is new. The +//! entry file of the upstream package and of each patch holds an id made +//! for the run, which the marker shows, and the lock of the workspace copy +//! gets the upstream `integrity` from the bytes the run serves +//! ([`lock_pinning`]); the fixture's own lock holds a placeholder there. +//! Step (a) has to fetch the upstream tarball then, and counts the +//! requests: a downloader rewrite or a registry route that stopped working +//! fails it on every run, not on an empty cache alone. +//! +//! One loopback server per checkout (the second machine of (e) has its own) +//! stands in for three hosts, by route prefix: the Socket API (`/v0`), the +//! patch host (`/patch`) and `registry.npmjs.org` (the upstream tarball, and +//! the version document `rollback` reads through `SOCKET_NPM_REGISTRY`). The +//! package is the tests' own, under this organisation's scope ([`NAME`]). +//! Its lock entry names no URL, as pnpm writes one, so rules_js asks for it +//! at the public registry's URL; the downloader config of the cell rewrites +//! that URL to the stand-in ([`route_registry`]), as a user behind a registry +//! mirror has it. + +use std::time::Instant; + +use base64::Engine as _; +use serde_json::{json, Value}; +use sha2::{Digest, Sha512}; +use socket_patch_core::hash::git_sha256::compute_git_sha256_from_bytes; + +use crate::harness::{ + changed, fixture_path, rewrite, Cell, Fixture, Ran, Run, StandIn, Tree, CONSUMER, +}; +use crate::hosted_memory_common::{Patch, ORG}; + +/// The workspace the tests build, and the ruleset each Bazel release builds +/// it with. +const FIXTURE: Fixture = Fixture { + name: "npm::FIXTURE", + dir: "rules_js/bzlmod", + workflow: "bazel-npm.yml", + eras: &[ + // aspect_rules_js 3.6.0, which asks for Bazel 7.6.0 or later. + ((7, 6, 0)..(10, 0, 0), "e9"), + ], +}; + +/// The package the fixture depends on, at the version its lock pins. The +/// scope is this organisation's on the public registry, so nobody else can +/// publish the name: a build or a pnpm that gets past the stand-in finds no +/// stranger's package there. +const NAME: &str = "@socketsecurity/bazel-fixture-upstream"; +const VERSION: &str = "1.0.0"; +/// With `%40` for the `@` of the scope, as this crate's tests spell the +/// Socket API's purl of a scoped package. `scan` takes either spelling and +/// names a patch in its events as the API did. +const PURL: &str = "pkg:npm/%40socketsecurity/bazel-fixture-upstream@1.0.0"; +/// The file name of its tarball on the registry and on the patch host: the +/// name without its scope. +const TGZ: &str = "bazel-fixture-upstream-1.0.0.tgz"; +/// Its `package.json`. The other file of a tarball is the entry file. +const MANIFEST: &str = "{\"name\": \"@socketsecurity/bazel-fixture-upstream\", \ + \"version\": \"1.0.0\", \"main\": \"index.js\"}\n"; +/// The entry file of the other bytes a build finds at the pinned place. +const TAMPERED_INDEX: &str = "module.exports = \"TAMPERED\";\n"; +/// The lock rules_js reads. +const LOCK: &str = "pnpm-lock.yaml"; +/// The route prefix under which the stand-in plays the npm registry, and +/// the route of the upstream tarball there. +const REGISTRY: &str = "registry.npmjs.org"; +const UPSTREAM_TGZ: &str = + "/registry.npmjs.org/@socketsecurity/bazel-fixture-upstream/-/bazel-fixture-upstream-1.0.0.tgz"; +/// The access-token level of a hosted tarball's URL, which `socket-patch` +/// writes as the API hands it out. +const TOKEN: &str = "22222222-2222-4222-8222-222222222222"; +/// The `pnpm-workspace.yaml` hosted mode creates beside a 9.0 lock: the +/// root as the one package, and the setting without which pnpm 11 and later +/// reject a lock whose tarball URL is not the registry's. +const TRUSTING_WORKSPACE: &str = "packages:\n - '.'\ntrustLockfile: true\n"; + +/// The `pnpm-workspace.yaml` vendored mode creates beside a 9.0 lock, which +/// rules_js reads: the root as the one package, and the override that sends +/// the package's one version to the committed tarball at `copy`. +fn overriding_workspace(copy: &str) -> String { + format!("packages:\n - '.'\noverrides:\n '{NAME}@{VERSION}': file:{copy}\n") +} + +/// The package with `index_js` as its entry file, as a registry tarball. +/// Every sum a test holds a build to is computed from the bytes it serves, +/// so nothing depends on the exact bytes the archive crates emit. +fn tarball(index_js: &str) -> Vec { + let gzip = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default()); + let mut archive = tar::Builder::new(gzip); + for (name, text) in [ + ("package/package.json", MANIFEST), + ("package/index.js", index_js), + ] { + let mut header = tar::Header::new_ustar(); + header.set_size(text.len() as u64); + header.set_mode(0o644); + archive + .append_data(&mut header, name, text.as_bytes()) + .expect("tar"); + } + let gzip = archive.into_inner().expect("tar"); + gzip.finish().expect("gzip") +} + +/// A sha512 as a pnpm lock and Bazel spell an `integrity`. +fn sri(sha512: &[u8]) -> String { + let digest = base64::engine::general_purpose::STANDARD.encode(sha512); + format!("sha512-{digest}") +} + +/// The `integrity` of a tarball. +fn integrity(tarball: &[u8]) -> String { + sri(&Sha512::digest(tarball)) +} + +/// One build of the package: an entry file that exports ` `, the +/// tarball of it, and the line the consumer prints once Bazel built that +/// tarball in. The id is new for every run, so no cache holds the tarball. +struct Package { + index_js: String, + tarball: Vec, + marker: String, +} + +impl Package { + fn new(word: &str, id: &str) -> Package { + let index_js = format!("module.exports = \"{word} {id}\";\n"); + Package { + tarball: tarball(&index_js), + marker: format!("OUT: {word} {id}"), + index_js, + } + } + + /// The package as the registry of one run has it. + fn upstream() -> Package { + Package::new("PRISTINE", &uuid::Uuid::new_v4().to_string()) + } +} + +/// One patch of the package, under an id that is new for every run. +struct Patched { + uuid: String, + package: Package, + /// The route of the tarball on the patch host. + route: String, +} + +impl Patched { + fn new() -> Patched { + let uuid = uuid::Uuid::new_v4().to_string(); + Patched { + package: Package::new("PATCHED", &uuid), + route: format!("/patch/npm/{NAME}/{VERSION}/{TOKEN}/{uuid}/{TGZ}"), + uuid, + } + } + + /// Where vendored mode commits the tarball: in the patch's directory, + /// under the scope of the name. + fn vendored(&self) -> String { + format!(".socket/vendor/npm/{}/{NAME}-{VERSION}.tgz", self.uuid) + } +} + +/// The entry of a lock that pins the package to the tarball of `integrity` +/// and names no URL, as pnpm writes the entry of a registry package (in +/// quotes: the key starts with `@`). +fn lock_entry(integrity: &str) -> String { + format!(" '{NAME}@{VERSION}':\n resolution: {{integrity: {integrity}}}\n") +} + +/// The `integrity` of the package in the fixture's own lock, which is the +/// sum of no tarball: sixty-four zero bytes. +fn placeholder() -> String { + sri(&[0; 64]) +} + +/// The fixture's lock with `upstream` in place of the placeholder: the lock +/// a test writes into its workspace copy before the first build. +fn lock_pinning(upstream: &Package) -> String { + let path = fixture_path(FIXTURE.dir).join("common").join(LOCK); + let lock = std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display())); + let unpinned = lock_entry(&placeholder()); + let entries = lock.matches(&unpinned).count(); + assert_eq!(entries, 1, "{}: not one\n{unpinned}", path.display()); + lock.replace(&unpinned, &lock_entry(&integrity(&upstream.tarball))) +} + +/// A stand-in that holds `upstream` as the registry does: the tarball, and +/// the version document with its integrity and the URL the public registry +/// would have it at. +fn registry(upstream: &Package) -> StandIn { + let standin = StandIn::start(); + let document = json!({ + "name": NAME, + "version": VERSION, + "dist": { + "tarball": format!("https://{REGISTRY}/{NAME}/-/{TGZ}"), + "integrity": integrity(&upstream.tarball), + }, + }); + let document = serde_json::to_vec(&document).expect("a JSON document"); + // `rollback` asks for the document of a scoped name with `%2f` for `/`. + let named = NAME.replace('/', "%2f"); + standin.serve(5, &format!("/{REGISTRY}/{named}/{VERSION}"), &document); + standin.serve(5, UPSTREAM_TGZ, &upstream.tarball); + standin +} + +/// The Socket API on `standin` for `patch` of `upstream`, free for every +/// caller, and the patch host with `served` at the URL the API hands out +/// for its tarball. The API names the sha512 of the patch's own tarball, +/// whatever is served. +fn grant(standin: &StandIn, upstream: &Package, patch: &Patched, served: &[u8]) { + let url = format!("{}{}", standin.origin(), patch.route); + let reference = json!({ + "status": "granted", + "url": url, + "purl": PURL, + "artifacts": [{ + "kind": "tarball", + "url": url, + "integrity": { "sha512": integrity(&patch.package.tarball) }, + }], + "registryOverride": null, + }); + let granted = Patch { + purl: PURL.to_string(), + uuid: patch.uuid.clone(), + reference, + }; + standin.api(&[granted]); + // Priority 1: ahead of the shared API mock's patch record, whose hashes + // are fixed, because vendored mode holds the downloaded file to `afterHash`. + let record = json!({ + "uuid": patch.uuid, + "purl": PURL, + "publishedAt": "2026-01-01T00:00:00Z", + "files": { "package/index.js": { + "beforeHash": compute_git_sha256_from_bytes(upstream.index_js.as_bytes()), + "afterHash": compute_git_sha256_from_bytes(patch.package.index_js.as_bytes()), + }}, + "vulnerabilities": { "GHSA-test-aaaa-bbbb": { + "cves": ["CVE-2024-0001"], "summary": "s", "severity": "high", "description": "d", + }}, + "description": "fixture", + "license": "MIT", + "tier": "free", + }); + let record = serde_json::to_vec(&record).expect("a JSON document"); + let record_route = format!("/v0/orgs/{ORG}/patches/view/{}", patch.uuid); + standin.serve(1, &record_route, &record); + standin.serve(5, &patch.route, served); +} + +/// Send Bazel's downloader to `standin` for the registry's files of the +/// package. Every other URL stays as it is, the patch host's included, +/// which a lock names at the stand-in's own origin. +fn route_registry(cell: &mut Cell, standin: &StandIn) { + let files = format!("{REGISTRY}/{NAME}/"); + cell.route_downloads(&[rewrite(&files, &standin.origin())]); +} + +/// The checkout of the fixture for `test`, the upstream package of this run, +/// which the lock of the checkout pins, and the stand-in that holds it, +/// which the builds and the commands of the checkout talk to. `None` after a +/// `SKIP` line when no Bazel runs here. +fn open(test: &str) -> Option<(Cell, StandIn, Package)> { + let mut cell = Cell::open(test, &FIXTURE)?; + let upstream = Package::upstream(); + cell.write(LOCK, lock_pinning(&upstream)); + let standin = registry(&upstream); + route_registry(&mut cell, &standin); + Some((cell, standin, upstream)) +} + +/// `socket-patch ` in `cell`, with `standin` as the Socket API, as +/// the patch server (a hosted pin is recognised by that origin) and as the +/// registry `rollback` reads the upstream entry from. +fn socket_patch(cell: &Cell, standin: &StandIn, args: &[&str]) -> Ran { + let origin = standin.origin(); + let registry = format!("{origin}/{REGISTRY}"); + let env = [ + ("SOCKET_PATCH_SERVER_URL", origin.as_str()), + ("SOCKET_NPM_REGISTRY", registry.as_str()), + ]; + cell.socket_patch(&origin, args, &env) +} + +/// A text file of a tree. +fn text(tree: &Tree, rel: &str) -> String { + String::from_utf8(tree[rel].clone()).expect("a text file") +} + +/// Step (a): the build of `cell` prints the marker of `upstream`, and Bazel +/// asked `standin` for that tarball, which no cache holds. Gives the files +/// of the checkout. +fn builds_upstream(cell: &Cell, standin: &StandIn, upstream: &Package) -> Tree { + let before = cell.builds_unpatched(&upstream.marker); + assert!( + standin.asked(UPSTREAM_TGZ) > 0, + "{}: the build did not fetch the upstream tarball from the stand-in registry", + cell.test + ); + before +} + +/// Step (e), up to its build: a second machine with the checkout of `cell` +/// and a stand-in of its own, which its Bazel and its commands talk to; the +/// patch `second`, whose URL on that patch host answers with `served`; and +/// `scan --mode --yes` there, which applies it. +fn second_machine( + cell: &Cell, + upstream: &Package, + mode: &str, + second: &Patched, + served: &[u8], +) -> (Cell, StandIn) { + let mut twin = cell.twin(); + let other = registry(upstream); + route_registry(&mut twin, &other); + grant(&other, upstream, second, served); + let scan = socket_patch(&twin, &other, &["scan", "--mode", mode, "--yes"]); + let (test, said) = (&twin.test, &scan.dump); + assert_eq!(scan.code, 0, "{test}: {said}"); + assert_eq!(scan.envelope["summary"]["applied"], 1, "{test}: {said}"); + (twin, other) +} + +/// Step (e): the build of `twin` fails because Bazel finds the sha512 of +/// `found` where the lock pins that of `pinned`, and the registry is not +/// asked for the upstream tarball instead. Gives the build and the line it +/// failed with. +fn refuses(twin: &Cell, other: &StandIn, found: &[u8], pinned: &[u8]) -> (Run, String) { + let test = &twin.test; + let build = twin.bazel("build", &[CONSUMER]); + assert!(!build.ok(), "{test}: other bytes built:\n{}", build.dump()); + let refusal = format!( + "Checksum was {} but wanted {}", + integrity(found), + integrity(pinned) + ); + assert!( + build.stderr().contains(&refusal), + "{test}: the build did not fail with {refusal:?}:\n{}", + build.dump() + ); + assert_eq!( + other.asked(UPSTREAM_TGZ), + 0, + "{test}: the build went to the upstream tarball" + ); + (build, refusal) +} + +/// The hosted flow of the module docs. +#[test] +#[ignore = "real Bazel; run with --ignored"] +fn hosted_npm_translate_lock() { + let test = "npm::hosted_npm_translate_lock"; + let started = Instant::now(); + let Some((cell, standin, upstream)) = open(test) else { + return; + }; + let release = cell.release().to_string(); + let origin = standin.origin(); + let patch = Patched::new(); + grant(&standin, &upstream, &patch, &patch.package.tarball); + + // (a) The unpatched build, with the upstream tarball from the stand-in. + let before = builds_upstream(&cell, &standin, &upstream); + + // (b) The one command of hosted mode. It rewrites those two files and + // no other: the entry of the package names the patched tarball, by its + // sum and by its URL on the patch host. + let scan = socket_patch(&cell, &standin, &["scan", "--mode", "hosted", "--yes"]); + let written = [LOCK, "pnpm-workspace.yaml"]; + let pinned = cell.pinned_by(&scan, PURL, &patch.uuid, &before, &written); + let lock = text(&pinned, LOCK); + let entry = format!( + " '{NAME}@{VERSION}':\n resolution: {{integrity: {}, tarball: {origin}{}}}\n", + integrity(&patch.package.tarball), + patch.route + ); + assert!(lock.contains(&entry), "{test}: {LOCK}:\n{lock}"); + let workspace = text(&pinned, "pnpm-workspace.yaml"); + assert_eq!(workspace, TRUSTING_WORKSPACE, "{test}"); + + // (c) The same build, no step in between: patched, from the patch host. + let patched_fetches = standin.asked(&patch.route); + cell.builds_patched(&pinned, &patch.package.marker); + assert!( + standin.asked(&patch.route) > patched_fetches, + "{test}: the patched build did not fetch the tarball from the stand-in patch host" + ); + + // (d) The undo: every file is the one of before, and so is the build. + let undo = socket_patch(&cell, &standin, &["rollback", "--yes"]); + cell.undone(&undo, "rolledBack", &before, &upstream.marker); + + // (e) A second machine, a second patch, and a patch host that serves + // other bytes at the URL the lock pins: still a tarball of the package, + // with another sum. + let second = Patched::new(); + let tampered = tarball(TAMPERED_INDEX); + let (twin, other) = second_machine(&cell, &upstream, "hosted", &second, &tampered); + let (_, refusal) = refuses(&twin, &other, &tampered, &second.package.tarball); + assert!( + other.asked(&second.route) > 0, + "{test}: the patch host was not asked, so the failed build proves nothing" + ); + + println!( + "{test}: Bazel {release} built {:?} from {origin}{} after `scan --mode hosted`; `rollback` \ + restored all {} files and the {:?} build; other bytes at the patch host failed a second \ + machine's build ({refusal}); the stand-in answered {} requests for the upstream tarball \ + and {} for the patched one; {:?}", + patch.package.marker, + patch.route, + before.len(), + upstream.marker, + standin.asked(UPSTREAM_TGZ), + standin.asked(&patch.route), + started.elapsed() + ); +} + +/// The vendored flow of the module docs. +#[test] +#[ignore = "real Bazel; run with --ignored"] +fn vendored_npm_translate_lock() { + let test = "npm::vendored_npm_translate_lock"; + let started = Instant::now(); + let Some((cell, standin, upstream)) = open(test) else { + return; + }; + let release = cell.release().to_string(); + let patch = Patched::new(); + grant(&standin, &upstream, &patch, &patch.package.tarball); + + // (a) The unpatched build, with the upstream tarball from the stand-in. + let before = builds_upstream(&cell, &standin, &upstream); + + // (b) The one command of vendored mode. + let scan = socket_patch(&cell, &standin, &["scan", "--mode", "vendored", "--yes"]); + let said = &scan.dump; + assert_eq!(scan.code, 0, "{test}: {said}"); + assert_eq!(scan.envelope["status"], "success", "{test}: {said}"); + assert_eq!(scan.envelope["summary"]["downloaded"], 1, "{test}: {said}"); + assert_eq!(scan.envelope["summary"]["applied"], 1, "{test}: {said}"); + // The tarball and its ledger under `.socket/vendor`, the three files + // that wire it in, and no other. + let dir = format!(".socket/vendor/npm/{}", patch.uuid); + let copy = patch.vendored(); + let written = [ + format!("{dir}/.gitattributes"), + format!("{dir}/.gitignore"), + copy.clone(), + format!("{dir}/socket-patch.vendor.json"), + ".socket/vendor/state.json".to_string(), + "package.json".to_string(), + LOCK.to_string(), + "pnpm-workspace.yaml".to_string(), + ]; + let vendored = cell.tree(); + assert_eq!(changed(&before, &vendored), written, "{test}: {said}"); + // The committed tarball is the one the patch host served; the lock + // names it by path and by sum, and the override sends the package's one + // version there. + let committed = vendored[©] == patch.package.tarball; + assert!(committed, "{test}: {copy} differs"); + assert!(standin.asked(&patch.route) > 0, "{test}: {said}"); + let lock = text(&vendored, LOCK); + let entry = format!( + " '{NAME}@file:{copy}':\n resolution: {{integrity: {}, tarball: file:{copy}}}\n", + integrity(&patch.package.tarball) + ); + assert!(lock.contains(&entry), "{test}: {LOCK}:\n{lock}"); + let manifest: Value = serde_json::from_slice(&vendored["package.json"]).expect("package.json"); + let overridden = &manifest["pnpm"]["overrides"][format!("{NAME}@{VERSION}")]; + assert_eq!(*overridden, format!("file:{copy}"), "{test}: {manifest:#}"); + let workspace = text(&vendored, "pnpm-workspace.yaml"); + assert_eq!(workspace, overriding_workspace(©), "{test}"); + + // (c) The same build, no step in between: patched, from the committed + // file. Bazel asks the stand-in for nothing. + let requests = standin.requests(); + cell.builds_patched(&vendored, &patch.package.marker); + assert_eq!(standin.requests(), requests, "{test}: Bazel asked a host"); + + // (d) The undo: every file is the one of before, and so is the build. + let undo = socket_patch(&cell, &standin, &["vendor", "--revert"]); + cell.undone(&undo, "removed", &before, &upstream.marker); + + // (e) A second machine and a second patch, vendored there, and then + // other bytes in the committed file: still a tarball of the package, + // with another sum. + let second = Patched::new(); + let served = &second.package.tarball; + let (twin, other) = second_machine(&cell, &upstream, "vendored", &second, served); + let second_copy = second.vendored(); + let committed = twin.tree().contains_key(&second_copy); + assert!(committed, "{test}: no {second_copy}"); + let tampered = tarball(TAMPERED_INDEX); + twin.write(&second_copy, &tampered); + let (build, refusal) = refuses(&twin, &other, &tampered, &second.package.tarball); + assert!( + build.stderr().contains(&format!("file:{second_copy}")), + "{test}: the build did not fail on the committed tarball:\n{}", + build.dump() + ); + + println!( + "{test}: Bazel {release} built {:?} from {copy} after `scan --mode vendored`, with no \ + request to the stand-in; `vendor --revert` restored all {} files and the {:?} build; \ + other bytes in the committed tarball failed a second machine's build ({refusal}); the \ + stand-in answered {} requests for the upstream tarball and {} for the patched one; {:?}", + patch.package.marker, + before.len(), + upstream.marker, + standin.asked(UPSTREAM_TGZ), + standin.asked(&patch.route), + started.elapsed() + ); +} + +/// Without a Bazel, what stays fixed now that no sum is committed: the +/// fixture's directories and the legs of its workflow are those of its +/// table; the fixture's lock holds the one entry a test pins, with the +/// placeholder and no URL, and pinning changes nothing else; the fixture +/// depends on the package at the version of that entry; two runs serve two +/// upstream tarballs; and the names written out above are those of the one +/// package, under the organisation's scope. +#[test] +fn a_test_pins_the_one_entry_of_the_fixture_lock() { + FIXTURE.check(); + let common = fixture_path(FIXTURE.dir).join("common"); + let read = |rel: &str| std::fs::read_to_string(common.join(rel)).expect(rel); + + let upstream = Package::upstream(); + let pinned = lock_pinning(&upstream); + let entry = lock_entry(&integrity(&upstream.tarball)); + assert!(pinned.contains(&entry), "no\n{entry}in\n{pinned}"); + let unpinned = pinned.replace(&entry, &lock_entry(&placeholder())); + assert_eq!(unpinned, read(LOCK)); + assert_ne!(Package::upstream().tarball, upstream.tarball); + + let fixture: Value = serde_json::from_str(&read("package.json")).expect("package.json"); + assert_eq!(fixture["dependencies"][NAME], VERSION); + let manifest: Value = serde_json::from_str(MANIFEST).expect("the package's package.json"); + assert_eq!(manifest["name"], NAME); + assert_eq!(manifest["version"], VERSION); + let bare = NAME.strip_prefix("@socketsecurity/").expect("the scope"); + assert_eq!(PURL, format!("pkg:npm/%40socketsecurity/{bare}@{VERSION}")); + assert_eq!(TGZ, format!("{bare}-{VERSION}.tgz")); + assert_eq!(UPSTREAM_TGZ, format!("/{REGISTRY}/{NAME}/-/{TGZ}")); +} diff --git a/crates/socket-patch-cli/tests/fixtures/bazel/rules_js/bzlmod/common/.bazelignore b/crates/socket-patch-cli/tests/fixtures/bazel/rules_js/bzlmod/common/.bazelignore new file mode 100644 index 000000000..3c3629e64 --- /dev/null +++ b/crates/socket-patch-cli/tests/fixtures/bazel/rules_js/bzlmod/common/.bazelignore @@ -0,0 +1 @@ +node_modules diff --git a/crates/socket-patch-cli/tests/fixtures/bazel/rules_js/bzlmod/common/BUILD.bazel b/crates/socket-patch-cli/tests/fixtures/bazel/rules_js/bzlmod/common/BUILD.bazel new file mode 100644 index 000000000..77da037e3 --- /dev/null +++ b/crates/socket-patch-cli/tests/fixtures/bazel/rules_js/bzlmod/common/BUILD.bazel @@ -0,0 +1,12 @@ +load("@aspect_rules_js//js:defs.bzl", "js_binary") +load("@npm//:defs.bzl", "npm_link_all_packages") + +npm_link_all_packages(name = "node_modules") + +# Prints `OUT: PRISTINE ` until the package is patched: a test builds the +# package anew, with an id of its own in the entry file. +js_binary( + name = "consumer", + data = [":node_modules/@socketsecurity/bazel-fixture-upstream"], + entry_point = "consumer.js", +) diff --git a/crates/socket-patch-cli/tests/fixtures/bazel/rules_js/bzlmod/common/consumer.js b/crates/socket-patch-cli/tests/fixtures/bazel/rules_js/bzlmod/common/consumer.js new file mode 100644 index 000000000..eb9730ede --- /dev/null +++ b/crates/socket-patch-cli/tests/fixtures/bazel/rules_js/bzlmod/common/consumer.js @@ -0,0 +1 @@ +console.log("OUT: " + require("@socketsecurity/bazel-fixture-upstream")); diff --git a/crates/socket-patch-cli/tests/fixtures/bazel/rules_js/bzlmod/common/package.json b/crates/socket-patch-cli/tests/fixtures/bazel/rules_js/bzlmod/common/package.json new file mode 100644 index 000000000..f5b6c3753 --- /dev/null +++ b/crates/socket-patch-cli/tests/fixtures/bazel/rules_js/bzlmod/common/package.json @@ -0,0 +1,11 @@ +{ + "name": "socket-fixture", + "version": "0.0.0", + "private": true, + "dependencies": { + "@socketsecurity/bazel-fixture-upstream": "1.0.0" + }, + "pnpm": { + "onlyBuiltDependencies": [] + } +} diff --git a/crates/socket-patch-cli/tests/fixtures/bazel/rules_js/bzlmod/common/pnpm-lock.yaml b/crates/socket-patch-cli/tests/fixtures/bazel/rules_js/bzlmod/common/pnpm-lock.yaml new file mode 100644 index 000000000..a2b2ffd26 --- /dev/null +++ b/crates/socket-patch-cli/tests/fixtures/bazel/rules_js/bzlmod/common/pnpm-lock.yaml @@ -0,0 +1,22 @@ +lockfileVersion: '9.0' + +settings: + autoInstallPeers: true + excludeLinksFromLockfile: false + +importers: + + .: + dependencies: + '@socketsecurity/bazel-fixture-upstream': + specifier: 1.0.0 + version: 1.0.0 + +packages: + + '@socketsecurity/bazel-fixture-upstream@1.0.0': + resolution: {integrity: sha512-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==} + +snapshots: + + '@socketsecurity/bazel-fixture-upstream@1.0.0': {} diff --git a/crates/socket-patch-cli/tests/fixtures/bazel/rules_js/bzlmod/e9/MODULE.bazel b/crates/socket-patch-cli/tests/fixtures/bazel/rules_js/bzlmod/e9/MODULE.bazel new file mode 100644 index 000000000..b92619630 --- /dev/null +++ b/crates/socket-patch-cli/tests/fixtures/bazel/rules_js/bzlmod/e9/MODULE.bazel @@ -0,0 +1,11 @@ +module(name = "socket_fixture", version = "0.0.0") + +bazel_dep(name = "aspect_rules_js", version = "3.6.0") +single_version_override(module_name = "aspect_rules_js", version = "3.6.0") + +npm = use_extension("@aspect_rules_js//npm:extensions.bzl", "npm") +npm.npm_translate_lock( + name = "npm", + pnpm_lock = "//:pnpm-lock.yaml", +) +use_repo(npm, "npm")