feat(desktop): runtime-configurable backend server address
The desktop shell is one generic .deb/.AppImage distributed via mca/public_releases, not built per-booth, but the backend origin was baked in at build time (VITE_API_BASE, hardcoded to http://127.0.0.1:3000) — the same installer could never point at a different appliance without a rebuild. Adds ConnectScreen (shown before Login in Tauri when no backend is saved), backed by tauri-plugin-store persisting the operator-entered URL across restarts. CSP's connect-src tightens to 'self' only — all backend traffic already routes through tauri-plugin-http/websocket, which run Rust-side and are outside connect-src's reach anyway — and the real access boundary moves to capabilities/default.json's http:default scope, wildcarded so an operator-chosen host is actually reachable. Adds a "Change server" control in Setup (desktop-only) to repoint an already-configured install. While tracing the desktop auth path for this: tauri-plugin-http's fetch() runs through Rust's reqwest, which keeps its own cookie jar separate from the webview, so document.cookie on tauri://localhost never sees the parking_csrf cookie the server sets (open upstream bug, tauri-apps/tauri#13045/#11518). This means the desktop app has likely been silently sending no CSRF header on every mutation since the shell was first built — pre-existing, independent of this change. Fixed by having sessionView() (routes/auth.ts) also echo the CSRF value in the login/me JSON body; the desktop client stashes it in memory and echoes that instead of reading document.cookie. assertCsrf() itself is untouched. Verified end-to-end against a real LAN-bound dev server: login returns a csrfToken matching the cookie, a mutation using the body-sourced token in X-CSRF-Token succeeds (200), and the same mutation without it still correctly 403s.
This commit is contained in:
@@ -65,7 +65,21 @@ function cleanProfileField(v: string | null | undefined): string | null | undefi
|
||||
|
||||
/** The session shape the SPA bootstraps from: identity + role + its permission
|
||||
* list (so the UI can gate nav/routes) + language. Role NAME is for display; the
|
||||
* permissions are the source of truth. */
|
||||
* permissions are the source of truth.
|
||||
*
|
||||
* `csrf`, when passed, echoes the SAME value already sent as the readable
|
||||
* parking_csrf cookie — not a new secret, just a second channel to learn it.
|
||||
* The desktop shell needs this: tauri-plugin-http's fetch() runs through
|
||||
* Rust's reqwest, which keeps its own cookie jar separate from the webview,
|
||||
* so document.cookie on the tauri://localhost page never sees a cookie set
|
||||
* on a plugin-routed response (open upstream bug, tauri-apps/tauri#13045).
|
||||
* The cookie itself IS still sent back to the server by reqwest on
|
||||
* subsequent requests — only the *client-side read* is broken — so
|
||||
* api.ts's desktop path stashes this body value in memory instead of
|
||||
* reading document.cookie, and echoes it in X-CSRF-Token exactly as the
|
||||
* browser path echoes the cookie. See lib/api.ts and assertCsrf() in
|
||||
* ../auth.ts (unchanged — this never touches verification, only how the
|
||||
* desktop client learns what to send). */
|
||||
function sessionView(
|
||||
db: Db,
|
||||
user: {
|
||||
@@ -78,6 +92,7 @@ function sessionView(
|
||||
fullName?: string | null;
|
||||
email?: string | null;
|
||||
},
|
||||
csrf?: string,
|
||||
) {
|
||||
const role = db.select().from(roles).where(eq(roles.id, user.roleId)).get();
|
||||
const permissions = [...permissionsFor(user.roleId)];
|
||||
@@ -92,6 +107,7 @@ function sessionView(
|
||||
fontScale: user.fontScale,
|
||||
fullName: user.fullName ?? null,
|
||||
email: user.email ?? null,
|
||||
...(csrf ? { csrfToken: csrf } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -126,7 +142,7 @@ export async function authRoutes(app: FastifyInstance, db: Db): Promise<void> {
|
||||
setAuthCookies(reply, token, csrf);
|
||||
// `language` is NOT in the JWT (identity/role only) — it's a mutable preference
|
||||
// read from the DB, so changing it needs no token refresh.
|
||||
return sessionView(db, user);
|
||||
return sessionView(db, user, csrf);
|
||||
});
|
||||
|
||||
app.post("/api/auth/logout", async (_req, reply) => {
|
||||
@@ -146,7 +162,9 @@ export async function authRoutes(app: FastifyInstance, db: Db): Promise<void> {
|
||||
clearAuthCookies(reply);
|
||||
return reply.code(401).send({ error: "session no longer valid" });
|
||||
}
|
||||
return sessionView(db, row);
|
||||
// req.user.csrf is the value bound into the JWT at login (see assertCsrf in
|
||||
// ../auth.ts) — same value as the cookie, re-surfaced for the desktop path.
|
||||
return sessionView(db, row, req.user.csrf);
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user