From b858fbe1e8494962b1eb7c8195279c1396bc7fe9 Mon Sep 17 00:00:00 2001 From: yiliang114 Date: Wed, 12 Aug 2026 18:13:04 +0800 Subject: [PATCH 1/5] fix(ci): cache downloaded linters on ECS runners --- scripts/lint.js | 51 ++++++++++++++++++++++++++++++++------ scripts/tests/lint.test.js | 22 +++++++++++++++- 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/scripts/lint.js b/scripts/lint.js index f893cd5e82b..cae2fcd957a 100644 --- a/scripts/lint.js +++ b/scripts/lint.js @@ -9,7 +9,7 @@ import { execSync } from 'node:child_process'; import { createHash } from 'node:crypto'; import { mkdirSync, rmSync } from 'node:fs'; -import { tmpdir } from 'node:os'; +import { homedir, tmpdir } from 'node:os'; import { join } from 'node:path'; const ACTIONLINT_VERSION = '1.7.12'; @@ -46,7 +46,20 @@ export function getLinterTempDir({ return join(baseDir, 'qwen-code-linters', `local-${workspaceHash}`); } +export function getLinterCacheDir({ + env = process.env, + homeDir = homedir(), +} = {}) { + return join( + env.XDG_CACHE_HOME || join(homeDir, '.cache'), + 'qwen-code', + 'linters', + ); +} + const TEMP_DIR = getLinterTempDir(); +// Share versioned archives; extracted binaries stay job-scoped in TEMP_DIR. +const CACHE_DIR = getLinterCacheDir(); function getPlatformArch() { const platform = process.platform; @@ -88,13 +101,29 @@ let lintersCache; function getLinters() { if (!lintersCache) { const platformArch = getPlatformArch(); + const actionlintArchive = join( + CACHE_DIR, + `actionlint_${ACTIONLINT_VERSION}_${platformArch.actionlint}.tar.gz`, + ); + const shellcheckArchive = join( + CACHE_DIR, + `shellcheck_${SHELLCHECK_VERSION}_${platformArch.shellcheck}.tar.xz`, + ); lintersCache = { actionlint: { check: 'command -v actionlint', installer: ` - mkdir -p "${TEMP_DIR}/actionlint" - curl -sSLo "${TEMP_DIR}/.actionlint.tgz" "https://github.com/rhysd/actionlint/releases/download/v${ACTIONLINT_VERSION}/actionlint_${ACTIONLINT_VERSION}_${platformArch.actionlint}.tar.gz" - tar -xzf "${TEMP_DIR}/.actionlint.tgz" -C "${TEMP_DIR}/actionlint" + set -e + mkdir -p "${TEMP_DIR}/actionlint" "${CACHE_DIR}" + if ! tar -tzf "${actionlintArchive}" >/dev/null 2>&1; then + curl -fsSL --retry 2 --retry-connrefused --connect-timeout 10 --max-time 90 \ + -o "${TEMP_DIR}/.actionlint.tgz" \ + "https://github.com/rhysd/actionlint/releases/download/v${ACTIONLINT_VERSION}/actionlint_${ACTIONLINT_VERSION}_${platformArch.actionlint}.tar.gz" + tar -tzf "${TEMP_DIR}/.actionlint.tgz" >/dev/null + cp "${TEMP_DIR}/.actionlint.tgz" "${actionlintArchive}.$$" + mv -f "${actionlintArchive}.$$" "${actionlintArchive}" + fi + tar -xzf "${actionlintArchive}" -C "${TEMP_DIR}/actionlint" `, run: ` actionlint \ @@ -111,9 +140,17 @@ function getLinters() { shellcheck: { check: 'command -v shellcheck', installer: ` - mkdir -p "${TEMP_DIR}/shellcheck" - curl -sSLo "${TEMP_DIR}/.shellcheck.txz" "https://github.com/koalaman/shellcheck/releases/download/v${SHELLCHECK_VERSION}/shellcheck-v${SHELLCHECK_VERSION}.${platformArch.shellcheck}.tar.xz" - tar -xf "${TEMP_DIR}/.shellcheck.txz" -C "${TEMP_DIR}/shellcheck" --strip-components=1 + set -e + mkdir -p "${TEMP_DIR}/shellcheck" "${CACHE_DIR}" + if ! tar -tf "${shellcheckArchive}" >/dev/null 2>&1; then + curl -fsSL --retry 2 --retry-connrefused --connect-timeout 10 --max-time 90 \ + -o "${TEMP_DIR}/.shellcheck.txz" \ + "https://github.com/koalaman/shellcheck/releases/download/v${SHELLCHECK_VERSION}/shellcheck-v${SHELLCHECK_VERSION}.${platformArch.shellcheck}.tar.xz" + tar -tf "${TEMP_DIR}/.shellcheck.txz" >/dev/null + cp "${TEMP_DIR}/.shellcheck.txz" "${shellcheckArchive}.$$" + mv -f "${shellcheckArchive}.$$" "${shellcheckArchive}" + fi + tar -xf "${shellcheckArchive}" -C "${TEMP_DIR}/shellcheck" --strip-components=1 `, run: ` git ls-files | grep -v '^integration-tests/terminal-bench/' | grep -E '^([^.]+|.*\\.(sh|zsh|bash))' | xargs file --mime-type \ diff --git a/scripts/tests/lint.test.js b/scripts/tests/lint.test.js index 330fb9f9880..6e0e902db71 100644 --- a/scripts/tests/lint.test.js +++ b/scripts/tests/lint.test.js @@ -11,7 +11,7 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; // paths so the suite also passes on the Windows gate. const toPosix = (value) => value.replaceAll(path.sep, '/'); -describe('getLinterTempDir', () => { +describe('linter directories', () => { const originalArgv = process.argv; beforeEach(() => { @@ -69,4 +69,24 @@ describe('getLinterTempDir', () => { expect(toPosix(second)).toMatch(/\/qwen-code-linters\/local-[a-f0-9]{16}$/); expect(first).not.toBe(second); }); + + it('shares cached downloads across GitHub Actions runs', async () => { + const { getLinterCacheDir } = await import('../lint.js'); + + const first = getLinterCacheDir({ + env: { + XDG_CACHE_HOME: '/runner/cache', + GITHUB_RUN_ID: '31583913822', + }, + }); + const second = getLinterCacheDir({ + env: { + XDG_CACHE_HOME: '/runner/cache', + GITHUB_RUN_ID: '31583913823', + }, + }); + + expect(toPosix(first)).toBe('/runner/cache/qwen-code/linters'); + expect(second).toBe(first); + }); }); From 0d6cfa342fce88cba7901d3d333403f4d7566482 Mon Sep 17 00:00:00 2001 From: yiliang114 Date: Wed, 12 Aug 2026 20:01:05 +0800 Subject: [PATCH 2/5] fix(ci): verify cached linter archives --- scripts/lint.js | 85 +++++++++++++++++++++++++++++--------- scripts/tests/lint.test.js | 72 ++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 20 deletions(-) diff --git a/scripts/lint.js b/scripts/lint.js index cae2fcd957a..afa876f77f1 100644 --- a/scripts/lint.js +++ b/scripts/lint.js @@ -15,6 +15,22 @@ import { join } from 'node:path'; const ACTIONLINT_VERSION = '1.7.12'; const SHELLCHECK_VERSION = '0.11.0'; const YAMLLINT_VERSION = '1.35.1'; +const ACTIONLINT_SHA256 = { + linux_amd64: + '8aca8db96f1b94770f1b0d72b6dddcb1ebb8123cb3712530b08cc387b349a3d8', + darwin_amd64: + '5b44c3bc2255115c9b69e30efc0fecdf498fdb63c5d58e17084fd5f16324c644', + darwin_arm64: + 'aba9ced2dee8d27fecca3dc7feb1a7f9a52caefa1eb46f3271ea66b6e0e6953f', +}; +const SHELLCHECK_SHA256 = { + 'linux.x86_64': + '8c3be12b05d5c177a04c29e3c78ce89ac86f1595681cab149b65b97c4e227198', + 'darwin.x86_64': + '3c89db4edcab7cf1c27bff178882e0f6f27f7afdf54e859fa041fca10febe4c6', + 'darwin.aarch64': + '56affdd8de5527894dca6dc3d7e0a99a873b0f004d7aabc30ae407d3f48b0a79', +}; function sanitizePathPart(value) { return value.replace(/[^A-Za-z0-9._-]/g, '_'); @@ -57,6 +73,35 @@ export function getLinterCacheDir({ ); } +export function getCachedArchiveInstaller({ + cacheArchive, + localArchive, + expectedSha256, + downloadUrl, + archiveCheck, + extract, + executable, +}) { + return ` + set -e + verify_sha256() { + "${process.execPath}" -e 'const {createHash}=require("node:crypto");const {readFileSync}=require("node:fs");process.exit(createHash("sha256").update(readFileSync(process.argv[1])).digest("hex")===process.argv[2]?0:1)' "$1" "$2" + } + if ! cp "${cacheArchive}" "${localArchive}" 2>/dev/null \ + || ! verify_sha256 "${localArchive}" "${expectedSha256}"; then + rm -f "${localArchive}" + curl -fsSL --retry 2 --retry-connrefused --connect-timeout 10 --max-time 90 \ + -o "${localArchive}" "${downloadUrl}" + verify_sha256 "${localArchive}" "${expectedSha256}" + ${archiveCheck} + cp "${localArchive}" "${cacheArchive}.$$" + mv -f "${cacheArchive}.$$" "${cacheArchive}" + fi + ${extract} + test -x "${executable}" + `; +} + const TEMP_DIR = getLinterTempDir(); // Share versioned archives; extracted binaries stay job-scoped in TEMP_DIR. const CACHE_DIR = getLinterCacheDir(); @@ -109,21 +154,22 @@ function getLinters() { CACHE_DIR, `shellcheck_${SHELLCHECK_VERSION}_${platformArch.shellcheck}.tar.xz`, ); + const actionlintLocalArchive = join(TEMP_DIR, '.actionlint.tgz'); + const shellcheckLocalArchive = join(TEMP_DIR, '.shellcheck.txz'); lintersCache = { actionlint: { check: 'command -v actionlint', installer: ` - set -e mkdir -p "${TEMP_DIR}/actionlint" "${CACHE_DIR}" - if ! tar -tzf "${actionlintArchive}" >/dev/null 2>&1; then - curl -fsSL --retry 2 --retry-connrefused --connect-timeout 10 --max-time 90 \ - -o "${TEMP_DIR}/.actionlint.tgz" \ - "https://github.com/rhysd/actionlint/releases/download/v${ACTIONLINT_VERSION}/actionlint_${ACTIONLINT_VERSION}_${platformArch.actionlint}.tar.gz" - tar -tzf "${TEMP_DIR}/.actionlint.tgz" >/dev/null - cp "${TEMP_DIR}/.actionlint.tgz" "${actionlintArchive}.$$" - mv -f "${actionlintArchive}.$$" "${actionlintArchive}" - fi - tar -xzf "${actionlintArchive}" -C "${TEMP_DIR}/actionlint" + ${getCachedArchiveInstaller({ + cacheArchive: actionlintArchive, + localArchive: actionlintLocalArchive, + expectedSha256: ACTIONLINT_SHA256[platformArch.actionlint], + downloadUrl: `https://github.com/rhysd/actionlint/releases/download/v${ACTIONLINT_VERSION}/actionlint_${ACTIONLINT_VERSION}_${platformArch.actionlint}.tar.gz`, + archiveCheck: `tar -tzf "${actionlintLocalArchive}" >/dev/null`, + extract: `tar -xzf "${actionlintLocalArchive}" -C "${TEMP_DIR}/actionlint"`, + executable: join(TEMP_DIR, 'actionlint', 'actionlint'), + })} `, run: ` actionlint \ @@ -140,17 +186,16 @@ function getLinters() { shellcheck: { check: 'command -v shellcheck', installer: ` - set -e mkdir -p "${TEMP_DIR}/shellcheck" "${CACHE_DIR}" - if ! tar -tf "${shellcheckArchive}" >/dev/null 2>&1; then - curl -fsSL --retry 2 --retry-connrefused --connect-timeout 10 --max-time 90 \ - -o "${TEMP_DIR}/.shellcheck.txz" \ - "https://github.com/koalaman/shellcheck/releases/download/v${SHELLCHECK_VERSION}/shellcheck-v${SHELLCHECK_VERSION}.${platformArch.shellcheck}.tar.xz" - tar -tf "${TEMP_DIR}/.shellcheck.txz" >/dev/null - cp "${TEMP_DIR}/.shellcheck.txz" "${shellcheckArchive}.$$" - mv -f "${shellcheckArchive}.$$" "${shellcheckArchive}" - fi - tar -xf "${shellcheckArchive}" -C "${TEMP_DIR}/shellcheck" --strip-components=1 + ${getCachedArchiveInstaller({ + cacheArchive: shellcheckArchive, + localArchive: shellcheckLocalArchive, + expectedSha256: SHELLCHECK_SHA256[platformArch.shellcheck], + downloadUrl: `https://github.com/koalaman/shellcheck/releases/download/v${SHELLCHECK_VERSION}/shellcheck-v${SHELLCHECK_VERSION}.${platformArch.shellcheck}.tar.xz`, + archiveCheck: `tar -tf "${shellcheckLocalArchive}" >/dev/null`, + extract: `tar -xf "${shellcheckLocalArchive}" -C "${TEMP_DIR}/shellcheck" --strip-components=1`, + executable: join(TEMP_DIR, 'shellcheck', 'shellcheck'), + })} `, run: ` git ls-files | grep -v '^integration-tests/terminal-bench/' | grep -E '^([^.]+|.*\\.(sh|zsh|bash))' | xargs file --mime-type \ diff --git a/scripts/tests/lint.test.js b/scripts/tests/lint.test.js index 6e0e902db71..94c8384bf31 100644 --- a/scripts/tests/lint.test.js +++ b/scripts/tests/lint.test.js @@ -4,6 +4,17 @@ * SPDX-License-Identifier: Apache-2.0 */ +import { execSync } from 'node:child_process'; +import { createHash } from 'node:crypto'; +import { + chmodSync, + mkdirSync, + mkdtempSync, + readFileSync, + rmSync, + writeFileSync, +} from 'node:fs'; +import { tmpdir } from 'node:os'; import path from 'node:path'; import { afterEach, beforeEach, describe, expect, it } from 'vitest'; @@ -89,4 +100,65 @@ describe('linter directories', () => { expect(toPosix(first)).toBe('/runner/cache/qwen-code/linters'); expect(second).toBe(first); }); + + it.skipIf(process.platform === 'win32')( + 'replaces an untrusted cache and reuses the verified archive offline', + async () => { + const { getCachedArchiveInstaller } = await import('../lint.js'); + const root = mkdtempSync(path.join(tmpdir(), 'linter-cache-')); + + try { + const binDir = path.join(root, 'bin'); + const cacheArchive = path.join(root, 'cache', 'tool.tar'); + const localArchive = path.join(root, 'job', 'tool.tar'); + const executable = path.join(root, 'job', 'tool'); + const fixture = path.join(root, 'official.tar'); + const curlLog = path.join(root, 'curl.log'); + const curl = path.join(binDir, 'curl'); + mkdirSync(binDir, { recursive: true }); + mkdirSync(path.dirname(cacheArchive), { recursive: true }); + mkdirSync(path.dirname(localArchive), { recursive: true }); + writeFileSync(cacheArchive, 'validator-passing plant'); + writeFileSync(fixture, 'official archive'); + writeFileSync( + curl, + '#!/bin/sh\nprintf "download\\n" >> "$CURL_LOG"\nwhile [ "$#" -gt 0 ]; do\n if [ "$1" = "-o" ]; then cp "$FIXTURE_ARCHIVE" "$2"; exit; fi\n shift\ndone\nexit 1\n', + ); + chmodSync(curl, 0o755); + + const expectedSha256 = createHash('sha256') + .update(readFileSync(fixture)) + .digest('hex'); + const installer = getCachedArchiveInstaller({ + cacheArchive, + localArchive, + expectedSha256, + downloadUrl: 'https://example.invalid/tool.tar', + archiveCheck: 'true', + extract: `cp "${localArchive}" "${executable}" && chmod +x "${executable}"`, + executable, + }); + const env = { + ...process.env, + PATH: `${binDir}:${process.env.PATH}`, + CURL_LOG: curlLog, + FIXTURE_ARCHIVE: fixture, + }; + + execSync(installer, { env }); + expect(readFileSync(cacheArchive, 'utf8')).toBe('official archive'); + expect(readFileSync(executable, 'utf8')).toBe('official archive'); + expect(readFileSync(curlLog, 'utf8')).toBe('download\n'); + + rmSync(localArchive); + rmSync(executable); + rmSync(fixture); + execSync(installer, { env }); + expect(readFileSync(executable, 'utf8')).toBe('official archive'); + expect(readFileSync(curlLog, 'utf8')).toBe('download\n'); + } finally { + rmSync(root, { recursive: true, force: true }); + } + }, + ); }); From 4799f6741890336c7bf28b59cf1f866c97148919 Mon Sep 17 00:00:00 2001 From: yiliang114 Date: Wed, 12 Aug 2026 21:29:08 +0800 Subject: [PATCH 3/5] fix(ci): make linter cache writes optional --- scripts/lint.js | 19 ++++++++++++++----- scripts/tests/lint.test.js | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/scripts/lint.js b/scripts/lint.js index afa876f77f1..012899a2289 100644 --- a/scripts/lint.js +++ b/scripts/lint.js @@ -85,7 +85,7 @@ export function getCachedArchiveInstaller({ return ` set -e verify_sha256() { - "${process.execPath}" -e 'const {createHash}=require("node:crypto");const {readFileSync}=require("node:fs");process.exit(createHash("sha256").update(readFileSync(process.argv[1])).digest("hex")===process.argv[2]?0:1)' "$1" "$2" + "${process.execPath}" -e 'const {createHash}=require("node:crypto");const {readFileSync}=require("node:fs");const actual=createHash("sha256").update(readFileSync(process.argv[1])).digest("hex");if(actual!==process.argv[2]){console.error("SHA-256 mismatch for "+process.argv[1]+": expected "+process.argv[2]+", got "+actual);process.exit(1)}' "$1" "$2" } if ! cp "${cacheArchive}" "${localArchive}" 2>/dev/null \ || ! verify_sha256 "${localArchive}" "${expectedSha256}"; then @@ -94,8 +94,15 @@ export function getCachedArchiveInstaller({ -o "${localArchive}" "${downloadUrl}" verify_sha256 "${localArchive}" "${expectedSha256}" ${archiveCheck} - cp "${localArchive}" "${cacheArchive}.$$" - mv -f "${cacheArchive}.$$" "${cacheArchive}" + cache_tmp='' + if cache_tmp="$(mktemp "${cacheArchive}.XXXXXX" 2>/dev/null)" \ + && cp "${localArchive}" "$cache_tmp" \ + && "${process.execPath}" -e 'require("node:fs").renameSync(process.argv[1],process.argv[2])' "$cache_tmp" "${cacheArchive}" 2>/dev/null; then + : + else + [ -z "$cache_tmp" ] || rm -f "$cache_tmp" + echo "Warning: could not persist linter archive to ${cacheArchive}" >&2 + fi fi ${extract} test -x "${executable}" @@ -160,7 +167,8 @@ function getLinters() { actionlint: { check: 'command -v actionlint', installer: ` - mkdir -p "${TEMP_DIR}/actionlint" "${CACHE_DIR}" + mkdir -p "${TEMP_DIR}/actionlint" + mkdir -p "${CACHE_DIR}" 2>/dev/null || true ${getCachedArchiveInstaller({ cacheArchive: actionlintArchive, localArchive: actionlintLocalArchive, @@ -186,7 +194,8 @@ function getLinters() { shellcheck: { check: 'command -v shellcheck', installer: ` - mkdir -p "${TEMP_DIR}/shellcheck" "${CACHE_DIR}" + mkdir -p "${TEMP_DIR}/shellcheck" + mkdir -p "${CACHE_DIR}" 2>/dev/null || true ${getCachedArchiveInstaller({ cacheArchive: shellcheckArchive, localArchive: shellcheckLocalArchive, diff --git a/scripts/tests/lint.test.js b/scripts/tests/lint.test.js index 94c8384bf31..83a7ebd3434 100644 --- a/scripts/tests/lint.test.js +++ b/scripts/tests/lint.test.js @@ -12,6 +12,7 @@ import { mkdtempSync, readFileSync, rmSync, + statSync, writeFileSync, } from 'node:fs'; import { tmpdir } from 'node:os'; @@ -99,10 +100,13 @@ describe('linter directories', () => { expect(toPosix(first)).toBe('/runner/cache/qwen-code/linters'); expect(second).toBe(first); + expect( + toPosix(getLinterCacheDir({ env: {}, homeDir: '/home/runner' })), + ).toBe('/home/runner/.cache/qwen-code/linters'); }); it.skipIf(process.platform === 'win32')( - 'replaces an untrusted cache and reuses the verified archive offline', + 'verifies and reuses archives without depending on cache writes', async () => { const { getCachedArchiveInstaller } = await import('../lint.js'); const root = mkdtempSync(path.join(tmpdir(), 'linter-cache-')); @@ -122,7 +126,7 @@ describe('linter directories', () => { writeFileSync(fixture, 'official archive'); writeFileSync( curl, - '#!/bin/sh\nprintf "download\\n" >> "$CURL_LOG"\nwhile [ "$#" -gt 0 ]; do\n if [ "$1" = "-o" ]; then cp "$FIXTURE_ARCHIVE" "$2"; exit; fi\n shift\ndone\nexit 1\n', + '#!/bin/sh\nprintf "download\\n" >> "$CURL_LOG"\nwhile [ "$#" -gt 0 ]; do\n if [ "$1" = "-o" ]; then\n if [ "$CORRUPT_DOWNLOAD" = "1" ]; then printf corrupt > "$2"; else cp "$FIXTURE_ARCHIVE" "$2"; fi\n exit\n fi\n shift\ndone\nexit 1\n', ); chmodSync(curl, 0o755); @@ -142,23 +146,46 @@ describe('linter directories', () => { ...process.env, PATH: `${binDir}:${process.env.PATH}`, CURL_LOG: curlLog, + CORRUPT_DOWNLOAD: '0', FIXTURE_ARCHIVE: fixture, }; + expect(() => + execSync(installer, { + env: { ...env, CORRUPT_DOWNLOAD: '1' }, + }), + ).toThrow(); + expect(readFileSync(cacheArchive, 'utf8')).toBe( + 'validator-passing plant', + ); + execSync(installer, { env }); expect(readFileSync(cacheArchive, 'utf8')).toBe('official archive'); expect(readFileSync(executable, 'utf8')).toBe('official archive'); - expect(readFileSync(curlLog, 'utf8')).toBe('download\n'); + expect(readFileSync(curlLog, 'utf8')).toBe('download\ndownload\n'); rmSync(localArchive); rmSync(executable); rmSync(fixture); execSync(installer, { env }); expect(readFileSync(executable, 'utf8')).toBe('official archive'); - expect(readFileSync(curlLog, 'utf8')).toBe('download\n'); + expect(readFileSync(curlLog, 'utf8')).toBe('download\ndownload\n'); + + writeFileSync(fixture, 'official archive'); + rmSync(localArchive); + rmSync(executable); + rmSync(cacheArchive); + mkdirSync(cacheArchive); + execSync(installer, { env }); + expect(readFileSync(executable, 'utf8')).toBe('official archive'); + expect(statSync(cacheArchive).isDirectory()).toBe(true); + expect(readFileSync(curlLog, 'utf8')).toBe( + 'download\ndownload\ndownload\n', + ); } finally { rmSync(root, { recursive: true, force: true }); } }, + 15_000, ); }); From cf6208d2f90625e921c14064a4055219b077ea00 Mon Sep 17 00:00:00 2001 From: yiliang114 Date: Wed, 12 Aug 2026 23:43:45 +0800 Subject: [PATCH 4/5] test(ci): cover linter cache fail-closed paths --- scripts/lint.js | 3 +++ scripts/tests/lint.test.js | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/scripts/lint.js b/scripts/lint.js index 012899a2289..5cdbd72052c 100644 --- a/scripts/lint.js +++ b/scripts/lint.js @@ -82,6 +82,9 @@ export function getCachedArchiveInstaller({ extract, executable, }) { + if (!expectedSha256) { + throw new Error(`Missing SHA-256 pin for ${downloadUrl}`); + } return ` set -e verify_sha256() { diff --git a/scripts/tests/lint.test.js b/scripts/tests/lint.test.js index 83a7ebd3434..19083a10326 100644 --- a/scripts/tests/lint.test.js +++ b/scripts/tests/lint.test.js @@ -133,6 +133,13 @@ describe('linter directories', () => { const expectedSha256 = createHash('sha256') .update(readFileSync(fixture)) .digest('hex'); + expect(() => + getCachedArchiveInstaller({ + cacheArchive, + localArchive, + downloadUrl: 'https://example.invalid/unpinned.tar', + }), + ).toThrow('Missing SHA-256 pin'); const installer = getCachedArchiveInstaller({ cacheArchive, localArchive, @@ -164,6 +171,19 @@ describe('linter directories', () => { expect(readFileSync(executable, 'utf8')).toBe('official archive'); expect(readFileSync(curlLog, 'utf8')).toBe('download\ndownload\n'); + rmSync(localArchive); + rmSync(executable); + const nonExecutableInstaller = getCachedArchiveInstaller({ + cacheArchive, + localArchive, + expectedSha256, + downloadUrl: 'https://example.invalid/tool.tar', + archiveCheck: 'true', + extract: `cp "${localArchive}" "${executable}"`, + executable, + }); + expect(() => execSync(nonExecutableInstaller, { env })).toThrow(); + rmSync(localArchive); rmSync(executable); rmSync(fixture); From ee7a70e803c7ee7b39be3c78b6b9a91990a0c211 Mon Sep 17 00:00:00 2001 From: yiliang114 Date: Thu, 13 Aug 2026 01:08:12 +0800 Subject: [PATCH 5/5] fix(ci): harden linter cache setup --- scripts/lint.js | 9 ++++----- scripts/tests/lint.test.js | 20 +++++++++++++++++--- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/scripts/lint.js b/scripts/lint.js index 5cdbd72052c..db5fd6d31da 100644 --- a/scripts/lint.js +++ b/scripts/lint.js @@ -10,7 +10,7 @@ import { execSync } from 'node:child_process'; import { createHash } from 'node:crypto'; import { mkdirSync, rmSync } from 'node:fs'; import { homedir, tmpdir } from 'node:os'; -import { join } from 'node:path'; +import { dirname, join } from 'node:path'; const ACTIONLINT_VERSION = '1.7.12'; const SHELLCHECK_VERSION = '0.11.0'; @@ -90,6 +90,7 @@ export function getCachedArchiveInstaller({ verify_sha256() { "${process.execPath}" -e 'const {createHash}=require("node:crypto");const {readFileSync}=require("node:fs");const actual=createHash("sha256").update(readFileSync(process.argv[1])).digest("hex");if(actual!==process.argv[2]){console.error("SHA-256 mismatch for "+process.argv[1]+": expected "+process.argv[2]+", got "+actual);process.exit(1)}' "$1" "$2" } + mkdir -p "${dirname(cacheArchive)}" || true if ! cp "${cacheArchive}" "${localArchive}" 2>/dev/null \ || ! verify_sha256 "${localArchive}" "${expectedSha256}"; then rm -f "${localArchive}" @@ -98,9 +99,9 @@ export function getCachedArchiveInstaller({ verify_sha256 "${localArchive}" "${expectedSha256}" ${archiveCheck} cache_tmp='' - if cache_tmp="$(mktemp "${cacheArchive}.XXXXXX" 2>/dev/null)" \ + if cache_tmp="$(mktemp "${cacheArchive}.XXXXXX")" \ && cp "${localArchive}" "$cache_tmp" \ - && "${process.execPath}" -e 'require("node:fs").renameSync(process.argv[1],process.argv[2])' "$cache_tmp" "${cacheArchive}" 2>/dev/null; then + && "${process.execPath}" -e 'require("node:fs").renameSync(process.argv[1],process.argv[2])' "$cache_tmp" "${cacheArchive}"; then : else [ -z "$cache_tmp" ] || rm -f "$cache_tmp" @@ -171,7 +172,6 @@ function getLinters() { check: 'command -v actionlint', installer: ` mkdir -p "${TEMP_DIR}/actionlint" - mkdir -p "${CACHE_DIR}" 2>/dev/null || true ${getCachedArchiveInstaller({ cacheArchive: actionlintArchive, localArchive: actionlintLocalArchive, @@ -198,7 +198,6 @@ function getLinters() { check: 'command -v shellcheck', installer: ` mkdir -p "${TEMP_DIR}/shellcheck" - mkdir -p "${CACHE_DIR}" 2>/dev/null || true ${getCachedArchiveInstaller({ cacheArchive: shellcheckArchive, localArchive: shellcheckLocalArchive, diff --git a/scripts/tests/lint.test.js b/scripts/tests/lint.test.js index 19083a10326..39973f30c48 100644 --- a/scripts/tests/lint.test.js +++ b/scripts/tests/lint.test.js @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { execSync } from 'node:child_process'; +import { execSync, spawnSync } from 'node:child_process'; import { createHash } from 'node:crypto'; import { chmodSync, @@ -192,15 +192,29 @@ describe('linter directories', () => { expect(readFileSync(curlLog, 'utf8')).toBe('download\ndownload\n'); writeFileSync(fixture, 'official archive'); + rmSync(localArchive); + rmSync(executable); + rmSync(path.dirname(cacheArchive), { recursive: true }); + execSync(installer, { env }); + expect(readFileSync(cacheArchive, 'utf8')).toBe('official archive'); + expect(readFileSync(curlLog, 'utf8')).toBe( + 'download\ndownload\ndownload\n', + ); + rmSync(localArchive); rmSync(executable); rmSync(cacheArchive); mkdirSync(cacheArchive); - execSync(installer, { env }); + const result = spawnSync(installer, { env, shell: true }); + expect(result.status).toBe(0); + expect(result.stderr.toString()).toContain('EISDIR'); + expect(result.stderr.toString()).toContain( + 'Warning: could not persist linter archive', + ); expect(readFileSync(executable, 'utf8')).toBe('official archive'); expect(statSync(cacheArchive).isDirectory()).toBe(true); expect(readFileSync(curlLog, 'utf8')).toBe( - 'download\ndownload\ndownload\n', + 'download\ndownload\ndownload\ndownload\n', ); } finally { rmSync(root, { recursive: true, force: true });