Title: fix(security): bind copilot chat attachment keys to their owner by waleedlatif1 · Pull Request #6179 · simstudioai/sim · GitHub
Open Graph Title: fix(security): bind copilot chat attachment keys to their owner by waleedlatif1 · Pull Request #6179 · simstudioai/sim
X Title: fix(security): bind copilot chat attachment keys to their owner by waleedlatif1 · Pull Request #6179 · simstudioai/sim
Description: Problem 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 row with no permission check and no key-ownership validation. workspace_files.key is the trusted binding that verifyFileAccess() and the whole workspace Files feature resolve authorization from. The sibling registration path (POST /api/workspaces/[id]/files/register) enforces the invariant explicitly; the copilot attachment path did not. Net effect: a read-only workspace member could take any other member's workspace file, re-bind it to their own private copilot chat, and make it disappear from the workspace — a write/delete capability reached from a read-only grant. The UPDATE matched on (key, workspaceId, deletedAt IS NULL) only. It set context='mothership', which removes the row from every workspace-file query (Files tab, folders, download-by-id, restore), and re-parented chatId to the attacker's chat — where copilot_chats.id ON DELETE CASCADE hard-deletes the victim's row when that chat is deleted. When no active row matched, the INSERT branch minted a fresh binding claiming a foreign object for the caller's workspace. Fix trackChatUpload — treat s3Key as untrusted, applying the invariants the direct-upload path already enforces: Reject keys that do not resolve to the target workspace (parseWorkspaceFileKey). Only two outcomes are claimable: the caller already owns an active chat-upload row for the key, or the key has no workspace_files record at all. Another member's row, a context='workspace' file, and soft-deleted records are all rejected. (Soft-deleted matters: the active-key unique index is partial on deleted_at IS NULL, so an INSERT over an archived row would succeed and grant read access to its bytes.) The UPDATE re-asserts every ownership predicate rather than trusting the row id captured by the lookup, so the statement itself is the atomic check. A concurrent materialize_file sets context='workspace' and clears chatId on the same row; an id-only predicate would drag that saved file back into chat scope. The binding is a compare-and-swap on chat_id (IS NULL OR = target). Two overlapping requests both observe an unbound row; only the first wins, and the loser fails closed instead of stealing the upload and its delete-cascade lifecycle. This also makes an upload bind to exactly one chat, matching the 409 the sibling local-files/stage route already returns. Verify the object exists in storage before minting a new binding. This is hygiene, not authorization, so only a definitive not-found rejects — headObject rethrows provider 5xx/throttles, and failing there would drop a legitimate >50MB multipart upload (the only path that reaches this branch). buildCopilotRequestPayload — gate attachment tracking on write/admin via the shared permissionSatisfies predicate, the same grant /api/files/presigned, /api/files/multipart and /api/files/upload already require to issue these keys. local-files/stage route — an ownership rejection is a client error; return 403 instead of 500. Backward compatibility Audited explicitly for regressions to legitimate users: All upload paths still work. insertFileMetadata passes context through verbatim as 'mothership' with chatId null, so presigned and the no-cloud-storage fallback land on the UPDATE branch; multipart persists no row and lands on INSERT. Its restore branch also clears deleted_at, so re-uploading revives a soft-deleted row before tracking sees it. Key format. All four live key-minting paths emit workspace/{workspaceId}/…. sanitizeFileName collapses names to [A-Za-z0-9.\-_] and never returns empty, so the pattern holds for unicode, spaces, long names and dotfiles. A pre-#1785 prefix-less format exists but is unreachable — trackChatUpload postdates it and every key is freshly minted in the same request. No permission tier loses access. Owners get admin via an explicit row; org owners/admins are derived admins; the route is session-only (no API-key principal). A read member cannot obtain a key in the first place, since all three upload routes already gate on write. No client flow relinks a key across chats. Drafts are scoped per workspaceId:chatId and cleared on submit; every queue/retry/abort-resume path replays under a pinned chat id; no UI attaches an already-uploaded file into a new chat; forking copies blobs to fresh keys. Tests bunx vitest run lib/uploads lib/copilot app/api/files app/api/mothership app/api/workspaces — 1718 passed. Typecheck clean; bun run check:api-validation passes. Fifteen new tests cover the ownership contract, the atomicity predicate, the chat compare-and-swap, and the transient-probe tolerance. Each was verified to fail against the unfixed code — reverting the manager turns the ownership/atomicity/CAS tests red, and reverting the gate turns the permission tests red. Not live-tested. Recommended smoke before production: attach a small file and a >50MB file in a chat and confirm the model reads both, then materialize_file one and confirm it appears in Files and the chat still sends. That covers the UPDATE branch, the INSERT + headObject branch, and the materialize interaction.
Open Graph Description: Problem 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 row with no ...
X Description: Problem 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 row with no ...
Opengraph URL: https://github.com/simstudioai/sim/pull/6179
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:b236c479-bb8d-1246-2ca5-ceb8fb901f75 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 9AA6:5A71D:7846C1:A59110:6AFB43B2 |
| html-safe-nonce | fd1ada3897f183c91e9bbc966308a8d9851487a9bdc021ca9e744da379fb2e02 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QUE2OjVBNzFEOjc4NDZDMTpBNTkxMTA6NkFGQjQzQjIiLCJ2aXNpdG9yX2lkIjoiNTM1MTc5ODg3MzQ3NzgyNTQ1OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8060fe55563b877af05820bf94033df93b9386672c66b247b7388fe159b234a7 |
| hovercard-subject-tag | pull_request:4186262707 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/simstudioai/sim/pull/6179/files |
| twitter:image | https://avatars.githubusercontent.com/u/40672544?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/40672544?s=400&v=4 |
| og:image:alt | Problem 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 row with no ... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | d849b928f455c800985398e7f1b659425adb312a5fa14fba5d9a14e62e9fe5dd |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/simstudioai/sim git https://github.com/simstudioai/sim.git |
| octolytics-dimension-user_id | 199344406 |
| octolytics-dimension-user_login | simstudioai |
| octolytics-dimension-repository_id | 912559512 |
| octolytics-dimension-repository_nwo | simstudioai/sim |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 912559512 |
| octolytics-dimension-repository_network_root_nwo | simstudioai/sim |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | a6c2f87cbcb6414452377b4967858ed6a66f1109 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width