refactor(convex): consolidate share/harness authorization into shareAuthCore - #157
Merged
Conversation
…uthCore Third deepening from the improve-codebase-architecture skill (design-it-twice → Design #2). The grant-based access model was mirrored across shares.ts and harnessShares.ts (two ~700-line files) — grantForToken, assertOwned*, resolveRole, activeGrantForCaller all near-identical — and the mirror had already rotted once (the sharedLocked cleanup fix landed on only one side). New shareAuthCore.ts owns the ONE security machine: empty-token→null guard, isActiveGrant (single impl) on the token lookup AND the per-user scan, opaque "Not found" (never leak existence), owner-wins-first, the cross-entity fk gate, editor-short-circuit / viewer-floor / max-role-wins, the truthy-userId guard (an anonymous caller rides the token path only — never matches a grantedToUserId===undefined row), and a required-projection sharedHeader (an unredacted egress is unrepresentable). Each entity wires it with concretely- typed closures via makeShareAuth(descriptor) — zero casts on the fk gate. Entity POLICY stays in the wrappers: publicMessage / publicHarnessProjection redaction, authorizeConversationWrite (owner|editor) vs editSharedHarness (editor-only + the sharedLocked lock + field allowlist), mint/rotate/list payloads, the harness lock-cleanup tail. Exported signatures unchanged (resolveConversationRole / authorizeConversationWrite / isActiveGrant re- exported from shares.ts) so messages.ts and other importers need zero edits. Behaviour-preserving: the 48 existing share/role tests (shares 32 + harnessShares 16) pass with ZERO expectation changes. New shareAuthCore.test.ts (12 cases) pins the invariants directly, incl. the anonymous-privilege- escalation negative and the cross-entity gate. convex 231 passed, tsc + biome clean. HOLD merge until AFTER v1.0 is cut (live authorization code — keep the release clean; a human should read the final diff).
4 tasks
This was referenced Jul 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Third deepening from the improve-codebase-architecture skill (design-it-twice → Design #2), after
event_encoder(#155) andProvisioningContext(#156).What
The grant-based access model was mirrored across
shares.tsandharnessShares.ts(two ~700-line files) —grantForToken,assertOwned*,resolveRole,activeGrantForCallerall near-identical — and the mirror already rotted once (thesharedLockedcleanup fix landed on only one side; that was a code-review finding earlier this cycle).New
shareAuthCore.tsowns the ONE security machine, wired per-entity viamakeShareAuth(descriptor)with concretely-typed closures (zero casts on the cross-entity gate):isActiveGrant(single impl) on the token lookup AND the per-user scan, opaque"Not found"(never leak existence), owner-wins-first, the cross-entity fk gate, editor-short-circuit / viewer-floor / max-role-wins, the truthy-userId guard (an anonymous caller rides the token path only — never matches agrantedToUserId===undefinedrow), and a required-projectionsharedHeader(an unredacted egress is unrepresentable at the type level).Entity policy stays in the wrappers:
publicMessage/publicHarnessProjectionredaction,authorizeConversationWrite(owner|editor) vseditSharedHarness(editor-only + thesharedLockedlock + field allowlist), mint/rotate/list payloads, the harness lock-cleanup tail. Exported signatures unchanged (resolveConversationRole,authorizeConversationWrite,isActiveGrantre-exported) somessages.ts,checkConversationAccess, and the FastAPI backend need zero edits.Net: −174 lines from the two share files → +211 core.
Why (glossary)
(ctx, userId, entityId, token)and get a role; the mirror can't rot.Verification
shares.test32 +harnessShares.test16) — the primary safety net; a changed assertion would mean behavior moved.shareAuthCore.test.tscases pin the invariants directly, incl. the anonymous-privilege-escalation negative (userId=nullmust never matchgrantedToUserId===undefined) and the cross-entity gate.behaviorPreserving: yes, 0 findings.convex 231 passed,tsc -p convex+ biome clean.No user-facing change → no CHANGELOG entry.