Commit a76b928
authored
fix(security): bind copilot chat attachment keys to their owner (#6179)
* fix(security): bind copilot chat attachment keys to their owner
`POST /api/copilot/chat` accepted a client-supplied `fileAttachments[].key`
and passed it straight into `trackChatUpload`, which wrote it into the
`workspace_files` ownership binding with no permission check and no
key-ownership validation. That binding is what `verifyFileAccess` and the
Files feature resolve authorization from, so any workspace member —
including read-only — could hand in another member's key and have their
file re-parented to a private chat: removed from the Files listing, from
folders and from download-by-id, and destroyable through the chat-delete
FK cascade. The sibling register route already enforced these invariants;
the copilot path did not.
- `trackChatUpload` now rejects keys that do not address the target
workspace, only re-links a chat-upload row the caller already owns
(matched by row id, not by raw key), and only mints a new binding when
the key has no prior record at all — including soft-deleted ones, which
the partial active-key unique index would otherwise let it insert over.
- Minting a new binding verifies the object exists in storage, matching
`registerUploadedWorkspaceFile`.
- `buildCopilotRequestPayload` gates attachment tracking on write/admin,
the same grant the upload routes that issue these keys require.
* fix(security): make the chat-upload ownership check atomic with its write
The ownership lookup and the binding UPDATE were separate statements, and the
UPDATE matched on the captured row id alone. A concurrent `materialize_file`
sets `context='workspace'` and clears `chatId` on that same row, so the
tracking write still matched and dragged the saved file back into chat scope —
hiding it from every workspace-file listing and re-exposing it to the
chat-delete cascade, with materialize's storage-usage increment left stranded.
Re-assert every ownership predicate in the UPDATE so the statement is itself
the atomic check. The lookup now only decides UPDATE-vs-INSERT and is no
longer load-bearing for authorization, which also makes the existing
`updated.length === 0` fail-closed branch correct rather than dead.
* fix(uploads): tolerate transient storage probes and reject unowned keys with 403
Follow-ups from a backward-compatibility audit of the attachment-key hardening.
The existence probe is hygiene, not authorization — the key-format and
no-prior-record guards already carry that, and a binding to a nonexistent
object grants nothing readable. But `headObject` rethrows non-404 provider
errors, so a transient 5xx or throttle would drop a legitimate attachment.
Only a definitive not-found now rejects; a thrown error logs and proceeds on
the ownership guards. This path is reached solely by >50MB multipart uploads,
the one flow that persists no metadata row at upload time.
The stage route mapped an ownership rejection to a 500. It is a client error;
return 403 instead.
* fix(security): compare-and-swap the chat binding on chat uploads
Two overlapping chat requests could both observe the same claimable row with
`chat_id IS NULL` and both satisfy the update predicate, so the later write
silently moved the upload to its own chat — taking over the delete-cascade
lifecycle of a file the first chat had already bound.
Scope the update to `chat_id IS NULL OR chat_id = <target>` so the statement
is a compare-and-swap: the loser matches zero rows and fails closed. The
resolver applies the same rule so the update-vs-insert decision stays
coherent.
This also makes an upload bind to exactly one chat, matching the 409 the
sibling `local-files/stage` route already returns for the same case; verified
no client flow relinks a key across chats (drafts are per-chat, every retry
path replays under a pinned chat id, forking copies blobs to fresh keys).
* refactor(copilot): gate attachment tracking with the shared permission predicate
`permissionSatisfies` is the documented single source of truth for permission
comparisons and exists to replace hand-written `=== 'admin' || === 'write'`
ladders. `userPermission` is typed `string` for legacy reasons, so narrow it
with `isPermissionType` first — an unrecognized value fails the gate instead of
ranking below every level. Behavior is unchanged for all three levels.
Imported from the dependency-free `/predicates` subpath rather than
`/workspace`, which would pull `@sim/db` onto the chat request path.1 parent e90378a commit a76b928
5 files changed
Lines changed: 507 additions & 32 deletions
File tree
- apps/sim
- app/api/mothership/local-files/stage
- lib
- copilot/chat
- uploads/contexts/workspace
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
17 | 20 | | |
18 | 21 | | |
19 | 22 | | |
| |||
95 | 98 | | |
96 | 99 | | |
97 | 100 | | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
98 | 108 | | |
99 | 109 | | |
100 | 110 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
| |||
104 | 106 | | |
105 | 107 | | |
106 | 108 | | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
107 | 113 | | |
108 | 114 | | |
109 | 115 | | |
| |||
209 | 215 | | |
210 | 216 | | |
211 | 217 | | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
212 | 265 | | |
213 | 266 | | |
214 | 267 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
333 | 334 | | |
334 | 335 | | |
335 | 336 | | |
336 | | - | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
337 | 341 | | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
338 | 346 | | |
339 | | - | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
340 | 356 | | |
341 | 357 | | |
342 | 358 | | |
| |||
0 commit comments