bug: Unreachable JSON extension and format validation in validate_input_path
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.
Đá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
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
- 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.
- Run a scan with
--from-jsonagainst 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
- Có Dockerfile hoặc tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của aboutcode-org/scancode-toolkit
-
CycloneDX output: metadata.properties[].value emits JSON array instead of stringCó thể đã có người làm @0Cymantek0 đã nhận 18 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
aboutcode-org/scancode-toolkit#5349 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 3 ngày
-
Duplicate 'COPYLEFT' entries in legal file classification names in legal.py and classify.pyCó thể đã có người làm @Pradeepbeleri đã nhận 46 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
aboutcode-org/scancode-toolkit#5319 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 3 ngày
-
Duplicate autogen.sh entry in commoncode default ignoresCó thể đã có người làm @Daksha1611 đã nhận 48 ngày trước. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
aboutcode-org/scancode-toolkit#5316 ·
Maintainer thường phản hồi trong vòng 3 ngày
-
Duplicate LGPL key in rubygems LICENSES_MAPPING makes one mapping unreachableCó thể đã có người làm @Daksha1611 đã nhận 48 ngày trước. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
aboutcode-org/scancode-toolkit#5314 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 3 ngày
-
Duplicate keys in alpine DECLARED_TO_SPDX silently discard two license mappingsCó thể đã có người làm @Daksha1611 đã nhận 48 ngày trước. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100
aboutcode-org/scancode-toolkit#5312 ·
Maintainer thường phản hồi trong vòng 3 ngày
Tất cả issue của aboutcode-org/scancode-toolkit
Issue tương tự
-
bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 86/100
awslabs/visual-asset-management-system#937 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Admin member of a research group cannot see the HX2 allocation until they've been added to itĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
-
follow-up
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 83/100
Maintainer thường phản hồi trong vòng 3 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Maintainer thường phản hồi trong vòng 2 ngày