@open-mercato/enterprise 0.6.6-develop.6551.1.1511bfa890 → 0.6.6
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/.turbo/turbo-build.log +1 -1
- package/dist/modules/sso/lib/oidc-provider.js +2 -46
- package/dist/modules/sso/lib/oidc-provider.js.map +3 -3
- package/package.json +6 -7
- package/src/modules/sso/lib/oidc-provider.ts +1 -62
- package/dist/modules/sso/lib/oidc-url-safety.js +0 -105
- package/dist/modules/sso/lib/oidc-url-safety.js.map +0 -7
- package/src/modules/sso/lib/oidc-url-safety.ts +0 -130
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
[build:enterprise] found
|
|
1
|
+
[build:enterprise] found 325 entry points
|
|
2
2
|
[build:enterprise] built successfully
|
|
@@ -1,15 +1,7 @@
|
|
|
1
1
|
import * as client from "openid-client";
|
|
2
|
-
import { createHash } from "node:crypto";
|
|
3
|
-
import { createOidcFetch } from "./oidc-url-safety.js";
|
|
4
|
-
const OIDC_DISCOVERY_TIMEOUT_SECONDS = 10;
|
|
5
|
-
const OIDC_DISCOVERY_CACHE_TTL_MS = 5 * 60 * 1e3;
|
|
6
|
-
const OIDC_DISCOVERY_CACHE_MAX_ENTRIES = 100;
|
|
7
|
-
const sharedDiscoveryCache = /* @__PURE__ */ new Map();
|
|
8
2
|
class OidcProvider {
|
|
9
3
|
constructor() {
|
|
10
4
|
this.protocol = "oidc";
|
|
11
|
-
this.requestDiscoveryCache = /* @__PURE__ */ new Map();
|
|
12
|
-
this.guardedFetch = createOidcFetch();
|
|
13
5
|
}
|
|
14
6
|
async buildAuthUrl(config, params) {
|
|
15
7
|
const oidcConfig = await this.discover(config, params.clientSecret);
|
|
@@ -75,47 +67,11 @@ class OidcProvider {
|
|
|
75
67
|
if (!config.clientId) {
|
|
76
68
|
throw new Error("SSO config is missing client ID");
|
|
77
69
|
}
|
|
78
|
-
|
|
79
|
-
const cacheKey = `${config.id}:${config.updatedAt.getTime()}:${clientSecretFingerprint}`;
|
|
80
|
-
const now = Date.now();
|
|
81
|
-
const cached = this.requestDiscoveryCache.get(cacheKey) ?? sharedDiscoveryCache.get(cacheKey);
|
|
82
|
-
if (cached && cached.expiresAt > now) return cached.promise;
|
|
83
|
-
for (const [key, entry2] of sharedDiscoveryCache) {
|
|
84
|
-
if (entry2.expiresAt <= now) sharedDiscoveryCache.delete(key);
|
|
85
|
-
}
|
|
86
|
-
if (sharedDiscoveryCache.size >= OIDC_DISCOVERY_CACHE_MAX_ENTRIES) {
|
|
87
|
-
const oldestKey = sharedDiscoveryCache.keys().next().value;
|
|
88
|
-
if (oldestKey) sharedDiscoveryCache.delete(oldestKey);
|
|
89
|
-
}
|
|
90
|
-
const promise = client.discovery(
|
|
70
|
+
return client.discovery(
|
|
91
71
|
new URL(config.issuer),
|
|
92
72
|
config.clientId,
|
|
93
|
-
clientSecret ?? void 0
|
|
94
|
-
void 0,
|
|
95
|
-
{
|
|
96
|
-
[client.customFetch]: this.guardedFetch,
|
|
97
|
-
timeout: OIDC_DISCOVERY_TIMEOUT_SECONDS
|
|
98
|
-
}
|
|
73
|
+
clientSecret ?? void 0
|
|
99
74
|
);
|
|
100
|
-
const entry = {
|
|
101
|
-
expiresAt: now + OIDC_DISCOVERY_CACHE_TTL_MS,
|
|
102
|
-
promise
|
|
103
|
-
};
|
|
104
|
-
this.requestDiscoveryCache.set(cacheKey, entry);
|
|
105
|
-
sharedDiscoveryCache.set(cacheKey, entry);
|
|
106
|
-
void promise.then((configuration) => {
|
|
107
|
-
if (typeof configuration.serverMetadata !== "function" && sharedDiscoveryCache.get(cacheKey)?.promise === promise) {
|
|
108
|
-
sharedDiscoveryCache.delete(cacheKey);
|
|
109
|
-
}
|
|
110
|
-
}).catch(() => {
|
|
111
|
-
if (this.requestDiscoveryCache.get(cacheKey)?.promise === promise) {
|
|
112
|
-
this.requestDiscoveryCache.delete(cacheKey);
|
|
113
|
-
}
|
|
114
|
-
if (sharedDiscoveryCache.get(cacheKey)?.promise === promise) {
|
|
115
|
-
sharedDiscoveryCache.delete(cacheKey);
|
|
116
|
-
}
|
|
117
|
-
});
|
|
118
|
-
return promise;
|
|
119
75
|
}
|
|
120
76
|
}
|
|
121
77
|
async function mergeWithUserInfoClaims(oidcConfig, tokens, claims) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/sso/lib/oidc-provider.ts"],
|
|
4
|
-
"sourcesContent": ["import * as client from 'openid-client'\nimport
|
|
5
|
-
"mappings": "AAAA,YAAY,YAAY;
|
|
6
|
-
"names": [
|
|
4
|
+
"sourcesContent": ["import * as client from 'openid-client'\nimport type { SsoConfig } from '../data/entities'\nimport type { SsoIdentityPayload, SsoProtocolProvider } from './types'\n\nexport class OidcProvider implements SsoProtocolProvider {\n readonly protocol = 'oidc' as const\n\n async buildAuthUrl(\n config: SsoConfig,\n params: {\n state: string\n nonce: string\n redirectUri: string\n codeVerifier?: string\n clientSecret?: string\n },\n ): Promise<string> {\n const oidcConfig = await this.discover(config, params.clientSecret)\n\n const codeChallenge = params.codeVerifier\n ? await client.calculatePKCECodeChallenge(params.codeVerifier)\n : undefined\n\n const authUrl = client.buildAuthorizationUrl(oidcConfig, {\n redirect_uri: params.redirectUri,\n scope: 'openid email profile',\n state: params.state,\n nonce: params.nonce,\n ...(codeChallenge\n ? { code_challenge: codeChallenge, code_challenge_method: 'S256' }\n : {}),\n })\n\n return authUrl.href\n }\n\n async handleCallback(\n config: SsoConfig,\n params: {\n callbackParams: Record<string, string>\n redirectUri: string\n expectedState: string\n expectedNonce: string\n codeVerifier?: string\n clientSecret?: string\n },\n ): Promise<SsoIdentityPayload> {\n const oidcConfig = await this.discover(config, params.clientSecret)\n\n const callbackUrl = new URL(params.redirectUri)\n for (const [key, value] of Object.entries(params.callbackParams)) {\n callbackUrl.searchParams.set(key, value)\n }\n\n const tokens = await client.authorizationCodeGrant(oidcConfig, callbackUrl, {\n pkceCodeVerifier: params.codeVerifier,\n expectedState: params.expectedState,\n expectedNonce: params.expectedNonce,\n })\n\n const claims = tokens.claims()\n if (!claims) {\n throw new Error('No ID token claims received from IdP')\n }\n\n const mergedClaims = await mergeWithUserInfoClaims(oidcConfig, tokens, claims)\n\n const subject = String(mergedClaims.sub ?? claims.sub ?? '')\n const rawEmail = mergedClaims.email as string | undefined\n const upnCandidate = (mergedClaims.upn ?? mergedClaims.unique_name) as string | undefined\n const upnAsEmail = typeof upnCandidate === 'string' && upnCandidate.includes('@') ? upnCandidate : undefined\n const email = rawEmail ?? upnAsEmail\n if (!email) {\n throw new Error('IdP did not return an email claim (checked: email, upn, unique_name)')\n }\n\n const emailVerified = normalizeEmailVerifiedClaim(mergedClaims.email_verified)\n const groups = extractIdentityGroups(mergedClaims)\n\n return {\n subject,\n email,\n ...(emailVerified === undefined ? {} : { emailVerified }),\n name: (mergedClaims.name as string) ?? undefined,\n groups,\n }\n }\n\n async validateConfig(\n config: SsoConfig,\n params?: { clientSecret?: string },\n ): Promise<{ ok: boolean; error?: string }> {\n try {\n await this.discover(config, params?.clientSecret)\n return { ok: true }\n } catch (err) {\n return {\n ok: false,\n error: err instanceof Error ? err.message : 'Discovery failed',\n }\n }\n }\n\n private async discover(\n config: SsoConfig,\n clientSecret?: string,\n ): Promise<client.Configuration> {\n if (!config.issuer) {\n throw new Error('SSO config is missing issuer URL')\n }\n if (!config.clientId) {\n throw new Error('SSO config is missing client ID')\n }\n\n return client.discovery(\n new URL(config.issuer),\n config.clientId,\n clientSecret ?? undefined,\n )\n }\n}\n\nasync function mergeWithUserInfoClaims(\n oidcConfig: client.Configuration,\n tokens: client.TokenEndpointResponse,\n claims: Record<string, unknown>,\n): Promise<Record<string, unknown>> {\n const accessToken = tokens.access_token\n if (!accessToken) return claims\n\n try {\n const userInfo = await client.fetchUserInfo(\n oidcConfig,\n accessToken,\n client.skipSubjectCheck,\n )\n return { ...(userInfo as Record<string, unknown>), ...claims }\n } catch {\n return claims\n }\n}\n\nexport function normalizeEmailVerifiedClaim(value: unknown): boolean | undefined {\n return typeof value === 'boolean' ? value : undefined\n}\n\nexport function extractIdentityGroups(claims: Record<string, unknown>): string[] | undefined {\n const groups = new Set<string>()\n\n const add = (value: unknown) => {\n for (const group of coerceClaimValues(value)) {\n groups.add(group)\n }\n }\n\n add(claims.groups)\n add(claims.roles)\n add(claims.role)\n\n for (const [key, value] of Object.entries(claims)) {\n if (!key.endsWith(':roles')) continue\n add(value)\n }\n\n return groups.size > 0 ? Array.from(groups) : undefined\n}\n\nexport function coerceClaimValues(value: unknown): string[] {\n if (typeof value === 'string') {\n const normalized = value.trim()\n return normalized ? [normalized] : []\n }\n\n if (Array.isArray(value)) {\n return value.flatMap((entry) => coerceClaimValues(entry))\n }\n\n if (value && typeof value === 'object') {\n const entries = Object.entries(value as Record<string, unknown>)\n const out = new Set<string>()\n for (const [key, nested] of entries) {\n const normalizedKey = key.trim()\n if (normalizedKey) out.add(normalizedKey)\n if (typeof nested === 'string') {\n const normalizedNested = nested.trim()\n if (normalizedNested) out.add(normalizedNested)\n } else if (nested && typeof nested === 'object') {\n const nestedName = (nested as Record<string, unknown>).name\n if (typeof nestedName === 'string' && nestedName.trim()) {\n out.add(nestedName.trim())\n }\n }\n }\n return Array.from(out)\n }\n\n return []\n}\n"],
|
|
5
|
+
"mappings": "AAAA,YAAY,YAAY;AAIjB,MAAM,aAA4C;AAAA,EAAlD;AACL,SAAS,WAAW;AAAA;AAAA,EAEpB,MAAM,aACJ,QACA,QAOiB;AACjB,UAAM,aAAa,MAAM,KAAK,SAAS,QAAQ,OAAO,YAAY;AAElE,UAAM,gBAAgB,OAAO,eACzB,MAAM,OAAO,2BAA2B,OAAO,YAAY,IAC3D;AAEJ,UAAM,UAAU,OAAO,sBAAsB,YAAY;AAAA,MACvD,cAAc,OAAO;AAAA,MACrB,OAAO;AAAA,MACP,OAAO,OAAO;AAAA,MACd,OAAO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K;AAAA,EACX;AAEA,SAAO,OAAO,OAAO,IAAI,MAAM,KAAK,MAAM,IAAI;AAChD;AAEO,SAAS,kBAAkB,OAA0B;AAC1D,MAAI,OAAO,UAAU,UAAU;AAC7B,UAAM,aAAa,MAAM,KAAK;AAC9B,WAAO,aAAa,CAAC,UAAU,IAAI,CAAC;AAAA,EACtC;AAEA,MAAI,MAAM,QAAQ,KAAK,GAAG;AACxB,WAAO,MAAM,QAAQ,CAAC,UAAU,kBAAkB,KAAK,CAAC;AAAA,EAC1D;AAEA,MAAI,SAAS,OAAO,UAAU,UAAU;AACtC,UAAM,UAAU,OAAO,QAAQ,KAAgC;AAC/D,UAAM,MAAM,oBAAI,IAAY;AAC5B,eAAW,CAAC,KAAK,MAAM,KAAK,SAAS;AACnC,YAAM,gBAAgB,IAAI,KAAK;AAC/B,UAAI,cAAe,KAAI,IAAI,aAAa;AACxC,UAAI,OAAO,WAAW,UAAU;AAC9B,cAAM,mBAAmB,OAAO,KAAK;AACrC,YAAI,iBAAkB,KAAI,IAAI,gBAAgB;AAAA,MAChD,WAAW,UAAU,OAAO,WAAW,UAAU;AAC/C,cAAM,aAAc,OAAmC;AACvD,YAAI,OAAO,eAAe,YAAY,WAAW,KAAK,GAAG;AACvD,cAAI,IAAI,WAAW,KAAK,CAAC;AAAA,QAC3B;AAAA,MACF;AAAA,IACF;AACA,WAAO,MAAM,KAAK,GAAG;AAAA,EACvB;AAEA,SAAO,CAAC;AACV;",
|
|
6
|
+
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-mercato/enterprise",
|
|
3
|
-
"version": "0.6.6
|
|
3
|
+
"version": "0.6.6",
|
|
4
4
|
"license": "SEE LICENSE IN LICENSE.md",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -65,8 +65,8 @@
|
|
|
65
65
|
}
|
|
66
66
|
},
|
|
67
67
|
"dependencies": {
|
|
68
|
-
"@open-mercato/core": "0.6.6
|
|
69
|
-
"@open-mercato/ui": "0.6.6
|
|
68
|
+
"@open-mercato/core": "0.6.6",
|
|
69
|
+
"@open-mercato/ui": "0.6.6",
|
|
70
70
|
"@simplewebauthn/browser": "^13.3.0",
|
|
71
71
|
"@simplewebauthn/server": "^13.3.2",
|
|
72
72
|
"@simplewebauthn/types": "^12.0.0",
|
|
@@ -76,12 +76,12 @@
|
|
|
76
76
|
"qrcode": "^1.5.4"
|
|
77
77
|
},
|
|
78
78
|
"peerDependencies": {
|
|
79
|
-
"@open-mercato/shared": "0.6.6
|
|
79
|
+
"@open-mercato/shared": "0.6.6",
|
|
80
80
|
"react": "^19.0.0",
|
|
81
81
|
"react-dom": "^19.0.0"
|
|
82
82
|
},
|
|
83
83
|
"devDependencies": {
|
|
84
|
-
"@open-mercato/shared": "0.6.6
|
|
84
|
+
"@open-mercato/shared": "0.6.6",
|
|
85
85
|
"@types/jest": "^30.0.0",
|
|
86
86
|
"@types/react": "^19.2.17",
|
|
87
87
|
"@types/react-dom": "^19.2.3",
|
|
@@ -98,6 +98,5 @@
|
|
|
98
98
|
"type": "git",
|
|
99
99
|
"url": "https://github.com/open-mercato/open-mercato",
|
|
100
100
|
"directory": "packages/enterprise"
|
|
101
|
-
}
|
|
102
|
-
"stableVersion": "0.6.5"
|
|
101
|
+
}
|
|
103
102
|
}
|
|
@@ -1,24 +1,9 @@
|
|
|
1
1
|
import * as client from 'openid-client'
|
|
2
|
-
import { createHash } from 'node:crypto'
|
|
3
2
|
import type { SsoConfig } from '../data/entities'
|
|
4
3
|
import type { SsoIdentityPayload, SsoProtocolProvider } from './types'
|
|
5
|
-
import { createOidcFetch } from './oidc-url-safety'
|
|
6
|
-
|
|
7
|
-
const OIDC_DISCOVERY_TIMEOUT_SECONDS = 10
|
|
8
|
-
const OIDC_DISCOVERY_CACHE_TTL_MS = 5 * 60 * 1000
|
|
9
|
-
const OIDC_DISCOVERY_CACHE_MAX_ENTRIES = 100
|
|
10
|
-
|
|
11
|
-
interface CachedDiscovery {
|
|
12
|
-
expiresAt: number
|
|
13
|
-
promise: Promise<client.Configuration>
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const sharedDiscoveryCache = new Map<string, CachedDiscovery>()
|
|
17
4
|
|
|
18
5
|
export class OidcProvider implements SsoProtocolProvider {
|
|
19
6
|
readonly protocol = 'oidc' as const
|
|
20
|
-
private readonly requestDiscoveryCache = new Map<string, CachedDiscovery>()
|
|
21
|
-
private readonly guardedFetch = createOidcFetch()
|
|
22
7
|
|
|
23
8
|
async buildAuthUrl(
|
|
24
9
|
config: SsoConfig,
|
|
@@ -127,57 +112,11 @@ export class OidcProvider implements SsoProtocolProvider {
|
|
|
127
112
|
throw new Error('SSO config is missing client ID')
|
|
128
113
|
}
|
|
129
114
|
|
|
130
|
-
|
|
131
|
-
.update(clientSecret ?? '')
|
|
132
|
-
.digest('base64url')
|
|
133
|
-
const cacheKey = `${config.id}:${config.updatedAt.getTime()}:${clientSecretFingerprint}`
|
|
134
|
-
const now = Date.now()
|
|
135
|
-
const cached =
|
|
136
|
-
this.requestDiscoveryCache.get(cacheKey) ?? sharedDiscoveryCache.get(cacheKey)
|
|
137
|
-
if (cached && cached.expiresAt > now) return cached.promise
|
|
138
|
-
|
|
139
|
-
for (const [key, entry] of sharedDiscoveryCache) {
|
|
140
|
-
if (entry.expiresAt <= now) sharedDiscoveryCache.delete(key)
|
|
141
|
-
}
|
|
142
|
-
if (sharedDiscoveryCache.size >= OIDC_DISCOVERY_CACHE_MAX_ENTRIES) {
|
|
143
|
-
const oldestKey = sharedDiscoveryCache.keys().next().value
|
|
144
|
-
if (oldestKey) sharedDiscoveryCache.delete(oldestKey)
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const promise = client.discovery(
|
|
115
|
+
return client.discovery(
|
|
148
116
|
new URL(config.issuer),
|
|
149
117
|
config.clientId,
|
|
150
118
|
clientSecret ?? undefined,
|
|
151
|
-
undefined,
|
|
152
|
-
{
|
|
153
|
-
[client.customFetch]: this.guardedFetch,
|
|
154
|
-
timeout: OIDC_DISCOVERY_TIMEOUT_SECONDS,
|
|
155
|
-
},
|
|
156
119
|
)
|
|
157
|
-
const entry = {
|
|
158
|
-
expiresAt: now + OIDC_DISCOVERY_CACHE_TTL_MS,
|
|
159
|
-
promise,
|
|
160
|
-
}
|
|
161
|
-
this.requestDiscoveryCache.set(cacheKey, entry)
|
|
162
|
-
sharedDiscoveryCache.set(cacheKey, entry)
|
|
163
|
-
void promise
|
|
164
|
-
.then((configuration) => {
|
|
165
|
-
if (
|
|
166
|
-
typeof configuration.serverMetadata !== 'function' &&
|
|
167
|
-
sharedDiscoveryCache.get(cacheKey)?.promise === promise
|
|
168
|
-
) {
|
|
169
|
-
sharedDiscoveryCache.delete(cacheKey)
|
|
170
|
-
}
|
|
171
|
-
})
|
|
172
|
-
.catch(() => {
|
|
173
|
-
if (this.requestDiscoveryCache.get(cacheKey)?.promise === promise) {
|
|
174
|
-
this.requestDiscoveryCache.delete(cacheKey)
|
|
175
|
-
}
|
|
176
|
-
if (sharedDiscoveryCache.get(cacheKey)?.promise === promise) {
|
|
177
|
-
sharedDiscoveryCache.delete(cacheKey)
|
|
178
|
-
}
|
|
179
|
-
})
|
|
180
|
-
return promise
|
|
181
120
|
}
|
|
182
121
|
}
|
|
183
122
|
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
parseOutboundUrl,
|
|
3
|
-
safeOutboundFetch,
|
|
4
|
-
UnsafeOutboundUrlError
|
|
5
|
-
} from "@open-mercato/shared/lib/url-safety";
|
|
6
|
-
const OIDC_URL_SUBJECT = "OIDC endpoint URL";
|
|
7
|
-
const DEFAULT_MAX_RESPONSE_BYTES = 1024 * 1024;
|
|
8
|
-
class UnsafeOidcUrlError extends UnsafeOutboundUrlError {
|
|
9
|
-
constructor(reason, message) {
|
|
10
|
-
super(reason, message);
|
|
11
|
-
this.name = "UnsafeOidcUrlError";
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
class OidcResponseTooLargeError extends Error {
|
|
15
|
-
constructor(maxBytes) {
|
|
16
|
-
super(`OIDC response exceeded the ${maxBytes}-byte limit`);
|
|
17
|
-
this.name = "OidcResponseTooLargeError";
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
function getPrivateOidcOriginAllowlist(rawValue = process.env.OM_SSO_OIDC_PRIVATE_ORIGIN_ALLOWLIST ?? "") {
|
|
21
|
-
const origins = /* @__PURE__ */ new Set();
|
|
22
|
-
for (const entry of rawValue.split(",")) {
|
|
23
|
-
const candidate = entry.trim().replace(/\/$/, "");
|
|
24
|
-
if (!candidate) continue;
|
|
25
|
-
try {
|
|
26
|
-
const url = new URL(candidate);
|
|
27
|
-
if (url.protocol === "https:" && !url.username && !url.password && candidate === url.origin) {
|
|
28
|
-
origins.add(url.origin);
|
|
29
|
-
}
|
|
30
|
-
} catch {
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return origins;
|
|
34
|
-
}
|
|
35
|
-
function createOidcFetch(deps = {}) {
|
|
36
|
-
const privateOriginAllowlist = deps.privateOriginAllowlist ?? getPrivateOidcOriginAllowlist();
|
|
37
|
-
const maxResponseBytes = deps.maxResponseBytes ?? DEFAULT_MAX_RESPONSE_BYTES;
|
|
38
|
-
return async (rawUrl, options) => {
|
|
39
|
-
const { url } = parseOutboundUrl(rawUrl, {
|
|
40
|
-
errorFactory: oidcUrlErrorFactory,
|
|
41
|
-
subject: OIDC_URL_SUBJECT
|
|
42
|
-
});
|
|
43
|
-
if (url.protocol !== "https:") {
|
|
44
|
-
throw oidcUrlErrorFactory(
|
|
45
|
-
"forbidden_protocol",
|
|
46
|
-
`${OIDC_URL_SUBJECT} must use HTTPS`
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
const allowPrivate = privateOriginAllowlist.has(url.origin);
|
|
50
|
-
const response = await safeOutboundFetch(rawUrl, options, {
|
|
51
|
-
allowPrivate,
|
|
52
|
-
errorFactory: oidcUrlErrorFactory,
|
|
53
|
-
fetchImpl: deps.fetchImpl,
|
|
54
|
-
lookupHost: deps.lookupHost,
|
|
55
|
-
pinAllowedPrivate: allowPrivate,
|
|
56
|
-
subject: OIDC_URL_SUBJECT
|
|
57
|
-
});
|
|
58
|
-
return readBoundedResponse(response, maxResponseBytes);
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
function oidcUrlErrorFactory(reason, message) {
|
|
62
|
-
return new UnsafeOidcUrlError(
|
|
63
|
-
reason === "blocked_hostname" ? "private_ip_resolved" : reason,
|
|
64
|
-
message
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
async function readBoundedResponse(response, maxBytes) {
|
|
68
|
-
const declaredLength = Number(response.headers.get("content-length"));
|
|
69
|
-
if (Number.isFinite(declaredLength) && declaredLength > maxBytes) {
|
|
70
|
-
await response.body?.cancel();
|
|
71
|
-
throw new OidcResponseTooLargeError(maxBytes);
|
|
72
|
-
}
|
|
73
|
-
if (!response.body) return response;
|
|
74
|
-
const reader = response.body.getReader();
|
|
75
|
-
const chunks = [];
|
|
76
|
-
let totalBytes = 0;
|
|
77
|
-
while (true) {
|
|
78
|
-
const { done, value } = await reader.read();
|
|
79
|
-
if (done) break;
|
|
80
|
-
totalBytes += value.byteLength;
|
|
81
|
-
if (totalBytes > maxBytes) {
|
|
82
|
-
await reader.cancel();
|
|
83
|
-
throw new OidcResponseTooLargeError(maxBytes);
|
|
84
|
-
}
|
|
85
|
-
chunks.push(value);
|
|
86
|
-
}
|
|
87
|
-
const body = new Uint8Array(totalBytes);
|
|
88
|
-
let offset = 0;
|
|
89
|
-
for (const chunk of chunks) {
|
|
90
|
-
body.set(chunk, offset);
|
|
91
|
-
offset += chunk.byteLength;
|
|
92
|
-
}
|
|
93
|
-
return new Response(body, {
|
|
94
|
-
headers: response.headers,
|
|
95
|
-
status: response.status,
|
|
96
|
-
statusText: response.statusText
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
export {
|
|
100
|
-
OidcResponseTooLargeError,
|
|
101
|
-
UnsafeOidcUrlError,
|
|
102
|
-
createOidcFetch,
|
|
103
|
-
getPrivateOidcOriginAllowlist
|
|
104
|
-
};
|
|
105
|
-
//# sourceMappingURL=oidc-url-safety.js.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../../src/modules/sso/lib/oidc-url-safety.ts"],
|
|
4
|
-
"sourcesContent": ["import type { CustomFetch } from 'openid-client'\nimport {\n parseOutboundUrl,\n safeOutboundFetch,\n UnsafeOutboundUrlError,\n type HostLookup,\n type UrlSafetyReason,\n} from '@open-mercato/shared/lib/url-safety'\n\nconst OIDC_URL_SUBJECT = 'OIDC endpoint URL'\nconst DEFAULT_MAX_RESPONSE_BYTES = 1024 * 1024\n\nexport interface OidcFetchDeps {\n fetchImpl?: typeof fetch\n lookupHost?: HostLookup\n maxResponseBytes?: number\n privateOriginAllowlist?: ReadonlySet<string>\n}\n\nexport class UnsafeOidcUrlError extends UnsafeOutboundUrlError {\n constructor(reason: UrlSafetyReason, message: string) {\n super(reason, message)\n this.name = 'UnsafeOidcUrlError'\n }\n}\n\nexport class OidcResponseTooLargeError extends Error {\n constructor(maxBytes: number) {\n super(`OIDC response exceeded the ${maxBytes}-byte limit`)\n this.name = 'OidcResponseTooLargeError'\n }\n}\n\nexport function getPrivateOidcOriginAllowlist(\n rawValue = process.env.OM_SSO_OIDC_PRIVATE_ORIGIN_ALLOWLIST ?? '',\n): ReadonlySet<string> {\n const origins = new Set<string>()\n for (const entry of rawValue.split(',')) {\n const candidate = entry.trim().replace(/\\/$/, '')\n if (!candidate) continue\n try {\n const url = new URL(candidate)\n if (\n url.protocol === 'https:' &&\n !url.username &&\n !url.password &&\n candidate === url.origin\n ) {\n origins.add(url.origin)\n }\n } catch {\n // Invalid entries fail closed and are ignored.\n }\n }\n return origins\n}\n\nexport function createOidcFetch(deps: OidcFetchDeps = {}): CustomFetch {\n const privateOriginAllowlist =\n deps.privateOriginAllowlist ?? getPrivateOidcOriginAllowlist()\n const maxResponseBytes = deps.maxResponseBytes ?? DEFAULT_MAX_RESPONSE_BYTES\n\n return async (rawUrl, options) => {\n const { url } = parseOutboundUrl(rawUrl, {\n errorFactory: oidcUrlErrorFactory,\n subject: OIDC_URL_SUBJECT,\n })\n if (url.protocol !== 'https:') {\n throw oidcUrlErrorFactory(\n 'forbidden_protocol',\n `${OIDC_URL_SUBJECT} must use HTTPS`,\n )\n }\n\n const allowPrivate = privateOriginAllowlist.has(url.origin)\n const response = await safeOutboundFetch(rawUrl, options as RequestInit, {\n allowPrivate,\n errorFactory: oidcUrlErrorFactory,\n fetchImpl: deps.fetchImpl,\n lookupHost: deps.lookupHost,\n pinAllowedPrivate: allowPrivate,\n subject: OIDC_URL_SUBJECT,\n })\n return readBoundedResponse(response, maxResponseBytes)\n }\n}\n\nfunction oidcUrlErrorFactory(reason: UrlSafetyReason, message: string): Error {\n return new UnsafeOidcUrlError(\n reason === 'blocked_hostname' ? 'private_ip_resolved' : reason,\n message,\n )\n}\n\nasync function readBoundedResponse(response: Response, maxBytes: number): Promise<Response> {\n const declaredLength = Number(response.headers.get('content-length'))\n if (Number.isFinite(declaredLength) && declaredLength > maxBytes) {\n await response.body?.cancel()\n throw new OidcResponseTooLargeError(maxBytes)\n }\n\n if (!response.body) return response\n\n const reader = response.body.getReader()\n const chunks: Uint8Array[] = []\n let totalBytes = 0\n while (true) {\n const { done, value } = await reader.read()\n if (done) break\n totalBytes += value.byteLength\n if (totalBytes > maxBytes) {\n await reader.cancel()\n throw new OidcResponseTooLargeError(maxBytes)\n }\n chunks.push(value)\n }\n\n const body = new Uint8Array(totalBytes)\n let offset = 0\n for (const chunk of chunks) {\n body.set(chunk, offset)\n offset += chunk.byteLength\n }\n\n return new Response(body, {\n headers: response.headers,\n status: response.status,\n statusText: response.statusText,\n })\n}\n"],
|
|
5
|
-
"mappings": "AACA;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
|
|
3
|
-
parseOutboundUrl,
|
|
4
|
-
safeOutboundFetch,
|
|
5
|
-
UnsafeOutboundUrlError,
|
|
6
|
-
type HostLookup,
|
|
7
|
-
type UrlSafetyReason,
|
|
8
|
-
} from '@open-mercato/shared/lib/url-safety'
|
|
9
|
-
|
|
10
|
-
const OIDC_URL_SUBJECT = 'OIDC endpoint URL'
|
|
11
|
-
const DEFAULT_MAX_RESPONSE_BYTES = 1024 * 1024
|
|
12
|
-
|
|
13
|
-
export interface OidcFetchDeps {
|
|
14
|
-
fetchImpl?: typeof fetch
|
|
15
|
-
lookupHost?: HostLookup
|
|
16
|
-
maxResponseBytes?: number
|
|
17
|
-
privateOriginAllowlist?: ReadonlySet<string>
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export class UnsafeOidcUrlError extends UnsafeOutboundUrlError {
|
|
21
|
-
constructor(reason: UrlSafetyReason, message: string) {
|
|
22
|
-
super(reason, message)
|
|
23
|
-
this.name = 'UnsafeOidcUrlError'
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export class OidcResponseTooLargeError extends Error {
|
|
28
|
-
constructor(maxBytes: number) {
|
|
29
|
-
super(`OIDC response exceeded the ${maxBytes}-byte limit`)
|
|
30
|
-
this.name = 'OidcResponseTooLargeError'
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function getPrivateOidcOriginAllowlist(
|
|
35
|
-
rawValue = process.env.OM_SSO_OIDC_PRIVATE_ORIGIN_ALLOWLIST ?? '',
|
|
36
|
-
): ReadonlySet<string> {
|
|
37
|
-
const origins = new Set<string>()
|
|
38
|
-
for (const entry of rawValue.split(',')) {
|
|
39
|
-
const candidate = entry.trim().replace(/\/$/, '')
|
|
40
|
-
if (!candidate) continue
|
|
41
|
-
try {
|
|
42
|
-
const url = new URL(candidate)
|
|
43
|
-
if (
|
|
44
|
-
url.protocol === 'https:' &&
|
|
45
|
-
!url.username &&
|
|
46
|
-
!url.password &&
|
|
47
|
-
candidate === url.origin
|
|
48
|
-
) {
|
|
49
|
-
origins.add(url.origin)
|
|
50
|
-
}
|
|
51
|
-
} catch {
|
|
52
|
-
// Invalid entries fail closed and are ignored.
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return origins
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function createOidcFetch(deps: OidcFetchDeps = {}): CustomFetch {
|
|
59
|
-
const privateOriginAllowlist =
|
|
60
|
-
deps.privateOriginAllowlist ?? getPrivateOidcOriginAllowlist()
|
|
61
|
-
const maxResponseBytes = deps.maxResponseBytes ?? DEFAULT_MAX_RESPONSE_BYTES
|
|
62
|
-
|
|
63
|
-
return async (rawUrl, options) => {
|
|
64
|
-
const { url } = parseOutboundUrl(rawUrl, {
|
|
65
|
-
errorFactory: oidcUrlErrorFactory,
|
|
66
|
-
subject: OIDC_URL_SUBJECT,
|
|
67
|
-
})
|
|
68
|
-
if (url.protocol !== 'https:') {
|
|
69
|
-
throw oidcUrlErrorFactory(
|
|
70
|
-
'forbidden_protocol',
|
|
71
|
-
`${OIDC_URL_SUBJECT} must use HTTPS`,
|
|
72
|
-
)
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const allowPrivate = privateOriginAllowlist.has(url.origin)
|
|
76
|
-
const response = await safeOutboundFetch(rawUrl, options as RequestInit, {
|
|
77
|
-
allowPrivate,
|
|
78
|
-
errorFactory: oidcUrlErrorFactory,
|
|
79
|
-
fetchImpl: deps.fetchImpl,
|
|
80
|
-
lookupHost: deps.lookupHost,
|
|
81
|
-
pinAllowedPrivate: allowPrivate,
|
|
82
|
-
subject: OIDC_URL_SUBJECT,
|
|
83
|
-
})
|
|
84
|
-
return readBoundedResponse(response, maxResponseBytes)
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function oidcUrlErrorFactory(reason: UrlSafetyReason, message: string): Error {
|
|
89
|
-
return new UnsafeOidcUrlError(
|
|
90
|
-
reason === 'blocked_hostname' ? 'private_ip_resolved' : reason,
|
|
91
|
-
message,
|
|
92
|
-
)
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
async function readBoundedResponse(response: Response, maxBytes: number): Promise<Response> {
|
|
96
|
-
const declaredLength = Number(response.headers.get('content-length'))
|
|
97
|
-
if (Number.isFinite(declaredLength) && declaredLength > maxBytes) {
|
|
98
|
-
await response.body?.cancel()
|
|
99
|
-
throw new OidcResponseTooLargeError(maxBytes)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (!response.body) return response
|
|
103
|
-
|
|
104
|
-
const reader = response.body.getReader()
|
|
105
|
-
const chunks: Uint8Array[] = []
|
|
106
|
-
let totalBytes = 0
|
|
107
|
-
while (true) {
|
|
108
|
-
const { done, value } = await reader.read()
|
|
109
|
-
if (done) break
|
|
110
|
-
totalBytes += value.byteLength
|
|
111
|
-
if (totalBytes > maxBytes) {
|
|
112
|
-
await reader.cancel()
|
|
113
|
-
throw new OidcResponseTooLargeError(maxBytes)
|
|
114
|
-
}
|
|
115
|
-
chunks.push(value)
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const body = new Uint8Array(totalBytes)
|
|
119
|
-
let offset = 0
|
|
120
|
-
for (const chunk of chunks) {
|
|
121
|
-
body.set(chunk, offset)
|
|
122
|
-
offset += chunk.byteLength
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return new Response(body, {
|
|
126
|
-
headers: response.headers,
|
|
127
|
-
status: response.status,
|
|
128
|
-
statusText: response.statusText,
|
|
129
|
-
})
|
|
130
|
-
}
|