bug: Unreachable JSON extension and format validation in validate_input_path

Open Beginner friendly
#5,351 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

@0Cymantek0 is already working on this.

Since Oct 11, 2026.

  • #5353 by @0Cymantek0 — open
  • #5358 by @deepak0x — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
cli

Research direction

Start in src/scancode/cli.py at validate_input_path, around lines 194-203, where the unconditional raise makes the JSON extension and format checks unreachable. Read the from_json branch and the is_file condition, then run the reproduction with png.png to see the generic error. Done when --from-json on a non-.json file raises not a JSON file with a .json extension early, and the regression test added to tests/scancode/test_cli.py passes.

Written by the indexing model from the issue text.

Description

Description

Lines 198–203 in validate_input_path are unreachable dead code. They are placed after an unconditional raise on line 197, inside the same if from_json and not is_file(...) block. Python never executes statements after an unconditional raise in the same block.

Buggy code (permalink):

if from_json and not is_file(location=inp, follow_symlinks=True):
    # extra JSON validation
    raise click.BadParameter(f"JSON input: {inp!r} is not a file")
    if not inp.lower().endswith(".json"):                          # unreachable
        raise click.BadParameter(f"JSON input: {inp!r} is not a JSON file with a .json extension")
    with open(inp) as js:                                          # unreachable
        start = js.read(100).strip()
    if not start.startswith("{"):                                   # unreachable
        raise click.BadParameter(f"JSON input: {inp!r} is not a well formed JSON file")

There is a second structural problem: even if the raise were absent, the extension and format checks are inside the if from_json and not is_file(...) block, so they would only run when the input is not a file — the opposite of the intended condition. They should run when the input is a file.

Observed behavior: Running --from-json with a real file whose name does not end in .json (e.g. png.png) produces no early validation error. The scan proceeds and fails later with a generic Input JSON scan file(s) is not valid JSON message instead of the intended not a JSON file with a .json extension error.

Expected behavior: validate_input_path raises a click.BadParameter with message not a JSON file with a .json extension before the scan begins.

How To Reproduce
  1. Confirm the dead code structurally:
python3 -c "
src = open('src/scancode/cli.py').read()
lines = src.splitlines()
for i, l in enumerate(lines[194:204], start=195):
    print(i, repr(l))
"

Lines 198–203 are all indented inside the if from_json and not is_file(...) block and appear after the raise on line 197.

  1. Run a scan with --from-json against a real non-JSON file:
scancode --from-json tests/scancode/data/various-inputs/png.png --json-pp -

The command does not produce not a JSON file with a .json extension. It instead emits Input JSON scan file(s) is not valid JSON, confirming the early check never executes.

Reproducing test

The following test currently fails on the unfixed code and is the concrete proof the bug is real. It is not a fix — it is a regression test that documents the broken behavior:

def test_scan_validates_json_extension_for_from_json_input():
    test_file = test_env.get_test_loc('various-inputs/png.png')
    result = run_scan_click(
        [
            '--from-json',
            test_file,
            '--json-pp',
            '-',
        ],
        expected_rc=2,
        retry=False,
    )
    # FAILS on buggy code: output contains 'Input JSON scan file(s) is not valid JSON'
    # PASSES on fixed code: output contains the early validation error
    assert 'not a JSON file with a .json extension' in result.output

Add to tests/scancode/test_cli.py.

System configuration
  • OS: Linux (Ubuntu)
  • scancode-toolkit version: commit 5e8448ecf2397c6eb7e7eb50326c8c880d9b598b
  • Installation method: source
Dominant language
Python
Stars
2.7k
Forks
871
Avg merge
1d 10h
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from aboutcode-org/scancode-toolkit

All issues in aboutcode-org/scancode-toolkit

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.