Skip to content

fix(attention): refuse an acknowledgement whose caller id is not a user id (TASK-176) - #1986

Merged
lilyshen0722 merged 3 commits into
mainfrom
kai/task176-ack-recipient-guard
Sep 28, 2026
Merged

lilyshen0722 merged 3 commits into
mainfrom
kai/task176-ack-recipient-guard

Conversation

@lilyshen0722

@lilyshen0722 lilyshen0722 commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

TASK-176, backend half. The CLI half is #1985.

The premise, measured

The row asked for this guard because acknowledgeAttention was said to be exploitable: "mongoose drops an undefined key, which would ack another recipient's item by id". That is not what happens, and it matters that the correction is written down rather than the fix being explained by it. Probe on bbab5c31 — each caller shape against a row owned by another recipient:

caller=undefined      -> {success:false,'Attention item not found'}   row status after: open
caller=null           -> {success:false,'Attention item not found'}   row status after: open
caller=''             -> THREW CastError: Cast to ObjectId failed …   row status after: open
caller='not-an-id'    -> THREW CastError: Cast to ObjectId failed …   row status after: open
caller=<the owner id> -> {success:true}                               row status after: resolved

So there is no cross-recipient acknowledgement to fix: undefined and null are refused and the other person's row keeps its open status. What is real is the last two lines: a caller id that is not an ObjectId throws out of the update, and the route (routes/activity.ts:265-274) turns that into a 500 Failed to acknowledge attention. A malformed id is a client-side fact, and the item-id guard three lines above already answers that shape with a refusal.

The change

if (!/^[a-f\d]{24}$/i.test(String(recipientUserId))) return { success: false, error: 'Invalid recipient' };

— the same guard getOpenQueue carries on its recipient (attentionItemService.ts:466), placed beside the existing item-id refusal, with the measurement in the comment so the next reader is not told a story the code cannot reproduce. Reachability today is latent (the single production caller passes a real id from auth), so the value here is the 400-vs-500 boundary and the invariant, not a closed exploit.

One existing arm moved its fixture

backend/__tests__/unit/services/attentionItemService.test.js:428 called acknowledgeMention('sam', <item>). 'sam' is not an ObjectId, so the guard now refuses it before the mocked update and the arm's mockUpdateOne.mock.calls.at(-1) was undefined. That arm's subject is the $or selector ("never decisions or approvals"), not the spelling of the recipient, so it now uses a real ObjectId — which the two neighbouring acknowledgement arms in the same file already did. No other arm in the repo passes a non-id recipient (grep across backend/__tests__).

Verification

backend/__tests__/service/attentionAcknowledgeRecipientGuard.test.js:

  • three caller ids that cannot be a user id ('', 'not-an-id', and the 12-character '507f191e810c') each refused by name (Invalid recipient), with three separately planted rows all still open — one row per caller so a passing case cannot mask a failing one;
  • a valid id belonging to somebody else refused with Attention item not found, with the owner's own acknowledgement as the positive control (and a second acknowledgement on the resolved row refused, because the selector requires status: 'open');
  • the malformed item id refused as Invalid attention item before any update, with the row open.

undefined and null are deliberately not arms: they are refused by unguarded code too (see the probe above), so an arm asserting them witnesses nothing. Their behaviour is in the file header as the reason the row's premise was withdrawn, not dressed up as a fix.

One more measurement, because the third caller rests on it rather than on the cast: '507f191e810c' does not throw — it casts to 353037663139316538313063, its own ASCII bytes read as the 12-byte form, i.e. a valid ObjectId belonging to nobody. The guard checks the canonical 24-hex shape rather than leaning on the cast, which is what makes that row of the table a refusal instead of a silent miss.

The two survivors Vera found, reproduced and then closed

Her review of 5c068c37 found the arms did not witness the regex's anchors, and both reproduce exactly as reported. Measured in this order — the ledger run against the gated head, then against this commit:

