modelbom 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- modelbom/__init__.py +9 -0
- modelbom/__main__.py +5 -0
- modelbom/artifacts.py +696 -0
- modelbom/attack_paths.py +202 -0
- modelbom/cli.py +202 -0
- modelbom/code_rules.py +642 -0
- modelbom/deps.py +461 -0
- modelbom/discovery.py +126 -0
- modelbom/graph.py +257 -0
- modelbom/models.py +201 -0
- modelbom/pickle_scan.py +417 -0
- modelbom/py.typed +0 -0
- modelbom/report/__init__.py +16 -0
- modelbom/report/cyclonedx.py +219 -0
- modelbom/report/jsonfmt.py +78 -0
- modelbom/report/sarif.py +110 -0
- modelbom/report/text.py +60 -0
- modelbom/rules.py +350 -0
- modelbom/scanner.py +77 -0
- modelbom-0.1.0.dist-info/METADATA +265 -0
- modelbom-0.1.0.dist-info/RECORD +25 -0
- modelbom-0.1.0.dist-info/WHEEL +4 -0
- modelbom-0.1.0.dist-info/entry_points.txt +2 -0
- modelbom-0.1.0.dist-info/licenses/LICENSE +202 -0
- modelbom-0.1.0.dist-info/licenses/NOTICE +14 -0
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""Machine-readable JSON report (``schema_version`` 1)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from dataclasses import asdict
|
|
7
|
+
from datetime import UTC, datetime
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from .. import __version__
|
|
11
|
+
from ..models import Finding, ScanResult
|
|
12
|
+
from ..rules import get_rule
|
|
13
|
+
|
|
14
|
+
SCHEMA_VERSION = 1
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def finding_to_dict(f: Finding) -> dict[str, Any]:
|
|
18
|
+
rule = get_rule(f.rule_id)
|
|
19
|
+
return {
|
|
20
|
+
"rule_id": f.rule_id,
|
|
21
|
+
"title": rule.title,
|
|
22
|
+
"severity": f.severity.label,
|
|
23
|
+
"path": f.path,
|
|
24
|
+
"line": f.line,
|
|
25
|
+
"location": f.location,
|
|
26
|
+
"message": f.message,
|
|
27
|
+
"remediation": rule.remediation,
|
|
28
|
+
"references": rule.references(),
|
|
29
|
+
"fingerprint": f.fingerprint,
|
|
30
|
+
"suppressed": f.suppressed,
|
|
31
|
+
"details": f.details,
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def to_dict(result: ScanResult) -> dict[str, Any]:
|
|
36
|
+
suppressed = sum(1 for f in result.findings if f.suppressed)
|
|
37
|
+
max_sev = result.max_severity()
|
|
38
|
+
return {
|
|
39
|
+
"tool": {"name": "modelbom", "version": __version__},
|
|
40
|
+
"schema_version": SCHEMA_VERSION,
|
|
41
|
+
"root": result.root,
|
|
42
|
+
"generated_at": datetime.now(UTC).isoformat(timespec="seconds").replace("+00:00", "Z"),
|
|
43
|
+
"online": result.online,
|
|
44
|
+
"summary": {
|
|
45
|
+
"files_scanned": result.files_scanned,
|
|
46
|
+
"code_files": len(result.code_files),
|
|
47
|
+
"artifacts": len(result.artifacts),
|
|
48
|
+
"model_references": len(result.model_refs),
|
|
49
|
+
"dependencies": len(result.dependencies),
|
|
50
|
+
"findings": result.counts(),
|
|
51
|
+
"suppressed": suppressed,
|
|
52
|
+
"attack_paths": len(result.attack_paths),
|
|
53
|
+
"max_severity": max_sev.label if max_sev is not None else None,
|
|
54
|
+
},
|
|
55
|
+
"findings": [finding_to_dict(f) for f in result.findings],
|
|
56
|
+
"artifacts": [asdict(a) for a in result.artifacts],
|
|
57
|
+
"model_references": [
|
|
58
|
+
{**asdict(r), "ref": r.ref, "pinned": r.pinned} for r in result.model_refs
|
|
59
|
+
],
|
|
60
|
+
"dependencies": [{**asdict(d), "purl": d.purl} for d in result.dependencies],
|
|
61
|
+
"attack_paths": [
|
|
62
|
+
{
|
|
63
|
+
"id": p.id,
|
|
64
|
+
"kind": p.kind,
|
|
65
|
+
"title": p.title,
|
|
66
|
+
"severity": p.severity.label,
|
|
67
|
+
"precondition": p.precondition,
|
|
68
|
+
"steps": [asdict(s) for s in p.steps],
|
|
69
|
+
"impact": p.impact,
|
|
70
|
+
"nodes": p.nodes,
|
|
71
|
+
}
|
|
72
|
+
for p in result.attack_paths
|
|
73
|
+
],
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def render_json(result: ScanResult) -> str:
|
|
78
|
+
return json.dumps(to_dict(result), indent=2, sort_keys=False, default=str) + "\n"
|
modelbom/report/sarif.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"""SARIF 2.1.0 output for GitHub code scanning and other SARIF consumers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from .. import __version__
|
|
9
|
+
from ..models import ScanResult, Severity
|
|
10
|
+
from ..rules import all_rules
|
|
11
|
+
|
|
12
|
+
SARIF_SCHEMA = "https://json.schemastore.org/sarif-2.1.0.json"
|
|
13
|
+
INFORMATION_URI = "https://github.com/rajyavardhan01/modelbom"
|
|
14
|
+
|
|
15
|
+
_LEVEL = {
|
|
16
|
+
Severity.CRITICAL: "error",
|
|
17
|
+
Severity.HIGH: "error",
|
|
18
|
+
Severity.MEDIUM: "warning",
|
|
19
|
+
Severity.LOW: "note",
|
|
20
|
+
Severity.INFO: "note",
|
|
21
|
+
}
|
|
22
|
+
_SECURITY_SEVERITY = {
|
|
23
|
+
Severity.CRITICAL: "9.5",
|
|
24
|
+
Severity.HIGH: "8.0",
|
|
25
|
+
Severity.MEDIUM: "5.5",
|
|
26
|
+
Severity.LOW: "3.0",
|
|
27
|
+
Severity.INFO: "0.0",
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _rule_descriptor(rule: Any) -> dict[str, Any]:
|
|
32
|
+
refs = rule.references()
|
|
33
|
+
tags = ["security", "ai-supply-chain", rule.category]
|
|
34
|
+
tags += refs["owasp_llm"] + refs["owasp_agentic"] + refs["mitre_atlas"]
|
|
35
|
+
tags += [c.replace("CWE-", "external/cwe/cwe-") for c in refs["cwe"]]
|
|
36
|
+
return {
|
|
37
|
+
"id": rule.id,
|
|
38
|
+
"name": rule.name,
|
|
39
|
+
"shortDescription": {"text": rule.title},
|
|
40
|
+
"fullDescription": {"text": rule.description},
|
|
41
|
+
"help": {
|
|
42
|
+
"text": f"{rule.description}\n\nRemediation: {rule.remediation}",
|
|
43
|
+
"markdown": f"{rule.description}\n\n**Remediation:** {rule.remediation}",
|
|
44
|
+
},
|
|
45
|
+
"helpUri": rule.help_uri,
|
|
46
|
+
"defaultConfiguration": {"level": _LEVEL[rule.severity]},
|
|
47
|
+
"properties": {
|
|
48
|
+
"tags": tags,
|
|
49
|
+
"precision": "high" if rule.category != "dependency" else "very-high",
|
|
50
|
+
"problem.severity": "error" if rule.severity >= Severity.HIGH else "warning",
|
|
51
|
+
"security-severity": _SECURITY_SEVERITY[rule.severity],
|
|
52
|
+
},
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def to_sarif(result: ScanResult) -> dict[str, Any]:
|
|
57
|
+
rules = [r for r in all_rules() if r.id != "MB206"]
|
|
58
|
+
index = {r.id: i for i, r in enumerate(rules)}
|
|
59
|
+
results: list[dict[str, Any]] = []
|
|
60
|
+
for f in result.findings:
|
|
61
|
+
if f.rule_id not in index:
|
|
62
|
+
continue
|
|
63
|
+
location: dict[str, Any] = {
|
|
64
|
+
"physicalLocation": {
|
|
65
|
+
"artifactLocation": {"uri": f.path, "uriBaseId": "%SRCROOT%"},
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if f.line:
|
|
69
|
+
location["physicalLocation"]["region"] = {"startLine": f.line}
|
|
70
|
+
entry: dict[str, Any] = {
|
|
71
|
+
"ruleId": f.rule_id,
|
|
72
|
+
"ruleIndex": index[f.rule_id],
|
|
73
|
+
"level": _LEVEL[f.severity],
|
|
74
|
+
"message": {"text": f.message},
|
|
75
|
+
"locations": [location],
|
|
76
|
+
"partialFingerprints": {"modelbom/v1": f.fingerprint},
|
|
77
|
+
"properties": {
|
|
78
|
+
"severity": f.severity.label,
|
|
79
|
+
"security-severity": _SECURITY_SEVERITY[f.severity],
|
|
80
|
+
},
|
|
81
|
+
}
|
|
82
|
+
if f.details.get("cell") is not None:
|
|
83
|
+
entry["properties"]["notebookCell"] = f.details["cell"]
|
|
84
|
+
entry["properties"]["notebookLine"] = f.details.get("cell_line")
|
|
85
|
+
if f.suppressed:
|
|
86
|
+
entry["suppressions"] = [{"kind": "inSource", "justification": "modelbom: ignore"}]
|
|
87
|
+
results.append(entry)
|
|
88
|
+
return {
|
|
89
|
+
"$schema": SARIF_SCHEMA,
|
|
90
|
+
"version": "2.1.0",
|
|
91
|
+
"runs": [
|
|
92
|
+
{
|
|
93
|
+
"tool": {
|
|
94
|
+
"driver": {
|
|
95
|
+
"name": "modelbom",
|
|
96
|
+
"version": __version__,
|
|
97
|
+
"semanticVersion": __version__,
|
|
98
|
+
"informationUri": INFORMATION_URI,
|
|
99
|
+
"rules": [_rule_descriptor(r) for r in rules],
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
"results": results,
|
|
103
|
+
"columnKind": "unicodeCodePoints",
|
|
104
|
+
}
|
|
105
|
+
],
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def render_sarif(result: ScanResult) -> str:
|
|
110
|
+
return json.dumps(to_sarif(result), indent=2) + "\n"
|
modelbom/report/text.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"""Human-readable terminal report."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .. import __version__
|
|
6
|
+
from ..models import ScanResult, Severity
|
|
7
|
+
from ..rules import get_rule
|
|
8
|
+
|
|
9
|
+
_COLORS = {
|
|
10
|
+
Severity.CRITICAL: "\033[1;31m",
|
|
11
|
+
Severity.HIGH: "\033[31m",
|
|
12
|
+
Severity.MEDIUM: "\033[33m",
|
|
13
|
+
Severity.LOW: "\033[36m",
|
|
14
|
+
Severity.INFO: "\033[2m",
|
|
15
|
+
}
|
|
16
|
+
_RESET = "\033[0m"
|
|
17
|
+
_BOLD = "\033[1m"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def render_text(result: ScanResult, *, color: bool = False, verbose: bool = False) -> str:
|
|
21
|
+
def paint(text: str, code: str) -> str:
|
|
22
|
+
return f"{code}{text}{_RESET}" if color else text
|
|
23
|
+
|
|
24
|
+
lines: list[str] = []
|
|
25
|
+
lines.append(paint(f"modelbom {__version__}", _BOLD) + f" · scanned {result.root}")
|
|
26
|
+
lines.append(
|
|
27
|
+
f" {result.files_scanned} files · {len(result.code_files)} code files · "
|
|
28
|
+
f"{len(result.artifacts)} artifacts · {len(result.model_refs)} hub references · "
|
|
29
|
+
f"{len(result.dependencies)} dependencies"
|
|
30
|
+
)
|
|
31
|
+
shown = [f for f in result.active() if verbose or f.rule_id != "MB206"]
|
|
32
|
+
for sev in sorted(Severity, reverse=True):
|
|
33
|
+
group = [f for f in shown if f.severity == sev]
|
|
34
|
+
if not group:
|
|
35
|
+
continue
|
|
36
|
+
lines.append("")
|
|
37
|
+
lines.append(paint(sev.label.upper(), _COLORS[sev]))
|
|
38
|
+
for f in group:
|
|
39
|
+
rule = get_rule(f.rule_id)
|
|
40
|
+
lines.append(f" {paint(f.rule_id, _COLORS[sev])} {f.location}")
|
|
41
|
+
lines.append(f" {f.message}")
|
|
42
|
+
if sev > Severity.INFO:
|
|
43
|
+
lines.append(f" fix: {rule.remediation}")
|
|
44
|
+
if result.attack_paths:
|
|
45
|
+
lines.append("")
|
|
46
|
+
lines.append(paint("ATTACK PATHS", _BOLD))
|
|
47
|
+
for p in result.attack_paths:
|
|
48
|
+
lines.append(f" {paint(p.id, _COLORS[p.severity])} [{p.severity.label}] {p.title}")
|
|
49
|
+
lines.append(f" precondition: {p.precondition}")
|
|
50
|
+
for i, step in enumerate(p.steps, start=1):
|
|
51
|
+
lines.append(f" {i}. {step.rule_id} {step.location}: {step.description}")
|
|
52
|
+
lines.append(f" impact: {p.impact}")
|
|
53
|
+
counts = result.counts()
|
|
54
|
+
suppressed = sum(1 for f in result.findings if f.suppressed)
|
|
55
|
+
parts = [f"{n} {label}" for label, n in counts.items() if n]
|
|
56
|
+
summary = " · ".join(parts) if parts else "no findings"
|
|
57
|
+
extra = f" ({suppressed} suppressed)" if suppressed else ""
|
|
58
|
+
lines.append("")
|
|
59
|
+
lines.append(paint("Summary: ", _BOLD) + summary + f" · {len(result.attack_paths)} attack paths{extra}")
|
|
60
|
+
return "\n".join(lines) + "\n"
|
modelbom/rules.py
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
"""Rule catalog. Every finding produced by modelbom references one of these rules.
|
|
2
|
+
|
|
3
|
+
References use public identifiers only: OWASP Top 10 for LLM Applications 2025 (``LLMxx:2025``),
|
|
4
|
+
OWASP Top 10 for Agentic Applications 2026 (``ASIxx:2026``), MITRE ATLAS technique IDs and CWE IDs.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import re
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
|
|
12
|
+
from .models import Severity
|
|
13
|
+
|
|
14
|
+
DOCS_URL = "https://github.com/rajyavardhan01/modelbom/blob/main/docs/rules.md"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass(frozen=True)
|
|
18
|
+
class Rule:
|
|
19
|
+
id: str
|
|
20
|
+
title: str
|
|
21
|
+
severity: Severity
|
|
22
|
+
category: str # "code" | "artifact" | "dependency"
|
|
23
|
+
description: str
|
|
24
|
+
remediation: str
|
|
25
|
+
owasp_llm: tuple[str, ...] = ()
|
|
26
|
+
owasp_agentic: tuple[str, ...] = ()
|
|
27
|
+
atlas: tuple[str, ...] = ()
|
|
28
|
+
cwe: tuple[str, ...] = ()
|
|
29
|
+
|
|
30
|
+
@property
|
|
31
|
+
def help_uri(self) -> str:
|
|
32
|
+
return f"{DOCS_URL}#{self.id.lower()}"
|
|
33
|
+
|
|
34
|
+
@property
|
|
35
|
+
def name(self) -> str:
|
|
36
|
+
"""CamelCase name, as expected by SARIF ``reportingDescriptor.name``."""
|
|
37
|
+
return "".join(w[:1].upper() + w[1:] for w in re.split(r"[^A-Za-z0-9]+", self.title) if w)
|
|
38
|
+
|
|
39
|
+
def references(self) -> dict[str, list[str]]:
|
|
40
|
+
return {
|
|
41
|
+
"owasp_llm": list(self.owasp_llm),
|
|
42
|
+
"owasp_agentic": list(self.owasp_agentic),
|
|
43
|
+
"mitre_atlas": list(self.atlas),
|
|
44
|
+
"cwe": list(self.cwe),
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
_S = Severity
|
|
49
|
+
|
|
50
|
+
_RULES: list[Rule] = [
|
|
51
|
+
Rule(
|
|
52
|
+
"MB100",
|
|
53
|
+
"Source file could not be analyzed",
|
|
54
|
+
_S.INFO,
|
|
55
|
+
"code",
|
|
56
|
+
"The Python file or notebook could not be parsed (syntax error, unsupported encoding or "
|
|
57
|
+
"size limit), so code rules were not applied to it.",
|
|
58
|
+
"Fix the syntax error, or exclude the file with --exclude or .modelbomignore.",
|
|
59
|
+
),
|
|
60
|
+
Rule(
|
|
61
|
+
"MB101",
|
|
62
|
+
"Remote code execution enabled at model load",
|
|
63
|
+
_S.HIGH,
|
|
64
|
+
"code",
|
|
65
|
+
"trust_remote_code=True lets the model or dataset repository ship Python code that runs "
|
|
66
|
+
"inside your process when it is loaded. Anyone who can change that repository (a "
|
|
67
|
+
"compromised maintainer account, a look-alike name, a transferred namespace) can execute "
|
|
68
|
+
"code on every machine that loads it.",
|
|
69
|
+
"Remove trust_remote_code=True where the architecture is supported natively. If custom "
|
|
70
|
+
"code is required, pin revision= to a reviewed commit SHA and audit or vendor the code.",
|
|
71
|
+
owasp_llm=("LLM03:2025",),
|
|
72
|
+
owasp_agentic=("ASI04:2026", "ASI05:2026"),
|
|
73
|
+
atlas=("AML.T0010.003", "AML.T0010.001"),
|
|
74
|
+
cwe=("CWE-829", "CWE-94"),
|
|
75
|
+
),
|
|
76
|
+
Rule(
|
|
77
|
+
"MB102",
|
|
78
|
+
"Hub model or dataset without a pinned revision",
|
|
79
|
+
_S.MEDIUM,
|
|
80
|
+
"code",
|
|
81
|
+
"A hub reference without revision= (or pinned to a branch) resolves to whatever the "
|
|
82
|
+
"default branch contains at load time. An upstream change, malicious or accidental, "
|
|
83
|
+
"silently swaps the weights, tokenizer, custom code or training data your system uses.",
|
|
84
|
+
'Pass revision="<commit-sha>" (a full 40-character commit hash, not a branch or tag) '
|
|
85
|
+
"and update it deliberately.",
|
|
86
|
+
owasp_llm=("LLM03:2025", "LLM04:2025"),
|
|
87
|
+
owasp_agentic=("ASI04:2026",),
|
|
88
|
+
atlas=("AML.T0010.003", "AML.T0010.002"),
|
|
89
|
+
cwe=("CWE-1357", "CWE-494"),
|
|
90
|
+
),
|
|
91
|
+
Rule(
|
|
92
|
+
"MB103",
|
|
93
|
+
"torch.load without weights_only=True",
|
|
94
|
+
_S.HIGH,
|
|
95
|
+
"code",
|
|
96
|
+
"torch.load is built on pickle. Without weights_only=True a crafted checkpoint can execute "
|
|
97
|
+
"arbitrary code. PyTorch 2.6 changed the default to True, but code that runs on older "
|
|
98
|
+
"versions, or passes weights_only=False, remains exposed.",
|
|
99
|
+
"Call torch.load(path, weights_only=True) explicitly and prefer safetensors for weights.",
|
|
100
|
+
owasp_llm=("LLM03:2025",),
|
|
101
|
+
atlas=("AML.T0011.000",),
|
|
102
|
+
cwe=("CWE-502",),
|
|
103
|
+
),
|
|
104
|
+
Rule(
|
|
105
|
+
"MB104",
|
|
106
|
+
"Unsafe deserialization API",
|
|
107
|
+
_S.HIGH,
|
|
108
|
+
"code",
|
|
109
|
+
"pickle, dill, cloudpickle, joblib, shelve, pandas.read_pickle and "
|
|
110
|
+
"numpy.load(allow_pickle=True) execute code embedded in the data they load. Loading a "
|
|
111
|
+
"model or dataset file from an untrusted or unverified source with these APIs is "
|
|
112
|
+
"equivalent to running a program from that source.",
|
|
113
|
+
"Use a data-only format (safetensors, JSON, Parquet, ONNX), or verify the file's SHA-256 "
|
|
114
|
+
"against a trusted value before loading it.",
|
|
115
|
+
owasp_llm=("LLM03:2025", "LLM04:2025"),
|
|
116
|
+
atlas=("AML.T0011.000", "AML.T0148"),
|
|
117
|
+
cwe=("CWE-502",),
|
|
118
|
+
),
|
|
119
|
+
Rule(
|
|
120
|
+
"MB105",
|
|
121
|
+
"Keras unsafe deserialization enabled",
|
|
122
|
+
_S.HIGH,
|
|
123
|
+
"code",
|
|
124
|
+
"load_model(..., safe_mode=False) or keras.config.enable_unsafe_deserialization() allows "
|
|
125
|
+
"Lambda layers and other serialized Python code inside a Keras model file to run when the "
|
|
126
|
+
"model is loaded.",
|
|
127
|
+
"Keep safe_mode=True (the default) and replace Lambda layers with registered custom "
|
|
128
|
+
"layer classes.",
|
|
129
|
+
owasp_llm=("LLM03:2025",),
|
|
130
|
+
atlas=("AML.T0011.000", "AML.T0148"),
|
|
131
|
+
cwe=("CWE-502", "CWE-94"),
|
|
132
|
+
),
|
|
133
|
+
Rule(
|
|
134
|
+
"MB106",
|
|
135
|
+
"Unsafe YAML loading",
|
|
136
|
+
_S.MEDIUM,
|
|
137
|
+
"code",
|
|
138
|
+
"yaml.load without a safe loader and yaml.unsafe_load can construct arbitrary Python "
|
|
139
|
+
"objects from configuration files, model cards or experiment definitions. "
|
|
140
|
+
"yaml.full_load is less permissive but still broader than needed (reported as low).",
|
|
141
|
+
"Use yaml.safe_load, or pass Loader=yaml.SafeLoader.",
|
|
142
|
+
owasp_llm=("LLM03:2025",),
|
|
143
|
+
atlas=("AML.T0011.000",),
|
|
144
|
+
cwe=("CWE-502",),
|
|
145
|
+
),
|
|
146
|
+
Rule(
|
|
147
|
+
"MB107",
|
|
148
|
+
"Model or data downloaded without integrity verification",
|
|
149
|
+
_S.MEDIUM,
|
|
150
|
+
"code",
|
|
151
|
+
"Downloading model or data files over the network without checking a known hash lets a "
|
|
152
|
+
"compromised host, mirror or network path substitute the file before it is loaded or "
|
|
153
|
+
"used for training.",
|
|
154
|
+
"Verify a pinned SHA-256 after download (for example hash_prefix= or check_hash=True for "
|
|
155
|
+
"torch.hub helpers, or hashlib), or fetch from a hub by commit SHA.",
|
|
156
|
+
owasp_llm=("LLM03:2025", "LLM04:2025"),
|
|
157
|
+
atlas=("AML.T0010.002", "AML.T0010.003"),
|
|
158
|
+
cwe=("CWE-494", "CWE-353"),
|
|
159
|
+
),
|
|
160
|
+
Rule(
|
|
161
|
+
"MB108",
|
|
162
|
+
"torch.hub.load executes repository code",
|
|
163
|
+
_S.HIGH,
|
|
164
|
+
"code",
|
|
165
|
+
"torch.hub.load downloads a GitHub repository and runs its hubconf.py. Without a pinned "
|
|
166
|
+
"ref, every run executes whatever the default branch contains at that moment. A pinned "
|
|
167
|
+
"tag is reported as medium because tags can be moved.",
|
|
168
|
+
"Pin the ref ('owner/repo:<commit-sha>'), review hubconf.py, or vendor the model "
|
|
169
|
+
"definition and load weights only.",
|
|
170
|
+
owasp_llm=("LLM03:2025",),
|
|
171
|
+
owasp_agentic=("ASI04:2026",),
|
|
172
|
+
atlas=("AML.T0010.001", "AML.T0010.003"),
|
|
173
|
+
cwe=("CWE-829", "CWE-494"),
|
|
174
|
+
),
|
|
175
|
+
Rule(
|
|
176
|
+
"MB109",
|
|
177
|
+
"Dangerous deserialization enabled for a vector store",
|
|
178
|
+
_S.HIGH,
|
|
179
|
+
"code",
|
|
180
|
+
"allow_dangerous_deserialization=True loads a pickled vector index (for example "
|
|
181
|
+
"FAISS.load_local). Whoever can write that index file, or poison the pipeline that "
|
|
182
|
+
"builds it, can execute code in the retrieval service.",
|
|
183
|
+
"Only load indexes your own pipeline produced and stored in a write-protected location, "
|
|
184
|
+
"verify their hash, or use a vector store that persists data without pickle.",
|
|
185
|
+
owasp_llm=("LLM03:2025", "LLM08:2025"),
|
|
186
|
+
owasp_agentic=("ASI06:2026",),
|
|
187
|
+
atlas=("AML.T0011.000", "AML.T0070"),
|
|
188
|
+
cwe=("CWE-502",),
|
|
189
|
+
),
|
|
190
|
+
Rule(
|
|
191
|
+
"MB110",
|
|
192
|
+
"safetensors explicitly disabled",
|
|
193
|
+
_S.MEDIUM,
|
|
194
|
+
"code",
|
|
195
|
+
"use_safetensors=False forces pickle-based weight files even when a safetensors version "
|
|
196
|
+
"of the model is available.",
|
|
197
|
+
"Remove use_safetensors=False (or set it to True) and publish safetensors weights.",
|
|
198
|
+
owasp_llm=("LLM03:2025",),
|
|
199
|
+
atlas=("AML.T0011.000",),
|
|
200
|
+
cwe=("CWE-502",),
|
|
201
|
+
),
|
|
202
|
+
Rule(
|
|
203
|
+
"MB201",
|
|
204
|
+
"Pickle artifact imports dangerous callables",
|
|
205
|
+
_S.CRITICAL,
|
|
206
|
+
"artifact",
|
|
207
|
+
"The pickle stream in this artifact references callables that can execute commands, "
|
|
208
|
+
"spawn processes, open network connections, evaluate code or load further pickles. "
|
|
209
|
+
"Loading the file runs them.",
|
|
210
|
+
"Do not load this file. Obtain the artifact from a trusted source or regenerate it, "
|
|
211
|
+
"and publish weights as safetensors.",
|
|
212
|
+
owasp_llm=("LLM03:2025", "LLM04:2025"),
|
|
213
|
+
owasp_agentic=("ASI04:2026",),
|
|
214
|
+
atlas=("AML.T0148", "AML.T0011.000", "AML.T0115.001"),
|
|
215
|
+
cwe=("CWE-502",),
|
|
216
|
+
),
|
|
217
|
+
Rule(
|
|
218
|
+
"MB202",
|
|
219
|
+
"Pickle artifact imports non-allowlisted callables",
|
|
220
|
+
_S.MEDIUM,
|
|
221
|
+
"artifact",
|
|
222
|
+
"The pickle references callables outside modelbom's allowlist of common tensor, "
|
|
223
|
+
"container and framework types (or a global that cannot be resolved statically). They "
|
|
224
|
+
"are not necessarily malicious, but each one is code that runs at load time.",
|
|
225
|
+
"Review the listed imports. If they are expected (your own classes), suppress with a "
|
|
226
|
+
"'MB202:<glob>' line in .modelbomignore; otherwise treat the file as untrusted.",
|
|
227
|
+
owasp_llm=("LLM03:2025",),
|
|
228
|
+
atlas=("AML.T0011.000",),
|
|
229
|
+
cwe=("CWE-502",),
|
|
230
|
+
),
|
|
231
|
+
Rule(
|
|
232
|
+
"MB203",
|
|
233
|
+
"Pickle-based model format",
|
|
234
|
+
_S.LOW,
|
|
235
|
+
"artifact",
|
|
236
|
+
"This artifact uses a pickle-based format. No dangerous import was detected, but pickle "
|
|
237
|
+
"formats can carry code and static analysis cannot prove a pickle safe.",
|
|
238
|
+
"Convert weights to safetensors (or ONNX/GGUF for inference) and load them with a "
|
|
239
|
+
"data-only loader.",
|
|
240
|
+
owasp_llm=("LLM03:2025",),
|
|
241
|
+
atlas=("AML.T0011.000",),
|
|
242
|
+
cwe=("CWE-502",),
|
|
243
|
+
),
|
|
244
|
+
Rule(
|
|
245
|
+
"MB204",
|
|
246
|
+
"Keras model contains a Lambda layer",
|
|
247
|
+
_S.HIGH,
|
|
248
|
+
"artifact",
|
|
249
|
+
"Lambda layers serialize Python bytecode inside the model file. Loading with unsafe "
|
|
250
|
+
"deserialization enabled, or with older Keras versions, executes it.",
|
|
251
|
+
"Replace Lambda layers with registered custom layer classes and keep safe_mode=True.",
|
|
252
|
+
owasp_llm=("LLM03:2025",),
|
|
253
|
+
atlas=("AML.T0148", "AML.T0018.001"),
|
|
254
|
+
cwe=("CWE-94", "CWE-502"),
|
|
255
|
+
),
|
|
256
|
+
Rule(
|
|
257
|
+
"MB205",
|
|
258
|
+
"Artifact could not be fully analyzed",
|
|
259
|
+
_S.LOW,
|
|
260
|
+
"artifact",
|
|
261
|
+
"The file is truncated, corrupt, compressed with an unsupported codec, or larger than the "
|
|
262
|
+
"analysis limit, so modelbom could not inspect all of it.",
|
|
263
|
+
"Inspect the artifact manually or raise --max-bytes, and treat it as untrusted until "
|
|
264
|
+
"verified.",
|
|
265
|
+
),
|
|
266
|
+
Rule(
|
|
267
|
+
"MB206",
|
|
268
|
+
"Artifact inventoried",
|
|
269
|
+
_S.INFO,
|
|
270
|
+
"artifact",
|
|
271
|
+
"Inventory record: the artifact was hashed and added to the AI-BOM, and static inspection "
|
|
272
|
+
"found no issue. Shown in JSON output and with --verbose; never affects --fail-on.",
|
|
273
|
+
"None required.",
|
|
274
|
+
),
|
|
275
|
+
Rule(
|
|
276
|
+
"MB207",
|
|
277
|
+
"Chat template contains code-execution primitives",
|
|
278
|
+
_S.HIGH,
|
|
279
|
+
"artifact",
|
|
280
|
+
"Chat templates are Jinja programs rendered at inference time. This template references "
|
|
281
|
+
"Python internals (dunder attributes, the attr filter, escaped underscores or process "
|
|
282
|
+
"APIs) of the kind used in template-engine sandbox escapes.",
|
|
283
|
+
"Replace the template with a reviewed one from a trusted source, and render templates "
|
|
284
|
+
"in a sandboxed environment with a current template engine.",
|
|
285
|
+
owasp_llm=("LLM03:2025",),
|
|
286
|
+
owasp_agentic=("ASI05:2026",),
|
|
287
|
+
atlas=("AML.T0148", "AML.T0011.000"),
|
|
288
|
+
cwe=("CWE-1336",),
|
|
289
|
+
),
|
|
290
|
+
Rule(
|
|
291
|
+
"MB208",
|
|
292
|
+
"Model directory requires custom code",
|
|
293
|
+
_S.MEDIUM,
|
|
294
|
+
"artifact",
|
|
295
|
+
"This config.json declares auto_map, so loading the model requires executing Python "
|
|
296
|
+
"modules shipped with the model (trust_remote_code).",
|
|
297
|
+
"Review the referenced modules and pin the revision, or use a natively supported "
|
|
298
|
+
"architecture.",
|
|
299
|
+
owasp_llm=("LLM03:2025",),
|
|
300
|
+
owasp_agentic=("ASI04:2026",),
|
|
301
|
+
atlas=("AML.T0010.003",),
|
|
302
|
+
cwe=("CWE-829",),
|
|
303
|
+
),
|
|
304
|
+
Rule(
|
|
305
|
+
"MB301",
|
|
306
|
+
"AI/ML dependency not pinned",
|
|
307
|
+
_S.LOW,
|
|
308
|
+
"dependency",
|
|
309
|
+
"The dependency is declared with a range or without a version, and no lock file or "
|
|
310
|
+
"other manifest pins it. A new release, or a compromised one, is picked up "
|
|
311
|
+
"automatically.",
|
|
312
|
+
"Pin exact versions (==) or commit a lock file (uv.lock, poetry.lock, pdm.lock, "
|
|
313
|
+
"Pipfile.lock) and install with hashes.",
|
|
314
|
+
owasp_llm=("LLM03:2025",),
|
|
315
|
+
owasp_agentic=("ASI04:2026",),
|
|
316
|
+
atlas=("AML.T0010.001",),
|
|
317
|
+
cwe=("CWE-1357",),
|
|
318
|
+
),
|
|
319
|
+
Rule(
|
|
320
|
+
"MB302",
|
|
321
|
+
"Known vulnerability in a pinned dependency",
|
|
322
|
+
_S.HIGH,
|
|
323
|
+
"dependency",
|
|
324
|
+
"OSV.dev reports a published vulnerability for this exact package version "
|
|
325
|
+
"(only checked with --online).",
|
|
326
|
+
"Upgrade to a fixed version listed in the advisory.",
|
|
327
|
+
owasp_llm=("LLM03:2025",),
|
|
328
|
+
atlas=("AML.T0010.001",),
|
|
329
|
+
cwe=("CWE-1395",),
|
|
330
|
+
),
|
|
331
|
+
Rule(
|
|
332
|
+
"MB303",
|
|
333
|
+
"Online vulnerability lookup failed",
|
|
334
|
+
_S.INFO,
|
|
335
|
+
"dependency",
|
|
336
|
+
"--online was requested but OSV.dev could not be queried, so known vulnerabilities were "
|
|
337
|
+
"not checked.",
|
|
338
|
+
"Check network access to api.osv.dev and re-run.",
|
|
339
|
+
),
|
|
340
|
+
]
|
|
341
|
+
|
|
342
|
+
RULES: dict[str, Rule] = {r.id: r for r in _RULES}
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def get_rule(rule_id: str) -> Rule:
|
|
346
|
+
return RULES[rule_id]
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
def all_rules() -> list[Rule]:
|
|
350
|
+
return list(_RULES)
|
modelbom/scanner.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"""Scan orchestration: discovery -> code / artifact / dependency analysis -> attack paths."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Sequence
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from .artifacts import DEFAULT_MAX_BYTES, ArtifactOptions, inspect_artifact, is_candidate
|
|
9
|
+
from .attack_paths import derive_attack_paths
|
|
10
|
+
from .code_rules import scan_notebook_file, scan_python_file
|
|
11
|
+
from .deps import HttpJson, collect_dependencies, is_manifest, osv_findings
|
|
12
|
+
from .discovery import IgnoreSpec, iter_files, load_ignore
|
|
13
|
+
from .graph import Link, build_links
|
|
14
|
+
from .models import ScanResult
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def scan(
|
|
18
|
+
target: str | Path,
|
|
19
|
+
*,
|
|
20
|
+
excludes: Sequence[str] = (),
|
|
21
|
+
online: bool = False,
|
|
22
|
+
hash_files: bool = True,
|
|
23
|
+
max_bytes: int = DEFAULT_MAX_BYTES,
|
|
24
|
+
http_json: HttpJson | None = None,
|
|
25
|
+
) -> ScanResult:
|
|
26
|
+
"""Scan a directory (or a single file) and return a :class:`ScanResult`."""
|
|
27
|
+
target_path = Path(target)
|
|
28
|
+
if not target_path.exists():
|
|
29
|
+
raise FileNotFoundError(f"no such file or directory: {target}")
|
|
30
|
+
if target_path.is_file():
|
|
31
|
+
root = target_path.parent
|
|
32
|
+
ignore = IgnoreSpec()
|
|
33
|
+
files: Sequence[str] = [target_path.name]
|
|
34
|
+
else:
|
|
35
|
+
root = target_path
|
|
36
|
+
ignore = load_ignore(root, excludes)
|
|
37
|
+
files = list(iter_files(root, ignore))
|
|
38
|
+
|
|
39
|
+
result = ScanResult(root=str(target), online=online)
|
|
40
|
+
opts = ArtifactOptions(hash_files=hash_files, max_bytes=max_bytes)
|
|
41
|
+
manifests: list[str] = []
|
|
42
|
+
for rel in files:
|
|
43
|
+
result.files_scanned += 1
|
|
44
|
+
path = root / rel
|
|
45
|
+
lower = rel.lower()
|
|
46
|
+
if lower.endswith(".py"):
|
|
47
|
+
code = scan_python_file(path, rel, root)
|
|
48
|
+
elif lower.endswith(".ipynb"):
|
|
49
|
+
code = scan_notebook_file(path, rel, root)
|
|
50
|
+
else:
|
|
51
|
+
code = None
|
|
52
|
+
if code is not None:
|
|
53
|
+
result.code_files.append(code.code_file)
|
|
54
|
+
result.findings.extend(code.findings)
|
|
55
|
+
result.model_refs.extend(code.model_refs)
|
|
56
|
+
if is_manifest(rel):
|
|
57
|
+
manifests.append(rel)
|
|
58
|
+
if is_candidate(rel):
|
|
59
|
+
artifact, findings = inspect_artifact(path, rel, opts)
|
|
60
|
+
if artifact is not None:
|
|
61
|
+
result.artifacts.append(artifact)
|
|
62
|
+
result.findings.extend(findings)
|
|
63
|
+
|
|
64
|
+
deps, dep_findings = collect_dependencies(root, manifests)
|
|
65
|
+
result.dependencies = deps
|
|
66
|
+
result.findings.extend(dep_findings)
|
|
67
|
+
if online:
|
|
68
|
+
result.findings.extend(osv_findings(deps, http_json=http_json))
|
|
69
|
+
|
|
70
|
+
for f in result.findings:
|
|
71
|
+
if not f.suppressed and ignore.suppresses(f.rule_id, f.path):
|
|
72
|
+
f.suppressed = True
|
|
73
|
+
result.findings.sort(key=lambda f: (-int(f.severity), f.path, f.line or 0, f.rule_id))
|
|
74
|
+
|
|
75
|
+
links: list[Link] = build_links(result)
|
|
76
|
+
result.attack_paths = derive_attack_paths(result, links)
|
|
77
|
+
return result
|