Let installed apps sign in over https again
The __Host- session cookie name now goes to browsers only. Native clients (no Origin or fetch metadata), including app builds from before the change, get the plain cookie name, still Secure over https, so they find their session after signing in.
This commit is contained in:
+11
-1
@@ -12,8 +12,18 @@ export function isHttps(req) {
|
||||
return forwarded === "https" || Boolean(req.socket?.encrypted);
|
||||
}
|
||||
|
||||
/** Whether the request comes from a browser (they send fetch metadata or an
|
||||
* Origin on a form POST); the native app, curl and peer proxies send neither. */
|
||||
export function isBrowserRequest(req) {
|
||||
return Boolean(req.headers["sec-fetch-site"] || req.headers.origin);
|
||||
}
|
||||
|
||||
/** Browsers over https get the host-locked `__Host-` cookie. Native clients
|
||||
* keep the plain name (still Secure over https): installed app builds from
|
||||
* before the `__Host-` cookie only read that name, and a native client has no
|
||||
* other site sharing its cookie jar, which is what `__Host-` guards against. */
|
||||
export function sessionCookieName(req) {
|
||||
return isHttps(req) ? HOST_SESSION_COOKIE : SESSION_COOKIE;
|
||||
return isHttps(req) && isBrowserRequest(req) ? HOST_SESSION_COOKIE : SESSION_COOKIE;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -98,8 +98,8 @@ test("login: Origin check, safe next, headers and the cookie prefix", async (t)
|
||||
const app = await fetch(proxy.base + "/runs/1", { headers: { Cookie: cookieOf(ok) } });
|
||||
assert.equal(app.headers.get("x-frame-options"), "SAMEORIGIN");
|
||||
assert.match(app.headers.get("content-security-policy"), /frame-ancestors 'self'/);
|
||||
// Over https (Tailscale serve): __Host- prefix, Secure, HSTS.
|
||||
const https = await login(proxy, { "X-Forwarded-Proto": "https" });
|
||||
// A browser over https (Tailscale serve): __Host- prefix, Secure, HSTS.
|
||||
const https = await login(proxy, { "X-Forwarded-Proto": "https", Origin: proxy.base, "Sec-Fetch-Site": "same-origin" });
|
||||
const secure = https.headers.get("set-cookie");
|
||||
assert.match(secure, /^__Host-pipelines_session=v2\./);
|
||||
assert.match(secure, /; Path=\/;/);
|
||||
@@ -109,6 +109,12 @@ test("login: Origin check, safe next, headers and the cookie prefix", async (t)
|
||||
assert.equal(await signedIn(proxy, cookieOf(https)), true);
|
||||
// A native client (no Origin) still signs in.
|
||||
assert.equal((await login(proxy)).status, 303);
|
||||
// Over https a native client keeps the plain name (installed apps from before
|
||||
// the __Host- cookie read only that), still Secure, and it signs in.
|
||||
const native = await login(proxy, { "X-Forwarded-Proto": "https" });
|
||||
assert.match(native.headers.get("set-cookie"), /^pipelines_session=v2\./);
|
||||
assert.match(native.headers.get("set-cookie"), /; Secure/);
|
||||
assert.equal(await signedIn(proxy, cookieOf(native)), true);
|
||||
});
|
||||
|
||||
test("logout is a same-origin POST that revokes the token and closes its sockets", async (t) => {
|
||||
|
||||
@@ -175,7 +175,10 @@ test("origin checks, https detection and cookie naming", () => {
|
||||
assert.equal(sameOriginRequest(req({ "sec-fetch-site": "same-site", origin: "https://pipelines.example:8443" })), false);
|
||||
assert.equal(isHttps(req({ "x-forwarded-proto": "https" })), true);
|
||||
assert.equal(isHttps(req({})), false);
|
||||
assert.equal(sessionCookieName(req({ "x-forwarded-proto": "https" })), "__Host-pipelines_session");
|
||||
assert.equal(sessionCookieName(req({ "x-forwarded-proto": "https", "sec-fetch-site": "same-origin" })), "__Host-pipelines_session");
|
||||
assert.equal(sessionCookieName(req({ "x-forwarded-proto": "https", origin: "https://pipelines.example:8443" })), "__Host-pipelines_session");
|
||||
// A native client over https keeps the plain name older app builds read.
|
||||
assert.equal(sessionCookieName(req({ "x-forwarded-proto": "https" })), "pipelines_session");
|
||||
assert.equal(sessionCookieName(req({})), "pipelines_session");
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user