Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(uploads): authorize internal file URLs before download#5049
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
912a983
fix(uploads): authorize internal file URLs before download
waleedlatif1 41ae711
chore(uploads): log denied internal file downloads for rollout telemetry
waleedlatif1 e9b2e11
fix(uploads): derive internal file context from key, not query param
waleedlatif1 a4bba94
docs(uploads): move context-derivation rationale into TSDoc
waleedlatif1 9f47c14
fix(uploads): match internal file marker in URL path only
waleedlatif1 bb1d384
consolidate access, billing principals
icecrasher321 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| 'use server' | ||
| import type { Logger } from '@sim/logger' | ||
| import { createLogger, type Logger } from '@sim/logger' | ||
| import { getErrorMessage } from '@sim/utils/errors' | ||
| import { getMaxExecutionTimeout } from '@/lib/core/execution-limits' | ||
| import { | ||
| @@ -25,6 +25,8 @@ import { | ||
| import { verifyFileAccess } from '@/app/api/files/authorization' | ||
| import type { UserFile } from '@/executor/types' | ||
| const logger = createLogger('FileUtilsServer') | ||
| /** | ||
| * Result type for file input resolution | ||
| */ | ||
| @@ -138,19 +140,62 @@ export async function resolveFileInputToUrl( | ||
| } | ||
| /** | ||
| * Download a file from a URL (internal or external) | ||
| * For internal URLs, uses direct storage access (server-side only) | ||
| * For external URLs, validates DNS/SSRF and uses secure fetch with IP pinning | ||
| * Options for {@link downloadFileFromUrl}. | ||
| */ | ||
| export interface DownloadFileFromUrlOptions { | ||
| /** Download timeout for external URLs. Defaults to the max execution timeout. */ | ||
| timeoutMs?: number | ||
| /** Hard cap on the number of bytes read from the source. */ | ||
| maxBytes?: number | ||
| /** | ||
| * Principal the download is performed on behalf of. Required to authorize | ||
| * internal (`/api/files/serve/...`) URLs: the resolved storage key is checked | ||
| * with {@link verifyFileAccess} before any bytes are read. Without it, internal | ||
| * URLs are rejected (fail closed) so a `/api/files/serve/` substring can never | ||
| * be treated as implicitly trusted. | ||
| */ | ||
| userId?: string | ||
| } | ||
| /** | ||
| * Download a file from a URL (internal or external). | ||
| * | ||
| * For internal URLs, uses direct storage access (server-side only) after | ||
| * authorizing the resolved storage key against `userId`. Context is derived | ||
| * from the key via {@link inferContextFromKey}, never from a caller-controlled | ||
| * `?context=` query param — trusting the param would let a private key be | ||
| * labeled with a world-readable context (e.g. profile-pictures) so | ||
| * {@link verifyFileAccess} short-circuits to granted while the private object is | ||
| * still read. This mirrors how `/api/files/serve` resolves context. | ||
| * | ||
| * For external URLs, validates DNS/SSRF and uses secure fetch with IP pinning. | ||
| */ | ||
| export async function downloadFileFromUrl( | ||
| fileUrl: string, | ||
| timeoutMs = getMaxExecutionTimeout(), | ||
| maxBytes?: number | ||
| options: DownloadFileFromUrlOptions = {} | ||
| ): Promise<Buffer> { | ||
| const { parseInternalFileUrl } = await import('./file-utils') | ||
| const { timeoutMs = getMaxExecutionTimeout(), maxBytes, userId } = options | ||
| if (isInternalFileUrl(fileUrl)) { | ||
| const { key, context } = parseInternalFileUrl(fileUrl) | ||
| if (!userId) { | ||
| logger.warn('Internal file download denied: no userId provided', { fileUrl }) | ||
| throw new Error('Access denied: internal file URL requires an authenticated user') | ||
| } | ||
| const key = extractStorageKey(fileUrl) | ||
| if (!key) { | ||
| logger.warn('Internal file download denied: could not resolve storage key', { fileUrl }) | ||
| throw new Error('Access denied: could not resolve internal file key') | ||
| } | ||
| const context = inferContextFromKey(key) | ||
| const hasAccess = await verifyFileAccess(key, userId, undefined, context, false) | ||
waleedlatif1 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (!hasAccess) { | ||
| logger.warn('Internal file download denied: access check failed', { key, context, userId }) | ||
| throw new Error('Access denied: file not found or insufficient permissions') | ||
| } | ||
| const { downloadFile } = await import('@/lib/uploads/core/storage-service') | ||
| return downloadFile({ key, context, maxBytes }) | ||
waleedlatif1 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.