Skip to content

Scan files as UTF-8 instead of the platform locale - #1014

Open
blaze0089 wants to merge 1 commit into
Yelp:masterfrom
blaze0089:fix/scan-utf8-locale
Open

blaze0089 wants to merge 1 commit into
Yelp:masterfrom
blaze0089:fix/scan-utf8-locale

Conversation

@blaze0089

Copy link
Copy Markdown

Fixes #1013.

_get_lines_from_file opened files without an encoding, so Python used the platform locale. On Windows that is usually the ANSI code page (cp1252), which cannot decode valid UTF-8 containing bytes such as 0x81 — the second byte of Cyrillic с, for example. The UnicodeDecodeError was caught by the existing binary-file handler and the whole file was dropped, so any secrets in it were silently missed.

This opens files as UTF-8, which is already what the rest of the project assumes (README.md and setup.py both read UTF-8 explicitly). Files that genuinely are not UTF-8 still fall through to the same handler; they now also get a debug line so a skipped file is at least visible with --log-level DEBUG.

The regression test simulates a cp1252 default by wrapping builtins.open and scans a UTF-8 file with a Basic Auth URL. It fails on master with an empty result and passes with the change.

Verification:

  • pytest tests/core/scan_test.py — 8 passed
  • flake8 --max-line-length 100 detect_secrets/core/scan.py tests/core/scan_test.py — clean
  • Full suite still has the same 11 failures on master in this environment (newer unidiff/pytest in untouched tests), unrelated to this change

Files were opened without an explicit encoding, so on Windows the default ANSI code page (often cp1252) was used. Valid UTF-8 files containing bytes that are undefined in that code page, such as 0x81, raised UnicodeDecodeError and were silently treated as binary, so any secrets in them were missed.

Open files as UTF-8, which is what detect-secrets already assumes elsewhere (README/setup.py), and log skipped files at debug level instead of dropping them without a trace.

Adds a regression test that simulates a cp1252 default and verifies a UTF-8 file is still scanned.
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.

Files that don't decode with the locale encoding are silently skipped (e.g. UTF-8 Cyrillic on Windows cp1252)

1 participant