Skip to content

Commit a28503d

Browse files
committed
Catch invalid license_expression in parse_license_expression
Signed-off-by: Chin Yeung Li <[email protected]>
1 parent 0d2f566 commit a28503d

File tree

4 files changed

+40
-21
lines changed

4 files changed

+40
-21
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Changelog
66

77
* Add character support (at most 2 characters) for `attribute` field
88
* Strip empty newline characters when loading an inventory
9+
* Catch invalid license_expression
910

1011

1112
2023-09-25

src/attributecode/attrib.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ def generate_sctk_input(abouts, min_license_score, license_dict):
225225
updated_lic_name = []
226226
updated_lic_score = []
227227
for index, lic in enumerate(updated_dict):
228-
_sp_char, lic_keys = parse_license_expression(lic)
228+
_sp_char, lic_keys, _invalid_lic_exp = parse_license_expression(
229+
lic)
229230
score, name = updated_dict[lic]
230231
if score >= min_license_score:
231232
for lic_key in lic_keys:
@@ -306,12 +307,17 @@ def generate_and_save(abouts, is_about_input, license_dict, output_location, sca
306307
for about in abouts:
307308
if not about.license_expression.value:
308309
continue
309-
special_char_in_expression, lic_list = parse_license_expression(
310+
special_char_in_expression, lic_list, invalid_lic_exp = parse_license_expression(
310311
about.license_expression.value)
311-
if special_char_in_expression:
312-
msg = (u"The following character(s) cannot be in the license_expression: " +
313-
str(special_char_in_expression))
312+
if special_char_in_expression or invalid_lic_exp:
313+
if special_char_in_expression:
314+
msg = (u"The following character(s) cannot be in the license_expression: " +
315+
str(special_char_in_expression))
316+
else:
317+
msg = (u"This license_expression is invalid: " +
318+
str(invalid_lic_exp))
314319
errors.append(Error(ERROR, msg))
320+
315321
rendering_error, rendered = generate_from_file(
316322
abouts,
317323
is_about_input,

src/attributecode/model.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,7 @@ def load_dict(self, fields_dict, base_dir, scancode=False, from_attrib=False, ru
11941194
lic_key_exp_list = []
11951195
lic_score_list = []
11961196
for lic in lic_list:
1197-
_char, lic_keys = parse_license_expression(
1197+
_char, lic_keys, _invalid_lic_exp = parse_license_expression(
11981198
lic['lic_exp'])
11991199
lic_key_list.append(lic_keys)
12001200
# for lic_key in lic_keys:
@@ -1327,7 +1327,7 @@ def dumps(self, licenses_dict=None):
13271327
# and get the parsed license key
13281328
if 'license_expression' in data:
13291329
if not license_key and data['license_expression']:
1330-
_spec_char, lic_list = parse_license_expression(
1330+
_spec_char, lic_list, _invalid_lic_exp = parse_license_expression(
13311331
data['license_expression'])
13321332
license_key = lic_list
13331333

@@ -1501,11 +1501,11 @@ def dump_lic(self, location, license_dict):
15011501
os.makedirs(add_unc(parent))
15021502

15031503
if self.license_expression.present:
1504-
special_char_in_expression, lic_list = parse_license_expression(
1504+
special_char_in_expression, lic_list, invalid_lic_exp = parse_license_expression(
15051505
self.license_expression.value)
15061506
self.license_key.value = lic_list
15071507
self.license_key.present = True
1508-
if not special_char_in_expression:
1508+
if not special_char_in_expression and not invalid_lic_exp:
15091509
for lic_key in lic_list:
15101510
license_name = ''
15111511
license_filename = ''
@@ -1905,11 +1905,15 @@ def pre_process_and_fetch_license_dict(abouts, from_check=False, api_url=None, a
19051905

19061906
if not about.license_expression.value and about.spdx_license_expression.value:
19071907
lic_exp_value = ""
1908-
special_char_in_expression, lic_list = parse_license_expression(
1908+
special_char_in_expression, lic_list, invalid_lic_exp = parse_license_expression(
19091909
about.spdx_license_expression.value)
1910-
if special_char_in_expression:
1911-
msg = (about.about_file_path + u": The following character(s) cannot be in the spdx_license_expression: " +
1912-
str(special_char_in_expression))
1910+
if special_char_in_expression or invalid_lic_exp:
1911+
if special_char_in_expression:
1912+
msg = (about.about_file_path + u": The following character(s) cannot be in the spdx_license_expression: " +
1913+
str(special_char_in_expression))
1914+
else:
1915+
msg = (about.about_file_path + u": This spdx_license_expression is invalid: " +
1916+
str(invalid_lic_exp))
19131917
errors.append(Error(ERROR, msg))
19141918
else:
19151919
spdx_lic_exp_segment = about.spdx_license_expression.value.split()
@@ -1925,11 +1929,15 @@ def pre_process_and_fetch_license_dict(abouts, from_check=False, api_url=None, a
19251929
about.license_expression.present = True
19261930

19271931
if about.license_expression.value:
1928-
special_char_in_expression, lic_list = parse_license_expression(
1932+
special_char_in_expression, lic_list, invalid_lic_exp = parse_license_expression(
19291933
about.license_expression.value)
1930-
if special_char_in_expression:
1931-
msg = (about.about_file_path + u": The following character(s) cannot be in the license_expression: " +
1932-
str(special_char_in_expression))
1934+
if special_char_in_expression or invalid_lic_exp:
1935+
if special_char_in_expression:
1936+
msg = (about.about_file_path + u": The following character(s) cannot be in the license_expression: " +
1937+
str(special_char_in_expression))
1938+
else:
1939+
msg = (about.about_file_path + u": This license_expression is invalid: " +
1940+
str(invalid_lic_exp))
19331941
errors.append(Error(ERROR, msg))
19341942
else:
19351943
for lic_key in lic_list:
@@ -2027,11 +2035,15 @@ def convert_spdx_expression_to_lic_expression(spdx_key, spdx_lic_dict):
20272035
def parse_license_expression(lic_expression):
20282036
licensing = Licensing()
20292037
lic_list = []
2038+
invalid_lic_exp = ''
20302039
special_char = detect_special_char(lic_expression)
20312040
if not special_char:
20322041
# Parse the license expression and save it into a list
2033-
lic_list = licensing.license_keys(lic_expression)
2034-
return special_char, lic_list
2042+
try:
2043+
lic_list = licensing.license_keys(lic_expression)
2044+
except:
2045+
invalid_lic_exp = lic_expression
2046+
return special_char, lic_list, invalid_lic_exp
20352047

20362048

20372049
def detect_special_char(expression):

tests/test_model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,15 +1217,15 @@ def test_collect_inventory_does_not_raise_error_and_maintains_order_on_custom_fi
12171217
assert expected == [a.dumps() for a in abouts]
12181218

12191219
def test_parse_license_expression(self):
1220-
spec_char, returned_lic = model.parse_license_expression(
1220+
spec_char, returned_lic, _invalid_lic_exp = model.parse_license_expression(
12211221
'mit or apache-2.0')
12221222
expected_lic = ['mit', 'apache-2.0']
12231223
expected_spec_char = []
12241224
assert expected_lic == returned_lic
12251225
assert expected_spec_char == spec_char
12261226

12271227
def test_parse_license_expression_with_special_chara(self):
1228-
spec_char, returned_lic = model.parse_license_expression(
1228+
spec_char, returned_lic, _invalid_lic_exp = model.parse_license_expression(
12291229
'mit, apache-2.0')
12301230
expected_lic = []
12311231
expected_spec_char = [',']

0 commit comments

Comments
 (0)