Skip to content

Commit 73a2a7b

Browse files
authored
Merge pull request #47 from CoreTrace/feat/advisory-feeds
feat: local advisory feeds and policies
2 parents 1f0abb4 + ff377d6 commit 73a2a7b

12 files changed

Lines changed: 815 additions & 12 deletions

File tree

‎README.md‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,25 @@ shipped `dependency/` plugins report a requirement that allows a vulnerable vers
4141
its line, and every call in the project to an API the advisory affects. When
4242
attacker-controlled data reaches such a call, the engine correlates the four facts into
4343
one critical `exploitable-vulnerability` finding, judged like any other flow. The shipped
44-
advisory database is a small offline sample; a live OSV feed is future work.
44+
advisory database is a small offline sample. A real feed stays offline too:
45+
`--import-advisories SRC OUT` converts a public OSV dump (a JSON file, a directory of
46+
them or a zip archive) into a local advisory file once, and a directory check reads
47+
`advisories.json` at the project root plus the files passed with `--advisories`, the
48+
local file winning over a plugin for the same advisory. OSV records name no affected
49+
APIs, so imported advisories feed the requirement checks and the SBOM; add
50+
`affected_symbols` by hand to a local entry and it feeds reachability and correlation
51+
too. A `coretrace-policy.toml` at the root, or the file passed with `--policy`, denies
52+
packages (`denied-dependency`), requires pins (`unpinned-dependency`) and lists accepted
53+
advisories whose findings are dropped:
54+
55+
```toml
56+
[dependencies]
57+
deny = ["pycrypto"]
58+
require_pinned = true
59+
60+
[advisories]
61+
ignore = ["CVE-2020-1747"]
62+
```
4563

4664
`--sbom PATH` writes a CycloneDX 1.5 bill of materials of the dependency graph, one
4765
component per requirement with its package URL, and the advisories affecting them as
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Requirements the project's policy denies or leaves unpinned."""
2+
3+
from __future__ import annotations
4+
5+
from collections.abc import Sequence
6+
from typing import ClassVar
7+
8+
from coretrace_python.analysis import AnyAnalysis
9+
from coretrace_python.dependency import DependencyAnalysis
10+
from coretrace_python.findings import Confidence, Finding, Severity
11+
from coretrace_python.plugins import ProjectContext, ProjectPlugin
12+
13+
14+
class DependencyPolicyPlugin(ProjectPlugin):
15+
name: ClassVar[str] = "dependency-policy"
16+
requires: ClassVar[frozenset[AnyAnalysis]] = frozenset({DependencyAnalysis})
17+
18+
def analyze_project(self, ctx: ProjectContext) -> Sequence[Finding]:
19+
findings: list[Finding] = []
20+
for requirement in ctx.dependencies.requirements:
21+
metadata = {"package": requirement.name, "specifier": requirement.specifier}
22+
if ctx.policy.denies(requirement.name):
23+
findings.append(
24+
Finding(
25+
"denied-dependency",
26+
f"{requirement.name} is denied by the dependency policy",
27+
Severity.HIGH,
28+
Confidence.HIGH,
29+
requirement.span,
30+
None,
31+
metadata,
32+
)
33+
)
34+
elif ctx.policy.require_pinned and requirement.pinned is None:
35+
findings.append(
36+
Finding(
37+
"unpinned-dependency",
38+
f"{requirement.name}{requirement.specifier} is not pinned to one version",
39+
Severity.LOW,
40+
Confidence.HIGH,
41+
requirement.span,
42+
None,
43+
metadata,
44+
)
45+
)
46+
return findings
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name = "dependency-policy"
2+
version = "1.0.0"
3+
plugin_api = ">=1,<2"
4+
requires = ["dependency.graph"]
5+
provides = ["policy.dependencies"]
6+
7+
[entrypoint]
8+
module = "dependency_policy"
9+
class = "DependencyPolicyPlugin"

‎src/coretrace_python/cli.py‎

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
import argparse
44
import sys
5+
import zipfile
56
from pathlib import Path
67

