Skip to content

find_hash_lookup: refuse a --json that names the scanned image or a folder - #6749

Open
evilclanker9000 wants to merge 3 commits into
ItsNotRexed:mainfrom
evilclanker9000:deepseek/8-findhash-json-out
Open

evilclanker9000 wants to merge 3 commits into
ItsNotRexed:mainfrom
evilclanker9000:deepseek/8-findhash-json-out

Conversation

@evilclanker9000

Copy link
Copy Markdown
Contributor

tools/find_hash_lookup.py writes its --json report after the scan with no check on the
path (the write was at the end of main()). Two consequences:

  • --json naming the image it reads (--json game_scarlett_master.exe) replaced the game
    executable with the JSON report: the tool opened the same file for writing after reading it,
    so the PE was truncated by its own report.
  • --json naming a folder, or a path in a folder that does not exist, raised a stack trace
    from the report write — after the ranking had been printed — and exited 1.

This refuses both before the image is read, with one ERROR: line and exit 2, the way
rtti_scan.py and the other tools guard their report path (tools/report_paths.py,
same_report_path). A report path that cannot be written (read-only file, full disk) is now
one ERROR: cannot write <path>: <reason> line and exit 2 instead of a traceback.

Test

python -I tests/test_find_hash_lookup.py

The three new cases build the same synthetic PE the test already used:

  • --json <the image>: exit 2, stdout empty (so the guard runs before the scan), and the
    image is byte for byte unchanged;
  • --json <a folder>: exit 2, one ERROR: line, no Traceback;
  • --json <tmp>/missing/report.json: exit 2, one ERROR: cannot write ... line, no report
    and no Traceback.

Before the change, with the tool reverted and the tests kept:

FAIL: test_json_in_a_missing_folder_is_one_error
AssertionError: 1 != 2 : Traceback (most recent call last):
  ...
FileNotFoundError: [Errno 2] No such file or directory: '...\missing\report.json'
FAIL: test_json_naming_a_folder_is_one_error
AssertionError: 1 != 2 : Traceback (most recent call last):
  ...
PermissionError: [Errno 13] Permission denied: '...\tmpvmlwvsw4'
FAIL: test_json_naming_the_image_is_refused_before_the_scan
AssertionError: 0 != 2 :
Ran 7 tests in 0.448s
FAILED (failures=3)

With the change:

Ran 7 tests in 0.403s
OK

python -m compileall -q and python -I tools/find_hash_lookup.py --help are clean.

No GPU needed.

Notes

  • I put the guard before the image is read, not next to the write, so a mistyped --json
    cannot destroy the executable even when the scan itself would have failed.
  • --window and --top still take any integer (--top 0 prints no candidates, a negative
    --window scans a shorter tail). That is a separate argument check and I left it out of
    this PR to keep it one refusal.
  • This touches main() only; the pending joaat change in find_hash_lookup: hash a name over the lower-cased Latin-1 bytes, as the other joaat copies do #6254 is in joaat() and does not
    overlap.

AI-assisted.

…older

The report was written after the scan with no check on its path, so --json naming the
.exe replaced the game image with the report. The guard runs before the image is read,
and a folder is refused here too instead of failing at the write.

(DeepSeek8)
…back

A missing parent folder, an --out that names a folder or a full disk raised from the
report write after the whole image had been scanned; the ranking had already been
printed and the exit status was 1 with a stack trace. Name the path and exit 2, as the
other tools' report writers do.

(DeepSeek8)
Three synthetic-PE cases: --json naming the image itself (the image must be byte for
byte unchanged and stdout empty, so the guard runs before the scan), --json naming a
folder, and --json in a missing folder (one ERROR line, exit 2, no traceback). All
three fail on upstream/main.

(DeepSeek8)
@Juryii

Juryii commented Nov 23, 2026

Copy link
Copy Markdown

Independently reviewed and tested 30d82354bed51ef28198cc9ccba8c58a1506984f on Windows / CPython 3.12.5: python -I tests/test_find_hash_lookup.py -v — 7 passed. Changed-file compilation and git diff --check pass; checkout clean.

