@stablekernel/opencode-bifrost 0.6.0 → 0.8.0-rc.1
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 +11 -8
- package/package.json +2 -2
- package/src/datasheet-supplement.test.ts +133 -0
- package/src/index.test.ts +1 -53
- package/src/index.ts +52 -283
- package/src/shared.test.ts +141 -0
- package/src/shared.ts +306 -0
- package/src/v2.test.ts +394 -0
- package/src/v2.ts +319 -0
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
2
|
+
import { mkdtempSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import {
|
|
6
|
+
listCliModels,
|
|
7
|
+
readCliKey,
|
|
8
|
+
resolveInfo,
|
|
9
|
+
} from "./shared.js";
|
|
10
|
+
|
|
11
|
+
/** HOME as the suite found it, restored after each test. */
|
|
12
|
+
const ORIGINAL_HOME = process.env.HOME;
|
|
13
|
+
|
|
14
|
+
/** XDG_CACHE_HOME as the suite found it, restored after each test. */
|
|
15
|
+
const ORIGINAL_XDG_CACHE_HOME = process.env.XDG_CACHE_HOME;
|
|
16
|
+
|
|
17
|
+
/** GATEWAY_DATASHEET_URL as the suite found it, restored after each test. */
|
|
18
|
+
const ORIGINAL_DATASHEET_URL = process.env.GATEWAY_DATASHEET_URL;
|
|
19
|
+
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
// The listCliModels tests run the real datasheet supplement. Keep it
|
|
22
|
+
// deterministic and machine-independent: point HOME and the cache root
|
|
23
|
+
// at an empty temp dir, so neither ~/.config/bifrost/datasheet.json nor
|
|
24
|
+
// a warm ~/.cache/opencode cache is read, and drop any feed override
|
|
25
|
+
// from the developer's shell.
|
|
26
|
+
delete process.env.GATEWAY_DATASHEET_URL;
|
|
27
|
+
delete process.env.XDG_CACHE_HOME;
|
|
28
|
+
process.env.HOME = mkdtempSync(join(tmpdir(), "gw-oc-home-"));
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
afterEach(() => {
|
|
32
|
+
if (ORIGINAL_HOME === undefined) delete process.env.HOME;
|
|
33
|
+
else process.env.HOME = ORIGINAL_HOME;
|
|
34
|
+
if (ORIGINAL_XDG_CACHE_HOME === undefined) delete process.env.XDG_CACHE_HOME;
|
|
35
|
+
else process.env.XDG_CACHE_HOME = ORIGINAL_XDG_CACHE_HOME;
|
|
36
|
+
if (ORIGINAL_DATASHEET_URL === undefined) delete process.env.GATEWAY_DATASHEET_URL;
|
|
37
|
+
else process.env.GATEWAY_DATASHEET_URL = ORIGINAL_DATASHEET_URL;
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Records every exec and answers from a scripted map. These three helpers
|
|
42
|
+
* are the whole CLI-discovery surface both majors share, so the tests pin
|
|
43
|
+
* the exact subcommands and the parse/allowlist behavior once.
|
|
44
|
+
*/
|
|
45
|
+
function fakeExec(responses: Record<string, string>, calls: string[][]) {
|
|
46
|
+
return async (args: string[]): Promise<string | null> => {
|
|
47
|
+
calls.push(args);
|
|
48
|
+
const cmd = args.join(" ");
|
|
49
|
+
if (!(cmd in responses)) return null;
|
|
50
|
+
return responses[cmd];
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const RESOLVE_OK = JSON.stringify({
|
|
55
|
+
env: "prod",
|
|
56
|
+
root: "/srv/bifrost",
|
|
57
|
+
anthropic_base_url: "https://gw.example.com/anthropic",
|
|
58
|
+
openai_base_url: "https://gw.example.com/openai/v1",
|
|
59
|
+
has_key: true,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe("resolveInfo", () => {
|
|
63
|
+
test("parses `resolve --json` and returns the resolution", async () => {
|
|
64
|
+
const calls: string[][] = [];
|
|
65
|
+
const info = await resolveInfo(fakeExec({ "resolve --json": RESOLVE_OK }, calls));
|
|
66
|
+
expect(info?.env).toBe("prod");
|
|
67
|
+
expect(info?.openai_base_url).toBe("https://gw.example.com/openai/v1");
|
|
68
|
+
expect(calls).toEqual([["resolve", "--json"]]);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("refuses a non-allowlisted gateway origin", async () => {
|
|
72
|
+
const info = await resolveInfo(
|
|
73
|
+
fakeExec(
|
|
74
|
+
{
|
|
75
|
+
"resolve --json": JSON.stringify({
|
|
76
|
+
...JSON.parse(RESOLVE_OK),
|
|
77
|
+
openai_base_url: "http://evil.example/v1",
|
|
78
|
+
}),
|
|
79
|
+
},
|
|
80
|
+
[],
|
|
81
|
+
),
|
|
82
|
+
);
|
|
83
|
+
expect(info).toBeNull();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("returns null when the CLI is missing or emits garbage", async () => {
|
|
87
|
+
expect(await resolveInfo(async () => null)).toBeNull();
|
|
88
|
+
expect(await resolveInfo(fakeExec({ "resolve --json": "not json" }, []))).toBeNull();
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe("readCliKey", () => {
|
|
93
|
+
test("returns the key when it is a virtual key", async () => {
|
|
94
|
+
const calls: string[][] = [];
|
|
95
|
+
const key = await readCliKey(
|
|
96
|
+
fakeExec({ "key print": "sk-bf-projectkey\n" }, calls),
|
|
97
|
+
);
|
|
98
|
+
expect(key).toBe("sk-bf-projectkey");
|
|
99
|
+
expect(calls).toEqual([["key", "print"]]);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("rejects a non-virtual-key value", async () => {
|
|
103
|
+
expect(await readCliKey(fakeExec({ "key print": "hunter2" }, []))).toBeNull();
|
|
104
|
+
expect(await readCliKey(async () => null)).toBeNull();
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe("listCliModels", () => {
|
|
109
|
+
const PAYLOAD = JSON.stringify({
|
|
110
|
+
wire: "openai-completions",
|
|
111
|
+
env: "prod",
|
|
112
|
+
models: [
|
|
113
|
+
{
|
|
114
|
+
id: "bedrock/amazon.nova-lite-v1:0",
|
|
115
|
+
name: "Nova Lite",
|
|
116
|
+
context_window: 300000,
|
|
117
|
+
max_output_tokens: 8192,
|
|
118
|
+
limits_source: "catalog",
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("parses `models --wire=openai` into the model list", async () => {
|
|
124
|
+
const calls: string[][] = [];
|
|
125
|
+
const payload = await listCliModels(
|
|
126
|
+
fakeExec({ "models --wire=openai": PAYLOAD }, calls),
|
|
127
|
+
"/repo",
|
|
128
|
+
);
|
|
129
|
+
expect(payload?.models?.map((m) => m.id)).toEqual([
|
|
130
|
+
"bedrock/amazon.nova-lite-v1:0",
|
|
131
|
+
]);
|
|
132
|
+
expect(calls).toEqual([["models", "--wire=openai"]]);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test("returns null when the CLI is missing or emits garbage", async () => {
|
|
136
|
+
expect(await listCliModels(async () => null, "/repo")).toBeNull();
|
|
137
|
+
expect(
|
|
138
|
+
await listCliModels(fakeExec({ "models --wire=openai": "not json" }, []), "/repo"),
|
|
139
|
+
).toBeNull();
|
|
140
|
+
});
|
|
141
|
+
});
|
package/src/shared.ts
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { datasheetSupplement, mergeModels } from "./datasheet.js";
|
|
4
|
+
import { keyFileValue, type Exec } from "./refresh.js";
|
|
5
|
+
|
|
6
|
+
/** Provider id registered with OpenCode. */
|
|
7
|
+
export const PROVIDER_ID = "bifrost";
|
|
8
|
+
|
|
9
|
+
/** Overridable so a non-PATH install still works. */
|
|
10
|
+
export const CLI = process.env.GATEWAY_CLI_BIN ?? "gateway-cli";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Suffix for models nothing could price.
|
|
14
|
+
*
|
|
15
|
+
* The gateway reports no pricing, and the public catalog does not cover every
|
|
16
|
+
* model it serves. Declaring a zero rate would render as "free", so the rate
|
|
17
|
+
* card is omitted entirely and the name carries the caveat instead.
|
|
18
|
+
*/
|
|
19
|
+
export const UNPRICED_SUFFIX = " (pricing unknown)";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Non-secret wiring emitted by `gateway-cli resolve --json`. The virtual key
|
|
23
|
+
* is deliberately absent: it is read separately via `gateway-cli key print`
|
|
24
|
+
* so this payload can be logged.
|
|
25
|
+
*/
|
|
26
|
+
export type Resolution = {
|
|
27
|
+
env: string;
|
|
28
|
+
root: string;
|
|
29
|
+
anthropic_base_url: string;
|
|
30
|
+
openai_base_url: string;
|
|
31
|
+
has_key: boolean;
|
|
32
|
+
key_name?: string;
|
|
33
|
+
key_source?: string;
|
|
34
|
+
headers?: Record<string, string>;
|
|
35
|
+
/** Resolved default reasoning effort (project over user), empty unset. */
|
|
36
|
+
reasoning_effort?: string;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** One model as reported by `gateway-cli models`. */
|
|
40
|
+
export type CliModel = {
|
|
41
|
+
id: string;
|
|
42
|
+
name: string;
|
|
43
|
+
context_window: number;
|
|
44
|
+
max_output_tokens: number;
|
|
45
|
+
cost?: {
|
|
46
|
+
input: number;
|
|
47
|
+
output: number;
|
|
48
|
+
cache_read: number;
|
|
49
|
+
cache_write: number;
|
|
50
|
+
};
|
|
51
|
+
limits_source: string;
|
|
52
|
+
cost_source?: string;
|
|
53
|
+
/** True when the gateway lists the model but it rejects completion on
|
|
54
|
+
* this key (not entitled, needs provisioned throughput, retired). */
|
|
55
|
+
disabled?: boolean;
|
|
56
|
+
/** Reasoning capability and applicable efforts, from the catalog. */
|
|
57
|
+
reasoning?: boolean;
|
|
58
|
+
reasoning_efforts?: string[];
|
|
59
|
+
reasoning_budget_min?: number;
|
|
60
|
+
/** True when the model accepts image content blocks (from the catalog's
|
|
61
|
+
* modalities.input). Absent means text-only, so a harness never guesses a
|
|
62
|
+
* capability from silence. */
|
|
63
|
+
image_input?: boolean;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export type CliModels = { models?: CliModel[] };
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* How long a "plugin is current" answer is trusted before the registry is
|
|
70
|
+
* asked again. Kept on disk so the once-daily check costs nothing on the
|
|
71
|
+
* frequent path (every opencode startup constructs this plugin).
|
|
72
|
+
*/
|
|
73
|
+
const VERSION_CHECK_TTL_MS = 24 * 60 * 60 * 1000;
|
|
74
|
+
|
|
75
|
+
/** npm registry endpoint for the published package's latest version. */
|
|
76
|
+
const REGISTRY_URL =
|
|
77
|
+
"https://registry.npmjs.org/@stablekernel/opencode-bifrost/latest";
|
|
78
|
+
|
|
79
|
+
/** The version of this build, read from the package's own package.json. */
|
|
80
|
+
function ownVersion(): string | null {
|
|
81
|
+
try {
|
|
82
|
+
const pkg = require("../package.json") as { version?: string };
|
|
83
|
+
return pkg.version ?? null;
|
|
84
|
+
} catch {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
type VersionCheckCache = { checkedAt: number; latest: string };
|
|
90
|
+
|
|
91
|
+
/** opencode's plugin cache root; matches where the host itself caches. */
|
|
92
|
+
function versionCheckPath(): string | null {
|
|
93
|
+
const home = process.env.HOME;
|
|
94
|
+
if (!home) return null;
|
|
95
|
+
const base = process.env.XDG_CACHE_HOME ?? `${home}/.cache`;
|
|
96
|
+
return `${base}/opencode/bifrost-version-check.json`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Where the datasheet feed cache lives; same base as the version check. */
|
|
100
|
+
function datasheetCacheDir(): string | null {
|
|
101
|
+
const home = process.env.HOME || homedir() || null;
|
|
102
|
+
const base = process.env.XDG_CACHE_HOME ?? (home ? `${home}/.cache` : null);
|
|
103
|
+
return base ? `${base}/opencode` : null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Run `gateway-cli resolve --json` through `exec` and allowlist the origin it
|
|
108
|
+
* reports. Null when the CLI is missing, the payload is malformed, or the
|
|
109
|
+
* origin is not a gateway one — the same gate every consumer applies, so it
|
|
110
|
+
* lives here.
|
|
111
|
+
*/
|
|
112
|
+
export async function resolveInfo(exec: Exec): Promise<Resolution | null> {
|
|
113
|
+
const raw = await exec(["resolve", "--json"]);
|
|
114
|
+
if (!raw) return null;
|
|
115
|
+
try {
|
|
116
|
+
const parsed = JSON.parse(raw) as Resolution;
|
|
117
|
+
// Allowlist the outbound origin before any derived URL is fetched.
|
|
118
|
+
return isAllowedGatewayUrl(parsed.openai_base_url) ? parsed : null;
|
|
119
|
+
} catch {
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Read the virtual key via `gateway-cli key print`. Only a well-formed
|
|
126
|
+
* virtual key is returned; anything else (empty, another credential) is null.
|
|
127
|
+
* Trimmed, so callers get the key without the CLI's trailing newline.
|
|
128
|
+
*/
|
|
129
|
+
export async function readCliKey(exec: Exec): Promise<string | null> {
|
|
130
|
+
const key = await exec(["key", "print"]);
|
|
131
|
+
const trimmed = key?.trim();
|
|
132
|
+
return trimmed && trimmed.startsWith("sk-bf-") ? trimmed : null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Fetch the model list via `gateway-cli models --wire=openai` and merge in
|
|
137
|
+
* the datasheet supplement: wildcard-provider ids the CLI cannot list yet
|
|
138
|
+
* (a feed Bifrost itself syncs from); CLI-listed ids always win. `root` is
|
|
139
|
+
* the project directory, where a per-project `.gateway/datasheet.json` may
|
|
140
|
+
* live — under OpenCode 2 the process cwd is the background service's, not
|
|
141
|
+
* the project's. Null when the CLI is missing or its payload is malformed.
|
|
142
|
+
*/
|
|
143
|
+
export async function listCliModels(
|
|
144
|
+
exec: Exec,
|
|
145
|
+
root: string,
|
|
146
|
+
): Promise<CliModels | null> {
|
|
147
|
+
const raw = await exec(["models", "--wire=openai"]);
|
|
148
|
+
if (!raw) return null;
|
|
149
|
+
try {
|
|
150
|
+
const payload = JSON.parse(raw) as CliModels;
|
|
151
|
+
const cacheDir = datasheetCacheDir();
|
|
152
|
+
if (cacheDir) {
|
|
153
|
+
const extra = await datasheetSupplement(root, cacheDir);
|
|
154
|
+
payload.models = mergeModels(payload.models ?? [], extra);
|
|
155
|
+
}
|
|
156
|
+
return payload;
|
|
157
|
+
} catch {
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* The command's picker description, shared so the two plugin majors cannot
|
|
164
|
+
* drift. Lives here (not refresh.ts, which is byte-identical with
|
|
165
|
+
* pi-bifrost's copy and must not grow opencode-only text).
|
|
166
|
+
*/
|
|
167
|
+
export const REFRESH_DESCRIPTION =
|
|
168
|
+
"Reconcile the Bifrost model list (gateway-cli sync), or list " +
|
|
169
|
+
"the gateway directly when the CLI is missing";
|
|
170
|
+
|
|
171
|
+
/** What the model sees if the command hook cannot inject its report. */
|
|
172
|
+
export const REFRESH_TEMPLATE =
|
|
173
|
+
"Refresh the Bifrost model list: run `gateway-cli sync` if it is " +
|
|
174
|
+
"installed, otherwise list the gateway's /v1/models directly, " +
|
|
175
|
+
"and report what changed.";
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The virtual key from the CLI's own default key file. Only the CLI-less
|
|
179
|
+
* refresh path reads it: normal provider registration never needs the key,
|
|
180
|
+
* and the value stays in memory for the one request header that uses it.
|
|
181
|
+
*/
|
|
182
|
+
export function readKeyFile(): string | null {
|
|
183
|
+
const home = process.env.HOME || homedir();
|
|
184
|
+
const base =
|
|
185
|
+
process.env.XDG_CONFIG_HOME ?? (home ? `${home}/.config` : null);
|
|
186
|
+
if (!base) return null;
|
|
187
|
+
try {
|
|
188
|
+
const path = `${base}/bifrost/keys/default.env`;
|
|
189
|
+
return keyFileValue(readFileSync(path, "utf8"));
|
|
190
|
+
} catch {
|
|
191
|
+
return null; // absent, unreadable, or not a key file
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* The latest published version, from a fresh-enough cache file when present
|
|
197
|
+
* and the npm registry otherwise. Null means unknown — offline, timeout, or
|
|
198
|
+
* an unreadable cache — and never warns.
|
|
199
|
+
*/
|
|
200
|
+
async function latestPublishedVersion(): Promise<string | null> {
|
|
201
|
+
const path = versionCheckPath();
|
|
202
|
+
if (path) {
|
|
203
|
+
try {
|
|
204
|
+
const cached = JSON.parse(
|
|
205
|
+
await Bun.file(path).text(),
|
|
206
|
+
) as VersionCheckCache;
|
|
207
|
+
if (
|
|
208
|
+
typeof cached.latest === "string" &&
|
|
209
|
+
Date.now() - cached.checkedAt < VERSION_CHECK_TTL_MS
|
|
210
|
+
) {
|
|
211
|
+
return cached.latest;
|
|
212
|
+
}
|
|
213
|
+
} catch {
|
|
214
|
+
// Missing or corrupt cache: fall through to the registry.
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
let latest: string | null = null;
|
|
218
|
+
try {
|
|
219
|
+
const res = await fetch(REGISTRY_URL, {
|
|
220
|
+
signal: AbortSignal.timeout(1500),
|
|
221
|
+
});
|
|
222
|
+
if (res.ok) {
|
|
223
|
+
const body = (await res.json()) as { version?: string };
|
|
224
|
+
latest = typeof body.version === "string" ? body.version : null;
|
|
225
|
+
}
|
|
226
|
+
} catch {
|
|
227
|
+
return null; // offline or slow registry: stay silent
|
|
228
|
+
}
|
|
229
|
+
if (latest && path) {
|
|
230
|
+
try {
|
|
231
|
+
const { mkdirSync, writeFileSync } = await import("node:fs");
|
|
232
|
+
const { dirname } = await import("node:path");
|
|
233
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
234
|
+
writeFileSync(
|
|
235
|
+
path,
|
|
236
|
+
JSON.stringify({
|
|
237
|
+
checkedAt: Date.now(),
|
|
238
|
+
latest,
|
|
239
|
+
} satisfies VersionCheckCache),
|
|
240
|
+
);
|
|
241
|
+
} catch {
|
|
242
|
+
// A failed cache write just means the next start asks again.
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return latest;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Whether the installed build should warn about a newer release. Dev builds
|
|
250
|
+
* ahead of the registry (installed > latest) are not stale.
|
|
251
|
+
*/
|
|
252
|
+
export function isStale(installed: string, latest: string): boolean {
|
|
253
|
+
if (installed === latest) return false;
|
|
254
|
+
// Strict semver core only: parseInt would silently accept "0.3.0-dev" as
|
|
255
|
+
// 0.3.0 and misjudge the comparison. Odd shapes (prereleases, two-part
|
|
256
|
+
// versions) fall back to "warn on any difference" rather than a guess.
|
|
257
|
+
const SEMVER = /^(\d+)\.(\d+)\.(\d+)$/;
|
|
258
|
+
const a = SEMVER.exec(installed);
|
|
259
|
+
const b = SEMVER.exec(latest);
|
|
260
|
+
if (!a || !b) return true;
|
|
261
|
+
for (let i = 1; i <= 3; i++) {
|
|
262
|
+
const av = Number(a[i]);
|
|
263
|
+
const bv = Number(b[i]);
|
|
264
|
+
if (bv > av) return true;
|
|
265
|
+
if (bv < av) return false;
|
|
266
|
+
}
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Report a newer published plugin exactly once per version via the host's
|
|
272
|
+
* log. Fire-and-forget from provider registration: version drift must never
|
|
273
|
+
* block or fail it.
|
|
274
|
+
*/
|
|
275
|
+
export async function announceIfStale(
|
|
276
|
+
log: (message: string) => void,
|
|
277
|
+
): Promise<void> {
|
|
278
|
+
const installed = ownVersion();
|
|
279
|
+
if (!installed) return;
|
|
280
|
+
const latest = await latestPublishedVersion();
|
|
281
|
+
if (!latest || !isStale(installed, latest)) return;
|
|
282
|
+
log(
|
|
283
|
+
`opencode-bifrost ${latest} is available (installed ${installed}). ` +
|
|
284
|
+
"Run `gateway-cli update` to refresh.",
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Accept only TLS gateway origins, plus plain-HTTP loopback for local
|
|
290
|
+
* gateway development. Keeps a malformed or hostile config from redirecting
|
|
291
|
+
* model discovery and the credential that goes with it.
|
|
292
|
+
*/
|
|
293
|
+
export function isAllowedGatewayUrl(raw: string): boolean {
|
|
294
|
+
let url: URL;
|
|
295
|
+
try {
|
|
296
|
+
url = new URL(raw);
|
|
297
|
+
} catch {
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
if (url.protocol === "https:") return true;
|
|
301
|
+
const loopback =
|
|
302
|
+
url.hostname === "localhost" ||
|
|
303
|
+
url.hostname === "127.0.0.1" ||
|
|
304
|
+
url.hostname === "::1";
|
|
305
|
+
return url.protocol === "http:" && loopback;
|
|
306
|
+
}
|