Skip to content

Commit 23205db

Browse files
authored
fix(syft): skip malformed JSON files instead of aborting sync (#2713)
### Type of change - [x] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Refactoring (no functional changes) - [ ] Documentation update - [ ] Other (please describe): ### Summary A single corrupt or empty JSON file in the Syft results directory currently raises `JSONDecodeError` out of `sync_syft_from_dir`, aborting ingestion of every remaining file in the batch. This change mirrors the behavior already implemented for Trivy in [`cartography/intel/trivy/__init__.py`](https://github.com/cartography-cncf/cartography/blob/master/cartography/intel/trivy/__init__.py): catch `JSONDecodeError` per file, log it at error level, and continue with the next file so that one bad input does not poison the whole sync. ### Related issues or links - Fixes # ### How was this tested? - `make test_lint` passes locally. - Manually verified the symmetry against the existing Trivy `try/except json.JSONDecodeError` block — same control flow, same log level, same `continue` on failure. Logging follows the `%`-style formatting already used by the surrounding Syft logger calls. ### Checklist #### General - [x] I have read the [contributing guidelines](https://cartography-cncf.github.io/cartography/dev/developer-guide.html). - [x] The linter passes locally (`make lint`). - [ ] I have added/updated tests that prove my fix is effective or my feature works. #### Proof of functionality - [ ] Screenshot showing the graph before and after changes. - [ ] New or updated unit/integration tests. ### Notes for reviewers No new test added: the fix is a defensive `try/except` symmetric to the one already shipped for Trivy, which itself has no dedicated test for the `JSONDecodeError` path. Happy to add one if maintainers prefer — let me know. Signed-off-by: Jeremy Chapeau <jeremy@subimage.io>
1 parent 5118f60 commit 23205db

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

cartography/intel/syft/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,12 @@ def sync_syft_from_dir(
128128
logger.info("Processing %d local Syft result files", len(json_files))
129129

130130
for file_path in json_files:
131-
with open(file_path, encoding="utf-8") as f:
132-
syft_data = json.load(f)
131+
try:
132+
with open(file_path, encoding="utf-8") as f:
133+
syft_data = json.load(f)
134+
except json.JSONDecodeError as e:
135+
logger.error("Failed to read Syft data from %s: %s", file_path, e)
136+
continue
133137
sync_single_syft(
134138
neo4j_session,
135139
syft_data,

0 commit comments

Comments
 (0)