Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

8.6 KB
Raw Blame
// A skill folder brought in from outside: SKILL.md and every file beside it, written once into a fresh folder and
// checked by hash whenever it is used, never read or written through a link.
import { createHash } from "node:crypto";
import { chmodSync, closeSync, constants, fstatSync, lstatSync, mkdirSync, openSync, readFileSync, writeFileSync } from "node:fs";
import { join, relative, resolve } from "node:path";
import { z } from "zod";

import { planSkillFolder, SKILL_FOLDER_LIMITS, SKILL_IMPORT_SKIP_REPORT_MAX, skillFilePathProblem } from "../electron/skill-folder-plan.mjs";
import type { SkillImportSummary, SkippedSkillFile } from "../shared/skill-import.ts";
import { parseSkillMd, scanSkillText, SKILL_FILE_MAX_BYTES, type ParsedSkill } from "../shared/skill-md.ts";
import { directoryEntryState } from "./skill-storage.ts";

export { planSkillFolder, SKILL_FOLDER_LIMITS, skillFilePathProblem, unsafeFileNameProblem, type FolderEntry } from "../electron/skill-folder-plan.mjs";
export type { GitHubSkillOrigin } from "../shared/skill-import.ts";

const MAX_WARNINGS = 20;
const SCRIPT = /\.(?:sh|bash|zsh|fish|ps1|psm1|bat|cmd|py|rb|pl|php|lua|js|mjs|cjs|ts)$/i;

export const isSkillScript = (file: { path: string; executable?: boolean }): boolean =>
  Boolean(file.executable) || SCRIPT.test(file.path);

export interface SkillFolderFile {
  path: string;
  content: string | Uint8Array;
  executable?: boolean;
}

export interface PreparedSkillFolder {
  skillMd: string;
  parsed: ParsedSkill;
  /** Beside SKILL.md, in the order they are written. */
  files: Array<{ path: string; content: Buffer; executable: boolean }>;
  warnings: string[];
  skipped: SkippedSkillFile[];
}

const decoder = new TextDecoder("utf-8", { fatal: true });

/** The text of a file, or null for one that is not UTF-8 text. */
export function skillFileText(content: string | Uint8Array): string | null {
  if (typeof content === "string") return content;
  try {
    const text = decoder.decode(content);
    return text.includes("\u0000") ? null : text;
  } catch {
    return null;
  }
}

/** The scan over SKILL.md and every text file beside it (scripts included), which can hold the same instructions. */
export function scanSkillFolder(skillMd: string, files: ReadonlyArray<{ path: string; content: string }>): string[] {
  const all = [...scanSkillText(skillMd), ...files.flatMap((file) => scanSkillText(file.content).map((warning) => `${file.path}: ${warning}`))];
  const unique = [...new Set(all)];
  return unique.length > MAX_WARNINGS ? [...unique.slice(0, MAX_WARNINGS), `${unique.length - MAX_WARNINGS} more warnings in its files`] : unique;
}

/** The storage gate: whoever supplied `files`, only what the plan admits is written. */
export function prepareSkillFolder(files: readonly SkillFolderFile[]): PreparedSkillFolder | { error: string } {
  const entries = files.map((file) => ({ ...file, kind: "file" as const, size: Buffer.byteLength(file.content) }));
  const { take, skipped } = planSkillFolder(entries);
  const skillMdFile = take.find((file) => file.path === "SKILL.md");
  if (!skillMdFile) return { error: "no SKILL.md found at that location" };
  if (skillMdFile.size > SKILL_FILE_MAX_BYTES) return { error: `SKILL.md is larger than ${SKILL_FILE_MAX_BYTES / 1024}KB` };
  const skillMd = skillFileText(skillMdFile.content);
  if (skillMd === null) return { error: "SKILL.md is not UTF-8 text" };
  const parsed = parseSkillMd(skillMd);
  if ("error" in parsed) return parsed;
  const beside = take.filter((file) => file !== skillMdFile)
    .map((file) => ({ path: file.path, content: Buffer.from(file.content), executable: Boolean(file.executable) }));
  const texts = beside.flatMap((file) => {
    const text = skillFileText(file.content);
    return text === null ? [] : [{ path: file.path, content: text }];
  });
  return { skillMd, parsed, files: beside, warnings: scanSkillFolder(skillMd, texts), skipped };
}

export function skillImportSummary(
  name: string,
  files: ReadonlyArray<{ path: string; executable?: boolean }>,
  skipped: readonly SkippedSkillFile[],
): SkillImportSummary {
  return {
    name,
    files: files.length,
    scripts: files.filter(isSkillScript).length,
    skipped: skipped.slice(0, SKILL_IMPORT_SKIP_REPORT_MAX),
    skippedCount: skipped.length,
  };
}

