|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { describe, expect, it } from "vite-plus/test"; |
| 6 | +import { parseAllDocuments } from "yaml"; |
| 7 | + |
| 8 | +const { version } = JSON.parse( |
| 9 | + readFileSync(new URL("../package.json", import.meta.url), "utf8"), |
| 10 | +) as { version: string }; |
| 11 | + |
| 12 | +const safeRefs = [`v${version}`, "a".repeat(40), "main", "refs/tags/v1.21.1", "feature/test_ref-1"]; |
| 13 | +const unsafeRefs = [ |
| 14 | + "v1/../../../attacker/repo/main", |
| 15 | + "../other-repo/main", |
| 16 | + "refs/../main", |
| 17 | + "refs/./main", |
| 18 | + "refs//main", |
| 19 | + "/main", |
| 20 | + "main/", |
| 21 | + ".main", |
| 22 | + "main.", |
| 23 | + "v1..2", |
| 24 | + "refs./main", |
| 25 | + "refs/.main", |
| 26 | + "v1/%2e%2e/%2e%2e/attacker/repo/main", |
| 27 | + "v1/%252e%252e/attacker/repo/main", |
| 28 | + "v1%2f..", |
| 29 | + "refs\\..\\main", |
| 30 | + "v1?query", |
| 31 | + "v1#fragment", |
| 32 | + "v1 with spaces", |
| 33 | + "v1\t", |
| 34 | + "v1\n", |
| 35 | + "v1\r\n", |
| 36 | + "v1\nmain", |
| 37 | + "v1\u0001", |
| 38 | + "rélease", |
| 39 | + "v1;exit 0", |
| 40 | + '$(printf injected > "$SETUP_VP_TEST_EXECUTED")', |
| 41 | + '`printf injected > "$SETUP_VP_TEST_EXECUTED"`', |
| 42 | + "$(Set-Content -LiteralPath $env:SETUP_VP_TEST_EXECUTED -Value injected)", |
| 43 | +]; |
| 44 | + |
| 45 | +type Shell = "sh" | "bash" | "powershell"; |
| 46 | + |
| 47 | +function readSource(file: string) { |
| 48 | + return readFileSync(new URL(`../${file}`, import.meta.url), "utf8"); |
| 49 | +} |
| 50 | + |
| 51 | +function readTemplate(file: string) { |
| 52 | + return parseAllDocuments(readSource(file), { |
| 53 | + customTags: [{ tag: "!reference", collection: "seq", resolve: (value) => value }], |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | +function runScript(file: string, shell: Shell, setupRef: string | undefined) { |
| 58 | + const directory = mkdtempSync(join(tmpdir(), "setup-vp-setup-ref-")); |
| 59 | + const download = join(directory, "download"); |
| 60 | + const executed = join(directory, "executed"); |
| 61 | + const isTemplate = file.endsWith(".yml"); |
| 62 | + const source = isTemplate |
| 63 | + ? readTemplate(file)[1]!.toJSON()[".setup-vp-bootstrap"].before_script[0] |
| 64 | + : readSource(file); |
| 65 | + const payload = isTemplate |
| 66 | + ? shell === "powershell" |
| 67 | + ? '[IO.File]::WriteAllText($env:SETUP_VP_TEST_EXECUTED, "executed")' |
| 68 | + : 'printf executed > "$SETUP_VP_TEST_EXECUTED"' |
| 69 | + : 'import { writeFileSync } from "node:fs"; writeFileSync(process.env.SETUP_VP_TEST_EXECUTED, "executed");'; |
| 70 | + const prelude = |
| 71 | + shell === "powershell" |
| 72 | + ? ` |
| 73 | +function Invoke-WebRequest { |
| 74 | + param([string]$Uri, [string]$OutFile, [int]$TimeoutSec) |
| 75 | + [IO.File]::WriteAllText($env:SETUP_VP_TEST_DOWNLOAD, $Uri) |
| 76 | + Copy-Item -LiteralPath $env:SETUP_VP_TEST_PAYLOAD -Destination $OutFile |
| 77 | +} |
| 78 | +` |
| 79 | + : ` |
| 80 | +curl() { |
| 81 | + printf '%s' "$6" > "$SETUP_VP_TEST_DOWNLOAD" |
| 82 | + cp "$SETUP_VP_TEST_PAYLOAD" "$8" |
| 83 | +} |
| 84 | +wget() { |
| 85 | + printf '%s' "$8" > "$SETUP_VP_TEST_DOWNLOAD" |
| 86 | + cp "$SETUP_VP_TEST_PAYLOAD" "$7" |
| 87 | +} |
| 88 | +`; |
| 89 | + const scriptPath = join(directory, shell === "powershell" ? "test.ps1" : "test.sh"); |
| 90 | + const payloadPath = join(directory, "payload"); |
| 91 | + writeFileSync(scriptPath, prelude + source); |
| 92 | + writeFileSync(payloadPath, payload); |
| 93 | + try { |
| 94 | + const result = spawnSync( |
| 95 | + shell === "powershell" ? "powershell.exe" : shell, |
| 96 | + shell === "powershell" |
| 97 | + ? ["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File", scriptPath] |
| 98 | + : [scriptPath], |
| 99 | + { |
| 100 | + encoding: "utf8", |
| 101 | + timeout: 10_000, |
| 102 | + env: { |
| 103 | + ...process.env, |
| 104 | + TMPDIR: directory, |
| 105 | + AGENT_TEMPDIRECTORY: directory, |
| 106 | + SETUP_VP_SETUP_REF: setupRef, |
| 107 | + SETUP_VP_RUNTIME_OUT: join(directory, "dist", "azure", "index.mjs"), |
| 108 | + SETUP_VP_TEST_DOWNLOAD: download, |
| 109 | + SETUP_VP_TEST_EXECUTED: executed, |
| 110 | + SETUP_VP_TEST_PAYLOAD: payloadPath, |
| 111 | + }, |
| 112 | + }, |
| 113 | + ); |
| 114 | + expect(result.error).toBeUndefined(); |
| 115 | + return { |
| 116 | + ...result, |
| 117 | + download: existsSync(download) ? readFileSync(download, "utf8") : undefined, |
| 118 | + executed: existsSync(executed), |
| 119 | + }; |
| 120 | + } finally { |
| 121 | + rmSync(directory, { recursive: true, force: true }); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +describe("setup-ref download boundaries", () => { |
| 126 | + it.each(["gitlab/setup-vp.yml", "gitlab/setup-vp-windows.yml"])( |
| 127 | + "%s constrains inputs before script interpolation", |
| 128 | + (file) => { |
| 129 | + const input = readTemplate(file)[0]!.toJSON().spec.inputs["setup-ref"]; |
| 130 | + expect(input.regex).toBe("\\A[A-Za-z0-9_-]+([./][A-Za-z0-9_-]+)*\\z"); |
| 131 | + }, |
| 132 | + ); |
| 133 | + |
| 134 | + const targets: Array<[string, Shell, string]> = [ |
| 135 | + ["gitlab/setup-vp.yml", "sh", "gitlab/bootstrap.sh"], |
| 136 | + ["gitlab/setup-vp.yml", "bash", "gitlab/bootstrap.sh"], |
| 137 | + ["gitlab/setup-vp-windows.yml", "powershell", "gitlab/bootstrap.ps1"], |
| 138 | + ["gitlab/bootstrap.sh", "bash", "dist/gitlab/index.mjs"], |
| 139 | + ["gitlab/bootstrap.ps1", "powershell", "dist/gitlab/index.mjs"], |
| 140 | + ["azure/bootstrap.sh", "bash", "dist/azure/index.mjs"], |
| 141 | + ["azure/bootstrap.ps1", "powershell", "dist/azure/index.mjs"], |
| 142 | + ]; |
| 143 | + for (const [file, shell, downloadPath] of targets) { |
| 144 | + // Exercise the same native shells as each CI runner. Windows coverage runs |
| 145 | + // in the existing test-unit matrix in .github/workflows/test.yml. |
| 146 | + describe.skipIf((shell === "powershell") !== (process.platform === "win32"))( |
| 147 | + `${file} (${shell})`, |
| 148 | + () => { |
| 149 | + it.each([...safeRefs, "", undefined])("downloads and executes safe ref %j", (setupRef) => { |
| 150 | + const result = runScript(file, shell, setupRef); |
| 151 | + expect(result.status, result.stderr).toBe(0); |
| 152 | + expect(result.download).toBe( |
| 153 | + `https://raw.githubusercontent.com/voidzero-dev/setup-vp/${setupRef || `v${version}`}/${downloadPath}`, |
| 154 | + ); |
| 155 | + expect(result.executed).toBe(true); |
| 156 | + }); |
| 157 | + |
| 158 | + it.each(unsafeRefs)( |
| 159 | + "rejects unsafe ref %j before any download or execution", |
| 160 | + (setupRef) => { |
| 161 | + const result = runScript(file, shell, setupRef); |
| 162 | + expect(result.status).not.toBe(0); |
| 163 | + expect(result.stderr).toContain( |
| 164 | + file.startsWith("azure/") ? "invalid setupRef" : "invalid setup-ref", |
| 165 | + ); |
| 166 | + expect(result.download).toBeUndefined(); |
| 167 | + expect(result.executed).toBe(false); |
| 168 | + }, |
| 169 | + ); |
| 170 | + }, |
| 171 | + ); |
| 172 | + } |
| 173 | +}); |
0 commit comments