Skip to content

The agent that grows with you

fix(signing): verify embedded signatures behind Windows catalogs

ethernet276174db7eb1
diff --git a/apps/desktop/scripts/payload-sign-cache.mjs b/apps/desktop/scripts/payload-sign-cache.mjs
index 16d020f9e31..4baab136204 100644
--- a/apps/desktop/scripts/payload-sign-cache.mjs
+++ b/apps/desktop/scripts/payload-sign-cache.mjs
@@ -33,7 +33,7 @@ export function peContentHash(file) {
return hash.digest('hex')
}
-export async function verifySignedPayloads(files, publisher) {
+export async function verifySignedPayloads(files, publisher, signtool = '') {
if (!files.length) return new Set()
const temp = fs.mkdtempSync(path.join(os.tmpdir(), 'verify-signatures-'))
try {
@@ -41,7 +41,8 @@ export async function verifySignedPayloads(files, publisher) {
fs.writeFileSync(manifest, JSON.stringify(files.map(file => ({ path: file, publisher }))))
const { stdout } = await exec('powershell.exe', [
'-NoProfile', '-NonInteractive', '-File',
- path.join(import.meta.dirname, 'verify-signed-payloads.ps1'), manifest
+ path.join(import.meta.dirname, 'verify-signed-payloads.ps1'), manifest,
+ ...(signtool ? ['-SignTool', signtool] : [])
], { windowsHide: true, timeout: 600000, maxBuffer: 4 * 1024 * 1024 })
const results = JSON.parse(stdout.replace(/^\uFEFF/, ''))
if (!Array.isArray(results) || results.length !== files.length ||
@@ -91,7 +92,7 @@ export function createPayloadSignCache({ root, env, signtool, dlib, timestampUrl
fs.rmSync(entry, { recursive: true, force: true })
}
}
- const valid = await verify(candidates.map(c => c.binary), publisher)
+ const valid = await verify(candidates.map(c => c.binary), publisher, signtool)
let restored = 0
for (const { group, binary } of candidates) {
if (!valid.has(binary)) {
@@ -111,7 +112,7 @@ export function createPayloadSignCache({ root, env, signtool, dlib, timestampUrl
},
async publish(plan) {
const files = plan.pending.map(g => g.files[0])
- const valid = await verify(files, publisher)
+ const valid = await verify(files, publisher, signtool)
for (const group of plan.pending) {
const file = group.files[0]
if (!valid.has(file) || peContentHash(file) !== group.content) {
diff --git a/apps/desktop/scripts/payload-sign-cache.test.mjs b/apps/desktop/scripts/payload-sign-cache.test.mjs
index d4fc01a76d1..6dc02c28950 100644
--- a/apps/desktop/scripts/payload-sign-cache.test.mjs
+++ b/apps/desktop/scripts/payload-sign-cache.test.mjs
@@ -4,8 +4,12 @@ import os from 'node:os'
import path from 'node:path'
import { execFileSync } from 'node:child_process'
import { expect, test, vi } from 'vitest'
-import { batchSignBinaries } from './batch-sign-binaries.mjs'
+import { batchSignBinaries, resolveSigntool } from './batch-sign-binaries.mjs'
import { createPayloadSignCache, peContentHash, verifySignedPayloads } from './payload-sign-cache.mjs'
+import { readSecurityDirectory } from './sanitize-pe-signatures.mjs'
+
+// The release builder installs this SDK before it runs the native signing tests.
+const nativeSigntool = process.platform === 'win32' ? resolveSigntool() : null
const hash = bytes => createHash('sha256').update(bytes).digest('hex')
@@ -69,6 +73,39 @@ test.runIf(process.platform === 'win32')('native verification binds cached bytes
} finally { f.cleanup() }
}, 30000)
+test.runIf(process.platform === 'win32' && nativeSigntool)('catalog trust cannot mask the embedded signature', async () => {
+ const f = fixture()
+ try {
+ const source = path.join(process.env.SystemRoot, 'System32', 'kernel32.dll')
+ const publisher = execFileSync('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command',
+ '[Security.Cryptography.X509Certificates.X509Certificate]::CreateFromSignedFile($env:NATIVE_SIGN_TEST_INPUT).Subject'
+ ], { encoding: 'utf8', windowsHide: true, env: { ...process.env, NATIVE_SIGN_TEST_INPUT: source } }).trim()
+ expect(publisher).not.toBe('')
+ const files = ['original.dll', 'renamed.exe'].map(name => path.join(f.root, name))
+ for (const file of files) fs.copyFileSync(source, file)
+ const catalog = () => JSON.parse(execFileSync('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command',
+ '$s = Get-AuthenticodeSignature -LiteralPath $env:NATIVE_SIGN_TEST_INPUT; [pscustomobject]@{ Status = [string]$s.Status; Type = [string]$s.SignatureType } | ConvertTo-Json -Compress'
+ ], { encoding: 'utf8', windowsHide: true, env: { ...process.env, NATIVE_SIGN_TEST_INPUT: files[1] } }))
+ expect(catalog()).toEqual({ Status: 'Valid', Type: 'Catalog' })
+ expect(await verifySignedPayloads(files, publisher, nativeSigntool)).toEqual(new Set(files))
+ expect(await verifySignedPayloads(files, 'CN=Wrong publisher', nativeSigntool)).toEqual(new Set())
+
+ // Catalog hashes omit the certificate table. Destroy it without changing code.
+ const bytes = fs.readFileSync(files[1])
+ const { certOffset, certSize } = readSecurityDirectory(files[1])
+ expect(certSize).toBeGreaterThan(8)
+ bytes.fill(0, certOffset + 8, certOffset + certSize)
+ fs.writeFileSync(files[1], bytes)
+ expect(catalog()).toEqual({ Status: 'Valid', Type: 'Catalog' })
+ expect(await verifySignedPayloads(files, publisher, nativeSigntool)).toEqual(new Set([files[0]]))
+
+ fs.copyFileSync(source, files[1])
+ execFileSync(nativeSigntool, ['remove', '/u', files[1]], { windowsHide: true })
+ expect(catalog()).toEqual({ Status: 'Valid', Type: 'Catalog' })
+ expect(await verifySignedPayloads(files, publisher, nativeSigntool)).toEqual(new Set([files[0]]))
+ } finally { f.cleanup() }
+}, 60000)
+
test('input bytes select entries across paths, while policy and executable content stay binding', async () => {
const f = fixture()
try {
diff --git a/apps/desktop/scripts/verify-signed-payloads.ps1 b/apps/desktop/scripts/verify-signed-payloads.ps1
index 3a656084da2..fc486d119cb 100644
--- a/apps/desktop/scripts/verify-signed-payloads.ps1
+++ b/apps/desktop/scripts/verify-signed-payloads.ps1
@@ -1,4 +1,7 @@
-param([Parameter(Mandatory = $true)][string]$Manifest)
+param(
+ [Parameter(Mandatory = $true)][string]$Manifest,
+ [string]$SignTool = ''
+)
$ErrorActionPreference = 'Stop'
[Console]::OutputEncoding = New-Object System.Text.UTF8Encoding($false)
$items = Get-Content -Raw -LiteralPath $Manifest | ConvertFrom-Json
@@ -9,6 +12,23 @@ $results = @(
$signature.SignatureType -eq 'Authenticode' -and
$null -ne $signature.TimeStamperCertificate -and
$signature.SignerCertificate.Subject -eq $item.publisher
+ if ($signature.SignatureType -eq 'Catalog' -and $SignTool) {
+ # Catalog lookup hides a replacement embedded signature. Verify index 0,
+ # without /a, so catalog trust cannot approve corrupt or unsigned bytes.
+ $exitCode = & {
+ # PowerShell 5 turns redirected native stderr into a terminating error.
+ $ErrorActionPreference = 'Continue'
+ $global:LASTEXITCODE = $null
+ & $SignTool verify /pa /tw /ds 0 $item.path *> $null
+ $global:LASTEXITCODE
+ }
+ $valid = $false
+ if ($exitCode -eq 0) {
+ $certificate = [Security.Cryptography.X509Certificates.X509Certificate]::CreateFromSignedFile($item.path)
+ try { $valid = $certificate.Subject -eq $item.publisher }
+ finally { $certificate.Dispose() }
+ }
+ }
[PSCustomObject]@{ path = $item.path; valid = [bool]$valid }
}
)