提交记录修改.github/workflows/windows-build.yml+5 −4看文件
修改scripts/bundle-qemu-sources.sh+3 −0看文件
修改scripts/esigner.mjs+20 −6看文件
修改scripts/esigner.test.mjs+24 −1看文件
修改scripts/prepare-qemu-release.ps1+3 −2看文件
| @@ -51,7 +51,7 @@ jobs: | ||
| 51 | 51 | msystem: UCRT64 |
| 52 | 52 | update: true |
| 53 | 53 | install: >- |
| 54 | 删除: make patch diffutils | |
| 54 | 新增: make patch diffutils git findutils curl | |
| 55 | 55 | mingw-w64-ucrt-x86_64-gcc |
| 56 | 56 | mingw-w64-ucrt-x86_64-glib2 |
| 57 | 57 | mingw-w64-ucrt-x86_64-pixman |
| @@ -131,7 +131,7 @@ jobs: | ||
| 131 | 131 | msystem: UCRT64 |
| 132 | 132 | update: true |
| 133 | 133 | install: >- |
| 134 | 删除: make patch diffutils | |
| 134 | 新增: make patch diffutils git findutils curl | |
| 135 | 135 | mingw-w64-ucrt-x86_64-gcc |
| 136 | 136 | mingw-w64-ucrt-x86_64-glib2 |
| 137 | 137 | mingw-w64-ucrt-x86_64-pixman |
| @@ -154,6 +154,7 @@ jobs: | ||
| 154 | 154 | ./scripts/build-qemu-windows.ps1 |
| 155 | 155 | $identity = Get-Content '_output/windows-deps/qemu-cage/cage-build.json' -Raw | ConvertFrom-Json |
| 156 | 156 | "fingerprint=$($identity.fingerprint)" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8 |
| 157 | 新增: "manifest=$((Get-FileHash '_output/windows-deps/qemu-cage/cage-build.json' -Algorithm SHA256).Hash)" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8 | |
| 157 | 158 | $collectorHash = (Get-FileHash 'scripts/bundle-qemu-sources.sh' -Algorithm SHA256).Hash + (Get-FileHash 'scripts/prepare-qemu-release.ps1' -Algorithm SHA256).Hash |
| 158 | 159 | "collector=$collectorHash" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8 |
| 159 | 160 | - name: Preserve only the completed verified runtime cache |
| @@ -166,7 +167,7 @@ jobs: | ||
| 166 | 167 | id: qemu_sources |
| 167 | 168 | with: |
| 168 | 169 | path: _output/windows-qemu-sources |
| 169 | 删除: key: cage-qemu-sources-${{ steps.qemu_build.outputs.fingerprint }}-${{ steps.qemu_build.outputs.collector }} | |
| 170 | 新增: key: cage-qemu-sources-${{ steps.qemu_build.outputs.fingerprint }}-${{ steps.qemu_build.outputs.manifest }}-${{ steps.qemu_build.outputs.collector }} | |
| 170 | 171 | - name: Collect and verify corresponding sources before signing |
| 171 | 172 | env: |
| 172 | 173 | CAGE_MSYS2_ROOT: ${{ steps.msys2.outputs.msys2-location }} |
| @@ -176,7 +177,7 @@ jobs: | ||
| 176 | 177 | uses: actions/cache/save@v4 |
| 177 | 178 | with: |
| 178 | 179 | path: _output/windows-qemu-sources |
| 179 | 删除: key: cage-qemu-sources-${{ steps.qemu_build.outputs.fingerprint }}-${{ steps.qemu_build.outputs.collector }} | |
| 180 | 新增: key: cage-qemu-sources-${{ steps.qemu_build.outputs.fingerprint }}-${{ steps.qemu_build.outputs.manifest }}-${{ steps.qemu_build.outputs.collector }} | |
| 180 | 181 | - uses: actions/setup-java@v4 |
| 181 | 182 | with: |
| 182 | 183 | distribution: temurin |
| @@ -6,6 +6,9 @@ runtime=$(cygpath -u "$2") | ||
| 6 | 6 | archive=$(cygpath -u "$3") |
| 7 | 7 | node_command=${CAGE_NODE:-node} |
| 8 | 8 | if [[ -n ${CAGE_NODE:-} ]]; then node_command=$(cygpath -u "$CAGE_NODE"); fi |
| 9 | 新增:for tool in "$node_command" git curl tar sha256sum find sort xargs pacman-key mkdir mktemp cp mv rm grep; do | |
| 10 | 新增: command -v "$tool" >/dev/null 2>&1 || { echo "Missing source-collection tool: $tool" >&2; exit 1; } | |
| 11 | 新增:done | |
| 9 | 12 | commit=$("$node_command" -p "JSON.parse(require('fs').readFileSync(process.argv[1])).qemuSource.commit" "$repository/packaging/windows/dependencies.json") |
| 10 | 13 | [[ $commit =~ ^[a-f0-9]{40}$ && -s "$runtime/build-source-packages.txt" ]] || { echo 'QEMU source/package identity is missing.' >&2; exit 1; } |
| 11 | 14 | stage=$(mktemp -d) |
| @@ -12,6 +12,8 @@ const root = fileURLToPath(new URL('../', import.meta.url)); | ||
| 12 | 12 | const pin = JSON.parse(await readFile(new URL('../packaging/windows/signing.json', import.meta.url), 'utf8')); |
| 13 | 13 | const configPath = 'conf/code_sign_tool.properties'; |
| 14 | 14 | const secretNames = ['CAGE_ESIGNER_PASSWORD', 'CAGE_ESIGNER_TOTP_SECRET']; |
| 15 | 新增:const providerCodes = ['server_error', 'invalid_grant', 'invalid_client', 'access_denied', 'unauthorized', 'invalid_request', 'invalid_otp', 'malware_detected']; | |
| 16 | 新增:const rejectedOrUncertain = /timeout|timed out|abort|cancel|invalid_grant|invalid_client|access_denied|unauthorized|(?:otp|totp|authentication|authorization|credential|password)[^\n]*(?:invalid|expired|failed|rejected)|(?:invalid|expired|failed|rejected)[^\n]*(?:otp|totp|authentication|credential|password)/i; | |
| 15 | 17 | const hash = bytes => createHash('sha256').update(bytes).digest('hex'); |
| 16 | 18 | const toolDirectory = () => resolve(process.env.CAGE_ESIGNER_TOOL_DIR || join(root, '_output/esigner', `CodeSignTool-v${pin.version}`)); |
| 17 | 19 | |
| @@ -124,7 +126,19 @@ export async function invokeJava({ java = 'java', jar, config, arguments: args, | ||
| 124 | 126 | if (otpError) throw otpError; |
| 125 | 127 | const output = redact(`${stdout}\n${stderr}`, knownSecrets); |
| 126 | 128 | if (code !== 0 || /\b(?:Error:|Exception|Missing required option|Unmatched argument)/u.test(output)) { |
| 127 | 删除: throw new Error(`CodeSignTool failed (exit ${code}). ${output.slice(-2500).trim()}`); | |
| 129 | 新增: let privateLog = ''; | |
| 130 | 新增: try { privateLog = await readFile(join(temp, 'logs', 'code_signing_tool.log'), 'utf8'); } catch { /* Optional diagnostic only. */ } | |
| 131 | 新增: const stages = { getMalwareScanResponse: 'malware-scan', getScanSettingsResponse: 'scan-settings', getCredentialsAuthorize: 'credentials-authorize', signHash: 'sign-hash', isOtpTypeOnline: 'otp-type', sendOtp: 'send-otp', getCredentialInfo: 'credentials-info', getCredentialIDs: 'credentials-list' }; | |
| 132 | 新增: const method = /^\s*at com\.ssl\.code\.signing\.tool\.csc\.CscApi\.(getMalwareScanResponse|getScanSettingsResponse|getCredentialsAuthorize|signHash|isOtpTypeOnline|sendOtp|getCredentialInfo|getCredentialIDs)\(/m.exec(privateLog)?.[1]; | |
| 133 | 新增: const failureStage = stages[method] || 'unknown'; | |
| 134 | 新增: const rawCode = /"error"\s*:\s*"([a-z_]+)"/.exec(output)?.[1]; | |
| 135 | 新增: const providerCode = providerCodes.includes(rawCode) ? rawCode : 'unclassified'; | |
| 136 | 新增: const quotaExceeded = /QuotaExceededError/.test(privateLog + output); | |
| 137 | 新增: // Never attach the raw response or vendor log to the exception. These | |
| 138 | 新增: // fixed classifications are the only diagnostic values leaving memory. | |
| 139 | 新增: throw Object.assign(new Error(`CodeSignTool failed (exit ${code}). stage=${failureStage}; error=${providerCode}; quotaExceeded=${quotaExceeded}.`), { | |
| 140 | 新增: failureStage, providerCode, quotaExceeded, nonRetryable: quotaExceeded || rejectedOrUncertain.test(output), | |
| 141 | 新增: }); | |
| 128 | 142 | } |
| 129 | 143 | return output; |
| 130 | 144 | } finally { |
| @@ -177,8 +191,9 @@ export async function invokeSigning({ invoke, outputPath, pause = ms => new Prom | ||
| 177 | 191 | catch (error) { |
| 178 | 192 | try { await access(outputPath); return ''; } |
| 179 | 193 | catch (outputError) { if (outputError.code !== 'ENOENT') throw error; } |
| 180 | 删除: const uncertainOrRejected = /timeout|timed out|abort|cancel|invalid_grant|invalid_client|access_denied|unauthorized|(?:otp|totp|authentication|authorization|credential|password)[^\n]*(?:invalid|expired|failed|rejected)/i.test(error.message); | |
| 181 | 删除: if (attempt === 2 || uncertainOrRejected || !/^CodeSignTool failed \(exit 11\)/.test(error.message) || !/"error"\s*:\s*"server_error"/.test(error.message)) throw error; | |
| 194 | 新增: const uncertainOrRejected = error.nonRetryable || error.quotaExceeded || rejectedOrUncertain.test(error.message) || /QuotaExceededError/.test(error.message); | |
| 195 | 新增: const serverError = error.providerCode === 'server_error' || /"error"\s*:\s*"server_error"/.test(error.message); | |
| 196 | 新增: if (attempt === 2 || uncertainOrRejected || !/^CodeSignTool failed \(exit 11\)/.test(error.message) || !serverError) throw error; | |
| 182 | 197 | console.error(`Signing service returned server_error; retrying in ${(attempt + 1) * 15} seconds.`); |
| 183 | 198 | await pause((attempt + 1) * 15000); |
| 184 | 199 | } |
| @@ -191,9 +206,8 @@ export function credentialDiagnostic(error) { | ||
| 191 | 206 | if (!error) return { operation: 'credential_info', success: true, stage: 'csc-credential-info', exitCode: 0, error: '' }; |
| 192 | 207 | const match = /^CodeSignTool failed \(exit ([0-9]{1,3})\)/.exec(error.message); |
| 193 | 208 | const exitCode = match && Number(match[1]) <= 255 ? Number(match[1]) : null; |
| 194 | 删除: const code = /"error"\s*:\s*"([a-z_]+)"/.exec(error.message)?.[1]; | |
| 195 | 删除: const allowed = ['server_error', 'invalid_grant', 'invalid_client', 'access_denied', 'unauthorized', 'invalid_request']; | |
| 196 | 删除: return { operation: 'credential_info', success: false, stage: exitCode === 1 ? 'oauth-token' : exitCode === 2 ? 'csc-credential-info' : 'client', exitCode, error: allowed.includes(code) ? code : 'unclassified' }; | |
| 209 | 新增: const code = error.providerCode || /"error"\s*:\s*"([a-z_]+)"/.exec(error.message)?.[1]; | |
| 210 | 新增: return { operation: 'credential_info', success: false, stage: exitCode === 1 ? 'oauth-token' : exitCode === 2 ? 'csc-credential-info' : 'client', exitCode, error: providerCodes.includes(code) ? code : 'unclassified' }; | |
| 197 | 211 | } |
| 198 | 212 | |
| 199 | 213 | async function main(argv) { |
| @@ -36,12 +36,15 @@ test('signing retries only declared server errors without output and remains bou | ||
| 36 | 36 | count = 0; |
| 37 | 37 | await assert.rejects(invokeSigning({ outputPath, pause: async () => {}, invoke: async () => { count++; throw failure(); } }), /server_error/); |
| 38 | 38 | assert.equal(count, 3); |
| 39 | 删除: for (const message of ['OTP rejected', 'authentication failed', 'timeout', 'CodeSignTool failed (exit 1). server_error', `${failure().message}\nOTP rejected`, `${failure().message}\ntimeout`]) { | |
| 39 | 新增: for (const message of ['OTP rejected', 'authentication failed', 'timeout', 'CodeSignTool failed (exit 1). server_error', `${failure().message}\nOTP rejected`, `${failure().message}\ninvalid otp`, `${failure().message}\ntimeout`]) { | |
| 40 | 40 | count = 0; |
| 41 | 41 | await assert.rejects(invokeSigning({ outputPath, pause: async () => assert.fail('must not wait'), invoke: async () => { count++; throw new Error(message); } })); |
| 42 | 42 | assert.equal(count, 1); |
| 43 | 43 | } |
| 44 | 44 | count = 0; |
| 45 | 新增: await assert.rejects(invokeSigning({ outputPath, pause: async () => assert.fail('quota is not transient'), invoke: async () => { count++; throw Object.assign(failure(), { quotaExceeded: true }); } })); | |
| 46 | 新增: assert.equal(count, 1); | |
| 47 | 新增: count = 0; | |
| 45 | 48 | await invokeSigning({ outputPath, pause: async () => assert.fail('output requires trust verification, never another signature'), invoke: async () => { count++; await writeFile(outputPath, 'candidate output'); throw failure(); } }); |
| 46 | 49 | assert.equal(count, 1); |
| 47 | 50 | assert.equal(await readFile(outputPath, 'utf8'), 'candidate output'); |
| @@ -72,6 +75,11 @@ public class Signer { | ||
| 72 | 75 | String otp = new Scanner(System.in).nextLine(); |
| 73 | 76 | if (!otp.equals("123456")) System.exit(16); |
| 74 | 77 | System.out.println(otp); |
| 78 | 新增: if (Arrays.asList(args).contains("quota")) { | |
| 79 | 新增: Files.writeString(Path.of("logs", "code_signing_tool.log"), "sensitive-user sensitive-id sensitive-token\\n at com.ssl.code.signing.tool.csc.CscApi.getMalwareScanResponse(CscApi.java:1)\\nQuotaExceededError dummy-password"); | |
| 80 | 新增: System.out.println("Error: {\\\"error\\\":\\\"server_error\\\",\\\"token\\\":\\\"sensitive-token\\\"}"); | |
| 81 | 新增: System.exit(11); | |
| 82 | 新增: } | |
| 75 | 83 | if (Arrays.asList(args).contains("fail")) { System.out.println("Error: dummy-password"); System.exit(4); } |
| 76 | 84 | } |
| 77 | 85 | } |
| @@ -104,6 +112,21 @@ public class Signer { | ||
| 104 | 112 | else process.env.CAGE_ESIGNER_PASSWORD = previous; |
| 105 | 113 | } |
| 106 | 114 | } |
| 115 | 新增: const quotaMarker = join(temp, 'quota-marker'); | |
| 116 | 新增: let quotaCalls = 0; | |
| 117 | 新增: await assert.rejects(invokeSigning({ outputPath: join(temp, 'no-signed-output.exe'), pause: async () => assert.fail('quota cannot cause a second signing request'), invoke: () => { | |
| 118 | 新增: quotaCalls++; | |
| 119 | 新增: return invokeJava({ jar, config, arguments: [quotaMarker, 'quota'], otp: async () => '123456', secrets: ['dummy-password'] }); | |
| 120 | 新增: } }), error => { | |
| 121 | 新增: assert.equal(error.failureStage, 'malware-scan'); | |
| 122 | 新增: assert.equal(error.providerCode, 'server_error'); | |
| 123 | 新增: assert.equal(error.quotaExceeded, true); | |
| 124 | 新增: for (const secret of ['sensitive-user', 'sensitive-id', 'sensitive-token', 'dummy-password', '123456']) assert.ok(!`${error.message}${JSON.stringify(error)}`.includes(secret)); | |
| 125 | 新增: return true; | |
| 126 | 新增: }); | |
| 127 | 新增: assert.equal(quotaCalls, 1); | |
| 128 | 新增: const quotaWorking = await readFile(quotaMarker, 'utf8'); | |
| 129 | 新增: await assert.rejects(readFile(join(quotaWorking, 'logs/code_signing_tool.log')), { code: 'ENOENT' }); | |
| 107 | 130 | }); |
| 108 | 131 | |
| 109 | 132 | test('modified vendor binaries are refused before executing Java', async t => { |
| @@ -11,13 +11,14 @@ if (-not $MsysDirectory) { throw 'Set CAGE_MSYS2_ROOT to the MSYS2 UCRT64 build | ||
| 11 | 11 | $runtime = Join-Path $repository '_output/windows-deps/qemu-cage' |
| 12 | 12 | & (Join-Path $PSScriptRoot 'build-qemu-windows.ps1') -OutputDirectory $runtime -MsysDirectory $MsysDirectory |
| 13 | 13 | $runtimeIdentity = Get-Content (Join-Path $runtime 'cage-build.json') -Raw | ConvertFrom-Json |
| 14 | 新增:$runtimeManifestHash = (Get-FileHash -LiteralPath (Join-Path $runtime 'cage-build.json') -Algorithm SHA256).Hash | |
| 14 | 15 | $collectorHash = (Get-FileHash -LiteralPath (Join-Path $PSScriptRoot 'bundle-qemu-sources.sh') -Algorithm SHA256).Hash + (Get-FileHash -LiteralPath $PSCommandPath -Algorithm SHA256).Hash |
| 15 | 16 | $directory = Join-Path $repository '_output/windows-qemu-sources' |
| 16 | 17 | $archive = Join-Path $directory 'full-source.tar.gz' |
| 17 | 18 | $record = Join-Path $directory 'source-identity.json' |
| 18 | 19 | if ((Test-Path -LiteralPath $record) -and (Test-Path -LiteralPath $archive)) { |
| 19 | 20 | $identity = Get-Content -LiteralPath $record -Raw | ConvertFrom-Json |
| 20 | 删除: if ($identity.schema -ne 1 -or $identity.fingerprint -ne $runtimeIdentity.fingerprint -or $identity.collectorHash -ne $collectorHash -or $identity.sha256 -ne (Get-FileHash -LiteralPath $archive -Algorithm SHA256).Hash) { | |
| 21 | 新增: if ($identity.schema -ne 2 -or $identity.fingerprint -ne $runtimeIdentity.fingerprint -or $identity.runtimeManifestHash -ne $runtimeManifestHash -or $identity.collectorHash -ne $collectorHash -or $identity.sha256 -ne (Get-FileHash -LiteralPath $archive -Algorithm SHA256).Hash) { | |
| 21 | 22 | throw 'Cached QEMU sources differ from their verified runtime identity.' |
| 22 | 23 | } |
| 23 | 24 | Write-Host 'Verified cached QEMU corresponding sources.' |
| @@ -31,7 +32,7 @@ try { | ||
| 31 | 32 | Invoke-BuildTool (Join-Path $MsysDirectory 'usr/bin/bash.exe') @('-l', |
| 32 | 33 | (Join-Path $PSScriptRoot 'bundle-qemu-sources.sh'), $repository, $runtime, $archive) |
| 33 | 34 | } finally { $env:MSYSTEM = $savedMsystem; $env:CHERE_INVOKING = $savedChere; $env:CAGE_NODE = $savedNode } |
| 34 | 删除:$identity = @{ schema = 1; fingerprint = $runtimeIdentity.fingerprint; collectorHash = $collectorHash; sha256 = (Get-FileHash -LiteralPath $archive -Algorithm SHA256).Hash } | |
| 35 | 新增:$identity = @{ schema = 2; fingerprint = $runtimeIdentity.fingerprint; runtimeManifestHash = $runtimeManifestHash; collectorHash = $collectorHash; sha256 = (Get-FileHash -LiteralPath $archive -Algorithm SHA256).Hash } | |
| 35 | 36 | $identity | ConvertTo-Json | Set-Content -LiteralPath "$record.partial" -Encoding utf8 |
| 36 | 37 | Move-Item -LiteralPath "$record.partial" -Destination $record -Force |
| 37 | 38 | Write-Host 'Collected and verified QEMU corresponding sources before application signing.' |

