stackparity 1.0.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.
- stackparity/__init__.py +17 -0
- stackparity/_engine/agents/investigator.py +294 -0
- stackparity/_engine/agents/memory_agent.py +37 -0
- stackparity/_engine/agents/pipeline_agent.py +152 -0
- stackparity/_engine/agents/remediation_agent.py +248 -0
- stackparity/_engine/agents/runtime.py +177 -0
- stackparity/_engine/agents/specialists.py +709 -0
- stackparity/_engine/agents/suspicious_change_agent.py +1275 -0
- stackparity/_engine/cli/__init__.py +2 -0
- stackparity/_engine/cli/main.py +1252 -0
- stackparity/_engine/cli/remediate.py +461 -0
- stackparity/_engine/cli/term.py +44 -0
- stackparity/_engine/config/__init__.py +0 -0
- stackparity/_engine/config/constants.py +88 -0
- stackparity/_engine/config/loader.py +155 -0
- stackparity/_engine/config/settings.py +108 -0
- stackparity/_engine/containers/__init__.py +12 -0
- stackparity/_engine/containers/discovery.py +143 -0
- stackparity/_engine/containers/models.py +72 -0
- stackparity/_engine/containers/trivy_image_scanner.py +165 -0
- stackparity/_engine/core/__init__.py +0 -0
- stackparity/_engine/core/capabilities.py +107 -0
- stackparity/_engine/core/finding_schema.py +305 -0
- stackparity/_engine/core/git_manager.py +206 -0
- stackparity/_engine/core/http.py +64 -0
- stackparity/_engine/core/models.py +109 -0
- stackparity/_engine/core/netpolicy.py +242 -0
- stackparity/_engine/core/plugins.py +43 -0
- stackparity/_engine/core/repository_inspector.py +243 -0
- stackparity/_engine/core/safe_env.py +48 -0
- stackparity/_engine/core/scan_orchestrator.py +289 -0
- stackparity/_engine/core/scan_state.py +58 -0
- stackparity/_engine/core/url_validator.py +135 -0
- stackparity/_engine/ecosystems/__init__.py +1 -0
- stackparity/_engine/ecosystems/registry.py +138 -0
- stackparity/_engine/evidence/__init__.py +1 -0
- stackparity/_engine/evidence/store.py +65 -0
- stackparity/_engine/explainability/__init__.py +0 -0
- stackparity/_engine/explainability/evidence_formatter.py +30 -0
- stackparity/_engine/explainability/explanation_engine.py +132 -0
- stackparity/_engine/explainability/explanation_validator.py +54 -0
- stackparity/_engine/explainability/prompt_templates.py +18 -0
- stackparity/_engine/graph/__init__.py +0 -0
- stackparity/_engine/graph/dependency_tracer.py +86 -0
- stackparity/_engine/graph/graph_builder.py +165 -0
- stackparity/_engine/graph/graph_diff.py +57 -0
- stackparity/_engine/graph/graph_exporter.py +194 -0
- stackparity/_engine/graph/graph_validator.py +46 -0
- stackparity/_engine/graph/impact_analyzer.py +23 -0
- stackparity/_engine/graph/tree_view.py +185 -0
- stackparity/_engine/intel/__init__.py +0 -0
- stackparity/_engine/intel/epss.py +53 -0
- stackparity/_engine/llm/__init__.py +1 -0
- stackparity/_engine/llm/client.py +138 -0
- stackparity/_engine/monitor/__init__.py +0 -0
- stackparity/_engine/monitor/watch.py +171 -0
- stackparity/_engine/non_cve/__init__.py +7 -0
- stackparity/_engine/non_cve/base.py +62 -0
- stackparity/_engine/non_cve/iac.py +144 -0
- stackparity/_engine/non_cve/sast.py +272 -0
- stackparity/_engine/non_cve/supply_chain.py +147 -0
- stackparity/_engine/orchestration/__init__.py +1 -0
- stackparity/_engine/orchestration/pipeline.py +279 -0
- stackparity/_engine/orchestration/supervisor.py +167 -0
- stackparity/_engine/pipeline_analysis/__init__.py +0 -0
- stackparity/_engine/pipeline_analysis/analyzer.py +90 -0
- stackparity/_engine/pipeline_analysis/ci.py +189 -0
- stackparity/_engine/pipeline_analysis/ci_adapters/__init__.py +24 -0
- stackparity/_engine/pipeline_analysis/ci_adapters/azure_pipelines.py +221 -0
- stackparity/_engine/pipeline_analysis/ci_adapters/base.py +239 -0
- stackparity/_engine/pipeline_analysis/ci_adapters/circleci.py +176 -0
- stackparity/_engine/pipeline_analysis/ci_adapters/github_actions.py +356 -0
- stackparity/_engine/pipeline_analysis/ci_adapters/gitlab_ci.py +268 -0
- stackparity/_engine/pipeline_analysis/ci_adapters/jenkins.py +180 -0
- stackparity/_engine/pipeline_analysis/ci_adapters/registry.py +80 -0
- stackparity/_engine/pipeline_analysis/ci_models.py +72 -0
- stackparity/_engine/pipeline_analysis/dockerfile.py +235 -0
- stackparity/_engine/pipeline_analysis/eol.py +62 -0
- stackparity/_engine/pipeline_analysis/models.py +37 -0
- stackparity/_engine/policy/__init__.py +0 -0
- stackparity/_engine/policy/engine.py +216 -0
- stackparity/_engine/policy/resources/default.yaml +56 -0
- stackparity/_engine/policy/resources/strict.yaml +35 -0
- stackparity/_engine/reachability/__init__.py +0 -0
- stackparity/_engine/reachability/app_reachability.py +186 -0
- stackparity/_engine/reachability/scanners.py +215 -0
- stackparity/_engine/remediation/__init__.py +0 -0
- stackparity/_engine/remediation/adapters.py +198 -0
- stackparity/_engine/remediation/apply_local.py +309 -0
- stackparity/_engine/remediation/code_fixes.py +383 -0
- stackparity/_engine/remediation/compat.py +156 -0
- stackparity/_engine/remediation/container_plan.py +127 -0
- stackparity/_engine/remediation/delivery.py +31 -0
- stackparity/_engine/remediation/export.py +44 -0
- stackparity/_engine/remediation/lifecycle.py +623 -0
- stackparity/_engine/remediation/local_trial.py +358 -0
- stackparity/_engine/remediation/patchers.py +215 -0
- stackparity/_engine/remediation/planner.py +285 -0
- stackparity/_engine/remediation/pr_template_engine.py +234 -0
- stackparity/_engine/remediation/reachability_impact.py +147 -0
- stackparity/_engine/remediation/versions.py +92 -0
- stackparity/_engine/remediation/whatif.py +64 -0
- stackparity/_engine/remediation/worktree_pr.py +434 -0
- stackparity/_engine/reporting/__init__.py +0 -0
- stackparity/_engine/reporting/counts.py +63 -0
- stackparity/_engine/reporting/fix_pdf.py +343 -0
- stackparity/_engine/reporting/fix_report.py +135 -0
- stackparity/_engine/reporting/json_exporter.py +61 -0
- stackparity/_engine/reporting/report_generator.py +200 -0
- stackparity/_engine/reporting/sarif.py +71 -0
- stackparity/_engine/reporting/vex.py +57 -0
- stackparity/_engine/reporting/view.py +166 -0
- stackparity/_engine/risk_analysis/__init__.py +0 -0
- stackparity/_engine/risk_analysis/contextual_scorer.py +253 -0
- stackparity/_engine/risk_analysis/evidence_collector.py +62 -0
- stackparity/_engine/risk_analysis/evidence_normalizer.py +37 -0
- stackparity/_engine/risk_analysis/finding_correlator.py +90 -0
- stackparity/_engine/risk_analysis/function_impact.py +184 -0
- stackparity/_engine/risk_analysis/production_context.py +252 -0
- stackparity/_engine/risk_analysis/risk_rules.py +81 -0
- stackparity/_engine/risk_analysis/risk_scorer.py +390 -0
- stackparity/_engine/sandbox/__init__.py +0 -0
- stackparity/_engine/sandbox/runner.py +142 -0
- stackparity/_engine/sbom/__init__.py +0 -0
- stackparity/_engine/sbom/adapters/__init__.py +19 -0
- stackparity/_engine/sbom/adapters/builtin.py +29 -0
- stackparity/_engine/sbom/adapters/syft.py +26 -0
- stackparity/_engine/sbom/adapters/trivy.py +76 -0
- stackparity/_engine/sbom/builtin_generator.py +227 -0
- stackparity/_engine/sbom/cyclonedx_validator.py +166 -0
- stackparity/_engine/sbom/exporters.py +153 -0
- stackparity/_engine/sbom/fusion/__init__.py +5 -0
- stackparity/_engine/sbom/fusion/engine.py +315 -0
- stackparity/_engine/sbom/fusion/hashes.py +47 -0
- stackparity/_engine/sbom/fusion/ledger.py +96 -0
- stackparity/_engine/sbom/fusion/registries.py +271 -0
- stackparity/_engine/sbom/fusion/resolver.py +143 -0
- stackparity/_engine/sbom/fusion/writer.py +114 -0
- stackparity/_engine/sbom/lockfile_analyzer.py +114 -0
- stackparity/_engine/sbom/merge.py +71 -0
- stackparity/_engine/sbom/package_extractors/common.py +104 -0
- stackparity/_engine/sbom/package_extractors/manifests.py +101 -0
- stackparity/_engine/sbom/package_extractors/npm.py +206 -0
- stackparity/_engine/sbom/package_extractors/others.py +267 -0
- stackparity/_engine/sbom/package_extractors/python.py +143 -0
- stackparity/_engine/sbom/quality_analyzer.py +191 -0
- stackparity/_engine/sbom/scanner_engine.py +98 -0
- stackparity/_engine/sbom/syft_runner.py +100 -0
- stackparity/_engine/sbom/tool_locator.py +69 -0
- stackparity/_engine/sbom/trivy_runner.py +82 -0
- stackparity/_engine/storage/__init__.py +0 -0
- stackparity/_engine/storage/cache.py +56 -0
- stackparity/_engine/storage/database.py +295 -0
- stackparity/_engine/storage/memory.py +146 -0
- stackparity/_engine/suspicious_changes/__init__.py +12 -0
- stackparity/_engine/suspicious_changes/analyzer.py +1022 -0
- stackparity/_engine/suspicious_changes/change_correlation.py +861 -0
- stackparity/_engine/suspicious_changes/lifecycle_script_detector.py +263 -0
- stackparity/_engine/suspicious_changes/lockfile_change_detector.py +316 -0
- stackparity/_engine/suspicious_changes/manifest_diff_detector.py +251 -0
- stackparity/_engine/suspicious_changes/package_risk.py +78 -0
- stackparity/_engine/suspicious_changes/popular_packages.py +94 -0
- stackparity/_engine/suspicious_changes/registry_detector.py +354 -0
- stackparity/_engine/suspicious_changes/suspicious_models.py +57 -0
- stackparity/_engine/suspicious_changes/typosquatting_detector.py +298 -0
- stackparity/_engine/tools/__init__.py +1 -0
- stackparity/_engine/tools/builtin.py +93 -0
- stackparity/_engine/tools/registry.py +66 -0
- stackparity/_engine/ui/__init__.py +0 -0
- stackparity/_engine/ui/components/__init__.py +0 -0
- stackparity/_engine/ui/components/common.py +238 -0
- stackparity/_engine/ui/components/live_progress.py +157 -0
- stackparity/_engine/ui/components/memory_panel.py +82 -0
- stackparity/_engine/ui/pages/__init__.py +0 -0
- stackparity/_engine/ui/pages/agents_page.py +76 -0
- stackparity/_engine/ui/pages/architecture_page.py +44 -0
- stackparity/_engine/ui/pages/fixes_page.py +98 -0
- stackparity/_engine/ui/pages/graph_page.py +203 -0
- stackparity/_engine/ui/pages/health_page.py +117 -0
- stackparity/_engine/ui/pages/monitor_page.py +67 -0
- stackparity/_engine/ui/pages/pipeline_page.py +219 -0
- stackparity/_engine/ui/pages/remediation_page.py +409 -0
- stackparity/_engine/ui/pages/reports_page.py +54 -0
- stackparity/_engine/ui/pages/sbom_page.py +210 -0
- stackparity/_engine/ui/pages/scan_page.py +131 -0
- stackparity/_engine/ui/pages/suspicious_page.py +551 -0
- stackparity/_engine/ui/pages/vulns_page.py +201 -0
- stackparity/_engine/ui/state.py +116 -0
- stackparity/_engine/ui/theme.py +201 -0
- stackparity/_engine/ui/visualizations/__init__.py +0 -0
- stackparity/_engine/ui/visualizations/graphs.py +373 -0
- stackparity/_engine/vulnerability/__init__.py +0 -0
- stackparity/_engine/vulnerability/cvss.py +58 -0
- stackparity/_engine/vulnerability/kev_client.py +47 -0
- stackparity/_engine/vulnerability/nvd_client.py +74 -0
- stackparity/_engine/vulnerability/osv_client.py +139 -0
- stackparity/_engine/vulnerability/remediation_engine.py +50 -0
- stackparity/_engine/vulnerability/verification.py +94 -0
- stackparity/_engine/vulnerability/version_matcher.py +299 -0
- stackparity/_engine/vulnerability/vulnerability_correlator.py +303 -0
- stackparity/_engine/vulnerability/vulnerability_models.py +64 -0
- stackparity/cli.py +30 -0
- stackparity/dashboard_app.py +95 -0
- stackparity/web_dist/assets/index-DRJYw2Du.css +1 -0
- stackparity/web_dist/assets/index-Q6LLpbEi.js +495 -0
- stackparity/web_dist/index.html +13 -0
- stackparity/web_server.py +533 -0
- stackparity-1.0.0.dist-info/METADATA +204 -0
- stackparity-1.0.0.dist-info/RECORD +212 -0
- stackparity-1.0.0.dist-info/WHEEL +4 -0
- stackparity-1.0.0.dist-info/entry_points.txt +2 -0
- stackparity-1.0.0.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Registry and Dependency Source Investigator (Phase 1.3)
|
|
3
|
+
Statically inspects .npmrc, .yarnrc, .yarnrc.yml, manifests and lockfiles.
|
|
4
|
+
|
|
5
|
+
Detects:
|
|
6
|
+
1. Insecure unencrypted HTTP registry URLs (MITM vulnerability).
|
|
7
|
+
2. Custom/third-party registry configurations.
|
|
8
|
+
3. Insecure TLS settings (strict-ssl=false, plaintext tokens).
|
|
9
|
+
4. Direct Git-based dependencies (git+https://, git://, git+ssh://, github:).
|
|
10
|
+
5. Direct raw tarball / HTTP dependencies (.tgz, .tar.gz).
|
|
11
|
+
6. Non-standard package registry sources in lockfiles.
|
|
12
|
+
7. Internal package scope anomalies (dependency confusion vectors).
|
|
13
|
+
|
|
14
|
+
Never executes untrusted code.
|
|
15
|
+
"""
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import json
|
|
19
|
+
import re
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
from typing import Any, Dict, List, Optional
|
|
22
|
+
|
|
23
|
+
from core import git_manager as gm
|
|
24
|
+
|
|
25
|
+
OFFICIAL_NPM_REGISTRIES = {
|
|
26
|
+
"https://registry.npmjs.org/",
|
|
27
|
+
"https://registry.npmjs.org",
|
|
28
|
+
"https://registry.yarnpkg.com/",
|
|
29
|
+
"https://registry.yarnpkg.com"
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _is_unencrypted_http(url: str) -> bool:
|
|
34
|
+
clean = url.strip().strip("'\"").lower()
|
|
35
|
+
return clean.startswith("http://") and not clean.startswith("http://localhost") and not clean.startswith("http://127.0.0.1")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _is_git_dependency(val: str) -> bool:
|
|
39
|
+
clean = val.strip().strip("'\"").lower()
|
|
40
|
+
return clean.startswith(("git+", "git://", "github:", "gitlab:", "bitbucket:")) or (
|
|
41
|
+
"github.com/" in clean and not clean.endswith(".tgz") and not clean.endswith(".tar.gz")
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _is_tarball_dependency(val: str) -> bool:
|
|
46
|
+
clean = val.strip().strip("'\"").lower()
|
|
47
|
+
return (clean.startswith("http://") or clean.startswith("https://")) and (
|
|
48
|
+
clean.endswith(".tgz") or clean.endswith(".tar.gz") or ".tgz?" in clean
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def inspect_registry_configurations(repo_dir: Path) -> List[Dict[str, Any]]:
|
|
53
|
+
"""Inspect .npmrc, .yarnrc, .yarnrc.yml files in repository."""
|
|
54
|
+
findings: List[Dict[str, Any]] = []
|
|
55
|
+
config_names = [".npmrc", ".yarnrc", ".yarnrc.yml", ".yarnrc.yaml"]
|
|
56
|
+
|
|
57
|
+
for cfg_name in config_names:
|
|
58
|
+
cfg_path = repo_dir / cfg_name
|
|
59
|
+
if not cfg_path.exists():
|
|
60
|
+
continue
|
|
61
|
+
|
|
62
|
+
rel_path = str(cfg_path.relative_to(repo_dir)).replace("\\", "/")
|
|
63
|
+
try:
|
|
64
|
+
content = cfg_path.read_text(encoding="utf-8", errors="replace")
|
|
65
|
+
except OSError:
|
|
66
|
+
continue
|
|
67
|
+
|
|
68
|
+
lines = content.splitlines()
|
|
69
|
+
for idx, line in enumerate(lines, start=1):
|
|
70
|
+
clean = line.strip()
|
|
71
|
+
if not clean or clean.startswith(("#", ";")) or (clean.startswith("//") and "_authToken" not in clean):
|
|
72
|
+
continue
|
|
73
|
+
|
|
74
|
+
# Check 1: Insecure HTTP registry URL
|
|
75
|
+
if "registry" in clean.lower():
|
|
76
|
+
match = re.search(r"https?://[^\s\"']+", clean)
|
|
77
|
+
if match:
|
|
78
|
+
url = match.group(0)
|
|
79
|
+
if _is_unencrypted_http(url):
|
|
80
|
+
findings.append({
|
|
81
|
+
"id": f"REG-INSECURE-HTTP-L{idx}",
|
|
82
|
+
"category": "Registry & Source",
|
|
83
|
+
"title": f"Unencrypted HTTP Registry Configured in {cfg_name}",
|
|
84
|
+
"severity": "HIGH",
|
|
85
|
+
"confidence": "HIGH",
|
|
86
|
+
"status": "OBSERVED",
|
|
87
|
+
"affected_file": rel_path,
|
|
88
|
+
"line_number": idx,
|
|
89
|
+
"evidence_type": "HEURISTIC_FLAG",
|
|
90
|
+
"evidence": clean,
|
|
91
|
+
"technical_explanation": (
|
|
92
|
+
f"Configuration '{cfg_name}' defines unencrypted HTTP registry URL '{url}'. "
|
|
93
|
+
"Package installations over cleartext HTTP are vulnerable to active Man-in-the-Middle "
|
|
94
|
+
"(MITM) network attacks allowing package tampering."
|
|
95
|
+
),
|
|
96
|
+
"potential_impact": "Network attacker can inject compromised packages during build or installation.",
|
|
97
|
+
"recommended_action": f"Update registry URL to use encrypted HTTPS (e.g. '{url.replace('http://', 'https://')}').",
|
|
98
|
+
"references": ["https://docs.npmjs.com/cli/v9/using-npm/config#registry"]
|
|
99
|
+
})
|
|
100
|
+
elif not any(url.rstrip("/").lower() == off.rstrip("/").lower() for off in OFFICIAL_NPM_REGISTRIES):
|
|
101
|
+
findings.append({
|
|
102
|
+
"id": f"REG-CUSTOM-MIRROR-L{idx}",
|
|
103
|
+
"category": "Registry & Source",
|
|
104
|
+
"title": f"Custom Non-Standard Registry Mirror in {cfg_name}",
|
|
105
|
+
"severity": "LOW",
|
|
106
|
+
"confidence": "MEDIUM",
|
|
107
|
+
"status": "OBSERVED",
|
|
108
|
+
"affected_file": rel_path,
|
|
109
|
+
"line_number": idx,
|
|
110
|
+
"evidence_type": "HEURISTIC_FLAG",
|
|
111
|
+
"evidence": clean,
|
|
112
|
+
"technical_explanation": (
|
|
113
|
+
f"Configuration '{cfg_name}' points to custom registry endpoint '{url}'. "
|
|
114
|
+
"Non-default registries must be verified to ensure package integrity and avoid "
|
|
115
|
+
"unauthorized mirrors."
|
|
116
|
+
),
|
|
117
|
+
"potential_impact": "Build depends on third-party registry availability and integrity.",
|
|
118
|
+
"recommended_action": "Verify upstream authenticity and operational security of custom registry mirror.",
|
|
119
|
+
"references": []
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
# Check 2: Disabled SSL Verification (strict-ssl=false)
|
|
123
|
+
if re.search(r"strict-ssl\s*=\s*(false|0)", clean, re.IGNORECASE):
|
|
124
|
+
findings.append({
|
|
125
|
+
"id": f"REG-STRICT-SSL-DISABLED-L{idx}",
|
|
126
|
+
"category": "Registry & Source",
|
|
127
|
+
"title": f"TLS Verification Disabled in {cfg_name}",
|
|
128
|
+
"severity": "CRITICAL",
|
|
129
|
+
"confidence": "HIGH",
|
|
130
|
+
"status": "OBSERVED",
|
|
131
|
+
"affected_file": rel_path,
|
|
132
|
+
"line_number": idx,
|
|
133
|
+
"evidence_type": "HEURISTIC_FLAG",
|
|
134
|
+
"evidence": clean,
|
|
135
|
+
"technical_explanation": (
|
|
136
|
+
f"Configuration in '{cfg_name}' explicitly disables TLS certificate validation ('strict-ssl=false'). "
|
|
137
|
+
"This permits invalid, expired, or intercepted certificates during package download."
|
|
138
|
+
),
|
|
139
|
+
"potential_impact": "Allows attackers with network access to forge certificates and deliver malicious payloads.",
|
|
140
|
+
"recommended_action": "Remove 'strict-ssl=false' and ensure proper CA trust configuration.",
|
|
141
|
+
"references": ["https://docs.npmjs.com/cli/v9/using-npm/config#strict-ssl"]
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
# Check 3: Plaintext Auth Token in committed config
|
|
145
|
+
if re.search(r"_authToken\s*=\s*([a-zA-Z0-9_\-]{20,})", clean):
|
|
146
|
+
findings.append({
|
|
147
|
+
"id": f"REG-COMMITTED-TOKEN-L{idx}",
|
|
148
|
+
"category": "Registry & Source",
|
|
149
|
+
"title": f"Committed Plaintext Registry Token in {cfg_name}",
|
|
150
|
+
"severity": "CRITICAL",
|
|
151
|
+
"confidence": "HIGH",
|
|
152
|
+
"status": "OBSERVED",
|
|
153
|
+
"affected_file": rel_path,
|
|
154
|
+
"line_number": idx,
|
|
155
|
+
"evidence_type": "HEURISTIC_FLAG",
|
|
156
|
+
"evidence": re.sub(r"(=)[a-zA-Z0-9_\-]+", r"\1[REDACTED]", clean),
|
|
157
|
+
"technical_explanation": (
|
|
158
|
+
f"Found plaintext authentication token committed in '{cfg_name}'. Committed credentials "
|
|
159
|
+
"can be harvested from version control history."
|
|
160
|
+
),
|
|
161
|
+
"potential_impact": "Unauthorized package publishing or registry access under developer credentials.",
|
|
162
|
+
"recommended_action": "Revoke the committed token immediately and reference environment variables (${NPM_TOKEN}).",
|
|
163
|
+
"references": []
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
return findings
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def inspect_manifest_dependency_sources(manifest_data: dict, file_path: str) -> List[Dict[str, Any]]:
|
|
170
|
+
"""Inspect package.json dependencies for direct Git URLs, raw HTTP tarballs, or internal scope confusion."""
|
|
171
|
+
findings: List[Dict[str, Any]] = []
|
|
172
|
+
|
|
173
|
+
for dep_sec in ("dependencies", "devDependencies", "peerDependencies", "optionalDependencies"):
|
|
174
|
+
deps = manifest_data.get(dep_sec, {})
|
|
175
|
+
if not isinstance(deps, dict):
|
|
176
|
+
continue
|
|
177
|
+
|
|
178
|
+
for pkg, spec in deps.items():
|
|
179
|
+
if not isinstance(spec, str):
|
|
180
|
+
continue
|
|
181
|
+
|
|
182
|
+
# Check 1: Direct Git repository dependencies
|
|
183
|
+
if _is_git_dependency(spec):
|
|
184
|
+
is_sha_pinned = bool(re.search(r"(?:#|/tarball/|/archive/|/commit/|@)[0-9a-fA-F]{40}$", spec.strip().strip("'\""))) or bool(re.search(r"[0-9a-fA-F]{40}$", spec.strip().strip("'\"")))
|
|
185
|
+
is_branch_ref = bool(re.search(r"(?:/tarball/|#)(?:master|main|develop|trunk|latest)$", spec.strip().strip("'\""), re.IGNORECASE))
|
|
186
|
+
sev = "LOW" if is_sha_pinned else "MEDIUM"
|
|
187
|
+
title = (
|
|
188
|
+
f"Git Dependency '{pkg}' Pinned to Commit SHA"
|
|
189
|
+
if is_sha_pinned
|
|
190
|
+
else f"Direct Git-Sourced Dependency '{pkg}' ({'Mutable Branch' if is_branch_ref else 'Unpinned Tag'})"
|
|
191
|
+
)
|
|
192
|
+
if is_sha_pinned:
|
|
193
|
+
tech_exp = (
|
|
194
|
+
f"Package '{pkg}' is sourced from Git ('{spec}') and pinned to an immutable 40-character commit SHA. "
|
|
195
|
+
"Cryptographic commit pinning provides immutable code anchoring."
|
|
196
|
+
)
|
|
197
|
+
else:
|
|
198
|
+
tech_exp = (
|
|
199
|
+
f"Package '{pkg}' is sourced directly from a Git repository ('{spec}') rather than a registered package registry. "
|
|
200
|
+
f"The specification references a {'mutable branch' if is_branch_ref else 'moving reference'}, "
|
|
201
|
+
"which poses reproducibility and upstream drift risks. However, if package-lock.json resolves "
|
|
202
|
+
"this reference to an immutable commit hash or integrity checksum, the local installation is frozen."
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
findings.append({
|
|
206
|
+
"id": f"SRC-GIT-DEP-{pkg.upper().replace('@', '').replace('/', '_')}",
|
|
207
|
+
"category": "Dependency Source",
|
|
208
|
+
"title": title,
|
|
209
|
+
"severity": sev,
|
|
210
|
+
"confidence": "HIGH" if is_sha_pinned else "MEDIUM",
|
|
211
|
+
"status": "OBSERVED",
|
|
212
|
+
"affected_file": file_path,
|
|
213
|
+
"line_number": 1,
|
|
214
|
+
"evidence_type": "HEURISTIC_FLAG",
|
|
215
|
+
"evidence": f'"{pkg}": "{spec}"',
|
|
216
|
+
"technical_explanation": tech_exp,
|
|
217
|
+
"potential_impact": (
|
|
218
|
+
"Minimal risk when pinned to full commit SHA; mutable branches or tags can introduce upstream changes between builds if lockfile is regenerated."
|
|
219
|
+
),
|
|
220
|
+
"recommended_action": "Pin Git dependencies to full 40-character commit SHAs or vendor with subresource integrity digests.",
|
|
221
|
+
"references": []
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
# Check 2: Direct raw HTTP/tarball dependencies
|
|
225
|
+
elif _is_tarball_dependency(spec):
|
|
226
|
+
is_http = _is_unencrypted_http(spec)
|
|
227
|
+
findings.append({
|
|
228
|
+
"id": f"SRC-TARBALL-DEP-{pkg.upper().replace('@', '').replace('/', '_')}",
|
|
229
|
+
"category": "Dependency Source",
|
|
230
|
+
"title": f"Direct URL/Tarball Dependency '{pkg}'",
|
|
231
|
+
"severity": "HIGH" if is_http else "MEDIUM",
|
|
232
|
+
"confidence": "HIGH",
|
|
233
|
+
"status": "OBSERVED",
|
|
234
|
+
"affected_file": file_path,
|
|
235
|
+
"line_number": 1,
|
|
236
|
+
"evidence_type": "HEURISTIC_FLAG",
|
|
237
|
+
"evidence": f'"{pkg}": "{spec}"',
|
|
238
|
+
"technical_explanation": (
|
|
239
|
+
f"Package '{pkg}' downloads directly from raw tarball URL '{spec}'. "
|
|
240
|
+
+ ("Source uses unencrypted HTTP (MITM risk). " if is_http else "")
|
|
241
|
+
+ "Direct tarballs lack centralized signature verification and provenance."
|
|
242
|
+
),
|
|
243
|
+
"potential_impact": "Upstream URL modification or MITM injection of malicious archive.",
|
|
244
|
+
"recommended_action": "Vendor archive with cryptographic checksum verification or migrate to official registry release.",
|
|
245
|
+
"references": []
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
return findings
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def inspect_lockfile_sources(lock_data: dict, file_path: str) -> List[Dict[str, Any]]:
|
|
252
|
+
"""Inspect package-lock.json resolved URLs for unencrypted HTTP, non-standard endpoints, or untrusted mirrors."""
|
|
253
|
+
findings: List[Dict[str, Any]] = []
|
|
254
|
+
packages = lock_data.get("packages", {})
|
|
255
|
+
|
|
256
|
+
# Check both lockfile v2/v3 packages and v1 dependencies
|
|
257
|
+
items = []
|
|
258
|
+
if isinstance(packages, dict):
|
|
259
|
+
for p_path, p_info in packages.items():
|
|
260
|
+
if isinstance(p_info, dict) and p_info.get("resolved"):
|
|
261
|
+
p_name = p_path.split("node_modules/")[-1] or p_info.get("name") or p_path
|
|
262
|
+
items.append((p_name, p_info.get("version", ""), p_info.get("resolved", "")))
|
|
263
|
+
|
|
264
|
+
deps = lock_data.get("dependencies", {})
|
|
265
|
+
if isinstance(deps, dict):
|
|
266
|
+
for d_name, d_info in deps.items():
|
|
267
|
+
if isinstance(d_info, dict) and d_info.get("resolved"):
|
|
268
|
+
items.append((d_name, d_info.get("version", ""), d_info.get("resolved", "")))
|
|
269
|
+
|
|
270
|
+
seen_sources = set()
|
|
271
|
+
for name, ver, resolved in items:
|
|
272
|
+
if not isinstance(resolved, str) or resolved in seen_sources:
|
|
273
|
+
continue
|
|
274
|
+
seen_sources.add(resolved)
|
|
275
|
+
|
|
276
|
+
# Check 1: Insecure cleartext HTTP resolved URL
|
|
277
|
+
if _is_unencrypted_http(resolved):
|
|
278
|
+
findings.append({
|
|
279
|
+
"id": f"LOCK-INSECURE-HTTP-{name.upper().replace('@', '').replace('/', '_')}",
|
|
280
|
+
"category": "Dependency Source",
|
|
281
|
+
"title": f"Package '{name}' Resolved from Unencrypted HTTP Source",
|
|
282
|
+
"severity": "HIGH",
|
|
283
|
+
"confidence": "HIGH",
|
|
284
|
+
"status": "OBSERVED",
|
|
285
|
+
"affected_file": file_path,
|
|
286
|
+
"line_number": 1,
|
|
287
|
+
"evidence_type": "HEURISTIC_FLAG",
|
|
288
|
+
"evidence": f'"{name}": {{"version": "{ver}", "resolved": "{resolved}"}}',
|
|
289
|
+
"technical_explanation": (
|
|
290
|
+
f"Dependency '{name}@{ver}' resolved from cleartext HTTP endpoint '{resolved}'. "
|
|
291
|
+
"Package archives transmitted over HTTP can be intercepted and modified by in-transit network attackers."
|
|
292
|
+
),
|
|
293
|
+
"potential_impact": "Man-in-the-middle code injection during npm install.",
|
|
294
|
+
"recommended_action": "Re-generate lockfile using HTTPS registry endpoints.",
|
|
295
|
+
"references": []
|
|
296
|
+
})
|
|
297
|
+
|
|
298
|
+
# Check 2: Direct non-registry Git archive resolution
|
|
299
|
+
elif "github.com/" in resolved and "/archive/" in resolved:
|
|
300
|
+
findings.append({
|
|
301
|
+
"id": f"LOCK-GIT-ARCHIVE-{name.upper().replace('@', '').replace('/', '_')}",
|
|
302
|
+
"category": "Dependency Source",
|
|
303
|
+
"title": f"Package '{name}' Resolved from Raw GitHub Archive",
|
|
304
|
+
"severity": "MEDIUM",
|
|
305
|
+
"confidence": "HIGH",
|
|
306
|
+
"status": "OBSERVED",
|
|
307
|
+
"affected_file": file_path,
|
|
308
|
+
"line_number": 1,
|
|
309
|
+
"evidence_type": "HEURISTIC_FLAG",
|
|
310
|
+
"evidence": f'"{name}": {{"version": "{ver}", "resolved": "{resolved}"}}',
|
|
311
|
+
"technical_explanation": (
|
|
312
|
+
f"Dependency '{name}@{ver}' is pinned to raw repository archive URL '{resolved}' rather than a "
|
|
313
|
+
"formal release registry. Git archives may not guarantee reproducible byte streams."
|
|
314
|
+
),
|
|
315
|
+
"potential_impact": "Non-reproducible build or uncontrolled code update.",
|
|
316
|
+
"recommended_action": "Migrate to canonical package release with immutable integrity hash.",
|
|
317
|
+
"references": []
|
|
318
|
+
})
|
|
319
|
+
|
|
320
|
+
return findings
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
def analyze_registry_and_sources(repo_dir: Path) -> List[Dict[str, Any]]:
|
|
324
|
+
"""Master registry and source investigator."""
|
|
325
|
+
all_findings: List[Dict[str, Any]] = []
|
|
326
|
+
|
|
327
|
+
# 1. Config files (.npmrc, .yarnrc)
|
|
328
|
+
all_findings.extend(inspect_registry_configurations(repo_dir))
|
|
329
|
+
|
|
330
|
+
# 2. Manifests (package.json)
|
|
331
|
+
for pkg_json in repo_dir.rglob("package.json"):
|
|
332
|
+
# Skip node_modules
|
|
333
|
+
if "node_modules" in pkg_json.parts:
|
|
334
|
+
continue
|
|
335
|
+
rel_path = str(pkg_json.relative_to(repo_dir)).replace("\\", "/")
|
|
336
|
+
try:
|
|
337
|
+
data = json.loads(pkg_json.read_text(encoding="utf-8", errors="replace"))
|
|
338
|
+
all_findings.extend(inspect_manifest_dependency_sources(data, rel_path))
|
|
339
|
+
except (json.JSONDecodeError, OSError):
|
|
340
|
+
continue
|
|
341
|
+
|
|
342
|
+
# 3. Lockfiles (package-lock.json)
|
|
343
|
+
for lock_file in repo_dir.rglob("package-lock.json"):
|
|
344
|
+
if "node_modules" in lock_file.parts:
|
|
345
|
+
continue
|
|
346
|
+
rel_path = str(lock_file.relative_to(repo_dir)).replace("\\", "/")
|
|
347
|
+
try:
|
|
348
|
+
data = json.loads(lock_file.read_text(encoding="utf-8", errors="replace"))
|
|
349
|
+
all_findings.extend(inspect_lockfile_sources(data, rel_path))
|
|
350
|
+
except (json.JSONDecodeError, OSError):
|
|
351
|
+
continue
|
|
352
|
+
|
|
353
|
+
return all_findings
|
|
354
|
+
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""Models for suspicious-change findings (typosquatting, lockfile changes)."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from typing import Any, Optional
|
|
5
|
+
|
|
6
|
+
from pydantic import BaseModel, Field
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class TyposquatFinding(BaseModel):
|
|
10
|
+
finding_id: str = ""
|
|
11
|
+
kind: str = "typosquatting"
|
|
12
|
+
classification: str = "suspicious_name_similarity" # never "malicious": similarity is a signal, not proof
|
|
13
|
+
package_name: str
|
|
14
|
+
ecosystem: str
|
|
15
|
+
versions: list[str] = Field(default_factory=list)
|
|
16
|
+
component_refs: list[str] = Field(default_factory=list)
|
|
17
|
+
similar_to: str
|
|
18
|
+
similarity_score: float # 0-100
|
|
19
|
+
edit_distance: int
|
|
20
|
+
techniques: list[str] = Field(default_factory=list)
|
|
21
|
+
reference_source: str = ""
|
|
22
|
+
evidence: list[str] = Field(default_factory=list)
|
|
23
|
+
registry: dict[str, Any] = Field(default_factory=dict)
|
|
24
|
+
evidence_strength: str = "low" # low | medium | high
|
|
25
|
+
evidence_points: int = 0
|
|
26
|
+
why_flagged: str = ""
|
|
27
|
+
caveats: list[str] = Field(default_factory=list)
|
|
28
|
+
dependency_type: str = "unknown"
|
|
29
|
+
roots: list[str] = Field(default_factory=list)
|
|
30
|
+
services: list[str] = Field(default_factory=list)
|
|
31
|
+
severity: Optional[str] = None # unused for anomalies (no CVSS); kept for storage symmetry
|
|
32
|
+
scan_timestamp: str = ""
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class LockfileChange(BaseModel):
|
|
36
|
+
finding_id: str = ""
|
|
37
|
+
kind: str = "lockfile_change"
|
|
38
|
+
commit: str
|
|
39
|
+
baseline: str
|
|
40
|
+
baseline_mode: str # parent | explicit
|
|
41
|
+
commit_info: dict[str, str] = Field(default_factory=dict)
|
|
42
|
+
file: str
|
|
43
|
+
ecosystem: str
|
|
44
|
+
package_name: str
|
|
45
|
+
change_type: str # added | removed | version_changed | integrity_changed | integrity_removed | source_changed | lockfile_removed | lockfile_added | unparseable
|
|
46
|
+
previous: dict[str, Any] = Field(default_factory=dict)
|
|
47
|
+
new: dict[str, Any] = Field(default_factory=dict)
|
|
48
|
+
manifest_changed: bool = False
|
|
49
|
+
manifest_files_changed: list[str] = Field(default_factory=list)
|
|
50
|
+
classification: str = "normal_update" # normal_update | unexplained_change | potentially_suspicious | insufficient_baseline
|
|
51
|
+
signals: list[str] = Field(default_factory=list)
|
|
52
|
+
reasons: list[str] = Field(default_factory=list)
|
|
53
|
+
evidence_strength: str = "low"
|
|
54
|
+
diff_excerpt: list[str] = Field(default_factory=list)
|
|
55
|
+
component_ref: Optional[str] = None
|
|
56
|
+
severity: Optional[str] = None
|
|
57
|
+
scan_timestamp: str = ""
|