@authowl/next 0.2.11 → 0.2.12

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/dist/server.cjs CHANGED
@@ -82,10 +82,34 @@ var AUTH_FETCH_TIMEOUT_MS = 1e4;
82
82
  function response(status, body) {
83
83
  return body ? Response.json(body, { status, headers: { "cache-control": "no-store" } }) : new Response(null, { status, headers: { "cache-control": "no-store" } });
84
84
  }
85
- function requestIsSameOriginBrowserPost(request) {
85
+ function singleForwardingHeader(headers, name) {
86
+ const value = headers.get(name);
87
+ if (!value || value !== value.trim() || value.includes(",")) return null;
88
+ return value;
89
+ }
90
+ function forwardedRequestOrigin(request) {
91
+ const protocol = singleForwardingHeader(request.headers, "x-forwarded-proto");
92
+ const host = singleForwardingHeader(request.headers, "x-forwarded-host") ?? singleForwardingHeader(request.headers, "host");
93
+ if (protocol !== "http" && protocol !== "https" || !host) return null;
94
+ try {
95
+ const url = new URL(`${protocol}://${host}`);
96
+ if (url.username || url.password || url.pathname !== "/" || url.search || url.hash) {
97
+ return null;
98
+ }
99
+ return url.origin;
100
+ } catch {
101
+ return null;
102
+ }
103
+ }
104
+ function sameOriginBrowserPostOrigin(request) {
86
105
  const origin = request.headers.get("origin");
87
106
  const site = request.headers.get("sec-fetch-site");
88
- return request.method === "POST" && origin === new URL(request.url).origin && (site === null || site === "same-origin") && request.headers.get(APP_SESSION_BRIDGE_HEADER) === "1" && request.headers.get("content-type")?.split(";", 1)[0]?.trim().toLowerCase() === "application/json";
107
+ const requestOrigin = new URL(request.url).origin;
108
+ if (request.method !== "POST" || origin === null || site !== null && site !== "same-origin" || request.headers.get(APP_SESSION_BRIDGE_HEADER) !== "1" || request.headers.get("content-type")?.split(";", 1)[0]?.trim().toLowerCase() !== "application/json") {
109
+ return null;
110
+ }
111
+ if (origin === requestOrigin) return requestOrigin;
112
+ return origin === forwardedRequestOrigin(request) ? origin : null;
89
113
  }
90
114
  function validToken(value) {
91
115
  return typeof value === "string" && value.length > 0 && value.length <= 8192 && /^[\u0021-\u007e]+$/.test(value) && !/[",;\\]/.test(value);
@@ -110,7 +134,8 @@ function createAuthOwlSessionBridge(input) {
110
134
  return async function authOwlSessionBridge(request) {
111
135
  const config = configured ?? getAuthConfig();
112
136
  const projectId = config.projectId;
113
- if (!requestIsSameOriginBrowserPost(request)) {
137
+ const browserOrigin = sameOriginBrowserPostOrigin(request);
138
+ if (!browserOrigin) {
114
139
  return response(403, { error: "forbidden" });
115
140
  }
116
141
  const raw = await request.text();
@@ -162,7 +187,7 @@ function createAuthOwlSessionBridge(input) {
162
187
  if (!Number.isFinite(expires.getTime()) || expires.getTime() <= Date.now()) {
163
188
  return response(401, { error: "invalid_session" });
164
189
  }
165
- const secure = new URL(request.url).protocol === "https:";
190
+ const secure = new URL(browserOrigin).protocol === "https:";
166
191
  const names = appSessionCookieNames(projectId);
167
192
  const result = response(204);
168
193
  result.headers.append(
package/dist/server.js CHANGED
@@ -52,10 +52,34 @@ var AUTH_FETCH_TIMEOUT_MS = 1e4;
52
52
  function response(status, body) {
53
53
  return body ? Response.json(body, { status, headers: { "cache-control": "no-store" } }) : new Response(null, { status, headers: { "cache-control": "no-store" } });
54
54
  }
55
- function requestIsSameOriginBrowserPost(request) {
55
+ function singleForwardingHeader(headers, name) {
56
+ const value = headers.get(name);
57
+ if (!value || value !== value.trim() || value.includes(",")) return null;
58
+ return value;
59
+ }
60
+ function forwardedRequestOrigin(request) {
61
+ const protocol = singleForwardingHeader(request.headers, "x-forwarded-proto");
62
+ const host = singleForwardingHeader(request.headers, "x-forwarded-host") ?? singleForwardingHeader(request.headers, "host");
63
+ if (protocol !== "http" && protocol !== "https" || !host) return null;
64
+ try {
65
+ const url = new URL(`${protocol}://${host}`);
66
+ if (url.username || url.password || url.pathname !== "/" || url.search || url.hash) {
67
+ return null;
68
+ }
69
+ return url.origin;
70
+ } catch {
71
+ return null;
72
+ }
73
+ }
74
+ function sameOriginBrowserPostOrigin(request) {
56
75
  const origin = request.headers.get("origin");
57
76
  const site = request.headers.get("sec-fetch-site");
58
- return request.method === "POST" && origin === new URL(request.url).origin && (site === null || site === "same-origin") && request.headers.get(APP_SESSION_BRIDGE_HEADER) === "1" && request.headers.get("content-type")?.split(";", 1)[0]?.trim().toLowerCase() === "application/json";
77
+ const requestOrigin = new URL(request.url).origin;
78
+ if (request.method !== "POST" || origin === null || site !== null && site !== "same-origin" || request.headers.get(APP_SESSION_BRIDGE_HEADER) !== "1" || request.headers.get("content-type")?.split(";", 1)[0]?.trim().toLowerCase() !== "application/json") {
79
+ return null;
80
+ }
81
+ if (origin === requestOrigin) return requestOrigin;
82
+ return origin === forwardedRequestOrigin(request) ? origin : null;
59
83
  }
60
84
  function validToken(value) {
61
85
  return typeof value === "string" && value.length > 0 && value.length <= 8192 && /^[\u0021-\u007e]+$/.test(value) && !/[",;\\]/.test(value);
@@ -80,7 +104,8 @@ function createAuthOwlSessionBridge(input) {
80
104
  return async function authOwlSessionBridge(request) {
81
105
  const config = configured ?? getAuthConfig();
82
106
  const projectId = config.projectId;
83
- if (!requestIsSameOriginBrowserPost(request)) {
107
+ const browserOrigin = sameOriginBrowserPostOrigin(request);
108
+ if (!browserOrigin) {
84
109
  return response(403, { error: "forbidden" });
85
110
  }
86
111
  const raw = await request.text();
@@ -132,7 +157,7 @@ function createAuthOwlSessionBridge(input) {
132
157
  if (!Number.isFinite(expires.getTime()) || expires.getTime() <= Date.now()) {
133
158
  return response(401, { error: "invalid_session" });
134
159
  }
135
- const secure = new URL(request.url).protocol === "https:";
160
+ const secure = new URL(browserOrigin).protocol === "https:";
136
161
  const names = appSessionCookieNames(projectId);
137
162
  const result = response(204);
138
163
  result.headers.append(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@authowl/next",
3
- "version": "0.2.11",
3
+ "version": "0.2.12",
4
4
  "description": "Next.js (App Router) server helpers for AuthOwl, the multi-tenant auth SaaS.",
5
5
  "license": "MIT",
6
6
  "publishConfig": {