/** Into `folder`, which must not exist yet: each level created as a real folder, never over or through anything. */
export function writeSkillFolder(
  folder: string,
  skillMd: string | Uint8Array,
  files: ReadonlyArray<{ path: string; content: string | Uint8Array; executable?: boolean }>,
): void {
  mkdirSync(folder, { mode: 0o700 });
  // A directory default ACL can ignore the mode passed to writeFileSync and leave the owner-execute bit set.
  const skillMdPath = join(folder, "SKILL.md");
  writeFileSync(skillMdPath, skillMd, { mode: 0o600, flag: "wx" });
  chmodSync(skillMdPath, 0o600);
  for (const file of files) {
    const target = resolve(folder, file.path);
    // Security: refused here too, whatever the caller checked, so no path can leave the folder.
    if (skillFilePathProblem(file.path) || relative(folder, target).split(/[\\/]/)[0] === "..") {
      throw new Error(`refusing to write ${JSON.stringify(file.path)} outside the skill folder`);
    }
    const parts = file.path.split("/");
    let at = folder;
    for (const part of parts.slice(0, -1)) {
      at = join(at, part);
      if (directoryEntryState(at) === "missing") mkdirSync(at, { mode: 0o700 });
      if (directoryEntryState(at) !== "directory") throw new Error(`${file.path} has no real folder to go in`);
    }
    const mode = file.executable ? 0o700 : 0o600;
    writeFileSync(target, file.content, { mode, flag: "wx" });
    chmodSync(target, mode);
  }
}

export function readNoFollow(path: string): Buffer | null {
  let fd: number | null = null;
  try {
    fd = openSync(path, constants.O_RDONLY | (process.platform === "win32" ? 0 : constants.O_NOFOLLOW));
    return fstatSync(fd).isFile() ? readFileSync(fd) : null;
  } catch {
    return null;
  } finally {
    if (fd !== null) {
      try {
        closeSync(fd);
      } catch {}
    }
  }
}

const digests = new Map<string, { key: string; sha256: string }>();

/** One hash over `paths` (the files beside SKILL.md that came in), each with its path, executable bit and bytes; null
 * when one is missing, is not a plain file, or is reached through a link at any level. SKILL.md keeps a hash of its own.
 * Security: only the files that came in are checked, so what a run adds beside them (a cache, a lockfile) leaves the
 * skill usable; a bot can add a file there, which a script may then load, but cannot change one that came in. */
export function skillFolderDigest(dir: string, paths: readonly string[], options: { remember?: boolean } = {}): string | null {
  if (directoryEntryState(dir) !== "directory") return null;
  const folders = new Set<string>();
  const files: Array<{ path: string; full: string; executable: boolean }> = [];
  const stats: string[] = [];
  for (const path of [...paths].sort()) {
    if (skillFilePathProblem(path)) return null;
    const parts = path.split("/");
    for (let depth = 1; depth < parts.length; depth++) {
      const folder = join(dir, ...parts.slice(0, depth));
      if (folders.has(folder)) continue;
      if (directoryEntryState(folder) !== "directory") return null;
      folders.add(folder);
    }
    const full = join(dir, ...parts);
    let info;
    try {
      info = lstatSync(full);
    } catch {
      return null;
    }
    if (!info.isFile()) return null;
    stats.push(`${path}\0${info.ino}\0${info.size}\0${info.mtimeMs}\0${info.ctimeMs}\0${info.mode}`);
    files.push({ path, full, executable: (info.mode & 0o100) !== 0 });
  }
  const key = createHash("sha256").update(stats.join("\n")).digest("hex");
  const known = digests.get(dir);
  if (known?.key === key) return known.sha256;
  const hash = createHash("sha256");
  for (const file of files) {
    const bytes = readNoFollow(file.full);
    if (bytes === null) return null;
    hash.update(`${file.path}\0${file.executable ? "x" : "-"}\0${createHash("sha256").update(bytes).digest("hex")}\n`);
  }
  const sha256 = hash.digest("hex");
  if (options.remember !== false) digests.set(dir, { key, sha256 });
  return sha256;
}

export const githubSkillOriginSchema = z.object({
  owner: z.string().min(1),
  repo: z.string().min(1),
  ref: z.string().min(1),
  commit: z.string().regex(/^[0-9a-f]{40}$/),
  path: z.string(),
});

/** What a manifest records about a folder's files beside SKILL.md: their paths, and skillFolderDigest over them. */
export const skillFolderRecordSchema = {
  files: z.array(z.string()).max(SKILL_FOLDER_LIMITS.files).optional(),
  filesSha256: z.string().regex(/^[a-f0-9]{64}$/).optional(),
};