Skip to content

armgit / kiln

Public Imported from GitHub
0

Build and revise procedural 3D assets with your coding agent. Local MCP server, CLI and TypeScript engine with rendered review, editable source and GLB export.

2.8 KB
Raw Blame
import test from 'node:test';import assert from 'node:assert/strict';import {zipSync,strToU8} from 'fflate';import {createHash} from 'node:crypto';import {checkedRuntimeArchive,safeRuntimePath,hydrateRuntime} from '../scripts/hydrate-runtime.mjs';import {mkdtemp,mkdir,writeFile,symlink,unlink,readdir,rm} from 'node:fs/promises';import {tmpdir} from 'node:os';import {join} from 'node:path';
const hash=bytes=>createHash('sha256').update(bytes).digest('hex'),pin=bytes=>({schema:'kiln.troy-runtime-pin/1',release:'fixture',base:'https://fixture.test',path:'scene.zip',bytes:bytes.byteLength,sha256:hash(bytes)});
test('hydration checks the exact sealed archive and extracts runtime data only',()=>{const bytes=zipSync({'web/runtime/active/bank.json':strToU8('{}'),'web/assets/a.glb':strToU8('model'),'web/main.mjs':strToU8('old executable'),'sources/a.json':strToU8('{}')});const files=checkedRuntimeArchive(bytes,pin(bytes));assert.deepEqual(Object.keys(files).sort(),['web/assets/a.glb','web/runtime/active/bank.json']);assert.equal(new TextDecoder().decode(files['web/assets/a.glb']),'model');assert.throws(()=>checkedRuntimeArchive(bytes,{...pin(bytes),sha256:'0'.repeat(64)}),/hash/);assert.throws(()=>checkedRuntimeArchive(bytes,{...pin(bytes),bytes:bytes.byteLength+1}),/size/);});
test('hydration rejects traversal, windows paths, encoded paths and symlink records before writing',()=>{for(const path of ['../out','/out','C:/out','web/../out','web/a\\b','web/a:b','web/%2e%2e/out','web/con/file','web/a\u0000b'])assert.throws(()=>safeRuntimePath(path),/Unsafe/);const bytes=zipSync({'web/../escape':strToU8('bad')});assert.throws(()=>checkedRuntimeArchive(bytes,pin(bytes)),/Unsafe/);});
test('hydration imposes decoded per-file and total limits',()=>{const bytes=zipSync({'web/runtime/a.bin':new Uint8Array(1000),'web/runtime/b.bin':new Uint8Array(1000)});assert.throws(()=>checkedRuntimeArchive(bytes,pin(bytes),{maxFileBytes:500}),/limit/);assert.throws(()=>checkedRuntimeArchive(bytes,pin(bytes),{maxTotalBytes:1500}),/limit/);});
test('hydration refuses a symlink ancestor before creating any external directory',async()=>{const root=await mkdtemp(join(tmpdir(),'troy-hydrate-safe-')),outside=await mkdtemp(join(tmpdir(),'troy-hydrate-external-')),bytes=zipSync({'web/runtime/newchild/a.bin':strToU8('fixture')}),record=pin(bytes),target=join(root,'.runtime',record.release+'-'+record.sha256.slice(0,12),'web'),link=join(target,'runtime'),archive=join(root,'scene.zip');try{await mkdir(target,{recursive:true});await symlink(outside,link,process.platform==='win32'?'junction':'dir');await writeFile(archive,bytes);await assert.rejects(hydrateRuntime({root,pin:record,archivePath:archive}),/symlinks/);assert.deepEqual(await readdir(outside),[]);}finally{await unlink(link).catch(()=>{});await rm(root,{recursive:true,force:true});await rm(outside,{recursive:true,force:true});}});