Skip to content

Fix open issues from the 2026-09-30 b58fce9 reanalysis (4.1-4.4) - #13

Merged
MuffinUser merged 5 commits into
mainfrom
fix-b58fce9-findings
Oct 1, 2026
Merged

MuffinUser merged 5 commits into
mainfrom
fix-b58fce9-findings

Conversation

@MuffinUser

@MuffinUser MuffinUser commented Oct 1, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fixes the four open findings from audits/2026-09-30-reanalysis-b58fce9.md, each confirmed with a real reproduction before fixing (not just the report's description taken at face value) and each covered by a regression test that was verified to actually fail on the pre-fix code.

  • 4.2 — confidentiality bug (highest priority): LooksEncrypted validated each ENC[...] field independently instead of sharing Parse's grammar, so several malformed-but-prefix-matching strings (e.g. ENC[AES256_SIV,data:]) were wrongly treated as already-encrypted and left unprotected by Clean.
  • 4.1 — wrong rotate recovery advice: the printed recovery hint told users to run git checkout -- ., which doesn't do what it claimed once the config was already staged. rotate now reverts itself automatically (config file, index entry, and orphaned local key) instead of printing instructions, and the auto-revert knows the difference between a plain HEAD and a still-uncommitted prior rotation (AC-11.9).
  • 4.3 — cross-process key file races: Set's read-modify-write of the local key file had no cross-process locking; a corrected repro lost 37/60 concurrent registrations in one run (the report's own repro number turned out to be a test-isolation artifact — corrected here). Added flock/LockFileEx-based locking, plus a second lock closing the related "two concurrent rotates mint the same version" collision the original report also described.
  • 4.4 — legacy value tag literal gaps: validTypeLiteral used strconv's parsers, which are more permissive than either output format (NaN/Inf for floats, 1/t for bools, +5/007 for ints) — closed via a round-trip check against each format's own Locate/Render. The int case was added in a follow-up commit after review.

Commits

Five commits: one per finding, plus a follow-up closing 4.4's int case (4.3's rotate-level half lives in the 4.1 commit since it's inseparable from rotate.go's changes there — noted in that commit's message). Each commit was individually checked out into a throwaway worktree and built + go vet + full test suite run against it in isolation to confirm it doesn't depend on a later commit.

Test plan

  • go build ./..., go vet ./... (linux/amd64, linux/arm64, darwin/arm64, windows/amd64)
  • go test ./... and go test -race ./..., full suite, green
  • Each new regression test verified to fail against the pre-fix code (lock removed, SetIndexEntry reverted to Add, etc.) before being confirmed to pass against the fix
  • scripts/gen-notices.sh re-run — no diff (x/sys was already fully documented as an indirect dependency)

