diff options
| author | Mohamed Hegazy <mhegazy@anthropic.com> | 2026-10-20 23:10:25 +0000 |
|---|---|---|
| committer | Claude <noreply@anthropic.com> | 2026-10-20 23:10:25 +0000 |
| commit | 984a7d4c9db1cd2f91033ee38d9105ddaaefb3c3 (patch) | |
| tree | 08ffe054e5158c354b0b2a454ac59991b4f674f8 | |
| parent | 703af71f6f1c8884cb741c586f359a59a3733c39 (diff) | |
security-guidance: match edited paths when the repo folder is spelled in another case
On file systems that ignore case, realpath keeps the case a path was given
in, so an edit recorded as /TMP/x/REPO/app.py for a repo at /tmp/x/repo was
counted as outside the repo. Fold case (and, on macOS, normalize) the whole
path and the root before taking the relative path, not only the result.
| -rw-r--r-- | plugins/security-guidance/hooks/diffstate.py | 13 | ||||
| -rw-r--r-- | plugins/security-guidance/tests/test_stop_finding_no_edit_record.py | 14 |
2 files changed, 23 insertions, 4 deletions
diff --git a/plugins/security-guidance/hooks/diffstate.py b/plugins/security-guidance/hooks/diffstate.py index ad2160e3..12751878 100644 --- a/plugins/security-guidance/hooks/diffstate.py +++ b/plugins/security-guidance/hooks/diffstate.py @@ -174,16 +174,21 @@ def _rel_in_repo(real_root, path): """`path` relative to the repo root (a real path), or None when it lies outside the repo.""" real = os.path.realpath(path) + if sys.platform == "darwin": + # git and the file system can spell one name composed and decomposed. + real = unicodedata.normalize("NFC", real) + real_root = unicodedata.normalize("NFC", real_root) + if _FOLD_CASE: + # Fold before comparing: realpath keeps the case a path was typed in, + # so the repo folder itself can be spelled differently too. + real, real_root = real.casefold(), real_root.casefold() try: rel = os.path.relpath(real, real_root) except ValueError: # another drive return None if rel == os.pardir or rel.startswith(os.pardir + os.sep): return None - if sys.platform == "darwin": - # git and the file system can spell one name composed and decomposed. - rel = unicodedata.normalize("NFC", rel) - return rel.casefold() if _FOLD_CASE else rel + return rel def touched_scope(repo_root, cwd, touched_paths): diff --git a/plugins/security-guidance/tests/test_stop_finding_no_edit_record.py b/plugins/security-guidance/tests/test_stop_finding_no_edit_record.py index e4ebcdf1..d2dd17dc 100644 --- a/plugins/security-guidance/tests/test_stop_finding_no_edit_record.py +++ b/plugins/security-guidance/tests/test_stop_finding_no_edit_record.py @@ -210,6 +210,20 @@ class TestRecordReadWhenTheNoteIsWritten: assert check([], {"state_resets": 1}) == ([], -1) assert check([], {"state_overwrites": 2}) == ([], -1) + def test_repo_folder_spelled_in_another_case(self, state, monkeypatch): + """On a file system that ignores case the edit tool can be given the + repo folder, or one above it, in a different case.""" + repo, check = state + monkeypatch.setattr(diffstate, "_FOLD_CASE", True) + real = os.path.realpath(str(repo)) + assert check([os.path.join(real.upper(), "app.py")], {}) == ([], 0) + assert check([os.path.join(real.upper(), "APP.PY")], {}) == ([], 0) + scope, inside = diffstate.touched_scope( + str(repo), str(repo), [os.path.join(real.upper(), "app.py")]) + assert (scope, inside) == (1, {"app.py"}) + # A sibling folder is still outside. + assert check([os.path.join(real.upper() + "2", "app.py")], {}) == (["app.py"], 1) + def test_reading_does_not_write_the_state_file(self, state, tmp_path): _, check = state check([], {"touched_paths": []}) |
