dsh-coding-subscription-oauth 0.6.2 → 0.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +222 -206
- package/CONTRIBUTING.md +129 -120
- package/INSTALL.md +218 -218
- package/LICENSE +19 -19
- package/NOTICE +11 -11
- package/README.de.md +289 -289
- package/README.es.md +290 -290
- package/README.fr.md +290 -290
- package/README.ja.md +290 -290
- package/README.ko.md +290 -290
- package/README.md +304 -304
- package/README.pt-BR.md +290 -290
- package/README.ru.md +290 -290
- package/README.zh-CN.md +302 -302
- package/compatibility/dsh-bom.json +0 -0
- package/cordis.patch.yml +13 -13
- package/docs/00-project-rules.md +212 -195
- package/docs/02-architecture.md +130 -130
- package/docs/02-architecture.zh-CN.md +130 -130
- package/lib/bin.js.map +1 -1
- package/lib/capability-settings.d.ts +8 -2
- package/lib/capability-settings.d.ts.map +1 -1
- package/lib/capability-tools.d.ts +7 -2
- package/lib/capability-tools.d.ts.map +1 -1
- package/lib/client.js +3 -3
- package/lib/client.js.map +2 -2
- package/lib/codex-images.d.ts +8 -0
- package/lib/codex-images.d.ts.map +1 -1
- package/lib/index.js +32 -7
- package/lib/index.js.map +2 -2
- package/lib/invariant.js.map +1 -1
- package/media/en/settings_accounts.png +0 -0
- package/media/en/settings_capabilities.png +0 -0
- package/media/en/settings_gateway.png +0 -0
- package/media/settings_accounts.png +0 -0
- package/media/settings_capabilities.png +0 -0
- package/media/settings_gateway.png +0 -0
- package/media/settings_overview.png +0 -0
- package/media/zh-CN/settings_accounts.png +0 -0
- package/media/zh-CN/settings_capabilities.png +0 -0
- package/media/zh-CN/settings_gateway.png +0 -0
- package/package.json +148 -148
- package/patches/dsh-agy@0.1.2.patch +25 -25
- package/scripts/release.mjs +186 -186
- package/scripts/smoke-deployed-routes.mjs +146 -146
- package/scripts/verify-deployed-catalog.mjs +87 -87
- package/src/adapter.ts +273 -273
- package/src/alias-adapter.ts +130 -130
- package/src/auth-routes.ts +921 -921
- package/src/auth.ts +67 -67
- package/src/bin.ts +350 -350
- package/src/capability-routes.ts +279 -279
- package/src/capability-runtime.ts +314 -313
- package/src/capability-settings.ts +671 -658
- package/src/capability-tools.ts +685 -666
- package/src/catalog.ts +271 -271
- package/src/client/GrokBuildSettings.tsx +771 -770
- package/src/client/api.ts +88 -88
- package/src/client/components/AboutTab.tsx +30 -30
- package/src/client/components/AccountsTab.tsx +241 -241
- package/src/client/components/Badge.tsx +33 -33
- package/src/client/components/CapabilitiesTab.tsx +265 -265
- package/src/client/components/CliPullPreview.tsx +116 -116
- package/src/client/components/CopyButton.tsx +57 -57
- package/src/client/components/GatewayTab.tsx +469 -469
- package/src/client/components/NoticeBanner.tsx +46 -46
- package/src/client/components/ProgressBar.tsx +53 -53
- package/src/client/components/ProviderCard.tsx +606 -606
- package/src/client/components/SettingsTabs.tsx +75 -75
- package/src/client/components/ToggleSwitch.tsx +71 -71
- package/src/client/constants.ts +230 -224
- package/src/client/display.ts +61 -61
- package/src/client/dshClientAdapter.ts +127 -127
- package/src/client/gatewaySnippets.ts +37 -37
- package/src/client/index.tsx +156 -156
- package/src/client/locales.ts +540 -535
- package/src/client/microStyles.ts +52 -52
- package/src/client/parsers.ts +398 -396
- package/src/client/styles.ts +325 -325
- package/src/client/types.ts +199 -197
- package/src/codex-http.ts +447 -447
- package/src/codex-images.ts +503 -485
- package/src/codex-model-capabilities.ts +320 -320
- package/src/codex-search.ts +245 -245
- package/src/codex-usage.ts +263 -263
- package/src/compatibility.ts +41 -41
- package/src/dsh-host-adapter.ts +173 -173
- package/src/gateway-anthropic-messages.ts +84 -84
- package/src/gateway-auth.ts +102 -102
- package/src/gateway-backend.ts +274 -274
- package/src/gateway-body.ts +49 -49
- package/src/gateway-config.ts +76 -76
- package/src/gateway-http.ts +104 -104
- package/src/gateway-openai-chat.ts +124 -124
- package/src/gateway-openai-responses.ts +53 -53
- package/src/gateway-parse.ts +224 -224
- package/src/gateway-protocol.ts +52 -52
- package/src/gateway-routes.ts +158 -158
- package/src/gateway.ts +258 -258
- package/src/grok-errors.ts +24 -24
- package/src/grok-imagine.ts +1627 -1627
- package/src/grok-import.ts +151 -151
- package/src/http-json.ts +82 -82
- package/src/ids.ts +59 -59
- package/src/imagine-routes.ts +463 -463
- package/src/index.ts +709 -709
- package/src/invariant.ts +17 -17
- package/src/kimi-errors.ts +26 -26
- package/src/media-store.ts +927 -927
- package/src/oauth-import-routes.ts +324 -324
- package/src/oauth-providers.ts +152 -152
- package/src/oauth-session.ts +183 -183
- package/src/oauth-sources.ts +1104 -1104
- package/src/oauth.ts +620 -620
- package/src/provider.ts +128 -128
- package/src/proxy.ts +11 -11
- package/src/redact.ts +72 -72
- package/src/session.ts +218 -218
- package/src/store.ts +217 -217
- package/src/web-origin.ts +296 -296
- package/src/web-routes.ts +38 -38
package/src/web-origin.ts
CHANGED
|
@@ -1,296 +1,296 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Owner-request authorization shared by private plugin Web routes.
|
|
3
|
-
* @module dsh-coding-subscription-oauth/web-origin
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { timingSafeEqual } from "node:crypto";
|
|
7
|
-
import type { IncomingMessage } from "node:http";
|
|
8
|
-
import type {
|
|
9
|
-
OwnerRequestPolicy as CoreOwnerRequestPolicy,
|
|
10
|
-
OwnerAccessMode,
|
|
11
|
-
OwnerRequestDecision,
|
|
12
|
-
} from "dsh-coding-oauth-core";
|
|
13
|
-
import type { DshCompatibilityDiagnostic } from "./compatibility.ts";
|
|
14
|
-
|
|
15
|
-
export const OWNER_PROOF_HEADER = "x-dsh-owner-proof";
|
|
16
|
-
export const OWNER_CSRF_HEADER = "x-dsh-csrf-token";
|
|
17
|
-
|
|
18
|
-
export type { OwnerAccessMode } from "dsh-coding-oauth-core";
|
|
19
|
-
|
|
20
|
-
export interface TrustedReverseProxyPolicyConfig {
|
|
21
|
-
/** Exact TCP peers allowed to inject owner-only headers. Forwarded peers never count. */
|
|
22
|
-
readonly peers?: readonly string[];
|
|
23
|
-
/** Exact browser origins allowed through the trusted proxy. */
|
|
24
|
-
readonly origins?: readonly string[];
|
|
25
|
-
/** Secret injected by the trusted proxy only after owner authentication. */
|
|
26
|
-
readonly ownerProof?: string;
|
|
27
|
-
/** Independent CSRF secret injected by the trusted proxy. */
|
|
28
|
-
readonly csrfToken?: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface OwnerRequestPolicyConfig {
|
|
32
|
-
/** Use when loopback is intentionally reached through an SSH port forward. */
|
|
33
|
-
readonly loopbackAccessMode?: "loopback" | "ssh-tunnel";
|
|
34
|
-
readonly trustedProxy?: TrustedReverseProxyPolicyConfig;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export type OwnerRequestPolicy = CoreOwnerRequestPolicy<IncomingMessage, DshCompatibilityDiagnostic>;
|
|
38
|
-
|
|
39
|
-
interface TrustedProxyPolicy {
|
|
40
|
-
readonly peers: ReadonlySet<string>;
|
|
41
|
-
readonly origins: ReadonlySet<string>;
|
|
42
|
-
readonly ownerProof: string;
|
|
43
|
-
readonly csrfToken: string;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const OWNER_ACCESS_MODES = new Set<OwnerAccessMode>(["loopback", "ssh-tunnel", "trusted-https-proxy"]);
|
|
47
|
-
|
|
48
|
-
/** Adapt a host-owned policy so service churn cannot escape through Web routes. */
|
|
49
|
-
export function safeguardOwnerRequestPolicy(policy: OwnerRequestPolicy): OwnerRequestPolicy {
|
|
50
|
-
return Object.freeze({
|
|
51
|
-
authorize(req: IncomingMessage): OwnerRequestDecision {
|
|
52
|
-
try {
|
|
53
|
-
const decision = policy.authorize(req);
|
|
54
|
-
if (decision?.authorized === false) {
|
|
55
|
-
return { authorized: false, reason: typeof decision.reason === "string" ? decision.reason : "denied" };
|
|
56
|
-
}
|
|
57
|
-
if (
|
|
58
|
-
decision?.authorized === true &&
|
|
59
|
-
typeof decision.accessMode === "string" &&
|
|
60
|
-
OWNER_ACCESS_MODES.has(decision.accessMode as OwnerAccessMode)
|
|
61
|
-
) {
|
|
62
|
-
return { authorized: true, accessMode: decision.accessMode as OwnerAccessMode };
|
|
63
|
-
}
|
|
64
|
-
return { authorized: false, reason: "invalid-owner-policy-decision" };
|
|
65
|
-
} catch {
|
|
66
|
-
return { authorized: false, reason: "owner-policy-error" };
|
|
67
|
-
}
|
|
68
|
-
},
|
|
69
|
-
diagnostics(): readonly DshCompatibilityDiagnostic[] {
|
|
70
|
-
try {
|
|
71
|
-
const diagnostics = policy.diagnostics();
|
|
72
|
-
if (!Array.isArray(diagnostics) || !diagnostics.every(isOwnerRequestDiagnostic)) {
|
|
73
|
-
return [ownerPolicyDiagnosticError("DSH owner-request policy returned invalid diagnostics")];
|
|
74
|
-
}
|
|
75
|
-
return Object.freeze([...diagnostics]);
|
|
76
|
-
} catch {
|
|
77
|
-
return [ownerPolicyDiagnosticError("DSH owner-request policy diagnostics failed")];
|
|
78
|
-
}
|
|
79
|
-
},
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function isOwnerRequestDiagnostic(value: unknown): value is DshCompatibilityDiagnostic {
|
|
84
|
-
if (typeof value !== "object" || value === null) return false;
|
|
85
|
-
const candidate = value as Partial<DshCompatibilityDiagnostic>;
|
|
86
|
-
return (
|
|
87
|
-
typeof candidate.id === "string" &&
|
|
88
|
-
(candidate.level === "info" || candidate.level === "warning" || candidate.level === "error") &&
|
|
89
|
-
typeof candidate.message === "string"
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function ownerPolicyDiagnosticError(message: string): DshCompatibilityDiagnostic {
|
|
94
|
-
return { id: "owner-request.dsh-policy-invalid", component: "owner-request", level: "error", message };
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/** Build an immutable owner-request policy. Partial proxy configuration is deliberately unusable. */
|
|
98
|
-
export function createOwnerRequestPolicy(config: OwnerRequestPolicyConfig = {}): OwnerRequestPolicy {
|
|
99
|
-
const loopbackAccessMode = config.loopbackAccessMode ?? "loopback";
|
|
100
|
-
const configured = config.trustedProxy !== undefined;
|
|
101
|
-
const trustedProxy = parseTrustedProxyPolicy(config.trustedProxy);
|
|
102
|
-
const diagnostics: DshCompatibilityDiagnostic[] = [
|
|
103
|
-
{
|
|
104
|
-
id: "owner-request.loopback",
|
|
105
|
-
component: "owner-request",
|
|
106
|
-
level: "info",
|
|
107
|
-
message: "loopback owner requests require a loopback TCP peer and loopback Host/origin",
|
|
108
|
-
},
|
|
109
|
-
];
|
|
110
|
-
if (configured && trustedProxy === undefined) {
|
|
111
|
-
diagnostics.push({
|
|
112
|
-
id: "owner-request.trusted-proxy-incomplete",
|
|
113
|
-
component: "owner-request",
|
|
114
|
-
level: "error",
|
|
115
|
-
message:
|
|
116
|
-
"trusted reverse proxy access is disabled because peers, origins, ownerProof, and csrfToken are not all valid",
|
|
117
|
-
});
|
|
118
|
-
} else if (trustedProxy !== undefined) {
|
|
119
|
-
diagnostics.push({
|
|
120
|
-
id: "owner-request.trusted-proxy",
|
|
121
|
-
component: "owner-request",
|
|
122
|
-
level: "info",
|
|
123
|
-
message: "trusted reverse proxy access requires peer, origin, owner proof, fetch metadata, and CSRF proof",
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
const frozenDiagnostics = Object.freeze([...diagnostics]);
|
|
127
|
-
|
|
128
|
-
return Object.freeze({
|
|
129
|
-
authorize(req: IncomingMessage): OwnerRequestDecision {
|
|
130
|
-
const peer = normalizePeer(req.socket.remoteAddress);
|
|
131
|
-
// A configured proxy peer is always evaluated as proxy traffic. This
|
|
132
|
-
// prevents a same-host reverse proxy from bypassing owner proof when it
|
|
133
|
-
// rewrites Host to a loopback authority.
|
|
134
|
-
if (trustedProxy !== undefined && peer !== undefined && trustedProxy.peers.has(peer)) {
|
|
135
|
-
return authorizeTrustedProxy(req, trustedProxy);
|
|
136
|
-
}
|
|
137
|
-
const loopback = authorizeLoopback(req, loopbackAccessMode);
|
|
138
|
-
if (loopback.authorized) return loopback;
|
|
139
|
-
if (trustedProxy === undefined) {
|
|
140
|
-
return configured ? { authorized: false, reason: "incomplete-policy" } : loopback;
|
|
141
|
-
}
|
|
142
|
-
return authorizeTrustedProxy(req, trustedProxy);
|
|
143
|
-
},
|
|
144
|
-
diagnostics: () => frozenDiagnostics,
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
export const LOOPBACK_OWNER_REQUEST_POLICY = createOwnerRequestPolicy();
|
|
149
|
-
|
|
150
|
-
/** Backward-compatible loopback predicate for external callers. */
|
|
151
|
-
export function isTrustedLoopbackWebRequest(req: IncomingMessage): boolean {
|
|
152
|
-
return authorizeLoopback(req, "loopback").authorized;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
function authorizeLoopback(req: IncomingMessage, accessMode: OwnerAccessMode): OwnerRequestDecision {
|
|
156
|
-
if (!isLoopbackPeer(req.socket.remoteAddress)) return { authorized: false, reason: "peer" };
|
|
157
|
-
if (singleHeader(req, "sec-fetch-site") === "cross-site") {
|
|
158
|
-
return { authorized: false, reason: "fetch-metadata" };
|
|
159
|
-
}
|
|
160
|
-
const host = parseLoopbackHost(singleHeader(req, "host"));
|
|
161
|
-
if (host === undefined) return { authorized: false, reason: "host" };
|
|
162
|
-
const origin = singleHeader(req, "origin");
|
|
163
|
-
if (origin === undefined) return { authorized: true, accessMode };
|
|
164
|
-
try {
|
|
165
|
-
const parsed = new URL(origin);
|
|
166
|
-
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return { authorized: false, reason: "origin" };
|
|
167
|
-
if (!isLoopbackHostname(parsed.hostname)) return { authorized: false, reason: "origin" };
|
|
168
|
-
return parsed.host.toLowerCase() === host
|
|
169
|
-
? { authorized: true, accessMode }
|
|
170
|
-
: { authorized: false, reason: "origin" };
|
|
171
|
-
} catch {
|
|
172
|
-
return { authorized: false, reason: "origin" };
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function authorizeTrustedProxy(req: IncomingMessage, policy: TrustedProxyPolicy): OwnerRequestDecision {
|
|
177
|
-
const peer = normalizePeer(req.socket.remoteAddress);
|
|
178
|
-
if (peer === undefined || !policy.peers.has(peer)) return { authorized: false, reason: "peer" };
|
|
179
|
-
const origin = normalizeHttpsOrigin(singleHeader(req, "origin"));
|
|
180
|
-
if (origin === undefined || !policy.origins.has(origin)) return { authorized: false, reason: "origin" };
|
|
181
|
-
const host = normalizeAuthority(singleHeader(req, "host"));
|
|
182
|
-
if (host === undefined || host !== new URL(origin).host.toLowerCase()) return { authorized: false, reason: "host" };
|
|
183
|
-
// Missing Fetch Metadata is not accepted on the remote path.
|
|
184
|
-
if (singleHeader(req, "sec-fetch-site") !== "same-origin") {
|
|
185
|
-
return { authorized: false, reason: "fetch-metadata" };
|
|
186
|
-
}
|
|
187
|
-
if (!secretMatches(singleHeader(req, OWNER_PROOF_HEADER), policy.ownerProof)) {
|
|
188
|
-
return { authorized: false, reason: "owner-proof" };
|
|
189
|
-
}
|
|
190
|
-
if (isMutation(req.method) && !secretMatches(singleHeader(req, OWNER_CSRF_HEADER), policy.csrfToken)) {
|
|
191
|
-
return { authorized: false, reason: "csrf" };
|
|
192
|
-
}
|
|
193
|
-
return { authorized: true, accessMode: "trusted-https-proxy" };
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
function parseTrustedProxyPolicy(config: TrustedReverseProxyPolicyConfig | undefined): TrustedProxyPolicy | undefined {
|
|
197
|
-
if (config === undefined) return undefined;
|
|
198
|
-
const peers = new Set(
|
|
199
|
-
(config.peers ?? []).map(normalizePeer).filter((value): value is string => value !== undefined),
|
|
200
|
-
);
|
|
201
|
-
const origins = new Set(
|
|
202
|
-
(config.origins ?? []).map(normalizeHttpsOrigin).filter((value): value is string => value !== undefined),
|
|
203
|
-
);
|
|
204
|
-
if (peers.size === 0 || origins.size === 0) return undefined;
|
|
205
|
-
if (!nonEmptySecret(config.ownerProof) || !nonEmptySecret(config.csrfToken)) return undefined;
|
|
206
|
-
if (config.ownerProof === config.csrfToken) return undefined;
|
|
207
|
-
return { peers, origins, ownerProof: config.ownerProof, csrfToken: config.csrfToken };
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
function singleHeader(req: IncomingMessage, name: string): string | undefined {
|
|
211
|
-
const value = req.headers[name];
|
|
212
|
-
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
function nonEmptySecret(value: string | undefined): value is string {
|
|
216
|
-
return typeof value === "string" && value.length > 0;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
function secretMatches(received: string | undefined, expected: string): boolean {
|
|
220
|
-
if (received === undefined) return false;
|
|
221
|
-
const actualBytes = Buffer.from(received);
|
|
222
|
-
const expectedBytes = Buffer.from(expected);
|
|
223
|
-
return actualBytes.byteLength === expectedBytes.byteLength && timingSafeEqual(actualBytes, expectedBytes);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
function normalizeHttpsOrigin(value: string | undefined): string | undefined {
|
|
227
|
-
if (value === undefined) return undefined;
|
|
228
|
-
try {
|
|
229
|
-
const parsed = new URL(value);
|
|
230
|
-
if (parsed.protocol !== "https:") return undefined;
|
|
231
|
-
if (
|
|
232
|
-
parsed.username !== "" ||
|
|
233
|
-
parsed.password !== "" ||
|
|
234
|
-
parsed.pathname !== "/" ||
|
|
235
|
-
parsed.search !== "" ||
|
|
236
|
-
parsed.hash !== ""
|
|
237
|
-
) {
|
|
238
|
-
return undefined;
|
|
239
|
-
}
|
|
240
|
-
return parsed.origin.toLowerCase();
|
|
241
|
-
} catch {
|
|
242
|
-
return undefined;
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
function normalizeAuthority(value: string | undefined): string | undefined {
|
|
247
|
-
if (value === undefined || value.includes("/") || value.includes("@")) return undefined;
|
|
248
|
-
try {
|
|
249
|
-
return new URL(`https://${value}`).host.toLowerCase();
|
|
250
|
-
} catch {
|
|
251
|
-
return undefined;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
function parseLoopbackHost(value: string | undefined): string | undefined {
|
|
256
|
-
if (value === undefined) return undefined;
|
|
257
|
-
try {
|
|
258
|
-
const parsed = new URL(`http://${value}`);
|
|
259
|
-
if (parsed.username !== "" || parsed.password !== "" || parsed.pathname !== "/" || parsed.search !== "") {
|
|
260
|
-
return undefined;
|
|
261
|
-
}
|
|
262
|
-
if (!isLoopbackHostname(parsed.hostname)) return undefined;
|
|
263
|
-
return parsed.host.toLowerCase();
|
|
264
|
-
} catch {
|
|
265
|
-
return undefined;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
function normalizePeer(value: string | undefined): string | undefined {
|
|
270
|
-
if (value === undefined) return undefined;
|
|
271
|
-
const normalized = value.trim().toLowerCase();
|
|
272
|
-
if (normalized.length === 0) return undefined;
|
|
273
|
-
return normalized.startsWith("::ffff:") ? normalized.slice("::ffff:".length) : normalized;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
function isLoopbackPeer(value: string | undefined): boolean {
|
|
277
|
-
const normalized = normalizePeer(value);
|
|
278
|
-
if (normalized === undefined) return false;
|
|
279
|
-
return normalized === "::1" || isIpv4Loopback(normalized);
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
function isLoopbackHostname(value: string): boolean {
|
|
283
|
-
const normalized = value.toLowerCase();
|
|
284
|
-
if (normalized === "localhost" || normalized === "[::1]") return true;
|
|
285
|
-
return isIpv4Loopback(normalized);
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
function isIpv4Loopback(value: string): boolean {
|
|
289
|
-
const octets = value.split(".");
|
|
290
|
-
if (octets.length !== 4 || octets[0] !== "127") return false;
|
|
291
|
-
return octets.every((octet) => /^(?:0|[1-9][0-9]{0,2})$/u.test(octet) && Number(octet) <= 255);
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
function isMutation(method: string | undefined): boolean {
|
|
295
|
-
return method !== "GET" && method !== "HEAD" && method !== "OPTIONS";
|
|
296
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Owner-request authorization shared by private plugin Web routes.
|
|
3
|
+
* @module dsh-coding-subscription-oauth/web-origin
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { timingSafeEqual } from "node:crypto";
|
|
7
|
+
import type { IncomingMessage } from "node:http";
|
|
8
|
+
import type {
|
|
9
|
+
OwnerRequestPolicy as CoreOwnerRequestPolicy,
|
|
10
|
+
OwnerAccessMode,
|
|
11
|
+
OwnerRequestDecision,
|
|
12
|
+
} from "dsh-coding-oauth-core";
|
|
13
|
+
import type { DshCompatibilityDiagnostic } from "./compatibility.ts";
|
|
14
|
+
|
|
15
|
+
export const OWNER_PROOF_HEADER = "x-dsh-owner-proof";
|
|
16
|
+
export const OWNER_CSRF_HEADER = "x-dsh-csrf-token";
|
|
17
|
+
|
|
18
|
+
export type { OwnerAccessMode } from "dsh-coding-oauth-core";
|
|
19
|
+
|
|
20
|
+
export interface TrustedReverseProxyPolicyConfig {
|
|
21
|
+
/** Exact TCP peers allowed to inject owner-only headers. Forwarded peers never count. */
|
|
22
|
+
readonly peers?: readonly string[];
|
|
23
|
+
/** Exact browser origins allowed through the trusted proxy. */
|
|
24
|
+
readonly origins?: readonly string[];
|
|
25
|
+
/** Secret injected by the trusted proxy only after owner authentication. */
|
|
26
|
+
readonly ownerProof?: string;
|
|
27
|
+
/** Independent CSRF secret injected by the trusted proxy. */
|
|
28
|
+
readonly csrfToken?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface OwnerRequestPolicyConfig {
|
|
32
|
+
/** Use when loopback is intentionally reached through an SSH port forward. */
|
|
33
|
+
readonly loopbackAccessMode?: "loopback" | "ssh-tunnel";
|
|
34
|
+
readonly trustedProxy?: TrustedReverseProxyPolicyConfig;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type OwnerRequestPolicy = CoreOwnerRequestPolicy<IncomingMessage, DshCompatibilityDiagnostic>;
|
|
38
|
+
|
|
39
|
+
interface TrustedProxyPolicy {
|
|
40
|
+
readonly peers: ReadonlySet<string>;
|
|
41
|
+
readonly origins: ReadonlySet<string>;
|
|
42
|
+
readonly ownerProof: string;
|
|
43
|
+
readonly csrfToken: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const OWNER_ACCESS_MODES = new Set<OwnerAccessMode>(["loopback", "ssh-tunnel", "trusted-https-proxy"]);
|
|
47
|
+
|
|
48
|
+
/** Adapt a host-owned policy so service churn cannot escape through Web routes. */
|
|
49
|
+
export function safeguardOwnerRequestPolicy(policy: OwnerRequestPolicy): OwnerRequestPolicy {
|
|
50
|
+
return Object.freeze({
|
|
51
|
+
authorize(req: IncomingMessage): OwnerRequestDecision {
|
|
52
|
+
try {
|
|
53
|
+
const decision = policy.authorize(req);
|
|
54
|
+
if (decision?.authorized === false) {
|
|
55
|
+
return { authorized: false, reason: typeof decision.reason === "string" ? decision.reason : "denied" };
|
|
56
|
+
}
|
|
57
|
+
if (
|
|
58
|
+
decision?.authorized === true &&
|
|
59
|
+
typeof decision.accessMode === "string" &&
|
|
60
|
+
OWNER_ACCESS_MODES.has(decision.accessMode as OwnerAccessMode)
|
|
61
|
+
) {
|
|
62
|
+
return { authorized: true, accessMode: decision.accessMode as OwnerAccessMode };
|
|
63
|
+
}
|
|
64
|
+
return { authorized: false, reason: "invalid-owner-policy-decision" };
|
|
65
|
+
} catch {
|
|
66
|
+
return { authorized: false, reason: "owner-policy-error" };
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
diagnostics(): readonly DshCompatibilityDiagnostic[] {
|
|
70
|
+
try {
|
|
71
|
+
const diagnostics = policy.diagnostics();
|
|
72
|
+
if (!Array.isArray(diagnostics) || !diagnostics.every(isOwnerRequestDiagnostic)) {
|
|
73
|
+
return [ownerPolicyDiagnosticError("DSH owner-request policy returned invalid diagnostics")];
|
|
74
|
+
}
|
|
75
|
+
return Object.freeze([...diagnostics]);
|
|
76
|
+
} catch {
|
|
77
|
+
return [ownerPolicyDiagnosticError("DSH owner-request policy diagnostics failed")];
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function isOwnerRequestDiagnostic(value: unknown): value is DshCompatibilityDiagnostic {
|
|
84
|
+
if (typeof value !== "object" || value === null) return false;
|
|
85
|
+
const candidate = value as Partial<DshCompatibilityDiagnostic>;
|
|
86
|
+
return (
|
|
87
|
+
typeof candidate.id === "string" &&
|
|
88
|
+
(candidate.level === "info" || candidate.level === "warning" || candidate.level === "error") &&
|
|
89
|
+
typeof candidate.message === "string"
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function ownerPolicyDiagnosticError(message: string): DshCompatibilityDiagnostic {
|
|
94
|
+
return { id: "owner-request.dsh-policy-invalid", component: "owner-request", level: "error", message };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Build an immutable owner-request policy. Partial proxy configuration is deliberately unusable. */
|
|
98
|
+
export function createOwnerRequestPolicy(config: OwnerRequestPolicyConfig = {}): OwnerRequestPolicy {
|
|
99
|
+
const loopbackAccessMode = config.loopbackAccessMode ?? "loopback";
|
|
100
|
+
const configured = config.trustedProxy !== undefined;
|
|
101
|
+
const trustedProxy = parseTrustedProxyPolicy(config.trustedProxy);
|
|
102
|
+
const diagnostics: DshCompatibilityDiagnostic[] = [
|
|
103
|
+
{
|
|
104
|
+
id: "owner-request.loopback",
|
|
105
|
+
component: "owner-request",
|
|
106
|
+
level: "info",
|
|
107
|
+
message: "loopback owner requests require a loopback TCP peer and loopback Host/origin",
|
|
108
|
+
},
|
|
109
|
+
];
|
|
110
|
+
if (configured && trustedProxy === undefined) {
|
|
111
|
+
diagnostics.push({
|
|
112
|
+
id: "owner-request.trusted-proxy-incomplete",
|
|
113
|
+
component: "owner-request",
|
|
114
|
+
level: "error",
|
|
115
|
+
message:
|
|
116
|
+
"trusted reverse proxy access is disabled because peers, origins, ownerProof, and csrfToken are not all valid",
|
|
117
|
+
});
|
|
118
|
+
} else if (trustedProxy !== undefined) {
|
|
119
|
+
diagnostics.push({
|
|
120
|
+
id: "owner-request.trusted-proxy",
|
|
121
|
+
component: "owner-request",
|
|
122
|
+
level: "info",
|
|
123
|
+
message: "trusted reverse proxy access requires peer, origin, owner proof, fetch metadata, and CSRF proof",
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
const frozenDiagnostics = Object.freeze([...diagnostics]);
|
|
127
|
+
|
|
128
|
+
return Object.freeze({
|
|
129
|
+
authorize(req: IncomingMessage): OwnerRequestDecision {
|
|
130
|
+
const peer = normalizePeer(req.socket.remoteAddress);
|
|
131
|
+
// A configured proxy peer is always evaluated as proxy traffic. This
|
|
132
|
+
// prevents a same-host reverse proxy from bypassing owner proof when it
|
|
133
|
+
// rewrites Host to a loopback authority.
|
|
134
|
+
if (trustedProxy !== undefined && peer !== undefined && trustedProxy.peers.has(peer)) {
|
|
135
|
+
return authorizeTrustedProxy(req, trustedProxy);
|
|
136
|
+
}
|
|
137
|
+
const loopback = authorizeLoopback(req, loopbackAccessMode);
|
|
138
|
+
if (loopback.authorized) return loopback;
|
|
139
|
+
if (trustedProxy === undefined) {
|
|
140
|
+
return configured ? { authorized: false, reason: "incomplete-policy" } : loopback;
|
|
141
|
+
}
|
|
142
|
+
return authorizeTrustedProxy(req, trustedProxy);
|
|
143
|
+
},
|
|
144
|
+
diagnostics: () => frozenDiagnostics,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export const LOOPBACK_OWNER_REQUEST_POLICY = createOwnerRequestPolicy();
|
|
149
|
+
|
|
150
|
+
/** Backward-compatible loopback predicate for external callers. */
|
|
151
|
+
export function isTrustedLoopbackWebRequest(req: IncomingMessage): boolean {
|
|
152
|
+
return authorizeLoopback(req, "loopback").authorized;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function authorizeLoopback(req: IncomingMessage, accessMode: OwnerAccessMode): OwnerRequestDecision {
|
|
156
|
+
if (!isLoopbackPeer(req.socket.remoteAddress)) return { authorized: false, reason: "peer" };
|
|
157
|
+
if (singleHeader(req, "sec-fetch-site") === "cross-site") {
|
|
158
|
+
return { authorized: false, reason: "fetch-metadata" };
|
|
159
|
+
}
|
|
160
|
+
const host = parseLoopbackHost(singleHeader(req, "host"));
|
|
161
|
+
if (host === undefined) return { authorized: false, reason: "host" };
|
|
162
|
+
const origin = singleHeader(req, "origin");
|
|
163
|
+
if (origin === undefined) return { authorized: true, accessMode };
|
|
164
|
+
try {
|
|
165
|
+
const parsed = new URL(origin);
|
|
166
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return { authorized: false, reason: "origin" };
|
|
167
|
+
if (!isLoopbackHostname(parsed.hostname)) return { authorized: false, reason: "origin" };
|
|
168
|
+
return parsed.host.toLowerCase() === host
|
|
169
|
+
? { authorized: true, accessMode }
|
|
170
|
+
: { authorized: false, reason: "origin" };
|
|
171
|
+
} catch {
|
|
172
|
+
return { authorized: false, reason: "origin" };
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function authorizeTrustedProxy(req: IncomingMessage, policy: TrustedProxyPolicy): OwnerRequestDecision {
|
|
177
|
+
const peer = normalizePeer(req.socket.remoteAddress);
|
|
178
|
+
if (peer === undefined || !policy.peers.has(peer)) return { authorized: false, reason: "peer" };
|
|
179
|
+
const origin = normalizeHttpsOrigin(singleHeader(req, "origin"));
|
|
180
|
+
if (origin === undefined || !policy.origins.has(origin)) return { authorized: false, reason: "origin" };
|
|
181
|
+
const host = normalizeAuthority(singleHeader(req, "host"));
|
|
182
|
+
if (host === undefined || host !== new URL(origin).host.toLowerCase()) return { authorized: false, reason: "host" };
|
|
183
|
+
// Missing Fetch Metadata is not accepted on the remote path.
|
|
184
|
+
if (singleHeader(req, "sec-fetch-site") !== "same-origin") {
|
|
185
|
+
return { authorized: false, reason: "fetch-metadata" };
|
|
186
|
+
}
|
|
187
|
+
if (!secretMatches(singleHeader(req, OWNER_PROOF_HEADER), policy.ownerProof)) {
|
|
188
|
+
return { authorized: false, reason: "owner-proof" };
|
|
189
|
+
}
|
|
190
|
+
if (isMutation(req.method) && !secretMatches(singleHeader(req, OWNER_CSRF_HEADER), policy.csrfToken)) {
|
|
191
|
+
return { authorized: false, reason: "csrf" };
|
|
192
|
+
}
|
|
193
|
+
return { authorized: true, accessMode: "trusted-https-proxy" };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function parseTrustedProxyPolicy(config: TrustedReverseProxyPolicyConfig | undefined): TrustedProxyPolicy | undefined {
|
|
197
|
+
if (config === undefined) return undefined;
|
|
198
|
+
const peers = new Set(
|
|
199
|
+
(config.peers ?? []).map(normalizePeer).filter((value): value is string => value !== undefined),
|
|
200
|
+
);
|
|
201
|
+
const origins = new Set(
|
|
202
|
+
(config.origins ?? []).map(normalizeHttpsOrigin).filter((value): value is string => value !== undefined),
|
|
203
|
+
);
|
|
204
|
+
if (peers.size === 0 || origins.size === 0) return undefined;
|
|
205
|
+
if (!nonEmptySecret(config.ownerProof) || !nonEmptySecret(config.csrfToken)) return undefined;
|
|
206
|
+
if (config.ownerProof === config.csrfToken) return undefined;
|
|
207
|
+
return { peers, origins, ownerProof: config.ownerProof, csrfToken: config.csrfToken };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function singleHeader(req: IncomingMessage, name: string): string | undefined {
|
|
211
|
+
const value = req.headers[name];
|
|
212
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function nonEmptySecret(value: string | undefined): value is string {
|
|
216
|
+
return typeof value === "string" && value.length > 0;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function secretMatches(received: string | undefined, expected: string): boolean {
|
|
220
|
+
if (received === undefined) return false;
|
|
221
|
+
const actualBytes = Buffer.from(received);
|
|
222
|
+
const expectedBytes = Buffer.from(expected);
|
|
223
|
+
return actualBytes.byteLength === expectedBytes.byteLength && timingSafeEqual(actualBytes, expectedBytes);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function normalizeHttpsOrigin(value: string | undefined): string | undefined {
|
|
227
|
+
if (value === undefined) return undefined;
|
|
228
|
+
try {
|
|
229
|
+
const parsed = new URL(value);
|
|
230
|
+
if (parsed.protocol !== "https:") return undefined;
|
|
231
|
+
if (
|
|
232
|
+
parsed.username !== "" ||
|
|
233
|
+
parsed.password !== "" ||
|
|
234
|
+
parsed.pathname !== "/" ||
|
|
235
|
+
parsed.search !== "" ||
|
|
236
|
+
parsed.hash !== ""
|
|
237
|
+
) {
|
|
238
|
+
return undefined;
|
|
239
|
+
}
|
|
240
|
+
return parsed.origin.toLowerCase();
|
|
241
|
+
} catch {
|
|
242
|
+
return undefined;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function normalizeAuthority(value: string | undefined): string | undefined {
|
|
247
|
+
if (value === undefined || value.includes("/") || value.includes("@")) return undefined;
|
|
248
|
+
try {
|
|
249
|
+
return new URL(`https://${value}`).host.toLowerCase();
|
|
250
|
+
} catch {
|
|
251
|
+
return undefined;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function parseLoopbackHost(value: string | undefined): string | undefined {
|
|
256
|
+
if (value === undefined) return undefined;
|
|
257
|
+
try {
|
|
258
|
+
const parsed = new URL(`http://${value}`);
|
|
259
|
+
if (parsed.username !== "" || parsed.password !== "" || parsed.pathname !== "/" || parsed.search !== "") {
|
|
260
|
+
return undefined;
|
|
261
|
+
}
|
|
262
|
+
if (!isLoopbackHostname(parsed.hostname)) return undefined;
|
|
263
|
+
return parsed.host.toLowerCase();
|
|
264
|
+
} catch {
|
|
265
|
+
return undefined;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function normalizePeer(value: string | undefined): string | undefined {
|
|
270
|
+
if (value === undefined) return undefined;
|
|
271
|
+
const normalized = value.trim().toLowerCase();
|
|
272
|
+
if (normalized.length === 0) return undefined;
|
|
273
|
+
return normalized.startsWith("::ffff:") ? normalized.slice("::ffff:".length) : normalized;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function isLoopbackPeer(value: string | undefined): boolean {
|
|
277
|
+
const normalized = normalizePeer(value);
|
|
278
|
+
if (normalized === undefined) return false;
|
|
279
|
+
return normalized === "::1" || isIpv4Loopback(normalized);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function isLoopbackHostname(value: string): boolean {
|
|
283
|
+
const normalized = value.toLowerCase();
|
|
284
|
+
if (normalized === "localhost" || normalized === "[::1]") return true;
|
|
285
|
+
return isIpv4Loopback(normalized);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function isIpv4Loopback(value: string): boolean {
|
|
289
|
+
const octets = value.split(".");
|
|
290
|
+
if (octets.length !== 4 || octets[0] !== "127") return false;
|
|
291
|
+
return octets.every((octet) => /^(?:0|[1-9][0-9]{0,2})$/u.test(octet) && Number(octet) <= 255);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function isMutation(method: string | undefined): boolean {
|
|
295
|
+
return method !== "GET" && method !== "HEAD" && method !== "OPTIONS";
|
|
296
|
+
}
|