Skip to content

Commit 8e415bd

Browse files
fix(approvals)!: an approver reassigns only their own slot (#36144)
Clause-②: no (narrowing) Metadata-protocol: none Skills-docs: none — no published skill teaches the reassign door's `from`; the approvals guide already lists reassign as the pending approver handing over a slot, which is now the enforced rule ## 维护者速读 - 审批人在多审批人的审批步骤上改派时,只能把自己的那一席交给别人;管理员和系统的改派不变,普通用户在界面上看不到差别。 - 为审批流程的职责分离加固,由安全席位派发。 - 出错的话,某位审批人改派自己那一席会被拒绝;改动只有一处判断,可以直接撤回。 - 无。 ## What changes An approver reassigns only their own slot. On `POST /api/v1/approvals/requests/:id/reassign` (and `IApprovalService.reassign`), a caller that is neither the system nor an administrator acting through the approval override may name as `from` only a slot it acts under: its user id, its account's email, or a position it holds — the same address set the slot test already reads. Any other `from` is refused with the existing refusal shape, `403 FORBIDDEN`, and nothing moves. Omitting `from` still hands over the caller's own slot. The administrator override, the administrator rescue of a slate that names no one, and the system are unchanged. - `packages/plugins/plugin-approvals/src/approval-service.ts` — `reassign()`: one guard in the hand-off branch, after the platform-slot check. - `.changeset/sec2-48g2.md` — `patch` on `@objectstack/plugin-approvals`, BREAKING for the narrowed caller, ADR-0087 disposition `not-required (no-migration-prescription)`. Read first (by risk): 1. `approval-service.ts`, the new guard in `reassign()` — the hand-off branch's authorization. 2. `packages/qa/dogfood/test/approval-reassign-own-slot.dogfood.test.ts` — the door pins. 3. `approval-service.test.ts`, describe `reassign: an approver hands over only their own slot` — the system leg, which has no HTTP door. ## Tests - Door (real boot, `bootStack`, the console's route): another approver's slot refused `403` with `code: 'FORBIDDEN'`, slate unchanged, no `reassign` action recorded; CONTROL own slot admitted; administrator override admitted. - Service: the same three legs plus the system leg. - Red on the base, green after; guard ablated red, restored green (counts in Verification). ## Acceptance notes - L2 owed: the contract-tier review is the security seat's. - The console and the `approval_reassign` action never send `from`; they are unaffected. ## Verification Two-minute check: `pnpm --filter @objectstack/dogfood exec vitest run test/approval-reassign-own-slot.dogfood.test.ts` (after building the dogfood closure) — expect `Tests 1 passed (1)`. <details><summary>Red on base, green after, ablation</summary> - Base (pins committed, guard not yet): service pins `Tests 1 failed | 3 passed`; door pin `Tests 1 failed (1)`. - With the guard (plugin rebuilt, guard text present in `dist/`): service `Tests 4 passed`; door `Tests 1 passed (1)`. - Ablation via `scripts/ablation-replace.mjs` (anchor 1 → 0, blob changed), plugin rebuilt, run at `17932d305` and again at the final head `ec011e0a5` with the same readings: service `1 failed | 3 passed`, door `1 failed (1)`. Restored: blob == HEAD, `git diff HEAD` empty; rebuilt, `ablation-dist-preflight --absent` marker absent from all 6 built files; service `4 passed`, door `1 passed (1)`. - `pnpm --filter @objectstack/plugin-approvals exec vitest run`: `Test Files 126 passed (126)`, `Tests 1812 passed (1812)`. - `typecheck`: `@objectstack/plugin-approvals` and `@objectstack/dogfood` exit 0. </details> <details><summary>Local gate subset (derived by dispatch-gates.mjs --commands, 76 families)</summary> - 73 of 76 exit 0; `check:system-context-census` re-run green after the guard's final spelling (head `ec011e0a5`). - NOT MEASURED, 3: `check-plugin-teardown-shape.mjs --self-test` (its positive control is a pinned commit this shallow checkout cannot reach); `check:dual-build-cjs-loads` (PREREQUISITE NOT MET: reads every package's `dist/`, and this order forbids a full-repo build); `check:type-check-debt` (builds every package; stopped for the same reason). CI's runs are the readings. - Full dogfood battery (owed by the `(narrowing)` arm): reported in the report comment on this PR. </details> --- _Generated by [Claude Code](https://claude.ai/code/session_017xRBcW9L8Noz9BxEwjwiov)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 5e37a35 commit 8e415bd

7 files changed

Lines changed: 328 additions & 4 deletions

File tree

‎.changeset/sec2-48g2.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
'@objectstack/plugin-approvals': patch
3+
---
4+
5+
fix(approvals)!: an approver reassigns only their own slot
6+
7+
Clause-②: no (narrowing)
8+
Metadata-protocol: none
9+
10+
<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped, and no export changes shape. What moves is who may name a slot as the `from` of `POST /api/v1/approvals/requests/:id/reassign`: no stored row, registry key or authored value changes, so there is nothing for a migration to rewrite and no tombstone. -->
11+
12+
**BREAKING** for a caller of `POST /api/v1/approvals/requests/:id/reassign` (and `IApprovalService.reassign`) that is neither the system nor an administrator acting through the approval override.
13+
14+
- TO: such a call may move only a slot held by the actor it names (an omitted `actorId` names the caller). A `from` naming any other slot is refused `403 FORBIDDEN`, and nothing moves: the slate is unchanged and no `reassign` action is recorded. The default actor holds its user id, its account's email and each position it holds; a named email or position address holds only that address and its equivalent spellings. Omitting `from` still hands over the actor's own slot.
15+
- **Not moved.** The administrator override and the system still move any pending slot, and the administrator rescue of a slate that names no one is unchanged.
16+
17+
The fix for a caller: ask the slot's holder to reassign it, or an administrator.

‎content/docs/automation/approvals.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ continuity levers on the same request. All are `POST
935935
| Verb | Route | Who | Effect |
936936
|:---|:---|:---|:---|
937937
| `approve` / `reject` | `/approve` `/reject` | pending approver | Records the decision (finalizes per `behavior`). Accepts `comment` (a reject's reason — required at every human door, see below), `attachments`, and — when the node declares `decisionOutputs` — structured `outputs` handed to the flow as `<nodeId>.<key>` variables. |
938-
| `reassign` | `/reassign` | pending approver | Hands one slot to another user (`to`); the request stays pending. |
938+
| `reassign` | `/reassign` | pending approver | Hands their own slot to another user (`to`); the request stays pending. A `from` naming a slot the acting approver does not hold is refused `403 FORBIDDEN`; an administrator (the override) or the system may move any pending slot. |
939939
| `revise` (send back) | `/revise` | pending approver | Ends this round as **`returned`**, unlocks the record for rework (ADR-0044). |
940940
| `request-info` | `/request-info` | pending approver | Asks the submitter for more info; the request **stays pending**. `comment` required. |
941941
| `remind` | `/remind` | submitter | Nudges the pending approvers (publishes a notification topic). |

‎packages/plugins/plugin-approvals/src/approval-service.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,6 +1932,74 @@ describe('ApprovalService (node era)', () => {
19321932
await expect(svc.reassign(req.id, { actorId: 'u9', to: 'u2' }, asUser('u9'))).rejects.toThrow(/VALIDATION_FAILED/);
19331933
});
19341934

1935+
describe('reassign: an approver hands over only their own slot', () => {
1936+
const PLATFORM_ADMIN_CTX = { userId: 'root', tenantId: 't1', positions: [], permissions: ['admin_full_access'] } as any;
1937+
const openUnanimous = async () => {
1938+
const req = await svc.openNodeRequest(openInput(['u9', 'u2'], {}, { behavior: 'unanimous' }), CTX);
1939+
staffDirectory(engine, 'u7');
1940+
return (req as { id: string }).id;
1941+
};
1942+
const slate = (id: string) => (engine._tables['sys_approval_request'] ?? []).find(r => r.id === id)?.pending_approvers;
1943+
const moves = (id: string) =>
1944+
(engine._tables['sys_approval_action'] ?? []).filter(r => r.request_id === id && r.action === 'reassign');
1945+
1946+
it('refuses a slot holder naming another approver\'s slot, and moves nothing', async () => {
1947+
const id = await openUnanimous();
1948+
await expect(svc.reassign(id, { actorId: 'u9', from: 'u2', to: 'u7' }, asUser('u9')))
1949+
.rejects.toThrow(/^FORBIDDEN: actor 'u9' may move only a slot it holds/);
1950+
expect(slate(id)).toBe('u9,u2');
1951+
expect(moves(id)).toEqual([]);
1952+
});
1953+
1954+
it('CONTROL: the same holder naming their own slot moves it', async () => {
1955+
const id = await openUnanimous();
1956+
const out = await svc.reassign(id, { actorId: 'u9', from: 'u9', to: 'u7' }, asUser('u9'));
1957+
expect(out.request.pending_approvers).toEqual(['u7', 'u2']);
1958+
expect(moves(id)).toHaveLength(1);
1959+
});
1960+
1961+
it('the administrator override still moves another approver\'s slot', async () => {
1962+
const id = await openUnanimous();
1963+
const out = await svc.reassign(id, { actorId: 'root', from: 'u2', to: 'u7' }, PLATFORM_ADMIN_CTX);
1964+
expect(out.request.pending_approvers).toEqual(['u9', 'u7']);
1965+
});
1966+
1967+
describe('a position slot', () => {
1968+
const FIN = { userId: 'u_fin', tenantId: 't1', positions: ['finance'], permissions: [] } as any;
1969+
const openOnPosition = async () => {
1970+
const req = await svc.openNodeRequest(openInput(['u2'], {}, {
1971+
approvers: [{ type: 'position' as const, value: 'finance' }, { type: 'user' as const, value: 'u2' }],
1972+
behavior: 'unanimous',
1973+
}), CTX);
1974+
staffDirectory(engine, 'u7');
1975+
const id = (req as { id: string }).id;
1976+
expect(slate(id), 'precondition: the slate holds the position slot and a user slot').toBe('position:finance,u2');
1977+
return id;
1978+
};
1979+
1980+
it('its holder moves it', async () => {
1981+
const id = await openOnPosition();
1982+
const out = await svc.reassign(id, { actorId: 'u_fin', from: 'position:finance', to: 'u7' }, FIN);
1983+
expect(out.request.pending_approvers).toEqual(['u7', 'u2']);
1984+
expect(moves(id)).toHaveLength(1);
1985+
});
1986+
1987+
it('its holder cannot move another approver\'s slot, and nothing moves', async () => {
1988+
const id = await openOnPosition();
1989+
await expect(svc.reassign(id, { actorId: 'u_fin', from: 'u2', to: 'u7' }, FIN))
1990+
.rejects.toThrow(/^FORBIDDEN: actor 'u_fin' may move only a slot it holds/);
1991+
expect(slate(id)).toBe('position:finance,u2');
1992+
expect(moves(id)).toEqual([]);
1993+
});
1994+
});
1995+
1996+
it('the system still moves another approver\'s slot', async () => {
1997+
const id = await openUnanimous();
1998+
const out = await svc.reassign(id, { actorId: 'system', from: 'u2', to: 'u7' }, SYS);
1999+
expect(out.request.pending_approvers).toEqual(['u9', 'u7']);
2000+
});
2001+
});
2002+
19352003
// ── reassign target resolution (#25485) ─────────────────────────
19362004
//
19372005
// A `to` that resolves to no one used to be written onto the slate, so the

‎packages/plugins/plugin-approvals/src/approval-service.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ import {
107107
// decision methods' slot test and `can_act` alike (`approver-address.ts`).
108108
import {
109109
actingAddresses,
110+
actorAddresses,
110111
addressedPosition,
111112
equivalentApproverAddresses,
112113
heldSlot,
@@ -3018,8 +3019,11 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
30183019
pending: readonly string[],
30193020
actorId: string,
30203021
context: ExecutionContext,
3022+
caller?: ActingCaller | null,
30213023
): Promise<string | undefined> {
3022-
return heldSlot(pending, actorId, await this.actingCaller(context));
3024+
// `caller`: the {@link actingCaller} a method already resolved for this
3025+
// context, so a method that reads it twice (reassign) asks once.
3026+
return heldSlot(pending, actorId, caller !== undefined ? caller : await this.actingCaller(context));
30233027
}
30243028

30253029
/**
@@ -7607,9 +7611,12 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
76077611
throw new Error(`VALIDATION_FAILED: '${to}' is already a pending approver`);
76087612
}
76097613
const isOverride = this.isOverrideActor(context, raw.organization_id ?? null, pending);
7614+
// The caller, resolved once: the slot test below and the own-slot test in
7615+
// the hand-off branch read the same answer.
7616+
const caller = await this.actingCaller(context);
76107617
// The slot the actor takes (`takenSlot`) — and, when `from` is not named,
76117618
// the slot being handed over.
7612-
const slot = await this.takenSlot(pending, actorId, context);
7619+
const slot = await this.takenSlot(pending, actorId, context, caller);
76137620
const from = String(input.from ?? slot ?? actorId).trim();
76147621
// #4466 — same rule as `decideNode`: the marker records that the actor was
76157622
// admitted only by the privileged branch, holding no slot themselves. A
@@ -7633,6 +7640,17 @@ export class ApprovalService implements IApprovalService, ObjectSchemaReadDelega
76337640
+ `platform administrator, or the system`,
76347641
);
76357642
}
7643+
// The call moves only a slot held by the actor it names: `from` must be
7644+
// one of that actor's addresses (`actorAddresses`, the same set the
7645+
// slot test reads; an omitted `actorId` names the caller). Moving any
7646+
// other slot is the system's or the override's (`isOverride` admits the
7647+
// system), never a fellow approver's.
7648+
if (!isOverride && !actorAddresses(actorId, caller).includes(from)) {
7649+
throw new Error(
7650+
`FORBIDDEN: actor '${actorId}' may move only a slot it holds, and does not hold '${from}': `
7651+
+ `that slot moves only for its holder, an administrator, or the system`,
7652+
);
7653+
}
76367654
next = pending.map(a => (a === from ? to : a));
76377655
} else if (isOverride) {
76387656
// Admin rescue (#3424): the caller holds no slot — the slate is an

‎packages/plugins/plugin-approvals/src/approver-address-readers.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ const READERS: ReadonlyArray<{ reader: string; method: string; reads: string }>
9595
{ reader: 'decision slot test — approve / reject', method: 'decideNode', reads: 'takenSlot' },
9696
{ reader: 'decision slot test — send back', method: 'sendBack', reads: 'takenSlot' },
9797
{ reader: 'decision slot test — reassign', method: 'reassign', reads: 'takenSlot' },
98+
{ reader: 'own-slot test — reassign moves only a slot the named actor holds', method: 'reassign', reads: 'actorAddresses' },
9899
{ reader: 'decision slot test — request info', method: 'requestInfo', reads: 'takenSlot' },
99100
{ reader: 'decision slot test — comment', method: 'comment', reads: 'takenSlot' },
100101
];
@@ -124,7 +125,7 @@ const SLOT_SITES: Readonly<Record<string, { role: SiteRole; why: string }>> = {
124125
},
125126
'approval-service.ts · reassign · pending.includes(from)': {
126127
role: 'slot-vs-slot',
127-
why: '`from` names the slot being moved (the taken slot by default); the caller is judged by takenSlot',
128+
why: '`from` names the slot being moved (the taken slot by default); outside the override it must also be one of actorAddresses(the named actor), the own-slot reader in READERS',
128129
},
129130
"approval-service.ts · remind · pending.filter(a => a && !a.includes(':'))": {
130131
role: 'slot-vs-slot',
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
//
3+
// An approver reassigns only their own slot. A reassign that neither the
4+
// system nor the administrator override admits moves the caller's own slot
5+
// and no other, through the door the console calls
6+
// (`POST /approvals/requests/:id/reassign`), on a real boot:
7+
//
8+
// ⭐ a slot holder naming another approver's slot as `from` is refused
9+
// `403 FORBIDDEN`, and nothing moved: the slate is unchanged and no
10+
// `reassign` action was recorded;
11+
// ⭐ CONTROL: the same holder naming their own slot moves it;
12+
// ⭐ the administrator override still moves another approver's slot.
13+
//
14+
// The system caller has no HTTP door; its leg is pinned beside the service
15+
// (`plugin-approvals` `approval-service.test.ts`, same rule).
16+
17+
import { describe, it, expect } from 'vitest';
18+
import { bootStack } from '@objectstack/verify';
19+
import { ApprovalsServicePlugin } from '@objectstack/plugin-approvals';
20+
import { RecordChangeTriggerPlugin } from '@objectstack/trigger-record-change';
21+
import { MessagingServicePlugin } from '@objectstack/service-messaging';
22+
import {
23+
reassignOwnSlotStack,
24+
reassignOwnSlotSecurity,
25+
FIRST_APPROVER_EMAIL,
26+
SECOND_APPROVER_EMAIL,
27+
} from './fixtures/reassign-own-slot-fixture.js';
28+
29+
const SYS = { context: { isSystem: true } } as const;
30+
const SUBMITTER = 'reassign-own-slot-submitter@example.com';
31+
const MEMBER = 'reassign-own-slot-member@example.com';
32+
const OTHER_MEMBER = 'reassign-own-slot-other@example.com';
33+
34+
interface Row {
35+
id: string;
36+
object_name?: string;
37+
pending_approvers?: string[];
38+
}
39+
40+
describe('an approver reassigns only their own slot', () => {
41+
it(
42+
'refuses another approver\'s slot, moves the caller\'s own, and leaves the override unchanged',
43+
async () => {
44+
const stack = await bootStack(reassignOwnSlotStack as unknown as Parameters<typeof bootStack>[0], {
45+
automation: true,
46+
security: reassignOwnSlotSecurity(),
47+
extraPlugins: [
48+
new MessagingServicePlugin({ reliableDelivery: false }),
49+
new RecordChangeTriggerPlugin(),
50+
new ApprovalsServicePlugin(),
51+
],
52+
});
53+
try {
54+
const ql: any = await stack.kernel.getServiceAsync('objectql');
55+
const idOf = async (email: string): Promise<string> => {
56+
const u = await ql.findOne('sys_user', { where: { email }, context: SYS.context });
57+
return String(u?.id ?? '');
58+
};
59+
// The dev admin first, so the sign-ups below are plain members.
60+
const adminToken = await stack.signIn();
61+
const submitterToken = await stack.signUp(SUBMITTER);
62+
const firstToken = await stack.signUp(FIRST_APPROVER_EMAIL);
63+
await stack.signUp(SECOND_APPROVER_EMAIL);
64+
await stack.signUp(MEMBER);
65+
await stack.signUp(OTHER_MEMBER);
66+
const memberId = await idOf(MEMBER);
67+
const otherMemberId = await idOf(OTHER_MEMBER);
68+
expect(memberId).not.toBe('');
69+
expect(otherMemberId).not.toBe('');
70+
71+
const created = await stack.apiAs(submitterToken, 'POST', '/data/reassign_own_slot', { name: 'two approvers' });
72+
expect(created.status).toBe(201);
73+
const pendingRes = await stack.apiAs(adminToken, 'GET', '/approvals/requests?status=pending');
74+
expect(pendingRes.status).toBe(200);
75+
const open = ((await pendingRes.json()) as { data: Row[] }).data
76+
.find((r) => r.object_name === 'reassign_own_slot');
77+
expect(open?.pending_approvers, 'precondition: the request is open on both named approvers')
78+
.toEqual([FIRST_APPROVER_EMAIL, SECOND_APPROVER_EMAIL]);
79+
const requestId = open!.id;
80+
81+
const reassign = (token: string, body: Record<string, unknown>) =>
82+
stack.apiAs(token, 'POST', `/approvals/requests/${requestId}/reassign`, body);
83+
const slate = async (): Promise<string> => {
84+
const row = await ql.findOne('sys_approval_request', { where: { id: requestId }, context: SYS.context });
85+
return String(row?.pending_approvers ?? '');
86+
};
87+
const moves = async (): Promise<unknown[]> =>
88+
(await ql.find('sys_approval_action', { where: { request_id: requestId }, context: SYS.context }))
89+
.filter((a: any) => a.action === 'reassign');
90+
91+
// ⭐ 1 — another approver's slot is refused, and nothing moved.
92+
const refused = await reassign(firstToken, { from: SECOND_APPROVER_EMAIL, to: memberId });
93+
expect(refused.status).toBe(403);
94+
expect(((await refused.json()) as { code?: string }).code).toBe('FORBIDDEN');
95+
expect(await slate()).toBe(`${FIRST_APPROVER_EMAIL},${SECOND_APPROVER_EMAIL}`);
96+
expect(await moves()).toEqual([]);
97+
98+
// ⭐ 2 — CONTROL: the same holder's own slot moves.
99+
const own = await reassign(firstToken, { from: FIRST_APPROVER_EMAIL, to: memberId });
100+
expect(own.status).toBe(200);
101+
expect(((await own.json()) as { request?: Row }).request?.pending_approvers)
102+
.toEqual([memberId, SECOND_APPROVER_EMAIL]);
103+
expect(await moves()).toHaveLength(1);
104+
105+
// ⭐ 3 — the administrator override still moves another approver's slot.
106+
const override = await reassign(adminToken, { from: SECOND_APPROVER_EMAIL, to: otherMemberId });
107+
expect(override.status).toBe(200);
108+
expect(((await override.json()) as { request?: Row }).request?.pending_approvers)
109+
.toEqual([memberId, otherMemberId]);
110+
expect(await moves()).toHaveLength(2);
111+
} finally {
112+
await stack.stop();
113+
}
114+
},
115+
120_000,
116+
);
117+
});

0 commit comments

Comments
 (0)