Skip to content

Fix dead and inert JSON input validation in validate_input_path - #5358

Open
deepak0x wants to merge 1 commit into
aboutcode-org:developfrom
deepak0x:fix/5351-from-json-input-validation
Open

deepak0x wants to merge 1 commit into
aboutcode-org:developfrom
deepak0x:fix/5351-from-json-input-validation

Conversation

@deepak0x

Copy link
Copy Markdown

Fixes #5351

Description

validate_input_path was meant to add extra validation for --from-json inputs, but the whole JSON branch never did anything useful. This fixes the four reasons it was dead or inert:

  1. options.get("--from-json") looked up the CLI flag string, but Click keys ctx.params by the Python parameter name from_json, so the lookup always returned False and the entire block was skipped. Now it reads options.get("from_json", False).
  2. --from-json was not eager, so it had not been parsed yet when the input argument callback ran. Even with the key fixed, the value would still read False. The option is now is_eager=True.
  3. The extension and header checks sat after an unconditional raise (so they were unreachable) and were nested under not is_file(...) (so once reachable they would only run for non-files, the opposite of the intent). They now run when the input is a file.
  4. The header check opened the file in text mode, which raises UnicodeDecodeError on binary content (for example a .json-named PNG) instead of a clean message. It now reads bytes and compares against b"{".

Tests

Added three tests in tests/scancode/test_cli.py covering a non-.json extension, malformed JSON content, and binary content (which previously crashed). The existing --from-json round-trip test still passes, confirming the is_eager and key changes do not affect valid JSON loading.

Credit to @0Cymantek0, who independently identified the wrong-key and wrong-nesting causes and the binary-read issue on the linked issue.

Tasks

  • Reviewed contribution guidelines
  • PR is descriptively titled and links the original issue above
  • Tests pass locally
  • Commits are in a uniquely-named feature branch and have no merge conflicts
  • Updated documentation pages (if applicable)
  • Updated CHANGELOG.rst (if applicable)

The --from-json branch of validate_input_path never ran its extension
and format checks:

- options.get("--from-json") looked up the CLI flag string, but Click
  keys ctx.params by the Python name from_json, so it was always False.
- --from-json was not eager, so it had not been parsed yet when the
  input argument callback ran; it is now is_eager so the value is set.
- The extension and header checks sat after an unconditional raise and
  under 'not is_file()', so they were unreachable and, once reachable,
  guarded by the wrong condition. They now run when the input is a file.
- The header check read the file in text mode, raising UnicodeDecodeError
  on binary content; it now reads bytes.

Add tests covering non-.json extensions, malformed JSON, and binary
content.

Signed-off-by: Deepak Bhagat <deepak988088@gmail.com>

This branch has not been deployed

No deployments
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.

bug: Unreachable JSON extension and format validation in validate_input_path

1 participant