Skip to content

fix(sandbox): return 409 instead of 500 for publish refusals on detached HEAD/protected branch - #5109

Merged
pedrofrxncx merged 1 commit into
mainfrom
fix/publish-protected-branch-409-w4
Jul 23, 2026
Merged

pedrofrxncx merged 1 commit into
mainfrom
fix/publish-protected-branch-409-w4

Conversation

@pedrofrxncx

@pedrofrxncx pedrofrxncx commented Jul 23, 2026 •

Copy link
Copy Markdown
Collaborator

Source: a bug found while auditing packages/sandbox/daemon/routes/git.ts for missing/incorrect status codes, the same file (and same audit angle) as the just-merged #5105 ("return 400 for path-traversal in git discard").

Why a maintainer wants it: makeGitPublishHandler returns a generic 500 when publish() throws because the sandbox is on a detached HEAD or the checked-out branch is protected (e.g. main) — but that's a conflict with the current git state, not a server crash, exactly the same class of condition repoNotReadyResponse() already maps to 409 elsewhere in this file. A 500 here misleads alerting/monitoring into treating a normal "you're on main, publish to a feature branch instead" refusal as a daemon fault.

Failure scenario + fix: POST /git/publish while checked out on main (or in a detached HEAD) throws Refusing to push to protected branch "main"... / Cannot publish from a detached HEAD, which the handler previously caught with the catch-all branch and returned as HTTP 500. Fixed by introducing PublishBlockedError, thrown from both refusal sites in publish(), and mapped to 409 in makeGitPublishHandler — mirroring the existing InvalidDecofileBlockError -> 400 and InvalidRemoteBranchNameError -> 400 patterns in the same file. Added a new handler-level test (publish route returns 409 (not 500) for a protected-branch refusal) asserting the status code; the existing function-level test for the protected-branch throw message is untouched (still passes, message unchanged).

Reviewer command: bun test packages/sandbox/daemon/routes/git.test.ts

Local checks run: bun run fmt, cd packages/sandbox && bunx tsc --noEmit (clean), and the targeted test file above (30 pass, up from 29). Full CI validates the rest.


Summary by cubic

Return 409 Conflict (not 500) when POST /git/publish is refused due to detached HEAD or a protected branch, so monitoring doesn’t treat it as a server error. Adds PublishBlockedError and maps it in makeGitPublishHandler; includes a new route test.

  • Bug Fixes
    • publish() now throws PublishBlockedError for detached HEAD or protected branches, and the handler returns 409 instead of 500 (mirrors existing error-to-status mappings).
    • Added git.test.ts test to assert the 409 response and error message.

Written for commit 34b3546. Summary will update on new commits.

Review in cubic

…hed HEAD/protected branch

publish() throwing on a detached HEAD or a protected branch is a conflict
with the sandbox's current git state, not a server fault, but
makeGitPublishHandler mapped it to a generic 500 like a real crash. Mirrors
the InvalidDecofileBlockError -> 400 and InvalidRemoteBranchNameError -> 400
patterns already in this file, and the 409 repoNotReadyResponse() uses for
the same "current git state blocks this request" class of condition.
@pedrofrxncx
pedrofrxncx merged commit 4f16161 into main Jul 23, 2026
15 checks passed
@pedrofrxncx
pedrofrxncx deleted the fix/publish-protected-branch-409-w4 branch July 23, 2026 17:10
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