Additional real-filesystem CLI checks on the suite's synthetic PE:

  • report.json hardlinked to image.exe: head refuses with exit 2 before ranking (empty stdout), preserving the image byte-for-byte. Exact base 1cc6e3c12 instead exits 0 and overwrites the same image with JSON.
  • case-only image/IMAGE.EXE output alias: exit 2, empty stdout, image unchanged.
  • Existing report held open with Win32 CreateFileW, permitting reads/deletion but denying write sharing: CLI exits 2 with exactly one ERROR: cannot write line, no traceback, and the previous report stays byte-identical. Image also stays unchanged.
  • After closing that handle, the same command exits 0 and writes parseable JSON containing the candidates report.

No blocking findings within this scope. The shared path guard catches the additional aliases, and the actual write failure is handled cleanly. This checks the tested refusal/write-error cases, not atomic replacement or power-loss durability of report output.

CPU only, synthetic PE and temporary reports; no game executable or GPU used. AI-assisted review with Codex; suites, negative control and Win32-handle reproduction executed locally.

@markankamaransheogarat-sys

Copy link
Copy Markdown
Contributor

Independent CPU review at 30d82354b (AI-assisted):

Code inspection:

  • tools/find_hash_lookup.py: refuses --json before scanning when os.path.isdir(a.json) or same_report_path(a.exe, a.json) (exit 2), and wraps open(a.json, "w") in try ... except OSError so unwritable paths print a clean ERROR: line and exit 2.
  • tests/test_find_hash_lookup.py: covers --json naming the input image (asserting image bytes stay intact), a directory, and a missing parent directory.

Checks run on Linux CPU:

  • python3 -m compileall -q tools/find_hash_lookup.py tests/test_find_hash_lookup.py -> passed.
  • python3 -I tests/test_find_hash_lookup.py -v -> Ran 7 tests in 0.236s -> OK.

Looks good to merge.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 30d8235 merged with main 14b2e44 on Linux/Python 3.12.16: pass

Commands run (merged worktree, -I isolated mode):

  • python -I tests/test_find_hash_lookup.py -> Ran 7 tests, OK
  • Negative control: git checkout upstream/main -- tools/find_hash_lookup.py, rerun -> FAILED (failures=3): e.g. --json naming the scanned image exits 0 on main (0 != 2) instead of refusing, and the folder and missing-folder cases fail too. Restored with git checkout HEAD -- tools/find_hash_lookup.py.

Diff read: main() refuses a --json that is a directory or the same path as the scanned image (via report_paths.same_report_path) before scanning, and turns an OSError on writing the report into ERROR: cannot write <path>: <reason> with exit 2; the sibling import is made with an explicit sys.path.insert because -I omits the script directory. The first new test also checks that the image bytes are unchanged.

@shayderrr shayderrr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

tools/find_hash_lookup.py inserts its script directory on sys.path for -I-mode sibling imports, refuses --json that aliases the scanned image or a folder, and wraps report writing in OSError handling with exit 2. Tests cover all three failure cases.

Why merge

Without these guards the tool could truncate its input image, fail on a directory path, or emit a raw traceback when its report folder is missing. The checks turn those into clear, safe errors.

Verdict: Approve

@shayderrr

Copy link
Copy Markdown
Collaborator

Summary

tools/find_hash_lookup.py inserts its script directory on sys.path for -I-mode sibling imports, refuses --json that aliases the scanned image or a folder, and wraps report writing in OSError handling with exit 2. Tests cover all three failure cases.

Why merge

Without these guards the tool could truncate its input image, fail on a directory path, or emit a raw traceback when its report folder is missing. The checks turn those into clear, safe errors.

Verdict: Approve

@airp1ane

Copy link
Copy Markdown
Contributor

Tested 30d8235 on Windows 11, Python 3.15.2, NVIDIA RTX 4060 Ti (driver 621.24; CPU-only checks, no game files, no GPU/llvm-mc/MSVC used): pass.

Ran the test files this PR adds or changes, from a clean checkout of the PR head: test_find_hash_lookup.py - all exit 0.

AI-assisted (Claude Code).

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.

6 participants