Skip to content

Commit 1ccc2c1

Browse files
committed
fix(plugin-auth): the engine ban writer carries the administrator's attribution
Without the vendor admin plugin the ban columns are written through the data engine; that write now takes the administrator in scope through the existing authSystemWriteContext seam, so plugin-audit's mirror row names them instead of the system, as the internalAdapter path already did. Written fields unchanged. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibyy9KSA69EGZtjDj9c
1 parent 0f9dc05 commit 1ccc2c1

4 files changed

Lines changed: 33 additions & 6 deletions

File tree

‎.changeset/35440-ban-unban-admin-audit-row.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
Clause-②: no
66
Metadata-protocol: none
77

8-
`POST /api/v1/auth/admin/ban-user` and `POST /api/v1/auth/admin/unban-user` now write one `sys_audit_log` row for a change that committed, through the same writer the other admin user doors use: `action: 'update'` on the target `sys_user`, naming the administrator, with `metadata` `{ event: 'user.admin_banned' | 'user.admin_unbanned', hostNotified }`. A refused ban or unban writes no row. Without the admin plugin, plugin-audit's own row for the ban columns names no administrator, so this row is the record of who banned. The ban reason and expiry stay on plugin-audit's row for the user's change, not in `metadata`. The route ledger note for `admin/remove-sso-user` and the authentication docs now name that door's `user.admin_removed_pending` row.
8+
`POST /api/v1/auth/admin/ban-user` and `POST /api/v1/auth/admin/unban-user` now write one `sys_audit_log` row for a change that committed, through the same writer the other admin user doors use: `action: 'update'` on the target `sys_user`, naming the administrator, with `metadata` `{ event: 'user.admin_banned' | 'user.admin_unbanned', hostNotified }`. A refused ban or unban writes no row. Without the admin plugin, the ban columns' write now carries the administrator's attribution too, so plugin-audit's own row for that change names the administrator instead of the system, as it already did with the admin plugin; the written fields are unchanged. The ban reason and expiry stay on plugin-audit's row for the user's change, not in `metadata`. The route ledger note for `admin/remove-sso-user` and the authentication docs now name that door's `user.admin_removed_pending` row.

‎packages/plugins/plugin-auth/src/admin-ban-endpoints.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@
9696
* A ban or an unban that committed writes the sibling doors' admin audit row
9797
* (`writeAdminAudit`, action `update`, event `user.admin_banned` /
9898
* `user.admin_unbanned`); a refused one writes none. plugin-audit's mirror row
99-
* for the column write does not stand in for it: without the admin plugin the
100-
* engine writer carries no attributed user, so that row names no
101-
* administrator, and on neither path does it carry the event or `hostNotified`.
99+
* for the column write names the administrator on both writers (the engine
100+
* one through `authSystemWriteContext`), but carries neither the event nor
101+
* `hostNotified`, so it does not stand in for this row.
102102
*/
103103

104104
import {

‎packages/plugins/plugin-auth/src/pending-sso-users.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,26 @@ describe('[#35440] admin/ban-user and admin/unban-user write an admin audit row
15971597
});
15981598
}
15991599

1600+
it('the ban columns\' write itself names the admin for plugin-audit\'s mirror row, with the admin plugin off as on', async () => {
1601+
for (const adminPlugin of [false, true]) {
1602+
const s = await stage({ adminPlugin });
1603+
const contexts: any[] = [];
1604+
const update = s.engine.update.bind(s.engine);
1605+
s.engine.update = (async (name: string, data: any, options?: any) => {
1606+
if (name === 'sys_user' && 'banned' in data) contexts.push(options?.context);
1607+
return update(name, data, options);
1608+
}) as typeof s.engine.update;
1609+
1610+
expect((await s.fire('/admin/ban-user', { userId: s.ids.member }, s.bearers.admin)).status).toBe(200);
1611+
expect((await s.fire('/admin/unban-user', { userId: s.ids.member }, s.bearers.admin)).status).toBe(200);
1612+
1613+
expect(contexts, `admin plugin ${adminPlugin ? 'on' : 'off'}`).toEqual([
1614+
expect.objectContaining({ isSystem: true, attributedUserId: s.ids.admin }),
1615+
expect.objectContaining({ isSystem: true, attributedUserId: s.ids.admin }),
1616+
]);
1617+
}
1618+
});
1619+
16001620
it('with no host wired the rows carry hostNotified false', async () => {
16011621
const s = await stage({ host: 'none' });
16021622
expect((await s.fire('/admin/ban-user', { userId: s.ids.member }, s.bearers.admin)).status).toBe(200);

‎packages/plugins/plugin-auth/src/user-ban-write.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
* (`deleteUserSessions` on `active: false`, measured on 1.7.2).
3434
*/
3535

36+
import { authSystemWriteContext } from './auth-actor-attribution.js';
37+
3638
export interface UserBanWriter {
3739
updateUser(id: string, data: Record<string, unknown>): Promise<unknown>;
3840
}
@@ -95,7 +97,8 @@ const BAN_COLUMN: Record<string, string> = {
9597
* [#33181] The ban write on a kernel WITHOUT the vendor admin plugin. There
9698
* `banReason` / `banExpires` are not fields of better-auth's user model, so
9799
* `internalAdapter.updateUser` would not land them; this writer lands the
98-
* same columns through the data engine, as the system. The break-glass
100+
* same columns through the data engine, as the system, attributed to the
101+
* administrator in scope (`authSystemWriteContext`). The break-glass
99102
* last-administrator guard judges engine writes in every context, `isSystem`
100103
* included, so it holds here too. A field the ban write does not know is
101104
* refused rather than written under a guessed column.
@@ -109,7 +112,11 @@ export function engineUserBanWriter(engine: UserBanEngine): UserBanWriter {
109112
if (!column) throw new Error(`engineUserBanWriter: "${field}" is not a ban field`);
110113
patch[column] = value;
111114
}
112-
return engine.update('sys_user', patch, { context: { isSystem: true, positions: [], permissions: [] } });
115+
// Authorized as the system, as before; the attribution seam adds only the
116+
// administrator the request scope names, so plugin-audit's mirror row
117+
// names them as it does on the admin plugin's `internalAdapter` path.
118+
const context = { positions: [], permissions: [], ...(await authSystemWriteContext()) };
119+
return engine.update('sys_user', patch, { context });
113120
},
114121
};
115122
}

0 commit comments

Comments
 (0)