Repository navigation
fix(dr): retry transient R2 5xx on backup S3 client #1090
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kody-bot
merged 3 commits into
main
from
cursor/sentry-triage-kody-cloudflare-7643617296-5f24
Jul 31, 2026
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| import { expect, test, vi } from 'vitest' | ||
| import { | ||
| createDrBackupS3Client, | ||
| DrBackupPreconditionFailedError, | ||
| isTransientDrBackupHttpStatus, | ||
| readDrBackupS3Config, | ||
| } from './backup-s3.ts' | ||
|
|
||
| const config = { | ||
| accountId: 'acct', | ||
| bucketName: 'backups', | ||
| accessKeyId: 'key', | ||
| secretAccessKey: 'secret', | ||
| } | ||
|
|
||
| function jsonResponse( | ||
| status: number, | ||
| body = '', | ||
| headers: Record<string, string> = {}, | ||
| ) { | ||
| return new Response(body, { status, headers }) | ||
| } | ||
|
|
||
| test('isTransientDrBackupHttpStatus matches platform blips only', () => { | ||
| expect(isTransientDrBackupHttpStatus(429)).toBe(true) | ||
| expect(isTransientDrBackupHttpStatus(500)).toBe(true) | ||
| expect(isTransientDrBackupHttpStatus(502)).toBe(true) | ||
| expect(isTransientDrBackupHttpStatus(503)).toBe(true) | ||
| expect(isTransientDrBackupHttpStatus(504)).toBe(true) | ||
| expect(isTransientDrBackupHttpStatus(400)).toBe(false) | ||
| expect(isTransientDrBackupHttpStatus(403)).toBe(false) | ||
| expect(isTransientDrBackupHttpStatus(404)).toBe(false) | ||
| expect(isTransientDrBackupHttpStatus(412)).toBe(false) | ||
| expect(isTransientDrBackupHttpStatus(200)).toBe(false) | ||
| }) | ||
|
|
||
| test('put retries transient HTTP 500 then succeeds', async () => { | ||
| const fetchImpl = vi | ||
| .fn() | ||
| .mockResolvedValueOnce(jsonResponse(500, 'blip')) | ||
| .mockResolvedValueOnce(jsonResponse(200, '', { etag: '"etag-1"' })) | ||
|
|
||
| const client = createDrBackupS3Client(config, fetchImpl as typeof fetch, { | ||
| maxAttempts: 3, | ||
| baseDelayMs: 1, | ||
| }) | ||
| await expect( | ||
| client.put('staging/day/exporter/progress.json', '{}', { | ||
| contentType: 'application/json', | ||
| ifMatch: '"prev"', | ||
| }), | ||
| ).resolves.toEqual({ etag: '"etag-1"' }) | ||
| expect(fetchImpl).toHaveBeenCalledTimes(2) | ||
| const secondInit = fetchImpl.mock.calls[1]?.[0] as Request | ||
| expect(secondInit.method).toBe('PUT') | ||
| expect(secondInit.headers.get('If-Match')).toBe('"prev"') | ||
| }) | ||
|
|
||
| test('put does not retry precondition failures', async () => { | ||
| const fetchImpl = vi.fn().mockResolvedValue(jsonResponse(412, 'conflict')) | ||
| const client = createDrBackupS3Client(config, fetchImpl as typeof fetch, { | ||
| maxAttempts: 4, | ||
| baseDelayMs: 1, | ||
| }) | ||
| await expect( | ||
| client.put('staging/day/exporter/progress.json', '{}', { | ||
| ifMatch: '"prev"', | ||
| }), | ||
| ).rejects.toBeInstanceOf(DrBackupPreconditionFailedError) | ||
| expect(fetchImpl).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| test('put retries transport errors then surfaces exhausted failures', async () => { | ||
| const fetchImpl = vi | ||
| .fn() | ||
| .mockRejectedValueOnce(new TypeError('network down')) | ||
| .mockResolvedValueOnce(jsonResponse(500, 'still bad')) | ||
| .mockResolvedValueOnce(jsonResponse(500, 'still bad')) | ||
|
|
||
| const client = createDrBackupS3Client(config, fetchImpl as typeof fetch, { | ||
| maxAttempts: 3, | ||
| baseDelayMs: 1, | ||
| }) | ||
| await expect(client.put('key', 'body')).rejects.toThrow( | ||
| 'DR backup PUT failed for key: HTTP 500', | ||
| ) | ||
| expect(fetchImpl).toHaveBeenCalledTimes(3) | ||
| }) | ||
|
|
||
| test('head and getText retry transient 503 responses', async () => { | ||
| const headFetch = vi | ||
| .fn() | ||
| .mockResolvedValueOnce(jsonResponse(503)) | ||
| .mockResolvedValueOnce(jsonResponse(200, '', { etag: '"h"' })) | ||
| const headClient = createDrBackupS3Client( | ||
| config, | ||
| headFetch as typeof fetch, | ||
| { maxAttempts: 2, baseDelayMs: 1 }, | ||
| ) | ||
| await expect(headClient.head('blob')).resolves.toEqual({ | ||
| exists: true, | ||
| status: 200, | ||
| etag: '"h"', | ||
| }) | ||
|
|
||
| const getFetch = vi | ||
| .fn() | ||
| .mockResolvedValueOnce(jsonResponse(503, 'busy')) | ||
| .mockResolvedValueOnce(jsonResponse(200, '{"ok":true}', { etag: '"g"' })) | ||
| const getClient = createDrBackupS3Client(config, getFetch as typeof fetch, { | ||
| maxAttempts: 2, | ||
| baseDelayMs: 1, | ||
| }) | ||
| await expect(getClient.getText('progress.json')).resolves.toEqual({ | ||
| text: '{"ok":true}', | ||
| etag: '"g"', | ||
| }) | ||
| }) | ||
|
|
||
| test('readDrBackupS3Config requires all fields', () => { | ||
| expect( | ||
| readDrBackupS3Config({ | ||
| DR_BACKUP_ACCOUNT_ID: 'a', | ||
| DR_BACKUP_BUCKET_NAME: 'b', | ||
| DR_BACKUP_ACCESS_KEY_ID: 'c', | ||
| DR_BACKUP_SECRET_ACCESS_KEY: 'd', | ||
| }), | ||
| ).toEqual({ | ||
| accountId: 'a', | ||
| bucketName: 'b', | ||
| accessKeyId: 'c', | ||
| secretAccessKey: 'd', | ||
| }) | ||
| expect( | ||
| readDrBackupS3Config({ | ||
| DR_BACKUP_ACCOUNT_ID: 'a', | ||
| DR_BACKUP_BUCKET_NAME: '', | ||
| DR_BACKUP_ACCESS_KEY_ID: 'c', | ||
| DR_BACKUP_SECRET_ACCESS_KEY: 'd', | ||
| }), | ||
| ).toBeNull() | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift
Namespace DR backup object access by
userId.The client signs the caller-provided
keydirectly, and its API has nouserId. HEAD, GET, and PUT therefore access one shared bucket keyspace without tenant isolation.packages/worker/src/dr/backup-s3.ts#L105-L108: require a validateduserIdat the client or operation boundary and derive the object key from that namespace before signing.packages/worker/src/dr/backup-s3.node.test.ts#L9-L14: update the fixture and add assertions that distinct user IDs produce isolated object keys.As per coding guidelines, “every read and write path must be scoped by
userId.”📍 Affects 2 files
packages/worker/src/dr/backup-s3.ts#L105-L108(this comment)packages/worker/src/dr/backup-s3.node.test.ts#L9-L14🤖 Prompt for AI Agents
Source: Coding guidelines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not applicable here: this client is the platform disaster-recovery backup path (
backup-control-plane), which intentionally writes a shared staging/sealed backup keyspace for the whole deployment. Per-user isolation applies to user-owned storage/MCP paths, not this operator backup bucket. AddinguserIdnamespacing would break the exporter/restore contract.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.