Skip to content

Clarify why DocumentApi by-id endpoints require an explicit request-scope guard - #7995

Closed
corinnekrych (corinnekrych) with Copilot wants to merge 2 commits into
mainfrom
copilot/tenant-api-v2-document-endpoints
Closed

corinnekrych (corinnekrych) with Copilot wants to merge 2 commits into
mainfrom
copilot/tenant-api-v2-document-endpoints

Conversation

Copilot AI commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Document by-id endpoints were relying on behavior that looks similar to tenant API v2 scoping, but documents is not a v2-active table and Hibernate does not tenant-filter primary-key loads. This change makes that boundary explicit in DocumentApi and centralizes the guarded lookup used by by-id document endpoints.

  • Problem made explicit

    • Document by-id reads/writes are loaded via findById(...)
    • @AccessControl authorizes capabilities, not tenant ownership
    • TxCtx does not scope this path yet because documents is not in openaev.tenant.active-tables
  • Code change

    • Introduced findDocumentInRequestScope(ctx, documentId) as the single entrypoint for guarded by-id document lookup
    • Reused it across:
      • get
      • get tags
      • update tags
      • update
      • relations
      • download
      • delete
  • Documentation / intent

    • Added focused Javadoc explaining why @Transactional + TxCtx is insufficient here
    • Kept assertDocumentInRequestScope(...) as the enforcement point for the current pre-v2 behavior
private Document findDocumentInRequestScope(TxCtx ctx, String documentId) {
  Document document =
      documentRepository
          .findById(documentId)
          .orElseThrow(() -> new ElementNotFoundException("Document not found"));
  assertDocumentInRequestScope(ctx, document);
  return document;
}
Original prompt

as part of tenant API v2 migration, i'm reviewing a PR in this branch with this description:
DocumentApi endpoints that take a document id now resolve the document within the request's tenant scope, the same way list and search endpoints are scoped. A document that belongs to another tenant gets the same 404 as a missing document.

Endpoints: get, get tags, update tags, update, relations, download and delete.

Details
The row is loaded by primary key, which the Hibernate tenant filter does not restrict, and accesscontrol checks capabilities. The new assertDocumentInRequestScope compares the document's tenant with the request TxCtx scope.
On the two PUT endpoints the scope check runs before the report-output rule, so a document outside the scope always answers 404.
A request with no scope (a caller without any tenant membership, on the non-prefixed route) is not checked and keeps today's behaviour; a document with no tenant is unaffected.
The non-prefixed X-Tenant-Ids route is handled with the v2 migration (see fix(multi-tenancy): attribute create writes from the request scope (#7958) #7959); the check itself reads the request scope and is not route specific.
Tests
DocumentByIdScopeTest (real stack, tenant path): for each endpoint, another tenant's document gets 404 and is left unchanged in the database, and a document of the request's tenant keeps working; plus the ordering of the report-output rule on both PUTs. DocumentByIdEmptyScopeTest pins the no-scope case. DocumentApiTest, AuditLoggerDocumentTest, ReportingApiTest and the multi-tenancy arch tests stay green.

Question: why dont we rely on @transactiona; + TxCtx filtering as all the toher pattern? why do we need "assertDocumentInRequestScope"

@corinnekrych
corinnekrych (corinnekrych) added this pull request to stack #7996 September 16, 2026 07:36
Copilot AI changed the title [WIP] Update DocumentApi endpoints for tenant scope resolution Clarify why DocumentApi by-id endpoints require an explicit request-scope guard Sep 16, 2026
Base automatically changed from fix/mtv2-document-byid-scope to main September 17, 2026 08:16
Co-authored-by: corinnekrych <1395710+corinnekrych@users.noreply.github.com>
@laugiov
Laurent Giovannoni (laugiov) force-pushed the copilot/tenant-api-v2-document-endpoints branch from d7bce2a to 4e8c2c0 Compare September 17, 2026 08:16
@github-actions

Copy link
Copy Markdown
Contributor

📖 Documentation check — ✅ Passed

1 functional file(s), 0 doc file(s) changed.

No documentation gaps detected.

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.

3 participants