From d65710c97d0d988b30fc55a7548697116a73eb2a Mon Sep 17 00:00:00 2001 From: abhiramshaji-cyber Date: Tue, 17 Nov 2026 00:26:42 -0500 Subject: [PATCH] action: verify a pinned sha256sum on cache hits When sha256sum is set, the cache key now carries the sum and a cache hit reruns the normal Linux or Windows install against what was restored. That path already hashes an existing tarball or MSI before using it, so a restored copy is only installed when it matches the pinned sum and anything else is downloaded again and verified. On Linux the verified tarball is now kept next to the binaries so it is cached too. Keys and behaviour without sha256sum are unchanged. Fixes #313 Signed-off-by: abhiramshaji-cyber --- dist/index.js | 24 +++++++++++++++++------- src/main.ts | 24 +++++++++++++++++------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/dist/index.js b/dist/index.js index 24b1876..e49f82c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -52456,19 +52456,24 @@ async function installTailscale(config, runnerOS) { const cacheKey = generateCacheKey(config, runnerOS); const toolPath = getToolPath(config, runnerOS); // Try to restore from cache first + let cacheHit = false; if (config.useCache && cacheKey) { - const cacheHit = await cache.restoreCache([toolPath], cacheKey); + cacheHit = !!(await cache.restoreCache([toolPath], cacheKey)); if (cacheHit) { (0, logging_1.logInfo)(config.logMode, `Found Tailscale ${config.resolvedVersion} in cache: ${toolPath}`); - // For Windows, install the cached MSI - if (runnerOS === runnerWindows) { + if (config.sha256Sum && runnerOS !== runnerMacOS) { + (0, logging_1.logInfo)(config.logMode, "Verifying cached download against sha256sum"); + } + else if (runnerOS === runnerWindows) { + // For Windows, install the cached MSI await installTailscaleWindows(config, toolPath, true); + return; } else { // For Linux/macOS, copy binaries to /usr/local/bin await installCachedBinaries(config, toolPath, runnerOS); + return; } - return; } } // Install fresh if not cached @@ -52482,7 +52487,7 @@ async function installTailscale(config, runnerOS) { await installTailscaleMacOS(config, toolPath); } // Save to cache after installation - if (config.useCache && cacheKey) { + if (config.useCache && cacheKey && !cacheHit) { try { await cache.saveCache([toolPath], cacheKey); (0, logging_1.logInfo)(config.logMode, `Cached Tailscale ${config.resolvedVersion} at: ${toolPath}`); @@ -52517,6 +52522,10 @@ async function installTailscaleLinux(config, toolPath) { const baseUrl = isStable ? "https://pkgs.tailscale.com/stable" : "https://pkgs.tailscale.com/unstable"; + // read sha256Sum before the fetch below fills it in + const tarDest = config.sha256Sum + ? path.join(toolPath, "tailscale.tgz") + : path.join(xdgCacheDir(), "tailscale.tgz"); // Get SHA256 if not provided if (!config.sha256Sum) { const shaUrl = `${baseUrl}/tailscale_${config.resolvedVersion}_${config.arch}.tgz.sha256`; @@ -52526,7 +52535,6 @@ async function installTailscaleLinux(config, toolPath) { // Download and extract const downloadUrl = `${baseUrl}/tailscale_${config.resolvedVersion}_${config.arch}.tgz`; const expectedSha = config.sha256Sum.trim().toLowerCase(); - const tarDest = path.join(xdgCacheDir(), "tailscale.tgz"); fs.mkdirSync(path.dirname(tarDest), { recursive: true }); // Check if the tarball already exists with the correct checksum (for // persistent self-hosted runners). tc.downloadTool refuses to overwrite an @@ -52865,7 +52873,9 @@ function generateCacheKey(config, runnerOS) { if (!config.useCache) { return undefined; } - return `action-setup-tailscale/${config.resolvedVersion}/${runnerOS}-${config.arch}`; + const key = `action-setup-tailscale/${config.resolvedVersion}/${runnerOS}-${config.arch}`; + const sha256Sum = config.sha256Sum.trim().toLowerCase(); + return sha256Sum ? `${key}/${sha256Sum}` : key; } function getToolPath(config, runnerOS) { const cacheDirectory = process.env.RUNNER_TOOL_CACHE || ""; diff --git a/src/main.ts b/src/main.ts index 73fb892..684dcd6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -401,22 +401,26 @@ async function installTailscale( const toolPath = getToolPath(config, runnerOS); // Try to restore from cache first + let cacheHit = false; if (config.useCache && cacheKey) { - const cacheHit = await cache.restoreCache([toolPath], cacheKey); + cacheHit = !!(await cache.restoreCache([toolPath], cacheKey)); if (cacheHit) { logInfo( config.logMode, `Found Tailscale ${config.resolvedVersion} in cache: ${toolPath}`, ); - // For Windows, install the cached MSI - if (runnerOS === runnerWindows) { + if (config.sha256Sum && runnerOS !== runnerMacOS) { + logInfo(config.logMode, "Verifying cached download against sha256sum"); + } else if (runnerOS === runnerWindows) { + // For Windows, install the cached MSI await installTailscaleWindows(config, toolPath, true); + return; } else { // For Linux/macOS, copy binaries to /usr/local/bin await installCachedBinaries(config, toolPath, runnerOS); + return; } - return; } } @@ -430,7 +434,7 @@ async function installTailscale( } // Save to cache after installation - if (config.useCache && cacheKey) { + if (config.useCache && cacheKey && !cacheHit) { try { await cache.saveCache([toolPath], cacheKey); logInfo( @@ -471,6 +475,11 @@ async function installTailscaleLinux( ? "https://pkgs.tailscale.com/stable" : "https://pkgs.tailscale.com/unstable"; + // read sha256Sum before the fetch below fills it in + const tarDest = config.sha256Sum + ? path.join(toolPath, "tailscale.tgz") + : path.join(xdgCacheDir(), "tailscale.tgz"); + // Get SHA256 if not provided if (!config.sha256Sum) { const shaUrl = `${baseUrl}/tailscale_${config.resolvedVersion}_${config.arch}.tgz.sha256`; @@ -487,7 +496,6 @@ async function installTailscaleLinux( const downloadUrl = `${baseUrl}/tailscale_${config.resolvedVersion}_${config.arch}.tgz`; const expectedSha = config.sha256Sum.trim().toLowerCase(); - const tarDest = path.join(xdgCacheDir(), "tailscale.tgz"); fs.mkdirSync(path.dirname(tarDest), { recursive: true }); // Check if the tarball already exists with the correct checksum (for @@ -974,7 +982,9 @@ function generateCacheKey( return undefined; } - return `action-setup-tailscale/${config.resolvedVersion}/${runnerOS}-${config.arch}`; + const key = `action-setup-tailscale/${config.resolvedVersion}/${runnerOS}-${config.arch}`; + const sha256Sum = config.sha256Sum.trim().toLowerCase(); + return sha256Sum ? `${key}/${sha256Sum}` : key; } function getToolPath(config: TailscaleConfig, runnerOS: string): string {