passcontrol 0.6.0 → 0.6.2
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 +1 -1
- package/bin/passcontrol.mjs +452 -17
- package/cli/config.mjs +60 -3
- package/cli/protocols.d.mts +16 -0
- package/cli/protocols.mjs +61 -0
- package/cli/proxy-policy.mjs +281 -0
- package/cli/sidecar.mjs +181 -8
- package/cli/update-check.mjs +169 -0
- package/cli/verify.mjs +4 -1
- package/cli/workspace-import-report.mjs +24 -0
- package/package.json +1 -1
- package/sdk/control.d.ts +7 -0
- package/sdk/control.js +6 -0
- package/sdk/passcontrol.d.ts +1 -1
- package/sdk/verify.d.ts +12 -1
- package/sdk/verify.js +2 -1
package/cli/config.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import fs from "node:fs";
|
|
|
2
2
|
import os from "node:os";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { WORKSPACE_EXPORT_PROTOCOL } from "./protocols.mjs";
|
|
5
6
|
|
|
6
7
|
export const CONFIG_FILE = ".passcontrol";
|
|
7
8
|
|
|
@@ -16,8 +17,13 @@ export const PACKAGE_VERSION = (() => {
|
|
|
16
17
|
return "0.0.0";
|
|
17
18
|
}
|
|
18
19
|
})();
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
// The CLI imports snapshots only through the shared protocol declaration. A
|
|
21
|
+
// standalone package still has no app dependency, but no longer carries a
|
|
22
|
+
// second numeric copy that can drift from system-health capability reporting.
|
|
23
|
+
export const WORKSPACE_IMPORT_MAX_VERSION = WORKSPACE_EXPORT_PROTOCOL.maximum;
|
|
24
|
+
|
|
25
|
+
export const PROVIDERS = ["openai", "anthropic", "groq", "mistral", "together", "deepseek", "gemini"];
|
|
26
|
+
export const OPENAI_SHAPE_PROVIDERS = new Set(["openai", "groq", "mistral", "together", "deepseek", "gemini"]);
|
|
21
27
|
|
|
22
28
|
const DEFAULT_GATEWAY = "http://localhost:3000";
|
|
23
29
|
const DEFAULT_PROVIDER = "anthropic";
|
|
@@ -63,6 +69,8 @@ export function defaultModelForProvider(provider) {
|
|
|
63
69
|
return "openai/gpt-oss-20b";
|
|
64
70
|
case "deepseek":
|
|
65
71
|
return "deepseek-chat";
|
|
72
|
+
case "gemini":
|
|
73
|
+
return "gemini-2.5-flash";
|
|
66
74
|
case "anthropic":
|
|
67
75
|
default:
|
|
68
76
|
return "claude-haiku-4-5";
|
|
@@ -134,16 +142,44 @@ export function loadConfigSources({ cwd = process.cwd(), env = process.env } = {
|
|
|
134
142
|
return sources;
|
|
135
143
|
}
|
|
136
144
|
|
|
145
|
+
/**
|
|
146
|
+
* Keys that arrived from a `.passcontrol` FILE rather than the operator's shell.
|
|
147
|
+
*
|
|
148
|
+
* A config file is checked in and travels with a repository, so for anything
|
|
149
|
+
* security-relevant "the environment says so" and "the operator said so" are not
|
|
150
|
+
* the same statement. Recorded at injection time rather than derived afterwards:
|
|
151
|
+
* a key present in BOTH the shell and a file keeps its shell value below, and
|
|
152
|
+
* that value is the operator's — so asking the file later would wrongly disown it.
|
|
153
|
+
*/
|
|
154
|
+
export const configInjectedKeys = new Set();
|
|
155
|
+
|
|
137
156
|
export function applyConfigSourcesToEnv({ cwd = process.cwd(), env = process.env } = {}) {
|
|
138
157
|
const sources = loadConfigSources({ cwd, env });
|
|
139
158
|
const merged = {};
|
|
140
159
|
for (const source of sources) Object.assign(merged, source.values);
|
|
141
160
|
for (const [key, value] of Object.entries(merged)) {
|
|
142
|
-
if (env[key] === undefined)
|
|
161
|
+
if (env[key] === undefined) {
|
|
162
|
+
env[key] = value;
|
|
163
|
+
configInjectedKeys.add(key);
|
|
164
|
+
}
|
|
143
165
|
}
|
|
144
166
|
return sources;
|
|
145
167
|
}
|
|
146
168
|
|
|
169
|
+
/**
|
|
170
|
+
* Read an environment variable ONLY if the operator set it.
|
|
171
|
+
*
|
|
172
|
+
* For an egress or credential control, a value a cloned repository's
|
|
173
|
+
* `.passcontrol` dropped into the environment must not stand in for a decision
|
|
174
|
+
* the operator made. Everything else — `MODEL`, `SIDECAR_PORT`, `PROMPT` — is
|
|
175
|
+
* still perfectly good to configure from a file and goes on reading `process.env`
|
|
176
|
+
* directly; this is deliberately opt-in per key rather than a blanket filter,
|
|
177
|
+
* because narrowing what a config file may set would break ordinary use.
|
|
178
|
+
*/
|
|
179
|
+
export function operatorEnv(key, { env = process.env, injected = configInjectedKeys } = {}) {
|
|
180
|
+
return injected.has(key) ? undefined : env[key];
|
|
181
|
+
}
|
|
182
|
+
|
|
147
183
|
export let configLoadError = null;
|
|
148
184
|
export let configSources = [];
|
|
149
185
|
|
|
@@ -319,6 +355,27 @@ export function bareGatewayOrigin(gateway, label = "PASSCONTROL_GATEWAY") {
|
|
|
319
355
|
* part of the credential's security, not configuration taste. Every `api()` call
|
|
320
356
|
* resolves its destination through this, never through the raw config string.
|
|
321
357
|
*/
|
|
358
|
+
/**
|
|
359
|
+
* The same origin rule, for probes that carry NO credential.
|
|
360
|
+
*
|
|
361
|
+
* `passcontrol version` reads an unauthenticated /api/version, so the leak the
|
|
362
|
+
* rule normally prevents — a signature or a `pc_` key reaching a host the
|
|
363
|
+
* operator did not choose — does not apply. The destination still has to be
|
|
364
|
+
* validated: a project-local `.passcontrol` pointing elsewhere would make a
|
|
365
|
+
* DIAGNOSTIC command report a stranger's build as your gateway's, which is its
|
|
366
|
+
* own kind of wrong answer, and it would turn `version` into a beacon.
|
|
367
|
+
*
|
|
368
|
+
* Returns null rather than throwing. A malformed gateway is a row in a report
|
|
369
|
+
* here, not a reason to abort the command the operator actually ran.
|
|
370
|
+
*/
|
|
371
|
+
export function probeGatewayOrigin(current = config) {
|
|
372
|
+
try {
|
|
373
|
+
return bareGatewayOrigin(current.gateway);
|
|
374
|
+
} catch {
|
|
375
|
+
return null;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
322
379
|
export function requireControlGateway(current = config) {
|
|
323
380
|
assertConfigLoaded();
|
|
324
381
|
return bareGatewayOrigin(current.gateway);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface ProtocolRange {
|
|
2
|
+
minimum: number;
|
|
3
|
+
maximum: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export const CONTROL_API_PROTOCOL: ProtocolRange;
|
|
7
|
+
export const GATEWAY_API_PROTOCOL: ProtocolRange;
|
|
8
|
+
export const RECEIPT_PROTOCOL: ProtocolRange;
|
|
9
|
+
export const AGENT_TOKEN_PROTOCOL: ProtocolRange;
|
|
10
|
+
export const WORKSPACE_EXPORT_PROTOCOL: ProtocolRange;
|
|
11
|
+
export const CLIENT_PROTOCOLS: Readonly<Record<string, ProtocolRange>>;
|
|
12
|
+
export function compareProtocolRanges(client: ProtocolRange, server: unknown): "compatible" | "partial" | "update-required" | "unavailable";
|
|
13
|
+
export function compareProtocolSets(serverProtocols: unknown): {
|
|
14
|
+
state: "compatible" | "partial" | "update-required" | "unavailable";
|
|
15
|
+
checks: Array<{ name: string; client: ProtocolRange; server: unknown; state: "compatible" | "partial" | "update-required" | "unavailable" }>;
|
|
16
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// Compatibility vocabulary shared by the Control API and the standalone CLI.
|
|
2
|
+
// The CLI ships without the Next app, so this plain ESM module is the canonical
|
|
3
|
+
// runtime source; its declaration lets Edge TypeScript import the same values.
|
|
4
|
+
export const CONTROL_API_PROTOCOL = { minimum: 1, maximum: 1 };
|
|
5
|
+
export const GATEWAY_API_PROTOCOL = { minimum: 1, maximum: 1 };
|
|
6
|
+
export const RECEIPT_PROTOCOL = { minimum: 1, maximum: 2 };
|
|
7
|
+
export const AGENT_TOKEN_PROTOCOL = { minimum: 1, maximum: 1 };
|
|
8
|
+
export const WORKSPACE_EXPORT_PROTOCOL = { minimum: 1, maximum: 1 };
|
|
9
|
+
|
|
10
|
+
export const CLIENT_PROTOCOLS = Object.freeze({
|
|
11
|
+
control_api: CONTROL_API_PROTOCOL,
|
|
12
|
+
gateway_api: GATEWAY_API_PROTOCOL,
|
|
13
|
+
receipt: RECEIPT_PROTOCOL,
|
|
14
|
+
agent_token: AGENT_TOKEN_PROTOCOL,
|
|
15
|
+
workspace_export: WORKSPACE_EXPORT_PROTOCOL,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const validRange = (value) =>
|
|
19
|
+
value &&
|
|
20
|
+
Number.isInteger(value.minimum) &&
|
|
21
|
+
Number.isInteger(value.maximum) &&
|
|
22
|
+
value.minimum >= 1 &&
|
|
23
|
+
value.minimum <= value.maximum;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Compare negotiated version ranges without pretending a missing or malformed
|
|
27
|
+
* server declaration is compatible. A non-identical overlap is useful but
|
|
28
|
+
* partial: an operator should know the pair is not speaking precisely the same
|
|
29
|
+
* contract before relying on a newer optional feature.
|
|
30
|
+
*/
|
|
31
|
+
export function compareProtocolRanges(client, server) {
|
|
32
|
+
if (!validRange(server)) return "unavailable";
|
|
33
|
+
// A server declaring a higher maximum may emit semantics this CLI cannot
|
|
34
|
+
// interpret, even when the numeric ranges overlap. Do not dress that up as
|
|
35
|
+
// partial compatibility. Conversely, an older server inside our maximum can
|
|
36
|
+
// still serve the shared contract and is merely partial.
|
|
37
|
+
if (
|
|
38
|
+
server.maximum > client.maximum ||
|
|
39
|
+
server.maximum < client.minimum
|
|
40
|
+
) return "update-required";
|
|
41
|
+
if (server.minimum !== client.minimum || server.maximum !== client.maximum) return "partial";
|
|
42
|
+
return "compatible";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function compareProtocolSets(serverProtocols) {
|
|
46
|
+
const checks = Object.entries(CLIENT_PROTOCOLS).map(([name, client]) => ({
|
|
47
|
+
name,
|
|
48
|
+
client,
|
|
49
|
+
server: serverProtocols?.[name],
|
|
50
|
+
state: compareProtocolRanges(client, serverProtocols?.[name]),
|
|
51
|
+
}));
|
|
52
|
+
const states = checks.map((check) => check.state);
|
|
53
|
+
const state = states.includes("update-required")
|
|
54
|
+
? "update-required"
|
|
55
|
+
: states.includes("unavailable")
|
|
56
|
+
? "unavailable"
|
|
57
|
+
: states.includes("partial")
|
|
58
|
+
? "partial"
|
|
59
|
+
: "compatible";
|
|
60
|
+
return { state, checks };
|
|
61
|
+
}
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
// Destination policy for the sidecar's forward-proxy front door.
|
|
2
|
+
//
|
|
3
|
+
// Everything else in the sidecar routes to a destination WE configured. This
|
|
4
|
+
// module is the one place that judges a destination the CLIENT chose, which is
|
|
5
|
+
// what `HTTPS_PROXY=http://127.0.0.1:8788` turns every agent request into. Two
|
|
6
|
+
// failures are possible and both are bad:
|
|
7
|
+
//
|
|
8
|
+
// * tunnel a provider host → an ungoverned provider call carried by the
|
|
9
|
+
// component whose entire job is governing it, with the operator believing
|
|
10
|
+
// scope and budget applied;
|
|
11
|
+
// * tunnel anything asked → an open proxy on a developer laptop.
|
|
12
|
+
//
|
|
13
|
+
// So the rule is deny-by-default with two narrow exits: the gateway we already
|
|
14
|
+
// send visas to, and hosts the operator named on the command line.
|
|
15
|
+
//
|
|
16
|
+
// Why a provider CONNECT is refused rather than governed: injecting the key
|
|
17
|
+
// would mean terminating the TLS, which means a locally trusted CA that can
|
|
18
|
+
// impersonate any host to any process trusting it. PassControl exists to get a
|
|
19
|
+
// secret with a one-provider blast radius off the agent's machine; installing a
|
|
20
|
+
// broader one to save an environment variable is the wrong trade, and every
|
|
21
|
+
// mainstream provider SDK already honours a base-URL variable that needs no CA.
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The provider upstreams, as host + base path.
|
|
25
|
+
*
|
|
26
|
+
* A second copy of `lib/providers.ts`'s `upstreamBaseUrl`, for the same reason
|
|
27
|
+
* `bareGatewayOrigin` exists twice: `cli/` is plain .mjs run straight from the
|
|
28
|
+
* checkout and published as-is, so it cannot import TypeScript that only exists
|
|
29
|
+
* after `tsc`. `tests/cli-provider-hosts.test.ts` runs both through one table and
|
|
30
|
+
* fails the moment they disagree — a provider added there and missed here is a
|
|
31
|
+
* provider this file would let through.
|
|
32
|
+
*/
|
|
33
|
+
export const PROVIDER_UPSTREAMS = {
|
|
34
|
+
openai: { hostname: "api.openai.com", basePath: "" },
|
|
35
|
+
anthropic: { hostname: "api.anthropic.com", basePath: "" },
|
|
36
|
+
groq: { hostname: "api.groq.com", basePath: "/openai" },
|
|
37
|
+
mistral: { hostname: "api.mistral.ai", basePath: "" },
|
|
38
|
+
together: { hostname: "api.together.ai", basePath: "" },
|
|
39
|
+
deepseek: { hostname: "api.deepseek.com", basePath: "" },
|
|
40
|
+
// Gemini's OpenAI-compat base carries a two-segment prefix, unlike groq's
|
|
41
|
+
// single `/openai`. Listing it here is what makes a CONNECT to Google refuse
|
|
42
|
+
// as `provider_tunnel_not_governed` — and, deliberately, what makes it
|
|
43
|
+
// permanently ineligible for `--allow-connect`.
|
|
44
|
+
gemini: { hostname: "generativelanguage.googleapis.com", basePath: "/v1beta/openai" },
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const HOST_BY_NAME = new Map(
|
|
48
|
+
Object.entries(PROVIDER_UPSTREAMS).map(([provider, entry]) => [entry.hostname, { provider, ...entry }])
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
// A hostname carrying any of these is not a hostname. Checked rather than
|
|
52
|
+
// assumed because the value arrives on a request line from whatever the client
|
|
53
|
+
// felt like sending.
|
|
54
|
+
const NOT_IN_A_HOSTNAME = /[\s/\\@?#]/;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Lower-case, unbracket, and drop the fully-qualified trailing dot.
|
|
58
|
+
*
|
|
59
|
+
* `api.anthropic.com.` resolves to exactly the same address as
|
|
60
|
+
* `api.anthropic.com`, so a string comparison that treats them as different
|
|
61
|
+
* hosts is a one-character bypass of the provider refusal below.
|
|
62
|
+
*/
|
|
63
|
+
function normalizeHostname(value) {
|
|
64
|
+
let host = String(value ?? "").trim().toLowerCase();
|
|
65
|
+
if (host.startsWith("[") && host.endsWith("]")) host = host.slice(1, -1);
|
|
66
|
+
if (host.endsWith(".")) host = host.slice(0, -1);
|
|
67
|
+
return host;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Exact match only — `evil-api.anthropic.com` is somebody else's hostname. */
|
|
71
|
+
export function providerForHost(hostname) {
|
|
72
|
+
return HOST_BY_NAME.get(normalizeHostname(hostname)) ?? null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function parsePort(value) {
|
|
76
|
+
if (!/^\d{1,5}$/.test(String(value))) return null;
|
|
77
|
+
const port = Number(value);
|
|
78
|
+
return port >= 1 && port <= 65535 ? port : null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Split a CONNECT authority-form target (`host:port`) as RFC 9110 defines it.
|
|
83
|
+
*
|
|
84
|
+
* The port is mandatory: a bare host is not a valid CONNECT target, and guessing
|
|
85
|
+
* 443 for it would mean inventing a destination the client never named.
|
|
86
|
+
*/
|
|
87
|
+
export function parseConnectTarget(raw) {
|
|
88
|
+
const value = String(raw ?? "").trim();
|
|
89
|
+
if (!value) return null;
|
|
90
|
+
|
|
91
|
+
let hostPart;
|
|
92
|
+
let portPart;
|
|
93
|
+
if (value.startsWith("[")) {
|
|
94
|
+
const close = value.indexOf("]");
|
|
95
|
+
if (close === -1 || value[close + 1] !== ":") return null;
|
|
96
|
+
hostPart = value.slice(0, close + 1);
|
|
97
|
+
portPart = value.slice(close + 2);
|
|
98
|
+
} else {
|
|
99
|
+
const colon = value.indexOf(":");
|
|
100
|
+
// Exactly one colon outside brackets. `a:1:2` is malformed, not an address.
|
|
101
|
+
if (colon === -1 || value.indexOf(":", colon + 1) !== -1) return null;
|
|
102
|
+
hostPart = value.slice(0, colon);
|
|
103
|
+
portPart = value.slice(colon + 1);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const hostname = normalizeHostname(hostPart);
|
|
107
|
+
const port = parsePort(portPart);
|
|
108
|
+
if (!hostname || port === null || NOT_IN_A_HOSTNAME.test(hostname)) return null;
|
|
109
|
+
return { hostname, port };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** The gateway's own host and effective port, which CONNECT is allowed to reach. */
|
|
113
|
+
function gatewayTarget(gatewayOrigin) {
|
|
114
|
+
let url;
|
|
115
|
+
try {
|
|
116
|
+
url = new URL(String(gatewayOrigin ?? ""));
|
|
117
|
+
} catch {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
const port = url.port ? parsePort(url.port) : url.protocol === "https:" ? 443 : 80;
|
|
121
|
+
if (port === null) return null;
|
|
122
|
+
return { hostname: normalizeHostname(url.hostname), port };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** `host` (any port) or `host:port` (that port only). */
|
|
126
|
+
function parseAllowEntry(entry) {
|
|
127
|
+
const value = String(entry ?? "").trim();
|
|
128
|
+
if (!value) return null;
|
|
129
|
+
const withPort = parseConnectTarget(value);
|
|
130
|
+
if (withPort) return withPort;
|
|
131
|
+
|
|
132
|
+
const hostname = normalizeHostname(value);
|
|
133
|
+
if (!hostname || NOT_IN_A_HOSTNAME.test(hostname) || hostname.includes(":")) return null;
|
|
134
|
+
return { hostname, port: null };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function parseAllowHosts(entries) {
|
|
138
|
+
const list = Array.isArray(entries)
|
|
139
|
+
? entries
|
|
140
|
+
: String(entries ?? "")
|
|
141
|
+
.split(",")
|
|
142
|
+
.filter(Boolean);
|
|
143
|
+
return list.map(parseAllowEntry).filter(Boolean);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function refuse(status, code, message, help) {
|
|
147
|
+
return { allow: false, status, code, message, help };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function governedPathHelp(provider, localBaseUrl) {
|
|
151
|
+
const base = localBaseUrl ? String(localBaseUrl).replace(/\/+$/, "") : "";
|
|
152
|
+
return `${base}/api/v1/${provider}`;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Decide what to do with `CONNECT host:port`.
|
|
157
|
+
*
|
|
158
|
+
* Order matters: the provider check runs FIRST, before the operator allowlist.
|
|
159
|
+
* An allowlist that could name a provider would stop being an egress control and
|
|
160
|
+
* become an off-switch for governance.
|
|
161
|
+
*/
|
|
162
|
+
export function classifyConnect({ target, gatewayOrigin, allowHosts = [], localBaseUrl = "" } = {}) {
|
|
163
|
+
const parsed = parseConnectTarget(target);
|
|
164
|
+
if (!parsed) {
|
|
165
|
+
return refuse(
|
|
166
|
+
400,
|
|
167
|
+
"bad_connect_target",
|
|
168
|
+
"CONNECT needs an authority-form target, for example api.example.com:443.",
|
|
169
|
+
"Check the proxy setting; PassControl received a target it could not parse."
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const provider = providerForHost(parsed.hostname);
|
|
174
|
+
if (provider) {
|
|
175
|
+
return refuse(
|
|
176
|
+
403,
|
|
177
|
+
"provider_tunnel_not_governed",
|
|
178
|
+
`PassControl will not tunnel TLS to ${parsed.hostname}. It cannot see inside that connection, so it could ` +
|
|
179
|
+
"neither inject the provider key nor apply scope, budget or the kill switch — the call would leave " +
|
|
180
|
+
"ungoverned while appearing to be governed.",
|
|
181
|
+
`Point the client's base URL at ${governedPathHelp(provider.provider, localBaseUrl)} instead. ` +
|
|
182
|
+
"PassControl does not install a TLS-intercepting CA."
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const gateway = gatewayTarget(gatewayOrigin);
|
|
187
|
+
if (gateway && parsed.hostname === gateway.hostname && parsed.port === gateway.port) {
|
|
188
|
+
return { allow: true, ...parsed, reason: "gateway" };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const allowed = parseAllowHosts(allowHosts).some(
|
|
192
|
+
(entry) => entry.hostname === parsed.hostname && (entry.port === null || entry.port === parsed.port)
|
|
193
|
+
);
|
|
194
|
+
if (allowed) return { allow: true, ...parsed, reason: "allowlist" };
|
|
195
|
+
|
|
196
|
+
return refuse(
|
|
197
|
+
403,
|
|
198
|
+
"host_not_allowed",
|
|
199
|
+
`PassControl is not an open proxy and will not tunnel to ${parsed.hostname}.`,
|
|
200
|
+
`Allow it explicitly with \`passcontrol sidecar --allow-connect ${parsed.hostname}\` if the agent needs it.`
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Decide what to do with a request line.
|
|
206
|
+
*
|
|
207
|
+
* Origin-form (`/api/v1/…`) is the sidecar's original mode and passes through.
|
|
208
|
+
* Absolute-form (`http://api.anthropic.com/v1/messages`) is what a proxy client
|
|
209
|
+
* sends for a plain-HTTP URL; it used to be concatenated onto the gateway origin
|
|
210
|
+
* verbatim, producing `https://gateway/http://api.anthropic.com/v1/messages`.
|
|
211
|
+
*/
|
|
212
|
+
export function classifyProxyRequest({ url, gatewayOrigin, localBaseUrl = "" } = {}) {
|
|
213
|
+
const target = String(url ?? "");
|
|
214
|
+
if (target.startsWith("/")) return { allow: true, path: target, reason: "origin-form" };
|
|
215
|
+
|
|
216
|
+
if (!/^https?:\/\//i.test(target)) {
|
|
217
|
+
return refuse(
|
|
218
|
+
400,
|
|
219
|
+
"bad_request_target",
|
|
220
|
+
"PassControl received a request target that is neither a path nor an absolute URL.",
|
|
221
|
+
"Point the client at the sidecar's base URL, or set HTTP_PROXY/HTTPS_PROXY to it."
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
let parsed;
|
|
226
|
+
try {
|
|
227
|
+
parsed = new URL(target);
|
|
228
|
+
} catch {
|
|
229
|
+
return refuse(400, "bad_request_target", "PassControl could not parse that request target.", "Check the client's base URL.");
|
|
230
|
+
}
|
|
231
|
+
// Never echoed anywhere below, and refused outright: a URL carrying userinfo
|
|
232
|
+
// is a credential in a request line, and the same rule already governs
|
|
233
|
+
// PASSCONTROL_GATEWAY in `bareGatewayOrigin`.
|
|
234
|
+
if (parsed.username || parsed.password) {
|
|
235
|
+
return refuse(
|
|
236
|
+
400,
|
|
237
|
+
"bad_request_target",
|
|
238
|
+
"PassControl refuses a request target with embedded credentials.",
|
|
239
|
+
"Remove the user:password@ prefix from the client's base URL."
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const hostname = normalizeHostname(parsed.hostname);
|
|
244
|
+
const provider = providerForHost(hostname);
|
|
245
|
+
if (provider) {
|
|
246
|
+
// The gateway re-adds the provider's base path from `upstreamBaseUrl`, so
|
|
247
|
+
// forwarding groq's `/openai` prefix as well would double it.
|
|
248
|
+
let rest = parsed.pathname;
|
|
249
|
+
if (provider.basePath && (rest === provider.basePath || rest.startsWith(`${provider.basePath}/`))) {
|
|
250
|
+
rest = rest.slice(provider.basePath.length);
|
|
251
|
+
}
|
|
252
|
+
if (!rest.startsWith("/")) rest = `/${rest}`;
|
|
253
|
+
return { allow: true, path: `/api/v1/${provider.provider}${rest}${parsed.search}`, reason: "provider" };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const gateway = gatewayTarget(gatewayOrigin);
|
|
257
|
+
const port = parsed.port ? parsePort(parsed.port) : parsed.protocol === "https:" ? 443 : 80;
|
|
258
|
+
if (gateway && hostname === gateway.hostname && port === gateway.port) {
|
|
259
|
+
return { allow: true, path: `${parsed.pathname}${parsed.search}`, reason: "gateway" };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return refuse(
|
|
263
|
+
403,
|
|
264
|
+
"host_not_allowed",
|
|
265
|
+
`PassControl is not an open proxy and will not forward to ${hostname}.`,
|
|
266
|
+
"Only the configured gateway and the supported providers are routed."
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Loopback bind addresses.
|
|
272
|
+
*
|
|
273
|
+
* The sidecar holds the passport and mints visas on demand, so a listener
|
|
274
|
+
* reachable off-host is a credential-issuing endpoint for anyone who can route
|
|
275
|
+
* to it. `127.0.0.0/8` in full, because `127.0.0.2` is as loopback as `127.0.0.1`.
|
|
276
|
+
*/
|
|
277
|
+
export function isLoopbackBindHost(host) {
|
|
278
|
+
const value = normalizeHostname(host);
|
|
279
|
+
if (!value) return false;
|
|
280
|
+
return value === "localhost" || value === "::1" || /^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(value);
|
|
281
|
+
}
|