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.

5.6 KB
Raw Blame
// SERVER-ONLY. Streaming multipart/form-data parser for /api/v1/fs/upload.
//
// Why hand-rolled: Request.formData() (undici) buffers EVERY part fully into RAM
// before a handler sees it — a large authenticated upload OOM-kills the host
// process that IS the cockpit. This consumes req.body as a stream, enforces a
// running total-bytes cap (throws UploadTooLargeError BEFORE buffering past it),
// and yields each part as a byte stream so callers spool straight to disk.
//
// Scope is the upload contract only: a text `dest` field + repeated `file` parts
// whose filename carries the relPath. It is not a general RFC 7578 parser.
import { HostError } from "./host-error";

export class UploadTooLargeError extends HostError {
  constructor(limit: number) {
    super(`Upload exceeds limit (${Math.floor(limit / (1024 * 1024))} MiB)`);
    this.name = "UploadTooLargeError";
  }
}

export type MultipartPart = {
  /** form field name, e.g. "dest" or "file" */
  name: string;
  /** filename for file parts (carries relPath); undefined for plain fields */
  filename?: string;
  /** raw body bytes of this part, streamed in order */
  body: AsyncIterable<Uint8Array>;
};

const DASH = 0x2d; // "-"
const CR = 0x0d;
const LF = 0x0a;

export function boundaryFromContentType(ct: string | null): string {
  const m = /;\s*boundary=(?:"([^"]+)"|([^;]+))/i.exec(ct ?? "");
  const b = (m?.[1] ?? m?.[2] ?? "").trim();
  if (!b) throw new HostError("expected multipart/form-data");
  return b;
}

function parseHeaders(block: string): { name: string; filename?: string } {
  let name = "";
  let filename: string | undefined;
  const disp = /content-disposition:[^\r\n]*/i.exec(block)?.[0] ?? "";
  name = /;\s*name="([^"]*)"/i.exec(disp)?.[1] ?? "";
  const fn = /;\s*filename="([^"]*)"/i.exec(disp)?.[1];
  if (fn !== undefined) filename = fn;
  return { name, filename };
}

// Stream parts from a Web ReadableStream multipart body. `limit` is the running
// total-bytes cap across ALL part bodies; crossing it throws UploadTooLargeError.
// Each part's `body` MUST be fully consumed before advancing to the next part.
export async function* parseMultipart(
  stream: ReadableStream<Uint8Array>,
  boundary: string,
  limit: number,
): AsyncGenerator<MultipartPart> {
  const reader = stream.getReader();
  const delim = new TextEncoder().encode(`\r\n--${boundary}`);
  const headerEnd = new Uint8Array([CR, LF, CR, LF]);
  const CRLF = new Uint8Array([CR, LF]);
  // A Buffer, not a bare Uint8Array: Uint8Array#indexOf searches for a single
  // BYTE VALUE, while Buffer#indexOf takes a subsequence — and subarray keeps the
  // Buffer type, so every slice below stays searchable. This replaced a hand-rolled
  // O(n*m) scan and an allocate-and-copy concat.
  let buf: Buffer = Buffer.alloc(0);
  let done = false;
  let total = 0; // running bytes across all part bodies (the cap subject)
  let pulled = 0; // RAW bytes read from the stream — the security backstop

  async function pull(): Promise<boolean> {
    if (done) return false;
    const { value, done: d } = await reader.read();
    if (d) {
      done = true;
      return false;
    }
    // Cap the RAW stream here, not just part bodies: a malicious preamble or
    // header block (no boundary/CRLFCRLF) would otherwise grow `buf` unbounded
    // and OOM the host before any part-body counter runs.
    pulled += value.length;
    if (pulled > limit) throw new UploadTooLargeError(limit);
    buf = Buffer.concat([buf, value]);
    return true;
  }

  // Skip the preamble up to the first boundary line.
  const first = new TextEncoder().encode(`--${boundary}`);
  for (;;) {
    const i = buf.indexOf(first);
    if (i >= 0) {
      buf = buf.subarray(i + first.length);
      break;
    }
    if (!(await pull())) return;
  }

  for (;;) {
    // After a boundary token: "--" + CRLF = end of stream; CRLF = next part.
    while (buf.length < 2) if (!(await pull())) return;
    if (buf[0] === DASH && buf[1] === DASH) return; // closing boundary
    // consume the CRLF after the boundary token
    let nl = buf.indexOf(CRLF);
    while (nl !== 0) {
      if (!(await pull())) return;
      nl = buf.indexOf(CRLF);
    }
    buf = buf.subarray(2);

    // Read this part's header block (terminated by CRLFCRLF).
    let he = buf.indexOf(headerEnd);
    while (he < 0) {
      if (!(await pull())) throw new HostError("malformed multipart header");
      he = buf.indexOf(headerEnd);
    }
    const head = new TextDecoder().decode(buf.subarray(0, he));
    buf = buf.subarray(he + headerEnd.length);
    const { name, filename } = parseHeaders(head);

    // Stream this part's body until the next boundary delimiter.
    let ended = false;
    async function* partBody(): AsyncGenerator<Uint8Array> {
      for (;;) {
        const d = buf.indexOf(delim);
        if (d >= 0) {
          if (d > 0) {
            total += d;
            if (total > limit) throw new UploadTooLargeError(limit);
            yield buf.subarray(0, d);
          }
          buf = buf.subarray(d + delim.length);
          ended = true;
          return;
        }
        // No delimiter yet: emit all but a tail that could hide a partial match.
        const safe = buf.length - delim.length;
        if (safe > 0) {
          total += safe;
          if (total > limit) throw new UploadTooLargeError(limit);
          yield buf.subarray(0, safe);
          buf = buf.subarray(safe);
        }
        if (!(await pull())) throw new HostError("unexpected end of multipart body");
      }
    }

    yield { name, filename, body: partBody() };
    // Defensive: caller must drain the body so `buf` advances past the delimiter.
    if (!ended) throw new HostError("multipart part body not consumed");
  }
}