vibecode-anything 0.3.0 → 0.4.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/README.md +239 -67
- package/dist/cli.js +2140 -73
- package/dist/core/adapters.d.ts +26 -3
- package/dist/core/adapters.js +411 -25
- package/dist/core/agent-adapter.d.ts +67 -0
- package/dist/core/agent-adapter.js +312 -0
- package/dist/core/anti-cheat.d.ts +6 -0
- package/dist/core/anti-cheat.js +372 -0
- package/dist/core/badge.d.ts +18 -0
- package/dist/core/badge.js +360 -0
- package/dist/core/cache.d.ts +47 -0
- package/dist/core/cache.js +403 -0
- package/dist/core/ci-export.d.ts +35 -0
- package/dist/core/ci-export.js +796 -0
- package/dist/core/ci-formats.d.ts +21 -0
- package/dist/core/ci-formats.js +388 -0
- package/dist/core/client-codegen.d.ts +36 -0
- package/dist/core/client-codegen.js +2538 -0
- package/dist/core/compose.d.ts +71 -0
- package/dist/core/compose.js +1088 -0
- package/dist/core/docs-export.d.ts +37 -0
- package/dist/core/docs-export.js +2706 -0
- package/dist/core/doctor.d.ts +21 -1
- package/dist/core/doctor.js +234 -21
- package/dist/core/exec.d.ts +26 -2
- package/dist/core/exec.js +235 -8
- package/dist/core/fixtures.d.ts +153 -0
- package/dist/core/fixtures.js +2245 -0
- package/dist/core/format.d.ts +77 -0
- package/dist/core/format.js +632 -0
- package/dist/core/fs-snapshot.d.ts +37 -0
- package/dist/core/fs-snapshot.js +216 -0
- package/dist/core/graphql-export.d.ts +34 -0
- package/dist/core/graphql-export.js +2482 -0
- package/dist/core/har-export.d.ts +29 -0
- package/dist/core/har-export.js +988 -0
- package/dist/core/html-report.d.ts +27 -0
- package/dist/core/html-report.js +1535 -0
- package/dist/core/http-export.d.ts +31 -0
- package/dist/core/http-export.js +726 -0
- package/dist/core/impact.d.ts +80 -0
- package/dist/core/impact.js +1412 -0
- package/dist/core/lint.d.ts +38 -0
- package/dist/core/lint.js +1111 -0
- package/dist/core/load-export.d.ts +37 -0
- package/dist/core/load-export.js +1813 -0
- package/dist/core/mcp-export.d.ts +35 -0
- package/dist/core/mcp-export.js +1736 -0
- package/dist/core/mock-server.d.ts +47 -0
- package/dist/core/mock-server.js +1722 -0
- package/dist/core/openapi-export.d.ts +44 -0
- package/dist/core/openapi-export.js +1074 -0
- package/dist/core/parity.d.ts +6 -1
- package/dist/core/parity.js +36 -1
- package/dist/core/playwright-exporter.d.ts +23 -0
- package/dist/core/playwright-exporter.js +1343 -0
- package/dist/core/png.js +45 -9
- package/dist/core/regression.d.ts +54 -0
- package/dist/core/regression.js +529 -0
- package/dist/core/responsive.d.ts +86 -0
- package/dist/core/responsive.js +606 -0
- package/dist/core/run.d.ts +74 -0
- package/dist/core/run.js +696 -0
- package/dist/core/scheduler.d.ts +24 -0
- package/dist/core/scheduler.js +162 -0
- package/dist/core/schema-export.d.ts +29 -0
- package/dist/core/schema-export.js +2215 -0
- package/dist/core/schema.d.ts +3 -2
- package/dist/core/schema.js +460 -17
- package/dist/core/security.d.ts +233 -0
- package/dist/core/security.js +1839 -0
- package/dist/core/server-export.d.ts +38 -0
- package/dist/core/server-export.js +3198 -0
- package/dist/core/spec-writer.d.ts +9 -0
- package/dist/core/spec-writer.js +1202 -0
- package/dist/core/store.d.ts +11 -2
- package/dist/core/store.js +128 -8
- package/dist/core/target-map.d.ts +34 -0
- package/dist/core/target-map.js +262 -0
- package/dist/core/test-exporter.d.ts +25 -0
- package/dist/core/test-exporter.js +1918 -0
- package/dist/core/types.d.ts +182 -14
- package/dist/core/visual-semantic.d.ts +67 -0
- package/dist/core/visual-semantic.js +673 -0
- package/dist/core/visual.js +65 -16
- package/dist/core/watch.d.ts +28 -3
- package/dist/core/watch.js +222 -12
- package/dist/core/yaml.d.ts +5 -0
- package/dist/core/yaml.js +1009 -0
- package/dist/diagnostics/diagnose.d.ts +53 -0
- package/dist/diagnostics/diagnose.js +1083 -0
- package/dist/diagnostics/flake.d.ts +71 -0
- package/dist/diagnostics/flake.js +608 -0
- package/dist/diagnostics/mutation-audit.d.ts +69 -0
- package/dist/diagnostics/mutation-audit.js +769 -0
- package/dist/diagnostics/spatial.d.ts +42 -0
- package/dist/diagnostics/spatial.js +346 -0
- package/dist/diagnostics/trends.d.ts +122 -0
- package/dist/diagnostics/trends.js +1190 -0
- package/dist/discovery/discover.d.ts +111 -0
- package/dist/discovery/discover.js +2017 -0
- package/dist/discovery/lock.d.ts +116 -0
- package/dist/discovery/lock.js +1505 -0
- package/dist/discovery/routes.d.ts +36 -0
- package/dist/discovery/routes.js +1494 -0
- package/dist/discovery/test-importer.d.ts +92 -0
- package/dist/discovery/test-importer.js +4952 -0
- package/dist/index.d.ts +123 -0
- package/dist/index.js +99 -0
- package/dist/ingestors/api-collections.d.ts +28 -0
- package/dist/ingestors/api-collections.js +2335 -0
- package/dist/ingestors/api-repo.d.ts +2 -1
- package/dist/ingestors/api-repo.js +483 -85
- package/dist/ingestors/asyncapi.d.ts +26 -0
- package/dist/ingestors/asyncapi.js +2105 -0
- package/dist/ingestors/bridges.d.ts +2 -0
- package/dist/ingestors/bridges.js +702 -0
- package/dist/ingestors/cli.d.ts +2 -1
- package/dist/ingestors/cli.js +234 -46
- package/dist/ingestors/curl.d.ts +14 -0
- package/dist/ingestors/curl.js +1024 -0
- package/dist/ingestors/graphql.d.ts +56 -0
- package/dist/ingestors/graphql.js +1852 -0
- package/dist/ingestors/http-file.d.ts +20 -0
- package/dist/ingestors/http-file.js +923 -0
- package/dist/ingestors/index.js +486 -6
- package/dist/ingestors/openrpc.d.ts +29 -0
- package/dist/ingestors/openrpc.js +1807 -0
- package/dist/ingestors/proto.d.ts +47 -0
- package/dist/ingestors/proto.js +2690 -0
- package/dist/ingestors/trace.d.ts +10 -0
- package/dist/ingestors/trace.js +599 -0
- package/dist/ingestors/video.d.ts +4 -1
- package/dist/ingestors/video.js +86 -36
- package/dist/ingestors/web.d.ts +16 -1
- package/dist/ingestors/web.js +474 -32
- package/dist/mcp/server.js +3532 -67
- package/dist/oracles/api-repo.js +102 -30
- package/dist/oracles/cli.js +65 -17
- package/dist/oracles/http-workflow.d.ts +68 -0
- package/dist/oracles/http-workflow.js +828 -0
- package/dist/oracles/image.js +178 -28
- package/dist/oracles/index.d.ts +3 -1
- package/dist/oracles/index.js +243 -14
- package/dist/oracles/perf-budget.d.ts +90 -0
- package/dist/oracles/perf-budget.js +1329 -0
- package/dist/oracles/stream.d.ts +94 -0
- package/dist/oracles/stream.js +1268 -0
- package/dist/oracles/ui-states.d.ts +49 -0
- package/dist/oracles/ui-states.js +894 -0
- package/dist/oracles/video.d.ts +1 -1
- package/dist/oracles/video.js +232 -16
- package/dist/oracles/web.js +464 -73
- package/dist/plugins/builtins.d.ts +12 -0
- package/dist/plugins/builtins.js +1052 -0
- package/dist/plugins/registry.d.ts +86 -0
- package/dist/plugins/registry.js +554 -0
- package/dist/setup/index.js +145 -33
- package/package.json +56 -47
- package/skills/vibecode-anything/SKILL.md +75 -52
|
@@ -0,0 +1,1807 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { VibecodeError, errorMessage } from "../core/errors.js";
|
|
4
|
+
import { validateContract } from "../core/schema.js";
|
|
5
|
+
import { parseYaml } from "../core/yaml.js";
|
|
6
|
+
import { isSensitiveHeader, isSensitiveQueryParam, mergeHeaders, redactHeaders } from "./trace.js";
|
|
7
|
+
const MAX_SOURCE_BYTES = 2 * 1024 * 1024;
|
|
8
|
+
const MAX_REF_DEPTH = 12;
|
|
9
|
+
const MAX_SCHEMA_DEPTH = 16;
|
|
10
|
+
const MAX_METHODS = 100;
|
|
11
|
+
const MAX_SAMPLE_DEPTH = 10;
|
|
12
|
+
const MAX_SANITIZE_DEPTH = 32;
|
|
13
|
+
const MAX_SCHEMA_KEYS = 200;
|
|
14
|
+
const MAX_NODE_BUDGET = 4000;
|
|
15
|
+
const OPENRPC_VERSION_RE = /^1\.\d+(?:\.\d+)?(?:-[0-9A-Za-z.-]+)?$/;
|
|
16
|
+
const OPENRPC_YAML_HEADER_RE = /^\s*['"]?openrpc['"]?\s*:\s*['"]?1\.\d+/m;
|
|
17
|
+
const SIBLING_YAML_HEADER_RE = /^\s*['"]?(?:openapi|swagger|asyncapi|_type)['"]?\s*:/m;
|
|
18
|
+
const GRAPHQL_SDL_RE = /^\s*(?:type\s+(?:Query|Mutation|Subscription)\b|schema\s*\{|input\s+[A-Za-z_]\w*\s*\{|interface\s+[A-Za-z_]\w*\s*\{|scalar\s+[A-Za-z_]\w*|directive\s+@)/m;
|
|
19
|
+
const HEADER_TOKEN_RE = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
|
20
|
+
const CONTROL_CHAR_RE = /[\x00-\x1f\x7f]/;
|
|
21
|
+
const CONTROL_CHAR_GLOBAL_RE = /[\x00-\x1f\x7f]+/g;
|
|
22
|
+
const SENSITIVE_HEADER_RE = /(?:^x-hub-signature(?:-256)?$|^x-signature(?:-256|-sha256|-hmac)?$|^x-webhook-signature$|^x-webhook-secret$|^x-hmac(?:-signature|-sha256)?$|signature|hmac|secret|token|password|passwd|passphrase|api[-_]?key|apikey|auth|jwt|cookie|csrf|xsrf|private[-_]?key|signing[-_]?key|credential)/i;
|
|
23
|
+
const SENSITIVE_FIELD_RE = /(?:secret|hmac|signature|signing[-_]?key|api[-_]?key|apikey|api[-_]?token|access[-_]?token|refresh[-_]?token|auth[-_]?token|id[-_]?token|session[-_]?token|bearer[-_]?token|password|passwd|passphrase|private[-_]?key|client[-_]?secret|webhook[-_]?secret|vault[-_]?path|bearer|jwt|credential|cookie|set[-_]?cookie|session[-_]?id|authorization|proxy[-_]?authorization|csrf|xsrf)/i;
|
|
24
|
+
const SENSITIVE_QUERY_RE = /(?:hmac|webhook[-_]?secret|signing[-_]?key|secret|token|signature|sig|api[-_]?key|apikey|password|passwd|passphrase|auth|jwt|cookie|session[-_]?id|csrf|xsrf|credential)/i;
|
|
25
|
+
const SENSITIVE_HEADER_VALUE_RE = /^\s*(?:Bearer|Basic|Token|Digest|ApiKey|HMAC)\s+\S+|^(?:hmac-)?(?:sha512|sha256|sha1)=[0-9a-fA-F]{8,}|^t=\d+,v\d+=[0-9a-fA-F]{8,}|\b(?:whsec|sk_live|sk_test|pk_live|gh[pousr])_[A-Za-z0-9_=-]{4,}|\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/i;
|
|
26
|
+
const INLINE_AUTH_VALUE_RE = /\b(Bearer|Basic|Token|Digest|ApiKey|HMAC)\s+([^\s"'`<>,;]+)/gi;
|
|
27
|
+
const INLINE_TOKEN_RE = /\b(?:whsec|sk_live|sk_test|pk_live|gh[pousr])_[A-Za-z0-9_=-]{4,}|\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/g;
|
|
28
|
+
const INLINE_HMAC_SIG_RE = /\b(?:hmac-)?(?:sha512|sha256|sha1)=[0-9a-fA-F]{8,}|\bt=\d+,v\d+=[0-9a-fA-F]{8,}/gi;
|
|
29
|
+
const VAULT_PATH_RE = /(?:file:\/\/)?(?:[A-Za-z]:)?(?:[\\/][^\\/\s"'`<>|]*)*[\\/]?\.vibecode-vault(?:[\\/][^\\/\s"'`<>|]*)*/gi;
|
|
30
|
+
// Greedy userinfo capture before authority delimiter so multi-@, non-HTTP RFC 3986, and protocol-relative // credentials are stripped.
|
|
31
|
+
const URL_USERINFO_RE = /([a-z][a-z0-9+.-]*:\/\/|(?:^|[\s"'`(\[{=])\/\/)([^/\s"'`<>]+)@/gi;
|
|
32
|
+
const INLINE_URL_RE = /[a-z][a-z0-9+.-]*:\/\/[^\s"'`<>)\]}]+/gi;
|
|
33
|
+
const INLINE_SENSITIVE_QUERY_RE = /([?&])([^=&\s#"'`<>]+)=([^&\s#"'`<>]+)/g;
|
|
34
|
+
const INLINE_SENSITIVE_HEADER_RE = /\b(?:(Cookie|Set-Cookie)\s*[:=]\s*[^\s,\r\n;]+(?:\s*;\s*[A-Za-z0-9_.-]+(?:=[^\s,\r\n;]+)?)*|(Authorization|Proxy-Authorization)\s*[:=]\s*(?:[A-Za-z][A-Za-z0-9_-]*\s+(?:\[REDACTED\]|[^\s,;]+)|\[REDACTED\]|[^\s,;]+)|(X-API-Key|X-Auth-Token|X-Access-Token|X-Session-Token|X-Secret|X-CSRF-Token|X-XSRF-Token|CSRF-Token|XSRF-Token|X-Hub-Signature(?:-256)?|X-Signature(?:-256|-SHA256|-HMAC)?|X-HMAC(?:-SHA256|-Signature)?|X-Webhook-(?:Signature|Secret))\s*[:=]\s*(?:(?:Bearer|Basic|Token|ApiKey|Digest|HMAC)\s+(?:\[REDACTED\]|[^\s,;]+)|\[REDACTED\]|[^\s,;]+))/gi;
|
|
35
|
+
const INLINE_JSON_SECRET_KV_RE = /(["'])([A-Za-z_][A-Za-z0-9_.-]*)\1\s*:\s*(?:(")((?:\\.|[^"\\\r\n])*)"|(')((?:\\.|[^'\\\r\n])*)')/g;
|
|
36
|
+
const ENV_SECRET_ASSIGN_RE = /\b([A-Z0-9_]*(?:SECRET|HMAC|SIGNATURE|TOKEN|API_KEY|APIKEY|PASSWORD|PASSWD|PASSPHRASE|PRIVATE_KEY|SIGNING_KEY|WEBHOOK_KEY|COOKIE|SESSION_ID|CSRF|XSRF|CREDENTIAL)[A-Z0-9_]*)\s*=\s*([^\s"'`,;]+)/gi;
|
|
37
|
+
const STRUCTURAL_SCHEMA_STRING_KEYS = new Set(["type", "format", "pattern"]);
|
|
38
|
+
function hasOwn(obj, key) {
|
|
39
|
+
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
40
|
+
}
|
|
41
|
+
function isObject(value) {
|
|
42
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
43
|
+
}
|
|
44
|
+
function isUnsafeKey(key) {
|
|
45
|
+
return key === "__proto__" || key === "constructor" || key === "prototype";
|
|
46
|
+
}
|
|
47
|
+
function safeGet(obj, key) {
|
|
48
|
+
if (!key || isUnsafeKey(key) || !hasOwn(obj, key))
|
|
49
|
+
return undefined;
|
|
50
|
+
return obj[key];
|
|
51
|
+
}
|
|
52
|
+
function stripBom(raw) {
|
|
53
|
+
return raw.charCodeAt(0) === 0xfeff ? raw.slice(1) : raw;
|
|
54
|
+
}
|
|
55
|
+
function pushWarning(ctx, message) {
|
|
56
|
+
const clean = message.trim();
|
|
57
|
+
if (!clean || ctx.warningSet.has(clean))
|
|
58
|
+
return;
|
|
59
|
+
ctx.warningSet.add(clean);
|
|
60
|
+
ctx.warnings.push(clean);
|
|
61
|
+
}
|
|
62
|
+
function isOpenRpcSensitiveHeader(name) {
|
|
63
|
+
const lower = name.trim().toLowerCase();
|
|
64
|
+
if (!lower)
|
|
65
|
+
return false;
|
|
66
|
+
return isSensitiveHeader(lower) || SENSITIVE_HEADER_RE.test(lower);
|
|
67
|
+
}
|
|
68
|
+
function isSensitiveFieldName(name) {
|
|
69
|
+
const trimmed = name.trim();
|
|
70
|
+
if (!trimmed)
|
|
71
|
+
return false;
|
|
72
|
+
return SENSITIVE_FIELD_RE.test(trimmed);
|
|
73
|
+
}
|
|
74
|
+
function isOpenRpcSensitiveQueryParam(name) {
|
|
75
|
+
const trimmed = name.trim();
|
|
76
|
+
if (!trimmed)
|
|
77
|
+
return false;
|
|
78
|
+
return isSensitiveQueryParam(trimmed) || SENSITIVE_QUERY_RE.test(trimmed);
|
|
79
|
+
}
|
|
80
|
+
function recordSecretValue(secretValues, raw) {
|
|
81
|
+
if (typeof raw !== "string")
|
|
82
|
+
return;
|
|
83
|
+
const trimmed = raw.trim();
|
|
84
|
+
if (!trimmed || trimmed === "[REDACTED]")
|
|
85
|
+
return;
|
|
86
|
+
if (trimmed.length >= 3) {
|
|
87
|
+
secretValues.add(trimmed);
|
|
88
|
+
}
|
|
89
|
+
const schemeMatch = /^(?:Bearer|Basic|Token|Digest|ApiKey|HMAC)\s+(.+)$/i.exec(trimmed);
|
|
90
|
+
if (schemeMatch && schemeMatch[1].trim().length >= 3) {
|
|
91
|
+
secretValues.add(schemeMatch[1].trim());
|
|
92
|
+
}
|
|
93
|
+
const sigMatch = /^(?:hmac-)?(?:sha512|sha256|sha1)=([0-9a-fA-F]{6,})$/i.exec(trimmed);
|
|
94
|
+
if (sigMatch && sigMatch[1]) {
|
|
95
|
+
secretValues.add(sigMatch[1]);
|
|
96
|
+
}
|
|
97
|
+
if (trimmed.includes("=")) {
|
|
98
|
+
for (const part of trimmed.split(/[;,]/)) {
|
|
99
|
+
const eqIdx = part.indexOf("=");
|
|
100
|
+
if (eqIdx > 0) {
|
|
101
|
+
const val = part.slice(eqIdx + 1).trim();
|
|
102
|
+
if (val.length >= 3 && val !== "[REDACTED]") {
|
|
103
|
+
secretValues.add(val);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function recordUserinfoSecrets(secretValues, userinfo) {
|
|
110
|
+
const colonIdx = userinfo.indexOf(":");
|
|
111
|
+
const rawUser = colonIdx >= 0 ? userinfo.slice(0, colonIdx) : userinfo;
|
|
112
|
+
const rawPass = colonIdx >= 0 ? userinfo.slice(colonIdx + 1) : "";
|
|
113
|
+
try {
|
|
114
|
+
recordSecretValue(secretValues, decodeURIComponent(userinfo));
|
|
115
|
+
recordSecretValue(secretValues, decodeURIComponent(rawUser));
|
|
116
|
+
if (rawPass)
|
|
117
|
+
recordSecretValue(secretValues, decodeURIComponent(rawPass));
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
recordSecretValue(secretValues, userinfo);
|
|
121
|
+
recordSecretValue(secretValues, rawUser);
|
|
122
|
+
if (rawPass)
|
|
123
|
+
recordSecretValue(secretValues, rawPass);
|
|
124
|
+
}
|
|
125
|
+
recordSecretValue(secretValues, userinfo);
|
|
126
|
+
recordSecretValue(secretValues, rawUser);
|
|
127
|
+
if (rawPass) {
|
|
128
|
+
recordSecretValue(secretValues, rawPass);
|
|
129
|
+
const lastColon = rawPass.lastIndexOf(":");
|
|
130
|
+
if (lastColon >= 0)
|
|
131
|
+
recordSecretValue(secretValues, rawPass.slice(lastColon + 1));
|
|
132
|
+
for (const seg of rawPass.split("@")) {
|
|
133
|
+
if (seg.length >= 3)
|
|
134
|
+
recordSecretValue(secretValues, seg);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function sanitizeParsedValue(value, depth = 0) {
|
|
139
|
+
if (depth >= MAX_SANITIZE_DEPTH)
|
|
140
|
+
return null;
|
|
141
|
+
if (value === null || typeof value !== "object")
|
|
142
|
+
return value;
|
|
143
|
+
if (Array.isArray(value)) {
|
|
144
|
+
return value.map((item) => sanitizeParsedValue(item, depth + 1));
|
|
145
|
+
}
|
|
146
|
+
const src = value;
|
|
147
|
+
const out = {};
|
|
148
|
+
for (const key of Object.keys(src)) {
|
|
149
|
+
if (isUnsafeKey(key))
|
|
150
|
+
continue;
|
|
151
|
+
out[key] = sanitizeParsedValue(src[key], depth + 1);
|
|
152
|
+
}
|
|
153
|
+
return out;
|
|
154
|
+
}
|
|
155
|
+
function normalizeEndpointPath(rawPath) {
|
|
156
|
+
const noCtrl = rawPath.trim().replace(CONTROL_CHAR_GLOBAL_RE, "");
|
|
157
|
+
if (!noCtrl)
|
|
158
|
+
return "/";
|
|
159
|
+
const segments = noCtrl.split("/").filter((s) => s.length > 0 && s !== "." && s !== "..");
|
|
160
|
+
if (segments.length === 0)
|
|
161
|
+
return "/";
|
|
162
|
+
return `/${segments.join("/")}`;
|
|
163
|
+
}
|
|
164
|
+
function sanitizeUrlString(rawUrl, secretValues, options = {}) {
|
|
165
|
+
let redacted = false;
|
|
166
|
+
let cleaned = rawUrl.trim().replace(CONTROL_CHAR_GLOBAL_RE, "");
|
|
167
|
+
if (!cleaned) {
|
|
168
|
+
return { safeUrl: "", origin: "", pathname: "/", redacted: false, valid: false };
|
|
169
|
+
}
|
|
170
|
+
// Strip raw userinfo before URL parsing (handles multi-@, RFC 3986 schemes, protocol-relative //, and templated URLs)
|
|
171
|
+
cleaned = cleaned.replace(URL_USERINFO_RE, (_m, scheme, userinfo) => {
|
|
172
|
+
redacted = true;
|
|
173
|
+
recordUserinfoSecrets(secretValues, userinfo);
|
|
174
|
+
return scheme;
|
|
175
|
+
});
|
|
176
|
+
if (cleaned.includes("#")) {
|
|
177
|
+
const hashIdx = cleaned.indexOf("#");
|
|
178
|
+
const fragment = cleaned.slice(hashIdx + 1);
|
|
179
|
+
if (fragment.length > 0) {
|
|
180
|
+
redacted = true;
|
|
181
|
+
recordSecretValue(secretValues, fragment);
|
|
182
|
+
}
|
|
183
|
+
cleaned = cleaned.slice(0, hashIdx);
|
|
184
|
+
}
|
|
185
|
+
try {
|
|
186
|
+
const normalized = /^[a-z][a-z0-9+.-]*:\/\//i.test(cleaned)
|
|
187
|
+
? cleaned
|
|
188
|
+
: cleaned.startsWith("//")
|
|
189
|
+
? `https:${cleaned}`
|
|
190
|
+
: `https://${cleaned}`;
|
|
191
|
+
const parsed = new URL(normalized);
|
|
192
|
+
if (options.requireHttp !== false && parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
193
|
+
return { safeUrl: "", origin: "", pathname: "/", redacted, valid: false };
|
|
194
|
+
}
|
|
195
|
+
if (parsed.username || parsed.password) {
|
|
196
|
+
redacted = true;
|
|
197
|
+
recordSecretValue(secretValues, parsed.username);
|
|
198
|
+
recordSecretValue(secretValues, parsed.password);
|
|
199
|
+
parsed.username = "";
|
|
200
|
+
parsed.password = "";
|
|
201
|
+
}
|
|
202
|
+
if (parsed.hash) {
|
|
203
|
+
redacted = true;
|
|
204
|
+
recordSecretValue(secretValues, parsed.hash.slice(1));
|
|
205
|
+
parsed.hash = "";
|
|
206
|
+
}
|
|
207
|
+
const safeParams = new URLSearchParams();
|
|
208
|
+
for (const [k, v] of parsed.searchParams.entries()) {
|
|
209
|
+
if (isUnsafeKey(k))
|
|
210
|
+
continue;
|
|
211
|
+
if (isOpenRpcSensitiveQueryParam(k) || SENSITIVE_HEADER_VALUE_RE.test(v)) {
|
|
212
|
+
redacted = true;
|
|
213
|
+
recordSecretValue(secretValues, v);
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
safeParams.append(k, v);
|
|
217
|
+
}
|
|
218
|
+
const queryStr = safeParams.toString();
|
|
219
|
+
const normPath = normalizeEndpointPath(parsed.pathname || "/");
|
|
220
|
+
const basePath = normPath === "/" ? "" : normPath;
|
|
221
|
+
const safeUrl = `${parsed.origin}${basePath}${queryStr ? `?${queryStr}` : ""}`;
|
|
222
|
+
return {
|
|
223
|
+
safeUrl,
|
|
224
|
+
origin: parsed.origin,
|
|
225
|
+
pathname: normPath,
|
|
226
|
+
redacted,
|
|
227
|
+
valid: true
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
// Also scrub sensitive query params from malformed/templated URLs so secrets are recorded
|
|
232
|
+
const qIdx = cleaned.indexOf("?");
|
|
233
|
+
if (qIdx >= 0) {
|
|
234
|
+
cleaned.slice(qIdx).replace(INLINE_SENSITIVE_QUERY_RE, (match, _sep, qKey, qVal) => {
|
|
235
|
+
if (isOpenRpcSensitiveQueryParam(qKey) || SENSITIVE_HEADER_VALUE_RE.test(qVal)) {
|
|
236
|
+
redacted = true;
|
|
237
|
+
recordSecretValue(secretValues, qVal);
|
|
238
|
+
}
|
|
239
|
+
return match;
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
return { safeUrl: "", origin: "", pathname: "/", redacted, valid: false };
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
function scrubText(text, secretValues) {
|
|
246
|
+
let redacted = false;
|
|
247
|
+
let out = text.replace(VAULT_PATH_RE, () => {
|
|
248
|
+
redacted = true;
|
|
249
|
+
return "[REDACTED]";
|
|
250
|
+
});
|
|
251
|
+
out = out.replace(INLINE_URL_RE, (rawMatch) => {
|
|
252
|
+
const res = sanitizeUrlString(rawMatch, secretValues, { requireHttp: false });
|
|
253
|
+
if (res.redacted)
|
|
254
|
+
redacted = true;
|
|
255
|
+
if (res.valid && res.redacted)
|
|
256
|
+
return res.safeUrl;
|
|
257
|
+
const hashIdx = rawMatch.indexOf("#");
|
|
258
|
+
let fallback = hashIdx >= 0
|
|
259
|
+
? (recordSecretValue(secretValues, rawMatch.slice(hashIdx + 1)), rawMatch.slice(0, hashIdx))
|
|
260
|
+
: rawMatch;
|
|
261
|
+
if (hashIdx >= 0 && rawMatch.length > hashIdx + 1)
|
|
262
|
+
redacted = true;
|
|
263
|
+
fallback = fallback.replace(URL_USERINFO_RE, (_m, scheme, userinfo) => {
|
|
264
|
+
redacted = true;
|
|
265
|
+
recordUserinfoSecrets(secretValues, userinfo);
|
|
266
|
+
return `${scheme}[REDACTED]@`;
|
|
267
|
+
});
|
|
268
|
+
fallback = fallback.replace(INLINE_SENSITIVE_QUERY_RE, (match, sep, qKey, qVal) => {
|
|
269
|
+
if (!isOpenRpcSensitiveQueryParam(qKey) && !SENSITIVE_HEADER_VALUE_RE.test(qVal))
|
|
270
|
+
return match;
|
|
271
|
+
redacted = true;
|
|
272
|
+
recordSecretValue(secretValues, qVal);
|
|
273
|
+
return `${sep}${qKey}=[REDACTED]`;
|
|
274
|
+
});
|
|
275
|
+
return fallback;
|
|
276
|
+
});
|
|
277
|
+
out = out.replace(URL_USERINFO_RE, (_m, scheme, userinfo) => {
|
|
278
|
+
redacted = true;
|
|
279
|
+
recordUserinfoSecrets(secretValues, userinfo);
|
|
280
|
+
return `${scheme}[REDACTED]@`;
|
|
281
|
+
});
|
|
282
|
+
out = out.replace(INLINE_SENSITIVE_QUERY_RE, (match, sep, qKey, qVal) => {
|
|
283
|
+
if (!isOpenRpcSensitiveQueryParam(qKey) && !SENSITIVE_HEADER_VALUE_RE.test(qVal))
|
|
284
|
+
return match;
|
|
285
|
+
redacted = true;
|
|
286
|
+
recordSecretValue(secretValues, qVal);
|
|
287
|
+
return `${sep}${qKey}=[REDACTED]`;
|
|
288
|
+
});
|
|
289
|
+
out = out.replace(INLINE_AUTH_VALUE_RE, (match, scheme, val) => {
|
|
290
|
+
if (val === "[REDACTED]")
|
|
291
|
+
return match;
|
|
292
|
+
redacted = true;
|
|
293
|
+
recordSecretValue(secretValues, val);
|
|
294
|
+
return `${scheme} [REDACTED]`;
|
|
295
|
+
});
|
|
296
|
+
out = out.replace(INLINE_SENSITIVE_HEADER_RE, (match, cookieHdr, authHdr, otherHdr) => {
|
|
297
|
+
const hdrName = cookieHdr || authHdr || otherHdr || "Authorization";
|
|
298
|
+
const sepIdx = match.search(/[:=]/);
|
|
299
|
+
const valPart = sepIdx >= 0 ? match.slice(sepIdx + 1).trim() : "";
|
|
300
|
+
if (valPart === "[REDACTED]")
|
|
301
|
+
return match;
|
|
302
|
+
redacted = true;
|
|
303
|
+
if (valPart) {
|
|
304
|
+
recordSecretValue(secretValues, valPart);
|
|
305
|
+
}
|
|
306
|
+
return `${hdrName}: [REDACTED]`;
|
|
307
|
+
});
|
|
308
|
+
out = out.replace(INLINE_TOKEN_RE, (match) => {
|
|
309
|
+
redacted = true;
|
|
310
|
+
recordSecretValue(secretValues, match);
|
|
311
|
+
return "[REDACTED]";
|
|
312
|
+
});
|
|
313
|
+
out = out.replace(INLINE_HMAC_SIG_RE, (match) => {
|
|
314
|
+
redacted = true;
|
|
315
|
+
recordSecretValue(secretValues, match);
|
|
316
|
+
return "[REDACTED]";
|
|
317
|
+
});
|
|
318
|
+
out = out.replace(INLINE_JSON_SECRET_KV_RE, (match, q1, key, dq, dv, sq, sv) => {
|
|
319
|
+
if (!isSensitiveFieldName(key))
|
|
320
|
+
return match;
|
|
321
|
+
const q2 = dq ?? sq ?? '"';
|
|
322
|
+
const innerVal = dv ?? sv ?? "";
|
|
323
|
+
if (innerVal === "[REDACTED]")
|
|
324
|
+
return match;
|
|
325
|
+
redacted = true;
|
|
326
|
+
if (innerVal) {
|
|
327
|
+
recordSecretValue(secretValues, innerVal);
|
|
328
|
+
}
|
|
329
|
+
return `${q1}${key}${q1}: ${q2}[REDACTED]${q2}`;
|
|
330
|
+
});
|
|
331
|
+
out = out.replace(ENV_SECRET_ASSIGN_RE, (match, envName, envVal) => {
|
|
332
|
+
if (envVal === "[REDACTED]")
|
|
333
|
+
return match;
|
|
334
|
+
redacted = true;
|
|
335
|
+
recordSecretValue(secretValues, envVal);
|
|
336
|
+
return `${envName}=[REDACTED]`;
|
|
337
|
+
});
|
|
338
|
+
const sorted = [...secretValues]
|
|
339
|
+
.filter((s) => s.length >= 3 && s !== "[REDACTED]")
|
|
340
|
+
.sort((a, b) => b.length - a.length);
|
|
341
|
+
for (const secret of sorted) {
|
|
342
|
+
if (out.includes(secret)) {
|
|
343
|
+
out = out.replaceAll(secret, "[REDACTED]");
|
|
344
|
+
redacted = true;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return { value: out, redacted };
|
|
348
|
+
}
|
|
349
|
+
function scrubValueDeep(value, secretValues, depth = 0, isSensitiveParent = false) {
|
|
350
|
+
if (depth >= MAX_SANITIZE_DEPTH)
|
|
351
|
+
return { value: null, redacted: false };
|
|
352
|
+
if (typeof value === "string") {
|
|
353
|
+
if (isSensitiveParent) {
|
|
354
|
+
if (value && value !== "[REDACTED]") {
|
|
355
|
+
recordSecretValue(secretValues, value);
|
|
356
|
+
return { value: "[REDACTED]", redacted: true };
|
|
357
|
+
}
|
|
358
|
+
return { value: "[REDACTED]", redacted: false };
|
|
359
|
+
}
|
|
360
|
+
const trimmedJson = value.trim();
|
|
361
|
+
if (depth + 1 < MAX_SANITIZE_DEPTH &&
|
|
362
|
+
trimmedJson.length <= 65536 &&
|
|
363
|
+
((trimmedJson.startsWith("{") && trimmedJson.endsWith("}")) ||
|
|
364
|
+
(trimmedJson.startsWith("[") && trimmedJson.endsWith("]")))) {
|
|
365
|
+
try {
|
|
366
|
+
const parsedJson = JSON.parse(trimmedJson);
|
|
367
|
+
if (parsedJson !== null && typeof parsedJson === "object") {
|
|
368
|
+
const nested = scrubValueDeep(parsedJson, secretValues, depth + 1, false);
|
|
369
|
+
if (nested.redacted) {
|
|
370
|
+
return { value: JSON.stringify(nested.value), redacted: true };
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
catch {
|
|
375
|
+
// Fall through to text scrubbing
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
return scrubText(value, secretValues);
|
|
379
|
+
}
|
|
380
|
+
if (value === null || typeof value !== "object") {
|
|
381
|
+
return { value, redacted: false };
|
|
382
|
+
}
|
|
383
|
+
if (Array.isArray(value)) {
|
|
384
|
+
let redacted = false;
|
|
385
|
+
const arr = value.map((item) => {
|
|
386
|
+
const res = scrubValueDeep(item, secretValues, depth + 1, isSensitiveParent);
|
|
387
|
+
if (res.redacted)
|
|
388
|
+
redacted = true;
|
|
389
|
+
return res.value;
|
|
390
|
+
});
|
|
391
|
+
return { value: arr, redacted };
|
|
392
|
+
}
|
|
393
|
+
const src = value;
|
|
394
|
+
const out = {};
|
|
395
|
+
let redacted = false;
|
|
396
|
+
for (const key of Object.keys(src)) {
|
|
397
|
+
if (isUnsafeKey(key))
|
|
398
|
+
continue;
|
|
399
|
+
const childSensitive = isSensitiveParent || isSensitiveFieldName(key);
|
|
400
|
+
const res = scrubValueDeep(src[key], secretValues, depth + 1, childSensitive);
|
|
401
|
+
if (res.redacted)
|
|
402
|
+
redacted = true;
|
|
403
|
+
out[key] = res.value;
|
|
404
|
+
}
|
|
405
|
+
return { value: out, redacted };
|
|
406
|
+
}
|
|
407
|
+
function scrubSchemaDeep(schema, secretValues, depth = 0, inSensitiveProp = false) {
|
|
408
|
+
if (depth >= MAX_SANITIZE_DEPTH)
|
|
409
|
+
return undefined;
|
|
410
|
+
if (Array.isArray(schema)) {
|
|
411
|
+
return schema.map((item) => scrubSchemaDeep(item, secretValues, depth + 1, inSensitiveProp));
|
|
412
|
+
}
|
|
413
|
+
if (!isObject(schema))
|
|
414
|
+
return schema;
|
|
415
|
+
const out = {};
|
|
416
|
+
for (const key of Object.keys(schema)) {
|
|
417
|
+
if (isUnsafeKey(key))
|
|
418
|
+
continue;
|
|
419
|
+
const val = schema[key];
|
|
420
|
+
if ((key === "properties" || key === "$defs" || key === "definitions") && isObject(val)) {
|
|
421
|
+
const cleanedMap = {};
|
|
422
|
+
for (const propKey of Object.keys(val)) {
|
|
423
|
+
if (isUnsafeKey(propKey))
|
|
424
|
+
continue;
|
|
425
|
+
cleanedMap[propKey] = scrubSchemaDeep(val[propKey], secretValues, depth + 1, isSensitiveFieldName(propKey));
|
|
426
|
+
}
|
|
427
|
+
out[key] = cleanedMap;
|
|
428
|
+
}
|
|
429
|
+
else if (key === "items" ||
|
|
430
|
+
key === "prefixItems" ||
|
|
431
|
+
key === "not" ||
|
|
432
|
+
key === "allOf" ||
|
|
433
|
+
key === "anyOf" ||
|
|
434
|
+
key === "oneOf") {
|
|
435
|
+
out[key] = scrubSchemaDeep(val, secretValues, depth + 1, inSensitiveProp);
|
|
436
|
+
}
|
|
437
|
+
else if (key === "additionalProperties") {
|
|
438
|
+
out[key] = typeof val === "boolean" ? val : scrubSchemaDeep(val, secretValues, depth + 1, false);
|
|
439
|
+
}
|
|
440
|
+
else if (key === "const" || key === "default" || key === "example" || key === "examples" || key === "enum") {
|
|
441
|
+
if (inSensitiveProp) {
|
|
442
|
+
scrubValueDeep(val, secretValues, 0, true);
|
|
443
|
+
}
|
|
444
|
+
else {
|
|
445
|
+
out[key] = scrubValueDeep(val, secretValues).value;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
else if (key === "type") {
|
|
449
|
+
if (typeof val === "string") {
|
|
450
|
+
out.type = val;
|
|
451
|
+
}
|
|
452
|
+
else if (Array.isArray(val)) {
|
|
453
|
+
out.type = val.filter((item) => typeof item === "string");
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
else if (key === "required" && Array.isArray(val)) {
|
|
457
|
+
out.required = val.map(String).filter((r) => !isUnsafeKey(r));
|
|
458
|
+
}
|
|
459
|
+
else if (key === "description" || key === "title" || key === "$comment" || key === "$ref") {
|
|
460
|
+
out[key] = typeof val === "string" ? scrubText(val, secretValues).value : val;
|
|
461
|
+
}
|
|
462
|
+
else if (typeof val === "string" || typeof val === "number" || typeof val === "boolean" || val === null) {
|
|
463
|
+
out[key] = val;
|
|
464
|
+
}
|
|
465
|
+
else if (Array.isArray(val)) {
|
|
466
|
+
out[key] = val.map((item) => typeof item === "string" ? scrubText(item, secretValues).value : item);
|
|
467
|
+
}
|
|
468
|
+
else if (isObject(val)) {
|
|
469
|
+
out[key] = scrubSchemaDeep(val, secretValues, depth + 1, false);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return out;
|
|
473
|
+
}
|
|
474
|
+
function sanitizeHeaderEntry(rawName, rawValue, ctx) {
|
|
475
|
+
if (CONTROL_CHAR_RE.test(rawName)) {
|
|
476
|
+
// Reject any header whose name contains control/CRLF characters outright; still record any secrets
|
|
477
|
+
if (isOpenRpcSensitiveHeader(rawName) || SENSITIVE_HEADER_VALUE_RE.test(rawValue)) {
|
|
478
|
+
ctx.redactedSecrets = true;
|
|
479
|
+
recordSecretValue(ctx.secretValues, rawValue);
|
|
480
|
+
}
|
|
481
|
+
pushWarning(ctx, "Dropped header with invalid or control characters to prevent header injection.");
|
|
482
|
+
return null;
|
|
483
|
+
}
|
|
484
|
+
let hadControl = false;
|
|
485
|
+
if (CONTROL_CHAR_RE.test(rawValue)) {
|
|
486
|
+
hadControl = true;
|
|
487
|
+
}
|
|
488
|
+
const cleanName = rawName.trim();
|
|
489
|
+
if (!cleanName || isUnsafeKey(cleanName) || !HEADER_TOKEN_RE.test(cleanName)) {
|
|
490
|
+
pushWarning(ctx, "Dropped header with invalid name to prevent header injection.");
|
|
491
|
+
return null;
|
|
492
|
+
}
|
|
493
|
+
const rawValueNoCtrl = rawValue.replace(CONTROL_CHAR_GLOBAL_RE, "").trim();
|
|
494
|
+
if (hadControl) {
|
|
495
|
+
pushWarning(ctx, `Stripped control/CRLF characters from header "${cleanName}".`);
|
|
496
|
+
}
|
|
497
|
+
if (isOpenRpcSensitiveHeader(cleanName) || SENSITIVE_HEADER_VALUE_RE.test(rawValueNoCtrl)) {
|
|
498
|
+
ctx.redactedSecrets = true;
|
|
499
|
+
recordSecretValue(ctx.secretValues, rawValueNoCtrl);
|
|
500
|
+
return null;
|
|
501
|
+
}
|
|
502
|
+
if (rawValue.includes("\r") || rawValue.includes("\n")) {
|
|
503
|
+
const lines = rawValue.split(/[\r\n]+/);
|
|
504
|
+
for (let i = 1; i < lines.length; i += 1) {
|
|
505
|
+
const smuggledLine = lines[i]?.trim();
|
|
506
|
+
if (!smuggledLine)
|
|
507
|
+
continue;
|
|
508
|
+
const colonIdx = smuggledLine.indexOf(":");
|
|
509
|
+
if (colonIdx > 0) {
|
|
510
|
+
const smuggledName = smuggledLine.slice(0, colonIdx).trim();
|
|
511
|
+
const smuggledVal = smuggledLine.slice(colonIdx + 1).trim();
|
|
512
|
+
if (isOpenRpcSensitiveHeader(smuggledName) || SENSITIVE_HEADER_VALUE_RE.test(smuggledVal)) {
|
|
513
|
+
ctx.redactedSecrets = true;
|
|
514
|
+
recordSecretValue(ctx.secretValues, smuggledVal);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
const firstLine = (lines[0] ?? "").replace(CONTROL_CHAR_GLOBAL_RE, "").trim();
|
|
519
|
+
const scrubbed = scrubText(firstLine, ctx.secretValues);
|
|
520
|
+
if (scrubbed.redacted)
|
|
521
|
+
ctx.redactedSecrets = true;
|
|
522
|
+
return { name: cleanName, value: scrubbed.value };
|
|
523
|
+
}
|
|
524
|
+
const scrubbed = scrubText(rawValueNoCtrl, ctx.secretValues);
|
|
525
|
+
if (scrubbed.redacted)
|
|
526
|
+
ctx.redactedSecrets = true;
|
|
527
|
+
return { name: cleanName, value: scrubbed.value };
|
|
528
|
+
}
|
|
529
|
+
function hasHeaderCaseInsensitive(headers, name) {
|
|
530
|
+
const lower = name.toLowerCase();
|
|
531
|
+
return Object.keys(headers).some((k) => k.toLowerCase() === lower);
|
|
532
|
+
}
|
|
533
|
+
function setHeaderCaseInsensitive(headers, name, value) {
|
|
534
|
+
const lower = name.toLowerCase();
|
|
535
|
+
for (const k of Object.keys(headers)) {
|
|
536
|
+
if (k.toLowerCase() === lower && k !== name) {
|
|
537
|
+
delete headers[k];
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
headers[name] = value;
|
|
541
|
+
}
|
|
542
|
+
function deleteHeaderCaseInsensitive(headers, name) {
|
|
543
|
+
const lower = name.toLowerCase();
|
|
544
|
+
for (const k of Object.keys(headers)) {
|
|
545
|
+
if (k.toLowerCase() === lower) {
|
|
546
|
+
delete headers[k];
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
function decodeJsonPointerSegment(seg) {
|
|
551
|
+
return seg.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
552
|
+
}
|
|
553
|
+
function resolveJsonPointer(doc, ref) {
|
|
554
|
+
if (typeof ref !== "string" || !ref.startsWith("#/"))
|
|
555
|
+
return undefined;
|
|
556
|
+
const rawParts = ref.slice(2).split("/");
|
|
557
|
+
let cur = doc;
|
|
558
|
+
for (const rawPart of rawParts) {
|
|
559
|
+
if (cur === null || cur === undefined)
|
|
560
|
+
return undefined;
|
|
561
|
+
const decoded = decodeJsonPointerSegment(rawPart);
|
|
562
|
+
if (isUnsafeKey(decoded))
|
|
563
|
+
return undefined;
|
|
564
|
+
if (Array.isArray(cur)) {
|
|
565
|
+
const idx = Number(decoded);
|
|
566
|
+
if (!Number.isInteger(idx) || idx < 0 || idx >= cur.length)
|
|
567
|
+
return undefined;
|
|
568
|
+
cur = cur[idx];
|
|
569
|
+
}
|
|
570
|
+
else if (isObject(cur)) {
|
|
571
|
+
if (hasOwn(cur, decoded)) {
|
|
572
|
+
cur = cur[decoded];
|
|
573
|
+
}
|
|
574
|
+
else {
|
|
575
|
+
let uriDecoded = decoded;
|
|
576
|
+
try {
|
|
577
|
+
uriDecoded = decodeJsonPointerSegment(decodeURIComponent(rawPart));
|
|
578
|
+
}
|
|
579
|
+
catch {
|
|
580
|
+
return undefined;
|
|
581
|
+
}
|
|
582
|
+
if (isUnsafeKey(uriDecoded) || !hasOwn(cur, uriDecoded))
|
|
583
|
+
return undefined;
|
|
584
|
+
cur = cur[uriDecoded];
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
else {
|
|
588
|
+
return undefined;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
return cur;
|
|
592
|
+
}
|
|
593
|
+
function resolveObjectRef(value, ctx, visitedRefs = new Set(), depth = 0) {
|
|
594
|
+
if (!isObject(value))
|
|
595
|
+
return undefined;
|
|
596
|
+
const ref = safeGet(value, "$ref");
|
|
597
|
+
if (typeof ref !== "string")
|
|
598
|
+
return value;
|
|
599
|
+
if (depth >= MAX_REF_DEPTH) {
|
|
600
|
+
pushWarning(ctx, `Max $ref resolution depth (${MAX_REF_DEPTH}) exceeded at "${scrubText(ref, ctx.secretValues).value}".`);
|
|
601
|
+
return undefined;
|
|
602
|
+
}
|
|
603
|
+
if (!ref.startsWith("#/")) {
|
|
604
|
+
pushWarning(ctx, `Skipped external $ref "${scrubText(ref, ctx.secretValues).value}" (only local #/ refs are resolved).`);
|
|
605
|
+
return undefined;
|
|
606
|
+
}
|
|
607
|
+
if (visitedRefs.has(ref)) {
|
|
608
|
+
pushWarning(ctx, `Circular $ref detected at "${scrubText(ref, ctx.secretValues).value}".`);
|
|
609
|
+
return undefined;
|
|
610
|
+
}
|
|
611
|
+
const target = resolveJsonPointer(ctx.doc, ref);
|
|
612
|
+
if (!isObject(target)) {
|
|
613
|
+
pushWarning(ctx, `Unresolved local $ref "${scrubText(ref, ctx.secretValues).value}".`);
|
|
614
|
+
return undefined;
|
|
615
|
+
}
|
|
616
|
+
const nextVisited = new Set(visitedRefs);
|
|
617
|
+
nextVisited.add(ref);
|
|
618
|
+
const resolvedTarget = resolveObjectRef(target, ctx, nextVisited, depth + 1);
|
|
619
|
+
if (!resolvedTarget)
|
|
620
|
+
return undefined;
|
|
621
|
+
const merged = { ...resolvedTarget };
|
|
622
|
+
for (const key of Object.keys(value)) {
|
|
623
|
+
if (key === "$ref" || isUnsafeKey(key))
|
|
624
|
+
continue;
|
|
625
|
+
merged[key] = value[key];
|
|
626
|
+
}
|
|
627
|
+
return merged;
|
|
628
|
+
}
|
|
629
|
+
function mergeJsonSchemas(base, extra) {
|
|
630
|
+
const out = {};
|
|
631
|
+
for (const key of Object.keys(base)) {
|
|
632
|
+
if (!isUnsafeKey(key))
|
|
633
|
+
out[key] = base[key];
|
|
634
|
+
}
|
|
635
|
+
for (const key of Object.keys(extra)) {
|
|
636
|
+
if (!isUnsafeKey(key))
|
|
637
|
+
out[key] = extra[key];
|
|
638
|
+
}
|
|
639
|
+
const baseReq = Array.isArray(base.required) ? base.required.map(String).filter((k) => !isUnsafeKey(k)) : [];
|
|
640
|
+
const extraReq = Array.isArray(extra.required) ? extra.required.map(String).filter((k) => !isUnsafeKey(k)) : [];
|
|
641
|
+
const req = [...new Set([...baseReq, ...extraReq])];
|
|
642
|
+
if (req.length > 0)
|
|
643
|
+
out.required = req;
|
|
644
|
+
if (isObject(base.properties) || isObject(extra.properties)) {
|
|
645
|
+
const props = {};
|
|
646
|
+
if (isObject(base.properties)) {
|
|
647
|
+
for (const k of Object.keys(base.properties)) {
|
|
648
|
+
if (!isUnsafeKey(k))
|
|
649
|
+
props[k] = base.properties[k];
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
if (isObject(extra.properties)) {
|
|
653
|
+
for (const k of Object.keys(extra.properties)) {
|
|
654
|
+
if (!isUnsafeKey(k))
|
|
655
|
+
props[k] = extra.properties[k];
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
out.properties = props;
|
|
659
|
+
}
|
|
660
|
+
if (isObject(base.items) && isObject(extra.items)) {
|
|
661
|
+
out.items = mergeJsonSchemas(base.items, extra.items);
|
|
662
|
+
}
|
|
663
|
+
return out;
|
|
664
|
+
}
|
|
665
|
+
function resolveSchemaInternal(rawSchema, ctx, visitedRefs, refDepth, schemaDepth, fieldName = "") {
|
|
666
|
+
if (ctx.nodeBudget.remaining <= 0) {
|
|
667
|
+
pushWarning(ctx, "Schema node expansion budget reached; truncated remaining nested schema.");
|
|
668
|
+
return {};
|
|
669
|
+
}
|
|
670
|
+
ctx.nodeBudget.remaining -= 1;
|
|
671
|
+
if (rawSchema === true)
|
|
672
|
+
return {};
|
|
673
|
+
if (rawSchema === false)
|
|
674
|
+
return { not: {} };
|
|
675
|
+
if (!isObject(rawSchema))
|
|
676
|
+
return {};
|
|
677
|
+
if (schemaDepth > MAX_SCHEMA_DEPTH) {
|
|
678
|
+
pushWarning(ctx, `Max schema nesting depth (${MAX_SCHEMA_DEPTH}) exceeded; truncated nested schema.`);
|
|
679
|
+
return {};
|
|
680
|
+
}
|
|
681
|
+
const ref = safeGet(rawSchema, "$ref");
|
|
682
|
+
if (typeof ref === "string") {
|
|
683
|
+
if (refDepth >= MAX_REF_DEPTH) {
|
|
684
|
+
pushWarning(ctx, `Max $ref resolution depth (${MAX_REF_DEPTH}) exceeded at "${scrubText(ref, ctx.secretValues).value}".`);
|
|
685
|
+
return {};
|
|
686
|
+
}
|
|
687
|
+
if (!ref.startsWith("#/")) {
|
|
688
|
+
pushWarning(ctx, `Skipped external schema $ref "${scrubText(ref, ctx.secretValues).value}".`);
|
|
689
|
+
return {};
|
|
690
|
+
}
|
|
691
|
+
if (visitedRefs.has(ref)) {
|
|
692
|
+
pushWarning(ctx, `Circular schema $ref detected at "${scrubText(ref, ctx.secretValues).value}".`);
|
|
693
|
+
return {};
|
|
694
|
+
}
|
|
695
|
+
const target = resolveJsonPointer(ctx.doc, ref);
|
|
696
|
+
if (!isObject(target) && typeof target !== "boolean") {
|
|
697
|
+
pushWarning(ctx, `Unresolved schema $ref "${scrubText(ref, ctx.secretValues).value}".`);
|
|
698
|
+
return {};
|
|
699
|
+
}
|
|
700
|
+
const nextVisited = new Set(visitedRefs);
|
|
701
|
+
nextVisited.add(ref);
|
|
702
|
+
const resolvedTarget = resolveSchemaInternal(target, ctx, nextVisited, refDepth + 1, schemaDepth, fieldName);
|
|
703
|
+
const siblingKeys = Object.keys(rawSchema).filter((k) => k !== "$ref" && !isUnsafeKey(k));
|
|
704
|
+
if (siblingKeys.length === 0) {
|
|
705
|
+
return resolvedTarget;
|
|
706
|
+
}
|
|
707
|
+
const siblingObj = {};
|
|
708
|
+
for (const k of siblingKeys) {
|
|
709
|
+
siblingObj[k] = rawSchema[k];
|
|
710
|
+
}
|
|
711
|
+
const resolvedSibling = resolveSchemaInternal(siblingObj, ctx, nextVisited, refDepth + 1, schemaDepth, fieldName);
|
|
712
|
+
return mergeJsonSchemas(resolvedTarget, resolvedSibling);
|
|
713
|
+
}
|
|
714
|
+
if (Array.isArray(rawSchema.allOf)) {
|
|
715
|
+
let merged = {};
|
|
716
|
+
for (const part of rawSchema.allOf) {
|
|
717
|
+
const resolvedPart = resolveSchemaInternal(part, ctx, visitedRefs, refDepth, schemaDepth + 1, fieldName);
|
|
718
|
+
merged = mergeJsonSchemas(merged, resolvedPart);
|
|
719
|
+
}
|
|
720
|
+
const restKeys = Object.keys(rawSchema).filter((k) => k !== "allOf" && !isUnsafeKey(k));
|
|
721
|
+
if (restKeys.length === 0)
|
|
722
|
+
return merged;
|
|
723
|
+
const restObj = {};
|
|
724
|
+
for (const k of restKeys)
|
|
725
|
+
restObj[k] = rawSchema[k];
|
|
726
|
+
const resolvedRest = resolveSchemaInternal(restObj, ctx, visitedRefs, refDepth, schemaDepth + 1, fieldName);
|
|
727
|
+
return mergeJsonSchemas(merged, resolvedRest);
|
|
728
|
+
}
|
|
729
|
+
const sensitiveField = isSensitiveFieldName(fieldName);
|
|
730
|
+
const out = {};
|
|
731
|
+
for (const key of Object.keys(rawSchema)) {
|
|
732
|
+
if (isUnsafeKey(key))
|
|
733
|
+
continue;
|
|
734
|
+
const val = rawSchema[key];
|
|
735
|
+
if (key === "properties" && isObject(val)) {
|
|
736
|
+
const props = {};
|
|
737
|
+
let count = 0;
|
|
738
|
+
for (const propKey of Object.keys(val)) {
|
|
739
|
+
if (isUnsafeKey(propKey))
|
|
740
|
+
continue;
|
|
741
|
+
if (count >= MAX_SCHEMA_KEYS) {
|
|
742
|
+
pushWarning(ctx, `Schema properties truncated at ${MAX_SCHEMA_KEYS} keys.`);
|
|
743
|
+
break;
|
|
744
|
+
}
|
|
745
|
+
props[propKey] = resolveSchemaInternal(val[propKey], ctx, visitedRefs, refDepth, schemaDepth + 1, propKey);
|
|
746
|
+
count += 1;
|
|
747
|
+
}
|
|
748
|
+
out.properties = props;
|
|
749
|
+
}
|
|
750
|
+
else if (key === "items") {
|
|
751
|
+
if (Array.isArray(val)) {
|
|
752
|
+
out.items = val.map((item) => resolveSchemaInternal(item, ctx, visitedRefs, refDepth, schemaDepth + 1));
|
|
753
|
+
}
|
|
754
|
+
else if (isObject(val) || typeof val === "boolean") {
|
|
755
|
+
out.items = resolveSchemaInternal(val, ctx, visitedRefs, refDepth, schemaDepth + 1);
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
else if (key === "additionalProperties") {
|
|
759
|
+
if (typeof val === "boolean") {
|
|
760
|
+
out.additionalProperties = val;
|
|
761
|
+
}
|
|
762
|
+
else if (isObject(val)) {
|
|
763
|
+
out.additionalProperties = resolveSchemaInternal(val, ctx, visitedRefs, refDepth, schemaDepth + 1);
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
else if (key === "not" && (isObject(val) || typeof val === "boolean")) {
|
|
767
|
+
out.not = resolveSchemaInternal(val, ctx, visitedRefs, refDepth, schemaDepth + 1);
|
|
768
|
+
}
|
|
769
|
+
else if ((key === "anyOf" || key === "oneOf") && Array.isArray(val)) {
|
|
770
|
+
out[key] = val.map((branch) => resolveSchemaInternal(branch, ctx, visitedRefs, refDepth, schemaDepth + 1, fieldName));
|
|
771
|
+
}
|
|
772
|
+
else if (key === "required" && Array.isArray(val)) {
|
|
773
|
+
out.required = val.map(String).filter((r) => !isUnsafeKey(r));
|
|
774
|
+
}
|
|
775
|
+
else if (key === "type") {
|
|
776
|
+
if (typeof val === "string") {
|
|
777
|
+
out.type = val;
|
|
778
|
+
}
|
|
779
|
+
else if (Array.isArray(val)) {
|
|
780
|
+
out.type = val.filter((item) => typeof item === "string");
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
else if (STRUCTURAL_SCHEMA_STRING_KEYS.has(key) && typeof val === "string") {
|
|
784
|
+
out[key] = val;
|
|
785
|
+
}
|
|
786
|
+
else if (key === "example" || key === "default" || key === "const") {
|
|
787
|
+
if (sensitiveField) {
|
|
788
|
+
ctx.redactedSecrets = true;
|
|
789
|
+
if (typeof val === "string")
|
|
790
|
+
recordSecretValue(ctx.secretValues, val);
|
|
791
|
+
else if (isObject(val) || Array.isArray(val)) {
|
|
792
|
+
scrubValueDeep(val, ctx.secretValues, 0, true);
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
else {
|
|
796
|
+
const scrubbed = scrubValueDeep(val, ctx.secretValues);
|
|
797
|
+
if (scrubbed.redacted)
|
|
798
|
+
ctx.redactedSecrets = true;
|
|
799
|
+
out[key] = scrubbed.value;
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
else if (key === "examples" && Array.isArray(val)) {
|
|
803
|
+
if (sensitiveField) {
|
|
804
|
+
ctx.redactedSecrets = true;
|
|
805
|
+
scrubValueDeep(val, ctx.secretValues, 0, true);
|
|
806
|
+
}
|
|
807
|
+
else {
|
|
808
|
+
const scrubbed = scrubValueDeep(val, ctx.secretValues);
|
|
809
|
+
if (scrubbed.redacted)
|
|
810
|
+
ctx.redactedSecrets = true;
|
|
811
|
+
out.examples = scrubbed.value;
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
else if (key === "enum" && Array.isArray(val)) {
|
|
815
|
+
if (sensitiveField) {
|
|
816
|
+
ctx.redactedSecrets = true;
|
|
817
|
+
scrubValueDeep(val, ctx.secretValues, 0, true);
|
|
818
|
+
}
|
|
819
|
+
else {
|
|
820
|
+
const scrubbed = scrubValueDeep(val, ctx.secretValues);
|
|
821
|
+
if (scrubbed.redacted)
|
|
822
|
+
ctx.redactedSecrets = true;
|
|
823
|
+
out.enum = scrubbed.value;
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
else if (typeof val === "string") {
|
|
827
|
+
const scrubbed = scrubText(val, ctx.secretValues);
|
|
828
|
+
if (scrubbed.redacted)
|
|
829
|
+
ctx.redactedSecrets = true;
|
|
830
|
+
out[key] = scrubbed.value;
|
|
831
|
+
}
|
|
832
|
+
else if (typeof val === "number" || typeof val === "boolean" || val === null) {
|
|
833
|
+
out[key] = val;
|
|
834
|
+
}
|
|
835
|
+
else if (Array.isArray(val)) {
|
|
836
|
+
const scrubbed = scrubValueDeep(val, ctx.secretValues);
|
|
837
|
+
if (scrubbed.redacted)
|
|
838
|
+
ctx.redactedSecrets = true;
|
|
839
|
+
out[key] = scrubbed.value;
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
return out;
|
|
843
|
+
}
|
|
844
|
+
function resolveSchema(rawSchema, ctx, fieldName = "") {
|
|
845
|
+
if (rawSchema === true)
|
|
846
|
+
return {};
|
|
847
|
+
if (rawSchema === false)
|
|
848
|
+
return { not: {} };
|
|
849
|
+
if (!isObject(rawSchema))
|
|
850
|
+
return undefined;
|
|
851
|
+
const resolved = resolveSchemaInternal(rawSchema, ctx, new Set(), 0, 0, fieldName);
|
|
852
|
+
return Object.keys(resolved).length > 0 ? resolved : undefined;
|
|
853
|
+
}
|
|
854
|
+
function sampleStringForFormat(format, minLength) {
|
|
855
|
+
switch (format) {
|
|
856
|
+
case "uuid":
|
|
857
|
+
return "00000000-0000-4000-8000-000000000000";
|
|
858
|
+
case "date-time":
|
|
859
|
+
return "2026-01-01T00:00:00Z";
|
|
860
|
+
case "date":
|
|
861
|
+
return "2026-01-01";
|
|
862
|
+
case "time":
|
|
863
|
+
return "00:00:00Z";
|
|
864
|
+
case "email":
|
|
865
|
+
return "rpc@example.com";
|
|
866
|
+
case "uri":
|
|
867
|
+
case "url":
|
|
868
|
+
return "https://example.com/resource";
|
|
869
|
+
case "hostname":
|
|
870
|
+
return "rpc.example.com";
|
|
871
|
+
case "ipv4":
|
|
872
|
+
return "127.0.0.1";
|
|
873
|
+
default: {
|
|
874
|
+
const base = "sample";
|
|
875
|
+
if (typeof minLength === "number" && Number.isFinite(minLength) && minLength > base.length) {
|
|
876
|
+
return base.padEnd(Math.min(Math.floor(minLength), 64), "x");
|
|
877
|
+
}
|
|
878
|
+
return base;
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
function sensitivePlaceholderForSchema(schema) {
|
|
883
|
+
if (isObject(schema)) {
|
|
884
|
+
const rawType = schema.type;
|
|
885
|
+
const primaryType = Array.isArray(rawType)
|
|
886
|
+
? (rawType.find((t) => t !== "null") ?? rawType[0])
|
|
887
|
+
: rawType;
|
|
888
|
+
if (primaryType === "integer" || primaryType === "number")
|
|
889
|
+
return 0;
|
|
890
|
+
if (primaryType === "boolean")
|
|
891
|
+
return false;
|
|
892
|
+
}
|
|
893
|
+
return "[REDACTED]";
|
|
894
|
+
}
|
|
895
|
+
function synthesizeSampleFromSchema(schema, ctx, depth = 0, fieldName = "") {
|
|
896
|
+
if (!isObject(schema) || depth > MAX_SAMPLE_DEPTH)
|
|
897
|
+
return undefined;
|
|
898
|
+
const sensitive = isSensitiveFieldName(fieldName);
|
|
899
|
+
if (sensitive) {
|
|
900
|
+
ctx.redactedSecrets = true;
|
|
901
|
+
return sensitivePlaceholderForSchema(schema);
|
|
902
|
+
}
|
|
903
|
+
if (hasOwn(schema, "const") && schema.const !== undefined) {
|
|
904
|
+
const s = scrubValueDeep(schema.const, ctx.secretValues);
|
|
905
|
+
if (s.redacted)
|
|
906
|
+
ctx.redactedSecrets = true;
|
|
907
|
+
return s.value;
|
|
908
|
+
}
|
|
909
|
+
if (hasOwn(schema, "example") && schema.example !== undefined) {
|
|
910
|
+
const s = scrubValueDeep(schema.example, ctx.secretValues);
|
|
911
|
+
if (s.redacted)
|
|
912
|
+
ctx.redactedSecrets = true;
|
|
913
|
+
return s.value;
|
|
914
|
+
}
|
|
915
|
+
if (Array.isArray(schema.examples) && schema.examples.length > 0) {
|
|
916
|
+
const s = scrubValueDeep(schema.examples[0], ctx.secretValues);
|
|
917
|
+
if (s.redacted)
|
|
918
|
+
ctx.redactedSecrets = true;
|
|
919
|
+
return s.value;
|
|
920
|
+
}
|
|
921
|
+
if (hasOwn(schema, "default") && schema.default !== undefined) {
|
|
922
|
+
const s = scrubValueDeep(schema.default, ctx.secretValues);
|
|
923
|
+
if (s.redacted)
|
|
924
|
+
ctx.redactedSecrets = true;
|
|
925
|
+
return s.value;
|
|
926
|
+
}
|
|
927
|
+
if (Array.isArray(schema.enum) && schema.enum.length > 0) {
|
|
928
|
+
const s = scrubValueDeep(schema.enum[0], ctx.secretValues);
|
|
929
|
+
if (s.redacted)
|
|
930
|
+
ctx.redactedSecrets = true;
|
|
931
|
+
return s.value;
|
|
932
|
+
}
|
|
933
|
+
if (Array.isArray(schema.oneOf) && schema.oneOf.length > 0) {
|
|
934
|
+
const candidate = schema.oneOf.find((b) => isObject(b) && b.type !== "null") ?? schema.oneOf[0];
|
|
935
|
+
return synthesizeSampleFromSchema(candidate, ctx, depth + 1, fieldName);
|
|
936
|
+
}
|
|
937
|
+
if (Array.isArray(schema.anyOf) && schema.anyOf.length > 0) {
|
|
938
|
+
const candidate = schema.anyOf.find((b) => isObject(b) && b.type !== "null") ?? schema.anyOf[0];
|
|
939
|
+
return synthesizeSampleFromSchema(candidate, ctx, depth + 1, fieldName);
|
|
940
|
+
}
|
|
941
|
+
const rawType = schema.type;
|
|
942
|
+
const primaryType = Array.isArray(rawType)
|
|
943
|
+
? (rawType.find((t) => t !== "null") ?? rawType[0])
|
|
944
|
+
: rawType;
|
|
945
|
+
if (primaryType === "object" || isObject(schema.properties)) {
|
|
946
|
+
const out = {};
|
|
947
|
+
if (isObject(schema.properties)) {
|
|
948
|
+
let count = 0;
|
|
949
|
+
for (const propKey of Object.keys(schema.properties)) {
|
|
950
|
+
if (isUnsafeKey(propKey))
|
|
951
|
+
continue;
|
|
952
|
+
if (count >= MAX_SCHEMA_KEYS)
|
|
953
|
+
break;
|
|
954
|
+
const propVal = synthesizeSampleFromSchema(schema.properties[propKey], ctx, depth + 1, propKey);
|
|
955
|
+
if (propVal !== undefined) {
|
|
956
|
+
out[propKey] = propVal;
|
|
957
|
+
}
|
|
958
|
+
else if (Array.isArray(schema.required) && schema.required.includes(propKey)) {
|
|
959
|
+
out[propKey] = isSensitiveFieldName(propKey) ? "[REDACTED]" : "sample";
|
|
960
|
+
}
|
|
961
|
+
count += 1;
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
return out;
|
|
965
|
+
}
|
|
966
|
+
if (primaryType === "array") {
|
|
967
|
+
if (Array.isArray(schema.items)) {
|
|
968
|
+
return schema.items
|
|
969
|
+
.map((item) => synthesizeSampleFromSchema(item, ctx, depth + 1))
|
|
970
|
+
.filter((v) => v !== undefined);
|
|
971
|
+
}
|
|
972
|
+
if (isObject(schema.items) && Object.keys(schema.items).length > 0) {
|
|
973
|
+
const itemVal = synthesizeSampleFromSchema(schema.items, ctx, depth + 1);
|
|
974
|
+
return itemVal === undefined ? [] : [itemVal];
|
|
975
|
+
}
|
|
976
|
+
return [];
|
|
977
|
+
}
|
|
978
|
+
if (primaryType === "integer") {
|
|
979
|
+
const min = typeof schema.minimum === "number" && Number.isFinite(schema.minimum) ? Math.ceil(schema.minimum) : 1;
|
|
980
|
+
return min;
|
|
981
|
+
}
|
|
982
|
+
if (primaryType === "number") {
|
|
983
|
+
const min = typeof schema.minimum === "number" && Number.isFinite(schema.minimum) ? schema.minimum : 1;
|
|
984
|
+
return min;
|
|
985
|
+
}
|
|
986
|
+
if (primaryType === "boolean")
|
|
987
|
+
return true;
|
|
988
|
+
if (primaryType === "null")
|
|
989
|
+
return null;
|
|
990
|
+
if (primaryType === "string") {
|
|
991
|
+
const fmt = typeof schema.format === "string" ? schema.format : undefined;
|
|
992
|
+
const minLen = typeof schema.minLength === "number" ? schema.minLength : undefined;
|
|
993
|
+
return sampleStringForFormat(fmt, minLen);
|
|
994
|
+
}
|
|
995
|
+
return "sample";
|
|
996
|
+
}
|
|
997
|
+
function substituteTemplateVariables(template, variables, ctx) {
|
|
998
|
+
return template.replace(/\{([^{}]+)\}/g, (_full, rawName) => {
|
|
999
|
+
const name = rawName.trim();
|
|
1000
|
+
if (!name || isUnsafeKey(name))
|
|
1001
|
+
return "sample";
|
|
1002
|
+
const rawVar = variables ? safeGet(variables, name) : undefined;
|
|
1003
|
+
const resolvedVar = resolveObjectRef(rawVar, ctx) ?? (isObject(rawVar) ? rawVar : undefined);
|
|
1004
|
+
if (isSensitiveFieldName(name)) {
|
|
1005
|
+
ctx.redactedSecrets = true;
|
|
1006
|
+
if (resolvedVar) {
|
|
1007
|
+
for (const k of ["default", "example"]) {
|
|
1008
|
+
const sv = safeGet(resolvedVar, k);
|
|
1009
|
+
if (typeof sv === "string")
|
|
1010
|
+
recordSecretValue(ctx.secretValues, sv);
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
return "redacted";
|
|
1014
|
+
}
|
|
1015
|
+
let chosen = undefined;
|
|
1016
|
+
if (resolvedVar) {
|
|
1017
|
+
if (hasOwn(resolvedVar, "default") && resolvedVar.default !== undefined) {
|
|
1018
|
+
chosen = resolvedVar.default;
|
|
1019
|
+
}
|
|
1020
|
+
else if (Array.isArray(resolvedVar.enum) && resolvedVar.enum.length > 0) {
|
|
1021
|
+
chosen = resolvedVar.enum[0];
|
|
1022
|
+
}
|
|
1023
|
+
else if (hasOwn(resolvedVar, "example") && resolvedVar.example !== undefined) {
|
|
1024
|
+
chosen = resolvedVar.example;
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
const strVal = chosen !== undefined && chosen !== null && typeof chosen !== "object" ? String(chosen) : "sample";
|
|
1028
|
+
const cleanVal = strVal.replace(CONTROL_CHAR_GLOBAL_RE, "");
|
|
1029
|
+
const scrubbed = scrubText(cleanVal, ctx.secretValues);
|
|
1030
|
+
if (scrubbed.redacted)
|
|
1031
|
+
ctx.redactedSecrets = true;
|
|
1032
|
+
return scrubbed.value || "sample";
|
|
1033
|
+
});
|
|
1034
|
+
}
|
|
1035
|
+
function extractServerInfo(doc, ctx) {
|
|
1036
|
+
const rawServers = safeGet(doc, "servers");
|
|
1037
|
+
if (!Array.isArray(rawServers) || rawServers.length === 0) {
|
|
1038
|
+
return { endpointPath: "/" };
|
|
1039
|
+
}
|
|
1040
|
+
for (const entry of rawServers) {
|
|
1041
|
+
const server = resolveObjectRef(entry, ctx);
|
|
1042
|
+
if (!server)
|
|
1043
|
+
continue;
|
|
1044
|
+
const rawUrl = safeGet(server, "url");
|
|
1045
|
+
if (typeof rawUrl !== "string" || !rawUrl.trim())
|
|
1046
|
+
continue;
|
|
1047
|
+
const vars = isObject(server.variables) ? server.variables : undefined;
|
|
1048
|
+
const substituted = substituteTemplateVariables(rawUrl.trim(), vars, ctx);
|
|
1049
|
+
// Relative path server URL (e.g. "/rpc" or "/v1/jsonrpc")
|
|
1050
|
+
if (substituted.startsWith("/") && !substituted.startsWith("//")) {
|
|
1051
|
+
let pathOnly = substituted.replace(CONTROL_CHAR_GLOBAL_RE, "");
|
|
1052
|
+
const hashIdx = pathOnly.indexOf("#");
|
|
1053
|
+
if (hashIdx >= 0) {
|
|
1054
|
+
const frag = pathOnly.slice(hashIdx + 1);
|
|
1055
|
+
if (frag) {
|
|
1056
|
+
ctx.redactedSecrets = true;
|
|
1057
|
+
recordSecretValue(ctx.secretValues, frag);
|
|
1058
|
+
}
|
|
1059
|
+
pathOnly = pathOnly.slice(0, hashIdx);
|
|
1060
|
+
}
|
|
1061
|
+
const qIdx = pathOnly.indexOf("?");
|
|
1062
|
+
if (qIdx >= 0) {
|
|
1063
|
+
const params = new URLSearchParams(pathOnly.slice(qIdx + 1));
|
|
1064
|
+
for (const [k, v] of params.entries()) {
|
|
1065
|
+
if (isOpenRpcSensitiveQueryParam(k) || SENSITIVE_HEADER_VALUE_RE.test(v)) {
|
|
1066
|
+
ctx.redactedSecrets = true;
|
|
1067
|
+
recordSecretValue(ctx.secretValues, v);
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
pathOnly = pathOnly.slice(0, qIdx);
|
|
1071
|
+
}
|
|
1072
|
+
const scrubbedPath = scrubText(pathOnly, ctx.secretValues);
|
|
1073
|
+
if (scrubbedPath.redacted)
|
|
1074
|
+
ctx.redactedSecrets = true;
|
|
1075
|
+
return { endpointPath: normalizeEndpointPath(scrubbedPath.value) };
|
|
1076
|
+
}
|
|
1077
|
+
const sanitized = sanitizeUrlString(substituted, ctx.secretValues, { requireHttp: true });
|
|
1078
|
+
if (sanitized.redacted)
|
|
1079
|
+
ctx.redactedSecrets = true;
|
|
1080
|
+
if (sanitized.valid && sanitized.safeUrl) {
|
|
1081
|
+
return {
|
|
1082
|
+
baseUrl: sanitized.safeUrl,
|
|
1083
|
+
endpointPath: sanitized.pathname || "/"
|
|
1084
|
+
};
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
return { endpointPath: "/" };
|
|
1088
|
+
}
|
|
1089
|
+
function slugify(text, fallback = "method") {
|
|
1090
|
+
const s = text
|
|
1091
|
+
.toLowerCase()
|
|
1092
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
1093
|
+
.replace(/^-+|-+$/g, "")
|
|
1094
|
+
.slice(0, 64);
|
|
1095
|
+
return s || fallback;
|
|
1096
|
+
}
|
|
1097
|
+
function uniqueMethodId(seed, used) {
|
|
1098
|
+
const base = slugify(seed, "method");
|
|
1099
|
+
if (!used.has(base)) {
|
|
1100
|
+
used.add(base);
|
|
1101
|
+
return base;
|
|
1102
|
+
}
|
|
1103
|
+
let n = 2;
|
|
1104
|
+
while (used.has(`${base}-${n}`))
|
|
1105
|
+
n += 1;
|
|
1106
|
+
const out = `${base}-${n}`;
|
|
1107
|
+
used.add(out);
|
|
1108
|
+
return out;
|
|
1109
|
+
}
|
|
1110
|
+
function extractExampleParamValues(methodObj, ctx) {
|
|
1111
|
+
const byName = new Map();
|
|
1112
|
+
const byIndex = [];
|
|
1113
|
+
const rawExamples = safeGet(methodObj, "examples");
|
|
1114
|
+
if (!Array.isArray(rawExamples) || rawExamples.length === 0) {
|
|
1115
|
+
return { byName, byIndex };
|
|
1116
|
+
}
|
|
1117
|
+
for (const rawPairing of rawExamples) {
|
|
1118
|
+
const pairing = resolveObjectRef(rawPairing, ctx);
|
|
1119
|
+
if (!pairing)
|
|
1120
|
+
continue;
|
|
1121
|
+
const rawParams = safeGet(pairing, "params");
|
|
1122
|
+
if (!Array.isArray(rawParams))
|
|
1123
|
+
continue;
|
|
1124
|
+
for (let i = 0; i < rawParams.length; i += 1) {
|
|
1125
|
+
const exObj = resolveObjectRef(rawParams[i], ctx);
|
|
1126
|
+
if (!exObj || !hasOwn(exObj, "value"))
|
|
1127
|
+
continue;
|
|
1128
|
+
const exName = typeof exObj.name === "string" ? exObj.name.trim() : "";
|
|
1129
|
+
const rawVal = exObj.value;
|
|
1130
|
+
const isSens = exName ? isSensitiveFieldName(exName) : false;
|
|
1131
|
+
if (isSens) {
|
|
1132
|
+
ctx.redactedSecrets = true;
|
|
1133
|
+
if (typeof rawVal === "string")
|
|
1134
|
+
recordSecretValue(ctx.secretValues, rawVal);
|
|
1135
|
+
else
|
|
1136
|
+
scrubValueDeep(rawVal, ctx.secretValues, 0, true);
|
|
1137
|
+
byName.set(exName, "[REDACTED]");
|
|
1138
|
+
byIndex[i] = "[REDACTED]";
|
|
1139
|
+
}
|
|
1140
|
+
else {
|
|
1141
|
+
const scrubbed = scrubValueDeep(rawVal, ctx.secretValues);
|
|
1142
|
+
if (scrubbed.redacted)
|
|
1143
|
+
ctx.redactedSecrets = true;
|
|
1144
|
+
if (exName && !isUnsafeKey(exName)) {
|
|
1145
|
+
byName.set(exName, scrubbed.value);
|
|
1146
|
+
}
|
|
1147
|
+
byIndex[i] = scrubbed.value;
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
break;
|
|
1151
|
+
}
|
|
1152
|
+
return { byName, byIndex };
|
|
1153
|
+
}
|
|
1154
|
+
function buildSampleParams(params, paramStructure, exampleValues, ctx) {
|
|
1155
|
+
if (paramStructure === "by-position") {
|
|
1156
|
+
return params.map((param, idx) => {
|
|
1157
|
+
const sensitive = isSensitiveFieldName(param.name);
|
|
1158
|
+
if (sensitive) {
|
|
1159
|
+
ctx.redactedSecrets = true;
|
|
1160
|
+
if (exampleValues.byName.has(param.name)) {
|
|
1161
|
+
scrubValueDeep(exampleValues.byName.get(param.name), ctx.secretValues, 0, true);
|
|
1162
|
+
}
|
|
1163
|
+
if (idx < exampleValues.byIndex.length && exampleValues.byIndex[idx] !== undefined) {
|
|
1164
|
+
scrubValueDeep(exampleValues.byIndex[idx], ctx.secretValues, 0, true);
|
|
1165
|
+
}
|
|
1166
|
+
return sensitivePlaceholderForSchema(param.schema);
|
|
1167
|
+
}
|
|
1168
|
+
if (exampleValues.byName.has(param.name)) {
|
|
1169
|
+
return exampleValues.byName.get(param.name);
|
|
1170
|
+
}
|
|
1171
|
+
if (idx < exampleValues.byIndex.length && exampleValues.byIndex[idx] !== undefined) {
|
|
1172
|
+
return exampleValues.byIndex[idx];
|
|
1173
|
+
}
|
|
1174
|
+
if (param.schema) {
|
|
1175
|
+
const synth = synthesizeSampleFromSchema(param.schema, ctx, 0, param.name);
|
|
1176
|
+
if (synth !== undefined)
|
|
1177
|
+
return synth;
|
|
1178
|
+
}
|
|
1179
|
+
return "sample";
|
|
1180
|
+
});
|
|
1181
|
+
}
|
|
1182
|
+
const out = {};
|
|
1183
|
+
for (let idx = 0; idx < params.length; idx += 1) {
|
|
1184
|
+
const param = params[idx];
|
|
1185
|
+
if (isUnsafeKey(param.name))
|
|
1186
|
+
continue;
|
|
1187
|
+
const sensitive = isSensitiveFieldName(param.name);
|
|
1188
|
+
if (sensitive) {
|
|
1189
|
+
ctx.redactedSecrets = true;
|
|
1190
|
+
if (exampleValues.byName.has(param.name)) {
|
|
1191
|
+
scrubValueDeep(exampleValues.byName.get(param.name), ctx.secretValues, 0, true);
|
|
1192
|
+
}
|
|
1193
|
+
if (idx < exampleValues.byIndex.length && exampleValues.byIndex[idx] !== undefined) {
|
|
1194
|
+
scrubValueDeep(exampleValues.byIndex[idx], ctx.secretValues, 0, true);
|
|
1195
|
+
}
|
|
1196
|
+
out[param.name] = sensitivePlaceholderForSchema(param.schema);
|
|
1197
|
+
continue;
|
|
1198
|
+
}
|
|
1199
|
+
if (exampleValues.byName.has(param.name)) {
|
|
1200
|
+
out[param.name] = exampleValues.byName.get(param.name);
|
|
1201
|
+
continue;
|
|
1202
|
+
}
|
|
1203
|
+
if (idx < exampleValues.byIndex.length && exampleValues.byIndex[idx] !== undefined) {
|
|
1204
|
+
out[param.name] = exampleValues.byIndex[idx];
|
|
1205
|
+
continue;
|
|
1206
|
+
}
|
|
1207
|
+
if (param.schema) {
|
|
1208
|
+
const synth = synthesizeSampleFromSchema(param.schema, ctx, 0, param.name);
|
|
1209
|
+
out[param.name] = synth !== undefined ? synth : "sample";
|
|
1210
|
+
}
|
|
1211
|
+
else {
|
|
1212
|
+
out[param.name] = "sample";
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
return out;
|
|
1216
|
+
}
|
|
1217
|
+
function parseMethods(doc, ctx) {
|
|
1218
|
+
const rawMethods = safeGet(doc, "methods");
|
|
1219
|
+
if (!Array.isArray(rawMethods))
|
|
1220
|
+
return [];
|
|
1221
|
+
const methods = [];
|
|
1222
|
+
const usedIds = new Set();
|
|
1223
|
+
for (let i = 0; i < rawMethods.length; i += 1) {
|
|
1224
|
+
if (methods.length >= MAX_METHODS) {
|
|
1225
|
+
pushWarning(ctx, `OpenRPC specification exceeded maximum of ${MAX_METHODS} methods; remaining methods were skipped.`);
|
|
1226
|
+
break;
|
|
1227
|
+
}
|
|
1228
|
+
const methodObj = resolveObjectRef(rawMethods[i], ctx);
|
|
1229
|
+
if (!methodObj)
|
|
1230
|
+
continue;
|
|
1231
|
+
const rawName = safeGet(methodObj, "name");
|
|
1232
|
+
if (typeof rawName !== "string" || !rawName.trim()) {
|
|
1233
|
+
pushWarning(ctx, `Skipped OpenRPC method at index ${i} with missing or empty name.`);
|
|
1234
|
+
continue;
|
|
1235
|
+
}
|
|
1236
|
+
const scrubbedName = scrubText(rawName.replace(CONTROL_CHAR_GLOBAL_RE, "").trim(), ctx.secretValues);
|
|
1237
|
+
if (scrubbedName.redacted)
|
|
1238
|
+
ctx.redactedSecrets = true;
|
|
1239
|
+
const methodName = scrubbedName.value;
|
|
1240
|
+
if (!methodName)
|
|
1241
|
+
continue;
|
|
1242
|
+
const rawParamStructure = safeGet(methodObj, "paramStructure");
|
|
1243
|
+
let paramStructure = "either";
|
|
1244
|
+
if (typeof rawParamStructure === "string" && rawParamStructure.trim()) {
|
|
1245
|
+
const trimmedStruct = rawParamStructure.trim();
|
|
1246
|
+
if (trimmedStruct === "either" || trimmedStruct === "by-name" || trimmedStruct === "by-position") {
|
|
1247
|
+
paramStructure = trimmedStruct;
|
|
1248
|
+
}
|
|
1249
|
+
else {
|
|
1250
|
+
pushWarning(ctx, `Method "${methodName}" has unknown paramStructure "${scrubText(trimmedStruct, ctx.secretValues).value}"; defaulting to "either".`);
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
const rawSummary = (typeof methodObj.summary === "string" && methodObj.summary.trim() ? methodObj.summary : undefined) ??
|
|
1254
|
+
(typeof methodObj.description === "string" && methodObj.description.trim() ? methodObj.description : undefined);
|
|
1255
|
+
let summary;
|
|
1256
|
+
if (rawSummary) {
|
|
1257
|
+
const cleanSummary = scrubText(rawSummary.replace(CONTROL_CHAR_GLOBAL_RE, " ").trim(), ctx.secretValues);
|
|
1258
|
+
if (cleanSummary.redacted)
|
|
1259
|
+
ctx.redactedSecrets = true;
|
|
1260
|
+
summary = cleanSummary.value;
|
|
1261
|
+
}
|
|
1262
|
+
const parsedParams = [];
|
|
1263
|
+
const rawParams = safeGet(methodObj, "params");
|
|
1264
|
+
if (Array.isArray(rawParams)) {
|
|
1265
|
+
for (let pIdx = 0; pIdx < rawParams.length; pIdx += 1) {
|
|
1266
|
+
const paramObj = resolveObjectRef(rawParams[pIdx], ctx);
|
|
1267
|
+
if (!paramObj)
|
|
1268
|
+
continue;
|
|
1269
|
+
const rawParamName = safeGet(paramObj, "name");
|
|
1270
|
+
const cleanParamName = typeof rawParamName === "string" ? rawParamName.replace(CONTROL_CHAR_GLOBAL_RE, "").trim() : "";
|
|
1271
|
+
if (!cleanParamName || isUnsafeKey(cleanParamName)) {
|
|
1272
|
+
continue;
|
|
1273
|
+
}
|
|
1274
|
+
const required = safeGet(paramObj, "required") === true;
|
|
1275
|
+
const rawParamSchema = safeGet(paramObj, "schema");
|
|
1276
|
+
const paramSchema = rawParamSchema !== undefined ? resolveSchema(rawParamSchema, ctx, cleanParamName) : undefined;
|
|
1277
|
+
const rawParamDesc = (typeof paramObj.description === "string" && paramObj.description.trim() ? paramObj.description : undefined) ??
|
|
1278
|
+
(typeof paramObj.summary === "string" && paramObj.summary.trim() ? paramObj.summary : undefined);
|
|
1279
|
+
let paramDesc;
|
|
1280
|
+
if (rawParamDesc) {
|
|
1281
|
+
const scrubbedDesc = scrubText(rawParamDesc.replace(CONTROL_CHAR_GLOBAL_RE, " ").trim(), ctx.secretValues);
|
|
1282
|
+
if (scrubbedDesc.redacted)
|
|
1283
|
+
ctx.redactedSecrets = true;
|
|
1284
|
+
paramDesc = scrubbedDesc.value;
|
|
1285
|
+
}
|
|
1286
|
+
parsedParams.push({
|
|
1287
|
+
name: cleanParamName,
|
|
1288
|
+
required,
|
|
1289
|
+
...(paramSchema ? { schema: paramSchema } : {}),
|
|
1290
|
+
...(paramDesc ? { description: paramDesc } : {})
|
|
1291
|
+
});
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
const exampleValues = extractExampleParamValues(methodObj, ctx);
|
|
1295
|
+
const sampleParams = buildSampleParams(parsedParams, paramStructure, exampleValues, ctx);
|
|
1296
|
+
let resultSchema;
|
|
1297
|
+
const rawResult = safeGet(methodObj, "result");
|
|
1298
|
+
if (rawResult !== undefined) {
|
|
1299
|
+
const resultObj = resolveObjectRef(rawResult, ctx);
|
|
1300
|
+
if (resultObj) {
|
|
1301
|
+
const rawResSchema = safeGet(resultObj, "schema");
|
|
1302
|
+
const resultName = typeof resultObj.name === "string" ? resultObj.name : "";
|
|
1303
|
+
if (rawResSchema !== undefined) {
|
|
1304
|
+
resultSchema = resolveSchema(rawResSchema, ctx, resultName);
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
const errorCodeSet = new Set();
|
|
1309
|
+
const rawErrors = safeGet(methodObj, "errors");
|
|
1310
|
+
if (Array.isArray(rawErrors)) {
|
|
1311
|
+
for (const rawErr of rawErrors) {
|
|
1312
|
+
const errObj = resolveObjectRef(rawErr, ctx);
|
|
1313
|
+
if (!errObj)
|
|
1314
|
+
continue;
|
|
1315
|
+
const code = safeGet(errObj, "code");
|
|
1316
|
+
if (typeof code === "number" && Number.isInteger(code)) {
|
|
1317
|
+
errorCodeSet.add(code);
|
|
1318
|
+
}
|
|
1319
|
+
// Record any secret that might appear inside error definitions so it never leaks elsewhere
|
|
1320
|
+
const errMsg = safeGet(errObj, "message");
|
|
1321
|
+
if (typeof errMsg === "string") {
|
|
1322
|
+
const s = scrubText(errMsg, ctx.secretValues);
|
|
1323
|
+
if (s.redacted)
|
|
1324
|
+
ctx.redactedSecrets = true;
|
|
1325
|
+
}
|
|
1326
|
+
if (hasOwn(errObj, "data")) {
|
|
1327
|
+
const s = scrubValueDeep(errObj.data, ctx.secretValues);
|
|
1328
|
+
if (s.redacted)
|
|
1329
|
+
ctx.redactedSecrets = true;
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
methods.push({
|
|
1334
|
+
id: uniqueMethodId(methodName, usedIds),
|
|
1335
|
+
name: methodName,
|
|
1336
|
+
...(summary ? { summary } : {}),
|
|
1337
|
+
paramStructure,
|
|
1338
|
+
params: parsedParams,
|
|
1339
|
+
sampleParams,
|
|
1340
|
+
...(resultSchema ? { resultSchema } : {}),
|
|
1341
|
+
errorCodes: [...errorCodeSet]
|
|
1342
|
+
});
|
|
1343
|
+
}
|
|
1344
|
+
return methods;
|
|
1345
|
+
}
|
|
1346
|
+
function finalScrubMethod(m, secretValues) {
|
|
1347
|
+
const scrubbedParams = m.params.map((p) => ({
|
|
1348
|
+
name: p.name,
|
|
1349
|
+
required: p.required,
|
|
1350
|
+
...(p.schema ? { schema: scrubSchemaDeep(p.schema, secretValues, 0, isSensitiveFieldName(p.name)) } : {}),
|
|
1351
|
+
...(p.description ? { description: scrubText(p.description, secretValues).value } : {})
|
|
1352
|
+
}));
|
|
1353
|
+
return {
|
|
1354
|
+
id: m.id,
|
|
1355
|
+
name: scrubText(m.name, secretValues).value,
|
|
1356
|
+
...(m.summary ? { summary: scrubText(m.summary, secretValues).value } : {}),
|
|
1357
|
+
paramStructure: m.paramStructure,
|
|
1358
|
+
params: scrubbedParams,
|
|
1359
|
+
sampleParams: scrubValueDeep(m.sampleParams, secretValues).value,
|
|
1360
|
+
...(m.resultSchema ? { resultSchema: scrubSchemaDeep(m.resultSchema, secretValues) } : {}),
|
|
1361
|
+
errorCodes: [...m.errorCodes]
|
|
1362
|
+
};
|
|
1363
|
+
}
|
|
1364
|
+
function buildJsonRpcEnvelopeSchema(resultSchema) {
|
|
1365
|
+
return {
|
|
1366
|
+
type: "object",
|
|
1367
|
+
required: ["jsonrpc", "id", "result"],
|
|
1368
|
+
properties: {
|
|
1369
|
+
jsonrpc: { const: "2.0" },
|
|
1370
|
+
id: { type: ["integer", "string"] },
|
|
1371
|
+
result: resultSchema ?? {},
|
|
1372
|
+
error: { not: {} }
|
|
1373
|
+
}
|
|
1374
|
+
};
|
|
1375
|
+
}
|
|
1376
|
+
function parseRawDocument(rawText) {
|
|
1377
|
+
if (typeof rawText !== "string") {
|
|
1378
|
+
throw new VibecodeError("CONTRACT_INVALID", "OpenRPC input must be a string.", {
|
|
1379
|
+
fallbackHint: "Pass a JSON or YAML OpenRPC 1.x document."
|
|
1380
|
+
});
|
|
1381
|
+
}
|
|
1382
|
+
if (Buffer.byteLength(rawText, "utf8") > MAX_SOURCE_BYTES) {
|
|
1383
|
+
throw new VibecodeError("CONTRACT_INVALID", `OpenRPC payload exceeds maximum size of ${MAX_SOURCE_BYTES} bytes.`, { fallbackHint: "Reduce the OpenRPC document below 2 MiB or split into smaller specs." });
|
|
1384
|
+
}
|
|
1385
|
+
if (rawText.includes("\0")) {
|
|
1386
|
+
throw new VibecodeError("CONTRACT_INVALID", "OpenRPC input contains NUL bytes.", {
|
|
1387
|
+
fallbackHint: "Provide a valid UTF-8 JSON or YAML OpenRPC document without binary data."
|
|
1388
|
+
});
|
|
1389
|
+
}
|
|
1390
|
+
const cleaned = stripBom(rawText).trim();
|
|
1391
|
+
if (!cleaned) {
|
|
1392
|
+
throw new VibecodeError("CONTRACT_INVALID", "OpenRPC input is empty.", {
|
|
1393
|
+
fallbackHint: "Provide a non-empty OpenRPC 1.x JSON or YAML specification."
|
|
1394
|
+
});
|
|
1395
|
+
}
|
|
1396
|
+
let parsed;
|
|
1397
|
+
try {
|
|
1398
|
+
if (cleaned.startsWith("{")) {
|
|
1399
|
+
parsed = JSON.parse(cleaned);
|
|
1400
|
+
}
|
|
1401
|
+
else {
|
|
1402
|
+
parsed = parseYaml(cleaned);
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
catch (err) {
|
|
1406
|
+
const scrubbedErr = scrubText(errorMessage(err), new Set()).value
|
|
1407
|
+
.replace(CONTROL_CHAR_GLOBAL_RE, " ")
|
|
1408
|
+
.replace(/"[^"\r\n]{1,200}"\s+is not valid JSON/gi, '"[REDACTED_INPUT]" is not valid JSON')
|
|
1409
|
+
.replace(/,\s*[^\r\n]{1,240}\s+is not valid JSON/gi, ", [REDACTED_INPUT] is not valid JSON")
|
|
1410
|
+
.slice(0, 160);
|
|
1411
|
+
throw new VibecodeError("CONTRACT_INVALID", `Failed to parse OpenRPC document: ${scrubbedErr}`, {
|
|
1412
|
+
fallbackHint: "Ensure the input is valid JSON or YAML conforming to OpenRPC 1.x."
|
|
1413
|
+
});
|
|
1414
|
+
}
|
|
1415
|
+
const sanitized = sanitizeParsedValue(parsed);
|
|
1416
|
+
if (!isObject(sanitized)) {
|
|
1417
|
+
throw new VibecodeError("UNSUPPORTED_FORMAT", "OpenRPC document must be a JSON/YAML object.", {
|
|
1418
|
+
fallbackHint: "Provide a root object containing 'openrpc' (1.x) and 'methods'."
|
|
1419
|
+
});
|
|
1420
|
+
}
|
|
1421
|
+
return sanitized;
|
|
1422
|
+
}
|
|
1423
|
+
export function isOpenRpcDoc(value) {
|
|
1424
|
+
if (!isObject(value))
|
|
1425
|
+
return false;
|
|
1426
|
+
// Exclude OpenAPI, Swagger, AsyncAPI, Insomnia, Postman, HAR, and GraphQL introspection JSON
|
|
1427
|
+
if (hasOwn(value, "openapi") || hasOwn(value, "swagger") || hasOwn(value, "asyncapi"))
|
|
1428
|
+
return false;
|
|
1429
|
+
if (safeGet(value, "_type") === "export")
|
|
1430
|
+
return false;
|
|
1431
|
+
const info = safeGet(value, "info");
|
|
1432
|
+
if (isObject(info) && Array.isArray(safeGet(value, "item")))
|
|
1433
|
+
return false;
|
|
1434
|
+
const log = safeGet(value, "log");
|
|
1435
|
+
if (isObject(log) && Array.isArray(safeGet(log, "entries")))
|
|
1436
|
+
return false;
|
|
1437
|
+
if (hasOwn(value, "__schema"))
|
|
1438
|
+
return false;
|
|
1439
|
+
const data = safeGet(value, "data");
|
|
1440
|
+
if (isObject(data) && hasOwn(data, "__schema"))
|
|
1441
|
+
return false;
|
|
1442
|
+
const rawVersion = safeGet(value, "openrpc");
|
|
1443
|
+
if (typeof rawVersion !== "string")
|
|
1444
|
+
return false;
|
|
1445
|
+
if (!OPENRPC_VERSION_RE.test(rawVersion.trim()))
|
|
1446
|
+
return false;
|
|
1447
|
+
return Array.isArray(safeGet(value, "methods"));
|
|
1448
|
+
}
|
|
1449
|
+
export function isOpenRpcText(rawText) {
|
|
1450
|
+
if (typeof rawText !== "string")
|
|
1451
|
+
return false;
|
|
1452
|
+
if (rawText.includes("\0"))
|
|
1453
|
+
return false;
|
|
1454
|
+
if (Buffer.byteLength(rawText, "utf8") > MAX_SOURCE_BYTES)
|
|
1455
|
+
return false;
|
|
1456
|
+
const cleaned = stripBom(rawText).trim();
|
|
1457
|
+
if (!cleaned)
|
|
1458
|
+
return false;
|
|
1459
|
+
if (cleaned.startsWith("{")) {
|
|
1460
|
+
if (!cleaned.includes("\"openrpc\""))
|
|
1461
|
+
return false;
|
|
1462
|
+
try {
|
|
1463
|
+
const parsed = JSON.parse(cleaned);
|
|
1464
|
+
return isOpenRpcDoc(parsed);
|
|
1465
|
+
}
|
|
1466
|
+
catch {
|
|
1467
|
+
return false;
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
if (GRAPHQL_SDL_RE.test(cleaned))
|
|
1471
|
+
return false;
|
|
1472
|
+
if (!OPENRPC_YAML_HEADER_RE.test(cleaned))
|
|
1473
|
+
return false;
|
|
1474
|
+
if (SIBLING_YAML_HEADER_RE.test(cleaned))
|
|
1475
|
+
return false;
|
|
1476
|
+
try {
|
|
1477
|
+
const parsed = parseYaml(cleaned);
|
|
1478
|
+
return isOpenRpcDoc(parsed);
|
|
1479
|
+
}
|
|
1480
|
+
catch {
|
|
1481
|
+
return false;
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
function parseOpenRpcInternal(rawText) {
|
|
1485
|
+
const doc = parseRawDocument(rawText);
|
|
1486
|
+
if (!isOpenRpcDoc(doc)) {
|
|
1487
|
+
const rawVersion = safeGet(doc, "openrpc");
|
|
1488
|
+
if (typeof rawVersion === "string" && rawVersion.trim()) {
|
|
1489
|
+
const safeVer = scrubText(rawVersion.trim(), new Set()).value
|
|
1490
|
+
.replace(CONTROL_CHAR_GLOBAL_RE, " ")
|
|
1491
|
+
.slice(0, 64);
|
|
1492
|
+
throw new VibecodeError("UNSUPPORTED_FORMAT", `Unsupported OpenRPC version "${safeVer}"; supported versions are OpenRPC 1.x.`, { fallbackHint: "Use an OpenRPC 1.x (e.g. 1.2.6 or 1.3.2) specification with a 'methods' array." });
|
|
1493
|
+
}
|
|
1494
|
+
throw new VibecodeError("UNSUPPORTED_FORMAT", "Document is not a valid OpenRPC 1.x specification.", { fallbackHint: "Include an 'openrpc' version field (1.x) and a 'methods' array." });
|
|
1495
|
+
}
|
|
1496
|
+
const openrpc = String(safeGet(doc, "openrpc")).trim();
|
|
1497
|
+
const ctx = {
|
|
1498
|
+
doc,
|
|
1499
|
+
warnings: [],
|
|
1500
|
+
warningSet: new Set(),
|
|
1501
|
+
secretValues: new Set(),
|
|
1502
|
+
redactedSecrets: false,
|
|
1503
|
+
nodeBudget: { remaining: MAX_NODE_BUDGET }
|
|
1504
|
+
};
|
|
1505
|
+
const infoObj = resolveObjectRef(safeGet(doc, "info"), ctx);
|
|
1506
|
+
const rawTitle = infoObj && typeof infoObj.title === "string" && infoObj.title.trim() ? infoObj.title.trim() : "OpenRPC Service";
|
|
1507
|
+
const scrubbedTitle = scrubText(rawTitle.replace(CONTROL_CHAR_GLOBAL_RE, " "), ctx.secretValues);
|
|
1508
|
+
if (scrubbedTitle.redacted)
|
|
1509
|
+
ctx.redactedSecrets = true;
|
|
1510
|
+
const { baseUrl, endpointPath } = extractServerInfo(doc, ctx);
|
|
1511
|
+
const methods = parseMethods(doc, ctx);
|
|
1512
|
+
if (methods.length === 0) {
|
|
1513
|
+
throw new VibecodeError("CONTRACT_INVALID", "OpenRPC specification contains no usable methods.", {
|
|
1514
|
+
fallbackHint: "Define at least one JSON-RPC method entry inside the 'methods' array."
|
|
1515
|
+
});
|
|
1516
|
+
}
|
|
1517
|
+
// Final pass so secrets discovered later in the spec are scrubbed everywhere
|
|
1518
|
+
const scrubbedTitleFinal = scrubText(scrubbedTitle.value, ctx.secretValues).value;
|
|
1519
|
+
const finalMethods = methods.map((m) => finalScrubMethod(m, ctx.secretValues));
|
|
1520
|
+
const finalWarnings = ctx.warnings.map((w) => scrubText(w, ctx.secretValues).value);
|
|
1521
|
+
const finalBaseUrl = baseUrl ? scrubText(baseUrl, ctx.secretValues).value : undefined;
|
|
1522
|
+
const finalEndpointPath = normalizeEndpointPath(scrubText(endpointPath, ctx.secretValues).value);
|
|
1523
|
+
const spec = {
|
|
1524
|
+
openrpc,
|
|
1525
|
+
title: scrubbedTitleFinal,
|
|
1526
|
+
...(finalBaseUrl ? { baseUrl: finalBaseUrl } : {}),
|
|
1527
|
+
endpointPath: finalEndpointPath,
|
|
1528
|
+
methods: finalMethods,
|
|
1529
|
+
warnings: finalWarnings
|
|
1530
|
+
};
|
|
1531
|
+
return {
|
|
1532
|
+
spec,
|
|
1533
|
+
secretValues: ctx.secretValues,
|
|
1534
|
+
redactedSecrets: ctx.redactedSecrets
|
|
1535
|
+
};
|
|
1536
|
+
}
|
|
1537
|
+
export function parseOpenRpcSpec(rawText) {
|
|
1538
|
+
return parseOpenRpcInternal(rawText).spec;
|
|
1539
|
+
}
|
|
1540
|
+
function looksLikeFilePath(trimmed) {
|
|
1541
|
+
if (/[\r\n{}]/.test(trimmed))
|
|
1542
|
+
return false;
|
|
1543
|
+
const ext = path.extname(trimmed).toLowerCase();
|
|
1544
|
+
if (ext === ".json" || ext === ".yaml" || ext === ".yml" || ext === ".openrpc" || ext === ".rpc")
|
|
1545
|
+
return true;
|
|
1546
|
+
if (!trimmed.includes(":") && !trimmed.includes("\n"))
|
|
1547
|
+
return true;
|
|
1548
|
+
if (/^[A-Za-z]:[\\/]/.test(trimmed))
|
|
1549
|
+
return true;
|
|
1550
|
+
return false;
|
|
1551
|
+
}
|
|
1552
|
+
function uniqueInvariantId(seed, used) {
|
|
1553
|
+
let id = slugify(seed, "openrpc");
|
|
1554
|
+
if (!id.startsWith("inv-"))
|
|
1555
|
+
id = `inv-${id}`;
|
|
1556
|
+
if (!used.has(id)) {
|
|
1557
|
+
used.add(id);
|
|
1558
|
+
return id;
|
|
1559
|
+
}
|
|
1560
|
+
let n = 2;
|
|
1561
|
+
while (used.has(`${id}-${n}`))
|
|
1562
|
+
n += 1;
|
|
1563
|
+
const out = `${id}-${n}`;
|
|
1564
|
+
used.add(out);
|
|
1565
|
+
return out;
|
|
1566
|
+
}
|
|
1567
|
+
function validateInvariantsOrThrow(invariants) {
|
|
1568
|
+
const contract = {
|
|
1569
|
+
schemaVersion: "0.2.0",
|
|
1570
|
+
target: {
|
|
1571
|
+
name: "openrpc",
|
|
1572
|
+
domains: ["api"],
|
|
1573
|
+
sources: [{ id: "src-1", kind: "openrpc", ref: "inline", meta: {} }]
|
|
1574
|
+
},
|
|
1575
|
+
artifacts: { root: ".vibecode/artifacts", files: {} },
|
|
1576
|
+
thresholds: {
|
|
1577
|
+
hardBehavioralParity: 1,
|
|
1578
|
+
softPerceptualParity: 0.85,
|
|
1579
|
+
softGateMode: "warn",
|
|
1580
|
+
defaults: {}
|
|
1581
|
+
},
|
|
1582
|
+
invariants
|
|
1583
|
+
};
|
|
1584
|
+
const res = validateContract(contract);
|
|
1585
|
+
if (!res.ok) {
|
|
1586
|
+
const err = res.errors[0];
|
|
1587
|
+
throw new VibecodeError("BRIDGE_FORMAT_ERROR", `OpenRPC ingestor produced invalid invariant: ${err.path} ${err.message}`, { details: res.errors });
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
export async function ingestOpenRpc(options = {}, _deps = {}) {
|
|
1591
|
+
if (typeof options.source !== "string" || !options.source.trim()) {
|
|
1592
|
+
throw new VibecodeError("TARGET_MISSING", "OpenRPC ingestor requires --source <openrpc.json|openrpc.yaml|inline>.", { fallbackHint: "Pass a path to an OpenRPC 1.x JSON or YAML file, or inline spec text." });
|
|
1593
|
+
}
|
|
1594
|
+
const cwd = options.cwd || process.cwd();
|
|
1595
|
+
const rawSource = options.source;
|
|
1596
|
+
const trimmedSource = rawSource.trim();
|
|
1597
|
+
let rawText = "";
|
|
1598
|
+
let isFileSource = false;
|
|
1599
|
+
if (!/[\r\n]/.test(trimmedSource) && !trimmedSource.includes("\0")) {
|
|
1600
|
+
const candidatePath = path.resolve(cwd, trimmedSource);
|
|
1601
|
+
let exists = false;
|
|
1602
|
+
try {
|
|
1603
|
+
exists = fs.existsSync(candidatePath);
|
|
1604
|
+
}
|
|
1605
|
+
catch {
|
|
1606
|
+
exists = false;
|
|
1607
|
+
}
|
|
1608
|
+
if (exists) {
|
|
1609
|
+
let stat;
|
|
1610
|
+
try {
|
|
1611
|
+
stat = fs.statSync(candidatePath);
|
|
1612
|
+
}
|
|
1613
|
+
catch (err) {
|
|
1614
|
+
const safeErr = scrubText(errorMessage(err), new Set()).value.slice(0, 160);
|
|
1615
|
+
throw new VibecodeError("CONTRACT_INVALID", `Failed to inspect OpenRPC source: ${safeErr}`, {
|
|
1616
|
+
fallbackHint: "Verify file permissions and path."
|
|
1617
|
+
});
|
|
1618
|
+
}
|
|
1619
|
+
if (!stat.isFile()) {
|
|
1620
|
+
const safePath = scrubText(trimmedSource, new Set()).value;
|
|
1621
|
+
throw new VibecodeError("CONTRACT_INVALID", `OpenRPC source path is a directory or non-file: ${safePath}`, {
|
|
1622
|
+
fallbackHint: "Point --source to an OpenRPC JSON or YAML file, not a directory."
|
|
1623
|
+
});
|
|
1624
|
+
}
|
|
1625
|
+
if (stat.size === 0) {
|
|
1626
|
+
const safePath = scrubText(trimmedSource, new Set()).value;
|
|
1627
|
+
throw new VibecodeError("CONTRACT_INVALID", `OpenRPC source file is empty: ${safePath}`, {
|
|
1628
|
+
fallbackHint: "Provide a non-empty OpenRPC 1.x specification file."
|
|
1629
|
+
});
|
|
1630
|
+
}
|
|
1631
|
+
if (stat.size > MAX_SOURCE_BYTES) {
|
|
1632
|
+
throw new VibecodeError("CONTRACT_INVALID", `OpenRPC source file exceeds maximum size of ${MAX_SOURCE_BYTES} bytes.`, { fallbackHint: "Reduce the OpenRPC file size below 2 MiB." });
|
|
1633
|
+
}
|
|
1634
|
+
let buf;
|
|
1635
|
+
try {
|
|
1636
|
+
buf = fs.readFileSync(candidatePath);
|
|
1637
|
+
}
|
|
1638
|
+
catch (err) {
|
|
1639
|
+
const safeErr = scrubText(errorMessage(err), new Set()).value.slice(0, 160);
|
|
1640
|
+
throw new VibecodeError("CONTRACT_INVALID", `Failed to read OpenRPC source file: ${safeErr}`, {
|
|
1641
|
+
fallbackHint: "Ensure the OpenRPC file is readable."
|
|
1642
|
+
});
|
|
1643
|
+
}
|
|
1644
|
+
if (buf.length > MAX_SOURCE_BYTES) {
|
|
1645
|
+
throw new VibecodeError("CONTRACT_INVALID", `OpenRPC source file exceeds maximum size of ${MAX_SOURCE_BYTES} bytes.`);
|
|
1646
|
+
}
|
|
1647
|
+
if (buf.includes(0)) {
|
|
1648
|
+
throw new VibecodeError("CONTRACT_INVALID", "OpenRPC source file contains NUL bytes.", {
|
|
1649
|
+
fallbackHint: "Provide a UTF-8 text JSON or YAML OpenRPC file."
|
|
1650
|
+
});
|
|
1651
|
+
}
|
|
1652
|
+
rawText = buf.toString("utf8");
|
|
1653
|
+
isFileSource = true;
|
|
1654
|
+
}
|
|
1655
|
+
else if (looksLikeFilePath(trimmedSource)) {
|
|
1656
|
+
const safePath = scrubText(trimmedSource, new Set()).value;
|
|
1657
|
+
throw new VibecodeError("TARGET_MISSING", `OpenRPC source file not found: ${safePath}`, {
|
|
1658
|
+
fallbackHint: "Verify the file path exists or pass inline OpenRPC JSON/YAML."
|
|
1659
|
+
});
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
if (!isFileSource) {
|
|
1663
|
+
if (Buffer.byteLength(rawSource, "utf8") > MAX_SOURCE_BYTES) {
|
|
1664
|
+
throw new VibecodeError("CONTRACT_INVALID", `Inline OpenRPC payload exceeds maximum size of ${MAX_SOURCE_BYTES} bytes.`, { fallbackHint: "Reduce the OpenRPC document size below 2 MiB." });
|
|
1665
|
+
}
|
|
1666
|
+
rawText = rawSource;
|
|
1667
|
+
}
|
|
1668
|
+
const parsedResult = parseOpenRpcInternal(rawText);
|
|
1669
|
+
const { spec, secretValues } = parsedResult;
|
|
1670
|
+
let redactedSecrets = parsedResult.redactedSecrets;
|
|
1671
|
+
const warnings = spec.warnings.map((message) => ({
|
|
1672
|
+
code: "BRIDGE_FORMAT_WARNING",
|
|
1673
|
+
message
|
|
1674
|
+
}));
|
|
1675
|
+
// Resolve and sanitize baseUrl & endpointPath (options.baseUrl overrides spec.baseUrl)
|
|
1676
|
+
let safeBaseUrl = spec.baseUrl || "";
|
|
1677
|
+
let endpointPath = spec.endpointPath || "/";
|
|
1678
|
+
if (typeof options.baseUrl === "string" && options.baseUrl.trim()) {
|
|
1679
|
+
const sanitizedBase = sanitizeUrlString(options.baseUrl, secretValues, { requireHttp: true });
|
|
1680
|
+
if (sanitizedBase.redacted)
|
|
1681
|
+
redactedSecrets = true;
|
|
1682
|
+
if (sanitizedBase.valid && sanitizedBase.safeUrl) {
|
|
1683
|
+
safeBaseUrl = sanitizedBase.safeUrl;
|
|
1684
|
+
if (sanitizedBase.pathname && sanitizedBase.pathname !== "/") {
|
|
1685
|
+
endpointPath = sanitizedBase.pathname;
|
|
1686
|
+
}
|
|
1687
|
+
}
|
|
1688
|
+
else {
|
|
1689
|
+
warnings.push({
|
|
1690
|
+
code: "ADAPTER_DEGRADED",
|
|
1691
|
+
message: "Invalid OpenRPC baseUrl provided; falling back to spec server URL or path-only invariants."
|
|
1692
|
+
});
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
// Sanitize global option headers
|
|
1696
|
+
const globalSafeHeaders = {};
|
|
1697
|
+
if (isObject(options.headers)) {
|
|
1698
|
+
const headerCtx = {
|
|
1699
|
+
doc: {},
|
|
1700
|
+
warnings: [],
|
|
1701
|
+
warningSet: new Set(),
|
|
1702
|
+
secretValues,
|
|
1703
|
+
redactedSecrets: false,
|
|
1704
|
+
nodeBudget: { remaining: 0 }
|
|
1705
|
+
};
|
|
1706
|
+
for (const [rawKey, rawVal] of Object.entries(options.headers)) {
|
|
1707
|
+
if (rawVal === undefined || rawVal === null)
|
|
1708
|
+
continue;
|
|
1709
|
+
const clean = sanitizeHeaderEntry(rawKey, String(rawVal), headerCtx);
|
|
1710
|
+
if (clean) {
|
|
1711
|
+
setHeaderCaseInsensitive(globalSafeHeaders, clean.name, clean.value);
|
|
1712
|
+
}
|
|
1713
|
+
}
|
|
1714
|
+
if (headerCtx.redactedSecrets)
|
|
1715
|
+
redactedSecrets = true;
|
|
1716
|
+
for (const msg of headerCtx.warnings) {
|
|
1717
|
+
warnings.push({ code: "BRIDGE_FORMAT_WARNING", message: scrubText(msg, secretValues).value });
|
|
1718
|
+
}
|
|
1719
|
+
}
|
|
1720
|
+
const baseHeaders = redactHeaders(mergeHeaders({ "content-type": "application/json" }, globalSafeHeaders), "strip");
|
|
1721
|
+
deleteHeaderCaseInsensitive(baseHeaders, "host");
|
|
1722
|
+
const finalHeaders = {};
|
|
1723
|
+
for (const [hk, hv] of Object.entries(baseHeaders)) {
|
|
1724
|
+
if (isUnsafeKey(hk) || isOpenRpcSensitiveHeader(hk) || SENSITIVE_HEADER_VALUE_RE.test(hv)) {
|
|
1725
|
+
redactedSecrets = true;
|
|
1726
|
+
recordSecretValue(secretValues, hv);
|
|
1727
|
+
continue;
|
|
1728
|
+
}
|
|
1729
|
+
const scrubbedVal = scrubText(hv.replace(CONTROL_CHAR_GLOBAL_RE, "").trim(), secretValues);
|
|
1730
|
+
if (scrubbedVal.redacted)
|
|
1731
|
+
redactedSecrets = true;
|
|
1732
|
+
finalHeaders[hk] = scrubbedVal.value;
|
|
1733
|
+
}
|
|
1734
|
+
if (!hasHeaderCaseInsensitive(finalHeaders, "content-type")) {
|
|
1735
|
+
finalHeaders["content-type"] = "application/json";
|
|
1736
|
+
}
|
|
1737
|
+
const finalEndpointPath = normalizeEndpointPath(scrubText(endpointPath, secretValues).value);
|
|
1738
|
+
const usedIds = new Set();
|
|
1739
|
+
const invariants = [];
|
|
1740
|
+
for (let i = 0; i < spec.methods.length; i += 1) {
|
|
1741
|
+
const method = spec.methods[i];
|
|
1742
|
+
const scrubbedParams = scrubValueDeep(method.sampleParams, secretValues).value;
|
|
1743
|
+
const scrubbedResultSchema = method.resultSchema
|
|
1744
|
+
? scrubSchemaDeep(method.resultSchema, secretValues)
|
|
1745
|
+
: undefined;
|
|
1746
|
+
const envelopeSchema = buildJsonRpcEnvelopeSchema(scrubbedResultSchema);
|
|
1747
|
+
const input = {
|
|
1748
|
+
method: "POST",
|
|
1749
|
+
path: finalEndpointPath,
|
|
1750
|
+
headers: { ...finalHeaders },
|
|
1751
|
+
body: {
|
|
1752
|
+
jsonrpc: "2.0",
|
|
1753
|
+
id: i + 1,
|
|
1754
|
+
method: scrubText(method.name, secretValues).value,
|
|
1755
|
+
params: scrubbedParams
|
|
1756
|
+
}
|
|
1757
|
+
};
|
|
1758
|
+
const expected = {
|
|
1759
|
+
status: 200,
|
|
1760
|
+
jsonSchema: envelopeSchema
|
|
1761
|
+
};
|
|
1762
|
+
const summarySuffix = method.summary ? ` — ${method.summary}` : "";
|
|
1763
|
+
const description = scrubText(`OpenRPC ${method.name} (POST ${finalEndpointPath})${summarySuffix}`, secretValues).value;
|
|
1764
|
+
const tags = ["api", "openrpc", "jsonrpc", method.paramStructure];
|
|
1765
|
+
invariants.push({
|
|
1766
|
+
id: uniqueInvariantId(`inv-openrpc-${method.id}`, usedIds),
|
|
1767
|
+
domain: "api",
|
|
1768
|
+
type: "http",
|
|
1769
|
+
severity: "hard",
|
|
1770
|
+
description,
|
|
1771
|
+
tags,
|
|
1772
|
+
sourceId: "src-1",
|
|
1773
|
+
input,
|
|
1774
|
+
expected,
|
|
1775
|
+
tolerance: {}
|
|
1776
|
+
});
|
|
1777
|
+
}
|
|
1778
|
+
validateInvariantsOrThrow(invariants);
|
|
1779
|
+
const safeRef = isFileSource
|
|
1780
|
+
? scrubText(trimmedSource, secretValues).value
|
|
1781
|
+
: safeBaseUrl || "inline";
|
|
1782
|
+
const finalWarnings = warnings.map((w) => ({
|
|
1783
|
+
...w,
|
|
1784
|
+
message: scrubText(w.message, secretValues).value
|
|
1785
|
+
}));
|
|
1786
|
+
return {
|
|
1787
|
+
domain: "api",
|
|
1788
|
+
sources: [
|
|
1789
|
+
{
|
|
1790
|
+
id: "src-1",
|
|
1791
|
+
kind: "openrpc",
|
|
1792
|
+
ref: safeRef,
|
|
1793
|
+
meta: {
|
|
1794
|
+
openrpcVersion: spec.openrpc,
|
|
1795
|
+
title: scrubText(spec.title, secretValues).value,
|
|
1796
|
+
endpointPath: finalEndpointPath,
|
|
1797
|
+
...(safeBaseUrl ? { baseUrl: scrubText(safeBaseUrl, secretValues).value } : {}),
|
|
1798
|
+
methodCount: spec.methods.length,
|
|
1799
|
+
...(redactedSecrets ? { redactedSecrets: true } : {})
|
|
1800
|
+
}
|
|
1801
|
+
}
|
|
1802
|
+
],
|
|
1803
|
+
artifacts: { root: ".vibecode/artifacts", files: {} },
|
|
1804
|
+
invariants,
|
|
1805
|
+
warnings: finalWarnings
|
|
1806
|
+
};
|
|
1807
|
+
}
|