mirror of
https://github.com/bitbaum/fleet.git
synced 2026-12-05 10:27:21 +01:00
fix(receipts): attest verified local packages by owning commit (#302)
This commit is contained in:
3 files changed
+367
-35
No files matched your search
@@ -116,8 +116,16 @@ and unpublished repository versions never establish the current target.
|
||||
`scripts/ci/release-receipt.mjs` records the clean checked-out commit, the selected
|
||||
lockfile digest, and the canonical locked/installed first-party package versions.
|
||||
It refuses a dirty or untracked source tree, missing installed packages,
|
||||
mutable workspace/branch references, unsupported locks and mismatched npm
|
||||
versions. It uses the actual Git checkout rather than the workflow run's label.
|
||||
unverified workspace links, mutable branch references, unsupported locks and
|
||||
mismatched npm versions. It uses the actual Git checkout rather than the
|
||||
workflow run's label.
|
||||
|
||||
A pnpm `workspace:` dependency locked to `link:` may be attested as the owning
|
||||
repository's immutable Git commit. The CLI checks that the installed package's
|
||||
real filesystem target is inside the clean checkout, its manifest is tracked,
|
||||
and its name/version and source path match the locked link. A version number
|
||||
alone cannot identify a local package. External links, wrong targets and dirty
|
||||
workspace source still fail; no raw local path is published.
|
||||
|
||||
The deployment integration writes the result into the staged public directory
|
||||
as `/.well-known/fleet-release.json`. It contains no environment, user data,
|
||||
|
||||
+117
-21
@@ -1,5 +1,11 @@
|
||||
import { readFileSync, existsSync, mkdirSync, writeFileSync } from "node:fs";
|
||||
import { resolve, relative, dirname } from "node:path";
|
||||
import {
|
||||
readFileSync,
|
||||
existsSync,
|
||||
mkdirSync,
|
||||
writeFileSync,
|
||||
realpathSync,
|
||||
} from "node:fs";
|
||||
import { resolve, relative, dirname, posix } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
@@ -33,7 +39,8 @@ const isProjectPath = (path) =>
|
||||
typeof path === "string" &&
|
||||
!path.startsWith("/") &&
|
||||
!/[\n\r\\]/.test(path) &&
|
||||
(path === "." || path.split("/").every((part) => part && part !== "." && part !== ".."));
|
||||
(path === "." ||
|
||||
path.split("/").every((part) => part && part !== "." && part !== ".."));
|
||||
/** Only public first-party package metadata; no environment, paths, or product data. */
|
||||
export function createReleaseReceipt({
|
||||
repo,
|
||||
@@ -46,6 +53,7 @@ export function createReleaseReceipt({
|
||||
manifests = {},
|
||||
lockImporters = {},
|
||||
installed = {},
|
||||
workspacePackages = {},
|
||||
createdAt = new Date().toISOString(),
|
||||
}) {
|
||||
if (
|
||||
@@ -61,15 +69,28 @@ export function createReleaseReceipt({
|
||||
importers.some((path) => !isProjectPath(path)) ||
|
||||
new Set(importers).size !== importers.length
|
||||
)
|
||||
throw new Error("Receipt importers must be project paths inside the checkout");
|
||||
const manifestsByPath = Object.fromEntries(importers.map((path) => [path, path === importer ? manifest : manifests[path]]));
|
||||
throw new Error(
|
||||
"Receipt importers must be project paths inside the checkout",
|
||||
);
|
||||
const manifestsByPath = Object.fromEntries(
|
||||
importers.map((path) => [
|
||||
path,
|
||||
path === importer ? manifest : manifests[path],
|
||||
]),
|
||||
);
|
||||
const wanted = [];
|
||||
for (let i = 0; i < importers.length; i++) {
|
||||
const path = importers[i];
|
||||
const current = manifestsByPath[path];
|
||||
if (!current) throw new Error(`${path}: project manifest is required for release receipt`);
|
||||
const importerDependencies = Object.entries(dependencies(current)).filter(([name, spec]) => isShared(dependencyIdentity(name, spec)));
|
||||
for (const [depName, spec] of importerDependencies) wanted.push({ depName, spec, importer: path });
|
||||
if (!current)
|
||||
throw new Error(
|
||||
`${path}: project manifest is required for release receipt`,
|
||||
);
|
||||
const importerDependencies = Object.entries(dependencies(current)).filter(
|
||||
([name, spec]) => isShared(dependencyIdentity(name, spec)),
|
||||
);
|
||||
for (const [depName, spec] of importerDependencies)
|
||||
wanted.push({ depName, spec, importer: path });
|
||||
}
|
||||
if (wanted.length && !pnpmLock && !npmLock)
|
||||
throw new Error("Shared packages require a supported lockfile");
|
||||
@@ -78,16 +99,47 @@ export function createReleaseReceipt({
|
||||
for (const { depName, spec, importer: currentImporter } of wanted) {
|
||||
const lockImporter = lockImporters[currentImporter] ?? currentImporter;
|
||||
if (!isProjectPath(lockImporter))
|
||||
throw new Error(`${currentImporter}: project is outside the selected lock root`);
|
||||
throw new Error(
|
||||
`${currentImporter}: project is outside the selected lock root`,
|
||||
);
|
||||
const name = dependencyIdentity(depName, spec),
|
||||
currentManifest = manifestsByPath[currentImporter],
|
||||
locked = pnpmLock
|
||||
? lockedDependencies(currentManifest, pnpmLock, lockImporter)
|
||||
: npmLock
|
||||
? lockedNpmDependencies(currentManifest, JSON.parse(npmLock), lockImporter)
|
||||
? lockedNpmDependencies(
|
||||
currentManifest,
|
||||
JSON.parse(npmLock),
|
||||
lockImporter,
|
||||
)
|
||||
: {},
|
||||
artifact = locked[name],
|
||||
actual = installed[depName];
|
||||
let artifact = locked[name];
|
||||
if (
|
||||
typeof spec === "string" &&
|
||||
spec.startsWith("workspace:") &&
|
||||
artifact?.startsWith("link:")
|
||||
) {
|
||||
const link = artifact.slice(5);
|
||||
const path = posix.normalize(posix.join(currentImporter, link));
|
||||
const local = workspacePackages[`${currentImporter}:${depName}`];
|
||||
if (
|
||||
link.startsWith("/") ||
|
||||
/[\\\r\n]/.test(link) ||
|
||||
!isProjectPath(path) ||
|
||||
!local ||
|
||||
local.path !== path ||
|
||||
local.name !== name ||
|
||||
local.version !== actual?.version
|
||||
) {
|
||||
throw new Error(
|
||||
`${name}: workspace package must resolve to its tracked in-repository source`,
|
||||
);
|
||||
}
|
||||
// A local package is part of this exact clean repository commit. Its
|
||||
// declared version alone would not distinguish later source changes.
|
||||
artifact = `https://codeload.github.com/${repo}/tar.gz/${commit}`;
|
||||
}
|
||||
if (!isImmutableArtifact(artifact ?? ""))
|
||||
throw new Error(`${name}: immutable locked artifact required`);
|
||||
if (!actual?.version || !isPackageVersion(actual.version))
|
||||
@@ -95,7 +147,9 @@ export function createReleaseReceipt({
|
||||
if (/^\d/.test(artifact) && artifact !== actual.version)
|
||||
throw new Error(`${name}: installed package differs from the lockfile`);
|
||||
if (artifacts[name] && artifacts[name] !== artifact)
|
||||
throw new Error(`${name}: workspace importers resolve different artifacts`);
|
||||
throw new Error(
|
||||
`${name}: workspace importers resolve different artifacts`,
|
||||
);
|
||||
artifacts[name] = artifact;
|
||||
versions[name] = actual.version;
|
||||
}
|
||||
@@ -167,24 +221,62 @@ function main() {
|
||||
: undefined;
|
||||
const lockRoot = dirname(pnpmLock ? pnpmPath : npmPath);
|
||||
const installed = {};
|
||||
const projects = (option("--projects", importer)).split(",");
|
||||
if (projects.some((project) => !isProjectPath(project)) || new Set(projects).size !== projects.length)
|
||||
throw new Error("Receipt importers must be unique project paths inside the checkout");
|
||||
const projects = option("--projects", importer).split(",");
|
||||
if (
|
||||
projects.some((project) => !isProjectPath(project)) ||
|
||||
new Set(projects).size !== projects.length
|
||||
)
|
||||
throw new Error(
|
||||
"Receipt importers must be unique project paths inside the checkout",
|
||||
);
|
||||
const projectManifests = {};
|
||||
const workspacePackages = {};
|
||||
for (const project of projects) {
|
||||
projectManifests[project] = JSON.parse(readFileSync(resolve(root, project, "package.json"), "utf8"));
|
||||
for (const [name, spec] of Object.entries(dependencies(projectManifests[project]))) {
|
||||
projectManifests[project] = JSON.parse(
|
||||
readFileSync(resolve(root, project, "package.json"), "utf8"),
|
||||
);
|
||||
for (const [name, spec] of Object.entries(
|
||||
dependencies(projectManifests[project]),
|
||||
)) {
|
||||
if (!isShared(dependencyIdentity(name, spec))) continue;
|
||||
let location = resolve(root, project);
|
||||
while (true) {
|
||||
const path = resolve(location, "node_modules", name, "package.json");
|
||||
if (existsSync(path)) {
|
||||
installed[name] = JSON.parse(readFileSync(path, "utf8"));
|
||||
if (typeof spec === "string" && spec.startsWith("workspace:")) {
|
||||
const sourcePath =
|
||||
relative(
|
||||
realpathSync(root),
|
||||
dirname(realpathSync(path)),
|
||||
).replaceAll("\\", "/") || ".";
|
||||
if (!isProjectPath(sourcePath))
|
||||
throw new Error(
|
||||
`${name}: workspace package is outside the checkout`,
|
||||
);
|
||||
execFileSync(
|
||||
"git",
|
||||
[
|
||||
"-C",
|
||||
root,
|
||||
"ls-files",
|
||||
"--error-unmatch",
|
||||
"--",
|
||||
`${sourcePath}/package.json`,
|
||||
],
|
||||
{ stdio: "pipe" },
|
||||
);
|
||||
workspacePackages[`${project}:${name}`] = {
|
||||
...installed[name],
|
||||
path: sourcePath,
|
||||
};
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (location === root) break;
|
||||
const parent = dirname(location);
|
||||
if (parent === location || relative(root, parent).startsWith("..")) break;
|
||||
if (parent === location || relative(root, parent).startsWith(".."))
|
||||
break;
|
||||
location = parent;
|
||||
}
|
||||
}
|
||||
@@ -197,11 +289,15 @@ function main() {
|
||||
npmLock,
|
||||
importer,
|
||||
importers: projects,
|
||||
lockImporters: Object.fromEntries(projects.map(project => [
|
||||
project, relative(lockRoot, resolve(root, project)).replaceAll("\\", "/") || ".",
|
||||
])),
|
||||
lockImporters: Object.fromEntries(
|
||||
projects.map((project) => [
|
||||
project,
|
||||
relative(lockRoot, resolve(root, project)).replaceAll("\\", "/") || ".",
|
||||
]),
|
||||
),
|
||||
manifests: projectManifests,
|
||||
installed,
|
||||
workspacePackages,
|
||||
});
|
||||
mkdirSync(dirname(output), { recursive: true });
|
||||
writeFileSync(output, JSON.stringify(receipt, null, 2) + "\n");
|
||||
|
||||
@@ -239,7 +239,8 @@ test("CLI uses the clean checked-out commit, requires public scope and rejects u
|
||||
});
|
||||
|
||||
test("CLI attests nested independent lock roots without confusing project identity with importer identity", async () => {
|
||||
const { mkdtempSync, writeFileSync, mkdirSync, readFileSync, rmSync } = await import("node:fs");
|
||||
const { mkdtempSync, writeFileSync, mkdirSync, readFileSync, rmSync } =
|
||||
await import("node:fs");
|
||||
const { tmpdir } = await import("node:os");
|
||||
const { join } = await import("node:path");
|
||||
const { execFileSync, spawnSync } = await import("node:child_process");
|
||||
@@ -251,22 +252,249 @@ test("CLI attests nested independent lock roots without confusing project identi
|
||||
mkdirSync(join(project, "node_modules/chat"), { recursive: true });
|
||||
writeFileSync(join(root, ".gitignore"), "node_modules/\n");
|
||||
writeFileSync(join(project, "package.json"), JSON.stringify(manifest));
|
||||
writeFileSync(join(project, "node_modules/chat/package.json"), JSON.stringify({ name, version: "0.15.2" }));
|
||||
if (manager === "pnpm") writeFileSync(join(project, "pnpm-lock.yaml"), pnpmLock);
|
||||
else writeFileSync(join(project, "package-lock.json"), JSON.stringify({
|
||||
lockfileVersion: 3, packages: { "node_modules/chat": { version: "0.15.2" }, "node_modules/unrelated": { version: "1.0.1" } },
|
||||
}));
|
||||
const git = (...args) => execFileSync("git", ["-C", root, ...args], { stdio: "pipe" });
|
||||
git("init", "--quiet"); git("add", ".");
|
||||
git("-c", "user.name=Fixture", "-c", "user.email=fixture@example.invalid", "-c", "commit.gpgsign=false", "commit", "--quiet", "-m", "nested fixture");
|
||||
writeFileSync(
|
||||
join(project, "node_modules/chat/package.json"),
|
||||
JSON.stringify({ name, version: "0.15.2" }),
|
||||
);
|
||||
if (manager === "pnpm")
|
||||
writeFileSync(join(project, "pnpm-lock.yaml"), pnpmLock);
|
||||
else
|
||||
writeFileSync(
|
||||
join(project, "package-lock.json"),
|
||||
JSON.stringify({
|
||||
lockfileVersion: 3,
|
||||
packages: {
|
||||
"node_modules/chat": { version: "0.15.2" },
|
||||
"node_modules/unrelated": { version: "1.0.1" },
|
||||
},
|
||||
}),
|
||||
);
|
||||
const git = (...args) =>
|
||||
execFileSync("git", ["-C", root, ...args], { stdio: "pipe" });
|
||||
git("init", "--quiet");
|
||||
git("add", ".");
|
||||
git(
|
||||
"-c",
|
||||
"user.name=Fixture",
|
||||
"-c",
|
||||
"user.email=fixture@example.invalid",
|
||||
"-c",
|
||||
"commit.gpgsign=false",
|
||||
"commit",
|
||||
"--quiet",
|
||||
"-m",
|
||||
"nested fixture",
|
||||
);
|
||||
const output = join(root, "receipt.json");
|
||||
const result = spawnSync(process.execPath, [fileURLToPath(new URL("./release-receipt.mjs", import.meta.url)),
|
||||
"--public", "--root", root, "--dir", project, "--repo", input.repo, "--output", output], { encoding: "utf8" });
|
||||
const result = spawnSync(
|
||||
process.execPath,
|
||||
[
|
||||
fileURLToPath(new URL("./release-receipt.mjs", import.meta.url)),
|
||||
"--public",
|
||||
"--root",
|
||||
root,
|
||||
"--dir",
|
||||
project,
|
||||
"--repo",
|
||||
input.repo,
|
||||
"--output",
|
||||
output,
|
||||
],
|
||||
{ encoding: "utf8" },
|
||||
);
|
||||
assert.equal(result.status, 0, `${manager}: ${result.stderr}`);
|
||||
const receipt = JSON.parse(readFileSync(output, "utf8"));
|
||||
assert.equal(receipt.project, "frontend");
|
||||
assert.deepEqual(receipt.projects, ["frontend"]);
|
||||
assert.deepEqual(receipt.artifacts, { [name]: "0.15.2" });
|
||||
} finally { rmSync(root, { recursive: true, force: true }); }
|
||||
} finally {
|
||||
rmSync(root, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("workspace package identity is the owning commit, never an unverified local link", () => {
|
||||
const localName = "@bitbaum/product-local";
|
||||
const localInput = {
|
||||
...input,
|
||||
manifest: { dependencies: { [localName]: "workspace:*" } },
|
||||
pnpmLock: `lockfileVersion: '9.0'\nimporters:\n .:\n dependencies:\n '${localName}':\n specifier: workspace:*\n version: link:packages/local\n`,
|
||||
installed: { [localName]: { name: localName, version: "0.2.0" } },
|
||||
workspacePackages: {
|
||||
[`.:${localName}`]: {
|
||||
path: "packages/local",
|
||||
name: localName,
|
||||
version: "0.2.0",
|
||||
},
|
||||
},
|
||||
};
|
||||
const receipt = createReleaseReceipt(localInput);
|
||||
assert.deepEqual(receipt.artifacts, {
|
||||
[localName]: `https://codeload.github.com/${input.repo}/tar.gz/${input.commit}`,
|
||||
});
|
||||
assert.equal(
|
||||
receiptMatches(receipt, {
|
||||
repo: input.repo,
|
||||
commit: input.commit,
|
||||
artifacts: receipt.artifacts,
|
||||
}),
|
||||
true,
|
||||
);
|
||||
for (const change of [
|
||||
{ workspacePackages: {} },
|
||||
{
|
||||
workspacePackages: {
|
||||
[`.:${localName}`]: {
|
||||
path: "packages/other",
|
||||
name: localName,
|
||||
version: "0.2.0",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
workspacePackages: {
|
||||
[`.:${localName}`]: {
|
||||
path: "packages/local",
|
||||
name: "@bitbaum/other",
|
||||
version: "0.2.0",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
workspacePackages: {
|
||||
[`.:${localName}`]: {
|
||||
path: "packages/local",
|
||||
name: localName,
|
||||
version: "0.1.0",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
pnpmLock: localInput.pnpmLock.replace(
|
||||
"link:packages/local",
|
||||
"link:../outside",
|
||||
),
|
||||
},
|
||||
{ manifest: { dependencies: { [localName]: "file:packages/local" } } },
|
||||
])
|
||||
assert.throws(
|
||||
() => createReleaseReceipt({ ...localInput, ...change }),
|
||||
/workspace package|immutable locked/,
|
||||
);
|
||||
});
|
||||
|
||||
test("CLI verifies tracked workspace sources and real installed symlink targets", async () => {
|
||||
const {
|
||||
mkdtempSync,
|
||||
writeFileSync,
|
||||
mkdirSync,
|
||||
readFileSync,
|
||||
rmSync,
|
||||
symlinkSync,
|
||||
} = await import("node:fs");
|
||||
const { tmpdir } = await import("node:os");
|
||||
const { join } = await import("node:path");
|
||||
const { execFileSync, spawnSync } = await import("node:child_process");
|
||||
const { fileURLToPath } = await import("node:url");
|
||||
const localName = "@bitbaum/product-local";
|
||||
for (const scenario of [
|
||||
"root",
|
||||
"nested",
|
||||
"wrong-lock-path",
|
||||
"outside",
|
||||
"dirty-source",
|
||||
]) {
|
||||
const base = mkdtempSync(join(tmpdir(), "fleet-local-receipt-"));
|
||||
const root = join(base, "repo");
|
||||
try {
|
||||
const project = scenario === "nested" ? "apps/web" : ".";
|
||||
const appDir = join(root, project);
|
||||
const localDir = join(root, "packages/local");
|
||||
const installedDir =
|
||||
scenario === "outside" ? join(base, "outside") : localDir;
|
||||
mkdirSync(join(appDir, "node_modules/@bitbaum"), { recursive: true });
|
||||
mkdirSync(localDir, { recursive: true });
|
||||
mkdirSync(installedDir, { recursive: true });
|
||||
const localManifest = JSON.stringify({
|
||||
name: localName,
|
||||
version: "0.2.0",
|
||||
});
|
||||
writeFileSync(join(localDir, "package.json"), localManifest);
|
||||
writeFileSync(join(installedDir, "package.json"), localManifest);
|
||||
writeFileSync(join(localDir, "source.ts"), "export const value = 1;\n");
|
||||
writeFileSync(join(root, ".gitignore"), "node_modules/\n");
|
||||
writeFileSync(
|
||||
join(appDir, "package.json"),
|
||||
JSON.stringify({ dependencies: { [localName]: "workspace:*" } }),
|
||||
);
|
||||
const link =
|
||||
scenario === "nested"
|
||||
? "../../packages/local"
|
||||
: scenario === "wrong-lock-path"
|
||||
? "packages/other"
|
||||
: "packages/local";
|
||||
writeFileSync(
|
||||
join(root, "pnpm-lock.yaml"),
|
||||
`lockfileVersion: '9.0'\nimporters:\n ${project}:\n dependencies:\n '${localName}':\n specifier: workspace:*\n version: link:${link}\n`,
|
||||
);
|
||||
symlinkSync(installedDir, join(appDir, "node_modules", localName), "dir");
|
||||
const git = (...args) =>
|
||||
execFileSync("git", ["-C", root, ...args], { stdio: "pipe" });
|
||||
git("init", "--quiet");
|
||||
git("add", ".");
|
||||
git(
|
||||
"-c",
|
||||
"user.name=Fixture",
|
||||
"-c",
|
||||
"user.email=fixture@example.invalid",
|
||||
"-c",
|
||||
"commit.gpgsign=false",
|
||||
"commit",
|
||||
"--quiet",
|
||||
"-m",
|
||||
"local fixture",
|
||||
);
|
||||
const commit = git("rev-parse", "HEAD").toString().trim();
|
||||
if (scenario === "dirty-source")
|
||||
writeFileSync(join(localDir, "source.ts"), "export const value = 2;\n");
|
||||
const output = join(base, "receipt.json");
|
||||
const result = spawnSync(
|
||||
process.execPath,
|
||||
[
|
||||
fileURLToPath(new URL("./release-receipt.mjs", import.meta.url)),
|
||||
"--public",
|
||||
"--root",
|
||||
root,
|
||||
"--dir",
|
||||
appDir,
|
||||
"--repo",
|
||||
input.repo,
|
||||
"--output",
|
||||
output,
|
||||
],
|
||||
{ encoding: "utf8" },
|
||||
);
|
||||
if (["root", "nested"].includes(scenario)) {
|
||||
assert.equal(result.status, 0, result.stderr);
|
||||
const receipt = JSON.parse(readFileSync(output, "utf8"));
|
||||
assert.equal(
|
||||
receipt.artifacts[localName],
|
||||
`https://codeload.github.com/${input.repo}/tar.gz/${commit}`,
|
||||
);
|
||||
assert.equal(receipt.versions[localName], "0.2.0");
|
||||
} else {
|
||||
assert.notEqual(
|
||||
result.status,
|
||||
0,
|
||||
`${scenario} must not receive an exact-commit receipt`,
|
||||
);
|
||||
assert.match(
|
||||
result.stderr,
|
||||
/outside the checkout|workspace package|diff --quiet/,
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
rmSync(base, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user