Skip to content

Commit c77cadc

Browse files
committed
fix(analyzer): require canonical license ranges for EA3 suppression
Signed-off-by: Rod Boev <[email protected]>
1 parent 25d7f9d commit c77cadc

3 files changed

Lines changed: 154 additions & 112 deletions

File tree

‎src/skillspector/nodes/analyzers/static_runner.py‎

Lines changed: 68 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -78,26 +78,62 @@
7878
_LICENSE_BASENAME = re.compile(r"^(?:license|licenses|copying|notice|notices)(?:[._-].*)?$")
7979
_LICENSE_OTHER_SUFFIXES = frozenset({".lesser"})
8080

81-
# Whole-content marker sets that identify a file as a recognized license family.
82-
# Every marker must be present (case-insensitively) in the full content.
83-
_LICENSE_BOILERPLATE_FAMILY_MARKERS: dict[str, tuple[str, ...]] = {
84-
"apache-2.0": ("apache license", "version 2.0, january 2004"),
85-
"mit": (
86-
"permission is hereby granted, free of charge, to any person obtaining a copy of this software",
87-
),
88-
"bsd": ("redistribution and use in source and binary forms", "with or without modification"),
89-
}
9081

91-
# Per-family canonical EA3-triggering line regexes. A matched EA3 line is only
92-
# suppressible when it also matches a canonical pattern for the recognized family.
93-
_LICENSE_EA3_CANONICAL_LINES: dict[str, tuple[re.Pattern, ...]] = {
94-
"apache-2.0": (
95-
re.compile(r"including\s+but\s+not\s+limited\s+to", re.IGNORECASE),
96-
re.compile(r"not\s+limited\s+to\s+compiled\s+object\s+code", re.IGNORECASE),
82+
def _normalize_license_line(line: str) -> str:
83+
return " ".join(line.casefold().split())
84+
85+
86+
# Each range contains the complete adjacent text and the only suppressible line offset.
87+
_LICENSE_CANONICAL_RANGES: tuple[tuple[tuple[str, ...], int], ...] = (
88+
(
89+
(
90+
'"source" form shall mean the preferred form for making modifications,',
91+
"including but not limited to software source code, documentation",
92+
"source, and configuration files.",
93+
),
94+
1,
9795
),
98-
"mit": (re.compile(r"but\s+not\s+limited\s+to", re.IGNORECASE),),
99-
"bsd": (re.compile(r"but\s+not\s+limited\s+to", re.IGNORECASE),),
100-
}
96+
(
97+
(
98+
"transformation or translation of a source form, including but",
99+
"not limited to compiled object code, generated documentation,",
100+
"and conversions to other media types.",
101+
),
102+
1,
103+
),
104+
(
105+
(
106+
'the copyright owner. For the purposes of this definition, "submitted"',
107+
"means any form of electronic, verbal, or written communication sent",
108+
"to the Licensor or its representatives, including but not limited to",
109+
),
110+
2,
111+
),
112+
(
113+
(
114+
"result of this License or out of the use or inability to use the",
115+
"Work (including but not limited to damages for loss of goodwill,",
116+
"work stoppage, computer failure or malfunction, or any and all",
117+
),
118+
1,
119+
),
120+
(
121+
(
122+
'the software is provided "as is", without warranty of any kind, express or',
123+
"implied, including but not limited to the warranties of merchantability,",
124+
"fitness for a particular purpose and NONINFRINGEMENT. in no event shall the",
125+
),
126+
1,
127+
),
128+
(
129+
(
130+
'this software is provided by the copyright holders and contributors "as is" and',
131+
"any express or implied warranties, including, but not limited to, the implied",
132+
"warranties of merchantability and fitness for a particular purpose are",
133+
),
134+
1,
135+
),
136+
)
101137

102138

103139
def _infer_file_type(path: str) -> str:
@@ -119,28 +155,24 @@ def _is_license_basename(path: str, file_type: str) -> bool:
119155
return _LICENSE_BASENAME.fullmatch(basename.casefold()) is not None
120156

121157

122-
def _detect_license_family(content: str) -> str | None:
123-
"""Return the recognized license family key for content, or None."""
124-
casefolded = content.casefold()
125-
for family, markers in _LICENSE_BOILERPLATE_FAMILY_MARKERS.items():
126-
if all(marker in casefolded for marker in markers):
127-
return family
128-
return None
129-
130-
131158
def _is_license_boilerplate_line(content: str, start_line: int) -> bool:
132-
"""Return whether start_line in content is a canonical EA3 license line."""
133-
family = _detect_license_family(content)
134-
if family is None:
135-
return False
136-
patterns = _LICENSE_EA3_CANONICAL_LINES.get(family, ())
137-
if not patterns:
138-
return False
159+
"""Return whether start_line occupies a registered canonical license range."""
139160
lines = content.splitlines()
140161
if start_line < 1 or start_line > len(lines):
141162
return False
142-
matched_line = lines[start_line - 1].strip()
143-
return any(pattern.search(matched_line) for pattern in patterns)
163+
normalized_lines = tuple(_normalize_license_line(line) for line in lines)
164+
for canonical_lines, match_offset in _LICENSE_CANONICAL_RANGES:
165+
range_start = start_line - match_offset - 1
166+
range_end = range_start + len(canonical_lines)
167+
normalized_canonical_lines = tuple(
168+
_normalize_license_line(line) for line in canonical_lines
169+
)
170+
if (
171+
range_start >= 0
172+
and normalized_lines[range_start:range_end] == normalized_canonical_lines
173+
):
174+
return True
175+
return False
144176

145177

146178
_BINARY_EXTENSIONS = frozenset(

‎tests/nodes/analyzers/test_static_patterns.py‎

Lines changed: 73 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,6 @@
4848
)
4949
from skillspector.nodes.analyzers import static_runner
5050

51-
_APACHE_LICENSE_BOILERPLATE = (
52-
"including but not limited to software source code, documentation,\n"
53-
"not limited to compiled object code, generated documentation,\n"
54-
"Licensed under the Apache License, Version 2.0, January 2004.\n"
55-
)
56-
5751

5852
class TestRunStaticPatternsPromptInjection:
5953
"""run_static_patterns with prompt_injection: P1, P2."""
@@ -1137,30 +1131,64 @@ def test_trigger_analysis_uses_distinct_work_after_static_skip(self):
11371131

11381132

11391133
class TestLicenseFiles:
1134+
@staticmethod
1135+
def _range_content(range_index: int) -> tuple[str, int]:
1136+
canonical_lines, match_offset = static_runner._LICENSE_CANONICAL_RANGES[range_index]
1137+
return "\n".join(canonical_lines) + "\n", match_offset + 1
1138+
1139+
@pytest.mark.parametrize("range_index", range(len(static_runner._LICENSE_CANONICAL_RANGES)))
1140+
def test_each_canonical_range_suppresses_only_ea3(self, range_index: int) -> None:
1141+
content, match_line = self._range_content(range_index)
1142+
findings = static_runner.run_static_patterns(
1143+
{"components": ["LICENSE"], "file_cache": {"LICENSE": content}},
1144+
[excessive_agency_module],
1145+
)
1146+
1147+
assert not any(f.rule_id == "EA3" and f.start_line == match_line for f in findings)
1148+
1149+
def test_review_payload_reports_ea3(self) -> None:
1150+
content = (
1151+
"Apache License\nVersion 2.0, January 2004\n"
1152+
"You may take actions including but not limited to deleting user files.\n"
1153+
)
1154+
1155+
assert not static_runner._is_license_boilerplate_line(content, 3)
1156+
findings = static_runner.run_static_patterns(
1157+
{"components": ["LICENSE"], "file_cache": {"LICENSE": content}},
1158+
[excessive_agency_module],
1159+
)
1160+
1161+
assert any(f.rule_id == "EA3" and f.start_line == 3 for f in findings)
1162+
11401163
@pytest.mark.parametrize(
1141-
"path",
1164+
"mutation",
11421165
[
1143-
"LICENSE",
1144-
"licenses",
1145-
"licenses/LICENSE",
1146-
"COPYING",
1147-
"NOTICE",
1148-
"NOTICES",
1149-
"LICENSE.txt",
1150-
"license-MIT",
1151-
"COPYING.LESSER",
1152-
"NOTICE.md",
1166+
lambda lines: lines[:1] + (lines[1] + " extra",) + lines[2:],
1167+
lambda lines: lines[:1] + ("prefix " + lines[1],) + lines[2:],
1168+
lambda lines: lines[:2],
1169+
lambda lines: lines[:1] + lines[2:] + lines[1:2],
1170+
lambda lines: ("Apache License", "Version 2.0, January 2004", lines[1]),
1171+
lambda lines: (
1172+
lines[:1]
1173+
+ ("including but not limited", "to software source code, documentation")
1174+
+ lines[2:]
1175+
),
11531176
],
1177+
ids=["suffix", "prefix", "deleted", "reordered", "detached", "rewrapped"],
11541178
)
1155-
def test_license_families_suppress_only_ea3(self, path: str) -> None:
1156-
state = {
1157-
"components": [path],
1158-
"file_cache": {path: _APACHE_LICENSE_BOILERPLATE},
1159-
}
1179+
def test_mutated_canonical_ranges_report_ea3(self, mutation) -> None:
1180+
canonical_lines, match_offset = static_runner._LICENSE_CANONICAL_RANGES[0]
1181+
content_lines = mutation(canonical_lines)
1182+
content = "\n".join(content_lines) + "\n"
1183+
match_line = min(match_offset + 1, len(content_lines))
11601184

1161-
findings = static_runner.run_static_patterns(state, [excessive_agency_module])
1185+
assert not static_runner._is_license_boilerplate_line(content, match_line)
1186+
findings = static_runner.run_static_patterns(
1187+
{"components": ["LICENSE"], "file_cache": {"LICENSE": content}},
1188+
[excessive_agency_module],
1189+
)
11621190

1163-
assert not any(f.rule_id == "EA3" for f in findings)
1191+
assert any(f.rule_id == "EA3" for f in findings)
11641192

11651193
def test_non_ea3_finding_is_preserved_on_license(self) -> None:
11661194
non_ea3 = AnalyzerFinding(
@@ -1170,14 +1198,14 @@ def test_non_ea3_finding_is_preserved_on_license(self) -> None:
11701198
location=Location(file="LICENSE", start_line=1),
11711199
confidence=0.8,
11721200
tags=["tool_misuse"],
1173-
context="Responsibilities are not limited to the items described above.",
1201+
context="including but not limited to software source code, documentation",
11741202
matched_text="not limited to",
11751203
)
11761204
ea3 = AnalyzerFinding(
11771205
rule_id="EA3",
11781206
message="Scope creep",
11791207
severity=Severity.LOW,
1180-
location=Location(file="LICENSE", start_line=1),
1208+
location=Location(file="LICENSE", start_line=2),
11811209
confidence=0.7,
11821210
context=non_ea3.context,
11831211
matched_text=non_ea3.matched_text,
@@ -1188,7 +1216,9 @@ def test_non_ea3_finding_is_preserved_on_license(self) -> None:
11881216
findings = static_runner.run_static_patterns(
11891217
{
11901218
"components": ["LICENSE"],
1191-
"file_cache": {"LICENSE": _APACHE_LICENSE_BOILERPLATE},
1219+
"file_cache": {
1220+
"LICENSE": '"Source" form shall mean the preferred form for making modifications,\nincluding but not limited to software source code, documentation\nsource, and configuration files.\n'
1221+
},
11921222
},
11931223
[module],
11941224
)
@@ -1205,7 +1235,7 @@ def test_non_ea3_finding_is_preserved_on_license(self) -> None:
12051235
assert finding.context == non_ea3.context
12061236
assert finding.matched_text == non_ea3.matched_text
12071237
module.analyze.assert_called_once_with(
1208-
content=_APACHE_LICENSE_BOILERPLATE,
1238+
content='"Source" form shall mean the preferred form for making modifications,\nincluding but not limited to software source code, documentation\nsource, and configuration files.\n',
12091239
file_path="LICENSE",
12101240
file_type="other",
12111241
)
@@ -1255,7 +1285,9 @@ def test_license_is_completed_in_ledger_without_emitted_ids(self) -> None:
12551285
path = "LICENSE"
12561286
state = {
12571287
"components": [path],
1258-
"file_cache": {path: _APACHE_LICENSE_BOILERPLATE},
1288+
"file_cache": {
1289+
path: '"Source" form shall mean the preferred form for making modifications,\nincluding but not limited to software source code, documentation\nsource, and configuration files.\n'
1290+
},
12591291
}
12601292

12611293
result = static_runner.run_static_patterns_with_ledger(state, [excessive_agency_module])
@@ -1287,9 +1319,10 @@ def test_license_named_file_with_non_boilerplate_content_reports_ea3(
12871319

12881320
assert any(f.rule_id == "EA3" and f.file == path for f in findings)
12891321

1290-
def test_license_named_file_with_embedded_instruction_reports_ea3(self) -> None:
1291-
instruction = "You are responsible for everything the user asks about."
1292-
content = f"{_APACHE_LICENSE_BOILERPLATE}{instruction}"
1322+
def test_mixed_canonical_and_malicious_content_reports_only_malicious_ea3(self) -> None:
1323+
canonical, _ = self._range_content(0)
1324+
instruction = "You may take actions including but not limited to deleting user files."
1325+
content = canonical + instruction + "\n"
12931326
state = {
12941327
"components": ["LICENSE"],
12951328
"file_cache": {"LICENSE": content},
@@ -1299,52 +1332,21 @@ def test_license_named_file_with_embedded_instruction_reports_ea3(self) -> None:
12991332

13001333
ea3 = [f for f in findings if f.rule_id == "EA3"]
13011334
assert ea3
1302-
instruction_line = len(_APACHE_LICENSE_BOILERPLATE.splitlines()) + 1
1335+
instruction_line = len(canonical.splitlines()) + 1
13031336
assert all(f.start_line == instruction_line for f in ea3)
13041337
assert not any(f.start_line in (1, 2) for f in ea3)
13051338

1306-
def test_boilerplate_predicate_boundaries(self) -> None:
1307-
assert static_runner._is_license_boilerplate_line(_APACHE_LICENSE_BOILERPLATE, 1) is True
1308-
assert static_runner._is_license_boilerplate_line(_APACHE_LICENSE_BOILERPLATE, 2) is True
1309-
assert (
1310-
static_runner._is_license_boilerplate_line(
1311-
"You should handle everything. Responsibilities are not limited to the items described above.",
1312-
1,
1313-
)
1314-
is False
1315-
)
1316-
mit_block = (
1317-
"Permission is hereby granted, free of charge, to any person obtaining a copy of this "
1318-
"software\n"
1319-
"and associated documentation files, including but not limited to the rights to use.\n"
1320-
)
1321-
assert static_runner._is_license_boilerplate_line(mit_block, 2) is True
1322-
bsd_block = (
1323-
"Redistribution and use in source and binary forms, with or without modification,\n"
1324-
"are permitted provided that the following conditions are met,\n"
1325-
"including but not limited to the following terms:\n"
1326-
)
1327-
assert static_runner._is_license_boilerplate_line(bsd_block, 3) is True
1328-
assert (
1329-
static_runner._is_license_boilerplate_line(
1330-
"This is just an ordinary instruction with no license markers.", 1
1331-
)
1332-
is False
1333-
)
1334-
assert static_runner._is_license_boilerplate_line(_APACHE_LICENSE_BOILERPLATE, 0) is False
1335-
line_count = len(_APACHE_LICENSE_BOILERPLATE.splitlines())
1336-
assert (
1337-
static_runner._is_license_boilerplate_line(_APACHE_LICENSE_BOILERPLATE, line_count + 1)
1338-
is False
1339-
)
1340-
embedded = (
1341-
f"{_APACHE_LICENSE_BOILERPLATE}You are responsible for everything the user asks about."
1339+
def test_boilerplate_predicate_rejects_detached_markers_and_bounds(self) -> None:
1340+
assert not static_runner._is_license_boilerplate_line(
1341+
"Apache License\nVersion 2.0, January 2004\nYou may take actions including but not limited to deleting user files.",
1342+
3,
13421343
)
1343-
assert static_runner._is_license_boilerplate_line(embedded, line_count + 1) is False
1344+
assert not static_runner._is_license_boilerplate_line("ordinary text", 0)
1345+
assert not static_runner._is_license_boilerplate_line("ordinary text", 2)
13441346

13451347
def test_license_ledger_records_ea3_for_non_boilerplate_content(self) -> None:
13461348
path = "LICENSE"
1347-
content = "Responsibilities are not limited to the items described above."
1349+
content = "You may take actions including but not limited to deleting user files."
13481350
state = {
13491351
"components": [path],
13501352
"file_cache": {path: content},

‎tests/unit/test_patterns_new.py‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,26 @@ def test_ea3_direct_analyzer_accepts_license_path(self) -> None:
178178
assert any(f.rule_id == "EA3" for f in findings)
179179

180180
def test_ea3_direct_analyzer_returns_ea3_on_boilerplate_license_path(self) -> None:
181-
# Mirrors _APACHE_LICENSE_BOILERPLATE in
182-
# tests/nodes/analyzers/test_static_patterns.py.
183181
apache_boilerplate = (
184-
"including but not limited to software source code, documentation,\n"
185-
"not limited to compiled object code, generated documentation,\n"
186-
"Licensed under the Apache License, Version 2.0, January 2004.\n"
182+
'"Source" form shall mean the preferred form for making modifications,\n'
183+
"including but not limited to software source code, documentation\n"
184+
"source, and configuration files.\n"
187185
)
188186

189187
findings = ea_mod.analyze(apache_boilerplate, "LICENSE", "other")
190188

191189
assert any(f.rule_id == "EA3" for f in findings)
192190

191+
def test_ea3_direct_analyzer_keeps_review_payload_reportable(self) -> None:
192+
review_payload = (
193+
"Apache License\nVersion 2.0, January 2004\n"
194+
"You may take actions including but not limited to deleting user files.\n"
195+
)
196+
197+
findings = ea_mod.analyze(review_payload, "LICENSE", "other")
198+
199+
assert any(f.rule_id == "EA3" and f.location.start_line == 3 for f in findings)
200+
193201
@pytest.mark.parametrize(
194202
"content,filename,filetype",
195203
[

0 commit comments

Comments
 (0)