Skip to content

fix(platform): scope project files to their owning project - #2549

Merged
larryro merged 1 commit into
mainfrom
fix/project-file-scope
Jul 8, 2026
Merged

larryro merged 1 commit into
mainfrom
fix/project-file-scope

Conversation

@larryro

@larryro larryro commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Summary

Documents attached to a project (projectId set, teamId cleared by the attach invariant) read as org-wide to hasTeamAccess, so every Knowledge surface leaked them to the whole org — hub lists, the @ mention picker and pin path, the rag_search/document_find/document_retrieve agent tools, the per-user-key REST API, and update/delete mutations — while project-chat RAG never searched them (nothing mapped the thread's project into agentProjectIds). This PR gives the scope decision one owner (convex/documents/access.ts) and enforces it at every surface, and wires the thread's project into RAG scope after re-verifying the sender's current project access.

Follow-ups: WebDAV exclusion #2545, attach-lifecycle gaps #2546.

What changed

  • New access owner — documents/access.ts: isProjectScopedDocument, hasKnowledgeHubDocumentAccess (sync, list-safe: project docs are never hub-visible), checkProjectDocumentAccess / canReadDocument (async: project docs resolve via checkProjectAccess on the owning project).
  • Knowledge read paths — listDocuments, listDocumentsPaginated, getDocumentsCursor, searchDocumentsForMention, getAccessibleDocumentIds, listDocumentsForAgent filter through the hub helper; getDocumentById uses canReadDocument so project members can still open their files by ID.
  • rag_search — list_indexed org bucket excludes project docs and gains a project bucket (agentProjectIds); retrieve/search storage-id verification (verify_thread_scoped_access) loads the backing hub doc and requires the caller's project scope to cover it. document_retrieve honors the same scope for parity.
  • @ pins — resolveReferencedFiles extracted to agents/resolve_referenced_files.ts: a project doc is pinable only when the thread belongs to that project and the sender can read it; thread metadata is read before reference resolution to supply the scope.
  • Mutations — updateDocument/deleteDocument require project edit access (the attach/detach standard) for project docs; teamId(s) assignment on a project doc is rejected in both update helpers (projectId/teamId mutual exclusivity, all callers incl. REST/sync).
  • RAG scope wiring — generate_response resolves getProjectIdForThread once, re-verifies the sender's current access via assertProjectAccessForChat (a revoked member's old project threads no longer grant scope), and unions the project into agentProjectIds for context injection and the rag_search tool context. chat_turn validates args.projectId before the prewarm branch, so an unvalidated prewarm can never bind a thread to a foreign project.
  • REST /api/v1/documents — hub-only (keys are per-user sessions): list filters project docs post-pagination; GET/PATCH/DELETE answer an opaque 404. Shared internals stay open for sync/retention/erasure callers.
  • RLS — documents read/modify/insert deny project docs (defense-in-depth; no documents path is RLS-wrapped today).

Independent adversarial review of the initial six-path fix found the rag_search bucket and the prewarm/stale-binding holes; both are closed here rather than deferred.

Checklist

  • bun run check passes — 42/47 tasks green in the worktree; the sole failure (@tale/ui app-shell.test.tsx) is a git-worktree artifact (Vite denies the hoisted node_modules font path outside the worktree root) and the same file passes 5/5 in the main checkout. This diff touches no packages/ui code.
  • bun run lint:sast passes — 0 findings on this diff; the repo-wide scan's single finding is pre-existing in services/sandbox-runtime (untouched).
  • Translations — N/A: no new user-visible strings; the gates reuse existing error codes (PROJECT_FORBIDDEN, DOCUMENT_SCOPE_CONFLICT) and the REST handlers reuse the existing "Document not found" wording.
  • Docs — N/A: docs/en/platform/projects/manage-files.md already documents exactly this behaviour ("The Files tab is not a knowledge base…", project chat sees project files); this fix makes the docs true.
  • READMEs — N/A: no env var, CLI flag, or config change. No schema change → no migration.

Test plan