mutation at 5c068c37 at this commit
delete the recipient guard killed killed
delete the item-id guard killed killed
reuse the item-id message for the recipient killed killed
move the guard below the update killed killed
drop the end anchor ({24}$ → {24}) SURVIVES killed
drop the start anchor (/^[a-f → /[a-f) SURVIVES killed

The two survivors are the guard's own value left unwitnessed: with $ gone, <24 hex>0 passes and the cast throws; with ^ gone, x<24 hex> does the same — both straight back to the 500 this PR removes. Neither is reachable from a real credential (getAuthenticatedUserId returns req.userId || req.user?.id || req.user?._id || null), so this is hardening of a hardening change, not a closed door.

Two caller shapes close it, appended to the array the arm already loops over (so toHaveLength(callers.length) scales by itself):

'507f191e810c19729de860ea0'   25 chars — kills the end-anchor edit
'x507f191e810c19729de860ea'   prefixed — kills the start-anchor edit

M4 is in the table because WHERE the guard sits is part of the guarantee: a refusal after the write is not a refusal, and the arm kills that move. One existing assertion had to scale with the list — the planted rows' statuses were asserted as the literal ['open','open','open'], which would have kept passing for the wrong reason at five callers; it is now callers.map(() => 'open').

Not touched, named rather than smuggled in: getOpenQueue:466 carries the identical regex with the identical anchor gap. Same class, pre-existing, out of this row's scope.

Mutation ledger: 6 mutations, NO SURVIVORS, each killed by its named arm (/tmp/kai-176be-ledger.py, baseline (1, 3) asserted on every run, and the mutated file verified byte-clean against the committed head at the end). The first run of that ledger reported both kills with named_arm_hit=False — a bug in the ledger, not the suite: it tested arm membership with name in names (list equality) against jest fullNames that carry their describe prefix. Fixed and re-run; both hits are real.

The mutation removing the recipient guard fails the suite with the same CastError the probe recorded, which is the defect stated above, restated by the code.

Regression across the neighbouring suites (5 suites / 59 tests, 0 failed): attentionQueue.counts, unit/services/attentionItemService, unit/routes/activity.read, unit/services/activityService.recap, plus the new file. npm run tsc:check exits 0. Lint diff on services/attentionItemService.ts against origin/main: base 15 findings, mine 15, added NONE, removed NONE (warnings included; the restore was checksum-verified).

Backend only, so no package version bump. Gate: Vera.

Also in the row, not this PR

#1985 now backs off on the /api/activity limiter (60/min, keyed on cf-connecting-ip, so shared by every session on a host and by the operator's own browser) and reports the server's code field instead of a bare HTTP 429.

…er id (TASK-176)

The guard `getOpenQueue` carries on its recipient was missing on the
acknowledgement path, and what that omission actually does had to be measured
rather than assumed. Probe on bbab5c3, acking another recipient's open item:

  caller=undefined      -> {success:false,'Attention item not found'}   row open
  caller=null           -> {success:false,'Attention item not found'}   row open
  caller=''             -> THREW CastError                             row open
  caller='not-an-id'    -> THREW CastError                             row open
  caller=<the owner id> -> {success:true}                              row resolved

So the cross-recipient acknowledgement this was proposed for is NOT reproducible:
the undefined and null filters are refused and the other person's row keeps its
open status. What is real is the CastError out of the update for an id that is
not an ObjectId — a client-side fact reported as a 500 'Failed to acknowledge
attention'. The new guard refuses both shapes in the 400 lane, beside the
item-id refusal that was already there.

One existing unit arm used `'sam'` as the recipient placeholder in a mocked
update; the guard now stops it before the update, and that arm's subject is the
`$or` selector rather than the spelling of the id, so its fixture moved to a real
ObjectId (the two neighbouring arms already used one).

Test: backend/__tests__/service/attentionAcknowledgeRecipientGuard.test.js —
four unusable caller ids refused with every planted row still open, plus the
owner's own acknowledgement as the positive control.
…oreign id, owner control (TASK-176)

connector-ops' 04:11Z correction executed the same probe and reached the same
conclusion, and revised the arms: drop `undefined` and `null` (they pass against
unguarded code, so an arm asserting them witnesses nothing), assert a typed
refusal for `''`, and add the 12-character form.

Adopted, with one addition measured here: `'507f191e810c'` does not throw — it
casts to 353037663139316538313063, its own ASCII bytes read as the 12-byte form,
so it is a valid ObjectId belonging to nobody. That is why the guard checks the
canonical 24-hex shape rather than leaning on the cast.

Arms now: three unusable caller ids each refused as `Invalid recipient` with all
three planted rows still open; a valid id belonging to somebody else refused
with the owner's own acknowledgement as the positive control (and a second
acknowledgement refused, because the selector requires status 'open'); a
malformed item id refused as `Invalid attention item` with the row untouched.
…ually rests on (TASK-176)

Vera's review of `5c068c37` found two survivors in the arms, and they reproduce
exactly as reported: with the trailing `$` dropped (`{24}$` -> `{24}`) a
25-character id passes the guard and the cast throws — straight back to the 500
this change exists to remove; with the leading `^` dropped, anything *preceding*
24 hex characters does the same. Neither is reachable from a real credential
(`getAuthenticatedUserId` returns `req.userId || req.user?.id || req.user?._id ||
null`), but the guard's whole value is refusal-instead-of-throw, and that value
was unwitnessed against the two edits most likely to be made when somebody copies
or tidies the regex.

Measured in that order, not asserted: this ledger run against the gated head had
M5/M6 surviving (`killed=False failed=0`); against this commit all six mutations
die inside the arm they name. `M4` (guard moved below the update) is included
because WHERE the guard sits is part of the guarantee — a refusal after the write
is not a refusal.

The two new caller shapes are 24 hex characters plus something:

  '507f191e810c19729de860ea0'   25 chars — kills the end-anchor edit
  'x507f191e810c19729de860ea'   prefixed — kills the start-anchor edit

One existing assertion had to scale with the list: the planted rows' statuses
were asserted as a literal ['open','open','open'], which would have kept passing
for the wrong reason at five callers. It is now `callers.map(() => 'open')`.

Test-only: `backend/services/attentionItemService.ts` is byte-identical to
`5c068c37`. Not touched, and named as a residual rather than smuggled in:
`getOpenQueue:466` carries the identical regex with the identical gap.

5 neighbouring suites / 58 tests / 0 failed; `tsc:check` clean; lint multiset for
the changed file identical to the gated head (9 pre-existing findings, added NONE).
@lilyshen0722
lilyshen0722 added this pull request to the merge queue Sep 28, 2026
Merged via the queue into main with commit 1911288 Sep 28, 2026
18 of 23 checks passed
samxu01 pushed a commit that referenced this pull request Sep 28, 2026
…d, and spell out that test-only is code-touching (rule 32)

Vera's HOLD was that the earned note described the `at`/`ms` divergence as "a
defect neither reviewer could see". It was hers: 75185 carries the measurement
(`at=04:00:00.000Z`, `ms` 31 ms behind it, every consumer taking `.at` while
`formatCursor` falls back to `ms`) and the prescription `ms: mark`. The note now
names all three findings and who earned each, which is what the file's authority
rests on — an earned clause that names an unidentifiable defect teaches a reader
the pattern is decorative.

Rider extended, at her request and one citation short of it: a **test**-only move
counts as code-touching, because the arms are much of what a clearance is about.
She cited #1986 `5c068c37`->`0cfd8c0f` and #1977 `f121340e`->`2c55b4d1`; the
first checks out (two commits inside the guard's own test file), the second is
`docs/agents/daemon-seat-state-surfaces.md` +67-0 then +90-0 — a docs move, which
the rule already calls a re-stamp. Only the verified pair is cited.

Docs prose: no automated witness. `docReferences.test.js` scans code dirs, and no
test reads this file (grep for it finds two comments that mention it, neither an
assertion).
lilyshen0722 added a commit to thantiklermcirony/commonly that referenced this pull request Oct 9, 2026
…r it (rule 32) (Team-Commonly#1991)

* docs(review-checklist): a clearance is spent when the head moves under it (rule 32)

* docs(review-checklist): name the three findings rule 32's rider earned, and spell out that test-only is code-touching (rule 32)

Vera's HOLD was that the earned note described the `at`/`ms` divergence as "a
defect neither reviewer could see". It was hers: 75185 carries the measurement
(`at=04:00:00.000Z`, `ms` 31 ms behind it, every consumer taking `.at` while
`formatCursor` falls back to `ms`) and the prescription `ms: mark`. The note now
names all three findings and who earned each, which is what the file's authority
rests on — an earned clause that names an unidentifiable defect teaches a reader
the pattern is decorative.

Rider extended, at her request and one citation short of it: a **test**-only move
counts as code-touching, because the arms are much of what a clearance is about.
She cited Team-Commonly#1986 `5c068c37`->`0cfd8c0f` and Team-Commonly#1977 `f121340e`->`2c55b4d1`; the
first checks out (two commits inside the guard's own test file), the second is
`docs/agents/daemon-seat-state-surfaces.md` +67-0 then +90-0 — a docs move, which
the rule already calls a re-stamp. Only the verified pair is cited.

Docs prose: no automated witness. `docReferences.test.js` scans code dirs, and no
test reads this file (grep for it finds two comments that mention it, neither an
assertion).

* docs(review-checklist): the mutation label in rule 32's earned note is the author's find, not a reviewer's

Vera's second HOLD on the same sentence, and the mirror of the first: I had
attributed the mislabeled mutation to her. It was mine. Her 75177 asks for the
pair of arms (carry and reset) and says nothing about a label; my own 75186
reports the label defect unprompted, and it surfaced from the ledger's named-arm
check on the author's side. Verified both messages at the source before changing
it, because the previous clause failed for exactly this reason.

The three attributions are now Wren / the author's own ledger / Vera, which is a
better argument for the rider than two of three belonging to one reviewer: a
reviewer, the builder's instrument, and the clearance itself.

* docs(review-checklist): state rule 32's TASK-177 findings once, where the count is (sprint-impl review)

The rider named the same two findings twice and the two counts disagreed: the
first list held "two further findings" and the second, introduced by "all three
folded", added the `at`/`ms` divergence that the first never mentioned. A reader
was told two, then three, then handed a list whose third item was new. The fold
carries one list — Wren's paging defect, the mutation label the author's own
ledger caught, and Vera's `at`/`ms` divergence — and then the count, so "three"
has an antecedent.

`two further findings` also had nothing to be further than, since the paragraph
never says what the clearance's own finding was; it is now `two other findings`.

The rider's length and the burial of the clause a presser needs (a stamp names
the head it was taken against) are noted and left alone: that is a question about
the file's shape, not about this sentence, and moving it would change the text the
existing clearance was taken against.

Found by sprint-impl reading the head, not by any witness in this file: docs
prose has none. `verify-numbered-rules.js` still reports 40 rules, 1..40 with no
gap and 17 citations resolving.
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.

1 participant