fix(auth): use host-only cookies for IP deployments
This commit is contained in:
@@ -70,11 +70,11 @@ export function normalizeUsername(username: string): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Compute the cookie domain scope from the configured DOMAIN. */
|
/** Compute the cookie domain scope from the configured DOMAIN. */
|
||||||
export function cookieDomain(domain: string): string {
|
export function cookieDomain(domain: string): string | undefined {
|
||||||
// IP addresses cannot have a registrable parent domain. Treating the first
|
// IP addresses cannot have a registrable parent domain. Treating the first
|
||||||
// octet as a subdomain would turn 100.74.255.106 into 74.255.106 and make
|
// octet as a subdomain would turn 100.74.255.106 into 74.255.106 and make
|
||||||
// browser sessions unusable on direct-IP test/self-hosted deployments.
|
// browser sessions unusable on direct-IP test/self-hosted deployments.
|
||||||
if (domain === "localhost" || /^(?:\d{1,3}\.){3}\d{1,3}$/.test(domain)) return domain;
|
if (domain === "localhost" || /^(?:\d{1,3}\.){3}\d{1,3}$/.test(domain)) return undefined;
|
||||||
if (domain.split(".").length > 2) {
|
if (domain.split(".").length > 2) {
|
||||||
// Subdomain deployment: scope to the registrable parent domain.
|
// Subdomain deployment: scope to the registrable parent domain.
|
||||||
return domain.split(".").slice(1).join(".");
|
return domain.split(".").slice(1).join(".");
|
||||||
|
|||||||
@@ -16,10 +16,11 @@ import { createLogger } from "../../lib/logger";
|
|||||||
const log = createLogger("auth");
|
const log = createLogger("auth");
|
||||||
|
|
||||||
function setSessionCookie(ctr: any, hash: string) {
|
function setSessionCookie(ctr: any, hash: string) {
|
||||||
|
const domain = cookieDomain(DOMAIN);
|
||||||
ctr.cookies.set(
|
ctr.cookies.set(
|
||||||
SESSION_COOKIE,
|
SESSION_COOKIE,
|
||||||
new Cookie(hash, {
|
new Cookie(hash, {
|
||||||
domain: cookieDomain(DOMAIN),
|
...(domain ? { domain } : {}),
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
path: "/",
|
path: "/",
|
||||||
sameSite: "lax",
|
sameSite: "lax",
|
||||||
|
|||||||
Reference in New Issue
Block a user