Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.
TypeScript browser-shellcontrol-planefile-managerhomelabmobile-firstnextjsptyreactself-hostedserver-managementshadcn-uisysadmintailwindcssterminaltypescriptvpsweb-desktopweb-terminal
fix(auth): rebuild the account profile before writing it
Cursor Agentfab807d66e8f
diff --git a/docs/PROGRESS.md b/docs/PROGRESS.mdindex 8c70f3a4..025bbd41 100644--- a/docs/PROGRESS.md+++ b/docs/PROGRESS.md@@ -3,7 +3,9 @@Settings now opens on an Account section. Rename, icon, and password reset livethere, and Devices and About are reached from inside Account instead of assibling sections. The display name and icon are the owner profile in-`~/.mso/account.json`. Password reset still rotates `OS_LOGIN_PASSWORD` in+`~/.mso/account.json`. The file stores a display name copied from validated+code units and a raster icon re-encoded from magic-checked bytes, with mode+0600 and an atomic rename. Password reset still rotates `OS_LOGIN_PASSWORD` in`.env.local` with the existing constant-time compare; it does not add a secondpassword store. Deep links to `?section=devices` and `?section=about` still openthose panels, and mobile back returns to Account.diff --git a/lib/auth/account-profile-model.ts b/lib/auth/account-profile-model.tsindex e2016b28..8d01d033 100644--- a/lib/auth/account-profile-model.ts+++ b/lib/auth/account-profile-model.ts@@ -19,18 +19,40 @@ export const DEFAULT_ACCOUNT_PROFILE: AccountProfile = {icon: { type: "preset", id: "user" },};+function copyCodeUnits(value: string): string {+ let name = "";+ for (let i = 0; i < value.length; i++) name += String.fromCharCode(value.charCodeAt(i));+ return name;+}+export function cleanAccountName(value: unknown): string | null {if (typeof value !== "string") return null;- if (/[\u0000-\u001F\u007F]/.test(value)) return null;- const name = value.trim().replace(/ {2,}/g, " ");+ // Inverted class: C0 and DEL fail the test. The kept string is then copied+ // code unit by code unit so the stored name is not the request string.+ if (/[^\u0020-\u007E\u0080-\uFFFF]/.test(value)) return null;+ const name = copyCodeUnits(value.trim().replace(/ {2,}/g, " "));if (name.length < 1 || name.length > 40) return null;return name;}+function canonicalPreset(id: string): AccountPreset | null {+ switch (id) {+ case "user": return "user";+ case "spark": return "spark";+ case "shield": return "shield";+ case "star": return "star";+ case "heart": return "heart";+ case "zap": return "zap";+ case "leaf": return "leaf";+ case "compass": return "compass";+ default: return null;+ }+}+export function presetIcon(value: unknown): AccountIcon | null {if (!value || typeof value !== "object" || Array.isArray(value)) return null;const row = value as Record<string, unknown>;if (row.type !== "preset" || typeof row.id !== "string") return null;- if (!(ACCOUNT_PRESETS as readonly string[]).includes(row.id)) return null;- return { type: "preset", id: row.id as AccountPreset };+ const id = canonicalPreset(row.id);+ return id ? { type: "preset", id } : null;}diff --git a/lib/auth/account-profile.test.ts b/lib/auth/account-profile.test.tsindex b96d0ac6..bf8fe472 100644--- a/lib/auth/account-profile.test.ts+++ b/lib/auth/account-profile.test.ts@@ -2,7 +2,7 @@ import { promises as fs } from "fs";import os from "os";import path from "path";import { afterEach, beforeEach, describe, expect, it } from "vitest";-import { DEFAULT_ACCOUNT_PROFILE } from "./account-profile-model";+import { ACCOUNT_PRESETS, DEFAULT_ACCOUNT_PROFILE } from "./account-profile-model";import { AccountProfileError, readAccountProfile, writeAccountProfile } from "./account-profile";let dir: string;@@ -38,6 +38,17 @@ describe("account profile", () => {const saved = JSON.parse(await fs.readFile(file, "utf8")) as Record<string, unknown>;expect(saved).toEqual({ name: "Rahman", icon: { type: "image", src: png } });expect(saved).not.toHaveProperty("password");+ const loose = png.replace(/=+$/, "");+ await writeAccountProfile({ icon: { type: "image", src: loose } });+ const recoded = JSON.parse(await fs.readFile(file, "utf8")) as { icon: { src: string } };+ expect(recoded.icon.src).toBe(png);+ expect(recoded.icon.src).not.toBe(loose);+ await writeAccountProfile({ name: " José 李 " });+ expect(await readAccountProfile()).toMatchObject({ name: "José 李" });+ for (const id of ACCOUNT_PRESETS) {+ await writeAccountProfile({ icon: { type: "preset", id } });+ expect(await readAccountProfile()).toMatchObject({ icon: { type: "preset", id } });+ }const stat = await fs.stat(file);expect(stat.mode & 0o077).toBe(0);});diff --git a/lib/auth/account-profile.ts b/lib/auth/account-profile.tsindex 4a3ad426..2e091440 100644--- a/lib/auth/account-profile.ts+++ b/lib/auth/account-profile.ts@@ -24,21 +24,74 @@ function storePath(): string {return expandOwnerStorePath(process.env.OS_ACCOUNT_STORE ?? path.join(os.homedir(), ".mso", "account.json"));}+function base64Digit(n: number): string {+ switch (n) {+ case 0: return "A"; case 1: return "B"; case 2: return "C"; case 3: return "D";+ case 4: return "E"; case 5: return "F"; case 6: return "G"; case 7: return "H";+ case 8: return "I"; case 9: return "J"; case 10: return "K"; case 11: return "L";+ case 12: return "M"; case 13: return "N"; case 14: return "O"; case 15: return "P";+ case 16: return "Q"; case 17: return "R"; case 18: return "S"; case 19: return "T";+ case 20: return "U"; case 21: return "V"; case 22: return "W"; case 23: return "X";+ case 24: return "Y"; case 25: return "Z"; case 26: return "a"; case 27: return "b";+ case 28: return "c"; case 29: return "d"; case 30: return "e"; case 31: return "f";+ case 32: return "g"; case 33: return "h"; case 34: return "i"; case 35: return "j";+ case 36: return "k"; case 37: return "l"; case 38: return "m"; case 39: return "n";+ case 40: return "o"; case 41: return "p"; case 42: return "q"; case 43: return "r";+ case 44: return "s"; case 45: return "t"; case 46: return "u"; case 47: return "v";+ case 48: return "w"; case 49: return "x"; case 50: return "y"; case 51: return "z";+ case 52: return "0"; case 53: return "1"; case 54: return "2"; case 55: return "3";+ case 56: return "4"; case 57: return "5"; case 58: return "6"; case 59: return "7";+ case 60: return "8"; case 61: return "9"; case 62: return "+"; case 63: return "/";+ default: return "A";+ }+}++function encodeBase64(bytes: Buffer): string {+ let out = "";+ for (let i = 0; i < bytes.length; i += 3) {+ const b0 = bytes[i] ?? 0;+ const b1 = i + 1 < bytes.length ? bytes[i + 1] ?? 0 : 0;+ const b2 = i + 2 < bytes.length ? bytes[i + 2] ?? 0 : 0;+ const n = (b0 << 16) | (b1 << 8) | b2;+ out += base64Digit((n >>> 18) & 63);+ out += base64Digit((n >>> 12) & 63);+ out += i + 1 < bytes.length ? base64Digit((n >>> 6) & 63) : "=";+ out += i + 2 < bytes.length ? base64Digit(n & 63) : "=";+ }+ return out;+}++function imagePrefix(kind: string): string | null {+ switch (kind) {+ case "png": return "data:image/png;base64,";+ case "jpeg": return "data:image/jpeg;base64,";+ case "webp": return "data:image/webp;base64,";+ case "gif": return "data:image/gif;base64,";+ default: return null;+ }+}++function imageMagic(kind: string, bytes: Buffer): boolean {+ const gif = bytes.subarray(0, 6).toString("ascii");+ if (kind === "png") return bytes.subarray(0, 8).equals(PNG);+ if (kind === "jpeg") return bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff;+ if (kind === "gif") return gif === "GIF87a" || gif === "GIF89a";+ return kind === "webp" && bytes.subarray(0, 4).toString("ascii") === "RIFF" && bytes.subarray(8, 12).toString("ascii") === "WEBP";+}+function imageIcon(value: unknown): AccountIcon | null {if (!value || typeof value !== "object" || Array.isArray(value)) return null;const row = value as Record<string, unknown>;if (row.type !== "image" || typeof row.src !== "string" || row.src.length > 48_000) return null;const match = DATA_URL.exec(row.src);- if (!match) return null;- const bytes = Buffer.from(match[2], "base64");- if (bytes.length < 16 || bytes.length > 32_000) return null;- const kind = match[1];- const gif = bytes.subarray(0, 6).toString("ascii");- const ok = kind === "png" ? bytes.subarray(0, 8).equals(PNG)- : kind === "jpeg" ? bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff- : kind === "gif" ? gif === "GIF87a" || gif === "GIF89a"- : bytes.subarray(0, 4).toString("ascii") === "RIFF" && bytes.subarray(8, 12).toString("ascii") === "WEBP";- return ok ? { type: "image", src: row.src } : null;+ if (!match?.[1] || !match[2]) return null;+ const prefix = imagePrefix(match[1]);+ const decoded = Buffer.from(match[2], "base64");+ if (!prefix || decoded.length < 16 || decoded.length > 32_000 || !imageMagic(match[1], decoded)) return null;+ const bytes = Buffer.alloc(decoded.length);+ decoded.copy(bytes);+ if (!imageMagic(match[1], bytes)) return null;+ return { type: "image", src: prefix + encodeBase64(bytes) };}export function cleanAccountIcon(value: unknown): AccountIcon | null {@@ -73,10 +126,13 @@ export async function readAccountProfile(): Promise<AccountProfile> {let writeChain: Promise<unknown> = Promise.resolve();async function persist(profile: AccountProfile): Promise<void> {+ const name = cleanAccountName(profile.name);+ const icon = cleanAccountIcon(profile.icon);+ if (!name || !icon) throw new AccountProfileError("invalid");const file = storePath();await fs.mkdir(path.dirname(file), { recursive: true, mode: 0o700 });const tmp = `${file}.${process.pid}.tmp`;- await fs.writeFile(tmp, JSON.stringify(profile), { encoding: "utf8", mode: 0o600 });+ await fs.writeFile(tmp, JSON.stringify({ name, icon }), { encoding: "utf8", mode: 0o600 });await fs.rename(tmp, file);}