@oxyhq/core 3.9.0 → 3.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,195 @@
1
+ /**
2
+ * Strict CORS allowlist for Oxy backends.
3
+ *
4
+ * WHY THIS EXISTS
5
+ * ---------------
6
+ * App backends kept hand-rolling CORS, and the unsafe patterns recurred:
7
+ * - `Access-Control-Allow-Origin: *` together with credentials (which is
8
+ * spec-invalid AND a credential-leak vector), or
9
+ * - a "reflect whatever Origin the request carried" fallback (effectively
10
+ * `*` for credentialed requests — the Allo wildcard-fallback class).
11
+ *
12
+ * `createOxyCors` returns a self-contained Express middleware (no `cors`
13
+ * package dependency) that:
14
+ * - allows the Oxy apex origin family (anything under `*.${CENTRAL_IDP_APEX}`,
15
+ * i.e. `oxy.so` — covering `auth.oxy.so`, `api.oxy.so`, `accounts.oxy.so`,
16
+ * `console.oxy.so`, `inbox.oxy.so`, the marketing site, …) reusing the
17
+ * central-origin constants already in core, NOT a fresh hardcoded list,
18
+ * - allows the caller's explicit `appOrigins`,
19
+ * - DENIES everything else (no reflection, never a wildcard with credentials),
20
+ * - echoes back the EXACT matched origin (so credentialed requests work) and
21
+ * sets `Vary: Origin` for correct caching,
22
+ * - answers CORS preflight (`OPTIONS`) with `204`.
23
+ *
24
+ * Node/Express-only: exported solely from `@oxyhq/core/server`.
25
+ */
26
+
27
+ import type { NextFunction, Request, RequestHandler, Response } from 'express';
28
+ import { CENTRAL_IDP_APEX } from '../utils/authWebUrl';
29
+ import { registrableApex } from '../utils/fapiAutoDetect';
30
+
31
+ /** Default HTTP methods allowed across origins. */
32
+ const DEFAULT_ALLOWED_METHODS = ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE', 'OPTIONS'];
33
+
34
+ /** Default request headers a browser may send on a credentialed cross-origin call. */
35
+ const DEFAULT_ALLOWED_HEADERS = [
36
+ 'Content-Type',
37
+ 'Authorization',
38
+ 'X-Requested-With',
39
+ 'X-Oxy-User-Id',
40
+ 'X-Oxy-Internal',
41
+ 'X-CSRF-Token',
42
+ ];
43
+
44
+ /** How long (seconds) a browser may cache a successful preflight. */
45
+ const DEFAULT_MAX_AGE_SECONDS = 86_400;
46
+
47
+ export interface OxyCorsOptions {
48
+ /**
49
+ * Explicit additional allowed origins (exact-origin match, e.g.
50
+ * `https://app.example.com`, `http://localhost:3000`). These are allowed IN
51
+ * ADDITION TO the Oxy apex origin family. Each is normalized via `new URL().origin`.
52
+ */
53
+ appOrigins?: string[];
54
+ /**
55
+ * Whether to emit `Access-Control-Allow-Credentials: true`. Default `true`
56
+ * (the Oxy ecosystem uses cookie/bearer credentials). Even when `true`, the
57
+ * helper NEVER emits a wildcard origin — only an exact matched origin.
58
+ */
59
+ allowCredentials?: boolean;
60
+ /** HTTP methods to allow. Defaults to the full standard set. */
61
+ methods?: string[];
62
+ /** Request headers to allow. Defaults to the common Oxy set. */
63
+ allowedHeaders?: string[];
64
+ /** Response headers to expose to the browser. Defaults to none. */
65
+ exposedHeaders?: string[];
66
+ /** Preflight cache lifetime in seconds. Default 86400 (24h). */
67
+ maxAgeSeconds?: number;
68
+ }
69
+
70
+ /**
71
+ * Whether `candidate` belongs to the Oxy apex origin family — i.e. its
72
+ * registrable apex equals {@link CENTRAL_IDP_APEX} (`oxy.so`). This matches the
73
+ * apex itself (`https://oxy.so`) and any subdomain (`https://auth.oxy.so`,
74
+ * `https://api.oxy.so`, …) over http or https, ports allowed. Returns false on
75
+ * any parse failure (fail closed).
76
+ */
77
+ function isOxyFamilyOrigin(candidate: string): boolean {
78
+ let hostname: string;
79
+ let protocol: string;
80
+ try {
81
+ const url = new URL(candidate);
82
+ hostname = url.hostname.toLowerCase();
83
+ protocol = url.protocol;
84
+ } catch {
85
+ return false;
86
+ }
87
+ if (protocol !== 'https:' && protocol !== 'http:') return false;
88
+ if (hostname === CENTRAL_IDP_APEX) return true;
89
+ return registrableApex(hostname) === CENTRAL_IDP_APEX;
90
+ }
91
+
92
+ /** Normalize a raw origin string to its canonical `scheme://host[:port]` form. */
93
+ function normalizeOrigin(raw: string): string | null {
94
+ try {
95
+ return new URL(raw).origin;
96
+ } catch {
97
+ return null;
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Build the origin-matching predicate: true iff `origin` is in the Oxy apex
103
+ * family OR exactly matches one of the configured app origins.
104
+ */
105
+ function buildOriginAllowed(appOrigins: string[]): (origin: string) => boolean {
106
+ const explicit = new Set<string>();
107
+ for (const raw of appOrigins) {
108
+ const normalized = normalizeOrigin(raw);
109
+ if (normalized) explicit.add(normalized);
110
+ }
111
+ return (origin: string): boolean => {
112
+ const normalized = normalizeOrigin(origin);
113
+ if (normalized === null) return false;
114
+ if (explicit.has(normalized)) return true;
115
+ return isOxyFamilyOrigin(normalized);
116
+ };
117
+ }
118
+
119
+ /**
120
+ * Create a strict Oxy CORS middleware. See module docs.
121
+ *
122
+ * @example
123
+ * ```ts
124
+ * app.use(createOxyCors({ appOrigins: ['https://app.example.com'] }));
125
+ * ```
126
+ */
127
+ export function createOxyCors(options: OxyCorsOptions = {}): RequestHandler {
128
+ const {
129
+ appOrigins = [],
130
+ allowCredentials = true,
131
+ methods = DEFAULT_ALLOWED_METHODS,
132
+ allowedHeaders = DEFAULT_ALLOWED_HEADERS,
133
+ exposedHeaders = [],
134
+ maxAgeSeconds = DEFAULT_MAX_AGE_SECONDS,
135
+ } = options;
136
+
137
+ const isOriginAllowed = buildOriginAllowed(appOrigins);
138
+ const methodsHeader = methods.join(', ');
139
+ const allowedHeadersHeader = allowedHeaders.join(', ');
140
+ const exposedHeadersHeader = exposedHeaders.join(', ');
141
+
142
+ return (req: Request, res: Response, next: NextFunction): void => {
143
+ const origin = req.headers.origin;
144
+
145
+ // Same-origin or non-browser requests carry no Origin header — pass through
146
+ // untouched (no ACAO header is emitted, which is correct for them).
147
+ if (typeof origin !== 'string' || origin.length === 0) {
148
+ if (req.method === 'OPTIONS') {
149
+ res.sendStatus(204);
150
+ return;
151
+ }
152
+ next();
153
+ return;
154
+ }
155
+
156
+ // Origin is present. Caching correctness: this response varies by Origin.
157
+ res.setHeader('Vary', 'Origin');
158
+
159
+ if (!isOriginAllowed(origin)) {
160
+ // DENY: do NOT reflect the origin, do NOT emit a wildcard. The browser
161
+ // will block the cross-origin read. Preflights for denied origins get a
162
+ // 204 with no CORS headers (the actual request then fails CORS).
163
+ if (req.method === 'OPTIONS') {
164
+ res.sendStatus(204);
165
+ return;
166
+ }
167
+ next();
168
+ return;
169
+ }
170
+
171
+ // ALLOW: echo the EXACT matched origin — never `*`, even without credentials.
172
+ res.setHeader('Access-Control-Allow-Origin', origin);
173
+ if (allowCredentials) {
174
+ res.setHeader('Access-Control-Allow-Credentials', 'true');
175
+ }
176
+ if (exposedHeadersHeader) {
177
+ res.setHeader('Access-Control-Expose-Headers', exposedHeadersHeader);
178
+ }
179
+
180
+ if (req.method === 'OPTIONS') {
181
+ res.setHeader('Access-Control-Allow-Methods', methodsHeader);
182
+ // Honour the browser's requested headers when present, else the default set.
183
+ const requested = req.headers['access-control-request-headers'];
184
+ res.setHeader(
185
+ 'Access-Control-Allow-Headers',
186
+ typeof requested === 'string' && requested.length > 0 ? requested : allowedHeadersHeader,
187
+ );
188
+ res.setHeader('Access-Control-Max-Age', String(maxAgeSeconds));
189
+ res.sendStatus(204);
190
+ return;
191
+ }
192
+
193
+ next();
194
+ };
195
+ }
@@ -34,3 +34,33 @@ export type {
34
34
  } from './auth';
35
35
  export { createOxyRateLimit } from './rateLimit';
36
36
  export type { OxyRateLimitOptions } from './rateLimit';
37
+
38
+ // SSRF-safe upstream fetch + URL validation (Node-only).
39
+ export {
40
+ assertSafePublicUrl,
41
+ isBlockedIp,
42
+ safeFetch,
43
+ SsrfRejection,
44
+ UpstreamError,
45
+ ALLOWED_PORTS,
46
+ ALLOWED_PROTOCOLS,
47
+ BLOCKED_HOSTNAMES,
48
+ DEFAULT_USER_AGENT,
49
+ MAX_REDIRECTS,
50
+ MAX_URL_LENGTH,
51
+ UPSTREAM_HEADERS_TIMEOUT_MS,
52
+ } from './safeFetch';
53
+ export type {
54
+ SafeFetchOptions,
55
+ SafeFetchResult,
56
+ SsrfCheckFail,
57
+ SsrfCheckOk,
58
+ SsrfCheckResult,
59
+ } from './safeFetch';
60
+
61
+ // Strict CORS allowlist (Oxy apex family + explicit app origins).
62
+ export { createOxyCors } from './cors';
63
+ export type { OxyCorsOptions } from './cors';
64
+
65
+ // Constant-time secret comparison.
66
+ export { verifySecret } from './verifySecret';