Open-source Xbox-inspired desktop launcher for Xodus on Linux
TypeScript
feat(runtime): safely materialize verified managed archives
domsvoboda64eeb8136f3f
diff --git a/src/main/runtime-family-archive.test.ts b/src/main/runtime-family-archive.test.tsnew file mode 100644index 0000000..9cdd9d1--- /dev/null+++ b/src/main/runtime-family-archive.test.ts@@ -0,0 +1,164 @@+import { afterEach, expect, it } from 'vitest';+import { promises as fs } from 'node:fs';+import path from 'node:path';+import os from 'node:os';+import { execFile } from 'node:child_process';+import { promisify } from 'node:util';+import { materializeRuntimeArchive } from './runtime-family-archive';+const execute = promisify(execFile), roots: string[] = [];+afterEach(async () => {+ await Promise.all(roots.splice(0).map(root => fs.rm(root, {+ recursive: true, force: true+ })));+});+const generator = String.raw `+import sys,tarfile,io,json,lzma+file,items,format=sys.argv[1:];items=json.loads(items)+if format=='huge':+ root=tarfile.TarInfo('runtime');root.type=tarfile.DIRTYPE;big=tarfile.TarInfo('runtime/big');big.size=8*1024**3+1+ open(file,'wb').write(lzma.compress(root.tobuf()+big.tobuf()+bytes(1024)));sys.exit()+if format=='pax-huge':+ root=tarfile.TarInfo('runtime');root.type=tarfile.DIRTYPE;big=tarfile.TarInfo('pax');big.type=tarfile.XHDTYPE;big.size=2*1024**2+1+ open(file,'wb').write(lzma.compress(root.tobuf()+big.tobuf()+bytes(1024)));sys.exit()+with tarfile.open(file,'w:xz',format=tarfile.GNU_FORMAT if format=='gnu' else tarfile.PAX_FORMAT) as tf:+ for item in items:+ member=tarfile.TarInfo(item['name']);member.type={'dir':tarfile.DIRTYPE,'file':tarfile.REGTYPE,'link':tarfile.SYMTYPE,'hard':tarfile.LNKTYPE,'device':tarfile.CHRTYPE,'sparse':tarfile.GNUTYPE_SPARSE}[item.get('type','file')];member.mode=0o755 if member.isdir() else 0o644+ member.linkname=item.get('target','');data=item.get('data','contents').encode();member.size=len(data) if member.isreg() else 0+ if 'pax' in item: member.pax_headers=item['pax']+ tf.addfile(member,io.BytesIO(data) if member.isreg() else None)+`;+async function fixture(items: unknown[], format = 'pax') {+ const root = await fs.mkdtemp(path.join(os.tmpdir(), 'family-archive-'));+ roots.push(root);+ const archive = path.join(root, 'archive.tar.xz'), stage = path.join(root, 'stage');+ await fs.mkdir(stage, {+ mode: 0o700+ });+ await execute('python3', ['-I', '-c', generator, archive, JSON.stringify([{+ name: 'runtime', type: 'dir'+ }, ...items]), format]);+ return {+ root, archive, stage+ };+}+it.each(['pax', 'gnu'])('materializes %s long names, literal links and copied forward hardlinks without following z:', async (format) => {+ const long = 'runtime/' + 'long'.repeat(40), f = await fixture([{+ name: 'runtime/hard', type: 'hard', target: long+ }, {+ name: long, data: 'actual bytes'+ }, {+ name: 'runtime/link', type: 'link', target: long.slice('runtime/'.length)+ }, {+ name: 'runtime/files/share/default_pfx/dosdevices/z:', type: 'link', target: '/'+ }], format);+ const result = await materializeRuntimeArchive(f.archive, f.stage, 'runtime');+ expect(await fs.readFile(path.join(result, 'hard'), 'utf8')).toBe('actual bytes');+ expect((await fs.stat(path.join(result, 'hard'))).ino).not.toBe((await fs.stat(path.join(f.stage, long))).ino);+ expect(await fs.readlink(path.join(result, 'files/share/default_pfx/dosdevices/z:'))).toBe('/');+});+const attacks: Record<string, unknown[]> = {+ absolute: [{+ name: '/escape'+ }], parent: [{+ name: 'runtime/../escape'+ }], duplicate: [{+ name: 'runtime/a'+ }, {+ name: 'runtime/a'+ }], linkParent: [{+ name: 'runtime/alias', type: 'link', target: 'target'+ }, {+ name: 'runtime/alias/child'+ }, {+ name: 'runtime/target', type: 'dir'+ }], hardEscape: [{+ name: 'runtime/hard', type: 'hard', target: '../escape'+ }], hardCycle: [{+ name: 'runtime/a', type: 'hard', target: 'runtime/b'+ }, {+ name: 'runtime/b', type: 'hard', target: 'runtime/a'+ }], wrongHardType: [{+ name: 'runtime/a', type: 'dir'+ }, {+ name: 'runtime/b', type: 'hard', target: 'runtime/a'+ }], linkCycle: [{+ name: 'runtime/a', type: 'link', target: 'b'+ }, {+ name: 'runtime/b', type: 'link', target: 'a'+ }], externalLink: [{+ name: 'runtime/a', type: 'link', target: '/tmp'+ }], zTraversal: [{+ name: 'runtime/files/share/default_pfx/dosdevices/z:', type: 'link', target: '/'+ }, {+ name: 'runtime/b', type: 'link', target: 'files/share/default_pfx/dosdevices/z:/tmp'+ }], device: [{+ name: 'runtime/device', type: 'device'+ }], backslash: [{+ name: 'runtime/one\\two'+ }], paxTraversal: [{+ name: 'safe', pax: {+ path: 'runtime/../escape'+ }+ }], paxNull: [{+ name: 'safe', pax: {+ path: 'runtime/bad\u0000name'+ }+ }], deep: [{+ name: 'runtime/' + Array(65).fill('d').join('/')+ }], dangling: [{+ name: 'runtime/link', type: 'link', target: 'missing'+ }]+};+it.each(Object.entries(attacks))('rejects %s before creating payload files', async (_name, items) => {+ const f = await fixture(items);+ await expect(materializeRuntimeArchive(f.archive, f.stage, 'runtime')).rejects.toThrow();+ expect(await fs.readdir(f.stage)).toEqual([]);+});+it.each(['huge', 'pax-huge'])('bounds declared %s metadata/content before writes', async (format) => {+ const f = await fixture([], format);+ await expect(materializeRuntimeArchive(f.archive, f.stage, 'runtime')).rejects.toThrow();+ expect(await fs.readdir(f.stage)).toEqual([]);+});+it('rejects a staging symlink without writing its target', async () => {+ const f = await fixture([{+ name: 'runtime/a'+ }]), alias = path.join(f.root, 'alias');+ await fs.symlink(f.stage, alias);+ await expect(materializeRuntimeArchive(f.archive, alias, 'runtime')).rejects.toThrow();+ expect(await fs.readdir(f.stage)).toEqual([]);+});+it('bounds decoded extension metadata and padding independently of exposed member bytes', async () => {+ const f = await fixture(Array.from({+ length: 12+ }, (_, i) => ({+ name: 'runtime/f' + i, pax: {+ comment: 'metadata '.repeat(50)+ }, data: 'x'+ })));+ await expect(materializeRuntimeArchive(f.archive, f.stage, 'runtime', 4096)).rejects.toThrow();+ expect(await fs.readdir(f.stage)).toEqual([]);+});+it('rejects a symlink path that tries to traverse a regular file through parent syntax before writes', async () => {+ const f = await fixture([{+ name: 'runtime/file'+ }, {+ name: 'runtime/target', type: 'dir'+ }, {+ name: 'runtime/link', type: 'link', target: 'file/../target'+ }]);+ await expect(materializeRuntimeArchive(f.archive, f.stage, 'runtime')).rejects.toThrow();+ expect(await fs.readdir(f.stage)).toEqual([]);+});+it('reports a missing Python prerequisite clearly without exposing an interpreter command', async () => {+ const f = await fixture([{+ name: 'runtime/a'+ }]);+ const previous = process.env.PATH;+ try {+ process.env.PATH = f.stage;+ await expect(materializeRuntimeArchive(f.archive, f.stage, 'runtime')).rejects.toThrow('Python 3 is required');+ }+ finally {+ process.env.PATH = previous;+ }+});diff --git a/src/main/runtime-family-archive.ts b/src/main/runtime-family-archive.tsnew file mode 100644index 0000000..51e66fb--- /dev/null+++ b/src/main/runtime-family-archive.ts@@ -0,0 +1,143 @@+import { execFile } from 'node:child_process';+import { promises as fs } from 'node:fs';+import path from 'node:path';+import { promisify } from 'node:util';+const execute = promisify(execFile);+// Python is already required by Proton. Isolated stdlib parsing avoids tar+// text-listing ambiguities and never follows archive links while writing.+const extractor = String.raw `+import os,sys,tarfile,posixpath,shutil,lzma,resource+resource.setrlimit(resource.RLIMIT_AS,(1024**3,1024**3))+class BoundedInput:+ def __init__(self,source): self.source=source+ def read(self,count=-1):+ if count<0 or count>2*1024*1024 or self.source.tell()+count>budget: raise ValueError('Archive decoded metadata exceeds limit')+ return self.source.read(count)+ def seek(self,offset,whence=0):+ if whence not in (0,1): raise ValueError('Unsupported archive seek')+ position=offset+(self.source.tell() if whence==1 else 0)+ if position<0 or position>budget: raise ValueError('Archive decoded position exceeds limit')+ return self.source.seek(offset,whence)+ def tell(self): return self.source.tell()+archive,stage,top,maximum=sys.argv[1:];budget=int(maximum)+if os.path.islink(stage) or not os.path.isdir(stage) or os.listdir(stage): raise ValueError('Owned empty staging directory required')+seen={}; total=0; links={}; directories=[]+def name(value):+ if not isinstance(value,str) or not value or len(value)>4096 or '\\' in value or any(ord(c)<32 or ord(c)==127 for c in value) or value.startswith('/'): raise ValueError('Unsafe archive path')+ parts=value.rstrip('/').split('/')+ if any(p in ('','.','..') for p in parts) or len(parts)>64 or parts[0]!=top: raise ValueError('Archive leaves selected runtime root')+ return '/'.join(parts)+with lzma.open(archive,'rb') as decoded, tarfile.open(fileobj=BoundedInput(decoded),mode='r:') as tf:+ for member in tf:+ key=name(member.name)+ if key in seen or len(seen)>=200000: raise ValueError('Duplicate or excessive archive entries')+ if member.type not in (tarfile.REGTYPE,tarfile.AREGTYPE,tarfile.DIRTYPE,tarfile.SYMTYPE,tarfile.LNKTYPE): raise ValueError('Unsupported archive entry type')+ if member.size<0 or member.size>8*1024**3: raise ValueError('Archive entry exceeds limit')+ total+=member.size+ if total>budget: raise ValueError('Expanded runtime exceeds limit')+ seen[key]=member+ if member.issym() or member.islnk(): links[key]=member+ if top not in seen or not seen[top].isdir(): raise ValueError('Missing runtime root directory')+ for key,member in seen.items():+ parent=posixpath.dirname(key)+ while parent:+ if parent in links: raise ValueError('Archive writes through a link')+ if parent in seen and not seen[parent].isdir(): raise ValueError('Archive parent is not a directory')+ parent=posixpath.dirname(parent)+ for key,member in links.items():+ target=member.linkname+ if member.islnk():+ target=name(target)+ trail=set()+ while target in links and links[target].islnk():+ if target in trail: raise ValueError('Cyclic archive hardlink')+ trail.add(target);target=name(links[target].linkname)+ if target not in seen or not seen[target].isreg(): raise ValueError('Hardlink must resolve to an archived regular file')+ total+=seen[target].size+ if total>budget: raise ValueError('Expanded hardlink copies exceed limit')+ else:+ special=key==top+'/files/share/default_pfx/dosdevices/z:' and target=='/'+ if not target or '\\' in target or any(ord(c)<32 or ord(c)==127 for c in target): raise ValueError('Unsafe link target')+ resolved=posixpath.normpath(posixpath.join(posixpath.dirname(key),target))+ if not special and (target.startswith('/') or not resolved.startswith(top+'/')): raise ValueError('Archive link escapes runtime')+ # Resolve every symlink chain, including links in intermediate directories,+ # before creating even the first payload file. z: is never followed.+ inferred=set()+ for key in seen:+ parent=posixpath.dirname(key)+ while parent: inferred.add(parent);parent=posixpath.dirname(parent)+ def resolve_virtual(key,trail):+ if key in trail: raise ValueError('Cyclic archive link')+ parts=key.split('/')+ for length in range(1,len(parts)+1):+ prefix='/'.join(parts[:length])+ if prefix in seen and not seen[prefix].isdir() and prefix not in links and length<len(parts): raise ValueError('Archive link traverses a non-directory')+ if prefix in links:+ item=links[prefix]+ if prefix==top+'/files/share/default_pfx/dosdevices/z:' and item.linkname=='/': raise ValueError('Archive link follows external template mapping')+ target=name(item.linkname) if item.islnk() else posixpath.normpath(posixpath.join(posixpath.dirname(prefix),item.linkname))+ if not target.startswith(top+'/'): raise ValueError('Archive link escapes runtime')+ if length<len(parts): target=posixpath.join(target,*parts[length:])+ return resolve_virtual(target,trail|{key})+ if key not in seen and key not in inferred: raise ValueError('Dangling archive link')+ return key+ for key,item in links.items():+ if key==top+'/files/share/default_pfx/dosdevices/z:' and item.linkname=='/': continue+ if item.issym():+ stack=[]+ for part in posixpath.join(posixpath.dirname(key),item.linkname).split('/'):+ if part=='..':+ previous=resolve_virtual('/'.join(stack),{key})+ if previous in seen and not seen[previous].isdir(): raise ValueError('Archive parent traversal crosses a non-directory')+ stack.pop()+ elif part not in ('','.'): stack.append(part)+ resolve_virtual(key,set())+ # Only after every header has passed, materialize files into private staging.+ for key,member in seen.items():+ destination=os.path.join(stage,*key.split('/'))+ if member.isdir(): os.makedirs(destination,mode=0o700,exist_ok=True);directories.append((destination,member.mode&0o777))+ elif member.isreg():+ os.makedirs(os.path.dirname(destination),mode=0o700,exist_ok=True)+ fd=os.open(destination,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,member.mode&0o777)+ with os.fdopen(fd,'wb') as out,tf.extractfile(member) as data: shutil.copyfileobj(data,out,1024*1024)+ for key,member in links.items():+ destination=os.path.join(stage,*key.split('/'));os.makedirs(os.path.dirname(destination),mode=0o700,exist_ok=True)+ if member.islnk():+ target=name(member.linkname)+ while target in links and links[target].islnk(): target=name(links[target].linkname)+ # Copy bytes instead of creating filesystem hardlinks.+ with open(os.path.join(stage,*target.split('/')),'rb') as source:+ fd=os.open(destination,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,member.mode&0o777)+ with os.fdopen(fd,'wb') as out: shutil.copyfileobj(source,out,1024*1024)+ else: os.symlink(member.linkname,destination)+ for destination,mode in sorted(directories,key=lambda x:len(x[0]),reverse=True): os.chmod(destination,mode)+print('Runtime archive materialized safely')+`;+export async function materializeRuntimeArchive(archive: string, stage: string, top: string, maximumExpandedBytes = 32 * 1024 ** 3): Promise<string> {+ if (!Number.isSafeInteger(maximumExpandedBytes) || maximumExpandedBytes <= 0 || maximumExpandedBytes > 32 * 1024 ** 3)+ throw Error('Invalid runtime expansion limit');+ if (!/^[A-Za-z0-9][A-Za-z0-9._-]{0,199}$/.test(top) || top.includes('..'))+ throw Error('Invalid runtime archive root');+ if (!(await fs.lstat(stage)).isDirectory() || (await fs.readdir(stage)).length)+ throw Error('Owned empty runtime staging directory required');+ try {+ await execute('python3', ['-I', '-c', extractor, archive, stage, top, String(maximumExpandedBytes)], {+ timeout: 600000, maxBuffer: 1024 * 1024+ });+ }+ catch (error) {+ const failure = error as NodeJS.ErrnoException & {+ stderr?: string;+ killed?: boolean;+ };+ if (failure.code === 'ENOENT')+ throw Error('Python 3 is required to install managed runtimes.');+ if (failure.killed)+ throw Error('The runtime archive could not be unpacked within the allowed time.');+ const reason = failure.stderr?.trim().split('\n').at(-1)?.slice(0, 240);+ throw new Error('The runtime archive failed safety checks.' + (reason ? ' ' + reason : ''), {+ cause: error+ });+ }+ return path.join(stage, top);+}