From c3f996e8c519492c95d61d34cc2b45ea6fa93b5f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:44:33 +0000 Subject: [PATCH 1/3] Initial plan From 9d48a810c632383cb34d0303c74eaa803cb4f41d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:55:22 +0000 Subject: [PATCH 2/3] Make guarded asset copies readable by the artifact uploader Co-authored-by: gh-aw-bot <259018956+gh-aw-bot@users.noreply.github.com> --- actions/setup/js/safe_outputs_handlers.cjs | 2 +- .../setup/js/safe_outputs_handlers.test.cjs | 6 ++- actions/setup/js/symlink_guard.cjs | 36 +++++++++++------ actions/setup/js/symlink_guard.test.cjs | 40 +++++++++++++++++++ actions/setup/js/upload_assets.cjs | 2 +- actions/setup/js/upload_assets.test.cjs | 4 ++ 6 files changed, 74 insertions(+), 16 deletions(-) diff --git a/actions/setup/js/safe_outputs_handlers.cjs b/actions/setup/js/safe_outputs_handlers.cjs index 77cb7960260..c1d4c88dffc 100644 --- a/actions/setup/js/safe_outputs_handlers.cjs +++ b/actions/setup/js/safe_outputs_handlers.cjs @@ -806,7 +806,7 @@ function createHandlers(server, appendSafeOutput, config = {}) { const stagedFileName = `${crypto.createHash("sha256").update(filePath).digest("hex")}${fileExt}`; const targetPath = path.join(assetsDir, stagedFileName); try { - writeGuardedFile(targetPath, fileContent); + writeGuardedFile(targetPath, fileContent, { mode: 0o644, directoryMode: 0o755 }); } catch (err) { if (err && typeof err === "object" && "code" in err && err.code === ERR_VALIDATION) throw err; throw new Error(`${ERR_SYSTEM}: Failed to copy file ${filePath} to ${targetPath}: ${getErrorMessage(err)}`, { cause: err }); diff --git a/actions/setup/js/safe_outputs_handlers.test.cjs b/actions/setup/js/safe_outputs_handlers.test.cjs index f537a626374..b6da39d1880 100644 --- a/actions/setup/js/safe_outputs_handlers.test.cjs +++ b/actions/setup/js/safe_outputs_handlers.test.cjs @@ -826,7 +826,11 @@ describe("safe_outputs_handlers", () => { expect(entry.size).toBe(Buffer.byteLength("test content")); const stagedFile = path.join(testRunnerTemp, "gh-aw", "safeoutputs", "assets", `${crypto.createHash("sha256").update(testFile).digest("hex")}.png`); expect(fs.readFileSync(stagedFile)).toEqual(Buffer.from("test content")); - expect(fs.statSync(stagedFile).mode & 0o777).toBe(0o600); + const stagedStat = fs.statSync(stagedFile); + expect(stagedStat.mode & 0o777).toBe(0o644); + expect(stagedStat.uid).toBe(process.getuid()); + expect(stagedStat.gid).toBe(process.getgid()); + expect(fs.statSync(path.dirname(stagedFile)).mode & 0o777).toBe(0o755); expect(entry.url).toContain("test-branch"); expect(result.content[0].type).toBe("text"); diff --git a/actions/setup/js/symlink_guard.cjs b/actions/setup/js/symlink_guard.cjs index 029e1b64427..2758f838b59 100644 --- a/actions/setup/js/symlink_guard.cjs +++ b/actions/setup/js/symlink_guard.cjs @@ -130,9 +130,10 @@ function resolveIssueArtifactPath(filename) { * @param {string} directory * @param {boolean} create * @param {(directoryPath: string) => T} operation + * @param {number} [directoryMode] * @returns {T} */ -function withGuardedDirectory(directory, create, operation) { +function withGuardedDirectory(directory, create, operation, directoryMode = 0o700) { if (process.platform !== "linux") { throw assetValidationError("Guarded asset access requires Linux descriptor-relative paths; macOS and Windows are unsupported"); } @@ -151,12 +152,14 @@ function withGuardedDirectory(directory, create, operation) { current = `/proc/self/fd/${parent.fd}/${component}`; } let stat; + let created = false; try { stat = lstatGuard(current); } catch (error) { if (!create || !error || typeof error !== "object" || !("code" in error) || error.code !== "ENOENT") throw error; try { - fs.mkdirSync(current, { mode: 0o700 }); + fs.mkdirSync(current, { mode: directoryMode }); + created = true; } catch (mkdirError) { if (!mkdirError || typeof mkdirError !== "object" || !("code" in mkdirError) || mkdirError.code !== "EEXIST") throw mkdirError; } @@ -168,6 +171,7 @@ function withGuardedDirectory(directory, create, operation) { directories.push({ fd, filename: current, stat }); const opened = fs.fstatSync(fd); if (opened.dev !== stat.dev || opened.ino !== stat.ino) throw assetValidationError("Asset directory changed while opening"); + if (created) fs.fchmodSync(fd, directoryMode); } const verify = () => { for (const directory of directories) { @@ -230,18 +234,24 @@ function readGuardedFile(filename, maximumBytes = 10 * 1024 * 1024) { * Exclusively create a staged/published file without following destination links. * @param {string} filename * @param {Buffer} content + * @param {{mode?: number, directoryMode?: number}} [options] */ -function writeGuardedFile(filename, content) { - return withGuardedDirectory(path.dirname(filename), true, directory => { - const target = path.join(directory, path.basename(filename)); - const fd = fs.openSync(target, fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL | fs.constants.O_NOFOLLOW, 0o600); - try { - fs.fchmodSync(fd, 0o600); - fs.writeFileSync(fd, content); - } finally { - fs.closeSync(fd); - } - }); +function writeGuardedFile(filename, content, { mode = 0o600, directoryMode = 0o700 } = {}) { + return withGuardedDirectory( + path.dirname(filename), + true, + directory => { + const target = path.join(directory, path.basename(filename)); + const fd = fs.openSync(target, fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL | fs.constants.O_NOFOLLOW, 0o600); + try { + fs.writeFileSync(fd, content); + fs.fchmodSync(fd, mode); + } finally { + fs.closeSync(fd); + } + }, + directoryMode + ); } module.exports = { lstatGuard, normalizeGuardedPath, resolveWorkspaceOrTmpPath, resolveIssueArtifactPath, validateUploadSourcePath, withGuardedDirectory, readGuardedFile, writeGuardedFile }; diff --git a/actions/setup/js/symlink_guard.test.cjs b/actions/setup/js/symlink_guard.test.cjs index 163b92f3e31..23316d7d38a 100644 --- a/actions/setup/js/symlink_guard.test.cjs +++ b/actions/setup/js/symlink_guard.test.cjs @@ -4,6 +4,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; const fs = require("fs"); const path = require("path"); +const os = require("os"); const { execFileSync } = require("child_process"); const { lstatGuard, readGuardedFile, writeGuardedFile } = require("./symlink_guard.cjs"); @@ -148,6 +149,45 @@ describe("lstatGuard", () => { } }); + it.skipIf(process.platform !== "linux")("creates uploader-readable assets and directories despite restrictive umasks", () => { + for (const mask of ["000", "077"]) { + const target = path.resolve(tmpDir, `assets-${mask}`, "claims", "claim-1", "chart.png"); + execFileSync(process.execPath, [ + "-e", + 'process.umask(parseInt(process.argv[1], 8)); require(process.argv[2]).writeGuardedFile(process.argv[3], Buffer.from("chart"), { mode: 0o644, directoryMode: 0o755 });', + mask, + path.resolve(__dirname, "symlink_guard.cjs"), + target, + ]); + const stat = fs.statSync(target); + expect(stat.mode & 0o777).toBe(0o644); + expect(stat.uid).toBe(process.getuid()); + expect(stat.gid).toBe(process.getgid()); + for (const directory of [path.dirname(target), path.dirname(path.dirname(target)), path.resolve(tmpDir, `assets-${mask}`)]) { + expect(fs.statSync(directory).mode & 0o777).toBe(0o755); + } + expect(() => writeGuardedFile(target, Buffer.from("overwrite"), { mode: 0o644, directoryMode: 0o755 })).toThrow("EEXIST"); + expect(fs.readFileSync(target, "utf8")).toBe("chart"); + } + expect(fs.statSync(tmpDir).mode & 0o777).toBe(0o700); + }); + + it.skipIf(process.platform !== "linux" || process.getuid?.() !== 0)("lets an unprivileged uploader read root-owned staged assets", () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), "asset-uploader-")); + try { + fs.chmodSync(root, 0o755); + const target = path.join(root, "assets", "claims", "claim-1", "chart.png"); + writeGuardedFile(target, Buffer.from("chart"), { mode: 0o644, directoryMode: 0o755 }); + expect(fs.statSync(target).uid).toBe(0); + expect(fs.statSync(target).gid).toBe(process.getgid()); + expect(fs.statSync(target).mode & 0o777).toBe(0o644); + const output = execFileSync(process.execPath, ["-e", 'process.stdout.write(require("fs").readFileSync(process.argv[1]));', target], { uid: 65534, gid: 65534, encoding: "utf8" }); + expect(output).toBe("chart"); + } finally { + fs.rmSync(root, { recursive: true, force: true }); + } + }); + it.each(["symlink", "regular"])("rejects a file swapped to a different %s between inspection and open", kind => { const source = path.join(tmpDir, "source.png"); const sentinel = path.join(tmpDir, "sentinel.png"); diff --git a/actions/setup/js/upload_assets.cjs b/actions/setup/js/upload_assets.cjs index 013d8631a97..31ebbbbd09f 100644 --- a/actions/setup/js/upload_assets.cjs +++ b/actions/setup/js/upload_assets.cjs @@ -178,7 +178,7 @@ async function main() { // Copy file to branch with target filename try { - writeGuardedFile(targetFileName, fileContent); + writeGuardedFile(targetFileName, fileContent, { mode: 0o644, directoryMode: 0o755 }); // Add to git await exec.exec("git", ["add", targetFileName]); diff --git a/actions/setup/js/upload_assets.test.cjs b/actions/setup/js/upload_assets.test.cjs index 7aee3a435d3..82c51ad6d54 100644 --- a/actions/setup/js/upload_assets.test.cjs +++ b/actions/setup/js/upload_assets.test.cjs @@ -193,6 +193,10 @@ describe("upload_assets.cjs", () => { } expect(mockCore.setFailed).not.toHaveBeenCalled(); expect(fs.readFileSync(target, "utf8")).toBe("verified chart"); + const publishedStat = fs.statSync(target); + expect(publishedStat.mode & 0o777).toBe(0o644); + expect(publishedStat.uid).toBe(process.getuid()); + expect(publishedStat.gid).toBe(process.getgid()); expect(crypto.createHash("sha256").update(fs.readFileSync(target)).digest("hex")).toBe(sha); }); From 900bf5051caf797a102a320de5fe56808bc5ef46 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 7 Nov 2026 23:53:12 +0000 Subject: [PATCH 3/3] Restore private safe-output asset staging modes Co-authored-by: gh-aw-bot <259018956+gh-aw-bot@users.noreply.github.com> --- actions/setup/js/safe_outputs_handlers.cjs | 2 +- .../setup/js/safe_outputs_handlers.test.cjs | 6 ++- actions/setup/js/symlink_guard.cjs | 3 ++ actions/setup/js/symlink_guard.test.cjs | 45 +++++++++---------- 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/actions/setup/js/safe_outputs_handlers.cjs b/actions/setup/js/safe_outputs_handlers.cjs index c1d4c88dffc..11a62d11db8 100644 --- a/actions/setup/js/safe_outputs_handlers.cjs +++ b/actions/setup/js/safe_outputs_handlers.cjs @@ -806,7 +806,7 @@ function createHandlers(server, appendSafeOutput, config = {}) { const stagedFileName = `${crypto.createHash("sha256").update(filePath).digest("hex")}${fileExt}`; const targetPath = path.join(assetsDir, stagedFileName); try { - writeGuardedFile(targetPath, fileContent, { mode: 0o644, directoryMode: 0o755 }); + writeGuardedFile(targetPath, fileContent, { mode: 0o600, directoryMode: 0o700 }); } catch (err) { if (err && typeof err === "object" && "code" in err && err.code === ERR_VALIDATION) throw err; throw new Error(`${ERR_SYSTEM}: Failed to copy file ${filePath} to ${targetPath}: ${getErrorMessage(err)}`, { cause: err }); diff --git a/actions/setup/js/safe_outputs_handlers.test.cjs b/actions/setup/js/safe_outputs_handlers.test.cjs index b6da39d1880..db7b2337abd 100644 --- a/actions/setup/js/safe_outputs_handlers.test.cjs +++ b/actions/setup/js/safe_outputs_handlers.test.cjs @@ -827,10 +827,12 @@ describe("safe_outputs_handlers", () => { const stagedFile = path.join(testRunnerTemp, "gh-aw", "safeoutputs", "assets", `${crypto.createHash("sha256").update(testFile).digest("hex")}.png`); expect(fs.readFileSync(stagedFile)).toEqual(Buffer.from("test content")); const stagedStat = fs.statSync(stagedFile); - expect(stagedStat.mode & 0o777).toBe(0o644); + expect(stagedStat.mode & 0o777).toBe(0o600); expect(stagedStat.uid).toBe(process.getuid()); expect(stagedStat.gid).toBe(process.getgid()); - expect(fs.statSync(path.dirname(stagedFile)).mode & 0o777).toBe(0o755); + for (const directory of [path.dirname(stagedFile), path.dirname(path.dirname(stagedFile)), path.dirname(path.dirname(path.dirname(stagedFile)))]) { + expect(fs.statSync(directory).mode & 0o777).toBe(0o700); + } expect(entry.url).toContain("test-branch"); expect(result.content[0].type).toBe("text"); diff --git a/actions/setup/js/symlink_guard.cjs b/actions/setup/js/symlink_guard.cjs index 2758f838b59..6fcb408d9bc 100644 --- a/actions/setup/js/symlink_guard.cjs +++ b/actions/setup/js/symlink_guard.cjs @@ -237,6 +237,9 @@ function readGuardedFile(filename, maximumBytes = 10 * 1024 * 1024) { * @param {{mode?: number, directoryMode?: number}} [options] */ function writeGuardedFile(filename, content, { mode = 0o600, directoryMode = 0o700 } = {}) { + if (!((mode === 0o600 && directoryMode === 0o700) || (mode === 0o644 && directoryMode === 0o755))) { + throw assetValidationError("Invalid guarded asset permissions"); + } return withGuardedDirectory( path.dirname(filename), true, diff --git a/actions/setup/js/symlink_guard.test.cjs b/actions/setup/js/symlink_guard.test.cjs index 23316d7d38a..9c5cb93f09c 100644 --- a/actions/setup/js/symlink_guard.test.cjs +++ b/actions/setup/js/symlink_guard.test.cjs @@ -149,42 +149,37 @@ describe("lstatGuard", () => { } }); - it.skipIf(process.platform !== "linux")("creates uploader-readable assets and directories despite restrictive umasks", () => { + it.skipIf(process.platform !== "linux")("creates private assets and directories despite restrictive umasks", () => { for (const mask of ["000", "077"]) { const target = path.resolve(tmpDir, `assets-${mask}`, "claims", "claim-1", "chart.png"); - execFileSync(process.execPath, [ - "-e", - 'process.umask(parseInt(process.argv[1], 8)); require(process.argv[2]).writeGuardedFile(process.argv[3], Buffer.from("chart"), { mode: 0o644, directoryMode: 0o755 });', - mask, - path.resolve(__dirname, "symlink_guard.cjs"), - target, - ]); + execFileSync(process.execPath, ["-e", 'process.umask(parseInt(process.argv[1], 8)); require(process.argv[2]).writeGuardedFile(process.argv[3], Buffer.from("chart"));', mask, path.resolve(__dirname, "symlink_guard.cjs"), target]); const stat = fs.statSync(target); - expect(stat.mode & 0o777).toBe(0o644); + expect(stat.mode & 0o777).toBe(0o600); expect(stat.uid).toBe(process.getuid()); expect(stat.gid).toBe(process.getgid()); - for (const directory of [path.dirname(target), path.dirname(path.dirname(target)), path.resolve(tmpDir, `assets-${mask}`)]) { - expect(fs.statSync(directory).mode & 0o777).toBe(0o755); + for (const directory of [path.dirname(target), path.dirname(path.dirname(target)), path.dirname(path.dirname(path.dirname(target))), path.resolve(tmpDir, `assets-${mask}`)]) { + expect(fs.statSync(directory).mode & 0o777).toBe(0o700); } - expect(() => writeGuardedFile(target, Buffer.from("overwrite"), { mode: 0o644, directoryMode: 0o755 })).toThrow("EEXIST"); + expect(() => writeGuardedFile(target, Buffer.from("overwrite"))).toThrow("EEXIST"); expect(fs.readFileSync(target, "utf8")).toBe("chart"); } expect(fs.statSync(tmpDir).mode & 0o777).toBe(0o700); }); - it.skipIf(process.platform !== "linux" || process.getuid?.() !== 0)("lets an unprivileged uploader read root-owned staged assets", () => { - const root = fs.mkdtempSync(path.join(os.tmpdir(), "asset-uploader-")); - try { - fs.chmodSync(root, 0o755); - const target = path.join(root, "assets", "claims", "claim-1", "chart.png"); - writeGuardedFile(target, Buffer.from("chart"), { mode: 0o644, directoryMode: 0o755 }); - expect(fs.statSync(target).uid).toBe(0); - expect(fs.statSync(target).gid).toBe(process.getgid()); - expect(fs.statSync(target).mode & 0o777).toBe(0o644); - const output = execFileSync(process.execPath, ["-e", 'process.stdout.write(require("fs").readFileSync(process.argv[1]));', target], { uid: 65534, gid: 65534, encoding: "utf8" }); - expect(output).toBe("chart"); - } finally { - fs.rmSync(root, { recursive: true, force: true }); + it("accepts only the private staging and published asset permission pairs", () => { + const publishTarget = path.join(tmpDir, "published", "chart.png"); + writeGuardedFile(publishTarget, Buffer.from("chart"), { mode: 0o644, directoryMode: 0o755 }); + expect(fs.statSync(publishTarget).mode & 0o777).toBe(0o644); + expect(fs.statSync(path.dirname(publishTarget)).mode & 0o777).toBe(0o755); + + for (const options of [ + { mode: 0o666, directoryMode: 0o777 }, + { mode: 0o600, directoryMode: 0o755 }, + { mode: 0o644, directoryMode: 0o700 }, + ]) { + const target = path.join(tmpDir, "invalid", `${options.mode}-${options.directoryMode}`, "chart.png"); + expect(() => writeGuardedFile(target, Buffer.from("chart"), options)).toThrow(expect.objectContaining({ code: "ERR_VALIDATION" })); + expect(fs.existsSync(path.dirname(target))).toBe(false); } });