-
Notifications
You must be signed in to change notification settings - Fork 424
fix(safe-outputs): trust cross-repo checkout dirs from manifest (dubious ownership) #40080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+189
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bfb3385
fix(safe-outputs): trust cross-repo checkout dirs from manifest
dsyme a28abd1
style: apply prettier formatting to test
dsyme 4dc109e
Potential fix for pull request finding
dsyme 3e685ab
Potential fix for pull request finding
dsyme 3b8eb9e
fix: restore for-loop closing brace in embedded manifest parser
dsyme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import { afterEach, beforeEach, describe, expect, it } from "vitest"; | ||
| import fs from "fs"; | ||
| import os from "os"; | ||
| import path from "path"; | ||
| import { spawnSync } from "child_process"; | ||
|
|
||
| // Path to the shell script under test. | ||
| const SCRIPT_PATH = path.join(__dirname, "..", "sh", "configure_git_credentials.sh"); | ||
|
|
||
| function createTempDir(prefix) { | ||
| return fs.mkdtempSync(path.join(os.tmpdir(), prefix)); | ||
| } | ||
|
|
||
| function removeDir(dir) { | ||
| if (dir && fs.existsSync(dir)) { | ||
| fs.rmSync(dir, { recursive: true, force: true }); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Run configure_git_credentials.sh in an isolated HOME so that the global git | ||
| * config it writes does not affect the developer's real ~/.gitconfig. | ||
| */ | ||
| function runScript(env) { | ||
| return spawnSync("sh", [SCRIPT_PATH], { | ||
| encoding: "utf8", | ||
| env: { ...process.env, ...env }, | ||
| }); | ||
| } | ||
|
|
||
| function readSafeDirectories(home) { | ||
| const result = spawnSync("git", ["config", "--global", "--get-all", "safe.directory"], { | ||
| encoding: "utf8", | ||
| env: { ...process.env, HOME: home }, | ||
| }); | ||
| if (result.status !== 0) { | ||
| return []; | ||
| } | ||
| return result.stdout.split("\n").filter(Boolean); | ||
| } | ||
|
|
||
| describe("configure_git_credentials.sh checkout manifest trust", () => { | ||
| const tempDirs = []; | ||
|
|
||
| function tempDir(prefix) { | ||
| const dir = createTempDir(prefix); | ||
| tempDirs.push(dir); | ||
| return dir; | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| while (tempDirs.length > 0) { | ||
| removeDir(tempDirs.pop()); | ||
| } | ||
| }); | ||
|
|
||
| function setup(manifest) { | ||
| const root = tempDir("cfg-git-creds-"); | ||
| const home = path.join(root, "home"); | ||
| const workspace = path.join(root, "ws"); | ||
| const runnerTemp = path.join(root, "runner"); | ||
| const safeOutputs = path.join(runnerTemp, "gh-aw", "safeoutputs"); | ||
| fs.mkdirSync(home, { recursive: true }); | ||
| fs.mkdirSync(workspace, { recursive: true }); | ||
| fs.mkdirSync(safeOutputs, { recursive: true }); | ||
| if (manifest !== undefined) { | ||
| fs.writeFileSync(path.join(safeOutputs, "checkout-manifest.json"), JSON.stringify(manifest), "utf8"); | ||
| } | ||
| return { home, workspace, runnerTemp }; | ||
| } | ||
|
|
||
| it("trusts cross-repo checkout subdirectories listed in the manifest", () => { | ||
| const { home, workspace, runnerTemp } = setup({ | ||
| "owner/repo": { repository: "owner/repo", path: "github", default_branch: "main" }, | ||
| "owner/tools": { repository: "owner/tools", path: "vendor/tools", default_branch: "main" }, | ||
| }); | ||
|
|
||
| const result = runScript({ HOME: home, GITHUB_WORKSPACE: workspace, RUNNER_TEMP: runnerTemp }); | ||
| expect(result.status).toBe(0); | ||
|
|
||
| const entries = readSafeDirectories(home); | ||
| expect(entries).toContain(workspace); | ||
| expect(entries).toContain(path.join(workspace, "github")); | ||
| expect(entries).toContain(path.join(workspace, "vendor", "tools")); | ||
| }); | ||
|
|
||
| it("skips manifest entries with an empty path", () => { | ||
| const { home, workspace, runnerTemp } = setup({ | ||
| "owner/repo": { repository: "owner/repo", path: "", default_branch: "main" }, | ||
| }); | ||
|
|
||
| const result = runScript({ HOME: home, GITHUB_WORKSPACE: workspace, RUNNER_TEMP: runnerTemp }); | ||
| expect(result.status).toBe(0); | ||
|
|
||
| const entries = readSafeDirectories(home); | ||
| // Only the workspace itself is trusted; the empty-path entry adds nothing extra. | ||
| expect(entries).toEqual([workspace]); | ||
| }); | ||
|
|
||
| it("rejects manifest paths that escape the workspace (path traversal)", () => { | ||
| const { home, workspace, runnerTemp } = setup({ | ||
| "evil/repo": { repository: "evil/repo", path: "../../escape", default_branch: "main" }, | ||
| }); | ||
|
|
||
| const result = runScript({ HOME: home, GITHUB_WORKSPACE: workspace, RUNNER_TEMP: runnerTemp }); | ||
| expect(result.status).toBe(0); | ||
|
|
||
| const entries = readSafeDirectories(home); | ||
| expect(entries).toEqual([workspace]); | ||
| expect(entries.some(e => e.includes("escape"))).toBe(false); | ||
| }); | ||
|
|
||
| it("honors GH_AW_CHECKOUT_MANIFEST override", () => { | ||
| const root = tempDir("cfg-git-creds-override-"); | ||
| const home = path.join(root, "home"); | ||
| const workspace = path.join(root, "ws"); | ||
| fs.mkdirSync(home, { recursive: true }); | ||
| fs.mkdirSync(workspace, { recursive: true }); | ||
| const manifestPath = path.join(root, "custom-manifest.json"); | ||
| fs.writeFileSync(manifestPath, JSON.stringify({ "owner/repo": { repository: "owner/repo", path: "sub", default_branch: "main" } }), "utf8"); | ||
|
|
||
| const result = runScript({ HOME: home, GITHUB_WORKSPACE: workspace, GH_AW_CHECKOUT_MANIFEST: manifestPath }); | ||
| expect(result.status).toBe(0); | ||
|
|
||
| const entries = readSafeDirectories(home); | ||
| expect(entries).toContain(path.join(workspace, "sub")); | ||
| }); | ||
|
|
||
| it("succeeds when no manifest is present", () => { | ||
| const { home, workspace, runnerTemp } = setup(undefined); | ||
|
|
||
| const result = runScript({ HOME: home, GITHUB_WORKSPACE: workspace, RUNNER_TEMP: runnerTemp }); | ||
| expect(result.status).toBe(0); | ||
|
|
||
| const entries = readSafeDirectories(home); | ||
| expect(entries).toEqual([workspace]); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.