Skip to content

Commit 9b9d33f

Browse files
yonatangrossclaude
andcommitted
fix(supply-chain): require relative edit distance for SC6 typosquat detection
Absolute edit-distance <= 2 produces false positives on short, legitimate package names: "task" is a real package and is edit-distance 2 from "flask", yet was flagged as a possible typosquat (one of the cases reported in #103). Add a relative-distance guard (dist/shorter_len <= 1/3) so short names need an all-but-one-character match while longer names may still differ by two characters. Existing behaviour is preserved: - "reqeusts" -> "requests" (len 8, dist 2) still flagged - "expreess" -> "express" (dist 1) still flagged - "task" -> "flask" (len 4, dist 2) no longer flagged Adds a regression test; all existing SC4/SC5/SC6 unit tests still pass and ruff is clean. Refs #103 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Signed-off-by: Yonatan Gross <[email protected]>
1 parent a5092dd commit 9b9d33f

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,19 @@ def _is_typosquat(pkg_name: str, popular: set[str], max_distance: int = 2) -> st
294294
if len(normalized) < 3 or len(pop_norm) < 3:
295295
continue
296296
dist = _edit_distance(normalized, pop_norm)
297-
if 0 < dist <= max_distance:
298-
return popular_name
297+
if not 0 < dist <= max_distance:
298+
continue
299+
# Relative-distance guard: a genuine typosquat perturbs only a small
300+
# fraction of the name. Short, legitimate-but-distinct names collide
301+
# under an absolute distance of 2 (e.g. "task" is edit-distance 2 from
302+
# "flask" yet is a real package) and are not typosquats. Require
303+
# dist/len <= 1/3, so short names need an all-but-one-character match
304+
# while longer names may still differ by two (e.g. "reqeusts" vs
305+
# "requests").
306+
shorter = min(len(normalized), len(pop_norm))
307+
if dist * 3 > shorter:
308+
continue
309+
return popular_name
299310
return None
300311

301312

‎tests/unit/test_patterns_new.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,14 @@ def test_is_typosquat_exact_match_returns_none(self) -> None:
10121012
def test_is_typosquat_too_distant_returns_none(self) -> None:
10131013
assert sc_mod._is_typosquat("completely_different", {"requests"}) is None
10141014

1015+
def test_is_typosquat_short_distinct_name_not_flagged(self) -> None:
1016+
# Regression: "task" is a real package and is edit-distance 2 from
1017+
# "flask", but distance 2 on a 4-char name is not a typosquat. Short
1018+
# names must clear the relative-distance guard, not just absolute <=2.
1019+
assert sc_mod._is_typosquat("task", {"flask"}) is None
1020+
# Longer names may still differ by two characters and be flagged.
1021+
assert sc_mod._is_typosquat("reqeusts", {"requests"}) == "requests"
1022+
10151023
@pytest.mark.parametrize(
10161024
"a,b,expected",
10171025
[

0 commit comments

Comments
 (0)