applier: refuse to clobber a symlinked LICENSE (#33) #40

Merged
KofTwentyTwo merged 1 commits from feature/GH-33-symlink-license into develop 2026-06-09 15:17:56 +00:00
KofTwentyTwo commented 2026-06-09 13:01:16 +00:00 (Migrated from github.com)

Summary

Fixes the [MED] write-path-safety issue: managed LICENSE / LICENSES/<id>.txt files were written via os.Stat + temp-file + os.Rename with no symlink check. If the target was a deliberate symlink (e.g. LICENSE -> LICENSES/MIT.txt), os.Rename replaced the link with a regular file, silently breaking it.

Changes

  • AtomicWrite now os.Lstats the target. If it is a symlink it refuses with a sentinel errSymlinkTarget and writes nothing, leaving the link and its target untouched. For a regular file, Lstat returns the same mode bits as Stat, so mode preservation is unchanged.
  • The refusal message contains "refusing to write", so the CLI's existing isWriteRefusal mapping classifies it as a write refusal (exit code 3) rather than a generic internal error.
  • writeManaged propagates a symlink refusal as a returned error (so it reaches License/Apply -> writeOrInternalError); all other write failures remain recorded per-file (fr.Err) so one bad path does not abort the run, matching prior behavior.

Scope

Source-file headers are unaffected (enumerate already skips symlinks). This change is specifically the license-file write path, plus the shared AtomicWrite guard.

Tests (TDD, red -> green)

  • TestAtomicWriteRefusesSymlink: symlinked target is refused; Lstat still reports a symlink; link destination and target content unchanged.
  • TestWriteManagedRefusesSymlink / TestLicenseRefusesSymlinkTarget: the managed/public write path surfaces the refusal as a returned error.
  • TestCmd...symlinked LICENSE is refused with the write-refused exit code: asserts exit code 3 via the CLI.
  • Normal (non-symlink) write + idempotency remain green.

Gate

  • gofmt -l .: clean
  • go vet ./...: clean
  • go test ./... -race: pass
  • coverage gate: 100%
  • golangci-lint run: 0 issues

Closes #33

🤖 Generated with Claude Code

## Summary Fixes the [MED] write-path-safety issue: managed `LICENSE` / `LICENSES/<id>.txt` files were written via `os.Stat` + temp-file + `os.Rename` with **no symlink check**. If the target was a deliberate symlink (e.g. `LICENSE -> LICENSES/MIT.txt`), `os.Rename` replaced the link with a regular file, silently breaking it. ## Changes - `AtomicWrite` now `os.Lstat`s the target. If it is a symlink it refuses with a sentinel `errSymlinkTarget` and writes nothing, leaving the link and its target untouched. For a regular file, `Lstat` returns the same mode bits as `Stat`, so mode preservation is unchanged. - The refusal message contains `"refusing to write"`, so the CLI's existing `isWriteRefusal` mapping classifies it as a **write refusal (exit code 3)** rather than a generic internal error. - `writeManaged` propagates a symlink refusal as a returned error (so it reaches `License`/`Apply` -> `writeOrInternalError`); all other write failures remain recorded per-file (`fr.Err`) so one bad path does not abort the run, matching prior behavior. ## Scope Source-file headers are unaffected (enumerate already skips symlinks). This change is specifically the license-file write path, plus the shared `AtomicWrite` guard. ## Tests (TDD, red -> green) - `TestAtomicWriteRefusesSymlink`: symlinked target is refused; `Lstat` still reports a symlink; link destination and target content unchanged. - `TestWriteManagedRefusesSymlink` / `TestLicenseRefusesSymlinkTarget`: the managed/public write path surfaces the refusal as a returned error. - `TestCmd...symlinked LICENSE is refused with the write-refused exit code`: asserts exit code 3 via the CLI. - Normal (non-symlink) write + idempotency remain green. ## Gate - `gofmt -l .`: clean - `go vet ./...`: clean - `go test ./... -race`: pass - coverage gate: 100% - `golangci-lint run`: 0 issues Closes #33 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign in to join this conversation.