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.

fix(security): bound audit growth and tail reads

Abdurrahman Fakhruld54e8ac8cfa0
diff --git a/lib/host/audit.test.ts b/lib/host/audit.test.ts
index ed1742e6..1da0b546 100644
--- a/lib/host/audit.test.ts
+++ b/lib/host/audit.test.ts
@@ -182,3 +182,28 @@ describe("test-run guard", () => {
expect(after).not.toContain(marker);
});
});
+
+
+describe("bounded audit storage", () => {
+ it("suppresses auth rate-limit amplification after the bounded window budget", async () => {
+ for (let i = 0; i < 100; i++) await audit({ action: "auth.ratelimited", ip: "203.0.113.1", ok: false });
+ const rows = await readLines();
+ expect(rows.length).toBeLessThanOrEqual(60);
+ });
+
+ it("reads only a bounded tail even when older log bytes are large", async () => {
+ const old = JSON.stringify({ ts: "old", action: "exec.run", detail: "x".repeat(1000) }) + "\n";
+ await fs.writeFile(logFile, old.repeat(400));
+ await audit({ action: "fs.write", detail: "newest" });
+ const rows = await readAuditTail({ limit: 5 });
+ expect(rows[0]?.detail).toBe("newest");
+ expect(rows.length).toBeLessThanOrEqual(5);
+ });
+
+ it("rotates an oversized current log before appending", async () => {
+ await fs.writeFile(logFile, Buffer.alloc(8 * 1024 * 1024, 0x20));
+ await audit({ action: "exec.run", detail: "after-rotation" });
+ expect((await fs.stat(logFile)).size).toBeLessThan(4096);
+ expect((await fs.stat(logFile + ".1")).size).toBe(8 * 1024 * 1024);
+ });
+});
diff --git a/lib/host/audit.ts b/lib/host/audit.ts
index 357be3cd..c86b1008 100644
--- a/lib/host/audit.ts
+++ b/lib/host/audit.ts
@@ -6,7 +6,7 @@
//
// Location: $OS_AUDIT_LOG, else ~/.mso/audit.log. Reads are NOT audited
// (bounded + high-volume); only state-changing actions are.
-import { promises as fs } from "fs";
+import { constants, promises as fs } from "fs";
import os from "os";
import path from "path";
@@ -79,7 +79,7 @@ export async function readAuditTail(opts?: {
limit?: number;
}): Promise<AuditRecord[]> {
const { prefix, limit = 100 } = opts ?? {};
- const raw = await fs.readFile(/* turbopackIgnore: true */ auditPath(), "utf8").catch(() => "");
+ const raw = await readBoundedTail(auditPath()).catch(() => "");
return raw
.split("\n")
.filter(Boolean)
@@ -98,7 +98,7 @@ export async function readAuditTail(opts?: {
(!prefix || e.action.startsWith(prefix)),
),
)
- .slice(-limit)
+ .slice(-Math.max(1, Math.min(500, Math.trunc(limit) || 100)))
.reverse();
}
@@ -107,6 +107,48 @@ export async function readAuditTail(opts?: {
// swallows + logs its own failure), otherwise one bad write would poison every
// subsequent audit call for the lifetime of the process.
let _writeChain: Promise<void> = Promise.resolve();
+const AUDIT_MAX_BYTES = 8 * 1024 * 1024;
+const AUDIT_READ_TAIL_BYTES = 256 * 1024;
+const RATELIMIT_AUDIT_MAX = 60;
+const RATELIMIT_AUDIT_WINDOW_MS = 60_000;
+let ratelimitWindow = 0;
+let ratelimitEntries = 0;
+
+function allowRateLimitAudit(action: AuditAction): boolean {
+ if (action !== "auth.ratelimited") return true;
+ const now = Date.now();
+ if (now - ratelimitWindow >= RATELIMIT_AUDIT_WINDOW_MS) {
+ ratelimitWindow = now;
+ ratelimitEntries = 0;
+ }
+ if (ratelimitEntries >= RATELIMIT_AUDIT_MAX) return false;
+ ratelimitEntries += 1;
+ return true;
+}
+
+async function readBoundedTail(file: string): Promise<string> {
+ let handle;
+ try {
+ handle = await fs.open(file, constants.O_RDONLY | constants.O_NOFOLLOW);
+ const stat = await handle.stat();
+ if (!stat.isFile()) return "";
+ const size = Math.min(stat.size, AUDIT_READ_TAIL_BYTES);
+ const offset = Math.max(0, stat.size - size);
+ const buffer = Buffer.alloc(size);
+ const { bytesRead } = await handle.read(buffer, 0, size, offset);
+ let text = buffer.subarray(0, bytesRead).toString("utf8");
+ if (offset > 0) {
+ const newline = text.indexOf("\n");
+ text = newline >= 0 ? text.slice(newline + 1) : "";
+ }
+ return text;
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return "";
+ throw error;
+ } finally {
+ await handle?.close().catch(() => undefined);
+ }
+}
async function writeLine(line: string): Promise<void> {
// A test run must never append to the OWNER's forensic trail. This is not
@@ -118,7 +160,18 @@ async function writeLine(line: string): Promise<void> {
const file = auditPath();
try {
await fs.mkdir(path.dirname(file), { recursive: true, mode: 0o700 });
- await fs.appendFile(file, line, { mode: 0o600 });
+ const stat = await fs.lstat(file).catch((error: NodeJS.ErrnoException) => {
+ if (error.code === "ENOENT") return null;
+ throw error;
+ });
+ if (stat && (!stat.isFile() || stat.isSymbolicLink())) throw new Error("audit log must be a regular file");
+ if (stat && stat.size + Buffer.byteLength(line) > AUDIT_MAX_BYTES) {
+ const rotated = file + ".1";
+ await fs.rm(rotated, { force: true });
+ await fs.rename(file, rotated);
+ }
+ const handle = await fs.open(file, constants.O_WRONLY | constants.O_APPEND | constants.O_CREAT | constants.O_NOFOLLOW, 0o600);
+ try { await handle.writeFile(line); } finally { await handle.close(); }
} catch (e) {
console.error("[audit] write failed:", e instanceof Error ? e.message : e);
}
@@ -129,6 +182,7 @@ async function writeLine(line: string): Promise<void> {
// and must never break the caller). Callers may fire-and-forget the returned
// promise; ordering across concurrent callers is preserved via _writeChain.
export function audit(entry: AuditEntry): Promise<void> {
+ if (!allowRateLimitAudit(entry.action)) return Promise.resolve();
// Drop `meta` from the serialized line when it's empty so existing greps that
// assume a fixed-shape record don't suddenly see `"meta":{}` everywhere.
const clamped = clampMeta(entry.meta);