Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

bug: Unreachable JSON extension and format validation in validate_input_path

Đang mở Phù hợp với người mới
#5,351 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 3 ngày

@0Cymantek0 đang làm issue này rồi.

Từ ngày 11/10/2026.

  • #5353 của @0Cymantek0 — đang mở
  • #5358 của @deepak0x — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
82/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
cli

Hướng nghiên cứu

Bắt đầu từ src/scancode/cli.py tại validate_input_path, khoảng dòng 194-203, nơi raise vô điều kiện khiến các kiểm tra phần mở rộng JSON và định dạng không thể đạt tới. Đọc nhánh from_json và điều kiện is_file, sau đó chạy tái hiện với png.png để thấy lỗi chung chung. Hoàn thành khi --from-json trên tệp không có phần mở rộng .json báo not a JSON file with a .json extension ngay từ sớm và bài kiểm tra hồi quy thêm vào tests/scancode/test_cli.py vượt qua.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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
Ngôn ngữ chính
Python
Star
2.7k
Fork
831
Merge trung bình
3 ngày 4 giờ
Pull request đã merge (30 ngày)
7

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của aboutcode-org/scancode-toolkit

Tất cả issue của aboutcode-org/scancode-toolkit

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.