Skip to content

armgit / kokon

Public Imported from GitHub
0

Edit photos by describing the change, on your own GPU. A free, open-source alternative to Nano Banana and ChatGPT image editing: photos stay on your computer, no daily limit. One installer for Windows, Mac and Linux sets up the best open model (Qwen-Image 2.1). MCP server for AI agents.

4.6 KB
Raw Blame
// Path policy for MCP clients. An agent may only read images from — and write images
// to — the project folder (MCP working directory), the app's output folder, and any
// folders listed in QLS_MCP_ALLOWED_DIRS (semicolon separated). The service validates
// again on its side (UNC/device paths, traversal, no overwrite). Windows paths compare
// case-insensitively and get the drive/stream/reserved-name checks; on macOS `\` is still
// read as a separator, so agents sending Windows-style relative paths behave the same.

import fs from "node:fs";
import path from "node:path";

const WIN = process.platform === "win32";
const p = WIN ? path.win32 : path.posix;
const SEP = p.sep;
const fold = (x: string) => (WIN ? x.toLowerCase() : x);

export const INPUT_EXT = [".png", ".jpg", ".jpeg", ".webp", ".bmp", ".gif", ".tif", ".tiff", ".heic", ".heif"];
export const OUTPUT_EXT = [".png", ".jpg", ".jpeg", ".webp"];
export const MAX_INPUT_BYTES = 200 * 1024 * 1024;

export class PathError extends Error {}

/** How to allow another folder (#39). */
export const ALLOW_HINT = "To allow it, add the folder to QLS_MCP_ALLOWED_DIRS (separated by semicolons) in the env of the MCP client's config, then restart the client.";

function realish(target: string): string {
  // Resolve symlinks/junctions on the deepest existing ancestor.
  let cur = target;
  const rest: string[] = [];
  while (!fs.existsSync(cur)) {
    const parent = p.dirname(cur);
    if (parent === cur) return target;
    rest.unshift(p.basename(cur));
    cur = parent;
  }
  let real = cur;
  try {
    real = fs.realpathSync.native(cur);
  } catch {
    /* keep lexical */
  }
  return p.join(real, ...rest);
}

export function isWithin(target: string, root: string): boolean {
  const t = fold(realish(p.resolve(target)));
  const r = fold(realish(p.resolve(root))).replace(/[\\/]+$/, "");
  return t === r || t.startsWith(r + SEP);
}

/** Lexical checks shared by inputs and outputs; returns an absolute normalized path. */
export function normalize(raw: string, cwd: string): string {
  const s = (raw ?? "").trim();
  if (!s) throw new PathError("The path is empty.");
  if (/[\u0000-\u001f]/.test(s)) throw new PathError("The path contains invalid characters.");
  if (s.startsWith("\\\\") || s.startsWith("//")) throw new PathError("Network and device paths are not allowed.");
  if (!WIN) return p.resolve(cwd, s.replace(/\\/g, "/"));
  if (/^[a-zA-Z]:(?![\\/])/.test(s)) throw new PathError("Drive-relative paths (like C:file.png) are not allowed.");
  const abs = p.resolve(cwd, s.replace(/\//g, "\\"));
  const parts = abs.slice(3).split("\\");
  for (const part of parts) {
    if (part.includes(":")) throw new PathError("Alternate data streams are not allowed.");
    if (/^(con|prn|aux|nul|com\d|lpt\d)(\..*)?$/i.test(part)) throw new PathError(`'${part}' is a reserved Windows name.`);
  }
  return abs;
}

export function checkInput(raw: string, cwd: string, roots: string[]): string {
  const abs = normalize(raw, cwd);
  if (!INPUT_EXT.includes(p.extname(abs).toLowerCase())) throw new PathError("Only PNG, JPEG, WebP, BMP, GIF, TIFF and HEIC images are supported.");
  if (!roots.some((r) => isWithin(abs, r))) throw new PathError(`${abs} is outside the allowed folders (${roots.join("; ")}). ${ALLOW_HINT}`);
  let st: fs.Stats;
  try {
    st = fs.statSync(abs);
  } catch {
    throw new PathError(`File not found: ${abs}`);
  }
  if (!st.isFile()) throw new PathError(`${abs} is not a file.`);
  if (st.size > MAX_INPUT_BYTES) throw new PathError("The image file is larger than 200 MB.");
  return abs;
}

export function checkOutput(raw: string, cwd: string, roots: string[]): string {
  let abs = normalize(raw, cwd);
  const ext = p.extname(abs).toLowerCase();
  if (!ext) abs += ".png";
  else if (!OUTPUT_EXT.includes(ext)) throw new PathError("output_path must end in .png, .jpg or .webp.");
  if (!roots.some((r) => isWithin(abs, r))) throw new PathError(`${abs} is outside the allowed output folders (${roots.join("; ")}). ${ALLOW_HINT}`);
  return abs;
}

/** The workspace to send with a job: the service writes outputs only to its own output folder
 * and the workspace, so an output in a QLS_MCP_ALLOWED_DIRS folder sends that folder (#39). */
export function jobWorkspace(out: string | undefined, cwd: string, roots: string[]): string {
  if (!out || isWithin(out, cwd)) return cwd;
  return roots.find((r) => isWithin(out, r)) ?? cwd;
}

export function allowedRoots(cwd: string, outputDir: string | null): string[] {
  const extra = (process.env.QLS_MCP_ALLOWED_DIRS ?? "")
    .split(";")
    .map((x) => x.trim())
    .filter(Boolean);
  return [cwd, ...(outputDir ? [outputDir] : []), ...extra].map((r) => p.resolve(r));
}