apisec-surface 0.1.13__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.
- apisec_code_bolt/__init__.py +48 -0
- apisec_code_bolt/__main__.py +11 -0
- apisec_code_bolt/analysis/__init__.py +96 -0
- apisec_code_bolt/analysis/analyzer.py +3518 -0
- apisec_code_bolt/analysis/binding_tracker.py +384 -0
- apisec_code_bolt/analysis/boundary_detection.py +809 -0
- apisec_code_bolt/analysis/call_graph.py +1397 -0
- apisec_code_bolt/analysis/call_graph_types.py +332 -0
- apisec_code_bolt/analysis/call_resolver.py +1155 -0
- apisec_code_bolt/analysis/capability_tagger.py +322 -0
- apisec_code_bolt/analysis/config_scanner.py +781 -0
- apisec_code_bolt/analysis/container_facts.py +320 -0
- apisec_code_bolt/analysis/data_flow.py +1972 -0
- apisec_code_bolt/analysis/dependency_extractor.py +3539 -0
- apisec_code_bolt/analysis/flow_analysis.py +1406 -0
- apisec_code_bolt/analysis/hof_catalog.py +61 -0
- apisec_code_bolt/analysis/integration_detector.py +1864 -0
- apisec_code_bolt/analysis/js_gradle_locks.py +277 -0
- apisec_code_bolt/analysis/legacy_contract.py +210 -0
- apisec_code_bolt/analysis/literal_scanner.py +579 -0
- apisec_code_bolt/analysis/manifest_partitioner.py +500 -0
- apisec_code_bolt/analysis/parse_index.py +87 -0
- apisec_code_bolt/analysis/path_normalizer.py +55 -0
- apisec_code_bolt/analysis/path_resolution_ladder.py +73 -0
- apisec_code_bolt/analysis/read_site_detector.py +326 -0
- apisec_code_bolt/analysis/request_patterns.py +168 -0
- apisec_code_bolt/analysis/route_like.py +23 -0
- apisec_code_bolt/analysis/secret_patterns.py +404 -0
- apisec_code_bolt/analysis/secrets_policy.py +246 -0
- apisec_code_bolt/analysis/sensitivity_classifier.py +249 -0
- apisec_code_bolt/analysis/sink_evidence.py +333 -0
- apisec_code_bolt/analysis/surface_enrichment.py +519 -0
- apisec_code_bolt/analysis/surface_ledger.py +146 -0
- apisec_code_bolt/analysis/surface_reconciliation.py +68 -0
- apisec_code_bolt/analysis/template_scanner.py +1020 -0
- apisec_code_bolt/analysis/tree_resolver.py +203 -0
- apisec_code_bolt/analysis/unsupported_frameworks.py +422 -0
- apisec_code_bolt/analysis/url_prefix_resolver.py +985 -0
- apisec_code_bolt/analysis/version_resolver.py +366 -0
- apisec_code_bolt/cli/__init__.py +5 -0
- apisec_code_bolt/cli/exit_codes.py +18 -0
- apisec_code_bolt/cli/main.py +1785 -0
- apisec_code_bolt/cloud/__init__.py +1 -0
- apisec_code_bolt/cloud/apisec_client.py +233 -0
- apisec_code_bolt/cloud/client.py +446 -0
- apisec_code_bolt/core/__init__.py +81 -0
- apisec_code_bolt/core/config.py +624 -0
- apisec_code_bolt/core/credentials.py +65 -0
- apisec_code_bolt/core/discovery.py +523 -0
- apisec_code_bolt/core/log_format.py +133 -0
- apisec_code_bolt/core/manifest.py +1390 -0
- apisec_code_bolt/core/memory_guard.py +423 -0
- apisec_code_bolt/core/repo.py +287 -0
- apisec_code_bolt/core/runtime_entry_points.py +45 -0
- apisec_code_bolt/core/state.py +75 -0
- apisec_code_bolt/core/telemetry.py +485 -0
- apisec_code_bolt/core/types.py +603 -0
- apisec_code_bolt/fingerprinting/__init__.py +1 -0
- apisec_code_bolt/frameworks/__init__.py +29 -0
- apisec_code_bolt/frameworks/_graphql_common.py +45 -0
- apisec_code_bolt/frameworks/_jwt_common.py +50 -0
- apisec_code_bolt/frameworks/_weak_random_common.py +140 -0
- apisec_code_bolt/frameworks/auth_helpers.py +437 -0
- apisec_code_bolt/frameworks/base.py +735 -0
- apisec_code_bolt/frameworks/dotnet/__init__.py +19 -0
- apisec_code_bolt/frameworks/dotnet/_path_helpers.py +43 -0
- apisec_code_bolt/frameworks/dotnet/aspnet_plugin.py +3092 -0
- apisec_code_bolt/frameworks/dotnet/grpc_plugin.py +559 -0
- apisec_code_bolt/frameworks/dotnet/jwt_config_extractor.py +545 -0
- apisec_code_bolt/frameworks/dotnet/legacy_aspnet_plugin.py +732 -0
- apisec_code_bolt/frameworks/dotnet/refit_plugin.py +374 -0
- apisec_code_bolt/frameworks/dotnet/taint_scanner.py +1597 -0
- apisec_code_bolt/frameworks/dotnet/wcf_plugin.py +1239 -0
- apisec_code_bolt/frameworks/dotnet/webforms_plugin.py +393 -0
- apisec_code_bolt/frameworks/java/__init__.py +7 -0
- apisec_code_bolt/frameworks/java/_annotations.py +377 -0
- apisec_code_bolt/frameworks/java/_constraints.py +128 -0
- apisec_code_bolt/frameworks/java/graphql_plugin.py +280 -0
- apisec_code_bolt/frameworks/java/jaxrs_plugin.py +813 -0
- apisec_code_bolt/frameworks/java/jwt_config_extractor.py +361 -0
- apisec_code_bolt/frameworks/java/micronaut_plugin.py +1121 -0
- apisec_code_bolt/frameworks/java/servlet_plugin.py +380 -0
- apisec_code_bolt/frameworks/java/spring_plugin.py +1491 -0
- apisec_code_bolt/frameworks/java/taint_scanner.py +2910 -0
- apisec_code_bolt/frameworks/js/__init__.py +9 -0
- apisec_code_bolt/frameworks/js/_route_helpers.py +30 -0
- apisec_code_bolt/frameworks/js/express_plugin.py +430 -0
- apisec_code_bolt/frameworks/js/fastify_plugin.py +421 -0
- apisec_code_bolt/frameworks/js/graphql_plugin.py +191 -0
- apisec_code_bolt/frameworks/js/koa_plugin.py +283 -0
- apisec_code_bolt/frameworks/js/nestjs_plugin.py +410 -0
- apisec_code_bolt/frameworks/js/taint_scanner.py +2715 -0
- apisec_code_bolt/frameworks/python/__init__.py +19 -0
- apisec_code_bolt/frameworks/python/celery_plugin.py +393 -0
- apisec_code_bolt/frameworks/python/click_plugin.py +427 -0
- apisec_code_bolt/frameworks/python/django_plugin.py +990 -0
- apisec_code_bolt/frameworks/python/fastapi/__init__.py +28 -0
- apisec_code_bolt/frameworks/python/fastapi/plugin.py +1741 -0
- apisec_code_bolt/frameworks/python/flask_plugin.py +748 -0
- apisec_code_bolt/frameworks/python/graphql_plugin.py +257 -0
- apisec_code_bolt/frameworks/python/prefect_plugin.py +251 -0
- apisec_code_bolt/frameworks/python/taint_scanner.py +1953 -0
- apisec_code_bolt/frameworks/python/weak_hash_facts.py +159 -0
- apisec_code_bolt/frameworks/python/webhook_plugin.py +255 -0
- apisec_code_bolt/frameworks/ruby/__init__.py +24 -0
- apisec_code_bolt/frameworks/ruby/_ts_helpers.py +16 -0
- apisec_code_bolt/frameworks/ruby/action_cable_plugin.py +152 -0
- apisec_code_bolt/frameworks/ruby/grape_plugin.py +486 -0
- apisec_code_bolt/frameworks/ruby/graphql_plugin.py +358 -0
- apisec_code_bolt/frameworks/ruby/hanami_plugin.py +254 -0
- apisec_code_bolt/frameworks/ruby/padrino_plugin.py +114 -0
- apisec_code_bolt/frameworks/ruby/rails_plugin.py +1098 -0
- apisec_code_bolt/frameworks/ruby/roda_plugin.py +260 -0
- apisec_code_bolt/frameworks/ruby/sidekiq_plugin.py +208 -0
- apisec_code_bolt/frameworks/ruby/sinatra_plugin.py +478 -0
- apisec_code_bolt/frameworks/ruby/taint_scanner.py +2772 -0
- apisec_code_bolt/frameworks/ruby/weak_hash_facts.py +192 -0
- apisec_code_bolt/parsing/__init__.py +62 -0
- apisec_code_bolt/parsing/base.py +555 -0
- apisec_code_bolt/parsing/csharp/__init__.py +5 -0
- apisec_code_bolt/parsing/csharp/language_services.py +203 -0
- apisec_code_bolt/parsing/csharp/literals.py +72 -0
- apisec_code_bolt/parsing/csharp/parser.py +1158 -0
- apisec_code_bolt/parsing/csharp/type_resolver.py +568 -0
- apisec_code_bolt/parsing/js/__init__.py +5 -0
- apisec_code_bolt/parsing/js/language_services.py +118 -0
- apisec_code_bolt/parsing/js/parser.py +677 -0
- apisec_code_bolt/parsing/jvm/__init__.py +7 -0
- apisec_code_bolt/parsing/jvm/language_services.py +555 -0
- apisec_code_bolt/parsing/jvm/member_reference.py +11 -0
- apisec_code_bolt/parsing/jvm/parser.py +954 -0
- apisec_code_bolt/parsing/jvm/type_resolver.py +422 -0
- apisec_code_bolt/parsing/python/__init__.py +150 -0
- apisec_code_bolt/parsing/python/cbv_extractor.py +606 -0
- apisec_code_bolt/parsing/python/constant_resolver.py +500 -0
- apisec_code_bolt/parsing/python/cross_file_resolver.py +1106 -0
- apisec_code_bolt/parsing/python/dynamic_route_detector.py +532 -0
- apisec_code_bolt/parsing/python/expression_utils.py +221 -0
- apisec_code_bolt/parsing/python/extraction_types.py +271 -0
- apisec_code_bolt/parsing/python/language_services.py +487 -0
- apisec_code_bolt/parsing/python/parameter_analyzer.py +855 -0
- apisec_code_bolt/parsing/python/parser.py +751 -0
- apisec_code_bolt/parsing/python/path_resolver.py +576 -0
- apisec_code_bolt/parsing/python/router_registry.py +964 -0
- apisec_code_bolt/parsing/python/type_resolver.py +730 -0
- apisec_code_bolt/parsing/python/visitors.py +1581 -0
- apisec_code_bolt/parsing/ruby/__init__.py +5 -0
- apisec_code_bolt/parsing/ruby/language_services.py +807 -0
- apisec_code_bolt/parsing/ruby/parser.py +342 -0
- apisec_code_bolt/parsing/services.py +544 -0
- apisec_code_bolt/pkgindex/__init__.py +15 -0
- apisec_code_bolt/pkgindex/indexer.py +319 -0
- apisec_code_bolt/pkgindex/metadata.py +333 -0
- apisec_code_bolt/pkgindex/module_graph.py +318 -0
- apisec_code_bolt/pkgindex/package_source.py +447 -0
- apisec_code_bolt/query/__init__.py +1 -0
- apisec_code_bolt/query/ast_cache.py +215 -0
- apisec_code_bolt/query/executor.py +481 -0
- apisec_code_bolt/query/handlers.py +1232 -0
- apisec_code_bolt/query/manifest_context.py +313 -0
- apisec_surface-0.1.13.dist-info/METADATA +541 -0
- apisec_surface-0.1.13.dist-info/RECORD +164 -0
- apisec_surface-0.1.13.dist-info/WHEEL +4 -0
- apisec_surface-0.1.13.dist-info/entry_points.txt +3 -0
|
@@ -0,0 +1,735 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Base classes and protocols for framework-specific plugins.
|
|
3
|
+
|
|
4
|
+
Each framework (FastAPI, Spring Boot, etc.) implements the FrameworkPlugin
|
|
5
|
+
protocol to provide specialized extraction of routes, dependencies, auth,
|
|
6
|
+
and middleware.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import re
|
|
12
|
+
from abc import ABC, abstractmethod
|
|
13
|
+
from dataclasses import dataclass, field
|
|
14
|
+
from typing import TYPE_CHECKING, Any, ClassVar
|
|
15
|
+
|
|
16
|
+
from ..core.types import (
|
|
17
|
+
AuthDependencyType,
|
|
18
|
+
AuthSchemeType,
|
|
19
|
+
CodeLocation,
|
|
20
|
+
Confidence,
|
|
21
|
+
Framework,
|
|
22
|
+
HttpMethod,
|
|
23
|
+
Language,
|
|
24
|
+
ParameterLocation,
|
|
25
|
+
QualifiedName,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
if TYPE_CHECKING:
|
|
29
|
+
from ..parsing.base import ParsedClass, ParsedDecorator, ParsedFile, ParsedFunction
|
|
30
|
+
from ..parsing.services import AnalysisContext
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# =============================================================================
|
|
34
|
+
# Route Extraction Types
|
|
35
|
+
# =============================================================================
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@dataclass
|
|
39
|
+
class ExtractedParameter:
|
|
40
|
+
"""A parameter extracted from a route definition."""
|
|
41
|
+
|
|
42
|
+
name: str
|
|
43
|
+
location: ParameterLocation
|
|
44
|
+
|
|
45
|
+
type_annotation: str | None = None
|
|
46
|
+
required: bool = True
|
|
47
|
+
default_value: str | None = None
|
|
48
|
+
|
|
49
|
+
# Framework-specific details
|
|
50
|
+
alias: str | None = None # Alternative name in request
|
|
51
|
+
description: str | None = None
|
|
52
|
+
|
|
53
|
+
# Validation constraints
|
|
54
|
+
constraints: dict[str, Any] = field(default_factory=dict)
|
|
55
|
+
|
|
56
|
+
# Source location
|
|
57
|
+
code_location: CodeLocation | None = None
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
@dataclass
|
|
61
|
+
class ExtractedBody:
|
|
62
|
+
"""Request body extracted from a route definition."""
|
|
63
|
+
|
|
64
|
+
content_type: str | None = None
|
|
65
|
+
model_name: str | None = None
|
|
66
|
+
model_qualified_name: QualifiedName | None = None
|
|
67
|
+
body_fields: list[str] = field(default_factory=list)
|
|
68
|
+
required: bool = True
|
|
69
|
+
|
|
70
|
+
code_location: CodeLocation | None = None
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@dataclass
|
|
74
|
+
class ExtractedResponse:
|
|
75
|
+
"""Response model extracted from a route definition."""
|
|
76
|
+
|
|
77
|
+
status_code: int = 200
|
|
78
|
+
model_name: str | None = None
|
|
79
|
+
model_qualified_name: QualifiedName | None = None
|
|
80
|
+
content_type: str | None = None
|
|
81
|
+
description: str | None = None
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
@dataclass
|
|
85
|
+
class ExtractedRoute:
|
|
86
|
+
"""
|
|
87
|
+
An entry point extracted from framework code.
|
|
88
|
+
|
|
89
|
+
This is the framework plugin's output for each detected entry point.
|
|
90
|
+
Covers HTTP routes, CLI commands, task definitions, message consumers, etc.
|
|
91
|
+
"""
|
|
92
|
+
|
|
93
|
+
method: HttpMethod
|
|
94
|
+
path: str
|
|
95
|
+
|
|
96
|
+
# Handler
|
|
97
|
+
handler_function: QualifiedName
|
|
98
|
+
handler_location: CodeLocation
|
|
99
|
+
|
|
100
|
+
# Parameters by location
|
|
101
|
+
path_params: list[ExtractedParameter] = field(default_factory=list)
|
|
102
|
+
query_params: list[ExtractedParameter] = field(default_factory=list)
|
|
103
|
+
header_params: list[ExtractedParameter] = field(default_factory=list)
|
|
104
|
+
cookie_params: list[ExtractedParameter] = field(default_factory=list)
|
|
105
|
+
|
|
106
|
+
# Body
|
|
107
|
+
body: ExtractedBody | None = None
|
|
108
|
+
|
|
109
|
+
# Response
|
|
110
|
+
response: ExtractedResponse = field(default_factory=ExtractedResponse)
|
|
111
|
+
|
|
112
|
+
# Framework metadata
|
|
113
|
+
router_name: str | None = None
|
|
114
|
+
tags: list[str] = field(default_factory=list)
|
|
115
|
+
operation_id: str | None = None
|
|
116
|
+
summary: str | None = None
|
|
117
|
+
description: str | None = None
|
|
118
|
+
deprecated: bool = False
|
|
119
|
+
intended_public: bool | None = None
|
|
120
|
+
|
|
121
|
+
# Dependencies used by this route
|
|
122
|
+
dependency_refs: list[str] = field(default_factory=list)
|
|
123
|
+
|
|
124
|
+
# Analysis metadata
|
|
125
|
+
confidence: Confidence = Confidence.HIGH
|
|
126
|
+
path_fully_resolved: bool = True
|
|
127
|
+
path_resolution_source: str = "literal"
|
|
128
|
+
path_dynamic_reason: str | None = None
|
|
129
|
+
extraction_source: str | None = None
|
|
130
|
+
|
|
131
|
+
# Entry point transport category (language-agnostic).
|
|
132
|
+
# "http" for web routes, "cli" for CLI commands, "task" for async tasks, etc.
|
|
133
|
+
kind: str = "http"
|
|
134
|
+
|
|
135
|
+
# Surface category, orthogonal to `kind`: "api" for programmatic endpoints,
|
|
136
|
+
# "ui" for routes that render an HTML page (server-rendered views/templates)
|
|
137
|
+
# rather than serving a machine-readable API response. UI routes are kept
|
|
138
|
+
# in the inventory but excluded from API security testing.
|
|
139
|
+
surface_kind: str = "api"
|
|
140
|
+
|
|
141
|
+
path_fully_resolved: bool = True
|
|
142
|
+
path_resolution_source: str = "literal"
|
|
143
|
+
path_dynamic_reason: str | None = None
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
# =============================================================================
|
|
147
|
+
# Dependency Injection Types
|
|
148
|
+
# =============================================================================
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
@dataclass
|
|
152
|
+
class ExtractedDependency:
|
|
153
|
+
"""
|
|
154
|
+
A dependency injection definition extracted from framework code.
|
|
155
|
+
|
|
156
|
+
This captures FastAPI's Depends(), Spring's @Autowired, etc.
|
|
157
|
+
"""
|
|
158
|
+
|
|
159
|
+
name: str
|
|
160
|
+
qualified_name: QualifiedName
|
|
161
|
+
location: CodeLocation
|
|
162
|
+
|
|
163
|
+
# Type of dependency
|
|
164
|
+
dependency_type: str # "function", "class", "provider", etc.
|
|
165
|
+
|
|
166
|
+
# What does this dependency provide?
|
|
167
|
+
provides_type: str | None = None
|
|
168
|
+
provides_qualified_name: QualifiedName | None = None
|
|
169
|
+
|
|
170
|
+
# Dependencies this depends on (chain)
|
|
171
|
+
depends_on: list[QualifiedName] = field(default_factory=list)
|
|
172
|
+
|
|
173
|
+
# Is this auth-related?
|
|
174
|
+
is_auth_related: bool = False
|
|
175
|
+
auth_scheme_ref: str | None = None
|
|
176
|
+
|
|
177
|
+
# Scope (singleton, request, etc.)
|
|
178
|
+
scope: str | None = None
|
|
179
|
+
|
|
180
|
+
# Analysis metadata
|
|
181
|
+
confidence: Confidence = Confidence.HIGH
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
# =============================================================================
|
|
185
|
+
# Authentication Types
|
|
186
|
+
# =============================================================================
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
@dataclass
|
|
190
|
+
class ExtractedAuthScheme:
|
|
191
|
+
"""
|
|
192
|
+
An authentication scheme extracted from framework code.
|
|
193
|
+
|
|
194
|
+
This captures OAuth2 schemes, API key definitions, etc.
|
|
195
|
+
"""
|
|
196
|
+
|
|
197
|
+
scheme_type: AuthSchemeType
|
|
198
|
+
name: str
|
|
199
|
+
location: CodeLocation
|
|
200
|
+
|
|
201
|
+
# Scheme configuration
|
|
202
|
+
config: dict[str, Any] = field(default_factory=dict)
|
|
203
|
+
|
|
204
|
+
# For OAuth2
|
|
205
|
+
token_url: str | None = None
|
|
206
|
+
authorization_url: str | None = None
|
|
207
|
+
scopes: dict[str, str] = field(default_factory=dict) # scope -> description
|
|
208
|
+
|
|
209
|
+
# For API keys
|
|
210
|
+
header_name: str | None = None
|
|
211
|
+
query_param_name: str | None = None
|
|
212
|
+
cookie_name: str | None = None
|
|
213
|
+
|
|
214
|
+
# Analysis metadata
|
|
215
|
+
confidence: Confidence = Confidence.HIGH
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
@dataclass
|
|
219
|
+
class ExtractedAuthDependency:
|
|
220
|
+
"""
|
|
221
|
+
An authentication dependency/guard extracted from framework code.
|
|
222
|
+
|
|
223
|
+
This captures functions/classes that perform authentication.
|
|
224
|
+
"""
|
|
225
|
+
|
|
226
|
+
name: str
|
|
227
|
+
qualified_name: QualifiedName
|
|
228
|
+
location: CodeLocation
|
|
229
|
+
|
|
230
|
+
dependency_type: AuthDependencyType
|
|
231
|
+
|
|
232
|
+
# What scheme(s) does this use?
|
|
233
|
+
uses_schemes: list[str] = field(default_factory=list)
|
|
234
|
+
|
|
235
|
+
# What other auth dependencies does this depend on?
|
|
236
|
+
depends_on: list[QualifiedName] = field(default_factory=list)
|
|
237
|
+
|
|
238
|
+
# What does this extract/validate?
|
|
239
|
+
extracts_fields: list[str] = field(default_factory=list)
|
|
240
|
+
validates: list[str] = field(default_factory=list)
|
|
241
|
+
|
|
242
|
+
# Role/scope requirements
|
|
243
|
+
requires_roles: list[str] = field(default_factory=list)
|
|
244
|
+
requires_scopes: list[str] = field(default_factory=list)
|
|
245
|
+
|
|
246
|
+
# JWT-specific operations
|
|
247
|
+
jwt_operations: list[str] = field(default_factory=list)
|
|
248
|
+
|
|
249
|
+
# Action-level scope filters (Rails before_action only:/except:, etc.)
|
|
250
|
+
# only_actions: empty = applies to all actions
|
|
251
|
+
# except_actions: empty = no exclusions
|
|
252
|
+
only_actions: list[str] = field(default_factory=list)
|
|
253
|
+
except_actions: list[str] = field(default_factory=list)
|
|
254
|
+
|
|
255
|
+
# Analysis metadata
|
|
256
|
+
confidence: Confidence = Confidence.HIGH
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
@dataclass
|
|
260
|
+
class ExtractedJwtConfig:
|
|
261
|
+
"""JWT configuration extracted from code."""
|
|
262
|
+
|
|
263
|
+
library: str
|
|
264
|
+
|
|
265
|
+
detected: bool = False
|
|
266
|
+
locations: list[CodeLocation] = field(default_factory=list)
|
|
267
|
+
|
|
268
|
+
algorithms: list[str] = field(default_factory=list)
|
|
269
|
+
|
|
270
|
+
validates_signature: bool = False
|
|
271
|
+
validates_expiry: bool = False
|
|
272
|
+
validates_issuer: bool = False
|
|
273
|
+
validates_audience: bool = False
|
|
274
|
+
|
|
275
|
+
secret_source: str | None = None # "env", "config", "hardcoded"
|
|
276
|
+
secret_name: str | None = None
|
|
277
|
+
|
|
278
|
+
confidence: Confidence = Confidence.HIGH
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
# =============================================================================
|
|
282
|
+
# Authorization Types
|
|
283
|
+
# =============================================================================
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
@dataclass
|
|
287
|
+
class ExtractedRouteAuthorization:
|
|
288
|
+
"""Function-level authorization evidence for one route handler.
|
|
289
|
+
|
|
290
|
+
Authorization is not authentication: a handler can require a logged-in
|
|
291
|
+
caller and still let any of them act. This carries what gates the handler
|
|
292
|
+
(roles, policies, permissions) and whether anything does at all, keyed by
|
|
293
|
+
the handler so the analyzer can attach it to the route it belongs to.
|
|
294
|
+
"""
|
|
295
|
+
|
|
296
|
+
#: Qualified name of the handler this evidence belongs to, as the route's
|
|
297
|
+
#: ``handler_function`` records it.
|
|
298
|
+
handler: str
|
|
299
|
+
#: True when a role/policy/permission check gates the handler; False when
|
|
300
|
+
#: the handler was analysed and none was found.
|
|
301
|
+
checked: bool
|
|
302
|
+
roles: list[str] = field(default_factory=list)
|
|
303
|
+
permissions: list[str] = field(default_factory=list)
|
|
304
|
+
#: How the check was found, for the reader of a finding: "attribute",
|
|
305
|
+
#: "body_call", or "" when nothing gates the handler.
|
|
306
|
+
source: str = ""
|
|
307
|
+
confidence: Confidence = Confidence.HIGH
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
# =============================================================================
|
|
311
|
+
# Middleware Types
|
|
312
|
+
# =============================================================================
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
@dataclass
|
|
316
|
+
class ExtractedMiddleware:
|
|
317
|
+
"""
|
|
318
|
+
Middleware/interceptor extracted from framework code.
|
|
319
|
+
|
|
320
|
+
This captures FastAPI middleware, Spring filters/interceptors, etc.
|
|
321
|
+
"""
|
|
322
|
+
|
|
323
|
+
name: str
|
|
324
|
+
location: CodeLocation
|
|
325
|
+
|
|
326
|
+
# Type
|
|
327
|
+
middleware_type: str # "middleware", "filter", "interceptor"
|
|
328
|
+
|
|
329
|
+
# Optional fields
|
|
330
|
+
qualified_name: QualifiedName | None = None
|
|
331
|
+
|
|
332
|
+
# Ordering
|
|
333
|
+
order: int | None = None
|
|
334
|
+
|
|
335
|
+
# What routes does this apply to?
|
|
336
|
+
applies_to_patterns: list[str] = field(default_factory=list)
|
|
337
|
+
applies_to_all: bool = False
|
|
338
|
+
|
|
339
|
+
# Detected operations
|
|
340
|
+
operations: list[str] = field(default_factory=list) # "auth", "cors", "logging", etc.
|
|
341
|
+
|
|
342
|
+
# Analysis metadata
|
|
343
|
+
confidence: Confidence = Confidence.HIGH
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
# =============================================================================
|
|
347
|
+
# Object-Reference Types
|
|
348
|
+
# =============================================================================
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
@dataclass
|
|
352
|
+
class ExtractedObjectReference:
|
|
353
|
+
"""A route parameter that selects a record, and what gates it.
|
|
354
|
+
|
|
355
|
+
The weakness this describes (CWE-639) is not that a handler looks a record
|
|
356
|
+
up by an id from the request — nearly every read endpoint does — but that
|
|
357
|
+
nothing ties the record it returns to the caller who asked for it.
|
|
358
|
+
"""
|
|
359
|
+
|
|
360
|
+
#: Qualified name of the handler, as the route's ``handler_function`` records it.
|
|
361
|
+
handler: str
|
|
362
|
+
#: The request parameter that reaches the lookup.
|
|
363
|
+
parameter: str
|
|
364
|
+
#: The lookup call the parameter reaches, e.g. "GetOrderByIdAsync".
|
|
365
|
+
lookup: str
|
|
366
|
+
#: True when something ties the record to the caller; False when the handler
|
|
367
|
+
#: was analysed and nothing was found.
|
|
368
|
+
ownership_checked: bool
|
|
369
|
+
#: What was found, for the reader of a finding: "owner_comparison",
|
|
370
|
+
#: "scoped_query", "authorization_call", or "" when nothing was.
|
|
371
|
+
ownership_evidence: str = ""
|
|
372
|
+
confidence: Confidence = Confidence.MEDIUM
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
# =============================================================================
|
|
376
|
+
# Shared helpers (used by plugins across languages)
|
|
377
|
+
# =============================================================================
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
# Matches ``{name}`` and ``{name:constraint}`` route-template segments.
|
|
381
|
+
# Captures the parameter name only — the optional ``:constraint`` suffix is
|
|
382
|
+
# stripped. Works for Spring/Micronaut path variables and ASP.NET route
|
|
383
|
+
# templates ({id:int} → id, {slug:regex(...)} → slug).
|
|
384
|
+
_PATH_TEMPLATE_NAME_RE = re.compile(r"\{([^}:]+)(?::[^}]+)?\}")
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
def extract_path_template_names(path: str) -> set[str]:
|
|
388
|
+
"""Return the set of parameter names embedded in a route template.
|
|
389
|
+
|
|
390
|
+
Strips any ``:constraint`` suffix on each segment::
|
|
391
|
+
|
|
392
|
+
"/api/users/{id}/orders/{orderId:int}"
|
|
393
|
+
→ {"id", "orderId"}
|
|
394
|
+
"""
|
|
395
|
+
return set(_PATH_TEMPLATE_NAME_RE.findall(path))
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
# Common middleware/filter "what does it do" keyword classifier.
|
|
399
|
+
# Maps an operation tag → list of substrings to look for in the lowercased
|
|
400
|
+
# (class-name + " " + base-class-names) combination. All three of Spring,
|
|
401
|
+
# Micronaut, and ASP.NET Core had their own near-identical version of this.
|
|
402
|
+
_MIDDLEWARE_OP_KEYWORDS: dict[str, tuple[str, ...]] = {
|
|
403
|
+
"auth": ("auth", "jwt", "token", "security", "login"),
|
|
404
|
+
"cors": ("cors",),
|
|
405
|
+
"logging": ("log",),
|
|
406
|
+
"rate_limiting": ("rate", "throttl"),
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
def infer_middleware_operations(
|
|
411
|
+
class_name: str,
|
|
412
|
+
base_classes: list[str] | tuple[str, ...] = (),
|
|
413
|
+
extra: dict[str, tuple[str, ...]] | None = None,
|
|
414
|
+
) -> list[str]:
|
|
415
|
+
"""Infer what a middleware/filter class does from its name and base classes.
|
|
416
|
+
|
|
417
|
+
Returns the matching operation tags (``["auth"]``, ``["cors", "logging"]``,
|
|
418
|
+
etc.), or ``["custom"]`` if nothing matches. Pass ``extra`` to add
|
|
419
|
+
framework-specific keywords (e.g. ``{"compression": ("compress", "gzip")}``
|
|
420
|
+
for Spring).
|
|
421
|
+
"""
|
|
422
|
+
combined = class_name.lower() + " " + " ".join(b.lower() for b in base_classes)
|
|
423
|
+
keywords = {**_MIDDLEWARE_OP_KEYWORDS, **(extra or {})}
|
|
424
|
+
ops = [op for op, kws in keywords.items() if any(kw in combined for kw in kws)]
|
|
425
|
+
return ops or ["custom"]
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
# Class/symbol name heuristics for "is this auth-related?" — used by
|
|
429
|
+
# dependency extraction to flag service classes that handle authentication.
|
|
430
|
+
# This is the union of what Spring/Micronaut/.NET previously each carried
|
|
431
|
+
# their own copy of. ``identity`` and ``middleware`` are .NET-flavoured
|
|
432
|
+
# but harmless on Java code (Spring rarely names things "Middleware").
|
|
433
|
+
AUTH_NAME_KEYWORDS: frozenset[str] = frozenset(
|
|
434
|
+
{
|
|
435
|
+
"auth",
|
|
436
|
+
"authentication",
|
|
437
|
+
"authorization",
|
|
438
|
+
"security",
|
|
439
|
+
"jwt",
|
|
440
|
+
"token",
|
|
441
|
+
"user",
|
|
442
|
+
"identity",
|
|
443
|
+
"principal",
|
|
444
|
+
"credential",
|
|
445
|
+
"login",
|
|
446
|
+
"filter",
|
|
447
|
+
"middleware",
|
|
448
|
+
}
|
|
449
|
+
)
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
def is_auth_related_name(name: str) -> bool:
|
|
453
|
+
"""True if a class/file/symbol name contains common auth-related substrings."""
|
|
454
|
+
name_lower = name.lower()
|
|
455
|
+
return any(kw in name_lower for kw in AUTH_NAME_KEYWORDS)
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
# =============================================================================
|
|
459
|
+
# Abstract Framework Plugin Base
|
|
460
|
+
# =============================================================================
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
class BaseFrameworkPlugin(ABC):
|
|
464
|
+
"""
|
|
465
|
+
Abstract base class for framework-specific plugins.
|
|
466
|
+
|
|
467
|
+
Each framework (FastAPI, Spring Boot, etc.) extends this
|
|
468
|
+
to provide specialized extraction logic.
|
|
469
|
+
"""
|
|
470
|
+
|
|
471
|
+
# Class-level constants to be overridden
|
|
472
|
+
FRAMEWORK: ClassVar[Framework]
|
|
473
|
+
LANGUAGE: ClassVar[Language]
|
|
474
|
+
|
|
475
|
+
# Import patterns that indicate this framework is used.
|
|
476
|
+
#
|
|
477
|
+
# - ``DETECTION_IMPORTS`` matches full module paths or ``module.symbol`` forms exactly.
|
|
478
|
+
# - ``DETECTION_IMPORT_PREFIXES`` matches the start of a module path — useful for
|
|
479
|
+
# namespace-rooted libraries where any subpackage import counts as detection
|
|
480
|
+
# (e.g. ``"org.springframework"``, ``"io.micronaut"``, ``"Microsoft.AspNetCore"``).
|
|
481
|
+
#
|
|
482
|
+
# Subclasses set at least one of the two; both are checked by the default
|
|
483
|
+
# ``detect()`` implementation. Subclasses with extra detection signals
|
|
484
|
+
# (e.g. attribute-only detection when imports are missing) override ``detect()``.
|
|
485
|
+
DETECTION_IMPORTS: ClassVar[frozenset[str]] = frozenset()
|
|
486
|
+
DETECTION_IMPORT_PREFIXES: ClassVar[tuple[str, ...]] = ()
|
|
487
|
+
|
|
488
|
+
@property
|
|
489
|
+
def framework(self) -> Framework:
|
|
490
|
+
"""The framework this plugin handles."""
|
|
491
|
+
return self.FRAMEWORK
|
|
492
|
+
|
|
493
|
+
@property
|
|
494
|
+
def language(self) -> Language:
|
|
495
|
+
"""The language this framework is for."""
|
|
496
|
+
return self.LANGUAGE
|
|
497
|
+
|
|
498
|
+
def detect(self, parsed_file: ParsedFile) -> bool:
|
|
499
|
+
"""
|
|
500
|
+
Detect if this framework is used in the parsed file.
|
|
501
|
+
|
|
502
|
+
Default implementation checks each import against ``DETECTION_IMPORTS``
|
|
503
|
+
(exact match) and ``DETECTION_IMPORT_PREFIXES`` (prefix match).
|
|
504
|
+
Subclasses override only if they need extra signals beyond imports.
|
|
505
|
+
"""
|
|
506
|
+
prefixes = self.DETECTION_IMPORT_PREFIXES
|
|
507
|
+
for imp in parsed_file.imports:
|
|
508
|
+
module = imp.module or ""
|
|
509
|
+
if module in self.DETECTION_IMPORTS:
|
|
510
|
+
return True
|
|
511
|
+
if prefixes and module.startswith(prefixes):
|
|
512
|
+
return True
|
|
513
|
+
for name in imp.names:
|
|
514
|
+
full_name = f"{module}.{name}" if module else name
|
|
515
|
+
if full_name in self.DETECTION_IMPORTS:
|
|
516
|
+
return True
|
|
517
|
+
return False
|
|
518
|
+
|
|
519
|
+
def registration_status_for_class(
|
|
520
|
+
self,
|
|
521
|
+
cls: ParsedClass,
|
|
522
|
+
parsed_file: ParsedFile,
|
|
523
|
+
) -> RegistrationStatus:
|
|
524
|
+
"""Whether this class registers live HTTP handlers with the framework.
|
|
525
|
+
|
|
526
|
+
Default ``unknown`` — plugins override when they can classify registration.
|
|
527
|
+
"""
|
|
528
|
+
from ..analysis.surface_ledger import RegistrationStatus
|
|
529
|
+
|
|
530
|
+
return RegistrationStatus.UNKNOWN
|
|
531
|
+
|
|
532
|
+
@abstractmethod
|
|
533
|
+
def extract_routes(
|
|
534
|
+
self,
|
|
535
|
+
parsed_file: ParsedFile,
|
|
536
|
+
context: AnalysisContext | None = None,
|
|
537
|
+
) -> list[ExtractedRoute]:
|
|
538
|
+
"""
|
|
539
|
+
Extract HTTP route definitions from the file.
|
|
540
|
+
|
|
541
|
+
Args:
|
|
542
|
+
parsed_file: The parsed source file
|
|
543
|
+
context: Optional analysis context with services
|
|
544
|
+
|
|
545
|
+
Returns:
|
|
546
|
+
List of extracted route definitions
|
|
547
|
+
"""
|
|
548
|
+
...
|
|
549
|
+
|
|
550
|
+
@abstractmethod
|
|
551
|
+
def extract_dependencies(self, parsed_file: ParsedFile) -> list[ExtractedDependency]:
|
|
552
|
+
"""
|
|
553
|
+
Extract dependency injection definitions.
|
|
554
|
+
|
|
555
|
+
Args:
|
|
556
|
+
parsed_file: The parsed source file
|
|
557
|
+
|
|
558
|
+
Returns:
|
|
559
|
+
List of extracted dependency definitions
|
|
560
|
+
"""
|
|
561
|
+
...
|
|
562
|
+
|
|
563
|
+
@abstractmethod
|
|
564
|
+
def extract_auth_schemes(self, parsed_file: ParsedFile) -> list[ExtractedAuthScheme]:
|
|
565
|
+
"""
|
|
566
|
+
Extract authentication scheme definitions.
|
|
567
|
+
|
|
568
|
+
Args:
|
|
569
|
+
parsed_file: The parsed source file
|
|
570
|
+
|
|
571
|
+
Returns:
|
|
572
|
+
List of extracted auth scheme definitions
|
|
573
|
+
"""
|
|
574
|
+
...
|
|
575
|
+
|
|
576
|
+
@abstractmethod
|
|
577
|
+
def extract_auth_dependencies(
|
|
578
|
+
self,
|
|
579
|
+
parsed_file: ParsedFile,
|
|
580
|
+
known_scheme_names: set[str] | None = None,
|
|
581
|
+
**kwargs: Any,
|
|
582
|
+
) -> list[ExtractedAuthDependency]:
|
|
583
|
+
"""
|
|
584
|
+
Extract authentication dependencies/guards.
|
|
585
|
+
|
|
586
|
+
Args:
|
|
587
|
+
parsed_file: The parsed source file
|
|
588
|
+
known_scheme_names: Optional set of auth scheme variable names
|
|
589
|
+
already extracted for this file, used to populate
|
|
590
|
+
``uses_schemes`` on returned dependencies.
|
|
591
|
+
|
|
592
|
+
Returns:
|
|
593
|
+
List of extracted auth dependency definitions
|
|
594
|
+
"""
|
|
595
|
+
...
|
|
596
|
+
|
|
597
|
+
def extract_jwt_config(self, parsed_file: ParsedFile) -> ExtractedJwtConfig | None:
|
|
598
|
+
"""
|
|
599
|
+
Extract JWT configuration.
|
|
600
|
+
|
|
601
|
+
Default implementation returns None. Override if the
|
|
602
|
+
framework has specific JWT patterns.
|
|
603
|
+
"""
|
|
604
|
+
return None
|
|
605
|
+
|
|
606
|
+
def extract_object_references(self, parsed_file: ParsedFile) -> list[ExtractedObjectReference]:
|
|
607
|
+
"""
|
|
608
|
+
Extract route parameters that select a record, and what gates them.
|
|
609
|
+
|
|
610
|
+
Defaults to empty rather than abstract: a plugin that does not analyse
|
|
611
|
+
parameter handling reports nothing, which leaves the route's
|
|
612
|
+
``object_reference`` absent instead of implying the route was checked.
|
|
613
|
+
|
|
614
|
+
Args:
|
|
615
|
+
parsed_file: The parsed source file
|
|
616
|
+
|
|
617
|
+
Returns:
|
|
618
|
+
One entry per handler parameter found selecting a record
|
|
619
|
+
"""
|
|
620
|
+
return []
|
|
621
|
+
|
|
622
|
+
@abstractmethod
|
|
623
|
+
def extract_middleware(self, parsed_file: ParsedFile) -> list[ExtractedMiddleware]:
|
|
624
|
+
"""
|
|
625
|
+
Extract middleware/interceptor definitions.
|
|
626
|
+
|
|
627
|
+
Args:
|
|
628
|
+
parsed_file: The parsed source file
|
|
629
|
+
|
|
630
|
+
Returns:
|
|
631
|
+
List of extracted middleware definitions
|
|
632
|
+
"""
|
|
633
|
+
...
|
|
634
|
+
|
|
635
|
+
def extract_route_authorization(
|
|
636
|
+
self, parsed_file: ParsedFile
|
|
637
|
+
) -> list[ExtractedRouteAuthorization]:
|
|
638
|
+
"""
|
|
639
|
+
Extract function-level authorization evidence per route handler.
|
|
640
|
+
|
|
641
|
+
Defaults to empty rather than abstract: a plugin that does not analyse
|
|
642
|
+
authorization checks reports nothing, which leaves
|
|
643
|
+
``authorization_checked`` None for its routes instead of asserting that
|
|
644
|
+
they are unprotected.
|
|
645
|
+
|
|
646
|
+
Args:
|
|
647
|
+
parsed_file: The parsed source file
|
|
648
|
+
|
|
649
|
+
Returns:
|
|
650
|
+
Authorization evidence, one entry per analysed handler
|
|
651
|
+
"""
|
|
652
|
+
return []
|
|
653
|
+
|
|
654
|
+
# ==========================================================================
|
|
655
|
+
# Helper Methods
|
|
656
|
+
# ==========================================================================
|
|
657
|
+
|
|
658
|
+
def _find_decorator(
|
|
659
|
+
self, function: ParsedFunction, decorator_names: set[str]
|
|
660
|
+
) -> ParsedDecorator | None:
|
|
661
|
+
"""Find a decorator by name on a function."""
|
|
662
|
+
for dec in function.decorators:
|
|
663
|
+
if dec.name in decorator_names:
|
|
664
|
+
return dec
|
|
665
|
+
# Check qualified name
|
|
666
|
+
if dec.qualified_name and dec.qualified_name.name in decorator_names:
|
|
667
|
+
return dec
|
|
668
|
+
return None
|
|
669
|
+
|
|
670
|
+
def _has_decorator(self, function: ParsedFunction, decorator_names: set[str]) -> bool:
|
|
671
|
+
"""Check if function has any of the given decorators."""
|
|
672
|
+
return self._find_decorator(function, decorator_names) is not None
|
|
673
|
+
|
|
674
|
+
def _extract_decorator_arg(
|
|
675
|
+
self, decorator: ParsedDecorator, arg_name: str, default: Any = None
|
|
676
|
+
) -> Any:
|
|
677
|
+
"""Extract an argument value from a decorator."""
|
|
678
|
+
return decorator.arguments.get(arg_name, default)
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
# =============================================================================
|
|
682
|
+
# Framework Plugin Registry
|
|
683
|
+
# =============================================================================
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
class FrameworkPluginRegistry:
|
|
687
|
+
"""
|
|
688
|
+
Registry of available framework plugins.
|
|
689
|
+
|
|
690
|
+
Plugins register themselves and the registry provides
|
|
691
|
+
lookup and detection capabilities.
|
|
692
|
+
"""
|
|
693
|
+
|
|
694
|
+
_plugins: dict[Framework, BaseFrameworkPlugin] = {}
|
|
695
|
+
_by_language: dict[Language, list[BaseFrameworkPlugin]] = {}
|
|
696
|
+
|
|
697
|
+
@classmethod
|
|
698
|
+
def register(cls, plugin: BaseFrameworkPlugin) -> None:
|
|
699
|
+
"""Register a framework plugin."""
|
|
700
|
+
cls._plugins[plugin.framework] = plugin
|
|
701
|
+
|
|
702
|
+
if plugin.language not in cls._by_language:
|
|
703
|
+
cls._by_language[plugin.language] = []
|
|
704
|
+
cls._by_language[plugin.language].append(plugin)
|
|
705
|
+
|
|
706
|
+
@classmethod
|
|
707
|
+
def get_plugin(cls, framework: Framework) -> BaseFrameworkPlugin | None:
|
|
708
|
+
"""Get plugin for a specific framework."""
|
|
709
|
+
return cls._plugins.get(framework)
|
|
710
|
+
|
|
711
|
+
@classmethod
|
|
712
|
+
def get_plugins_for_language(cls, language: Language) -> list[BaseFrameworkPlugin]:
|
|
713
|
+
"""Get all plugins for a language."""
|
|
714
|
+
return cls._by_language.get(language, [])
|
|
715
|
+
|
|
716
|
+
@classmethod
|
|
717
|
+
def detect_frameworks(cls, parsed_file: ParsedFile) -> list[Framework]:
|
|
718
|
+
"""
|
|
719
|
+
Detect which frameworks are used in a parsed file.
|
|
720
|
+
|
|
721
|
+
Returns list of detected frameworks, may be empty.
|
|
722
|
+
"""
|
|
723
|
+
detected = []
|
|
724
|
+
plugins = cls.get_plugins_for_language(parsed_file.language)
|
|
725
|
+
|
|
726
|
+
for plugin in plugins:
|
|
727
|
+
if plugin.detect(parsed_file):
|
|
728
|
+
detected.append(plugin.framework)
|
|
729
|
+
|
|
730
|
+
return detected
|
|
731
|
+
|
|
732
|
+
@classmethod
|
|
733
|
+
def supported_frameworks(cls) -> frozenset[Framework]:
|
|
734
|
+
"""Get set of supported frameworks."""
|
|
735
|
+
return frozenset(cls._plugins.keys())
|