@oracle-agent/oracle 0.1.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/CONTRIBUTING.md +98 -0
- package/LICENSE +202 -0
- package/README.md +384 -0
- package/SECURITY.md +89 -0
- package/SETUP.md +235 -0
- package/artifacts/inscription/oracle-was-here-preview.png +0 -0
- package/artifacts/inscription/oracle-was-here.svg +1 -0
- package/artifacts/specialist-packs/oracle-full-crypto.json +273 -0
- package/bin/desk-server.mjs +438 -0
- package/bin/oracle-data-mcp.mjs +603 -0
- package/bin/oracle-init.mjs +423 -0
- package/bin/oracle-public-server.mjs +36 -0
- package/bin/oracle-route.mjs +254 -0
- package/bin/oracle-scan.mjs +192 -0
- package/docs/adding-a-chain.md +229 -0
- package/docs/architecture.md +135 -0
- package/docs/profiles.md +132 -0
- package/examples/add-a-chain.mjs +65 -0
- package/examples/research-a-token.mjs +70 -0
- package/package.json +90 -0
- package/profiles/_template/SOUL.md +47 -0
- package/profiles/_template/profile.json +22 -0
- package/profiles/bitcoin-agent/SOUL.md +31 -0
- package/profiles/bitcoin-agent/profile.json +32 -0
- package/profiles/hyperliquid-agent/SOUL.md +34 -0
- package/profiles/hyperliquid-agent/profile.json +37 -0
- package/profiles/oracle/SOUL.md +65 -0
- package/profiles/oracle/profile.json +35 -0
- package/profiles/polymarket-agent/SOUL.md +35 -0
- package/profiles/polymarket-agent/profile.json +34 -0
- package/profiles/profile.schema.json +90 -0
- package/profiles/protocol-builder/SOUL.md +50 -0
- package/profiles/protocol-builder/profile.json +37 -0
- package/profiles/robinhood-agent/SOUL.md +45 -0
- package/profiles/robinhood-agent/profile.json +39 -0
- package/profiles/solana-agent/SOUL.md +37 -0
- package/profiles/solana-agent/profile.json +37 -0
- package/profiles/stable-agent/SOUL.md +43 -0
- package/profiles/stable-agent/profile.json +37 -0
- package/public/oracle-console/app.js +272 -0
- package/public/oracle-console/bitcoin-wallets.js +206 -0
- package/public/oracle-console/index.html +91 -0
- package/public/oracle-console/styles.css +239 -0
- package/public/oracle-splash/index.html +931 -0
- package/scripts/build-inscription.py +230 -0
- package/scripts/check-test-count.mjs +105 -0
- package/scripts/e2e-hl-markets.mjs +21 -0
- package/scripts/e2e-hl-perps.mjs +48 -0
- package/scripts/e2e-hypercore-staking.mjs +128 -0
- package/scripts/e2e-solana-bitcoin.mjs +183 -0
- package/scripts/public-api-scan.mjs +23 -0
- package/scripts/secret-scan.mjs +181 -0
- package/scripts/verify-v3-venues.mjs +192 -0
- package/skills/oracle-best-execution/SKILL.md +127 -0
- package/skills/oracle-bitcoin/SKILL.md +53 -0
- package/skills/oracle-chain-graphs-telegram-cards/SKILL.md +59 -0
- package/skills/oracle-circuit-breaker/SKILL.md +51 -0
- package/skills/oracle-contract-research/SKILL.md +55 -0
- package/skills/oracle-desk/SKILL.md +58 -0
- package/skills/oracle-dex-launch/SKILL.md +38 -0
- package/skills/oracle-grants/SKILL.md +69 -0
- package/skills/oracle-hypercore-staking/SKILL.md +57 -0
- package/skills/oracle-hyperliquid/SKILL.md +56 -0
- package/skills/oracle-meme-token-sniper/SKILL.md +73 -0
- package/skills/oracle-nft-gacha-launch/SKILL.md +48 -0
- package/skills/oracle-nft-mint-gas-war/SKILL.md +63 -0
- package/skills/oracle-polymarket/SKILL.md +60 -0
- package/skills/oracle-protocol-builder/SKILL.md +38 -0
- package/skills/oracle-protocol-security/SKILL.md +60 -0
- package/skills/oracle-public-product/SKILL.md +44 -0
- package/skills/oracle-receipts/SKILL.md +52 -0
- package/skills/oracle-rfq-tokenized-assets/SKILL.md +69 -0
- package/skills/oracle-smart-wallet-scanner/SKILL.md +49 -0
- package/skills/oracle-solana/SKILL.md +65 -0
- package/skills/oracle-solana-nft/SKILL.md +54 -0
- package/skills/oracle-token-research/SKILL.md +67 -0
- package/src/agent-auth.mjs +191 -0
- package/src/approval-guard.mjs +282 -0
- package/src/attestation-secret.mjs +88 -0
- package/src/audit-log.mjs +196 -0
- package/src/auto-slippage.mjs +378 -0
- package/src/capability-posture.mjs +125 -0
- package/src/chains.mjs +62 -0
- package/src/data/catalog.mjs +495 -0
- package/src/data/desk-data.mjs +623 -0
- package/src/data/http.mjs +200 -0
- package/src/data/provider-endpoint.mjs +94 -0
- package/src/data/providers/aerodrome.mjs +244 -0
- package/src/data/providers/balancer.mjs +208 -0
- package/src/data/providers/bitcoin-esplora.mjs +230 -0
- package/src/data/providers/bitcoin-meta.mjs +378 -0
- package/src/data/providers/blockscout.mjs +14 -0
- package/src/data/providers/bridges.mjs +241 -0
- package/src/data/providers/cowswap.mjs +501 -0
- package/src/data/providers/curve.mjs +200 -0
- package/src/data/providers/defillama.mjs +88 -0
- package/src/data/providers/dexscreener.mjs +43 -0
- package/src/data/providers/evm-rpc.mjs +203 -0
- package/src/data/providers/geckoterminal.mjs +34 -0
- package/src/data/providers/gmx.mjs +495 -0
- package/src/data/providers/hl-info.mjs +102 -0
- package/src/data/providers/hl-markets.mjs +210 -0
- package/src/data/providers/hl-perps.mjs +382 -0
- package/src/data/providers/hl-staking.mjs +352 -0
- package/src/data/providers/hl-ws.mjs +119 -0
- package/src/data/providers/hyperevm-dex.mjs +49 -0
- package/src/data/providers/jupiter.mjs +182 -0
- package/src/data/providers/lifi.mjs +150 -0
- package/src/data/providers/magiceden-sol.mjs +355 -0
- package/src/data/providers/morpho.mjs +173 -0
- package/src/data/providers/odos.mjs +155 -0
- package/src/data/providers/oneinch.mjs +173 -0
- package/src/data/providers/opensea-multichain.mjs +136 -0
- package/src/data/providers/opensea-nft.mjs +99 -0
- package/src/data/providers/paraswap.mjs +117 -0
- package/src/data/providers/pendle.mjs +187 -0
- package/src/data/providers/poly-public.mjs +96 -0
- package/src/data/providers/poly-ws.mjs +103 -0
- package/src/data/providers/rh-agent.mjs +59 -0
- package/src/data/providers/satflow.mjs +336 -0
- package/src/data/providers/solana-rpc.mjs +186 -0
- package/src/data/providers/uniswap-v3.mjs +303 -0
- package/src/data/providers/zerox.mjs +166 -0
- package/src/data/public-api-scan.mjs +61 -0
- package/src/data/quote-placeholder.mjs +31 -0
- package/src/exact-integer.mjs +72 -0
- package/src/exec-policy.mjs +444 -0
- package/src/flags.mjs +15 -0
- package/src/fresh-window.mjs +76 -0
- package/src/gmx-attestation.mjs +175 -0
- package/src/index.mjs +50 -0
- package/src/nft-gas-war-guard.mjs +139 -0
- package/src/onboarding/agent-keys.mjs +157 -0
- package/src/onboarding/index.mjs +18 -0
- package/src/onboarding/tiers.mjs +139 -0
- package/src/oracle-env.mjs +38 -0
- package/src/protocol-execution.mjs +84 -0
- package/src/public-api/buzz-integration.mjs +256 -0
- package/src/public-api/connect-agent.mjs +397 -0
- package/src/public-api/grants.mjs +142 -0
- package/src/public-api/http.mjs +374 -0
- package/src/public-control/aa-adapter.mjs +402 -0
- package/src/public-control/build-registry.mjs +227 -0
- package/src/public-control/bundler-client.mjs +372 -0
- package/src/public-control/grant-indexer.mjs +296 -0
- package/src/public-control/policy-render.mjs +69 -0
- package/src/public-control/policy-schema.mjs +318 -0
- package/src/public-control/runtime-config.mjs +265 -0
- package/src/public-control/session-key-model.mjs +374 -0
- package/src/public-control/session-orchestrator.mjs +412 -0
- package/src/route-attestation.mjs +132 -0
- package/src/router/best-execution.mjs +221 -0
- package/src/router/index.mjs +185 -0
- package/src/router/prepare-bridge.mjs +288 -0
- package/src/router/prepare-route.mjs +341 -0
- package/src/router/proposal.mjs +311 -0
- package/src/router/risk-classifier.mjs +119 -0
- package/src/router/route-sources.mjs +292 -0
- package/src/scanner/chains.config.mjs +381 -0
- package/src/scanner/contract.mjs +270 -0
- package/src/scanner/evm-scanner.mjs +394 -0
- package/src/scanner/index.mjs +9 -0
- package/src/scanner/v2-venue.mjs +335 -0
- package/src/scanner/v3-venue.mjs +290 -0
- package/src/scopes.mjs +44 -0
- package/src/sell-simulation.mjs +167 -0
- package/src/token-transfer-guard.mjs +188 -0
- package/src/vault-attestation.mjs +145 -0
- package/src/venues.mjs +206 -0
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
// Oracle Public HTTP surface (Slice G) — secret-free BFF over the public
|
|
2
|
+
// read/assemble contract.
|
|
3
|
+
//
|
|
4
|
+
// Hard boundaries for this file:
|
|
5
|
+
// - Imports ONLY the public API/peer modules (connect-agent, grants,
|
|
6
|
+
// policy-schema, oracle-env). NEVER the private executor stack
|
|
7
|
+
// (get-signer, keystore, exec-policy, local-signer/*, adapters/*,
|
|
8
|
+
// exec-server, mint-capability).
|
|
9
|
+
// - NEVER signs anything and NEVER holds key material. Every payload this
|
|
10
|
+
// server emits is unsigned public data; the user signs client-side.
|
|
11
|
+
// - Fail-closed no-secret invariant: EVERY response body is recursively
|
|
12
|
+
// scanned with assertNoSecretMaterial() before it is written to the
|
|
13
|
+
// socket. If the scan throws, the body is dropped and the client gets
|
|
14
|
+
// 500 { error: "secret-leak-blocked" } instead. The model proposes,
|
|
15
|
+
// the owner authorizes.
|
|
16
|
+
// - No auth secrets in code: this plane serves public reads/assembly only,
|
|
17
|
+
// binds to loopback by default, and carries no bearer/token logic.
|
|
18
|
+
|
|
19
|
+
import { createServer } from "node:http";
|
|
20
|
+
import { readFileSync, existsSync } from "node:fs";
|
|
21
|
+
import { dirname, join, normalize, sep } from "node:path";
|
|
22
|
+
import { fileURLToPath } from "node:url";
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
buildConnectRequest,
|
|
26
|
+
assembleUnsignedGrant,
|
|
27
|
+
assertNoSecretMaterial,
|
|
28
|
+
assertSerializedNoSecrets,
|
|
29
|
+
SecretLeakError,
|
|
30
|
+
} from "./connect-agent.mjs";
|
|
31
|
+
import { listActiveGrants, getGrant } from "./grants.mjs";
|
|
32
|
+
import { GrantValidationError } from "../public-control/policy-schema.mjs";
|
|
33
|
+
import { env } from "../oracle-env.mjs";
|
|
34
|
+
|
|
35
|
+
const pkg = JSON.parse(
|
|
36
|
+
readFileSync(new URL("../../package.json", import.meta.url), "utf8")
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
export const PUBLIC_PLANE = "public";
|
|
40
|
+
export const DEFAULT_HOST = "127.0.0.1";
|
|
41
|
+
export const DEFAULT_PORT = 8799;
|
|
42
|
+
export const VERSION = pkg.version;
|
|
43
|
+
|
|
44
|
+
const ROOT_DIR = join(dirname(fileURLToPath(import.meta.url)), "../..");
|
|
45
|
+
const CONSOLE_DIR = join(ROOT_DIR, "public", "oracle-console");
|
|
46
|
+
|
|
47
|
+
/** Allowlisted console static files only — no directory listing / traversal. */
|
|
48
|
+
const STATIC_FILES = Object.freeze({
|
|
49
|
+
"/": { file: "index.html", type: "text/html; charset=utf-8" },
|
|
50
|
+
"/index.html": { file: "index.html", type: "text/html; charset=utf-8" },
|
|
51
|
+
"/app.js": { file: "app.js", type: "application/javascript; charset=utf-8" },
|
|
52
|
+
"/styles.css": { file: "styles.css", type: "text/css; charset=utf-8" },
|
|
53
|
+
"/oracle-console": { file: "index.html", type: "text/html; charset=utf-8" },
|
|
54
|
+
"/oracle-console/": { file: "index.html", type: "text/html; charset=utf-8" },
|
|
55
|
+
"/oracle-console/index.html": { file: "index.html", type: "text/html; charset=utf-8" },
|
|
56
|
+
"/oracle-console/app.js": { file: "app.js", type: "application/javascript; charset=utf-8" },
|
|
57
|
+
"/oracle-console/styles.css": { file: "styles.css", type: "text/css; charset=utf-8" },
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
/** Refuse absurd bodies before parsing (fail closed, cheap DoS guard). */
|
|
61
|
+
const MAX_BODY_BYTES = 1_000_000;
|
|
62
|
+
|
|
63
|
+
class HttpError extends Error {
|
|
64
|
+
constructor(status, code, message) {
|
|
65
|
+
super(message || code);
|
|
66
|
+
this.status = status;
|
|
67
|
+
this.code = code;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Write a JSON response. The no-secret invariant runs HERE, on every body,
|
|
73
|
+
* immediately before the bytes hit the socket.
|
|
74
|
+
*
|
|
75
|
+
* Two layers, in order:
|
|
76
|
+
* 1. assertNoSecretMaterial() walks the object graph (precise path in the
|
|
77
|
+
* error, catches forbidden key names on real properties).
|
|
78
|
+
* 2. assertSerializedNoSecrets() scans the ACTUAL JSON bytes we are about to
|
|
79
|
+
* write. This is the authoritative check: toJSON(), getters, symbol keys
|
|
80
|
+
* and non-enumerable properties can all put a value on the wire that the
|
|
81
|
+
* graph walk never visited.
|
|
82
|
+
*
|
|
83
|
+
* If either throws, the original body is never serialized to the client; a
|
|
84
|
+
* fixed 500 secret-leak-blocked envelope is sent instead.
|
|
85
|
+
*/
|
|
86
|
+
function send(res, status, body) {
|
|
87
|
+
let payloadStatus = status;
|
|
88
|
+
let json;
|
|
89
|
+
try {
|
|
90
|
+
assertNoSecretMaterial(body);
|
|
91
|
+
json = assertSerializedNoSecrets(body);
|
|
92
|
+
} catch {
|
|
93
|
+
payloadStatus = 500;
|
|
94
|
+
json = JSON.stringify({ error: "secret-leak-blocked" });
|
|
95
|
+
}
|
|
96
|
+
res.writeHead(payloadStatus, {
|
|
97
|
+
"content-type": "application/json; charset=utf-8",
|
|
98
|
+
"cache-control": "no-store",
|
|
99
|
+
"x-oracle-plane": PUBLIC_PLANE,
|
|
100
|
+
"x-oracle-unsigned": "true",
|
|
101
|
+
});
|
|
102
|
+
res.end(json);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function readJsonBody(req) {
|
|
106
|
+
return new Promise((resolve, reject) => {
|
|
107
|
+
const chunks = [];
|
|
108
|
+
let size = 0;
|
|
109
|
+
req.on("data", (chunk) => {
|
|
110
|
+
size += chunk.length;
|
|
111
|
+
if (size > MAX_BODY_BYTES) {
|
|
112
|
+
reject(new HttpError(413, "payload-too-large", "request body too large"));
|
|
113
|
+
req.destroy();
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
chunks.push(chunk);
|
|
117
|
+
});
|
|
118
|
+
req.on("end", () => {
|
|
119
|
+
const raw = Buffer.concat(chunks).toString("utf8").trim();
|
|
120
|
+
if (raw === "") return resolve({});
|
|
121
|
+
try {
|
|
122
|
+
resolve(JSON.parse(raw));
|
|
123
|
+
} catch {
|
|
124
|
+
reject(new HttpError(400, "invalid-json", "request body must be valid JSON"));
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
req.on("error", () => reject(new HttpError(400, "bad-request", "request stream error")));
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function asPlainObject(v, code) {
|
|
132
|
+
if (v == null) return {};
|
|
133
|
+
if (typeof v !== "object" || Array.isArray(v)) {
|
|
134
|
+
throw new HttpError(400, code, `${code} must be a JSON object`);
|
|
135
|
+
}
|
|
136
|
+
return v;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ---------------------------------------------------------------------------
|
|
140
|
+
// Route handlers — each returns { status, body }; errors are thrown.
|
|
141
|
+
// ---------------------------------------------------------------------------
|
|
142
|
+
|
|
143
|
+
function handleHealth() {
|
|
144
|
+
return { status: 200, body: { ok: true, plane: PUBLIC_PLANE, version: VERSION } };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function handleConnectRequest(body) {
|
|
148
|
+
const b = asPlainObject(body, "body");
|
|
149
|
+
// Accept either { input, opts } or the raw grant fields as the body itself.
|
|
150
|
+
const input = b.input !== undefined ? asPlainObject(b.input, "input") : b;
|
|
151
|
+
const opts = asPlainObject(b.opts, "opts");
|
|
152
|
+
const rawInput = input === b ? (({ opts: _drop, ...rest }) => rest)(b) : input;
|
|
153
|
+
return { status: 200, body: buildConnectRequest(rawInput, opts) };
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function handleConnectAssemble(body) {
|
|
157
|
+
const b = asPlainObject(body, "body");
|
|
158
|
+
const input =
|
|
159
|
+
b.request !== undefined
|
|
160
|
+
? asPlainObject(b.request, "request")
|
|
161
|
+
: b.input !== undefined
|
|
162
|
+
? asPlainObject(b.input, "input")
|
|
163
|
+
: b;
|
|
164
|
+
const opts = asPlainObject(b.opts, "opts");
|
|
165
|
+
const rawInput = input === b ? (({ opts: _drop, ...rest }) => rest)(b) : input;
|
|
166
|
+
return { status: 200, body: assembleUnsignedGrant(rawInput, opts) };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function handleGrantsActive(body) {
|
|
170
|
+
const b = asPlainObject(body, "body");
|
|
171
|
+
const opts = asPlainObject(b.opts, "opts");
|
|
172
|
+
const grants = listActiveGrants(b.store ?? [], opts);
|
|
173
|
+
return { status: 200, body: { ok: true, count: grants.length, grants } };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function handleGrantsGet(body) {
|
|
177
|
+
const b = asPlainObject(body, "body");
|
|
178
|
+
const opts = asPlainObject(b.opts, "opts");
|
|
179
|
+
const found = getGrant(b.store ?? [], b.id, opts);
|
|
180
|
+
if (found == null) {
|
|
181
|
+
return { status: 404, body: { error: "grant-not-found" } };
|
|
182
|
+
}
|
|
183
|
+
return { status: 200, body: found };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Public runtime config for the browser console.
|
|
188
|
+
*
|
|
189
|
+
* Advertises which wallet-connection modes this deployment supports so the frontend
|
|
190
|
+
* does not have to guess or hardcode. Two modes, both self-custodial:
|
|
191
|
+
*
|
|
192
|
+
* injected EIP-1193 / EIP-6963 browser wallet (MetaMask, Rabby, ...)
|
|
193
|
+
* privy embedded/social login, still user-held keys
|
|
194
|
+
*
|
|
195
|
+
* A Privy APP ID is a PUBLIC client identifier -- it is designed to ship in
|
|
196
|
+
* frontend bundles. The app SECRET is server-side and must never appear here, which
|
|
197
|
+
* is why only the id is read and the send() path runs a secret scan over every
|
|
198
|
+
* response body regardless.
|
|
199
|
+
*
|
|
200
|
+
* Absent env means the mode is simply unavailable. Reporting it as disabled is
|
|
201
|
+
* honest; inventing a default app id would produce a broken login flow.
|
|
202
|
+
*/
|
|
203
|
+
function handleConfig() {
|
|
204
|
+
const privyAppId = (process.env.ORACLE_PRIVY_APP_ID || process.env.PRIVY_APP_ID || "").trim();
|
|
205
|
+
const privyEnabled = privyAppId.length > 0;
|
|
206
|
+
// Injected wallets need no server config, so this deployment can always offer it.
|
|
207
|
+
const injectedEnabled = true;
|
|
208
|
+
|
|
209
|
+
const modes = [];
|
|
210
|
+
if (privyEnabled) modes.push("privy");
|
|
211
|
+
if (injectedEnabled) modes.push("injected");
|
|
212
|
+
|
|
213
|
+
return {
|
|
214
|
+
status: 200,
|
|
215
|
+
body: {
|
|
216
|
+
ok: true,
|
|
217
|
+
plane: PUBLIC_PLANE,
|
|
218
|
+
version: VERSION,
|
|
219
|
+
custody: {
|
|
220
|
+
// Stated explicitly: no deployment of the public plane ever holds keys.
|
|
221
|
+
selfCustodial: true,
|
|
222
|
+
signer: "user-wallet",
|
|
223
|
+
privyEnabled,
|
|
224
|
+
injectedEnabled,
|
|
225
|
+
modes,
|
|
226
|
+
},
|
|
227
|
+
privy: {
|
|
228
|
+
// Public client id only. Never the app secret.
|
|
229
|
+
appId: privyAppId || null,
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const ROUTES = Object.freeze({
|
|
236
|
+
"GET /public/health": handleHealth,
|
|
237
|
+
"GET /public/config": handleConfig,
|
|
238
|
+
"POST /public/connect/request": handleConnectRequest,
|
|
239
|
+
"POST /public/connect/assemble": handleConnectAssemble,
|
|
240
|
+
"POST /public/grants/active": handleGrantsActive,
|
|
241
|
+
"POST /public/grants/get": handleGrantsGet,
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
function publicHeaders(extra = {}) {
|
|
245
|
+
return {
|
|
246
|
+
"cache-control": "no-store",
|
|
247
|
+
"x-oracle-plane": PUBLIC_PLANE,
|
|
248
|
+
"x-oracle-unsigned": "true",
|
|
249
|
+
...extra,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function sendStatic(res, status, contentType, body) {
|
|
254
|
+
res.writeHead(status, publicHeaders({ "content-type": contentType }));
|
|
255
|
+
res.end(body);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Serve allowlisted Oracle console assets only.
|
|
260
|
+
* Returns true if the request was handled (including 404 for missing file).
|
|
261
|
+
*/
|
|
262
|
+
function tryServeStatic(req, res, pathname) {
|
|
263
|
+
if (req.method !== "GET" && req.method !== "HEAD") return false;
|
|
264
|
+
// Keep trailing slash variants resolvable via STATIC_FILES keys.
|
|
265
|
+
const rawPath = pathname === "" ? "/" : pathname;
|
|
266
|
+
const meta = STATIC_FILES[rawPath] || STATIC_FILES[rawPath.replace(/\/+$/, "") || "/"];
|
|
267
|
+
if (!meta) return false;
|
|
268
|
+
|
|
269
|
+
const abs = normalize(join(CONSOLE_DIR, meta.file));
|
|
270
|
+
if (!abs.startsWith(CONSOLE_DIR + sep) && abs !== CONSOLE_DIR) {
|
|
271
|
+
send(res, 404, { error: "not-found", message: "unknown static path" });
|
|
272
|
+
return true;
|
|
273
|
+
}
|
|
274
|
+
if (!existsSync(abs)) {
|
|
275
|
+
send(res, 404, { error: "not-found", message: `missing static file ${meta.file}` });
|
|
276
|
+
return true;
|
|
277
|
+
}
|
|
278
|
+
const body = readFileSync(abs);
|
|
279
|
+
// Static HTML/JS/CSS are not JSON secret-scanned the same way as API bodies;
|
|
280
|
+
// content is repo-authored allowlisted files only.
|
|
281
|
+
if (req.method === "HEAD") {
|
|
282
|
+
res.writeHead(200, publicHeaders({
|
|
283
|
+
"content-type": meta.type,
|
|
284
|
+
"content-length": String(body.length),
|
|
285
|
+
}));
|
|
286
|
+
res.end();
|
|
287
|
+
return true;
|
|
288
|
+
}
|
|
289
|
+
sendStatic(res, 200, meta.type, body);
|
|
290
|
+
return true;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
async function handleRequest(req, res) {
|
|
294
|
+
const url = new URL(req.url, "http://localhost");
|
|
295
|
+
// Preserve a single trailing slash only for static console aliases.
|
|
296
|
+
let pathname = url.pathname || "/";
|
|
297
|
+
if (pathname.length > 1 && pathname.endsWith("/") && pathname !== "/oracle-console/") {
|
|
298
|
+
pathname = pathname.replace(/\/+$/, "");
|
|
299
|
+
}
|
|
300
|
+
if (pathname === "") pathname = "/";
|
|
301
|
+
const routePath = pathname.replace(/\/+$/, "") || "/";
|
|
302
|
+
const key = `${req.method} ${routePath}`;
|
|
303
|
+
const handler = ROUTES[key];
|
|
304
|
+
|
|
305
|
+
try {
|
|
306
|
+
if (!handler) {
|
|
307
|
+
if (tryServeStatic(req, res, pathname)) return;
|
|
308
|
+
const pathKnown = Object.keys(ROUTES).some((k) => k.endsWith(` ${routePath}`));
|
|
309
|
+
if (pathKnown) throw new HttpError(405, "method-not-allowed", `unsupported method for ${routePath}`);
|
|
310
|
+
throw new HttpError(404, "not-found", `unknown route ${routePath}`);
|
|
311
|
+
}
|
|
312
|
+
const body = req.method === "GET" || req.method === "HEAD" ? undefined : await readJsonBody(req);
|
|
313
|
+
const out = handler(body);
|
|
314
|
+
send(res, out.status, out.body);
|
|
315
|
+
} catch (err) {
|
|
316
|
+
if (err instanceof SecretLeakError) {
|
|
317
|
+
// Never echo the leak path/rule detail either — fixed envelope only.
|
|
318
|
+
send(res, 500, { error: "secret-leak-blocked" });
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
if (err instanceof GrantValidationError) {
|
|
322
|
+
send(res, 400, {
|
|
323
|
+
error: "invalid-grant",
|
|
324
|
+
errors: err.errors.map((e) => ({ field: e.field, message: e.message })),
|
|
325
|
+
});
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
if (err instanceof HttpError) {
|
|
329
|
+
send(res, err.status, { error: err.code, message: err.message });
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
if (err instanceof TypeError) {
|
|
333
|
+
send(res, 400, { error: "bad-request", message: err.message });
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
// Unknown failure: fixed envelope, no internal detail leaves the server.
|
|
337
|
+
send(res, 500, { error: "internal-error" });
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** Build (but do not bind) the public HTTP server. */
|
|
342
|
+
export function createPublicServer() {
|
|
343
|
+
return createServer((req, res) => {
|
|
344
|
+
handleRequest(req, res).catch(() => {
|
|
345
|
+
try {
|
|
346
|
+
send(res, 500, { error: "internal-error" });
|
|
347
|
+
} catch {
|
|
348
|
+
/* socket already gone */
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Resolve host/port: explicit args > ORACLE_PUBLIC_HOST/PORT env (via
|
|
356
|
+
* oracle-env's env() helper) > loopback 127.0.0.1:8799 default.
|
|
357
|
+
*/
|
|
358
|
+
export function resolvePublicBind({ host, port } = {}) {
|
|
359
|
+
const h = host ?? env("ORACLE_PUBLIC_HOST", "MAD_PUBLIC_HOST", DEFAULT_HOST);
|
|
360
|
+
const rawPort = port ?? env("ORACLE_PUBLIC_PORT", "MAD_PUBLIC_PORT", String(DEFAULT_PORT));
|
|
361
|
+
const p = Number(rawPort);
|
|
362
|
+
if (!Number.isInteger(p) || p < 0 || p > 65535) {
|
|
363
|
+
throw new TypeError(`invalid public port: ${String(rawPort)}`);
|
|
364
|
+
}
|
|
365
|
+
return { host: h, port: p };
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** Create + bind the public server. Returns the node:http server instance. */
|
|
369
|
+
export function startPublicServer(bind = {}) {
|
|
370
|
+
const { host, port } = resolvePublicBind(bind);
|
|
371
|
+
const server = createPublicServer();
|
|
372
|
+
server.listen(port, host);
|
|
373
|
+
return server;
|
|
374
|
+
}
|