diff --git a/actions/setup/js/safe_outputs_handlers.cjs b/actions/setup/js/safe_outputs_handlers.cjs index 77cb7960260..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); + 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 f537a626374..db7b2337abd 100644 --- a/actions/setup/js/safe_outputs_handlers.test.cjs +++ b/actions/setup/js/safe_outputs_handlers.test.cjs @@ -826,7 +826,13 @@ 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(0o600); + expect(stagedStat.uid).toBe(process.getuid()); + expect(stagedStat.gid).toBe(process.getgid()); + 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 029e1b64427..6fcb408d9bc 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,27 @@ 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 } = {}) { + if (!((mode === 0o600 && directoryMode === 0o700) || (mode === 0o644 && directoryMode === 0o755))) { + throw assetValidationError("Invalid guarded asset permissions"); + } + 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..9c5cb93f09c 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,40 @@ describe("lstatGuard", () => { } }); + 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"));', mask, path.resolve(__dirname, "symlink_guard.cjs"), target]); + const stat = fs.statSync(target); + 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.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"))).toThrow("EEXIST"); + expect(fs.readFileSync(target, "utf8")).toBe("chart"); + } + expect(fs.statSync(tmpDir).mode & 0o777).toBe(0o700); + }); + + 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); + } + }); + 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); });