mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-29 06:50:43 +01:00
fix(docker): use numeric owner ids for the linked node_modules copy (#23010)
COPY --link builds the layer without the destination image's /etc/passwd, so --chown=node:node failed with 'invalid user index -1' and the master image could not build. Use the production stage's USER_UID/USER_GID build args, which the base stage assigns to the node user. A guard test requires numeric owner ids on every linked copy, in any flag order and instruction case, and equal id defaults in the base and production stages. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-Authored-By: Devin Foley <devinfoley@users.noreply.github.com>
This commit is contained in:
2 files changed
+40
-2
No files matched your search
@@ -25,7 +25,7 @@ function stageBody(stageName: string): string {
|
||||
|
||||
it("copies installed dependencies as their own linked layer, before the per-commit app files", () => {
|
||||
const production = stageBody("production");
|
||||
const dependencies = production.search(/^COPY --link --chown=node:node --from=deps \/app\/node_modules \/app\/node_modules$/m);
|
||||
const dependencies = production.search(/^COPY --link --chown=\$\{USER_UID\}:\$\{USER_GID\} --from=deps \/app\/node_modules \/app\/node_modules$/m);
|
||||
const app = production.search(/^COPY --chown=node:node --exclude=node_modules --from=build \/app \/app$/m);
|
||||
expect(dependencies, "dependencies must come from the deps stage with --link").toBeGreaterThanOrEqual(0);
|
||||
expect(app, "the app copy must leave out the root node_modules").toBeGreaterThan(dependencies);
|
||||
@@ -54,3 +54,41 @@ it("copies the large provider-pack files as a linked layer with fixed timestamps
|
||||
expect(large, "large files need --link so the layer does not depend on the app layer").toBeGreaterThanOrEqual(0);
|
||||
expect(rest).toBeGreaterThan(large);
|
||||
});
|
||||
|
||||
function linkedCopyOwners(text: string): string[] {
|
||||
return [...text.matchAll(/^COPY\b.*$/gim)]
|
||||
.map((m) => m[0])
|
||||
.filter((line) => /\s--link\b/.test(line))
|
||||
.flatMap((line) => [...line.matchAll(/\s--chown=(\S+)/g)].map((m) => m[1]));
|
||||
}
|
||||
|
||||
function argDefault(stageName: string, name: string): string | undefined {
|
||||
return stageBody(stageName).match(new RegExp(`^ARG ${name}=(\\S+)$`, "m"))?.[1];
|
||||
}
|
||||
|
||||
it("finds linked copy owners regardless of flag order or instruction case", () => {
|
||||
const fixture = [
|
||||
"COPY --chown=node:node --link --from=deps /a /a",
|
||||
"COPY --link --chown=0:0 --from=pack /b /b",
|
||||
"COPY --chown=node:node --from=build /c /c",
|
||||
"copy --chown=node:node --link --from=deps /d /d",
|
||||
"CoPy --link --chown=0:0 --from=pack /e /e",
|
||||
].join("\n");
|
||||
expect(linkedCopyOwners(fixture)).toEqual(["node:node", "0:0", "node:node", "0:0"]);
|
||||
});
|
||||
|
||||
it("gives every linked copy numeric owner ids, because a linked layer cannot look up user names", () => {
|
||||
const owners = linkedCopyOwners(dockerfile);
|
||||
expect(owners.length).toBeGreaterThan(0);
|
||||
for (const owner of owners) {
|
||||
expect(owner, `COPY --link --chown=${owner} must use numeric ids`).toMatch(/^(\d+|\$\{USER_UID\}):(\d+|\$\{USER_GID\})$/);
|
||||
}
|
||||
});
|
||||
|
||||
it("uses the same owner id defaults in the production stage as the base stage gives the node user", () => {
|
||||
for (const name of ["USER_UID", "USER_GID"]) {
|
||||
const base = argDefault("base", name);
|
||||
expect(base, `the base stage must declare ${name}`).toBeDefined();
|
||||
expect(argDefault("production", name), `${name} defaults must match`).toBe(base);
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user