diff --git a/web/src/webSecurity.js b/web/src/webSecurity.js index 5f3d1b4d..9b10ff2f 100644 --- a/web/src/webSecurity.js +++ b/web/src/webSecurity.js @@ -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; } /** diff --git a/web/test/authProxy.test.mjs b/web/test/authProxy.test.mjs index 88c53ee1..3812dc81 100644 --- a/web/test/authProxy.test.mjs +++ b/web/test/authProxy.test.mjs @@ -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) => { diff --git a/web/test/authStore.test.mjs b/web/test/authStore.test.mjs index be17168e..dc7bf20b 100644 --- a/web/test/authStore.test.mjs +++ b/web/test/authStore.test.mjs @@ -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"); });