cauthy-auth 0.28.0 → 0.28.1

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/index.mjs CHANGED
@@ -846,6 +846,13 @@ function providerIn(segment) {
846
846
  function env2(name) {
847
847
  return typeof process !== "undefined" ? process.env?.[name] : void 0;
848
848
  }
849
+ function hostOf(origin) {
850
+ try {
851
+ return new URL(origin).hostname;
852
+ } catch {
853
+ return void 0;
854
+ }
855
+ }
849
856
  function b64url(bytes) {
850
857
  let s = "";
851
858
  for (const b of bytes) s += String.fromCharCode(b);
@@ -879,6 +886,10 @@ function createCauthy(given = {}) {
879
886
  if (!/^https?:\/\//.test(issuer)) throw new CauthyConfigError("issuer (CAUTHY_ISSUER) must be Cauthy's address, e.g. https://id.example.com");
880
887
  if (clientId === "") throw new CauthyConfigError("clientId (CAUTHY_CLIENT_ID) is required");
881
888
  if (secret.length < 32) throw new CauthyConfigError("secret (CAUTHY_SECRET) must be at least 32 characters: it encrypts the session cookie");
889
+ const baseURL = given.baseURL ?? env2("CAUTHY_BASE_URL");
890
+ if (baseURL !== void 0 && baseURL !== "" && !/^https?:\/\/[^/]+\/*$/.test(baseURL)) {
891
+ throw new CauthyConfigError("baseURL (CAUTHY_BASE_URL) must be the application's public origin, e.g. https://app.example.com");
892
+ }
882
893
  const basePath = "/" + (given.basePath ?? "/api/auth").replace(/^\/+|\/+$/g, "");
883
894
  const afterSignIn = localPath(given.afterSignIn, "/");
884
895
  const afterSignOut = localPath(given.afterSignOut, "/");
@@ -894,7 +905,12 @@ function createCauthy(given = {}) {
894
905
  verifyEmail: given.pages?.verifyEmail ? localPath(given.pages.verifyEmail, "/") : void 0
895
906
  };
896
907
  const ownSystem = createSystem({ issuer, clientId, clientSecret, key: given.systemKey, fetch: given.fetch });
897
- const originOf = (request) => withoutTrailingSlashes(given.baseURL ?? new URL(request.url).origin);
908
+ const originOf = (request) => {
909
+ if (baseURL) return withoutTrailingSlashes(baseURL);
910
+ const url = new URL(request.url);
911
+ const proto = request.headers.get("x-forwarded-proto")?.split(",")[0]?.trim().toLowerCase();
912
+ return proto === "https" || proto === "http" ? `${proto}://${url.host}` : url.origin;
913
+ };
898
914
  const attrs = (request, maxAge) => ({ secure: originOf(request).startsWith("https://"), maxAge });
899
915
  async function token(form) {
900
916
  const body2 = new URLSearchParams({ client_id: clientId, ...form });
@@ -1192,9 +1208,18 @@ function createCauthy(given = {}) {
1192
1208
  });
1193
1209
  const problem = (status2, code, message, field, headers, reasons) => json({ error: { code, message, ...field ? { field } : {}, ...reasons && reasons.length > 0 ? { reasons } : {} } }, status2, headers);
1194
1210
  const invalid = (p) => problem(400, p.code, p.message, p.field);
1211
+ let explained = false;
1195
1212
  function ownRequest(request) {
1196
1213
  const origin = request.headers.get("origin");
1197
- if (origin !== null) return origin === originOf(request) || origin === new URL(request.url).origin;
1214
+ if (origin !== null) {
1215
+ const own = originOf(request);
1216
+ if (origin === own || origin === new URL(request.url).origin) return true;
1217
+ if (!explained && hostOf(origin) === new URL(own).hostname) {
1218
+ explained = true;
1219
+ console.warn(`cauthy-auth: refused a request from ${origin} as cross_site: this application sees itself as ${own}. Behind a proxy? Set CAUTHY_BASE_URL (or baseURL) to the public origin.`);
1220
+ }
1221
+ return false;
1222
+ }
1198
1223
  return (request.headers.get("sec-fetch-site") ?? "same-origin") === "same-origin";
1199
1224
  }
1200
1225
  async function body(request) {