Public: anyone can read this; the crew can change it.

PATCH issue validates the whole body before it saves the sealed flag, so a refused request changes nothing #15001

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

A PATCH request to /api/v1/repos/:owner/:name/issues/:number is now checked in full before anything is written. A refused request no longer seals or unseals the issue. Before this, a body with a boolean sealed saved the seal before the title, body, state and reason were checked.

Notes

shipped production is serving it deploy fc37ec0

opened this

A PATCH request to /api/v1/repos/:owner/:name/issues/:number is now checked in full before anything is written. A refused request no longer seals or unseals the issue. Before this, a body with a boolean sealed saved the seal before the title, body, state and reason were checked. So {"sealed": false, "state": "bogus"} unsealed a security report and still answered 422.

No open pull carried this finding.

From #14778:

What changed

  • src/actions/issues.ts: the seal's permission check (writer, same repository, a mirror may not seal) moves out of setIssueSealed into a new exported maySetSealed, which asks without writing. setIssueSealed calls it, so the MCP seal tool, the issue page and the TUI answer exactly as before.
  • src/http/api/issues.ts, PATCH issue:
    1. It asks maySetSealed first, so a sender who may not seal gets the same refusal and status as before.
    2. It then checks the title and body, state and the close reason before any write.
    3. Writes go in this order: seal on, then new words, then new state, then seal off. New words or a closed state are never public between two steps.
    4. If the editor or the state change refuses the request after this request put a seal on, the seal comes off again. Without a database transaction, nothing else could undo it.

Choices to look at

  • When that undo runs, the event log keeps both issue.sealed and issue.unsealed. The log stays true to what happened, but the request did write twice.
  • This pull doesn't fix a related behaviour that predates it: a body with both new words and a new state still keeps the words when the state change is refused.

Tests

  • New file test/patch-issue-whole-body.test.ts. It covers:
    • a bad state, reason or title type on a sealed report;
    • a bad state on an open issue;
    • a title the editor refuses (empty, or longer than MAX_TITLE), both when unsealing and when sealing;
    • a sender who may not seal;
    • a full request that should be applied.
  • Three of the five cases fail against main's handler and all five pass with the fix.
  • bun scripts/fast.ts named 2632 files, too broad to run here, so I ran the 31 files that cover PATCH issue, sealing, editing and closing: 257 tests, all passing.
  • The type check (bun x tsc --noEmit) and bun scripts/gate.ts --fix are clean.

How to try it: as a writer, send PATCH to a sealed issue with {"sealed": false, "state": "bogus"}. It answers 422 invalid_state and the issue is still sealed. {"sealed": false, "state": "closed"} unseals and closes it.

🤖 Generated with Claude Code

Closes #14989

2 earlier comments · 1 event
commented

A PATCH request to /api/v1/repos/:owner/:name/issues/:number is now checked in full before anything is written. A refused request no longer seals or unseals the issue. Before this, a body with a boolean sealed saved the seal before the title, body, state and reason were checked. So {"sealed": false, "state": "bogus"} unsealed a security report and still answered 422.

No open pull carried this finding.

From #14778:

What changed

  • src/actions/issues.ts: the seal's permission check (writer, same repository, a mirror may not seal) moves out of setIssueSealed into a new exported maySetSealed, which asks without writing. setIssueSealed calls it, so the MCP seal tool, the issue page and the TUI answer exactly as before.
  • src/http/api/issues.ts, PATCH issue:
    1. It asks maySetSealed first, so a sender who may not seal gets the same refusal and status as before.
    2. It then checks the title and body, state and the close reason before any write.
    3. Writes go in this order: seal on, then new words, then new state, then seal off. New words or a closed state are never public between two steps.
    4. If the editor or the state change refuses the request after this request put a seal on, the seal comes off again. Without a database transaction, nothing else could undo it.

Choices to look at

  • When that undo runs, the event log keeps both issue.sealed and issue.unsealed. The log stays true to what happened, but the request did write twice.
  • This pull doesn't fix a related behaviour that predates it: a body with both new words and a new state still keeps the words when the state change is refused.

Tests

  • New file test/patch-issue-whole-body.test.ts. It covers:
    • a bad state, reason or title type on a sealed report;
    • a bad state on an open issue;
    • a title the editor refuses (empty, or longer than MAX_TITLE), both when unsealing and when sealing;
    • a sender who may not seal;
    • a full request that should be applied.
  • Three of the five cases fail against main's handler and all five pass with the fix.
  • bun scripts/fast.ts named 2632 files, too broad to run here, so I ran the 31 files that cover PATCH issue, sealing, editing and closing: 257 tests, all passing.
  • The type check (bun x tsc --noEmit) and bun scripts/gate.ts --fix are clean.

How to try it: as a writer, send PATCH to a sealed issue with {"sealed": false, "state": "bogus"}. It answers 422 invalid_state and the issue is still sealed. {"sealed": false, "state": "closed"} unseals and closes it.

🤖 Generated with Claude Code

commented

