fix(identity): reconcile managedBy:'better-auth' apiMethods with the write guard (#1591)#3213
Merged
Merged
Conversation
…write guard (#1591) Phase 1 — tighten `apiMethods` on better-auth-managed identity objects. `managedBy: 'better-auth'` promises generic CRUD is suppressed (writes flow through better-auth's endpoints; the plugin-auth identity write guard rejects direct user-context create/update/delete), yet these schemas still advertised the write verbs in `enable.apiMethods`. That made the HTTP exposure gate admit the request and let it 403 at the engine instead of answering a clean 405, and the metadata contradicted its own doc comments. Each better-auth object is now read-only (`get`/`list`), except `sys_user`, which keeps `update` — the one generic write opened on an identity table (ADR-0092 D4), server-side clamped to the profile-field whitelist ({name, image}) and declared via `userActions.edit`. Phase 2 — registration-time consistency backstop. `reconcileManagedApiMethods` runs in `SchemaRegistry.registerObject` and strips any generic write verb a better-auth object advertises but does not grant (per `resolveCrudAffordances`: the managedBy bucket default plus `userActions` overrides), with a warning. Reads are never touched; non-better-auth objects are untouched. This makes the contradiction impossible to reintroduce — even if a schema re-adds a write verb, the effective exposed set is corrected at registration. Generalizing to other `managedBy` buckets (`externallyManaged`/`writeVia`) stays with #1878. Proven: registry unit tests for the derivation; the identity-create dogfood suite updated to assert `POST /data/sys_team` now returns 405 (OBJECT_API_METHOD_NOT_ALLOWED) at the exposure gate, before the engine's 403 backstop — system-context inserts still succeed. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GwY68hKVysP98BX7i5eUoE
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
📓 Docs Drift CheckThis PR changes 3 package(s): 16 hand-written doc(s) reference the affected code and may need an implementation-accuracy re-verification:
|
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GwY68hKVysP98BX7i5eUoE
os-zhuang
marked this pull request as ready for review
July 18, 2026 13:48
os-zhuang
added a commit
that referenced
this pull request
Jul 18, 2026
…sys_metadata (#3220) (#3222) Follow-through on #1591/#3213 for two non-better-auth managed objects that shipped the same contradiction the better-auth reconciliation fixed: their enable.apiMethods advertised generic create/update/delete while their managedBy bucket forbids user-context writes, leaving the generic /data route open. - sys_presence (append-only) advertised create/update/delete — update/delete on an append-only object — but is written only over the realtime websocket/ in-memory path, never through ObjectQL. Narrowed to ['get','list']. - sys_metadata (system) advertised full CRUD but overlays are authored only via the metadata-protocol RPC (engine writes carry a transaction context, not a user session); neither the framework nor the Console (objectui) POSTs /data/sys_metadata. Narrowed to ['get','list']. Reads stay open. The metadata-protocol / realtime write paths are engine-level and bypass the HTTP exposure gate, so they are unaffected — verified by the metadata-authoring dogfood (5 passed), the objectql overlay engine-insert tests (6 passed), and metadata-core (100 passed). A blast-radius audit found the broader system/append-only buckets are NOT safe to guard wholesale: several system objects (sys_user_position, sys_user_permission_set, sys_position_permission_set, sys_user_preference, sys_import_job) are user-writable by design (delegated administration, user preferences, imports). Generalizing the engine write guard to those buckets is intentionally out of scope — the root cause is the overloaded system bucket, tracked in #3220. Claude-Session: https://claude.ai/code/session_01GwY68hKVysP98BX7i5eUoE Co-authored-by: Claude <noreply@anthropic.com>
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.
Continues #1591 — lands Phase 1 + Phase 2 of the development plan in this comment. The headline vulnerability (generic
/dataCRUD bypassing better-auth) was already closed by the engine-level identity write guard (ADR-0092 D2). What remained was the metadata contradiction: identity objects still advertised generic write verbs inenable.apiMethodsthat the guard rejects anyway.Phase 1 — tighten
apiMethodson better-auth-managed objectsFor every
managedBy: 'better-auth'object inpackages/platform-objects/src/identity/, the generic write verbs (create/update/delete) were removed fromenable.apiMethods, leaving reads (get/list) only. Effect: the HTTP exposure gate (ADR-0049,enforceApiAccess) now answers a semantically-correct 405 before the engine's 403 backstop even runs, and the schema metadata matches its own doc comments.The one exception is
sys_user, which keepsupdate— the single generic write opened on an identity table (ADR-0092 D4), server-side clamped to the profile-field whitelist ({name, image}) by the write guard and declared in-schema viauserActions: { edit: true }.Objects tightened:
sys_account,sys_api_key,sys_device_code,sys_invitation,sys_member,sys_organization,sys_session,sys_team,sys_team_member,sys_two_factor,sys_user(reads +update),sys_verification. Objects already read-only or API-disabled (sys_oauth_*,sys_scim_provider,sys_sso_provider,sys_jwks) were left as-is.Phase 2 — registration-time consistency backstop
New
reconcileManagedApiMethodsruns insideSchemaRegistry.registerObject. For amanagedBy: 'better-auth'object it strips any generic write verb the object does not grant — computed fromresolveCrudAffordances(themanagedBybucket default plususerActionsoverrides, the same function the UI uses) — logging a warning. Reads are never touched; non-better-authobjects are a no-op. This makes the contradiction impossible to reintroduce: even if a schema re-addsdelete, the effective exposed set is corrected at registration.Generalizing this to other
managedBybuckets via anexternallyManaged/writeViacapability (Phase 3) stays with #1878 / ADR-0049 and is intentionally out of scope here.Verification
packages/objectql/src/registry.test.ts) — newreconcileManagedApiMethodssuite: stripscreate/update/deletewith no affordances; keepsupdatewhenuserActions.editgrants edit (thesys_usercase); no-op when nothing needs stripping; never touches reads; leavesplatform-bucket objects untouched; and applies throughregisterObject. Full suite: 69 passed.single-tenant-identity-create.dogfood.test.ts) — updated to assertPOST /data/sys_teamnow returns 405OBJECT_API_METHOD_NOT_ALLOWED(was 403PERMISSION_DENIED); system-context insert still succeeds. Ran green against the booted showcase stack.@objectstack/platform-objects(213) and@objectstack/plugin-authidentity-write-guard + auth-plugin (75) suites pass; builds/DTS clean.Closes #1591.
🤖 Generated with Claude Code
Generated by Claude Code