Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions packages/deploy-cloudflare/src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RecentCloudflareVersion> | undefined;
let trustedCandidateChain = false;
if (options.expectedPreviousVersionId !== undefined || options.verifyInheritedSecrets === true) {
Expand All @@ -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 {
Expand Down
40 changes: 40 additions & 0 deletions packages/deploy-cloudflare/test/unit/version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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-'));
Expand Down
11 changes: 9 additions & 2 deletions packages/void/src/cli/cloudflare-candidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand Down
35 changes: 35 additions & 0 deletions packages/void/test/unit/cloudflare-candidate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
});
});
Loading