-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(uploads): treat a missing storage object as absent metadata, not a failure #6378
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e93bf2e
fix(uploads): treat a missing storage object as absent metadata, not …
waleedlatif1 f3f3607
fix(uploads): read the not-found label from code as well as name
waleedlatif1 b9ef75e
fix(uploads): keep a missing bucket or container out of the not-found…
waleedlatif1 48133e9
fix(uploads): require an object-level label before treating a lookup …
waleedlatif1 bcde675
refactor(uploads): let getFileMetadata delegate to the provider head …
waleedlatif1 fb85639
fix(files): log a missing file at info rather than error when serving
waleedlatif1 fac2ed9
test(uploads): cover the Blob not-found paths the shared predicate no…
waleedlatif1 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
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
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,69 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { isObjectNotFoundError } from '@/lib/uploads/core/errors' | ||
|
|
||
| describe('isObjectNotFoundError', () => { | ||
| it('matches the shapes each storage provider uses for a missing object', () => { | ||
| /** S3 HeadObject, exactly as the production payload arrived. */ | ||
| expect( | ||
| isObjectNotFoundError({ | ||
| name: 'NotFound', | ||
| $fault: 'client', | ||
| $metadata: { httpStatusCode: 404 }, | ||
| }) | ||
| ).toBe(true) | ||
| /** S3 GetObject. */ | ||
| expect(isObjectNotFoundError({ name: 'NoSuchKey', $metadata: { httpStatusCode: 404 } })).toBe( | ||
| true | ||
| ) | ||
| /** Azure Blob. */ | ||
| expect(isObjectNotFoundError({ code: 'BlobNotFound', statusCode: 404 })).toBe(true) | ||
| /** GCS, which reports a numeric code. */ | ||
| expect(isObjectNotFoundError({ code: 404 })).toBe(true) | ||
| }) | ||
|
|
||
| it('reads the label from code when name carries the error class instead', () => { | ||
| /** Azure raises a `RestError`; the reason lives in `code`, not `name`. */ | ||
| expect(isObjectNotFoundError({ name: 'RestError', code: 'BlobNotFound' })).toBe(true) | ||
| expect(isObjectNotFoundError({ name: 'Error', code: 'NoSuchKey' })).toBe(true) | ||
| }) | ||
|
|
||
| it('does not read a missing bucket or container as an absent object', () => { | ||
| /** | ||
| * These answer 404 too. Reading them as absence would turn a total storage | ||
| * misconfiguration into silent fail-closed reads with nothing to alert on. | ||
| */ | ||
| expect( | ||
| isObjectNotFoundError({ name: 'NoSuchBucket', $metadata: { httpStatusCode: 404 } }) | ||
| ).toBe(false) | ||
| expect( | ||
| isObjectNotFoundError({ name: 'RestError', code: 'ContainerNotFound', statusCode: 404 }) | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| it('matches on status alone when the provider sends no label', () => { | ||
| expect(isObjectNotFoundError({ $metadata: { httpStatusCode: 404 } })).toBe(true) | ||
| expect(isObjectNotFoundError({ statusCode: 404 })).toBe(true) | ||
| }) | ||
|
|
||
| it('does not swallow a genuine failure', () => { | ||
| expect( | ||
| isObjectNotFoundError({ name: 'AccessDenied', $metadata: { httpStatusCode: 403 } }) | ||
| ).toBe(false) | ||
| expect( | ||
| isObjectNotFoundError({ name: 'InternalError', $metadata: { httpStatusCode: 500 } }) | ||
| ).toBe(false) | ||
| expect(isObjectNotFoundError({ name: 'TimeoutError' })).toBe(false) | ||
| expect(isObjectNotFoundError({ code: 'ECONNRESET' })).toBe(false) | ||
| expect(isObjectNotFoundError({ code: 403 })).toBe(false) | ||
| }) | ||
|
|
||
| it('tolerates values that are not error objects', () => { | ||
| expect(isObjectNotFoundError(null)).toBe(false) | ||
| expect(isObjectNotFoundError(undefined)).toBe(false) | ||
| expect(isObjectNotFoundError('NotFound')).toBe(false) | ||
| expect(isObjectNotFoundError(404)).toBe(false) | ||
| }) | ||
| }) |
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,47 @@ | ||
| const OBJECT_NOT_FOUND_LABELS = new Set(['NotFound', 'NoSuchKey', 'BlobNotFound']) | ||
|
|
||
| /** | ||
| * A missing bucket or container is a misconfiguration, not an absent object, and | ||
| * it also answers 404. Without this it would read as "no metadata" and every file | ||
| * read would fail closed with no error to alert on. | ||
| */ | ||
| const CONTAINER_NOT_FOUND_LABELS = new Set(['NoSuchBucket', 'ContainerNotFound']) | ||
|
|
||
| function readLabels(error: unknown): string[] | null { | ||
| if (!error || typeof error !== 'object') return null | ||
| const { name, code } = error as { name?: unknown; code?: unknown } | ||
| /** | ||
| * `name` and `code` are both consulted: Azure raises a `RestError` whose `name` | ||
| * carries the class and whose `code` carries the reason, while the AWS SDK puts | ||
| * the reason in `name`. | ||
| */ | ||
| return [name, code].filter((value): value is string => typeof value === 'string') | ||
| } | ||
|
|
||
| /** | ||
| * True when a storage provider reports that an object does not exist. | ||
| * | ||
| * Call this only from code that has just performed an object-level operation, so a | ||
| * bare 404 can be attributed to that object. A bare 404 is otherwise ambiguous — | ||
| * GCS answers a missing object and a missing bucket identically (`code: 404`, | ||
| * `errors[].reason: 'notFound'`), separable only by a human-readable message — and | ||
| * every caller here is a provider client that knows exactly what it asked for. | ||
| * | ||
| * Absence is an expected outcome of a lookup, so callers turn it into an empty | ||
| * result rather than propagating it. | ||
| * | ||
| * A network failure, a permission denial, or a provider 5xx still propagates. | ||
| */ | ||
| export function isObjectNotFoundError(error: unknown): boolean { | ||
| const labels = readLabels(error) | ||
| if (!labels) return false | ||
| if (labels.some((label) => CONTAINER_NOT_FOUND_LABELS.has(label))) return false | ||
| if (labels.some((label) => OBJECT_NOT_FOUND_LABELS.has(label))) return true | ||
|
|
||
| const { code, statusCode, $metadata } = error as { | ||
| code?: unknown | ||
| statusCode?: unknown | ||
| $metadata?: { httpStatusCode?: unknown } | ||
| } | ||
| return code === 404 || statusCode === 404 || $metadata?.httpStatusCode === 404 | ||
| } | ||
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,78 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { mockGetFileMetadataByKey, mockHeadS3Object } = vi.hoisted(() => ({ | ||
| mockGetFileMetadataByKey: vi.fn(), | ||
| mockHeadS3Object: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/uploads/config', () => ({ | ||
| USE_S3_STORAGE: true, | ||
| USE_BLOB_STORAGE: false, | ||
| USE_GCS_STORAGE: false, | ||
| S3_CONFIG: { bucket: 'bucket', region: 'region' }, | ||
| })) | ||
|
|
||
| vi.mock('@/lib/uploads/providers/s3/client', () => ({ | ||
| headS3Object: mockHeadS3Object, | ||
| })) | ||
|
|
||
| vi.mock('@/lib/uploads/server/metadata', () => ({ | ||
| getFileMetadataByKey: mockGetFileMetadataByKey, | ||
| })) | ||
|
|
||
| import { getFileMetadata } from '@/lib/uploads/core/storage-client' | ||
|
|
||
| describe('getFileMetadata', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| mockGetFileMetadataByKey.mockResolvedValue(null) | ||
| }) | ||
|
|
||
| it('reports an absent object as no metadata rather than throwing', async () => { | ||
| /** The provider client owns not-found and reports absence as `null`. */ | ||
| mockHeadS3Object.mockResolvedValue(null) | ||
|
|
||
| await expect(getFileMetadata('workspace/ws/superseded-key.md')).resolves.toEqual({}) | ||
| }) | ||
|
|
||
| it('still propagates a genuine storage failure', async () => { | ||
| mockHeadS3Object.mockRejectedValue( | ||
| Object.assign(new Error('AccessDenied'), { | ||
| name: 'AccessDenied', | ||
| $metadata: { httpStatusCode: 403 }, | ||
| }) | ||
| ) | ||
|
|
||
| await expect(getFileMetadata('workspace/ws/key.md')).rejects.toThrow('AccessDenied') | ||
| }) | ||
|
|
||
| it('returns provider metadata when the object exists', async () => { | ||
| mockHeadS3Object.mockResolvedValue({ size: 12, metadata: { workspaceid: 'ws-1' } }) | ||
|
|
||
| await expect(getFileMetadata('workspace/ws/key.md')).resolves.toEqual({ workspaceid: 'ws-1' }) | ||
| }) | ||
|
|
||
| it('treats an object carrying no metadata as no metadata', async () => { | ||
| mockHeadS3Object.mockResolvedValue({ size: 12 }) | ||
|
|
||
| await expect(getFileMetadata('workspace/ws/key.md')).resolves.toEqual({}) | ||
| }) | ||
|
|
||
| it('prefers the database record when one exists', async () => { | ||
| mockGetFileMetadataByKey.mockResolvedValue({ | ||
| userId: 'user-1', | ||
| workspaceId: 'ws-1', | ||
| originalName: 'doc.md', | ||
| uploadedAt: new Date('2026-01-01T00:00:00Z'), | ||
| context: 'workspace', | ||
| }) | ||
|
|
||
| const metadata = await getFileMetadata('workspace/ws/key.md') | ||
|
|
||
| expect(metadata.workspaceId).toBe('ws-1') | ||
| expect(mockHeadS3Object).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
Oops, something went wrong.
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.