78
from coretrace_python import __version__, engine
89
from coretrace_python.analysis import AnalysisError
910
from coretrace_python.cache import ProjectCache
1011
from coretrace_python.cfg import CFGError
11-
from coretrace_python.dependency import render_sbom
12+
from coretrace_python.dependency import dump_advisories, import_osv, read_osv, render_sbom
1213
from coretrace_python.frontend import HIRBuildError, ParseError, build_hir
1314
from coretrace_python.ir.lowering import LoweringError, lower_module
1415
from coretrace_python.ir.printer import format_module
@@ -42,7 +43,9 @@ def build_parser() -> argparse.ArgumentParser:
4243
prog=engine.TOOL_NAME,
4344
description="Analyze Python source with CoreTrace.",
4445
)
45-
parser.add_argument("path", type=Path, help="Python source file, or a directory for --check")
46+
parser.add_argument(
47+
"path", type=Path, nargs="?", help="Python source file, or a directory for --check"
48+
)
4649
parser.add_argument(
4750
"--emit-ir",
4851
action="store_true",
@@ -94,14 +97,51 @@ def build_parser() -> argparse.ArgumentParser:
9497
metavar="PATH",
9598
help="with --check on a directory, write a CycloneDX bill of materials to PATH",
9699
)
100+
parser.add_argument(
101+
"--advisories",
102+
action="append",
103+
type=Path,
104+
default=[],
105+
metavar="FILE",
106+
help="with --check on a directory, a local advisory file to read in addition to "
107+
"advisories.json at the root (repeatable)",
108+
)
109+
parser.add_argument(
110+
"--policy",
111+
type=Path,
112+
default=None,
113+
metavar="FILE",
114+
help="with --check on a directory, the dependency policy to apply instead of "
115+
"coretrace-policy.toml at the root",
116+
)
117+
parser.add_argument(
118+
"--import-advisories",
119+
nargs=2,
120+
type=Path,
121+
default=None,
122+
metavar=("SRC", "OUT"),
123+
help="convert an OSV dump (a JSON file, a directory of them or a zip archive) "
124+
"into the local advisory file OUT, then exit",
125+
)
97126
return parser
98127

99128

100129
def main(argv: list[str] | None = None) -> int:
101130
args = build_parser().parse_args(argv)
131+
if args.import_advisories is not None:
132+
source, out = args.import_advisories
133+
try:
134+
out.write_text(dump_advisories(import_osv(read_osv(source))), encoding="utf-8")
135+
except (OSError, ValueError, zipfile.BadZipFile) as error:
136+
print(f"error: {error}", file=sys.stderr)
137+
return EXIT_ERROR
138+
return EXIT_CLEAN
102139
if not (args.emit_ir or args.check):
103140
print("error: no action selected; pass --emit-ir or --check", file=sys.stderr)
104141
return EXIT_ERROR
142+
if args.path is None:
143+
print("error: a path is required", file=sys.stderr)
144+
return EXIT_ERROR
105145
if args.format is not None and not args.check:
106146
print("error: --format only applies to --check", file=sys.stderr)
107147
return EXIT_ERROR
@@ -117,6 +157,12 @@ def main(argv: list[str] | None = None) -> int:
117157
if args.sbom is not None and not (args.check and args.path.is_dir()):
118158
print("error: --sbom only applies to --check on a directory", file=sys.stderr)
119159
return EXIT_ERROR
160+
if args.advisories and not (args.check and args.path.is_dir()):
161+
print("error: --advisories only applies to --check on a directory", file=sys.stderr)
162+
return EXIT_ERROR
163+
if args.policy is not None and not (args.check and args.path.is_dir()):
164+
print("error: --policy only applies to --check on a directory", file=sys.stderr)
165+
return EXIT_ERROR
120166

121167
if args.path.is_dir() and args.emit_ir:
122168
print(f"error: {args.path} is a directory; --emit-ir needs a file", file=sys.stderr)
@@ -130,7 +176,12 @@ def main(argv: list[str] | None = None) -> int:
130176
if args.path.is_dir():
131177
cache = None if args.cache is None else ProjectCache(args.cache)
132178
analysis = engine.analyze_project(
133-
args.path, args.plugins, cache=cache, jobs=args.jobs or 1
179+
args.path,
180+
args.plugins,
181+
cache=cache,
182+
jobs=args.jobs or 1,
183+
advisory_files=args.advisories,
184+
policy_file=args.policy,
134185
)
135186
findings = analysis.findings
136187
if args.sbom is not None:

‎src/coretrace_python/dependency/__init__.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
"""Dependency resolution and advisories (architecture §26)."""
22

3+
from coretrace_python.dependency.advisories import (
4+
ADVISORY_FILE,
5+
AdvisoryFileError,
6+
dump_advisories,
7+
import_osv,
8+
load_advisories,
9+
read_osv,
10+
)
311
from coretrace_python.dependency.graph import (
412
DEPENDENCY_FILES,
513
Advisory,
@@ -10,16 +18,27 @@
1018
normalize,
1119
parse_dependencies,
1220
)
21+
from coretrace_python.dependency.policy import POLICY_FILE, Policy, apply_policy, load_policy
1322
from coretrace_python.dependency.sbom import render_sbom
1423

1524
__all__ = [
25+
"ADVISORY_FILE",
1626
"DEPENDENCY_FILES",
27+
"POLICY_FILE",
1728
"Advisory",
29+
"AdvisoryFileError",
1830
"DependencyAnalysis",
1931
"DependencyGraph",
32+
"Policy",
2033
"Requirement",
2134
"Version",
35+
"apply_policy",
36+
"dump_advisories",
37+
"import_osv",
38+
"load_advisories",
39+
"load_policy",
2240
"normalize",
2341
"parse_dependencies",
42+
"read_osv",
2443
"render_sbom",
2544
]

0 commit comments

Comments
 (0)