api-gate-keeper 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.
- api_gate_keeper-0.1.0.dist-info/METADATA +654 -0
- api_gate_keeper-0.1.0.dist-info/RECORD +35 -0
- api_gate_keeper-0.1.0.dist-info/WHEEL +5 -0
- api_gate_keeper-0.1.0.dist-info/entry_points.txt +2 -0
- api_gate_keeper-0.1.0.dist-info/licenses/LICENSE +21 -0
- api_gate_keeper-0.1.0.dist-info/top_level.txt +1 -0
- app_test/__init__.py +2 -0
- app_test/auth.py +107 -0
- app_test/categories/__init__.py +24 -0
- app_test/categories/auth_robustness.py +186 -0
- app_test/categories/authz.py +119 -0
- app_test/categories/baseline.py +29 -0
- app_test/categories/bola_matrix.py +340 -0
- app_test/categories/contract_conformance.py +249 -0
- app_test/categories/error_leakage_and_debug_surface.py +270 -0
- app_test/categories/file_upload_security.py +302 -0
- app_test/categories/jwt_security.py +261 -0
- app_test/categories/mass_assignment.py +200 -0
- app_test/categories/owasp.py +127 -0
- app_test/categories/pen_test.py +37 -0
- app_test/categories/privilege_escalation.py +180 -0
- app_test/categories/rate_limit_and_resource_abuse.py +240 -0
- app_test/categories/schema_contract_and_validation.py +297 -0
- app_test/categories/security_misc.py +99 -0
- app_test/categories/soak_profile.py +255 -0
- app_test/categories/sql_injection.py +36 -0
- app_test/categories/ssrf_and_redirects.py +280 -0
- app_test/categories/token_security.py +195 -0
- app_test/categories/workflow_chains.py +437 -0
- app_test/cli.py +1085 -0
- app_test/config.py +19 -0
- app_test/discovery.py +269 -0
- app_test/executor.py +184 -0
- app_test/models.py +54 -0
- app_test/report.py +628 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import httpx
|
|
4
|
+
|
|
5
|
+
from app_test.executor import execute_endpoint
|
|
6
|
+
from app_test.models import EndpointSpec, ExecutionResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
MUTATING_METHODS = {"POST", "PUT", "PATCH"}
|
|
10
|
+
MAX_TARGETS = 20
|
|
11
|
+
UNKNOWN_FIELD_NAME = "__apptest_unknown_field__"
|
|
12
|
+
UNKNOWN_FIELD_VALUE = "__apptest_unknown_value__"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _sample_value(schema_type: str) -> str | int | float | bool | list[str] | dict[str, str] | None:
|
|
16
|
+
if schema_type == "integer":
|
|
17
|
+
return 1
|
|
18
|
+
if schema_type == "number":
|
|
19
|
+
return 1.0
|
|
20
|
+
if schema_type == "boolean":
|
|
21
|
+
return True
|
|
22
|
+
if schema_type == "array":
|
|
23
|
+
return ["sample"]
|
|
24
|
+
if schema_type == "object":
|
|
25
|
+
return {"sample": "value"}
|
|
26
|
+
return "sample"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _invalid_value(schema_type: str) -> str | int | float | bool | list[str] | dict[str, str] | None:
|
|
30
|
+
if schema_type in {"integer", "number"}:
|
|
31
|
+
return "wrong-type"
|
|
32
|
+
if schema_type == "boolean":
|
|
33
|
+
return "wrong-type"
|
|
34
|
+
if schema_type == "array":
|
|
35
|
+
return "wrong-type"
|
|
36
|
+
if schema_type == "object":
|
|
37
|
+
return "wrong-type"
|
|
38
|
+
if schema_type == "string":
|
|
39
|
+
return ["wrong-type"]
|
|
40
|
+
return None
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _json_schema(endpoint: EndpointSpec) -> dict[str, object]:
|
|
44
|
+
return (
|
|
45
|
+
endpoint.request_body.get("content", {})
|
|
46
|
+
.get("application/json", {})
|
|
47
|
+
.get("schema", {})
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _json_properties(endpoint: EndpointSpec) -> dict[str, dict[str, object]]:
|
|
52
|
+
properties = _json_schema(endpoint).get("properties", {})
|
|
53
|
+
return properties if isinstance(properties, dict) else {}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _required_fields(endpoint: EndpointSpec) -> list[str]:
|
|
57
|
+
required = _json_schema(endpoint).get("required", [])
|
|
58
|
+
return [field for field in required if isinstance(field, str)] if isinstance(required, list) else []
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _candidate_targets(endpoints: list[EndpointSpec]) -> list[EndpointSpec]:
|
|
62
|
+
targets: list[EndpointSpec] = []
|
|
63
|
+
for endpoint in endpoints:
|
|
64
|
+
if endpoint.method.upper() not in MUTATING_METHODS:
|
|
65
|
+
continue
|
|
66
|
+
if not endpoint.requires_auth:
|
|
67
|
+
continue
|
|
68
|
+
if not _json_properties(endpoint):
|
|
69
|
+
continue
|
|
70
|
+
targets.append(endpoint)
|
|
71
|
+
return targets[:MAX_TARGETS]
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _base_payload(endpoint: EndpointSpec) -> dict[str, object]:
|
|
75
|
+
payload: dict[str, object] = {}
|
|
76
|
+
for key, prop in _json_properties(endpoint).items():
|
|
77
|
+
prop_type = "string"
|
|
78
|
+
if isinstance(prop, dict):
|
|
79
|
+
prop_type = str(prop.get("type", "string"))
|
|
80
|
+
payload[key] = _sample_value(prop_type)
|
|
81
|
+
return payload
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _normalize_result(
|
|
85
|
+
result: ExecutionResult,
|
|
86
|
+
*,
|
|
87
|
+
test_name: str,
|
|
88
|
+
success_statuses: set[int],
|
|
89
|
+
fail_message: str,
|
|
90
|
+
skip_message: str | None = None,
|
|
91
|
+
reflection_token: str | None = None,
|
|
92
|
+
) -> ExecutionResult:
|
|
93
|
+
if result.status == "skipped":
|
|
94
|
+
return ExecutionResult(
|
|
95
|
+
category="schema_contract_and_validation",
|
|
96
|
+
test_name=test_name,
|
|
97
|
+
service=result.service,
|
|
98
|
+
method=result.method,
|
|
99
|
+
url=result.url,
|
|
100
|
+
status="skipped",
|
|
101
|
+
message=result.message,
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
if result.status == "error":
|
|
105
|
+
return ExecutionResult(
|
|
106
|
+
category="schema_contract_and_validation",
|
|
107
|
+
test_name=test_name,
|
|
108
|
+
service=result.service,
|
|
109
|
+
method=result.method,
|
|
110
|
+
url=result.url,
|
|
111
|
+
status="error",
|
|
112
|
+
http_status=result.http_status,
|
|
113
|
+
latency_ms=result.latency_ms,
|
|
114
|
+
message=result.message,
|
|
115
|
+
retries=result.retries,
|
|
116
|
+
diagnostics=result.diagnostics,
|
|
117
|
+
response_snippet=result.response_snippet,
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
status_code = result.http_status
|
|
121
|
+
snippet = result.response_snippet.lower()
|
|
122
|
+
if status_code in success_statuses:
|
|
123
|
+
status = "pass"
|
|
124
|
+
message = f"Schema validation rejected invalid payload ({status_code})"
|
|
125
|
+
elif status_code is not None and 200 <= status_code < 300:
|
|
126
|
+
if reflection_token and reflection_token.lower() in snippet:
|
|
127
|
+
status = "fail"
|
|
128
|
+
message = fail_message
|
|
129
|
+
elif skip_message is not None:
|
|
130
|
+
status = "skipped"
|
|
131
|
+
message = skip_message
|
|
132
|
+
else:
|
|
133
|
+
status = "fail"
|
|
134
|
+
message = fail_message
|
|
135
|
+
else:
|
|
136
|
+
status = "pass"
|
|
137
|
+
message = f"Schema validation returned non-success status ({status_code})"
|
|
138
|
+
|
|
139
|
+
return ExecutionResult(
|
|
140
|
+
category="schema_contract_and_validation",
|
|
141
|
+
test_name=test_name,
|
|
142
|
+
service=result.service,
|
|
143
|
+
method=result.method,
|
|
144
|
+
url=result.url,
|
|
145
|
+
status=status,
|
|
146
|
+
http_status=result.http_status,
|
|
147
|
+
latency_ms=result.latency_ms,
|
|
148
|
+
message=message,
|
|
149
|
+
retries=result.retries,
|
|
150
|
+
diagnostics=result.diagnostics,
|
|
151
|
+
response_snippet=result.response_snippet,
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def _run_missing_required_field_check(
|
|
156
|
+
client: httpx.Client,
|
|
157
|
+
base_url: str,
|
|
158
|
+
token: str,
|
|
159
|
+
endpoint: EndpointSpec,
|
|
160
|
+
include_unsafe: bool,
|
|
161
|
+
) -> ExecutionResult:
|
|
162
|
+
payload = _base_payload(endpoint)
|
|
163
|
+
required_fields = _required_fields(endpoint)
|
|
164
|
+
if not required_fields:
|
|
165
|
+
return ExecutionResult(
|
|
166
|
+
category="schema_contract_and_validation",
|
|
167
|
+
test_name="required_field_validation_check",
|
|
168
|
+
service=endpoint.service,
|
|
169
|
+
method=endpoint.method.upper(),
|
|
170
|
+
url=f"{base_url.rstrip('/')}{endpoint.path}",
|
|
171
|
+
status="skipped",
|
|
172
|
+
message="No required JSON fields declared for this endpoint",
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
payload.pop(required_fields[0], None)
|
|
176
|
+
base_result = execute_endpoint(
|
|
177
|
+
client=client,
|
|
178
|
+
endpoint=endpoint,
|
|
179
|
+
base_url=base_url,
|
|
180
|
+
token=token,
|
|
181
|
+
category="schema_contract_and_validation",
|
|
182
|
+
test_name="required_field_validation_check",
|
|
183
|
+
include_unsafe=include_unsafe,
|
|
184
|
+
body_override=payload,
|
|
185
|
+
)
|
|
186
|
+
return _normalize_result(
|
|
187
|
+
base_result,
|
|
188
|
+
test_name="required_field_validation_check",
|
|
189
|
+
success_statuses={400, 401, 403, 404, 409, 422},
|
|
190
|
+
fail_message="Required field was missing but request still succeeded",
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _run_wrong_type_check(
|
|
195
|
+
client: httpx.Client,
|
|
196
|
+
base_url: str,
|
|
197
|
+
token: str,
|
|
198
|
+
endpoint: EndpointSpec,
|
|
199
|
+
include_unsafe: bool,
|
|
200
|
+
) -> ExecutionResult:
|
|
201
|
+
payload = _base_payload(endpoint)
|
|
202
|
+
properties = _json_properties(endpoint)
|
|
203
|
+
target_field: str | None = None
|
|
204
|
+
invalid_value: object = None
|
|
205
|
+
for field, prop in properties.items():
|
|
206
|
+
prop_type = str((prop or {}).get("type", "string")) if isinstance(prop, dict) else "string"
|
|
207
|
+
invalid_value = _invalid_value(prop_type)
|
|
208
|
+
if invalid_value is not None:
|
|
209
|
+
target_field = field
|
|
210
|
+
break
|
|
211
|
+
|
|
212
|
+
if target_field is None:
|
|
213
|
+
return ExecutionResult(
|
|
214
|
+
category="schema_contract_and_validation",
|
|
215
|
+
test_name="wrong_type_validation_check",
|
|
216
|
+
service=endpoint.service,
|
|
217
|
+
method=endpoint.method.upper(),
|
|
218
|
+
url=f"{base_url.rstrip('/')}{endpoint.path}",
|
|
219
|
+
status="skipped",
|
|
220
|
+
message="No suitable typed JSON field discovered for wrong-type validation",
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
payload[target_field] = invalid_value
|
|
224
|
+
base_result = execute_endpoint(
|
|
225
|
+
client=client,
|
|
226
|
+
endpoint=endpoint,
|
|
227
|
+
base_url=base_url,
|
|
228
|
+
token=token,
|
|
229
|
+
category="schema_contract_and_validation",
|
|
230
|
+
test_name="wrong_type_validation_check",
|
|
231
|
+
include_unsafe=include_unsafe,
|
|
232
|
+
body_override=payload,
|
|
233
|
+
)
|
|
234
|
+
return _normalize_result(
|
|
235
|
+
base_result,
|
|
236
|
+
test_name="wrong_type_validation_check",
|
|
237
|
+
success_statuses={400, 401, 403, 404, 409, 422},
|
|
238
|
+
fail_message=f"Wrong-type value for '{target_field}' was accepted",
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def _run_unknown_field_check(
|
|
243
|
+
client: httpx.Client,
|
|
244
|
+
base_url: str,
|
|
245
|
+
token: str,
|
|
246
|
+
endpoint: EndpointSpec,
|
|
247
|
+
include_unsafe: bool,
|
|
248
|
+
) -> ExecutionResult:
|
|
249
|
+
payload = _base_payload(endpoint)
|
|
250
|
+
payload[UNKNOWN_FIELD_NAME] = UNKNOWN_FIELD_VALUE
|
|
251
|
+
base_result = execute_endpoint(
|
|
252
|
+
client=client,
|
|
253
|
+
endpoint=endpoint,
|
|
254
|
+
base_url=base_url,
|
|
255
|
+
token=token,
|
|
256
|
+
category="schema_contract_and_validation",
|
|
257
|
+
test_name="unknown_field_handling_check",
|
|
258
|
+
include_unsafe=include_unsafe,
|
|
259
|
+
body_override=payload,
|
|
260
|
+
)
|
|
261
|
+
return _normalize_result(
|
|
262
|
+
base_result,
|
|
263
|
+
test_name="unknown_field_handling_check",
|
|
264
|
+
success_statuses={400, 401, 403, 404, 409, 422},
|
|
265
|
+
fail_message="Unknown field was accepted and reflected in the response",
|
|
266
|
+
skip_message="Unknown field request succeeded but response did not reflect the injected field",
|
|
267
|
+
reflection_token=UNKNOWN_FIELD_VALUE,
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def run(
|
|
272
|
+
client: httpx.Client,
|
|
273
|
+
base_url: str,
|
|
274
|
+
token: str,
|
|
275
|
+
endpoints: list[EndpointSpec],
|
|
276
|
+
include_unsafe: bool,
|
|
277
|
+
) -> list[ExecutionResult]:
|
|
278
|
+
targets = _candidate_targets(endpoints)
|
|
279
|
+
if not targets:
|
|
280
|
+
return [
|
|
281
|
+
ExecutionResult(
|
|
282
|
+
category="schema_contract_and_validation",
|
|
283
|
+
test_name="schema_validation_target_discovery",
|
|
284
|
+
service="gateway",
|
|
285
|
+
method="POST",
|
|
286
|
+
url=base_url.rstrip("/"),
|
|
287
|
+
status="skipped",
|
|
288
|
+
message="No authenticated JSON mutation endpoints discovered for schema validation checks",
|
|
289
|
+
)
|
|
290
|
+
]
|
|
291
|
+
|
|
292
|
+
results: list[ExecutionResult] = []
|
|
293
|
+
for endpoint in targets:
|
|
294
|
+
results.append(_run_missing_required_field_check(client, base_url, token, endpoint, include_unsafe))
|
|
295
|
+
results.append(_run_wrong_type_check(client, base_url, token, endpoint, include_unsafe))
|
|
296
|
+
results.append(_run_unknown_field_check(client, base_url, token, endpoint, include_unsafe))
|
|
297
|
+
return results
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import httpx
|
|
4
|
+
|
|
5
|
+
from app_test.models import EndpointSpec, ExecutionResult
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def run(
|
|
9
|
+
client: httpx.Client,
|
|
10
|
+
base_url: str,
|
|
11
|
+
token: str,
|
|
12
|
+
endpoints: list[EndpointSpec],
|
|
13
|
+
include_unsafe: bool,
|
|
14
|
+
) -> list[ExecutionResult]:
|
|
15
|
+
results: list[ExecutionResult] = []
|
|
16
|
+
|
|
17
|
+
try:
|
|
18
|
+
preflight = client.options(
|
|
19
|
+
f"{base_url.rstrip('/')}/api/data/auth/login",
|
|
20
|
+
headers={
|
|
21
|
+
"Origin": "https://evil.example",
|
|
22
|
+
"Access-Control-Request-Method": "POST",
|
|
23
|
+
},
|
|
24
|
+
)
|
|
25
|
+
allowed_origin = preflight.headers.get("access-control-allow-origin", "")
|
|
26
|
+
if allowed_origin == "*":
|
|
27
|
+
results.append(
|
|
28
|
+
ExecutionResult(
|
|
29
|
+
category="security_misc",
|
|
30
|
+
test_name="cors_wildcard_check",
|
|
31
|
+
service="data",
|
|
32
|
+
method="OPTIONS",
|
|
33
|
+
url=str(preflight.request.url),
|
|
34
|
+
status="fail",
|
|
35
|
+
http_status=preflight.status_code,
|
|
36
|
+
message="CORS allow-origin wildcard detected",
|
|
37
|
+
)
|
|
38
|
+
)
|
|
39
|
+
else:
|
|
40
|
+
results.append(
|
|
41
|
+
ExecutionResult(
|
|
42
|
+
category="security_misc",
|
|
43
|
+
test_name="cors_wildcard_check",
|
|
44
|
+
service="data",
|
|
45
|
+
method="OPTIONS",
|
|
46
|
+
url=str(preflight.request.url),
|
|
47
|
+
status="pass",
|
|
48
|
+
http_status=preflight.status_code,
|
|
49
|
+
message=f"allow-origin={allowed_origin or 'not-set'}",
|
|
50
|
+
)
|
|
51
|
+
)
|
|
52
|
+
except Exception as exc:
|
|
53
|
+
results.append(
|
|
54
|
+
ExecutionResult(
|
|
55
|
+
category="security_misc",
|
|
56
|
+
test_name="cors_wildcard_check",
|
|
57
|
+
service="data",
|
|
58
|
+
method="OPTIONS",
|
|
59
|
+
url=f"{base_url.rstrip('/')}/api/data/auth/login",
|
|
60
|
+
status="error",
|
|
61
|
+
message=str(exc),
|
|
62
|
+
)
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
for endpoint in endpoints:
|
|
66
|
+
if endpoint.path.endswith("/health"):
|
|
67
|
+
try:
|
|
68
|
+
responses = [
|
|
69
|
+
client.get(f"{base_url.rstrip('/')}{endpoint.path}")
|
|
70
|
+
for _ in range(5)
|
|
71
|
+
]
|
|
72
|
+
all_ok = all(r.status_code < 500 for r in responses)
|
|
73
|
+
results.append(
|
|
74
|
+
ExecutionResult(
|
|
75
|
+
category="security_misc",
|
|
76
|
+
test_name="basic_rate_probe",
|
|
77
|
+
service=endpoint.service,
|
|
78
|
+
method="GET",
|
|
79
|
+
url=f"{base_url.rstrip('/')}{endpoint.path}",
|
|
80
|
+
status="pass" if all_ok else "fail",
|
|
81
|
+
http_status=responses[-1].status_code,
|
|
82
|
+
message="5 rapid requests sent",
|
|
83
|
+
)
|
|
84
|
+
)
|
|
85
|
+
except Exception as exc:
|
|
86
|
+
results.append(
|
|
87
|
+
ExecutionResult(
|
|
88
|
+
category="security_misc",
|
|
89
|
+
test_name="basic_rate_probe",
|
|
90
|
+
service=endpoint.service,
|
|
91
|
+
method="GET",
|
|
92
|
+
url=f"{base_url.rstrip('/')}{endpoint.path}",
|
|
93
|
+
status="error",
|
|
94
|
+
message=str(exc),
|
|
95
|
+
)
|
|
96
|
+
)
|
|
97
|
+
break
|
|
98
|
+
|
|
99
|
+
return results
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
import statistics
|
|
5
|
+
import time
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
import httpx
|
|
9
|
+
|
|
10
|
+
from app_test.auth import auth_headers_from_token
|
|
11
|
+
from app_test.executor import _materialize_url_and_params
|
|
12
|
+
from app_test.models import EndpointSpec, ExecutionResult
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
MAX_TARGETS = 15
|
|
16
|
+
DEFAULT_REQUESTS_PER_ENDPOINT = 12
|
|
17
|
+
DEFAULT_MAX_ALLOWED_5XX_RATE = 0.15
|
|
18
|
+
DEFAULT_MAX_ALLOWED_EXCEPTION_RATE = 0.05
|
|
19
|
+
DEFAULT_MAX_ALLOWED_P95_LATENCY_MS = 2500.0
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# ---------------------------------------------------------------------------
|
|
23
|
+
# Target discovery
|
|
24
|
+
# ---------------------------------------------------------------------------
|
|
25
|
+
|
|
26
|
+
def _safe_targets(endpoints: list[EndpointSpec]) -> list[EndpointSpec]:
|
|
27
|
+
return [
|
|
28
|
+
ep
|
|
29
|
+
for ep in endpoints
|
|
30
|
+
if ep.method.upper() in {"GET", "HEAD", "OPTIONS"}
|
|
31
|
+
][:MAX_TARGETS]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
# ---------------------------------------------------------------------------
|
|
35
|
+
# Stats helpers
|
|
36
|
+
# ---------------------------------------------------------------------------
|
|
37
|
+
|
|
38
|
+
def _percentile(values: list[float], pct: float) -> float:
|
|
39
|
+
if not values:
|
|
40
|
+
return 0.0
|
|
41
|
+
if len(values) == 1:
|
|
42
|
+
return values[0]
|
|
43
|
+
|
|
44
|
+
ordered = sorted(values)
|
|
45
|
+
rank = (pct / 100.0) * (len(ordered) - 1)
|
|
46
|
+
low = math.floor(rank)
|
|
47
|
+
high = math.ceil(rank)
|
|
48
|
+
if low == high:
|
|
49
|
+
return ordered[low]
|
|
50
|
+
frac = rank - low
|
|
51
|
+
return ordered[low] * (1 - frac) + ordered[high] * frac
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _result(
|
|
55
|
+
endpoint: EndpointSpec,
|
|
56
|
+
base_url: str,
|
|
57
|
+
status: str,
|
|
58
|
+
test_name: str,
|
|
59
|
+
http_status: int | None,
|
|
60
|
+
latency_ms: float | None,
|
|
61
|
+
message: str,
|
|
62
|
+
snippet: str = "",
|
|
63
|
+
) -> ExecutionResult:
|
|
64
|
+
return ExecutionResult(
|
|
65
|
+
category="soak_profile",
|
|
66
|
+
test_name=test_name,
|
|
67
|
+
service=endpoint.service,
|
|
68
|
+
method=endpoint.method,
|
|
69
|
+
url=f"{base_url.rstrip('/')}{endpoint.path}",
|
|
70
|
+
status=status,
|
|
71
|
+
http_status=http_status,
|
|
72
|
+
latency_ms=latency_ms,
|
|
73
|
+
message=message,
|
|
74
|
+
response_snippet=snippet[:400],
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
# ---------------------------------------------------------------------------
|
|
79
|
+
# Per-endpoint soak probe
|
|
80
|
+
# ---------------------------------------------------------------------------
|
|
81
|
+
|
|
82
|
+
def _probe_endpoint(
|
|
83
|
+
client: httpx.Client,
|
|
84
|
+
endpoint: EndpointSpec,
|
|
85
|
+
base_url: str,
|
|
86
|
+
token: str | None,
|
|
87
|
+
requests_per_endpoint: int,
|
|
88
|
+
max_allowed_5xx_rate: float,
|
|
89
|
+
max_allowed_exception_rate: float,
|
|
90
|
+
max_allowed_p95_latency_ms: float,
|
|
91
|
+
) -> ExecutionResult:
|
|
92
|
+
method = endpoint.method.upper()
|
|
93
|
+
url, query_params = _materialize_url_and_params(base_url, endpoint)
|
|
94
|
+
|
|
95
|
+
headers: dict[str, str] = {}
|
|
96
|
+
if endpoint.requires_auth and token:
|
|
97
|
+
headers.update(auth_headers_from_token(token))
|
|
98
|
+
|
|
99
|
+
status_counts: dict[int, int] = {}
|
|
100
|
+
latencies: list[float] = []
|
|
101
|
+
exceptions = 0
|
|
102
|
+
last_status: int | None = None
|
|
103
|
+
last_snippet = ""
|
|
104
|
+
|
|
105
|
+
for _ in range(requests_per_endpoint):
|
|
106
|
+
t0 = time.monotonic()
|
|
107
|
+
try:
|
|
108
|
+
resp = client.request(method=method, url=url, params=query_params, headers=headers)
|
|
109
|
+
latency_ms = (time.monotonic() - t0) * 1000
|
|
110
|
+
latencies.append(latency_ms)
|
|
111
|
+
status = resp.status_code
|
|
112
|
+
status_counts[status] = status_counts.get(status, 0) + 1
|
|
113
|
+
last_status = status
|
|
114
|
+
last_snippet = " ".join(resp.text.split())[:400]
|
|
115
|
+
except Exception:
|
|
116
|
+
exceptions += 1
|
|
117
|
+
|
|
118
|
+
total = requests_per_endpoint
|
|
119
|
+
count_5xx = sum(v for k, v in status_counts.items() if 500 <= k < 600)
|
|
120
|
+
count_2xx = sum(v for k, v in status_counts.items() if 200 <= k < 300)
|
|
121
|
+
count_429 = status_counts.get(429, 0)
|
|
122
|
+
|
|
123
|
+
exception_rate = exceptions / total
|
|
124
|
+
error_5xx_rate = count_5xx / total
|
|
125
|
+
|
|
126
|
+
if latencies:
|
|
127
|
+
p50 = statistics.median(latencies)
|
|
128
|
+
p95 = _percentile(latencies, 95)
|
|
129
|
+
else:
|
|
130
|
+
p50 = 0.0
|
|
131
|
+
p95 = 0.0
|
|
132
|
+
|
|
133
|
+
status_summary = ", ".join(f"{code}:{count}" for code, count in sorted(status_counts.items())) or "none"
|
|
134
|
+
message = (
|
|
135
|
+
f"samples={total}, statuses=[{status_summary}], exceptions={exceptions}, "
|
|
136
|
+
f"2xx={count_2xx}, 429={count_429}, 5xx={count_5xx}, "
|
|
137
|
+
f"p50={p50:.1f}ms, p95={p95:.1f}ms"
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
# If auth-required endpoint never returns 2xx, mark as skipped to avoid false alarms
|
|
141
|
+
# when auth mode intentionally cannot access protected data.
|
|
142
|
+
if endpoint.requires_auth and count_2xx == 0:
|
|
143
|
+
return _result(
|
|
144
|
+
endpoint,
|
|
145
|
+
base_url,
|
|
146
|
+
"skipped",
|
|
147
|
+
"soak_endpoint_access",
|
|
148
|
+
last_status,
|
|
149
|
+
p95,
|
|
150
|
+
f"No successful responses on auth-required endpoint under current auth context. {message}",
|
|
151
|
+
last_snippet,
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
# Fail only on clear reliability/performance instability.
|
|
155
|
+
if exception_rate > max_allowed_exception_rate:
|
|
156
|
+
return _result(
|
|
157
|
+
endpoint,
|
|
158
|
+
base_url,
|
|
159
|
+
"fail",
|
|
160
|
+
"soak_endpoint_stability",
|
|
161
|
+
last_status,
|
|
162
|
+
p95,
|
|
163
|
+
f"High transport exception rate ({exception_rate:.1%} > {max_allowed_exception_rate:.1%}). {message}",
|
|
164
|
+
last_snippet,
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
if error_5xx_rate > max_allowed_5xx_rate:
|
|
168
|
+
return _result(
|
|
169
|
+
endpoint,
|
|
170
|
+
base_url,
|
|
171
|
+
"fail",
|
|
172
|
+
"soak_endpoint_stability",
|
|
173
|
+
last_status,
|
|
174
|
+
p95,
|
|
175
|
+
f"High 5xx rate ({error_5xx_rate:.1%} > {max_allowed_5xx_rate:.1%}). {message}",
|
|
176
|
+
last_snippet,
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
if p95 > max_allowed_p95_latency_ms:
|
|
180
|
+
return _result(
|
|
181
|
+
endpoint,
|
|
182
|
+
base_url,
|
|
183
|
+
"fail",
|
|
184
|
+
"soak_endpoint_latency",
|
|
185
|
+
last_status,
|
|
186
|
+
p95,
|
|
187
|
+
f"High p95 latency ({p95:.1f}ms > {max_allowed_p95_latency_ms:.1f}ms). {message}",
|
|
188
|
+
last_snippet,
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
return _result(
|
|
192
|
+
endpoint,
|
|
193
|
+
base_url,
|
|
194
|
+
"pass",
|
|
195
|
+
"soak_endpoint_stability",
|
|
196
|
+
last_status,
|
|
197
|
+
p95,
|
|
198
|
+
f"Endpoint remained stable under repeated safe requests. {message}",
|
|
199
|
+
last_snippet,
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
# ---------------------------------------------------------------------------
|
|
204
|
+
# Category entry point
|
|
205
|
+
# ---------------------------------------------------------------------------
|
|
206
|
+
|
|
207
|
+
def run(
|
|
208
|
+
client: httpx.Client,
|
|
209
|
+
base_url: str,
|
|
210
|
+
token: str | None,
|
|
211
|
+
endpoints: list[EndpointSpec],
|
|
212
|
+
include_unsafe: bool,
|
|
213
|
+
soak_samples: int | None = None,
|
|
214
|
+
soak_max_5xx_rate: float | None = None,
|
|
215
|
+
soak_max_exception_rate: float | None = None,
|
|
216
|
+
soak_max_p95_ms: float | None = None,
|
|
217
|
+
) -> list[ExecutionResult]:
|
|
218
|
+
del include_unsafe
|
|
219
|
+
|
|
220
|
+
targets = _safe_targets(endpoints)
|
|
221
|
+
if not targets:
|
|
222
|
+
return [
|
|
223
|
+
ExecutionResult(
|
|
224
|
+
category="soak_profile",
|
|
225
|
+
test_name="soak_profile_target_discovery",
|
|
226
|
+
service="gateway",
|
|
227
|
+
method="GET",
|
|
228
|
+
url=base_url.rstrip("/"),
|
|
229
|
+
status="skipped",
|
|
230
|
+
message="No safe endpoints discovered for soak profiling",
|
|
231
|
+
)
|
|
232
|
+
]
|
|
233
|
+
|
|
234
|
+
results: list[ExecutionResult] = []
|
|
235
|
+
requests_per_endpoint = soak_samples if soak_samples is not None else DEFAULT_REQUESTS_PER_ENDPOINT
|
|
236
|
+
max_allowed_5xx_rate = soak_max_5xx_rate if soak_max_5xx_rate is not None else DEFAULT_MAX_ALLOWED_5XX_RATE
|
|
237
|
+
max_allowed_exception_rate = (
|
|
238
|
+
soak_max_exception_rate if soak_max_exception_rate is not None else DEFAULT_MAX_ALLOWED_EXCEPTION_RATE
|
|
239
|
+
)
|
|
240
|
+
max_allowed_p95_latency_ms = soak_max_p95_ms if soak_max_p95_ms is not None else DEFAULT_MAX_ALLOWED_P95_LATENCY_MS
|
|
241
|
+
|
|
242
|
+
for endpoint in targets:
|
|
243
|
+
results.append(
|
|
244
|
+
_probe_endpoint(
|
|
245
|
+
client,
|
|
246
|
+
endpoint,
|
|
247
|
+
base_url,
|
|
248
|
+
token,
|
|
249
|
+
requests_per_endpoint,
|
|
250
|
+
max_allowed_5xx_rate,
|
|
251
|
+
max_allowed_exception_rate,
|
|
252
|
+
max_allowed_p95_latency_ms,
|
|
253
|
+
)
|
|
254
|
+
)
|
|
255
|
+
return results
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import httpx
|
|
4
|
+
|
|
5
|
+
from app_test.executor import execute_endpoint
|
|
6
|
+
from app_test.models import EndpointSpec, ExecutionResult
|
|
7
|
+
|
|
8
|
+
SQLI_PAYLOADS = [
|
|
9
|
+
"' OR '1'='1",
|
|
10
|
+
"admin' --",
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def run(
|
|
15
|
+
client: httpx.Client,
|
|
16
|
+
base_url: str,
|
|
17
|
+
token: str,
|
|
18
|
+
endpoints: list[EndpointSpec],
|
|
19
|
+
include_unsafe: bool,
|
|
20
|
+
) -> list[ExecutionResult]:
|
|
21
|
+
results: list[ExecutionResult] = []
|
|
22
|
+
for endpoint in endpoints:
|
|
23
|
+
for payload in SQLI_PAYLOADS:
|
|
24
|
+
results.append(
|
|
25
|
+
execute_endpoint(
|
|
26
|
+
client=client,
|
|
27
|
+
endpoint=endpoint,
|
|
28
|
+
base_url=base_url,
|
|
29
|
+
token=token,
|
|
30
|
+
category="sql_injection",
|
|
31
|
+
test_name=f"sqli_payload:{payload}",
|
|
32
|
+
include_unsafe=include_unsafe,
|
|
33
|
+
attack_payload=payload,
|
|
34
|
+
)
|
|
35
|
+
)
|
|
36
|
+
return results
|