-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat(files): upload and safely extract ZIP archives #6782
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
11 commits
Select commit
Hold shift + click to select a range
6e4fd82
feat(files): support zip extraction
j15z d546097
fix(files): harden zip extraction safety
j15z c779a15
fix(files): batch extraction notifications
j15z f139e28
fix(files): defer rollback storage cleanup
j15z 0d255a2
fix(files): restore reliable drag uploads
j15z 16a774a
fix(files): use explicit archive extraction route
j15z 5393ee3
Merge remote-tracking branch 'origin/staging' into feat/uploading-and…
j15z 2d9ed10
fix(ci): account for archive extraction route
j15z 07661d5
refactor(files): bound every archive extraction and trim the extracto…
waleedlatif1 ef57474
fix(files): bound the extraction write loop so it cannot outlive its …
waleedlatif1 52b5c2f
fix(files): only remap the deadline abort itself, and stop overclaimi…
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
82 changes: 82 additions & 0 deletions
82
apps/sim/app/api/workspaces/[id]/files/[fileId]/extract/route.test.ts
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,82 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { NextRequest } from 'next/server' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| getSession: vi.fn(), | ||
| extract: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/auth', () => ({ getSession: mocks.getSession })) | ||
|
|
||
| vi.mock('@/lib/workspace-files/application/extract-workspace-file', () => ({ | ||
| extractWorkspaceFile: { | ||
| operation: { id: 'files.extract_archive', minimumRole: 'write', workspaceApiKey: 'deny' }, | ||
| execute: mocks.extract, | ||
| }, | ||
| })) | ||
|
|
||
| import { ArchiveError } from '@/lib/uploads/archive' | ||
| import { POST } from '@/app/api/workspaces/[id]/files/[fileId]/extract/route' | ||
|
|
||
| const WORKSPACE_ID = 'workspace-1' | ||
| const FILE_ID = 'wf_1' | ||
| const context = { params: Promise.resolve({ id: WORKSPACE_ID, fileId: FILE_ID }) } | ||
|
|
||
| function callExtract() { | ||
| return POST( | ||
| new NextRequest( | ||
| `http://localhost:3000/api/workspaces/${WORKSPACE_ID}/files/${FILE_ID}/extract`, | ||
| { method: 'POST' } | ||
| ), | ||
| context | ||
| ) | ||
| } | ||
|
|
||
| describe('POST /api/workspaces/[id]/files/[fileId]/extract', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| mocks.getSession.mockResolvedValue({ | ||
| user: { id: 'user-1' }, | ||
| session: { id: 'session-1' }, | ||
| }) | ||
| mocks.extract.mockResolvedValue({ folderName: 'bundle', extractedCount: 2, skippedCount: 0 }) | ||
| }) | ||
|
|
||
| it('passes a session principal and canonical assertion to the extraction use case', async () => { | ||
| const response = await callExtract() | ||
|
|
||
| expect(response.status).toBe(200) | ||
| expect(await response.json()).toEqual({ | ||
| success: true, | ||
| folderName: 'bundle', | ||
| extractedCount: 2, | ||
| skippedCount: 0, | ||
| }) | ||
| expect(mocks.extract).toHaveBeenCalledWith({ | ||
| principal: { kind: 'session', userId: 'user-1', sessionId: 'session-1' }, | ||
| input: { fileId: FILE_ID, assertedWorkspaceId: WORKSPACE_ID }, | ||
| request: expect.anything(), | ||
| }) | ||
| }) | ||
|
|
||
| it('authenticates before invoking extraction', async () => { | ||
| mocks.getSession.mockResolvedValue(null) | ||
|
|
||
| const response = await callExtract() | ||
|
|
||
| expect(response.status).toBe(401) | ||
| expect(mocks.extract).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('returns a caller-safe error for an invalid zip', async () => { | ||
| mocks.extract.mockRejectedValue(new ArchiveError('invalid', 'Not a valid .zip archive.')) | ||
|
|
||
| const response = await callExtract() | ||
|
|
||
| expect(response.status).toBe(400) | ||
| expect(await response.json()).toEqual({ error: 'Not a valid .zip archive.' }) | ||
| }) | ||
| }) |
27 changes: 27 additions & 0 deletions
27
apps/sim/app/api/workspaces/[id]/files/[fileId]/extract/route.ts
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,27 @@ | ||
| import { extractWorkspaceFileContract } from '@/lib/api/contracts/workspace-files' | ||
| import { | ||
| defineInternalJsonRoute, | ||
| internalRateLimits, | ||
| internalSessionAuth, | ||
| } from '@/lib/api/server/routes' | ||
| import { internalFileErrorPolicies } from '@/lib/workspace-files/api' | ||
| import { extractWorkspaceFile } from '@/lib/workspace-files/application/extract-workspace-file' | ||
| import { fileOperations } from '@/lib/workspace-files/application/operations' | ||
|
|
||
| export const dynamic = 'force-dynamic' | ||
| export const maxDuration = 300 | ||
|
|
||
| /** | ||
| * POST /api/workspaces/[id]/files/[fileId]/extract | ||
| * Unzip an archive file into a new folder beside it (requires write permission) | ||
| */ | ||
| export const POST = defineInternalJsonRoute({ | ||
| contract: extractWorkspaceFileContract, | ||
| auth: internalSessionAuth, | ||
| operation: fileOperations.extractArchive, | ||
| rateLimit: internalRateLimits.none({ reason: 'Preserve existing internal file behavior' }), | ||
| errorPolicy: internalFileErrorPolicies.extractArchive, | ||
| mapInput: ({ params }) => ({ fileId: params.fileId, assertedWorkspaceId: params.id }), | ||
| useCase: extractWorkspaceFile, | ||
| present: (result) => ({ success: true, ...result }), | ||
| }) | ||
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,55 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { ApiClientError } from '@/lib/api/client/errors' | ||
| import { useExtractWorkspaceFile } from '@/hooks/queries/workspace-file-folders' | ||
|
|
||
| const { queryClient } = vi.hoisted(() => ({ | ||
| queryClient: { | ||
| invalidateQueries: vi.fn(), | ||
| }, | ||
| })) | ||
|
|
||
| vi.mock('@sim/emcn', () => ({ | ||
| toast: { error: vi.fn(), success: vi.fn() }, | ||
| })) | ||
|
|
||
| vi.mock('@tanstack/react-query', () => ({ | ||
| keepPreviousData: {}, | ||
| useMutation: vi.fn((options) => options), | ||
| useQuery: vi.fn(), | ||
| useQueryClient: vi.fn(() => queryClient), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/api/client/request', () => ({ requestJson: vi.fn() })) | ||
|
|
||
| const variables = { workspaceId: 'workspace-1', fileId: 'file-1', fileName: 'archive.zip' } | ||
|
|
||
| describe('useExtractWorkspaceFile reconciliation', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| it('invalidates file browsers after success', () => { | ||
| const mutation = useExtractWorkspaceFile() | ||
|
|
||
| mutation.onSuccess( | ||
| { success: true, folderName: 'archive', extractedCount: 2, skippedCount: 0 }, | ||
| variables | ||
| ) | ||
| mutation.onSettled(undefined, undefined, variables) | ||
|
|
||
| expect(queryClient.invalidateQueries).toHaveBeenCalledTimes(3) | ||
| }) | ||
|
|
||
| it('invalidates file browsers after an API error response', () => { | ||
| const mutation = useExtractWorkspaceFile() | ||
| const error = new ApiClientError({ status: 409, message: 'Folder exists', body: {} }) | ||
|
|
||
| mutation.onError(error, variables) | ||
| mutation.onSettled(undefined, error, variables) | ||
|
|
||
| expect(queryClient.invalidateQueries).toHaveBeenCalledTimes(3) | ||
| }) | ||
| }) |
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.
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.