Every leak class carries a red→green regression test (watched failing on the pre-fix code):

  • documents/access.test.ts — hub/project split, canReadDocument, checkProjectDocumentAccess role matrix (15 cases).
  • agents/resolve_referenced_files.test.ts — pin gate: own-project thread allowed; global chat / foreign project / no project access rejected (8 cases).
  • Extended: list_documents_paginated, get_accessible_document_ids, search_documents_for_mention, list_documents_for_agent, list_indexed_documents_for_agent (org bucket + project bucket), update_document_internal (teamId-on-project-doc invariant).
  • Suites: convex/documents/ + convex/agents/ + convex/agent_tools/ — 937 tests green; typecheck/oxlint/oxfmt clean.

Manual verification for a reviewer (dev stack, two users, one team-scoped project):

  1. Upload a file in the project's Files tab → it does not appear in Knowledge → Documents (any user), and rag_search {operation:'list_indexed'} in a global chat does not list it.
  2. In a global chat, the @ picker never offers it; sending a crafted referencedDocumentIds pin fails with KB_REF_INVALID.
  3. In the project's chat, the default assistant (knowledgeMode tool) retrieves the file's content via rag_search; document_retrieve of the same fileId also succeeds.
  4. As an org member outside the project's team: getDocumentById returns null; update/delete throw PROJECT_FORBIDDEN; REST GET/PATCH/DELETE /api/v1/documents/:id return 404 and the list omits it.

Not live-observed end-to-end: project-chat RAG retrieval on a seeded stack (step 3) — the unit tests pin every decision point and the wiring is typecheck-verified, but I could not run a full RAG corpus in this environment; step 3 is the one to smoke first.

Project documents (projectId set, teamId cleared by the attach
invariant) read as org-wide to hasTeamAccess, so every Knowledge Hub
surface leaked them to the whole org while project-chat RAG never
searched them. Give the scope decision one owner, documents/access.ts,
and enforce it everywhere:

- Knowledge read paths (list/paginated/cursor/mention/accessible-ids/
  document_find) filter via hasKnowledgeHubDocumentAccess; by-ID reads
  use canReadDocument so project members can still open their files.
- rag_search: list_indexed org bucket excludes project docs and gains a
  project bucket; retrieve/search verify project files against the
  thread's project scope (verify_thread_scoped_access).
- @-pins (extracted resolve_referenced_files): a project doc is pinable
  only in its own project's thread by a sender with project access.
- updateDocument/deleteDocument require project edit access; teamId(s)
  assignment on a project doc is rejected at both update helpers
  (mutual-exclusivity invariant).
- generate_response resolves the thread's project once, re-verifies the
  sender's CURRENT access (revoked members lose project scope), and
  unions it into agentProjectIds for context injection + rag_search;
  document_retrieve honors the same scope. chat_turn validates
  args.projectId before the prewarm branch so an unvalidated prewarm
  can never bind a thread to a foreign project.
- REST /api/v1/documents is hub-only: project docs filtered from list,
  opaque 404 on GET/PATCH/DELETE.
- RLS documents rules deny project docs (defense-in-depth).

WebDAV exclusion tracked in #2545; attach-lifecycle gaps in #2546.
@larryro
larryro force-pushed the fix/project-file-scope branch from 828bafb to 3337986 Compare July 8, 2026 05:08
@larryro
larryro merged commit 777d132 into main Jul 8, 2026
52 checks passed
@larryro
larryro deleted the fix/project-file-scope branch July 8, 2026 07:05
larryro added a commit that referenced this pull request Jul 8, 2026
Folders extended to project scope but single files did not — you could
pin @contracts yet not the contract inside it. searchDocumentsForMention
gains a projectId (access re-verified in the wrapper, mirroring the
folders search): a bounded index scan ranks the thread project's
RAG-indexed files ahead of the hub results, with the project folder path
as the subtitle. The send path already permitted these pins
(resolveReferencedFiles, #2549), so retrieval scoping, sandbox delivery
on both paths, chips, and the toast exclusion all apply unchanged. Org
threads see exactly the previous behavior.
larryro added a commit that referenced this pull request Jul 8, 2026
Folders extended to project scope but single files did not — you could
pin @contracts yet not the contract inside it. searchDocumentsForMention
gains a projectId (access re-verified in the wrapper, mirroring the
folders search): a bounded index scan ranks the thread project's
RAG-indexed files ahead of the hub results, with the project folder path
as the subtitle. The send path already permitted these pins
(resolveReferencedFiles, #2549), so retrieval scoping, sandbox delivery
on both paths, chips, and the toast exclusion all apply unchanged. Org
threads see exactly the previous behavior.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant