Skip to content

Commit 8397824

Browse files
committed
Fix input validation for directories and handle unreachable functions in JSON checks
Signed-off-by: Aayush Kumar <[email protected]>
1 parent b0aff52 commit 8397824

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/scancode/cli.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,26 +181,28 @@ def validate_input_path(ctx, param, value):
181181
Validate a ``value`` list of inputs path strings
182182
"""
183183
options = ctx.params
184-
from_json = options.get("--from-json", False)
184+
from_json = options.get("from_json", False)
185185
for inp in value:
186186
if not (is_file(location=inp, follow_symlinks=True) or is_dir(location=inp, follow_symlinks=True)):
187187
raise click.BadParameter(f"input: {inp!r} is not a regular file or a directory")
188188

189189
if not is_readable(location=inp):
190190
raise click.BadParameter(f"input: {inp!r} is not readable")
191191

192-
if from_json and not is_file(location=inp, follow_symlinks=True):
193-
# extra JSON validation
194-
raise click.BadParameter(f"JSON input: {inp!r} is not a file")
192+
if from_json:
193+
if is_dir(location=inp, follow_symlinks=True):
194+
raise click.BadParameter("Error: Invalid value: Input JSON scan file(s) is not valid JSON")
195+
195196
if not inp.lower().endswith(".json"):
196-
raise click.BadParameter(f"JSON input: {inp!r} is not a JSON file with a .json extension")
197-
with open(inp) as js:
198-
start = js.read(100).strip()
199-
if not start.startswith("{"):
200-
raise click.BadParameter(f"JSON input: {inp!r} is not a well formed JSON file")
197+
raise click.BadParameter("Error: Invalid value: Input JSON scan file(s) is not valid JSON")
201198

202-
return value
199+
try:
200+
with open(inp, 'r', encoding='utf-8') as f:
201+
json.load(f) # Try to parse the file as JSON
202+
except (json.JSONDecodeError, UnicodeDecodeError):
203+
raise click.BadParameter("Error: Invalid value: Input JSON scan file(s) is not valid JSON")
203204

205+
return value
204206

205207
@click.command(name='scancode',
206208
epilog=epilog_text,

0 commit comments

Comments
 (0)