Skip to content

fix: project overrides can widen tenant-scoped-query scope (#15) - #16

Open
joao-oliveira-softtor wants to merge 1 commit into
mainfrom
fix/tenant-scoped-query-rls
Open

joao-oliveira-softtor wants to merge 1 commit into
mainfrom
fix/tenant-scoped-query-rls

Conversation

@joao-oliveira-softtor

Copy link
Copy Markdown
Member

Closes #15.

What

overrides[].check.unlessInEnclosingDeclaration in the project rulebook replaces the enclosing-declaration regex of an inherited regex rule. Composition rejects the field on any other check kind (RulebookCompositionError).

Lets a project on Postgres RLS declare, in one alternation, which aggregate ids and findUnique-by-id shapes count as tenant-scoped, instead of grandfathering whole files via scope.exclude (138 of the 226 baseline FAILs in the first pilot came from this rule).

Why this mechanism

  • No new check kind, no per-model lists, base rulebook untouched (no sha256 stamp churn for consumers).
  • enclosingContains already tests the regex against the whole enclosing method, so one field covers the three proposals in the issue.
  • Replace, not OR: the project states the full accepted scope. The recipe in rulebooks/project.example.rulebook.yaml and the README keep organizationId in the alternation and say why.
  • Before: Zod silently stripped the unknown check key, so such an override was dropped and the rule kept firing.

Proof

  • New scripts/__tests__/tenant-scoped-query-rls.spec.ts uses the shipped softtor-conventions rulebook: base rule still flags the RLS-shaped file (3 findings); with the override, 0; same override on a non-RLS file, still 3; invalid regex rejected. Red before the fix (2 of 4 failing), green after.
  • bun test: 334 → 339 passing. tsc -p tsconfig.json clean. validate-frontmatter ok. examples/ under --rulebook hexagonal --strict: byte-identical to main (0 FAIL, 1 WARN).

Docs

README (override fields + RLS recipe), project.example.rulebook.yaml, skills/onboard-project/SKILL.md (baseline step names the override before "lower to WARN"), CHANGELOG.md under a new Unreleased heading. No version bump.

Known limit: the example recipe's \bprisma\.(...) branch does not cover a transaction client (tx.); projects add (?:prisma|tx) if they need it.

Issue #15: in a project where Postgres RLS enforces tenant isolation,
repository methods query by an aggregate id that is already tenant-bound
(conversationId, subscriptionId, findUnique by id on an RLS-protected
model). softtor/tenant-scoped-query only accepted organizationId in the
enclosing method, so 138 of the pilot's 226 baseline FAILs came from it
and every file had to be grandfathered with scope.exclude.

Mechanism: overrides[].check.unlessInEnclosingDeclaration. The project
rulebook replaces the enclosing-declaration regex of an inherited regex
rule; composition rejects it on any other check kind. One field covers
all three proposals in the issue because the regex is tested against the
whole enclosing method, so it can name aggregate ids, the model and the
where shape (prisma.<model>.findUnique({ where: { id } })) without a new
check kind or per-model lists. Replace, not OR: the project states the
full accepted scope, as the issue's own example does.

The base rulebook is untouched, so no stamp changes and projects without
the override keep the current behaviour; methods with no declared scope
still fail under the override. Hooks compose the same rulebook and pick
the override up with no extra code.

Before the change the schema stripped the unknown check key silently and
the base rule kept firing; the new spec reproduces that (red) and proves
the override (green), plus the non-RLS file that still fails.
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
🔒 Security Review ✅ Completed 2026-09-21T03:00:53.049314Z 0cf55b7 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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.

softtor/tenant-scoped-query: false positives for aggregate-scoped queries under RLS

1 participant