Repository navigation
[ADS-1632] Handle blank and BOM-prefixed Agent Guard JSON configs - #574
Conversation
Treat blank JSON files as empty configs and preserve a UTF-8 BOM when rewriting an existing file. Related: ADS-1632 Co-authored-by: FORGE <forge@snyk.io>
PR Summary by QodoHandle blank and BOM-prefixed Agent Guard JSON configs
AI Description
Merge Risk
|
Code Review by Qodo🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)
Great, no issues found!Qodo reviewed your code and found no material issues that require reviewTip of the day💡 Did you know, you can commit Qodo's fix in one click with committable suggestions (GitHub & GitLab) |
phsommer
left a comment
There was a problem hiding this comment.
Looks good to me. Please have a look at the small suggestion below.
| with_bom = False | ||
| with contextlib.suppress(FileNotFoundError), path.open("rb") as f: | ||
| with_bom = f.read(len(codecs.BOM_UTF8)) == codecs.BOM_UTF8 |
There was a problem hiding this comment.
Can we use the same mechanism to check if the file has a BOM?
I suggest to use a a small helper _has_utf8_bom(path) which is then shared by both _write_json() and _write_json_if_changed
Related: ADS-1632 Co-authored-by: FORGE <forge@snyk.io>
What this does
Agent Guard currently fails to parse blank JSON configs and valid JSON configs that start with a UTF-8 byte-order mark (BOM).
The shared JSON reader now treats empty, whitespace-only, and BOM-only files as
{}and accepts BOM-prefixed JSON. Install, status, and uninstall all use that reader.When Guard rewrites a config, it preserves the file’s BOM convention and uses the existing backup and atomic write flow. An identical reinstall leaves the file untouched.
Notes for the reviewer
The agreed scope for ADS-1632 is blank-file and BOM handling in
agent-scan. A revert restores the previous config behavior. The change is limited to Guard’s local JSON config files.Evidence
SNYK_TOKENwas unset. The API test module passed on rerun with a dummy token.More information
ADS-1632
Note
Low Risk
Localized to Guard's local JSON read/write/backup paths for agent hook configs; behavior is additive preservation with regression tests and no auth or network changes.
Overview
Fixes Agent Guard failing on JSON configs that are empty, whitespace-only, or UTF-8 BOM-prefixed (common on Windows editors).
_read_json_or_emptynow reads withutf-8-sigand treats blank or whitespace-only files as{}, so install/status/uninstall no longer throw parse errors on those inputs.Writes preserve encoding conventions:
_has_utf8_bomdetects an existing BOM;_dump_config_jsoncan re-emit\ufeff; backup, change-detection, uninstall, and reinstall paths pass that through so BOM-only configs, backups, and no-op reinstalls stay byte-stable while non-ASCII user content (e.g."café") still round-trips.Unit tests in
TestJsonConfigEncodingcover blank/BOM-only files, BOM + user content through install/uninstall, backups, and unchanged reinstalls.Reviewed by Cursor Bugbot for commit c02896b. Bugbot is set up for automated code reviews on this repo. Configure here.