Skip to content

armgit / mso

Public Imported from GitHub
0

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.

4.5 KB
Raw Blame
import { constants, promises as fs } from "node:fs";
import os from "node:os";
import path from "node:path";
import { expandOwnerStorePath } from "./owner-store-path.js";

// A kernel-pinned parent keeps lock/file operations on one directory even if its
// pathname is replaced. Linux /proc/self/fd is the supported VPS/WSL runtime.
// Store filenames remain adjacent and compatible with the independent device CLI.
const STORE_ENV = [
  "OS_DEVICE_STORE", "OS_MCP_STORE", "OS_INFRA_STORE", "OS_MEMORY_STORE", "OS_MCP_ACTIVITY_LOG",
  "OS_A2A_STORE", "OS_A2A_TASK_STORE", "OS_A2A_CREDENTIAL_STORE", "OS_A2A_INBOUND_TOKEN_STORE",
  "OS_A2A_LOCAL_AUTH_STORE", "OS_LOCAL_AGENT_MESSAGE_STORE", "OS_LOCAL_AGENT_PRESENCE_STORE",
  "OS_CONFIG_STORE", "OS_PREFS_PATH", "OS_SKILL_MEMORY_STORE", "MSO_SURFACE_APPS_FILE",
  "OS_MANAGED_DOMAINS_STORE", "OS_SHELL_APP_DOMAINS_STORE",
] as const;
const DIRECTORY_ENV = ["OS_TOOL_FORGE_DIR", "OS_AGENT_MEMORY_DIR", "OS_AGENT_SESSIONS_DIR", "OS_PROJECT_AGENT_TASKS_DIR", "OS_AGENT_PROFILES_DIR"] as const;
const expand = (value: string) => path.resolve(expandOwnerStorePath(value));

export async function pinSecurityStorePath(storePath: string, { durable = false }: { durable?: boolean } = {}) {
  if (process.platform !== "linux") throw new Error("Pinned security-store operations require Linux or WSL");
  storePath = expandOwnerStorePath(storePath);
  if (typeof storePath !== "string" || storePath.includes("\0") || !path.isAbsolute(storePath)) throw new Error("Security store needs an absolute path");
  const absolute = path.resolve(storePath);
  const name = path.basename(absolute);
  if (!/^[A-Za-z0-9_.-]{1,221}$/.test(name) || name === "." || name === "..") throw new Error("Invalid security-store filename");
  const allowed = [os.homedir(), ...(process.env.OS_FS_WRITE_ROOTS ?? "").split(":").filter(Boolean),
    ...STORE_ENV.flatMap((key) => process.env[key] ? [path.dirname(expand(process.env[key]!))] : []),
    ...DIRECTORY_ENV.flatMap((key) => process.env[key] ? [expand(process.env[key]!)] : []),
    // Test sandboxes are private mkdtemp directories, never the shared temp root itself.
    ...(process.env.VITEST ? [os.tmpdir()] : [])];
  if (!allowed.some((root) => absolute.startsWith(`${expand(root)}${path.sep}`))) throw new Error("Security store is outside configured owner roots");
  const parent = path.dirname(absolute);
  const directory = await openDirectoryChain(parent, durable);
  try {
    const stat = await directory.stat();
    if (!stat.isDirectory() || stat.uid !== process.getuid?.() || (stat.mode & 0o022)) throw new Error("Security-store parent must be owned and not writable by others");
    const pinnedRoot = `/proc/self/fd/${directory.fd}`;
    // Reject all symlinked ancestor spellings as well as a swapped parent inode.
    if (await fs.realpath(pinnedRoot) !== parent) throw new Error("Security-store parent is not canonical");
    return { file: `${pinnedRoot}/${name}`, directory, absolute };
  } catch (error) {
    await directory.close();
    throw error;
  }
}

// Walk from a stable descriptor, opening one basename at a time with O_NOFOLLOW.
// Creating a missing parent must not follow an attacker-controlled intermediate link.
async function openDirectoryChain(parent: string, durable: boolean) {
  let directory = await fs.open("/", constants.O_RDONLY | constants.O_DIRECTORY);
  try {
    const parts = parent.split(path.sep).filter(Boolean);
    if (parts.length > 128) throw new Error("Security-store path is too deep");
    for (const part of parts) {
      if (!part || part === "." || part === ".." || /[/\\\0]/.test(part)) throw new Error("Invalid store directory component");
      const child = `/proc/self/fd/${directory.fd}/${path.basename(part)}`;
      let next;
      try { next = await fs.open(child, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW); }
      catch (error) {
        if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
        await fs.mkdir(child, { mode: 0o700 }).catch((cause: NodeJS.ErrnoException) => { if (cause.code !== "EEXIST") throw cause; });
        next = await fs.open(child, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
      }
      try {
        // A prior lock/read may have created this child; flush its parent entry even if it already exists.
        if (durable) await directory.sync();
        await directory.close();
      } catch (error) { await next.close().catch(() => undefined); throw error; }
      directory = next;
    }
    return directory;
  } catch (error) { await directory.close().catch(() => undefined); throw error; }
}