@kirrosh/zond 0.21.0 → 0.23.0
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.
- package/CHANGELOG.md +758 -3
- package/README.md +78 -15
- package/package.json +17 -10
- package/src/cli/argv.ts +122 -0
- package/src/cli/commands/add-api.ts +134 -0
- package/src/cli/commands/api/annotate/idempotency.ts +59 -0
- package/src/cli/commands/api/annotate/index.ts +525 -0
- package/src/cli/commands/api/annotate/lifecycle.ts +74 -0
- package/src/cli/commands/api/annotate/overlay.ts +206 -0
- package/src/cli/commands/api/annotate/pagination.ts +60 -0
- package/src/cli/commands/api/annotate/prompts.ts +183 -0
- package/src/cli/commands/api/annotate/readback.ts +58 -0
- package/src/cli/commands/api/annotate/resources.ts +91 -0
- package/src/cli/commands/api/annotate/seed-bodies.ts +61 -0
- package/src/cli/commands/audit.ts +480 -0
- package/src/cli/commands/bootstrap.ts +710 -0
- package/src/cli/commands/catalog.ts +35 -0
- package/src/cli/commands/check.ts +348 -0
- package/src/cli/commands/checks.ts +756 -0
- package/src/cli/commands/ci-init.ts +55 -6
- package/src/cli/commands/clean.ts +212 -0
- package/src/cli/commands/cleanup.ts +262 -0
- package/src/cli/commands/completions.ts +192 -0
- package/src/cli/commands/coverage.ts +605 -132
- package/src/cli/commands/db.ts +180 -8
- package/src/cli/commands/describe.ts +37 -2
- package/src/cli/commands/discover.ts +1236 -0
- package/src/cli/commands/doctor.ts +607 -0
- package/src/cli/commands/fixtures.ts +402 -0
- package/src/cli/commands/generate.ts +420 -47
- package/src/cli/commands/init/agents-md.ts +61 -0
- package/src/cli/commands/init/bootstrap.ts +108 -0
- package/src/cli/commands/init/index.ts +244 -0
- package/src/cli/commands/init/skills.ts +98 -0
- package/src/cli/commands/init/templates/agents.md +77 -0
- package/src/cli/commands/init/templates/markdown.d.ts +4 -0
- package/src/cli/commands/init/templates/skills/zond-checks.md +397 -0
- package/src/cli/commands/init/templates/skills/zond-triage.md +210 -0
- package/src/cli/commands/init/templates/skills/zond.md +651 -0
- package/src/cli/commands/init/templates/zond-config.yml +14 -0
- package/src/cli/commands/prepare-fixtures.ts +135 -0
- package/src/cli/commands/probe/mass-assignment.ts +503 -0
- package/src/cli/commands/probe/security.ts +454 -0
- package/src/cli/commands/probe/static.ts +255 -0
- package/src/cli/commands/probe/webhooks.ts +161 -0
- package/src/cli/commands/probe.ts +459 -0
- package/src/cli/commands/reference.ts +87 -0
- package/src/cli/commands/refresh-api.ts +169 -0
- package/src/cli/commands/remove-api.ts +150 -0
- package/src/cli/commands/report-bundle.ts +318 -0
- package/src/cli/commands/report.ts +241 -0
- package/src/cli/commands/request.ts +379 -4
- package/src/cli/commands/run.ts +911 -33
- package/src/cli/commands/session.ts +244 -0
- package/src/cli/commands/use.ts +74 -0
- package/src/cli/index.ts +36 -607
- package/src/cli/json-envelope.ts +112 -3
- package/src/cli/json-schemas.ts +263 -0
- package/src/cli/program.ts +218 -0
- package/src/cli/resolve.ts +105 -0
- package/src/cli/status-filter.ts +124 -0
- package/src/cli/util/api-context.ts +85 -0
- package/src/cli/version.ts +8 -0
- package/src/core/anti-fp/bootstrap.ts +34 -0
- package/src/core/anti-fp/index.ts +33 -0
- package/src/core/anti-fp/registry.ts +44 -0
- package/src/core/anti-fp/rules/baseline-echo.ts +74 -0
- package/src/core/anti-fp/rules/schemathesis/body_negation_becomes_valid.ts +52 -0
- package/src/core/anti-fp/rules/schemathesis/coverage_phase_boundary_positive.ts +38 -0
- package/src/core/anti-fp/rules/schemathesis/has_unverifiable_mutations.ts +35 -0
- package/src/core/anti-fp/rules/schemathesis/index.ts +24 -0
- package/src/core/anti-fp/rules/schemathesis/string_type_mutation_becomes_valid.ts +53 -0
- package/src/core/anti-fp/rules/subscription-gated/index.ts +11 -0
- package/src/core/anti-fp/rules/subscription-gated/paid-plan-403.ts +75 -0
- package/src/core/anti-fp/types.ts +68 -0
- package/src/core/checks/checks/_crud-helpers.ts +133 -0
- package/src/core/checks/checks/_negative_mutator.ts +133 -0
- package/src/core/checks/checks/_readback-helpers.ts +133 -0
- package/src/core/checks/checks/content_type_conformance.ts +39 -0
- package/src/core/checks/checks/cross_call_references.ts +134 -0
- package/src/core/checks/checks/ensure_resource_availability.ts +62 -0
- package/src/core/checks/checks/idempotency_replay.ts +246 -0
- package/src/core/checks/checks/ignored_auth.ts +211 -0
- package/src/core/checks/checks/index.ts +65 -0
- package/src/core/checks/checks/lifecycle_transitions.ts +273 -0
- package/src/core/checks/checks/missing_required_header.ts +40 -0
- package/src/core/checks/checks/negative_data_rejection.ts +45 -0
- package/src/core/checks/checks/not_a_server_error.ts +27 -0
- package/src/core/checks/checks/open_cors_on_sensitive.ts +131 -0
- package/src/core/checks/checks/pagination_invariants.ts +238 -0
- package/src/core/checks/checks/positive_data_acceptance.ts +36 -0
- package/src/core/checks/checks/rate_limit_headers_absent.ts +77 -0
- package/src/core/checks/checks/response_headers_conformance.ts +74 -0
- package/src/core/checks/checks/response_schema_conformance.ts +30 -0
- package/src/core/checks/checks/status_code_conformance.ts +61 -0
- package/src/core/checks/checks/unsupported_method.ts +63 -0
- package/src/core/checks/checks/use_after_free.ts +78 -0
- package/src/core/checks/index.ts +30 -0
- package/src/core/checks/mode.ts +79 -0
- package/src/core/checks/recommended-action.ts +64 -0
- package/src/core/checks/registry.ts +78 -0
- package/src/core/checks/runner.ts +874 -0
- package/src/core/checks/sarif.ts +230 -0
- package/src/core/checks/stateful.ts +121 -0
- package/src/core/checks/types.ts +189 -0
- package/src/core/classifier/recommended-action.ts +222 -0
- package/src/core/context/current.ts +51 -0
- package/src/core/context/session.ts +78 -0
- package/src/core/coverage/loader.ts +185 -0
- package/src/core/coverage/reasons.ts +300 -0
- package/src/core/diagnostics/db-analysis.ts +161 -12
- package/src/core/diagnostics/failure-class.ts +120 -0
- package/src/core/diagnostics/failure-hints.ts +212 -9
- package/src/core/diagnostics/spec-pointer.ts +99 -0
- package/src/core/diagnostics/suggested-fixes.ts +156 -0
- package/src/core/exporter/case-study/index.ts +270 -0
- package/src/core/exporter/curl.ts +40 -0
- package/src/core/exporter/exporter.ts +48 -0
- package/src/core/exporter/html-report/escape.ts +24 -0
- package/src/core/exporter/html-report/index.ts +479 -0
- package/src/core/exporter/html-report/script.ts +100 -0
- package/src/core/exporter/html-report/styles.ts +408 -0
- package/src/core/generator/chunker.ts +53 -15
- package/src/core/generator/coverage-phase.ts +0 -0
- package/src/core/generator/create-body.ts +89 -0
- package/src/core/generator/data-factory.ts +490 -33
- package/src/core/generator/describe.ts +1 -1
- package/src/core/generator/fixtures-builder.ts +325 -0
- package/src/core/generator/index.ts +7 -5
- package/src/core/generator/openapi-reader.ts +55 -3
- package/src/core/generator/path-param-disambig.ts +114 -0
- package/src/core/generator/resources-builder.ts +648 -0
- package/src/core/generator/schema-utils.ts +11 -3
- package/src/core/generator/serializer.ts +114 -15
- package/src/core/generator/suite-generator.ts +484 -77
- package/src/core/generator/types.ts +8 -0
- package/src/core/identity/identity-file.ts +129 -0
- package/src/core/lint/affects.ts +28 -0
- package/src/core/lint/config.ts +96 -0
- package/src/core/lint/format.ts +42 -0
- package/src/core/lint/index.ts +94 -0
- package/src/core/lint/reporter.ts +128 -0
- package/src/core/lint/rules/consistency.ts +158 -0
- package/src/core/lint/rules/heuristics.ts +97 -0
- package/src/core/lint/rules/strictness.ts +109 -0
- package/src/core/lint/types.ts +96 -0
- package/src/core/lint/walker.ts +248 -0
- package/src/core/meta/meta-store.ts +6 -73
- package/src/core/output/README.md +91 -0
- package/src/core/output/index.ts +13 -0
- package/src/core/output/run.ts +126 -0
- package/src/core/output/types.ts +129 -0
- package/src/core/parser/env-interpolation.ts +104 -0
- package/src/core/parser/filter.ts +57 -0
- package/src/core/parser/schema.ts +132 -5
- package/src/core/parser/types.ts +29 -2
- package/src/core/parser/variables.ts +0 -0
- package/src/core/parser/yaml-parser.ts +108 -13
- package/src/core/probe/bootstrap.ts +34 -0
- package/src/core/probe/dry-run-envelope.ts +57 -0
- package/src/core/probe/mass-assignment-probe-class.ts +198 -0
- package/src/core/probe/mass-assignment-probe.ts +1122 -0
- package/src/core/probe/mass-assignment-template.ts +212 -0
- package/src/core/probe/method-probe.ts +164 -0
- package/src/core/probe/method-shared.ts +69 -0
- package/src/core/probe/negative-probe.ts +691 -0
- package/src/core/probe/orphan-tracker.ts +188 -0
- package/src/core/probe/path-discovery.ts +440 -0
- package/src/core/probe/probe-harness.ts +120 -0
- package/src/core/probe/registry.ts +89 -0
- package/src/core/probe/runner.ts +136 -0
- package/src/core/probe/security-probe-class.ts +201 -0
- package/src/core/probe/security-probe.ts +1453 -0
- package/src/core/probe/shared.ts +505 -0
- package/src/core/probe/static-probe-class.ts +125 -0
- package/src/core/probe/types.ts +165 -0
- package/src/core/probe/verdict-aggregator.ts +33 -0
- package/src/core/probe/webhooks-probe.ts +284 -0
- package/src/core/reporter/console.ts +69 -4
- package/src/core/reporter/index.ts +2 -3
- package/src/core/reporter/json.ts +15 -2
- package/src/core/reporter/junit.ts +27 -12
- package/src/core/reporter/ndjson.ts +37 -0
- package/src/core/reporter/types.ts +3 -0
- package/src/core/runner/assertions.ts +62 -2
- package/src/core/runner/async-pool.ts +108 -0
- package/src/core/runner/auth-path.ts +8 -0
- package/src/core/runner/ci-context.ts +72 -0
- package/src/core/runner/executor.ts +391 -52
- package/src/core/runner/form-encode.ts +51 -0
- package/src/core/runner/http-client.ts +115 -7
- package/src/core/runner/learn-drift.ts +293 -0
- package/src/core/runner/preflight-vars.ts +149 -0
- package/src/core/runner/progress-tracker.ts +73 -0
- package/src/core/runner/rate-limiter.ts +203 -0
- package/src/core/runner/run-kind.ts +39 -0
- package/src/core/runner/schema-validator.ts +312 -0
- package/src/core/runner/send-request.ts +153 -20
- package/src/core/runner/types.ts +38 -0
- package/src/core/secrets/registry.ts +164 -0
- package/src/core/secrets/secrets-file.ts +115 -0
- package/src/core/selectors/operation-filter.ts +144 -0
- package/src/core/setup-api.ts +419 -17
- package/src/core/severity/category.ts +94 -0
- package/src/core/severity/index.ts +121 -0
- package/src/core/spec/layers.ts +154 -0
- package/src/core/util/format-eta.ts +21 -0
- package/src/core/utils.ts +5 -1
- package/src/core/workspace/config.ts +129 -0
- package/src/core/workspace/manifest.ts +283 -0
- package/src/core/workspace/output-rotation.ts +62 -0
- package/src/core/workspace/root.ts +94 -0
- package/src/core/workspace/triage-path.ts +87 -0
- package/src/db/lint-runs.ts +47 -0
- package/src/db/migrate.ts +126 -0
- package/src/db/migrations/0001_run_kind.sql +25 -0
- package/src/db/migrations/sql.d.ts +4 -0
- package/src/db/queries/collections.ts +133 -0
- package/src/db/queries/coverage.ts +9 -0
- package/src/db/queries/dashboard.ts +59 -0
- package/src/db/queries/results.ts +128 -0
- package/src/db/queries/runs.ts +235 -0
- package/src/db/queries/sessions.ts +42 -0
- package/src/db/queries/settings.ts +28 -0
- package/src/db/queries/types.ts +172 -0
- package/src/db/queries.ts +72 -802
- package/src/db/schema.ts +179 -48
- package/src/cli/commands/export.ts +0 -144
- package/src/cli/commands/guide.ts +0 -127
- package/src/cli/commands/init.ts +0 -57
- package/src/cli/commands/serve.ts +0 -81
- package/src/cli/commands/sync.ts +0 -269
- package/src/cli/commands/update.ts +0 -189
- package/src/cli/commands/validate.ts +0 -34
- package/src/core/exporter/postman.ts +0 -963
- package/src/core/generator/guide-builder.ts +0 -253
- package/src/core/meta/types.ts +0 -21
- package/src/core/parser/index.ts +0 -21
- package/src/core/runner/execute-run.ts +0 -132
- package/src/core/runner/index.ts +0 -12
- package/src/core/sync/spec-differ.ts +0 -38
- package/src/web/data/collection-state.ts +0 -362
- package/src/web/routes/api.ts +0 -314
- package/src/web/routes/dashboard.ts +0 -350
- package/src/web/routes/runs.ts +0 -64
- package/src/web/schemas.ts +0 -121
- package/src/web/server.ts +0 -134
- package/src/web/static/htmx.min.cjs +0 -1
- package/src/web/static/style.css +0 -1148
- package/src/web/views/endpoints-tab.ts +0 -174
- package/src/web/views/explorer-tab.ts +0 -402
- package/src/web/views/health-strip.ts +0 -92
- package/src/web/views/layout.ts +0 -48
- package/src/web/views/results.ts +0 -210
- package/src/web/views/runs-tab.ts +0 -126
- package/src/web/views/suites-tab.ts +0 -181
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `cross_call_references` (m-20 ARV-169) — POST→GET shape-diff probe.
|
|
3
|
+
*
|
|
4
|
+
* For each CRUD group with create+read, POST a generated body, capture
|
|
5
|
+
* the new id from the response, then GET the resource and diff the
|
|
6
|
+
* write-shape against the read-shape. Fields the create accepted (or
|
|
7
|
+
* echoed) but the read didn't return surface as drift findings — the
|
|
8
|
+
* server is silently dropping state.
|
|
9
|
+
*
|
|
10
|
+
* Severity policy:
|
|
11
|
+
* • `state_not_persisted` (POST echoed, GET dropped) is the high-
|
|
12
|
+
* signal class, so the check is registered as HIGH.
|
|
13
|
+
* • `write_only` (POST accepted, GET never returned) is also surfaced
|
|
14
|
+
* in the same finding's evidence. Anti-FP: write-only fields the
|
|
15
|
+
* spec explicitly declares are *not* present on GET (e.g. password
|
|
16
|
+
* write-only properties) are filtered out via spec.responses.GET
|
|
17
|
+
* declared field set.
|
|
18
|
+
* • Per-resource quirks (Stripe `metadata` stripping, livemode) are
|
|
19
|
+
* declared in `.api-resources[.local].yaml` `readback_diff` blocks
|
|
20
|
+
* and the harness threads them through `resourceConfigs`.
|
|
21
|
+
*
|
|
22
|
+
* The check fails (one finding) when EITHER list is non-empty. The
|
|
23
|
+
* evidence carries the per-field breakdown so the reporter can show
|
|
24
|
+
* exactly which fields drifted and how. We deliberately emit a single
|
|
25
|
+
* finding per resource — splitting into one-finding-per-field would
|
|
26
|
+
* inflate counts but tell the user the same story.
|
|
27
|
+
*/
|
|
28
|
+
import type { OpenAPIV3 } from "openapi-types";
|
|
29
|
+
import type { CrudStatefulCheck } from "../stateful.ts";
|
|
30
|
+
import { extractIdFromCreateResponse, fillPathWithId, fillPathParams, serializeCheckBody, resolveCreateBody } from "./_crud-helpers.ts";
|
|
31
|
+
import { computeDrift } from "./_readback-helpers.ts";
|
|
32
|
+
|
|
33
|
+
function declaredReadFields(read: { responses: Array<{ statusCode: number; schema?: unknown }> }): Set<string> {
|
|
34
|
+
const success = read.responses.find((r) => r.statusCode >= 200 && r.statusCode < 300);
|
|
35
|
+
const schema = success?.schema as OpenAPIV3.SchemaObject | undefined;
|
|
36
|
+
if (!schema?.properties) return new Set();
|
|
37
|
+
return new Set(Object.keys(schema.properties));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function safeParse(v: unknown): unknown {
|
|
41
|
+
if (typeof v !== "string") return v;
|
|
42
|
+
try {
|
|
43
|
+
return JSON.parse(v);
|
|
44
|
+
} catch {
|
|
45
|
+
return v;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const crossCallReferences: CrudStatefulCheck = {
|
|
50
|
+
id: "cross_call_references",
|
|
51
|
+
severity: "high",
|
|
52
|
+
defaultExpected: "Fields accepted or echoed by POST must be readable via GET on the same resource",
|
|
53
|
+
references: [{ id: "ARV-169" }, { id: "OWASP-API-3-2023" }],
|
|
54
|
+
phase: "crud",
|
|
55
|
+
applies(g) {
|
|
56
|
+
return Boolean(g.create && g.read);
|
|
57
|
+
},
|
|
58
|
+
async run(g, h) {
|
|
59
|
+
if (h.bootstrapCleanupFailed) {
|
|
60
|
+
return { kind: "skip", reason: "bootstrap-cleanup failed — stateful checks paused" };
|
|
61
|
+
}
|
|
62
|
+
const create = g.create!;
|
|
63
|
+
const read = g.read!;
|
|
64
|
+
const baseHeaders = { Accept: "application/json", ...h.authHeaders };
|
|
65
|
+
|
|
66
|
+
const seedBody = h.resourceConfigs?.get(g.resource)?.seedBody;
|
|
67
|
+
if (!create.requestBodySchema && !seedBody) {
|
|
68
|
+
return { kind: "skip", reason: "create has no requestBody schema and no seed_body — nothing to diff" };
|
|
69
|
+
}
|
|
70
|
+
const writeBody = resolveCreateBody(create, seedBody);
|
|
71
|
+
if (writeBody == null) {
|
|
72
|
+
return { kind: "skip", reason: "could not produce a create body (no seed_body, generator returned non-object)" };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const createUrl = `${h.baseUrl.replace(/\/+$/, "")}${fillPathParams(create.path, h.pathVars)}`;
|
|
76
|
+
// ARV-191: form-urlencoded dispatch — see _crud-helpers.serializeCheckBody.
|
|
77
|
+
// ARV-187: seed_body.content_type overrides spec'd contentType when set.
|
|
78
|
+
const { body: createBodyStr, contentType } = serializeCheckBody(create, writeBody, h.pathVars, seedBody?.contentType);
|
|
79
|
+
const createResp = await h.send({
|
|
80
|
+
method: "POST",
|
|
81
|
+
url: createUrl,
|
|
82
|
+
headers: { ...baseHeaders, "Content-Type": contentType },
|
|
83
|
+
body: createBodyStr,
|
|
84
|
+
});
|
|
85
|
+
if (createResp.status < 200 || createResp.status >= 300) {
|
|
86
|
+
return { kind: "skip", reason: `create returned ${createResp.status} — broken-baseline guard` };
|
|
87
|
+
}
|
|
88
|
+
const echo = createResp.body_parsed ?? safeParse(createResp.body);
|
|
89
|
+
const id = extractIdFromCreateResponse(echo, g.idParam);
|
|
90
|
+
if (id == null) return { kind: "skip", reason: "could not extract id from create response" };
|
|
91
|
+
|
|
92
|
+
// Substitute parent-scope vars first (e.g., {organization_id_or_slug}),
|
|
93
|
+
// then the captured id for {idParam}. Order matters: fillPathWithId's
|
|
94
|
+
// fallback regex replaces ANY remaining `{...}` with the id, so parent
|
|
95
|
+
// vars must already be resolved when it runs.
|
|
96
|
+
const readPath = fillPathWithId(fillPathParams(read.path, h.pathVars), g.idParam, id);
|
|
97
|
+
const readUrl = `${h.baseUrl.replace(/\/+$/, "")}${readPath}`;
|
|
98
|
+
const readResp = await h.send({ method: "GET", url: readUrl, headers: baseHeaders });
|
|
99
|
+
if (readResp.status < 200 || readResp.status >= 300) {
|
|
100
|
+
return { kind: "skip", reason: `read returned ${readResp.status} — broken-baseline guard` };
|
|
101
|
+
}
|
|
102
|
+
const readBody = readResp.body_parsed ?? safeParse(readResp.body);
|
|
103
|
+
|
|
104
|
+
const cfg = h.resourceConfigs?.get(g.resource)?.readbackDiff;
|
|
105
|
+
const specDeclared = declaredReadFields(read);
|
|
106
|
+
const drift = computeDrift(writeBody, echo, readBody, specDeclared, cfg);
|
|
107
|
+
|
|
108
|
+
const stateNotPersisted = drift.stateNotPersisted;
|
|
109
|
+
const writeOnly = drift.writeOnly;
|
|
110
|
+
if (stateNotPersisted.length === 0 && writeOnly.length === 0) {
|
|
111
|
+
return { kind: "pass" };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const driftedFields = [
|
|
115
|
+
...stateNotPersisted.map((d) => d.field),
|
|
116
|
+
...writeOnly.map((d) => d.field),
|
|
117
|
+
];
|
|
118
|
+
return {
|
|
119
|
+
kind: "fail",
|
|
120
|
+
message:
|
|
121
|
+
stateNotPersisted.length > 0
|
|
122
|
+
? `POST→GET drift on ${g.resource}: ${stateNotPersisted.length} state-not-persisted field(s)` +
|
|
123
|
+
(writeOnly.length > 0 ? `, ${writeOnly.length} write-only field(s)` : "")
|
|
124
|
+
: `POST→GET drift on ${g.resource}: ${writeOnly.length} write-only field(s)`,
|
|
125
|
+
evidence: {
|
|
126
|
+
resource: g.resource,
|
|
127
|
+
id,
|
|
128
|
+
state_not_persisted: stateNotPersisted.map((d) => ({ field: d.field, written_value: d.writtenValue })),
|
|
129
|
+
write_only: writeOnly.map((d) => d.field),
|
|
130
|
+
drifted_fields: driftedFields,
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
},
|
|
134
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ensure_resource_availability` (m-15 ARV-3) — create a resource via
|
|
3
|
+
* POST, then GET by id; the read must succeed (2xx). Catches lost-
|
|
4
|
+
* write bugs where the create returns 201 but the resource never
|
|
5
|
+
* actually appears in storage.
|
|
6
|
+
*/
|
|
7
|
+
import type { CrudStatefulCheck } from "../stateful.ts";
|
|
8
|
+
import { extractIdFromCreateResponse, fillPathWithId, fillPathParams, serializeCheckBody, resolveCreateBody } from "./_crud-helpers.ts";
|
|
9
|
+
|
|
10
|
+
export const ensureResourceAvailability: CrudStatefulCheck = {
|
|
11
|
+
id: "ensure_resource_availability",
|
|
12
|
+
severity: "medium",
|
|
13
|
+
defaultExpected: "GET on a freshly-created resource must succeed (2xx)",
|
|
14
|
+
references: [{ id: "CWE-924" }],
|
|
15
|
+
phase: "crud",
|
|
16
|
+
applies(g) {
|
|
17
|
+
return Boolean(g.create && g.read);
|
|
18
|
+
},
|
|
19
|
+
async run(g, h) {
|
|
20
|
+
if (h.bootstrapCleanupFailed) {
|
|
21
|
+
return { kind: "skip", reason: "bootstrap-cleanup failed — security checks paused (ARV-3 AC #6)" };
|
|
22
|
+
}
|
|
23
|
+
const create = g.create!;
|
|
24
|
+
const read = g.read!;
|
|
25
|
+
const baseHeaders = { Accept: "application/json", ...h.authHeaders };
|
|
26
|
+
// ARV-191: form-urlencoded vs JSON dispatch — Stripe-style APIs
|
|
27
|
+
// declare x-www-form-urlencoded; JSON.stringify would yield "400
|
|
28
|
+
// missing param" the broken-baseline guard then silently swallows.
|
|
29
|
+
// ARV-187: prefer LLM-authored seed_body over generator.
|
|
30
|
+
const seedBody = h.resourceConfigs?.get(g.resource)?.seedBody;
|
|
31
|
+
const generated = resolveCreateBody(create, seedBody) ?? {};
|
|
32
|
+
const { body, contentType } = serializeCheckBody(
|
|
33
|
+
create,
|
|
34
|
+
generated,
|
|
35
|
+
h.pathVars,
|
|
36
|
+
seedBody?.contentType,
|
|
37
|
+
);
|
|
38
|
+
const createResp = await h.send({
|
|
39
|
+
method: "POST",
|
|
40
|
+
url: `${h.baseUrl.replace(/\/+$/, "")}${fillPathParams(create.path, h.pathVars)}`,
|
|
41
|
+
headers: { ...baseHeaders, "Content-Type": contentType },
|
|
42
|
+
body,
|
|
43
|
+
});
|
|
44
|
+
if (createResp.status < 200 || createResp.status >= 300) {
|
|
45
|
+
return { kind: "skip", reason: `create returned ${createResp.status} — broken-baseline guard` };
|
|
46
|
+
}
|
|
47
|
+
const id = extractIdFromCreateResponse(createResp.body_parsed ?? createResp.body, g.idParam);
|
|
48
|
+
if (id == null) return { kind: "skip", reason: "could not extract id from create response" };
|
|
49
|
+
|
|
50
|
+
const readResp = await h.send({
|
|
51
|
+
method: "GET",
|
|
52
|
+
url: `${h.baseUrl.replace(/\/+$/, "")}${fillPathWithId(fillPathParams(read.path, h.pathVars), g.idParam, id)}`,
|
|
53
|
+
headers: baseHeaders,
|
|
54
|
+
});
|
|
55
|
+
if (readResp.status >= 200 && readResp.status < 300) return { kind: "pass" };
|
|
56
|
+
return {
|
|
57
|
+
kind: "fail",
|
|
58
|
+
message: `GET on freshly-created resource ${id} returned ${readResp.status}`,
|
|
59
|
+
evidence: { resource: g.resource, id, create_status: createResp.status, read_status: readResp.status },
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
};
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `idempotency_replay` (m-20 ARV-170) — Idempotency-Key honor probe.
|
|
3
|
+
*
|
|
4
|
+
* For each CRUD group with create+delete where idempotency is opted-in
|
|
5
|
+
* (either via `.api-resources.yaml` `idempotency:` block or by the
|
|
6
|
+
* create endpoint declaring an `Idempotency-Key` header parameter),
|
|
7
|
+
* POST the same body twice with the same key. The server must:
|
|
8
|
+
*
|
|
9
|
+
* 1. return the *same* resource id on both calls (id1 == id2 — no
|
|
10
|
+
* duplicate created), AND
|
|
11
|
+
* 2. return bit-identical responses modulo a small allow-list of
|
|
12
|
+
* timestamp / request-id fields (R1 == R2).
|
|
13
|
+
*
|
|
14
|
+
* Severity policy: HIGH. The two failure classes share one finding —
|
|
15
|
+
* the runner doesn't support per-finding severity downgrade and
|
|
16
|
+
* `duplicate_resource` is the dominant signal anyway. `non_bit_identical`
|
|
17
|
+
* piggybacks via finding.evidence.kind so consumers can split the
|
|
18
|
+
* digest if they care.
|
|
19
|
+
*
|
|
20
|
+
* Anti-FP guards:
|
|
21
|
+
* • Skip when the second POST gets a 429 / 409 / 5xx — replay rate
|
|
22
|
+
* limiting and locking races confuse the verdict.
|
|
23
|
+
* • Skip when either POST fails the broken-baseline check (non-2xx).
|
|
24
|
+
* • Cleanup tolerates missing DELETE wiring — emits a warning via
|
|
25
|
+
* `cleanup_warning` in evidence.
|
|
26
|
+
*
|
|
27
|
+
* Auto-detect fallback: if no yaml block exists but the create endpoint
|
|
28
|
+
* declares an `Idempotency-Key` header in its `parameters[]`, the check
|
|
29
|
+
* runs with default settings. Explicit yaml wins — it lets the user
|
|
30
|
+
* override the header name and customize the ignore list.
|
|
31
|
+
*/
|
|
32
|
+
import type { OpenAPIV3 } from "openapi-types";
|
|
33
|
+
import type { CrudStatefulCheck } from "../stateful.ts";
|
|
34
|
+
import type { IdempotencyConfig } from "../../generator/resources-builder.ts";
|
|
35
|
+
import { extractIdFromCreateResponse, fillPathWithId, fillPathParams, serializeCheckBody, resolveCreateBody } from "./_crud-helpers.ts";
|
|
36
|
+
|
|
37
|
+
/** Default header name used when yaml omits it and we're running on
|
|
38
|
+
* spec-detected idempotency support. Matches the Stripe / Resend
|
|
39
|
+
* convention; specs that use a different casing should declare it
|
|
40
|
+
* explicitly. */
|
|
41
|
+
const DEFAULT_HEADER = "Idempotency-Key";
|
|
42
|
+
|
|
43
|
+
/** Baseline response fields stripped before bit-identical compare.
|
|
44
|
+
* Mirrors the readback-diff baseline minus a few read-shape-specific
|
|
45
|
+
* ones (livemode, _links). Timestamps + request-id + etag cover the
|
|
46
|
+
* common "every replay has a new request_id" surface. */
|
|
47
|
+
const DEFAULT_IGNORE_RESPONSE: ReadonlySet<string> = new Set([
|
|
48
|
+
"created",
|
|
49
|
+
"created_at",
|
|
50
|
+
"createdAt",
|
|
51
|
+
"updated",
|
|
52
|
+
"updated_at",
|
|
53
|
+
"updatedAt",
|
|
54
|
+
"request_id",
|
|
55
|
+
"requestId",
|
|
56
|
+
"x_request_id",
|
|
57
|
+
"etag",
|
|
58
|
+
"_etag",
|
|
59
|
+
]);
|
|
60
|
+
|
|
61
|
+
function safeParse(v: unknown): unknown {
|
|
62
|
+
if (typeof v !== "string") return v;
|
|
63
|
+
try {
|
|
64
|
+
return JSON.parse(v);
|
|
65
|
+
} catch {
|
|
66
|
+
return v;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function detectSpecHeader(create: { parameters: OpenAPIV3.ParameterObject[] }): string | null {
|
|
71
|
+
for (const p of create.parameters) {
|
|
72
|
+
if (p.in !== "header") continue;
|
|
73
|
+
if (p.name.toLowerCase() === "idempotency-key") return p.name;
|
|
74
|
+
}
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function resolveConfig(
|
|
79
|
+
cfg: IdempotencyConfig | undefined,
|
|
80
|
+
create: { parameters: OpenAPIV3.ParameterObject[] },
|
|
81
|
+
): { header: string; ignore: ReadonlySet<string> } | null {
|
|
82
|
+
if (cfg) {
|
|
83
|
+
const header = cfg.header ?? DEFAULT_HEADER;
|
|
84
|
+
const ignore = cfg.ignoreResponseFields
|
|
85
|
+
? new Set<string>([...DEFAULT_IGNORE_RESPONSE, ...cfg.ignoreResponseFields])
|
|
86
|
+
: DEFAULT_IGNORE_RESPONSE;
|
|
87
|
+
return { header, ignore };
|
|
88
|
+
}
|
|
89
|
+
const detected = detectSpecHeader(create);
|
|
90
|
+
if (detected) return { header: detected, ignore: DEFAULT_IGNORE_RESPONSE };
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Shallow object diff with field-level ignore. Returns the list of
|
|
95
|
+
* keys whose values differ (or whose presence differs) between a and
|
|
96
|
+
* b, excluding ignored fields. Both inputs treated as `{}` when not
|
|
97
|
+
* object-shaped. */
|
|
98
|
+
function diffFields(a: unknown, b: unknown, ignore: ReadonlySet<string>): string[] {
|
|
99
|
+
const av = (a && typeof a === "object" && !Array.isArray(a)) ? (a as Record<string, unknown>) : {};
|
|
100
|
+
const bv = (b && typeof b === "object" && !Array.isArray(b)) ? (b as Record<string, unknown>) : {};
|
|
101
|
+
const keys = new Set<string>([...Object.keys(av), ...Object.keys(bv)]);
|
|
102
|
+
const diffs: string[] = [];
|
|
103
|
+
for (const k of keys) {
|
|
104
|
+
if (ignore.has(k)) continue;
|
|
105
|
+
const sa = JSON.stringify(av[k] ?? null);
|
|
106
|
+
const sb = JSON.stringify(bv[k] ?? null);
|
|
107
|
+
if (sa !== sb) diffs.push(k);
|
|
108
|
+
}
|
|
109
|
+
return diffs;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function generateKey(): string {
|
|
113
|
+
// Bun + Node 19+ ship crypto.randomUUID; fall back to a timestamp+rand
|
|
114
|
+
// mash so tests on minimal stubs still produce a stable-ish key.
|
|
115
|
+
const c = (globalThis as { crypto?: { randomUUID?: () => string } }).crypto;
|
|
116
|
+
if (c?.randomUUID) return c.randomUUID();
|
|
117
|
+
return `zond-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export const idempotencyReplay: CrudStatefulCheck = {
|
|
121
|
+
id: "idempotency_replay",
|
|
122
|
+
severity: "high",
|
|
123
|
+
defaultExpected: "Two POSTs with the same Idempotency-Key must return the same resource id and bit-identical responses",
|
|
124
|
+
references: [{ id: "ARV-170" }, { id: "stripe-idempotent-requests" }],
|
|
125
|
+
phase: "crud",
|
|
126
|
+
applies(g) {
|
|
127
|
+
return Boolean(g.create);
|
|
128
|
+
},
|
|
129
|
+
async run(g, h) {
|
|
130
|
+
if (h.bootstrapCleanupFailed) {
|
|
131
|
+
return { kind: "skip", reason: "bootstrap-cleanup failed — stateful checks paused" };
|
|
132
|
+
}
|
|
133
|
+
const create = g.create!;
|
|
134
|
+
|
|
135
|
+
const cfg = h.resourceConfigs?.get(g.resource)?.idempotency;
|
|
136
|
+
const resolved = resolveConfig(cfg, create);
|
|
137
|
+
if (!resolved) {
|
|
138
|
+
return { kind: "skip", reason: "no idempotency config and no Idempotency-Key parameter in spec" };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const seedBody = h.resourceConfigs?.get(g.resource)?.seedBody;
|
|
142
|
+
if (!create.requestBodySchema && !seedBody) {
|
|
143
|
+
return { kind: "skip", reason: "create has no requestBody schema and no seed_body — nothing to replay" };
|
|
144
|
+
}
|
|
145
|
+
const writeBody = resolveCreateBody(create, seedBody);
|
|
146
|
+
if (writeBody == null) {
|
|
147
|
+
return { kind: "skip", reason: "could not produce a create body (no seed_body, generator returned non-object)" };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const key = generateKey();
|
|
151
|
+
// ARV-191: form-urlencoded vs JSON dispatch — Stripe-style APIs
|
|
152
|
+
// honor Idempotency-Key but expect x-www-form-urlencoded payloads;
|
|
153
|
+
// JSON.stringify would yield broken-baseline 400s on every replay.
|
|
154
|
+
const { body: bodyStr, contentType } = serializeCheckBody(create, writeBody, h.pathVars, seedBody?.contentType);
|
|
155
|
+
const baseHeaders = {
|
|
156
|
+
Accept: "application/json",
|
|
157
|
+
"Content-Type": contentType,
|
|
158
|
+
[resolved.header]: key,
|
|
159
|
+
...h.authHeaders,
|
|
160
|
+
};
|
|
161
|
+
const url = `${h.baseUrl.replace(/\/+$/, "")}${fillPathParams(create.path, h.pathVars)}`;
|
|
162
|
+
|
|
163
|
+
// 1st POST
|
|
164
|
+
const r1 = await h.send({ method: "POST", url, headers: baseHeaders, body: bodyStr });
|
|
165
|
+
if (r1.status < 200 || r1.status >= 300) {
|
|
166
|
+
return { kind: "skip", reason: `1st create returned ${r1.status} — broken-baseline guard` };
|
|
167
|
+
}
|
|
168
|
+
const body1 = r1.body_parsed ?? safeParse(r1.body);
|
|
169
|
+
const id1 = extractIdFromCreateResponse(body1, g.idParam);
|
|
170
|
+
if (id1 == null) return { kind: "skip", reason: "could not extract id from 1st create response" };
|
|
171
|
+
|
|
172
|
+
// 2nd POST — same body, same key
|
|
173
|
+
const r2 = await h.send({ method: "POST", url, headers: baseHeaders, body: bodyStr });
|
|
174
|
+
if (r2.status === 429 || r2.status === 409) {
|
|
175
|
+
// Cleanup r1 before bailing — we did create something.
|
|
176
|
+
await tryCleanup(g, h, id1);
|
|
177
|
+
return { kind: "skip", reason: `2nd create returned ${r2.status} — rate-limit/conflict, replay verdict ambiguous` };
|
|
178
|
+
}
|
|
179
|
+
if (r2.status < 200 || r2.status >= 300) {
|
|
180
|
+
await tryCleanup(g, h, id1);
|
|
181
|
+
return { kind: "skip", reason: `2nd create returned ${r2.status} — broken-baseline guard` };
|
|
182
|
+
}
|
|
183
|
+
const body2 = r2.body_parsed ?? safeParse(r2.body);
|
|
184
|
+
const id2 = extractIdFromCreateResponse(body2, g.idParam);
|
|
185
|
+
|
|
186
|
+
// Verdict
|
|
187
|
+
const duplicate = id2 != null && String(id1) !== String(id2);
|
|
188
|
+
const diffs = duplicate ? [] : diffFields(body1, body2, resolved.ignore);
|
|
189
|
+
const nonBitIdentical = !duplicate && diffs.length > 0;
|
|
190
|
+
|
|
191
|
+
// Cleanup. If duplicate, both ids need to go.
|
|
192
|
+
const cleanupWarn: string[] = [];
|
|
193
|
+
const okCleanup1 = await tryCleanup(g, h, id1);
|
|
194
|
+
if (!okCleanup1) cleanupWarn.push(`failed to DELETE id=${id1}`);
|
|
195
|
+
if (duplicate && id2 != null) {
|
|
196
|
+
const okCleanup2 = await tryCleanup(g, h, id2);
|
|
197
|
+
if (!okCleanup2) cleanupWarn.push(`failed to DELETE id=${id2}`);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (!duplicate && !nonBitIdentical) {
|
|
201
|
+
return { kind: "pass" };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const kind = duplicate && nonBitIdentical
|
|
205
|
+
? "both"
|
|
206
|
+
: duplicate ? "duplicate_resource" : "non_bit_identical";
|
|
207
|
+
const message = duplicate
|
|
208
|
+
? `Idempotency-Key not honored on ${g.resource}: replay produced a new resource (id1=${id1}, id2=${id2})`
|
|
209
|
+
: `Idempotency-Key replay on ${g.resource} is not bit-identical (${diffs.length} field(s) differ): ${diffs.slice(0, 5).join(", ")}`;
|
|
210
|
+
|
|
211
|
+
return {
|
|
212
|
+
kind: "fail",
|
|
213
|
+
message,
|
|
214
|
+
evidence: {
|
|
215
|
+
resource: g.resource,
|
|
216
|
+
kind,
|
|
217
|
+
header: resolved.header,
|
|
218
|
+
key,
|
|
219
|
+
id1,
|
|
220
|
+
id2,
|
|
221
|
+
diff_fields: diffs,
|
|
222
|
+
...(cleanupWarn.length > 0 ? { cleanup_warning: cleanupWarn } : {}),
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
async function tryCleanup(
|
|
229
|
+
g: { delete?: { path: string }; idParam: string },
|
|
230
|
+
h: import("../stateful.ts").StatefulHarness,
|
|
231
|
+
id: string | number,
|
|
232
|
+
): Promise<boolean> {
|
|
233
|
+
if (!g.delete) return false;
|
|
234
|
+
const url = `${h.baseUrl.replace(/\/+$/, "")}${fillPathWithId(fillPathParams(g.delete.path, h.pathVars), g.idParam, id)}`;
|
|
235
|
+
try {
|
|
236
|
+
const resp = await h.send({
|
|
237
|
+
method: "DELETE",
|
|
238
|
+
url,
|
|
239
|
+
headers: { Accept: "application/json", ...h.authHeaders },
|
|
240
|
+
});
|
|
241
|
+
// 404 = already gone (good); 2xx = deleted; anything else = failure.
|
|
242
|
+
return resp.status === 404 || (resp.status >= 200 && resp.status < 300);
|
|
243
|
+
} catch {
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ignored_auth` (m-15 ARV-3, refined in ARV-181) — for every operation
|
|
3
|
+
* that declares a security requirement, send 3 requests:
|
|
4
|
+
*
|
|
5
|
+
* 1. baseline — full real-auth headers,
|
|
6
|
+
* 2. no_auth — drop every auth-shaped header,
|
|
7
|
+
* 3. bogus_auth — replace each auth header value with a malformed-
|
|
8
|
+
* shaped, plausibly-typed bogus.
|
|
9
|
+
*
|
|
10
|
+
* Verdict logic (ARV-181 differential):
|
|
11
|
+
*
|
|
12
|
+
* - baseline 5xx → skip (server unhealthy; nothing we say is trustworthy).
|
|
13
|
+
* - baseline 2xx → strict mode. Any 2xx on no_auth/bogus → HIGH bypass.
|
|
14
|
+
* - baseline 4xx → soft mode. The auth token didn't get a 2xx (wrong
|
|
15
|
+
* permissions, real path-var not provided, etc.), but we can still
|
|
16
|
+
* learn from how the server treats *worse* credentials:
|
|
17
|
+
* · no_auth/bogus returns **strictly better** status (lower 4xx
|
|
18
|
+
* class, or 2xx/3xx) → HIGH bypass. The classic smoking gun
|
|
19
|
+
* is `baseline 403 / no_auth 200`.
|
|
20
|
+
* · same or worse status → pass (auth was checked; the resource
|
|
21
|
+
* simply isn't accessible to anyone).
|
|
22
|
+
*
|
|
23
|
+
* Strictness:
|
|
24
|
+
* - default: no_auth/bogus passes if status is in [400..499] (any 4xx).
|
|
25
|
+
* - --strict-401 (CheckRuntimeOptions.strict401): only 401 passes; any
|
|
26
|
+
* other status — even 403/404 — fails. Mirrors schemathesis V4.
|
|
27
|
+
*
|
|
28
|
+
* Anti-FP guards (kept from ARV-3):
|
|
29
|
+
* - skip operations with `security: []` override (explicitly public),
|
|
30
|
+
* - skip when `bootstrap_cleanup_failed` (data state corrupted),
|
|
31
|
+
* - skip when no auth headers provided to the harness at all.
|
|
32
|
+
*/
|
|
33
|
+
import type { OpenAPIV3 } from "openapi-types";
|
|
34
|
+
import type { AuthStatefulCheck } from "../stateful.ts";
|
|
35
|
+
import type { HttpRequest } from "../../runner/types.ts";
|
|
36
|
+
|
|
37
|
+
function buildBogus(name: string, value: string): string {
|
|
38
|
+
// Keep the original prefix so the auth scheme detection at the
|
|
39
|
+
// server still matches (Bearer xxx, Basic xxx). Only the secret
|
|
40
|
+
// payload is replaced.
|
|
41
|
+
if (/^Bearer\s+/i.test(value)) return "Bearer aaaaaaaaaaaa.bbbbbbbbbbbb.cccccccccccc";
|
|
42
|
+
if (/^Basic\s+/i.test(value)) return "Basic " + Buffer.from("zzz:zzz").toString("base64");
|
|
43
|
+
// apiKey / custom header — preserve length-class, replace content.
|
|
44
|
+
return name.toLowerCase().includes("token") ? "ZZZZZZZZZZ" : "bogus-" + "z".repeat(8);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** ARV-181: substitute path placeholders using `h.pathVars` first, then
|
|
48
|
+
* fall back to schema-derived placeholders. Mirrors `fillPathParams`
|
|
49
|
+
* in `runner.ts` (kept inline to avoid a cross-module dependency that
|
|
50
|
+
* would yank generator imports into stateful checks). */
|
|
51
|
+
function fillPath(
|
|
52
|
+
path: string,
|
|
53
|
+
op: { parameters: OpenAPIV3.ParameterObject[] },
|
|
54
|
+
pathVars: Record<string, string> | undefined,
|
|
55
|
+
): string {
|
|
56
|
+
return path.replace(/\{([^}]+)\}/g, (_, name: string) => {
|
|
57
|
+
const real = pathVars?.[name];
|
|
58
|
+
if (typeof real === "string" && real.length > 0) return encodeURIComponent(real);
|
|
59
|
+
const param = op.parameters.find((p) => p.name === name && p.in === "path");
|
|
60
|
+
const schema = param?.schema as OpenAPIV3.SchemaObject | undefined;
|
|
61
|
+
if (schema?.format === "uuid") return "00000000-0000-0000-0000-000000000000";
|
|
62
|
+
if (schema?.type === "integer" || schema?.type === "number") return "1";
|
|
63
|
+
return "x";
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function isAuthHeaderName(name: string): boolean {
|
|
68
|
+
const n = name.toLowerCase();
|
|
69
|
+
return n === "authorization" || n.startsWith("x-api") || n.includes("token") || n.includes("key");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** ARV-181: classify response status into a single ordering bucket so
|
|
73
|
+
* the differential broken-baseline logic can answer "did stripping
|
|
74
|
+
* auth give a *better* status than baseline?". Lower index = more
|
|
75
|
+
* permissive (worse from auth-enforcement POV). 5xx is its own
|
|
76
|
+
* bucket — never compare across it. */
|
|
77
|
+
function statusBucket(status: number): number {
|
|
78
|
+
if (status >= 200 && status < 400) return 0; // accepted-ish
|
|
79
|
+
if (status === 401) return 3; // canonical "auth required"
|
|
80
|
+
if (status === 403) return 2; // permission denied
|
|
81
|
+
if (status >= 400 && status < 500) return 1; // other 4xx (404, 422, ...)
|
|
82
|
+
return -1; // 5xx / 1xx — incomparable
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function isAcceptableRejection(status: number, strict401: boolean): boolean {
|
|
86
|
+
if (strict401) return status === 401;
|
|
87
|
+
return status >= 400 && status < 500;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export const ignoredAuth: AuthStatefulCheck = {
|
|
91
|
+
id: "ignored_auth",
|
|
92
|
+
severity: "high",
|
|
93
|
+
defaultExpected: "Server must reject requests without (or with bogus) auth credentials with 401/403",
|
|
94
|
+
references: [{ id: "OWASP-API-01" }],
|
|
95
|
+
phase: "auth",
|
|
96
|
+
applies(op) {
|
|
97
|
+
// Anti-FP: explicit `security: []` override means the op is intentionally public.
|
|
98
|
+
if (op.security.length === 0) return false;
|
|
99
|
+
return true;
|
|
100
|
+
},
|
|
101
|
+
async run(op, h) {
|
|
102
|
+
if (h.bootstrapCleanupFailed) {
|
|
103
|
+
return { kind: "skip", reason: "bootstrap-cleanup failed — security checks paused (ARV-3 AC #6)" };
|
|
104
|
+
}
|
|
105
|
+
if (Object.keys(h.authHeaders).length === 0) {
|
|
106
|
+
return { kind: "skip", reason: "no auth headers provided to harness — pass --auth-header" };
|
|
107
|
+
}
|
|
108
|
+
const strict401 = h.options?.strict401 === true;
|
|
109
|
+
const url = `${h.baseUrl.replace(/\/+$/, "")}${fillPath(op.path, op, h.pathVars)}`;
|
|
110
|
+
const method = op.method.toUpperCase();
|
|
111
|
+
const baseHeaders: Record<string, string> = { Accept: "application/json", ...h.authHeaders };
|
|
112
|
+
|
|
113
|
+
// 1. baseline
|
|
114
|
+
const baseReq: HttpRequest = { method, url, headers: baseHeaders };
|
|
115
|
+
const baseline = await h.send(baseReq);
|
|
116
|
+
if (baseline.status >= 500) {
|
|
117
|
+
return { kind: "skip", reason: `baseline returned ${baseline.status} — server-side error, no trustworthy signal` };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// 2. no_auth — strip every auth-shaped header
|
|
121
|
+
const noAuthHeaders: Record<string, string> = { ...baseHeaders };
|
|
122
|
+
for (const k of Object.keys(noAuthHeaders)) {
|
|
123
|
+
if (isAuthHeaderName(k)) delete noAuthHeaders[k];
|
|
124
|
+
}
|
|
125
|
+
const noAuth = await h.send({ method, url, headers: noAuthHeaders });
|
|
126
|
+
|
|
127
|
+
// 3. bogus_auth — keep header names, replace values
|
|
128
|
+
const bogusHeaders: Record<string, string> = { ...baseHeaders };
|
|
129
|
+
for (const k of Object.keys(bogusHeaders)) {
|
|
130
|
+
if (isAuthHeaderName(k)) bogusHeaders[k] = buildBogus(k, bogusHeaders[k]!);
|
|
131
|
+
}
|
|
132
|
+
const bogus = await h.send({ method, url, headers: bogusHeaders });
|
|
133
|
+
|
|
134
|
+
const baseBucket = statusBucket(baseline.status);
|
|
135
|
+
const baseIs2xx = baseline.status >= 200 && baseline.status < 300;
|
|
136
|
+
|
|
137
|
+
// ── strict-2xx-baseline branch (legacy path, unchanged semantically) ──
|
|
138
|
+
if (baseIs2xx) {
|
|
139
|
+
if (noAuth.status >= 200 && noAuth.status < 300) {
|
|
140
|
+
return {
|
|
141
|
+
kind: "fail",
|
|
142
|
+
message: `Server accepted request without auth credentials (status ${noAuth.status}) — auth is being ignored`,
|
|
143
|
+
evidence: { variant: "no_auth", baseline_status: baseline.status, no_auth_status: noAuth.status },
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
if (bogus.status >= 200 && bogus.status < 300) {
|
|
147
|
+
return {
|
|
148
|
+
kind: "fail",
|
|
149
|
+
message: `Server accepted request with bogus auth (status ${bogus.status}) — credentials not validated`,
|
|
150
|
+
evidence: { variant: "bogus_auth", baseline_status: baseline.status, bogus_auth_status: bogus.status },
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
if (strict401) {
|
|
154
|
+
if (noAuth.status !== 401) {
|
|
155
|
+
return {
|
|
156
|
+
kind: "fail",
|
|
157
|
+
message: `no_auth returned ${noAuth.status}, expected 401 (--strict-401)`,
|
|
158
|
+
evidence: { variant: "no_auth_strict", baseline_status: baseline.status, no_auth_status: noAuth.status, strict_401: true },
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
if (bogus.status !== 401) {
|
|
162
|
+
return {
|
|
163
|
+
kind: "fail",
|
|
164
|
+
message: `bogus_auth returned ${bogus.status}, expected 401 (--strict-401)`,
|
|
165
|
+
evidence: { variant: "bogus_auth_strict", baseline_status: baseline.status, bogus_auth_status: bogus.status, strict_401: true },
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return { kind: "pass" };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// ── differential 4xx-baseline branch (ARV-181) ─────────────────────
|
|
173
|
+
if (baseBucket < 0) {
|
|
174
|
+
return { kind: "skip", reason: `baseline returned ${baseline.status} — incomparable status, no trustworthy signal` };
|
|
175
|
+
}
|
|
176
|
+
const noAuthBucket = statusBucket(noAuth.status);
|
|
177
|
+
const bogusBucket = statusBucket(bogus.status);
|
|
178
|
+
|
|
179
|
+
if (noAuthBucket >= 0 && noAuthBucket < baseBucket) {
|
|
180
|
+
return {
|
|
181
|
+
kind: "fail",
|
|
182
|
+
message: `Server gave a more permissive status (${noAuth.status}) without auth than with valid auth (${baseline.status}) — possible bypass`,
|
|
183
|
+
evidence: { variant: "no_auth_differential", baseline_status: baseline.status, no_auth_status: noAuth.status },
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
if (bogusBucket >= 0 && bogusBucket < baseBucket) {
|
|
187
|
+
return {
|
|
188
|
+
kind: "fail",
|
|
189
|
+
message: `Server gave a more permissive status (${bogus.status}) with bogus auth than with valid auth (${baseline.status}) — possible bypass`,
|
|
190
|
+
evidence: { variant: "bogus_auth_differential", baseline_status: baseline.status, bogus_auth_status: bogus.status },
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
if (strict401) {
|
|
194
|
+
if (!isAcceptableRejection(noAuth.status, true) && noAuthBucket >= 0) {
|
|
195
|
+
return {
|
|
196
|
+
kind: "fail",
|
|
197
|
+
message: `no_auth returned ${noAuth.status}, expected 401 (--strict-401, baseline ${baseline.status})`,
|
|
198
|
+
evidence: { variant: "no_auth_strict", baseline_status: baseline.status, no_auth_status: noAuth.status, strict_401: true },
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
if (!isAcceptableRejection(bogus.status, true) && bogusBucket >= 0) {
|
|
202
|
+
return {
|
|
203
|
+
kind: "fail",
|
|
204
|
+
message: `bogus_auth returned ${bogus.status}, expected 401 (--strict-401, baseline ${baseline.status})`,
|
|
205
|
+
evidence: { variant: "bogus_auth_strict", baseline_status: baseline.status, bogus_auth_status: bogus.status, strict_401: true },
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return { kind: "pass" };
|
|
210
|
+
},
|
|
211
|
+
};
|