diff --git a/packages/deploy-cloudflare/src/version.ts b/packages/deploy-cloudflare/src/version.ts index dc8dbf445..0803ccd8d 100644 --- a/packages/deploy-cloudflare/src/version.ts +++ b/packages/deploy-cloudflare/src/version.ts @@ -838,6 +838,14 @@ async function activateCloudflareVersionWithSecretsFile( return { ok: false, reason: 'uploader-changed' }; } const previousVersionId = previous.versionId; + // Activation can succeed before trigger synchronization or the local receipt + // cleanup fails. Once its last recorded candidate owns all live traffic, the + // receipt no longer authorizes any *inactive* successor. The history checks + // below still reject a newer upload from another operation. + const trustedInheritanceVersionIds = + previousVersionId && options.trustedInheritanceVersionIds?.at(-1) === previousVersionId + ? [] + : options.trustedInheritanceVersionIds; let inheritanceBaseline: Array | undefined; let trustedCandidateChain = false; if (options.expectedPreviousVersionId !== undefined || options.verifyInheritedSecrets === true) { @@ -848,22 +856,22 @@ async function activateCloudflareVersionWithSecretsFile( const latestVersionId = recent.at(-1)?.id ?? null; if ( options.expectedPreviousVersionId !== undefined || - options.trustedInheritanceVersionIds !== undefined + trustedInheritanceVersionIds !== undefined ) { const activeIndex = recent.findIndex((version) => version.id === previous.versionId); const inheritedChain = activeIndex < 0 ? [] : recent.slice(activeIndex); const inheritedCandidates = inheritedChain.slice(1).map((version) => version.id); trustedCandidateChain = - options.trustedInheritanceVersionIds !== undefined && - options.trustedInheritanceVersionIds.length > 0 && - arraysEqual(inheritedCandidates, options.trustedInheritanceVersionIds); + trustedInheritanceVersionIds !== undefined && + trustedInheritanceVersionIds.length > 0 && + arraysEqual(inheritedCandidates, trustedInheritanceVersionIds); const safeInheritanceChain = inheritedChain.length > 0 && inheritedChain.every( (version, index) => index === 0 || version.number === inheritedChain[index - 1]!.number + 1, ) && - (arraysEqual(inheritedCandidates, options.trustedInheritanceVersionIds ?? []) || + (arraysEqual(inheritedCandidates, trustedInheritanceVersionIds ?? []) || options.pinActiveSecrets !== undefined); if (!safeInheritanceChain) { return { diff --git a/packages/deploy-cloudflare/test/unit/version.test.ts b/packages/deploy-cloudflare/test/unit/version.test.ts index 3c3f4289b..c40978e1d 100644 --- a/packages/deploy-cloudflare/test/unit/version.test.ts +++ b/packages/deploy-cloudflare/test/unit/version.test.ts @@ -376,6 +376,46 @@ describe('Cloudflare version activation', () => { ).resolves.toMatchObject({ ok: true, versionId: NEXT }); }); + it('recovers a receipt whose last candidate already owns production', async () => { + const runWrangler = runner(); + const recordVerifiedCandidate = vi.fn(); + await expect( + activateCloudflareVersion({ + runWrangler, + workerName: 'my-worker', + verifyUploader: () => true, + verifyInheritedSecrets: true, + trustedInheritanceVersionIds: [CONCURRENT, CURRENT], + message: encodeCloudflareVersionMessage([]), + fetch: async () => Response.json({ ready: true }), + recordVerifiedCandidate, + }), + ).resolves.toMatchObject({ ok: true, versionId: NEXT, previousVersionId: CURRENT }); + expect(recordVerifiedCandidate).toHaveBeenCalledWith(NEXT, CURRENT); + }); + + it('still rejects a foreign upload after the recorded candidate became active', async () => { + const runWrangler = runner({ + versions: ok( + JSON.stringify([ + { id: CURRENT, number: 1 }, + { id: CONCURRENT, number: 2 }, + ]), + ), + }); + await expect( + activateCloudflareVersion({ + runWrangler, + workerName: 'my-worker', + verifyUploader: () => true, + verifyInheritedSecrets: true, + trustedInheritanceVersionIds: [CURRENT], + message: encodeCloudflareVersionMessage([]), + }), + ).resolves.toMatchObject({ ok: false, reason: 'inheritance-source-changed' }); + expect(runWrangler.mock.calls.some(([args]) => args[1] === 'upload')).toBe(false); + }); + it('does not inherit any remote secret from a failed candidate newer than production', async () => { const failed = '44444444-4444-4444-8444-444444444444'; const directory = mkdtempSync(join(tmpdir(), 'void-active-secret-inheritance-')); diff --git a/packages/void/src/cli/cloudflare-candidate.ts b/packages/void/src/cli/cloudflare-candidate.ts index b11d452d3..762b8bbc8 100644 --- a/packages/void/src/cli/cloudflare-candidate.ts +++ b/packages/void/src/cli/cloudflare-candidate.ts @@ -81,13 +81,20 @@ export function recordCloudflareCandidateUpload( expected && (expected.accountId !== accountId || expected.workerName !== workerName || - expected.parentVersionId !== parentVersionId) + (expected.parentVersionId !== parentVersionId && + expected.versionIds.at(-1) !== parentVersionId)) ) { throw new Error( 'The saved Cloudflare candidate belongs to a different Worker or live version. Inspect the deployment before retrying.', ); } - const versionIds = [...(expected?.versionIds ?? []), versionId]; + // The activation runner supplies its verified live parent. If the last saved + // candidate already became that parent, begin a new chain instead of retaining + // the interrupted deployment's old parent and inactive-version authorization. + const versionIds = [ + ...(expected?.parentVersionId === parentVersionId ? expected.versionIds : []), + versionId, + ]; mkdirSync(join(root, '.void'), { recursive: true }); const path = join(root, CANDIDATE_FILE); const temp = `${path}.${process.pid}.tmp`; diff --git a/packages/void/test/unit/cloudflare-candidate.test.ts b/packages/void/test/unit/cloudflare-candidate.test.ts index b420bc5e6..2e3555ddd 100644 --- a/packages/void/test/unit/cloudflare-candidate.test.ts +++ b/packages/void/test/unit/cloudflare-candidate.test.ts @@ -52,4 +52,39 @@ describe('ordinary Cloudflare candidate receipt', () => { rmSync(root, { recursive: true, force: true }); } }); + + it('starts a new chain only when the verified parent is the last saved candidate', () => { + const root = mkdtempSync(join(tmpdir(), 'void-candidate-activated-')); + try { + recordCloudflareCandidateUpload(root, null, 'account', 'worker', 'live', 'draft'); + recordCloudflareCandidateUpload( + root, + readCloudflareCandidate(root), + 'account', + 'worker', + 'live', + 'draft-2', + ); + const receipt = readCloudflareCandidate(root); + expect(() => + recordCloudflareCandidateUpload(root, receipt, 'account', 'worker', 'draft', 'next'), + ).toThrow('different Worker'); + expect(() => + recordCloudflareCandidateUpload(root, receipt, 'other', 'worker', 'draft-2', 'next'), + ).toThrow('different Worker'); + expect(() => + recordCloudflareCandidateUpload(root, receipt, 'account', 'other', 'draft-2', 'next'), + ).toThrow('different Worker'); + expect(readCloudflareCandidate(root)).toEqual(receipt); + recordCloudflareCandidateUpload(root, receipt, 'account', 'worker', 'draft-2', 'next'); + expect(readCloudflareCandidate(root)).toEqual({ + accountId: 'account', + workerName: 'worker', + parentVersionId: 'draft-2', + versionIds: ['next'], + }); + } finally { + rmSync(root, { recursive: true, force: true }); + } + }); });