Known remaining items (not in this PR)

  • nebel status (missing feature, spec 09)
  • config.Save is not atomic (a crash mid-write could still truncate .nebel.yaml, independent of this PR's revert logic)
  • Directory fsync after rename, for durability against power loss
  • scanUndecrypted (used by rotate's pre-flight check) still uses the bare ENC[ prefix check rather than the now-stricter LooksEncrypted — a plaintext secret that happens to look like a malformed tag will now be correctly encrypted by Clean, but rotate would still misreport it as "stuck ciphertext" first run

🤖 Generated with Claude Code

MuffinUser and others added 5 commits October 1, 2026 11:44
The 2026-09-30 reanalysis (finding 4.2) found that LooksEncrypted
validated each ENC[...] field independently instead of sharing Parse's
grammar, so a plaintext value shaped like one of these was wrongly
treated as already-encrypted and left unprotected by Clean:

  ENC[AES256_SIV,data:]
  ENC[AES256_SIV,data:AAAA,data:AAAA]
  ENC[AES256_SIV_TB,data:AAAA]

LooksEncrypted now delegates to the same positional field-parsing core
Parse uses (rejecting duplicate/reordered/trailing fields and an
AES256_SIV_TB tag missing its mandatory key field automatically), with
one documented, narrow exception: a keyless pre-rotation AES256_SIV tag
is still recognized as genuine ciphertext rather than double-wrapped.
It also now rejects a "data:" field that decodes to fewer bytes than
real AES-256-SIV output can ever be (internal/siv.MinCiphertextLen).

Several existing test fixtures used placeholder ciphertext shorter than
that minimum; bumped them to realistic lengths rather than loosening the
new check to keep them passing.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
The 2026-09-30 reanalysis (finding 4.4) found that validTypeLiteral used
strconv's own parsers to decide whether a decrypted value was a genuine
literal of its claimed type, but strconv is more permissive than either
output format: strconv.ParseFloat accepts "NaN"/"Inf", and
strconv.ParseBool accepts "1"/"t" and more. Neither reads back as the
claimed type in JSON or this project's YAML handling, so an attacker
with write access who flips a legacy (pre-Package-F, unauthenticated)
tag's type field onto one of these could make Clean/Smudge render
invalid JSON, or a silently wrong-typed value.

Added Handler.RoundTrip (internal/format): it renders a value as its
claimed type, then re-reads that rendered text back through the same
Locate function Clean/Smudge already use, requiring the exact same value
and type to come back out. validTypeLiteral now uses this for
TypeFloat/TypeBool instead of strconv directly. TypeInt keeps its
existing strconv range check, since JSON has no defined integer range
for RoundTrip to enforce on its own (the deliberate int64-overflow limit
from the original R06 fix).

Because Render and Locate are the same pair of functions a field's type
was originally classified by, anything that actually went through Clean
normally round-trips by construction — this only ever rejects a value
whose claimed type doesn't match what a parser would otherwise infer for
it, i.e. exactly the adversarial case.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
The 2026-09-30 reanalysis (finding 4.3) found that Set's read-modify-write
of the local key file was serialized only within one process (setMu): two
concurrent `nebel` processes (a racing filter invocation, two concurrent
`nebel rotate` runs, ...) could each read the same old content and then
each write back a different, equally "complete" update, silently losing
whichever lost the race. A corrected repro (the originally reported
figure was a test-isolation artifact, not a real measurement) lost 37 of
60 concurrent registrations in a single run.

Set now also takes an exclusive, cross-process advisory lock on a
dedicated sibling file for the same read-modify-write span: flock on Unix
(internal/localkey/filelock_unix.go), LockFileEx on Windows
(filelock_windows.go). Locking a sibling file rather than the key file
itself matters because writeKeyFile replaces the key file by renaming a
temp file onto it (for atomicity against a concurrent reader) — a lock
held on the key file's own inode would stop protecting anything the
moment the first writer's rename swapped a new inode in underneath it.

Also adds:
- Remove, to delete a single version's entry (used by cmd/nebel/rotate.go
  in a later commit to undo its own Set on a failed, reverted rotation).
- LockRotation, a second lock serializing an entire `nebel rotate`
  operation against another concurrent one in the same clone (used in the
  same later commit) — Set's own lock only protects the key file's bytes,
  not the higher-level decision of which next version number to mint, so
  it alone doesn't stop two concurrent rotations from both reading the
  same current version and each minting the same next one with a
  different key.

golang.org/x/sys was already an indirect dependency (via golang.org/x/term);
go.mod now lists it directly, with no version change.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
…races

The 2026-09-30 reanalysis (finding 4.1) found that rotateRecoveryHint told
the user to run `git checkout -- .` to undo a failed rotation, claiming it
"fully discards" the attempt. That command restores the working tree from
the index, not HEAD — once gitutil.Add had already staged the rotated
config (exactly the state when a later re-encryption failure is reported),
the rotation stayed staged afterward despite the message's claim. `.`
could also discard unrelated uncommitted work elsewhere in the repo.

`nebel rotate` now reverts itself automatically instead of printing
instructions. Before changing anything, it snapshots .nebel.yaml's exact
working-tree bytes and index entry ("<mode>,<sha>", gitutil.IndexEntry),
and the local keyring's prior entry (if any) for the version it's about
to mint. On any failure, it restores all of that precisely:

- The working tree file, always.
- The index entry via gitutil.SetIndexEntry rather than re-running `git
  add` — which would instead stage whatever happens to be in the working
  tree by then, including a pre-existing unstaged edit to .nebel.yaml that
  predates this rotate attempt and was never staged in the first place.
- The local key registration for the version this attempt minted: Set it
  back to whatever was there before (almost always nothing, so Remove),
  rather than leaving this attempt's orphaned key registered. Left behind,
  it would collide with whatever key a teammate's own later, independent
  rotation to that same version number eventually registers, turning that
  clone's next `git pull` into a hard authentication failure instead of
  the usual graceful "missing key" passthrough.

Restoring to the snapshot taken when this attempt started, not to HEAD,
matters: AC-11.9 explicitly allows running `rotate` again before a
previous rotation is even committed, in which case HEAD is one rotation
further back than what a failed retry should revert to.

Also closes the other half of reanalysis finding 4.3: two concurrent
`nebel rotate` runs in the same clone could each read the same current
version and independently mint the same next one with a different key,
silently colliding. runRotate now holds localkey.LockRotation for the
whole operation, so the second attempt sees the first's committed-to-disk
result and mints the version after it instead.

Fixed the CLI help text, which claimed rotate "does not touch
already-encrypted content" — it eagerly re-encrypts everything.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
The previous commit closed reanalysis finding 4.4 for floats and bools,
but left ints on strconv.ParseInt alone. That accepts a leading "+" and
leading zeros: "+5" isn't valid JSON number syntax, and "007" is neither
valid JSON nor read back as an int by the YAML handler. A legacy value
tag whose unauthenticated type field is flipped from str to int could
therefore still render invalid JSON, or a silently changed type.

validTypeLiteral now runs handler.RoundTrip for ints too. The strconv
check stays alongside it for the deliberate int64 range limit, which
RoundTrip can't enforce on its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MuffinUser
MuffinUser merged commit 3122426 into main Oct 1, 2026
6 checks passed
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