Skip to content

Commit 60b5181

Browse files
authored
fix(workspace-source): read apply and plan bodies past 10 MiB, refuse oversized ones locally (#10541)
* fix(workspace-source): read apply and plan bodies past 10 MiB, refuse oversized ones locally * fix(workspace-source): reserve body room before reading, check size before staging
1 parent c9c0a3d commit 60b5181

24 files changed

Lines changed: 584 additions & 253 deletions

‎apps/docs/content/docs/cli/reference.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10129,7 +10129,7 @@ sim workspaces source apply [options]
1012910129

1013010130
</CommandTable>
1013110131

10132-
Exit codes: 0 applied (or a clean dry run); 1 the source has errors (each printed as file:line:column with its frame and hint), or every change it would write was held back because it uses a declared secret the workspace has no value for or a declared credential key nothing binds to (nothing was written; a dry run of such an apply exits 1 too), the directory was read from another workspace than the one --workspace or SIM_WORKSPACE names (SOURCE_OTHER_WORKSPACE: drop it, or promote it with --to; without either an apply targets the workspace the directory was read from), its base records binary files no get downloaded (SOURCE_BLOBS_NOT_FETCHED: get again first), a --run test run did not complete (the apply is written; deployments/ was not applied), or the command failed; 5 a resource changed or was deleted on the server since the directory was read (SOURCE_STALE: get it again into a fresh directory, reapply the edit, retry); 6 a resource (a workflow or its folder, a table) is locked (a locked block exits 1, E_APPLY_LOCKED_BLOCK); 7 the directory is too large; 8 the apply stopped part-way on a server failure: what was written is saved into the directory, and applying again continues. An apply a resource refused part-way (stale, locked, forbidden) saves what was written too, and exits as that refusal: 5, 6 or 1; 9 rate limited (HTTP 429) after the CLI's retries, before anything was written: wait a minute and apply again (binary files already uploaded are not sent again); 10 another apply is writing the workspace (SOURCE_BUSY), or another command is using the directory (SOURCE_DIR_BUSY): nothing was written, run the command again once the other finished; 13 not logged in, the login expired, or the server refused the credential (AUTH_REQUIRED): nothing was sent, run `sim login` (or fix or unset SIM_API_KEY when the key comes from it) and the command again; 14 without --yes and with no terminal to ask: the plan was printed with no errors and nothing was written (run the same command with --yes to apply it); 15 written in part: changes that use a declared secret the workspace has no value for, or a declared credential key nothing binds to, were held back and the rest was applied and saved (set the secret or connect the credential, then apply again); a dry run that would do so exits 15 too, having written nothing; 130 interrupted (Ctrl-C): a write already sent is finished and saved into the directory first, waiting up to 60s for its answer, and a second Ctrl-C, or no answer by then, stops at once and prints that it may have been written and how to check.
10132+
Exit codes: 0 applied (or a clean dry run); 1 the source has errors (each printed as file:line:column with its frame and hint), or every change it would write was held back because it uses a declared secret the workspace has no value for or a declared credential key nothing binds to (nothing was written; a dry run of such an apply exits 1 too), the directory was read from another workspace than the one --workspace or SIM_WORKSPACE names (SOURCE_OTHER_WORKSPACE: drop it, or promote it with --to; without either an apply targets the workspace the directory was read from), its base records binary files no get downloaded (SOURCE_BLOBS_NOT_FETCHED: get again first), a --run test run did not complete (the apply is written; deployments/ was not applied), or the command failed; 5 a resource changed or was deleted on the server since the directory was read (SOURCE_STALE: get it again into a fresh directory, reapply the edit, retry); 6 a resource (a workflow or its folder, a table) is locked (a locked block exits 1, E_APPLY_LOCKED_BLOCK); 7 the directory is too large (apply part of it with --only); 8 the apply stopped part-way on a server failure: what was written is saved into the directory, and applying again continues. An apply a resource refused part-way (stale, locked, forbidden) saves what was written too, and exits as that refusal: 5, 6 or 1; 9 rate limited (HTTP 429) after the CLI's retries, before anything was written: wait a minute and apply again (binary files already uploaded are not sent again); 10 another apply is writing the workspace (SOURCE_BUSY), or another command is using the directory (SOURCE_DIR_BUSY): nothing was written, run the command again once the other finished; 13 not logged in, the login expired, or the server refused the credential (AUTH_REQUIRED): nothing was sent, run `sim login` (or fix or unset SIM_API_KEY when the key comes from it) and the command again; 14 without --yes and with no terminal to ask: the plan was printed with no errors and nothing was written (run the same command with --yes to apply it); 15 written in part: changes that use a declared secret the workspace has no value for, or a declared credential key nothing binds to, were held back and the rest was applied and saved (set the secret or connect the credential, then apply again); a dry run that would do so exits 15 too, having written nothing; 130 interrupted (Ctrl-C): a write already sent is finished and saved into the directory first, waiting up to 60s for its answer, and a second Ctrl-C, or no answer by then, stops at once and prints that it may have been written and how to check.
1013310133

1013410134
API request and response schemas: [applyWorkspaceSource](/api-reference/workflows/applyWorkspaceSource), [findMissingWorkspaceSourceBlobs](/api-reference/workflows/findMissingWorkspaceSourceBlobs), [stageWorkspaceSourceBlobs](/api-reference/workflows/stageWorkspaceSourceBlobs), [getWorkspaceSource](/api-reference/workflows/getWorkspaceSource), [getSourceCatalog](/api-reference/catalog/getSourceCatalog), [getWorkflowDeployment](/api-reference/workflows/getWorkflowDeployment), [executeWorkflow](/api-reference/workflows/executeWorkflowV2).
1013510135

‎apps/docs/content/docs/cli/workspaces.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ sim workspaces source apply [options]
763763

764764
</CommandTable>
765765

766-
Exit codes: 0 applied (or a clean dry run); 1 the source has errors (each printed as file:line:column with its frame and hint), or every change it would write was held back because it uses a declared secret the workspace has no value for or a declared credential key nothing binds to (nothing was written; a dry run of such an apply exits 1 too), the directory was read from another workspace than the one --workspace or SIM_WORKSPACE names (SOURCE_OTHER_WORKSPACE: drop it, or promote it with --to; without either an apply targets the workspace the directory was read from), its base records binary files no get downloaded (SOURCE_BLOBS_NOT_FETCHED: get again first), a --run test run did not complete (the apply is written; deployments/ was not applied), or the command failed; 5 a resource changed or was deleted on the server since the directory was read (SOURCE_STALE: get it again into a fresh directory, reapply the edit, retry); 6 a resource (a workflow or its folder, a table) is locked (a locked block exits 1, E_APPLY_LOCKED_BLOCK); 7 the directory is too large; 8 the apply stopped part-way on a server failure: what was written is saved into the directory, and applying again continues. An apply a resource refused part-way (stale, locked, forbidden) saves what was written too, and exits as that refusal: 5, 6 or 1; 9 rate limited (HTTP 429) after the CLI's retries, before anything was written: wait a minute and apply again (binary files already uploaded are not sent again); 10 another apply is writing the workspace (SOURCE_BUSY), or another command is using the directory (SOURCE_DIR_BUSY): nothing was written, run the command again once the other finished; 13 not logged in, the login expired, or the server refused the credential (AUTH_REQUIRED): nothing was sent, run `sim login` (or fix or unset SIM_API_KEY when the key comes from it) and the command again; 14 without --yes and with no terminal to ask: the plan was printed with no errors and nothing was written (run the same command with --yes to apply it); 15 written in part: changes that use a declared secret the workspace has no value for, or a declared credential key nothing binds to, were held back and the rest was applied and saved (set the secret or connect the credential, then apply again); a dry run that would do so exits 15 too, having written nothing; 130 interrupted (Ctrl-C): a write already sent is finished and saved into the directory first, waiting up to 60s for its answer, and a second Ctrl-C, or no answer by then, stops at once and prints that it may have been written and how to check.
766+
Exit codes: 0 applied (or a clean dry run); 1 the source has errors (each printed as file:line:column with its frame and hint), or every change it would write was held back because it uses a declared secret the workspace has no value for or a declared credential key nothing binds to (nothing was written; a dry run of such an apply exits 1 too), the directory was read from another workspace than the one --workspace or SIM_WORKSPACE names (SOURCE_OTHER_WORKSPACE: drop it, or promote it with --to; without either an apply targets the workspace the directory was read from), its base records binary files no get downloaded (SOURCE_BLOBS_NOT_FETCHED: get again first), a --run test run did not complete (the apply is written; deployments/ was not applied), or the command failed; 5 a resource changed or was deleted on the server since the directory was read (SOURCE_STALE: get it again into a fresh directory, reapply the edit, retry); 6 a resource (a workflow or its folder, a table) is locked (a locked block exits 1, E_APPLY_LOCKED_BLOCK); 7 the directory is too large (apply part of it with --only); 8 the apply stopped part-way on a server failure: what was written is saved into the directory, and applying again continues. An apply a resource refused part-way (stale, locked, forbidden) saves what was written too, and exits as that refusal: 5, 6 or 1; 9 rate limited (HTTP 429) after the CLI's retries, before anything was written: wait a minute and apply again (binary files already uploaded are not sent again); 10 another apply is writing the workspace (SOURCE_BUSY), or another command is using the directory (SOURCE_DIR_BUSY): nothing was written, run the command again once the other finished; 13 not logged in, the login expired, or the server refused the credential (AUTH_REQUIRED): nothing was sent, run `sim login` (or fix or unset SIM_API_KEY when the key comes from it) and the command again; 14 without --yes and with no terminal to ask: the plan was printed with no errors and nothing was written (run the same command with --yes to apply it); 15 written in part: changes that use a declared secret the workspace has no value for, or a declared credential key nothing binds to, were held back and the rest was applied and saved (set the secret or connect the credential, then apply again); a dry run that would do so exits 15 too, having written nothing; 130 interrupted (Ctrl-C): a write already sent is finished and saved into the directory first, waiting up to 60s for its answer, and a second Ctrl-C, or no answer by then, stops at once and prints that it may have been written and how to check.
767767

768768
API request and response schemas: [applyWorkspaceSource](/api-reference/workflows/applyWorkspaceSource), [findMissingWorkspaceSourceBlobs](/api-reference/workflows/findMissingWorkspaceSourceBlobs), [stageWorkspaceSourceBlobs](/api-reference/workflows/stageWorkspaceSourceBlobs), [getWorkspaceSource](/api-reference/workflows/getWorkspaceSource), [getSourceCatalog](/api-reference/catalog/getSourceCatalog), [getWorkflowDeployment](/api-reference/workflows/getWorkflowDeployment), [executeWorkflow](/api-reference/workflows/executeWorkflowV2).
769769

‎apps/docs/content/docs/platform/self-hosting/networking.mdx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ A reverse-proxy limit of at least 250 MB clears these configured defaults; use t
259259
260260
Most `/api` requests also pass through Next.js Proxy. The current app configuration leaves its `proxyClientMaxBodySize` at 10 MiB. Larger bodies are truncated to that buffer rather than automatically rejected by Next.js; the route may then fail to parse them. Receiving larger custom-route bodies requires changing `experimental.proxyClientMaxBodySize` in the app build, which increases per-request memory use. The shared JSON parser separately clamps contract-validated bodies to 10 MiB, so raising the Next.js buffer alone does not increase their limit. See [Next.js proxy body buffering](https://nextjs.org/docs/app/api-reference/config/next-config-js/proxyClientMaxBodySize).
261261
262+
Workspace source apply and plan requests (`POST /api/v2/workspaces/{workspaceId}/source` and `/source/plan`) skip Next.js Proxy and accept bodies up to 58 MB, so your reverse proxy must allow that size for large table rows to apply.
263+
262264
**With object storage configured**, regular file uploads do not flow through the proxy — the browser `PUT`s them directly to the bucket using a presigned URL, so the proxy limits matter only for chat attachments, API payloads, and webhook bodies. On the default local-disk storage there is no presigned path and every upload goes through the proxy, so its body limit applies to all of them.
263265
264266
## Outbound connectivity

‎apps/sim/app/api/v2/workspaces/[workspaceId]/source/plan/route.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,63 @@ describe('POST /api/v2/workspaces/{workspaceId}/source/plan', () => {
109109
expect(data.diagnostics).toHaveLength(1_001)
110110
expect(data.diagnostics.at(-1)).toMatchObject({ code: 'I_FINDINGS_TRUNCATED' })
111111
})
112+
113+
it('reads a body past the 10 MiB proxy ceiling whole, as a large table rows file sends', async () => {
114+
const rows = `${'{"code":"a","note":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}\n'.repeat(160_000)}`
115+
116+
const response = await plan({ 'tables/leads.rows.jsonl': rows })
117+
118+
expect(response.status).toBe(200)
119+
expect(mocks.plan.mock.calls[0][0].input.files['tables/leads.rows.jsonl']).toBe(rows)
120+
})
121+
122+
it('refuses a body unread with 429 once the plans in flight hold as many bodies as their slots, and frees the room of every answered request', async () => {
123+
const result = await mocks.plan()
124+
const finish: Array<() => void> = []
125+
mocks.plan.mockImplementation(
126+
() => new Promise((resolve) => finish.push(() => resolve(result)))
127+
)
128+
// No declared length, so each reserves a body at the cap.
129+
const running = Array.from({ length: 4 }, () => plan({ 'workflows/a/workflow.ts': WORKFLOW }))
130+
await vi.waitFor(() => expect(finish).toHaveLength(4))
131+
132+
let read = false
133+
const unread = await POST(
134+
new NextRequest(`http://localhost:3000/api/v2/workspaces/${WORKSPACE_ID}/source/plan`, {
135+
method: 'POST',
136+
headers: { 'x-api-key': 'key', 'content-type': 'application/json', 'content-length': '2' },
137+
body: new ReadableStream(
138+
{
139+
pull(controller) {
140+
read = true
141+
controller.enqueue(new TextEncoder().encode('{}'))
142+
controller.close()
143+
},
144+
},
145+
{ highWaterMark: 0 }
146+
),
147+
duplex: 'half',
148+
}),
149+
{ params: Promise.resolve({ workspaceId: WORKSPACE_ID }) }
150+
)
151+
expect(unread.status).toBe(429)
152+
expect(read).toBe(false)
153+
154+
for (const done of finish) done()
155+
await Promise.all(running)
156+
mocks.plan.mockResolvedValue(result)
157+
// More malformed bodies one after another than the lane holds at once: each frees its room.
158+
for (let attempt = 0; attempt < 5; attempt++) {
159+
const malformed = await POST(
160+
new NextRequest(`http://localhost:3000/api/v2/workspaces/${WORKSPACE_ID}/source/plan`, {
161+
method: 'POST',
162+
headers: { 'x-api-key': 'key', 'content-type': 'application/json' },
163+
body: '{',
164+
}),
165+
{ params: Promise.resolve({ workspaceId: WORKSPACE_ID }) }
166+
)
167+
expect(malformed.status).toBe(400)
168+
}
169+
expect((await plan({ 'workflows/a/workflow.ts': WORKFLOW })).status).toBe(200)
170+
})
112171
})