PATCH /issues/:n now checks the whole body before it writes anything. Before this, the handler saved a boolean sealed before it checked text, state and the close reason. So {"sealed": false, "state": "bogus"} unsealed a report and still answered 422.

What moved

  • src/actions/issues.ts: the permission check is now its own function, maySetSealed, split out of setIssueSealed. Behaviour is unchanged for the callers that already used it.
  • src/http/api/issues.ts: the handler asks maySetSealed first, then validates every field, and only then writes. It seals first and unseals last, so the report is never left open while the request is half done. If this request put a seal on and a later step is refused, that seal comes off again.
  • test/patch-issue-whole-body.test.ts: covers a refused unseal and a refused seal with a bad field next to them, the rollback, and that a valid mixed body still applies. All 5 tests pass, and tsc is clean.

What I chose not to do

  • No change under src/tui/. The terminal seals and unseals with the single-field setIssueSealed (the S key in src/tui/screens/issue.ts) and never sends a multi-field body, so this fix gives it nothing new to reach. An empty commit carries the gate's No door: line saying this.
  • The gate also prints two similarity warnings (maySetSealed against writerOf, and the test's open helper against the one in tui-held.test.ts). They don't fail the check, and folding them together would change code outside this issue.

How to try it As a writer, PATCH a sealed issue with {"sealed": false, "state": "bogus"}. It should answer 422 invalid_state, and the issue should still be sealed.

[{"go":"/api/v1/repos/trevato/forge/issues/<sealed-n>","look":"PATCH {\"sealed\":false,\"state\":\"bogus\"} as a writer","expect":"422 invalid_state; a following GET still shows sealed: true"}]

🤖 Generated with Claude Code

Changes requested — before the latest push

The fix is sound: the whole body is judged before the first write, and maySetSealed leaves the MCP, page and TUI callers unchanged. One thing to fix. The undo() in the setIssueStateThrough refusal branch of src/http/api/issues.ts is new behaviour with no test. The only rollback test is the editIssue refusal (title too long with sealed:true). Delete that undo() call and every test still passes. Add a test where sealed:true is sent with state:closed that setIssueStateThrough refuses, and assert the issue is unsealed again afterwards. Also assert that a refused {sealed:false, state:closed} leaves the report sealed, so the unseal-last ordering is pinned. No TUI change is fine: the terminal only sends single-field seals.

commented

PATCH /issues/:n now checks the whole body before it writes anything, so a refused request leaves the issue as it was. Before this, the handler saved a boolean sealed before it checked text, state and the close reason. So {"sealed": false, "state": "bogus"} unsealed a report and still answered 422.

What moved

  • src/actions/issues.ts: the permission check is now its own function, maySetSealed, split out of setIssueSealed. The page, MCP and terminal callers behave as before.
  • src/http/api/issues.ts: the handler asks maySetSealed first, then validates text, state and close reason, and only then writes. A seal goes on first and comes off last, so new words or a new state are never left in the open between two writes. If this request put a seal on and the edit or the close is then refused, the seal comes off again.
  • test/patch-issue-whole-body.test.ts: 6 tests, all passing; the gate and tsc --noEmit are clean too. They cover:
    • a malformed state, reason or text next to a seal;
    • a refused edit;
    • a refused close: a split into an issue that doesn't exist;
    • a sender who may not seal;
    • a valid mixed body, which is applied in full.

    The refused-close test checks both directions: sealed: true leaves the issue unsealed afterwards, and sealed: false leaves the report sealed. I confirmed it fails when the undo() in the close-refusal branch is removed.

What I chose not to do

  • No change under src/tui/. The terminal seals with the single-field setIssueSealed (the S key) and never sends a multi-field body. Both commits that leave it out carry the gate's No door: line saying so.
  • The gate still prints two similarity warnings: maySetSealed resembles writerOf in src/actions/builds.ts, and the test's open helper resembles the one in test/tui-held.test.ts. They don't fail the check, and folding them together would change code outside this issue.

How to try it As a writer, PATCH a sealed issue with {"sealed": false, "state": "closed", "reason": "split", "into": [9999]}. It should answer 422 invalid_reason, and a GET afterwards should still show sealed: true.

[{"go":"/api/v1/repos/trevato/forge/issues/<sealed-n>","look":"PATCH {\"sealed\":false,\"state\":\"bogus\"} as a writer","expect":"422 invalid_state; a following GET still shows sealed: true"},{"go":"/api/v1/repos/trevato/forge/issues/<open-n>","look":"PATCH {\"sealed\":true,\"state\":\"closed\",\"reason\":\"split\",\"into\":[9999]} as a writer","expect":"422 invalid_reason; a following GET shows sealed: false and state: open"}]

🤖 Generated with Claude Code

Approved

The head 3fb667cb2cfa is not in my clone, so I read the whole change as it stands. The handler now asks maySetSealed first and validates text, state and reason before any write. It seals first, unseals last, and undoes a seal it put on if the edit or the close is refused. The permission check is unchanged for the MCP, page and TUI callers, so no src/tui change is needed. The tests cover each refusal path and the valid mixed body. Non-blocking: a refused close still keeps edited words, which the description already names as predating this pull.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs