Skip to content

Fix #994: write the CRLF-unsafe pre-commit hook via open(newline="\n"), not Path.write_text() - #998

Merged
fdaviddpt merged 2 commits into
mainfrom
fix/994
Oct 8, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix/994

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

tests/test_git_backup_rotate_advice_974.py:154's test_could_not_untrack_also_logs_rotate_advice wrote its pre-commit git hook via pre_commit.write_text("#!/bin/sh\nexit 1\n") with no newline= guard. Path.write_text()'s default newline=None applies universal-newline translation on write, which on Windows turns every \n into \r\n, corrupting the shebang line into a "bad interpreter" exec failure instead of the intended exit 1.

Inert today: the whole file carries pytestmark = pytest.mark.skipif(sys.platform == "win32", ...) for an unrelated reason (#79), so nothing in CI exercises the CRLF path currently. Fixed anyway rather than left as a trap for whoever later narrows that skip.

Fix

Extracted the write into a new helper, _write_fail_hook(hooks_dir), written via open(path, "w", newline="\n", encoding="utf-8") -- the same pattern tests/test_autonomous_log_retention_487.py's _write_shell_script already uses for the identical reason (CI PR #960, windows-latest). Path.write_text(newline=...) was deliberately avoided: that kwarg is Python 3.10+ only, and this repo's CI floor is 3.9 (.github/workflows/tests.yml matrix python-version: ["3.9", "3.10", "3.11", "3.12"]).

test_could_not_untrack_also_logs_rotate_advice -- the test #994 itself names -- now calls _write_fail_hook instead of inlining the vulnerable write.

A new regression test, test_fail_hook_writer_never_uses_path_write_text, pins the call: it mocks Path.write_text to raise if triggered, then asserts _write_fail_hook never does, and checks the written bytes and file mode. This is a REASONED cross-platform claim, not an OBSERVED one -- this lane ran on darwin, where os.linesep is already \n, so the CRLF corruption itself cannot be reproduced locally; the test instead pins the call path deterministically regardless of platform.

Self-review caught a real bug

The first commit added _write_fail_hook and unit-tested it in isolation but never rewired the actual named test to call it, leaving the fix as dead code from the production suite's point of view. Both spawned reviewers (Explore and oss:auditor) independently found this; fixed in a follow-up commit that rewires the named test and re-confirms green (5 passed).

Below-bar finding (not fixed, recorded here)

oss:auditor also noted three other write_text(...) calls in the same file (writing now.md and config.json fixture content) share the same unguarded-write shape, but are not shebang/script writes -- they are plain content read back only via a helper that itself uses read_text(), so any CRLF introduced on Windows round-trips transparently on read and produces no observable difference in any string comparison in this suite. Verified independently: Path.read_text()'s default newline=None also applies universal-newline translation on read. No reachable caller sees a different result -- below-bar, not a defect.

Adjacent finding -- reported for filing, not fixed (out of scope here)

Three sibling test files share the same unguarded write_text("#!/bin/sh...") shape as #994's file, found by recon, none touched by this diff:

Each would need its own red/green cycle and roughly triples this diff's size; left for a separate issue.

Tests

Red: python3 -m pytest tests/test_git_backup_rotate_advice_974.py::test_fail_hook_writer_never_uses_path_write_text -q -> 1 failed (mocked Path.write_text raised).
Green: python3 -m pytest tests/test_git_backup_rotate_advice_974.py -q -> 5 passed in 2.24s.
Full suite not run (targeted file only, per brief); guard sweep via lane_setup.py found no cross-cutting guard applies.

Closes #994

🤖 Generated with Claude Code

[AI-generated]

fdaviddpt and others added 2 commits October 8, 2026 12:46
…h.write_text()

`tests/test_git_backup_rotate_advice_974.py::test_could_not_untrack_also_logs_rotate_advice`
wrote its `pre-commit` hook script with `Path.write_text()`, whose default
`newline=None` applies universal-newline translation and would turn the
shebang's `\n` into `\r\n` on Windows, corrupting it into a "bad interpreter"
exec failure instead of the intended `exit 1`.

Extracted into `_write_fail_hook`, now written via
`open(path, "w", newline="\n", encoding="utf-8")`, the same pattern
`tests/test_autonomous_log_retention_487.py`'s `_write_shell_script` already
uses for the identical reason (CI, PR #960, windows-latest).

Inert today -- this whole file is skipped on win32 for an unrelated reason
(#79), so nothing in CI exercises the CRLF path either way -- but fixed
rather than left as a trap for whoever narrows that skip later.

Adds a regression test pinning the write *call* itself (via
unittest.mock.patch.object(Path, "write_text", ...)), not the output bytes:
os.linesep is already "\n" on every platform this suite can exercise
locally, so the corrupted bytes this bug produces on Windows would not
appear here even without the fix.

Red (pre-fix, Path.write_text() reinstated): 1 failed -
AssertionError: pre-commit hook written via Path.write_text(), not
open(newline='\n')
Green (post-fix): tests/test_git_backup_rotate_advice_974.py -- 5 passed in 2.02s

Co-Authored-By: Claude Sonnet 5 <[email protected]>
…d test

Both spawned reviewers (Explore and oss:auditor) independently found the
same bug in the previous commit: `_write_fail_hook` was added and unit-
tested in isolation, but `test_could_not_untrack_also_logs_rotate_advice`
-- the exact test #994, the commit message and the changelog fragment all
named as the one being fixed -- still built its own `pre_commit` path and
called `Path.write_text()` directly, unchanged. The new helper was dead
code from the production test suite's point of view.

Rewires that test to call `_write_fail_hook(hooks_dir)` instead of
inlining the vulnerable write. Re-ran
tests/test_git_backup_rotate_advice_974.py after this fix: 5 passed.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@fdaviddpt
fdaviddpt merged commit 112dcef into main Oct 8, 2026
19 checks passed
@fdaviddpt
fdaviddpt deleted the fix/994 branch October 8, 2026 11:27
fdaviddpt added a commit that referenced this pull request Oct 31, 2026
)

* Fix #999: three sibling test fixtures write shebangs CRLF-safely

tests/test_detect_tools_cache_668.py (two sites), tests/test_stdin_source_top_level_wins_344.py
and tests/test_session_start_compact_recap_339.py each wrote a `#!/bin/sh` shim via
`Path.write_text()`, whose default `newline=None` applies universal-newline translation
on write and corrupts the shebang into a "bad interpreter" exec failure on Windows -- the
same shape #994/#998 fixed. test_detect_tools_cache_668.py carries no win32 skip, so this
one is live on windows-latest CI today; the other two are currently inert behind an
existing skip, same as #994's own file, but are fixed anyway rather than left as a trap.

Each file gains a local _write_shell_script helper (open(..., newline="\n")) and a
regression test pinning that the helper never falls back to Path.write_text().

Co-Authored-By: Claude Sonnet 5 <[email protected]>

* Fix #999 follow-up: raw-string the shebang helper docstrings and assertion message

Self-review (Explore) caught that the new `_write_shell_script` helper's and its
regression test's docstrings, plus the mocked AssertionError message, used plain
(non-raw) triple/double-quoted strings containing literal-looking `\n`/`\r\n`
sequences meant as prose about escape characters. Without `r"""`/`r"..."`, Python's
parser turns those into real embedded newlines at compile time, garbling the
rendered docstring text (confirmed via __doc__ repr before/after). All three
files are now consistently raw-stringed for these literals.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

---------

Co-authored-by: Claude Sonnet 5 <[email protected]>
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.

test_git_backup_rotate_advice_974.py: pre_commit.write_text has no newline= guard against CRLF shebang on Windows

1 participant