‎apps/sim/app/api/v2/workspaces/[workspaceId]/source/plan/route.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { WORKSPACE_SOURCE_REQUEST_MAX_BYTES } from '@sim/workspace-code/limits'
22
import { v2PlanWorkspaceSourceContract } from '@/lib/api/contracts/v2/workspace-source'
33
import { defineV2JsonRoute, v2ApiKeyAuth, v2RateLimits } from '@/lib/api/server/routes'
4-
import { withSourceSlot } from '@/lib/workflows/api'
4+
import { reserveWorkspaceSourceBody, withSourceSlot } from '@/lib/workflows/api'
55
import {
66
presentWorkspaceApply,
77
v2WorkspaceSourceErrorPolicy,
@@ -27,7 +27,8 @@ export const POST = defineV2JsonRoute({
2727
operation: workspaceSourceOperations.plan,
2828
rateLimit: v2RateLimits.sourcePlan,
2929
errorPolicy: v2WorkspaceSourceErrorPolicy,
30-
parseOptions: { maxBodyBytes: WORKSPACE_SOURCE_REQUEST_MAX_BYTES },
30+
parseOptions: { maxBodyBytes: WORKSPACE_SOURCE_REQUEST_MAX_BYTES, bypassesProxy: true },
31+
reserveBody: reserveWorkspaceSourceBody('plan'),
3132
mapInput: ({ params, body }) => ({
3233
workspaceId: params.workspaceId,
3334
files: body.files,

‎apps/sim/app/api/v2/workspaces/[workspaceId]/source/route.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,25 @@ describe('POST /api/v2/workspaces/{workspaceId}/source', () => {
199199
expect(data.diagnostics).toHaveLength(1_001)
200200
expect(data.diagnostics.at(-1)).toMatchObject({ code: 'I_FINDINGS_TRUNCATED' })
201201
})
202+
203+
it('reads a body past the 10 MiB proxy ceiling whole, as a large table rows file sends', async () => {
204+
mocks.apply.mockResolvedValue({
205+
workspaceId: WORKSPACE_ID,
206+
dryRun: false,
207+
plan: [],
208+
applied: [],
209+
needBlobs: [],
210+
files: {},
211+
base: { version: 1, workspaceId: WORKSPACE_ID, resources: {} },
212+
diagnostics: [],
213+
})
214+
const rows = `${'{"code":"a","note":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}\n'.repeat(160_000)}`
215+
216+
const response = await apply('', { 'tables/leads.rows.jsonl': rows })
217+
218+
expect(response.status).toBe(200)
219+
expect(mocks.apply.mock.calls[0][0].input.files['tables/leads.rows.jsonl']).toBe(rows)
220+
})
202221
})
203222

204223
/** `count` warnings, one per workflow, as a stored workspace whose tables are gone reads. */

‎apps/sim/app/api/v2/workspaces/[workspaceId]/source/route.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import {
44
v2GetWorkspaceSourceContract,
55
} from '@/lib/api/contracts/v2/workspace-source'
66
import { defineV2JsonRoute, v2ApiKeyAuth, v2RateLimits } from '@/lib/api/server/routes'
7-
import { applyOrDryRunLane, withSourceSlot } from '@/lib/workflows/api'
7+
import {
8+
applyOrDryRunLane,
9+
applyOrDryRunRequestLane,
10+
reserveWorkspaceSourceBody,
11+
withSourceSlot,
12+
} from '@/lib/workflows/api'
813
import {
914
presentWorkspaceApply,
1015
v2WorkspaceSourceErrorPolicy,
@@ -55,7 +60,8 @@ export const POST = defineV2JsonRoute({
5560
operation: workspaceSourceOperations.apply,
5661
rateLimit: v2RateLimits.sourceApply,
5762
errorPolicy: v2WorkspaceSourceErrorPolicy,
58-
parseOptions: { maxBodyBytes: WORKSPACE_SOURCE_REQUEST_MAX_BYTES },
63+
parseOptions: { maxBodyBytes: WORKSPACE_SOURCE_REQUEST_MAX_BYTES, bypassesProxy: true },
64+
reserveBody: reserveWorkspaceSourceBody(applyOrDryRunRequestLane),
5965
mapInput: ({ params, query, body }) => ({
6066
workspaceId: params.workspaceId,
6167
files: body.files,

0 commit comments

Comments
 (0)