A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(editor): project sandbox was lexical, so a link walked out of it
yhhb5842a5309d2
diff --git a/desktop/electron/projectFs.ts b/desktop/electron/projectFs.tsindex e45d64c7a..2764c8c26 100644--- a/desktop/electron/projectFs.ts+++ b/desktop/electron/projectFs.ts@@ -4,12 +4,12 @@* @file Project filesystem access for the Electron main process.** All renderer file I/O funnels through here and is sandboxed to the currently- * open project root — paths that escape the root (via `..` or absolute) are- * refused, so a compromised/buggy renderer can't read or write arbitrary files.- * See RC12 §E7.+ * open project root — paths that escape it, by `..`, by being absolute, or by+ * following a link out, are refused, so neither a compromised renderer nor a+ * project someone else authored can read or write arbitrary files.*/import { readFile, writeFile, readdir, mkdir, rename, cp, stat, rm } from 'node:fs/promises';-import { existsSync } from 'node:fs';+import { existsSync, realpathSync } from 'node:fs';import { randomUUID } from 'node:crypto';import { tmpdir } from 'node:os';import path from 'node:path';@@ -27,17 +27,64 @@ import { META_EXT, isContentDir, isContentFile } from './contentPolicy';export { META_EXT };+/** Whether `abs` is `base` or sits under it, lexically. */+function contains(base: string, abs: string): boolean {+ const rel = path.relative(base, abs);+ return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel));+}++// Roots change once per project open; the candidate side is still resolved live.+const realRootCache = new Map<string, string>();++/** `root` with links resolved — a project may itself live under one (/tmp on macOS). */+function realRoot(root: string): string {+ const key = path.resolve(root);+ const hit = realRootCache.get(key);+ if (hit !== undefined) return hit;+ let real = key;+ try {+ real = realpathSync.native(key);+ } catch {+ // Not created yet (a project being made) — the lexical form is all there is.+ }+ realRootCache.set(key, real);+ return real;+}+/**- * Resolve a project-relative path, refusing anything that escapes `root`.- * Pure (no I/O) — the security boundary for every fs op below.+ * `abs` with links resolved. The leaf often does not exist yet (a write, a mkdir,+ * a rename target), so resolve the deepest ancestor that does and re-attach the+ * rest — that is the part an attacker can have made a link.+ */+function realResolve(abs: string): string {+ const tail: string[] = [];+ let head = abs;+ for (;;) {+ try {+ return path.join(realpathSync.native(head), ...tail);+ } catch {+ const parent = path.dirname(head);+ if (parent === head) return abs;+ tail.unshift(path.basename(head));+ head = parent;+ }+ }+}++/**+ * Resolve a project-relative path, refusing anything that escapes `root` — the+ * security boundary for every fs op below. A link inside the project holds no+ * `..` and points wherever it likes, so both sides are compared with links+ * resolved; racing a swap after the check needs O_NOFOLLOW, which node lacks.*/export function resolveInRoot(root: string, relPath: string): string {const resolved = path.resolve(root, relPath);- const rel = path.relative(root, resolved);- if (rel === '') return resolved; // the root itself- if (rel.startsWith('..') || path.isAbsolute(rel)) {+ if (!contains(root, resolved)) {throw new Error(`path "${relPath}" escapes the project root`);}+ if (!contains(realRoot(root), realResolve(resolved))) {+ throw new Error(`path "${relPath}" escapes the project root through a link`);+ }return resolved;}@@ -85,7 +132,10 @@ export async function openProject(root: string): Promise<OpenedProject> {return { root, manifest, workspace };}-export function readInRoot(root: string, relPath: string): Promise<string> {+/** `async` so a refused path REJECTS. Returning the promise directly let+ * resolveInRoot throw synchronously out of a function typed `Promise`, which no+ * `.catch()` sees — every sibling here rejects. */+export async function readInRoot(root: string, relPath: string): Promise<string> {return readTextInRoot(resolveInRoot(root, relPath));}diff --git a/desktop/tests/project-fs.test.ts b/desktop/tests/project-fs.test.tsindex 8f66c67a1..606dc6829 100644--- a/desktop/tests/project-fs.test.ts+++ b/desktop/tests/project-fs.test.ts@@ -7,7 +7,9 @@* share one in the registry), and the root sandbox refuses escapes.*/import { describe, it, expect, beforeEach, afterEach } from 'vitest';-import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, existsSync, rmSync } from 'node:fs';+import {+ mkdtempSync, mkdirSync, writeFileSync, readFileSync, existsSync, rmSync, symlinkSync,+} from 'node:fs';import { tmpdir } from 'node:os';import path from 'node:path';import {@@ -18,6 +20,8 @@ import {mkdirInRoot,duplicateInRoot,statInRoot,+ readInRoot,+ writeInRoot,readSliceInRoot,searchInRoot,snapshotForTrash,@@ -28,6 +32,12 @@ import { importAssets, createAsset } from '../electron/importAssets';let root: string;const read = (rel: string) => readFileSync(path.join(root, rel), 'utf8');+/** Link a directory. Windows refuses plain symlinks without elevation but allows+ * junctions, which defeat a lexical containment check just the same. */+const linkDir = (target: string, link: string): void => {+ if (process.platform === 'win32') symlinkSync(target, link, 'junction');+ else symlinkSync(target, link, 'dir');+};const meta = (uuid: string, type = 'texture') =>JSON.stringify({ uuid, version: '2.0', type, importer: {} }, null, 2) + '\n';@@ -162,6 +172,71 @@ describe('resolveInRoot', () => {expect(() => resolveInRoot(root, '../secret')).toThrow(/escapes/);expect(() => resolveInRoot(root, '/etc/passwd')).toThrow(/escapes/);});++ it('accepts ordinary paths, existing or not', () => {+ expect(() => resolveInRoot(root, 'assets/hero.png')).not.toThrow();+ expect(() => resolveInRoot(root, 'assets/deep/not/created/yet.txt')).not.toThrow();+ expect(resolveInRoot(root, '')).toBe(path.resolve(root));+ });++ // A project is not trusted input — it can be cloned, unzipped or shared, and a+ // link inside it holds no `..` for a lexical check to catch.+ it('refuses a link that points out of the root', () => {+ const outside = mkdtempSync(path.join(tmpdir(), 'estella-outside-'));+ writeFileSync(path.join(outside, 'secret.txt'), 'PRIVATE');+ try {+ linkDir(outside, path.join(root, 'assets', 'escape'));+ } catch {+ return; // no permission to make one here; the check is exercised below anyway+ }+ expect(() => resolveInRoot(root, 'assets/escape/secret.txt')).toThrow(/link/);+ expect(() => resolveInRoot(root, 'assets/escape')).toThrow(/link/);+ // Reaching THROUGH the link for a path that does not exist yet is a write.+ expect(() => resolveInRoot(root, 'assets/escape/planted.txt')).toThrow(/link/);+ rmSync(outside, { recursive: true, force: true });+ });++ // The door itself, not just the helper: a real read and a real write.+ it('does not read or write through a link, end to end', async () => {+ const outside = mkdtempSync(path.join(tmpdir(), 'estella-outside-'));+ writeFileSync(path.join(outside, 'secret.txt'), 'PRIVATE');+ try {+ linkDir(outside, path.join(root, 'assets', 'escape'));+ } catch {+ return;+ }+ await expect(readInRoot(root, 'assets/escape/secret.txt')).rejects.toThrow(/link/);+ await expect(writeInRoot(root, 'assets/escape/planted.txt', 'x')).rejects.toThrow(/link/);+ expect(existsSync(path.join(outside, 'planted.txt'))).toBe(false);+ rmSync(outside, { recursive: true, force: true });+ });++ // The counterpart: a link that stays inside is ordinary project content.+ it('allows a link that stays within the root', () => {+ mkdirSync(path.join(root, 'assets', 'shared'), { recursive: true });+ writeFileSync(path.join(root, 'assets', 'shared', 'note.txt'), 'ok');+ try {+ linkDir(path.join(root, 'assets', 'shared'), path.join(root, 'assets', 'alias'));+ } catch {+ return;+ }+ expect(() => resolveInRoot(root, 'assets/alias/note.txt')).not.toThrow();+ });++ // A project living under a symlinked path (macOS /tmp, a symlinked projects dir)+ // must not have every one of its own files rejected.+ it('works when the ROOT is itself reached through a link', () => {+ const base = mkdtempSync(path.join(tmpdir(), 'estella-linkroot-'));+ const alias = path.join(base, 'alias');+ try {+ linkDir(root, alias);+ } catch {+ return;+ }+ expect(() => resolveInRoot(alias, 'assets/hero.png')).not.toThrow();+ expect(() => resolveInRoot(alias, '../secret')).toThrow(/escapes/);+ rmSync(base, { recursive: true, force: true });+ });});describe('renameInRoot', () => {diff --git a/tools/check-comment-style.mjs b/tools/check-comment-style.mjsindex 134c79d0d..e284cf5f9 100644--- a/tools/check-comment-style.mjs+++ b/tools/check-comment-style.mjs@@ -112,7 +112,10 @@ function scan(byFile) {if (block.length === 0) return;const prose = block.filter(([, t]) => !NOT_PROSE.test(t));const isHeader = block.some(([, t]) => FILE_HEADER.test(t));- const isDoc = block.some(([, t]) => /^\s*\/\*\*/.test(t));+ // A block edited inside an existing `/** */` shows up in the diff without its+ // opener, so a run of ` * ` continuation lines counts as a doc block too.+ const isDoc = block.some(([, t]) => /^\s*\/\*\*/.test(t))+ || /^\s*\*/.test(block[0][1]);const limit = isDoc ? MAX_DOC_LINES : MAX_INLINE_LINES;if (!isHeader && prose.length > limit) {findings.push({