mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-12-05 08:15:41 -08:00
Sign the macOS app's dashboard frame in by a path prefix, since WebKit gives it no cookie
AI Assisted
This commit is contained in:
1 parent
1d007737b1
commit
98bdbc97c3
10 files changed
+411
-21
No files matched your search
@@ -156,21 +156,24 @@ function noteEvent(dir, event) {
|
||||
|
||||
export function handle(req, res, ctx) {
|
||||
const port = ctx.port();
|
||||
if (!hostOk(req, ctx)) return send(res, 403, "", "text/plain");
|
||||
|
||||
// Before anything reads the path: the macOS app's frame comes in under its
|
||||
// prefix, which comes off here, once.
|
||||
const framed = takeFramePrefix(req, ctx);
|
||||
const url = new URL(req.url, `http://127.0.0.1:${port}`);
|
||||
const pathname = url.pathname;
|
||||
|
||||
if (!hostOk(req, port)) return send(res, 403, "", "text/plain");
|
||||
|
||||
let gated = false;
|
||||
for (const route of ROUTES) {
|
||||
if (!route.open && !gated) {
|
||||
if (!authed(req, ctx)) return send(res, 403, "Open the link the terminal printed.", "text/plain");
|
||||
if (!framed && !authed(req, ctx)) return send(res, 403, "Open the link the terminal printed.", "text/plain");
|
||||
ctx.touch();
|
||||
gated = true;
|
||||
}
|
||||
if (route.path !== undefined ? pathname !== route.path : !route.match(pathname, req)) continue;
|
||||
if (route.method && req.method !== route.method) continue;
|
||||
if (route.origin && !originOk(req, port)) return json(res, 403, { error: "bad origin" });
|
||||
if (route.origin && !originOk(req, ctx)) return json(res, 403, { error: "bad origin" });
|
||||
try {
|
||||
return route.run(req, res, ctx, url);
|
||||
} catch (e) {
|
||||
@@ -203,6 +206,16 @@ function signIn(req, res, ctx, url) {
|
||||
const given = url.pathname.slice(3).replace(/\/$/, "");
|
||||
if (!ctx.token || !timingSafeEq(given, ctx.token)) return send(res, 403, "Not found", "text/plain");
|
||||
ctx.touch();
|
||||
// The macOS app's frame goes under its prefix instead (takeFramePrefix):
|
||||
// WebKit on macOS gives that cross-site frame no cookie, Lax included. A
|
||||
// browser tab that sends `Sec-Fetch-Dest: document` keeps the cookie, so
|
||||
// the prefix stays out of its address bar. A client that sends no such
|
||||
// header gets the prefix, the answer that works for the frame.
|
||||
const key = frameKey(ctx);
|
||||
if (key && req.headers["sec-fetch-dest"] !== "document") {
|
||||
res.writeHead(302, baseHeaders({ location: `/f/${key}/` }));
|
||||
return res.end();
|
||||
}
|
||||
res.writeHead(
|
||||
302,
|
||||
baseHeaders({
|
||||
@@ -223,7 +236,9 @@ function signIn(req, res, ctx, url) {
|
||||
// arrives over plain http, so None; Secure never comes back and every
|
||||
// request after the sign-in is a 403: the app shows "its page did not show
|
||||
// in this window". Lax, without Secure, is kept and sent there (measured in
|
||||
// WebKitGTK 2.52 under both its cookie policies).
|
||||
// WebKitGTK 2.52 under both its cookie policies). macOS WebKit drops even
|
||||
// Lax in a cross-site frame, so the macOS app's frame never gets this
|
||||
// cookie: signIn() sends it under its path prefix instead (takeFramePrefix).
|
||||
function cookieSameSite(ancestor) {
|
||||
if (ancestor === "'none'") return "SameSite=Strict";
|
||||
if (ancestor === "tauri://localhost") return "SameSite=Lax";
|
||||
@@ -1533,7 +1548,7 @@ function uploads(req, res, ctx, url) {
|
||||
});
|
||||
}
|
||||
if (req.method === "DELETE") {
|
||||
if (!originOk(req, ctx.port())) return json(res, 403, { error: "bad origin" });
|
||||
if (!originOk(req, ctx)) return json(res, 403, { error: "bad origin" });
|
||||
const m = UPLOAD_ANY_ROUTE.exec(req.url);
|
||||
const file = m && m[0] === pathname ? uploadPath(ctx, m[1], m[2]) : null;
|
||||
if (!file || !fs.existsSync(file)) return json(res, 404, { error: "not found" });
|
||||
@@ -2106,6 +2121,40 @@ function authed(req, ctx) {
|
||||
return c ? timingSafeEq(c, ctx.token) : false;
|
||||
}
|
||||
|
||||
// The macOS app's frame signs in by a path prefix, not by cookie. The app's
|
||||
// page is tauri://localhost, cross-site to this server, and WebKit gives a
|
||||
// cross-site frame no cookie in any form (its third-party cookie blocking has
|
||||
// no switch). So the sign-in link sends that frame to /f/<key>/ on this same
|
||||
// 127.0.0.1 address, and a request under exactly that prefix is signed in.
|
||||
// The key is a hash of the token, still 128 bits, so the token itself never
|
||||
// sits in the frame's address. Unlike the HttpOnly cookie, the key is
|
||||
// readable by the page's own script (location.pathname), so this relies on
|
||||
// the page's strict CSP keeping any other script out. Off (null) everywhere
|
||||
// else, Windows and Linux included. Not a host name such as <key>.localhost:
|
||||
// macOS tries it on [::1] first, where another process can listen on the
|
||||
// same port and be handed the key.
|
||||
function frameKey(ctx) {
|
||||
if (process.platform !== "darwin" || embedAncestor() !== "tauri://localhost" || !ctx.token) return null;
|
||||
return crypto.createHash("sha256").update(ctx.token).digest("hex").slice(0, 32);
|
||||
}
|
||||
|
||||
// True, with the prefix taken off req.url, for a request under the frame's
|
||||
// prefix. It comes off the raw request line, not the parsed path, so the
|
||||
// routes that refuse anything URL parsing had to fold (`..`) still see it.
|
||||
// Only the first prefix comes off: a second is just a path, and nothing
|
||||
// serves it. The page asks for everything page-relative (no leading slash),
|
||||
// so the same page works at / in a browser tab and under the prefix here.
|
||||
const FRAME_PREFIX = /^\/f\/([^/]*)\//;
|
||||
|
||||
function takeFramePrefix(req, ctx) {
|
||||
const key = frameKey(ctx);
|
||||
if (!key) return false;
|
||||
const m = FRAME_PREFIX.exec(req.url);
|
||||
if (!m || !timingSafeEq(m[1], key)) return false;
|
||||
req.url = req.url.slice(m[0].length - 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The DNS-rebinding defense, and the highest-value check in this file.
|
||||
*
|
||||
@@ -2113,15 +2162,17 @@ function authed(req, ctx) {
|
||||
* 127.0.0.1 and then talk to this server as same-origin. That is not
|
||||
* theoretical: it is the exact shape of Vite's GHSA-vg6x-rcgg-rjx6.
|
||||
*/
|
||||
function hostOk(req, port) {
|
||||
function hostOk(req, ctx) {
|
||||
const host = req.headers.host;
|
||||
if (!host) return false;
|
||||
const port = ctx.port();
|
||||
return host === `127.0.0.1:${port}` || host === `localhost:${port}`;
|
||||
}
|
||||
|
||||
function originOk(req, port) {
|
||||
function originOk(req, ctx) {
|
||||
const origin = req.headers.origin;
|
||||
if (!origin) return true; // same-origin fetches and plain navigations send none
|
||||
const port = ctx.port();
|
||||
return origin === `http://127.0.0.1:${port}` || origin === `http://localhost:${port}`;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user