Open-source Xbox-inspired desktop launcher for Xodus on Linux
TypeScript
[source-only] fix(dlc): support layout-declared SRC content metadata (#383)
Nexus Maintainerf3e8c74ef752
diff --git a/src/main/game-content.test.ts b/src/main/game-content.test.tsindex 872abf6..8995d47 100644--- a/src/main/game-content.test.ts+++ b/src/main/game-content.test.ts@@ -246,6 +246,81 @@ it('handles case-insensitive SRC metadata with a wildcard root layout and safe rexpect(await fs.readFile(path.join(f.directory, 'MicrosoftGame.config'), 'utf8')).toBe(childXml());});+async function destinationConfigFixture() {+ const f = await installFixture();+ await fs.mkdir(path.join(f.directory, 'Assets'));+ await fs.rename(path.join(f.directory, 'logo.PNG'), path.join(f.directory, 'Assets', 'logo.PNG'));+ await fs.mkdir(path.join(f.directory, 'SRC'));+ await fs.rename(path.join(f.directory, 'MicrosoftGame.Config'), path.join(f.directory, 'SRC', 'MicrosoftGame.config'));+ await fs.writeFile(path.join(f.directory, `layout_${GUID}.xml`), '<Package><Chunk><FileGroup DestinationPath="\\SRC" SourcePath="." Include="*.*"/><FileGroup DestinationPath="\\Assets" SourcePath=".\\Assets" Include="*.*"/></Chunk></Package>');+ return f;+}+async function snapshotTree(directory: string): Promise<string[]> {+ const paths = (await fs.readdir(directory, { recursive: true }) as string[]).sort(), rows: string[] = [];+ for (const relative of paths) {+ const file = path.join(directory, relative), stat = await fs.lstat(file);+ rows.push(`${relative}|${stat.isDirectory() ? 'd' : stat.isSymbolicLink() ? 'l' : await fs.readFile(file, 'utf8')}`);+ }+ return rows;+}+it('installs the actual destination-coordinate SRC layout and preserves the exact declared config (#383)', async () => {+ const f = await destinationConfigFixture();+ await f.manager.complete(f.game, f.selected);+ expect(await fs.readFile(path.join(f.directory, 'MicrosoftGame.config'), 'utf8')).toBe(childXml());+ expect(await fs.readFile(path.join(f.directory, 'SRC', 'MicrosoftGame.config'), 'utf8')).toBe(childXml());+ expect(await f.manager.roots(f.game)).toEqual([f.directory]);+ await f.manager.complete(f.game, f.selected);+ expect(await f.manager.roots(f.game)).toEqual([f.directory]);+ await fs.writeFile(path.join(f.directory, 'SRC', 'MicrosoftGame.config'), childXml(PAID));+ expect(await f.manager.roots(f.game)).toEqual([]);+});+it('accepts case-insensitive destination metadata without rewriting the declared layout (#383)', async () => {+ const f = await destinationConfigFixture();+ await fs.rename(path.join(f.directory, 'SRC'), path.join(f.directory, 'src'));+ await fs.rename(path.join(f.directory, 'src', 'MicrosoftGame.config'), path.join(f.directory, 'src', 'MICROSOFTGAME.CONFIG'));+ await fs.writeFile(path.join(f.directory, `layout_${GUID}.xml`), '<Package><Chunk><FileGroup DestinationPath="\\sRc" SourcePath="." Include="*"/><FileGroup DestinationPath="\\Assets" SourcePath=".\\Assets" Include="*.*"/></Chunk></Package>');+ await f.manager.complete(f.game, f.selected);+ expect(await fs.readFile(path.join(f.directory, 'MicrosoftGame.config'), 'utf8')).toBe(childXml());+ expect(await fs.readFile(path.join(f.directory, 'src', 'MICROSOFTGAME.CONFIG'), 'utf8')).toBe(childXml());+ expect(await f.manager.roots(f.game)).toEqual([f.directory]);+});+it('rejects ambiguous, excluded or unsafe destination layouts without mutating content (#383)', async () => {+ const layout = (body: string) => `<Package><Chunk>${body}</Chunk></Package>`;+ const assets = '<FileGroup DestinationPath="\\Assets" SourcePath=".\\Assets" Include="*.*"/>';+ const reasons: Record<string, string> = {+ 'ambiguous-mapping': 'unambiguously mapped', exclude: 'unambiguously mapped', identity: 'does not match', dependency: 'does not match',+ conflict: 'Conflicting root and SRC', 'source-traversal': 'Unsupported content layout path', 'destination-traversal': 'Unsupported content layout path',+ drive: 'Unsupported content layout path', unc: 'Unsupported content layout path', control: 'Unsupported content layout path',+ symlink: 'Invalid content payload directory', payload: 'declared by the content layout is missing', 'payload-ambiguous': 'Missing or ambiguous content payload directory',+ };+ for (const [failure, reason] of Object.entries(reasons)) {+ const f = await destinationConfigFixture();+ if (failure === 'ambiguous-mapping') await fs.writeFile(path.join(f.directory, `layout_${GUID}.xml`), layout('<FileGroup DestinationPath="\\SRC" SourcePath="." Include="*.*"/><FileGroup DestinationPath="." SourcePath="SRC" Include="MicrosoftGame.config"/>' + assets));+ if (failure === 'exclude') await fs.writeFile(path.join(f.directory, `layout_${GUID}.xml`), layout('<FileGroup DestinationPath="\\SRC" SourcePath="." Include="*.*" Exclude="MicrosoftGame.config"/>' + assets));+ if (failure === 'identity') await fs.writeFile(path.join(f.directory, 'SRC', 'MicrosoftGame.config'), childXml(PAID));+ if (failure === 'dependency') await fs.writeFile(path.join(f.directory, 'appxmanifest.xml'), appxXml.replace('Example.Base', 'Example.Other'));+ if (failure === 'conflict') await fs.writeFile(path.join(f.directory, 'MicrosoftGame.config'), childXml(PAID));+ if (failure === 'source-traversal') await fs.writeFile(path.join(f.directory, `layout_${GUID}.xml`), layout('<FileGroup DestinationPath="\\SRC" SourcePath="..\\outside" Include="*.*"/>' + assets));+ if (failure === 'destination-traversal') await fs.writeFile(path.join(f.directory, `layout_${GUID}.xml`), layout('<FileGroup DestinationPath="\\..\\outside" SourcePath="." Include="*.*"/>' + assets));+ if (failure === 'drive') await fs.writeFile(path.join(f.directory, `layout_${GUID}.xml`), layout('<FileGroup DestinationPath="C:\\SRC" SourcePath="." Include="*.*"/>' + assets));+ if (failure === 'unc') await fs.writeFile(path.join(f.directory, `layout_${GUID}.xml`), layout('<FileGroup DestinationPath="\\\\server\\share" SourcePath="." Include="*.*"/>' + assets));+ if (failure === 'control') await fs.writeFile(path.join(f.directory, `layout_${GUID}.xml`), layout('<FileGroup DestinationPath="\\SRC" SourcePath="." Include="*.*"/>' + assets));+ if (failure === 'symlink') {+ await fs.rename(path.join(f.directory, 'Assets'), path.join(f.root, 'outside-assets'));+ await fs.symlink(path.join(f.root, 'outside-assets'), path.join(f.directory, 'Assets'));+ }+ if (failure === 'payload') await fs.unlink(path.join(f.directory, 'Assets', 'logo.PNG'));+ if (failure === 'payload-ambiguous') {+ await fs.mkdir(path.join(f.directory, 'assets'));+ await fs.writeFile(path.join(f.directory, 'assets', 'logo.PNG'), 'conflicting payload');+ }+ const before = await snapshotTree(f.directory);+ await expect(f.manager.complete(f.game, f.selected)).rejects.toThrow(reason);+ expect(await snapshotTree(f.directory)).toEqual(before);+ expect(await f.manager.roots(f.game)).toEqual([]);+ }+});+async function installedWithoutDeclaredContent() {const f = await installFixture();diff --git a/src/main/game-content.ts b/src/main/game-content.tsindex beab459..4b362f0 100644--- a/src/main/game-content.ts+++ b/src/main/game-content.ts@@ -59,6 +59,60 @@ async function contentConfiguration(directory: string): Promise<ContentConfigurafunction contentPattern(pattern: string): RegExp {return new RegExp('^' + pattern.replace(/[.+^${}()|[\]\\]/g, '\\$&').replaceAll('*', '.*').replaceAll('?', '.') + '$', 'i');}+type LayoutCoordinate = 'source' | 'destination';+interface FileGroupLayout {+ group: XmlNode;+ source: string[]; destination: string[]; prefix: string[]; pattern: string;+}+/**+ * Package layout paths are relative to the package root. A single leading+ * separator still denotes that package root, never the host filesystem or a+ * network share, so it is normalized away instead of being opened as `/SRC`.+ */+function layoutParts(value: string, optional = false): string[] {+ if (!optional && !value) throw Error('Unsupported content layout path.');+ if (/^[\\/]{2}/.test(value) || /[:\x00-\x1f]/.test(value)) throw Error('Unsupported content layout path.');+ const parts = value.split(/[\\/]+/).filter(part => part && part !== '.');+ if (parts.some(part => part === '..')) throw Error('Unsupported content layout path.');+ return parts;+}+function layoutGroups(groups: XmlNode[]): FileGroupLayout[] {+ return groups.map(group => {+ const source = layoutParts(group.attributes.SourcePath ?? '');+ const include = layoutParts(group.attributes.Include ?? '');+ const destination = layoutParts(group.attributes.DestinationPath ?? '', true);+ const pattern = include.at(-1), prefix = include.slice(0, -1);+ if (!pattern || /[*?]/.test(source.join('/')) || prefix.some(part => /[*?]/.test(part)) || destination.some(part => /[*?]/.test(part)))+ throw Error('Unsupported content layout wildcard.');+ return { group, source, destination, prefix, pattern };+ });+}+function layoutExcluded(layout: FileGroupLayout, relative: string[]): boolean {+ const exclude = layout.group.attributes.Exclude;+ if (!exclude) return false;+ const parts = layoutParts(exclude);+ return parts.length === relative.length && parts.every((part, index) => contentPattern(part).test(relative[index]));+}+function sameLayoutPath(expected: string[], actual: string[]): boolean {+ return expected.length === actual.length && expected.every((part, index) => part.toLowerCase() === actual[index].toLowerCase());+}+async function layoutPayload(directory: string, layout: FileGroupLayout, coordinate: LayoutCoordinate): Promise<'ok' | 'missing' | 'ambiguous' | 'unsafe'> {+ let folder = directory;+ for (const part of [...(coordinate === 'source' ? layout.source : layout.destination), ...layout.prefix]) {+ const matches = (await fs.readdir(folder)).filter(name => name.toLowerCase() === part.toLowerCase());+ if (!matches.length) return 'missing';+ if (matches.length > 1) return 'ambiguous';+ folder = path.join(folder, matches[0]);+ const stat = await fs.lstat(folder);+ if (!stat.isDirectory() || stat.isSymbolicLink()) return 'unsafe';+ }+ const names = await fs.readdir(folder);+ if (names.length > 4096) throw Error('Content payload directory limit exceeded.');+ const pattern = contentPattern(layout.pattern);+ for (const name of names.filter(name => pattern.test(name) && !layoutExcluded(layout, [...layout.prefix, name])))+ if ((await fs.lstat(path.join(folder, name))).isFile()) return 'ok';+ return 'missing';+}export function contentManifest(xml: string): Manifest {const root = parsePackageXml(xml);if (root.name.split(':').at(-1) !== 'Game') throw Error('Expected a Microsoft Game manifest.');@@ -202,37 +256,38 @@ export class GameContent {}visit(layout, 0);if (!groups.length || groups.length > 256) throw Error('Content layout contains no supported file groups.');+ const fileGroups = layoutGroups(groups);+ let coordinate: LayoutCoordinate = 'source';if (configuration.source) {- const parts = (value: string) => value.split(/[/\\]/).filter(part => part && part !== '.');- const mappings = groups.filter(group => {- const source = parts(group.attributes.SourcePath ?? '').join('/').toLowerCase();- const include = parts(group.attributes.Include ?? '');- const destination = parts(group.attributes.DestinationPath ?? '');- return source === 'src' && include.length === 1 && !destination.length- && !group.attributes.Exclude && contentPattern(include[0]).test('MicrosoftGame.config');+ // A downloaded tree can be extracted at either layout coordinate. SRC+ // metadata is authoritative because the root copy is optional, then the+ // whole layout must agree on one interpretation. Source-coordinate trees+ // are deployable only when their declared destination is the package root+ // that runtime discovery reads; destination-coordinate trees already sit+ // at their deployment paths, including a package-relative \SRC.+ const configName = 'MicrosoftGame.config';+ const physical = path.relative(directory, configuration.source).split(path.sep).filter(Boolean);+ const declarations = fileGroups.filter(group => {+ if (!contentPattern(group.pattern).test(configName)) return false;+ const relative = [...group.prefix, configName];+ return (sameLayoutPath([...group.source, ...relative], physical) || sameLayoutPath([...group.destination, ...relative], physical))+ && !layoutExcluded(group, relative);});- if (mappings.length !== 1) throw Error('SRC MicrosoftGame.config must be mapped to the package root by its layout.');+ if (declarations.length !== 1) throw Error('MicrosoftGame.config must be unambiguously mapped by the content layout.');+ const declaration = declarations[0], relative = [...declaration.prefix, configName];+ const sourcePath = [...declaration.source, ...relative], destinationPath = [...declaration.destination, ...relative];+ // A source-coordinate tree can be registered only when its declared+ // deployment is the package root. Otherwise the tree must already sit at+ // its destination coordinates, including a package-relative \SRC.+ if (sameLayoutPath(sourcePath, physical) && sameLayoutPath(destinationPath, [configName])) coordinate = 'source';+ else if (sameLayoutPath(destinationPath, physical)) coordinate = 'destination';+ else throw Error('MicrosoftGame.config is not at a layout-declared location.');}- for (const group of groups) {- const source = group.attributes.SourcePath, include = group.attributes.Include;- const valid = (value: string | undefined) => value && !/^[/\\]/.test(value) && !/[:\x00-\x1f]/.test(value) && !value.split(/[/\\]/).includes('..');- if (!valid(source) || !valid(include) || /[*?]/.test(source)) throw Error('Unsupported content layout path.');- const parts = [...source.split(/[/\\]/), ...include.split(/[/\\]/)].filter(part => part && part !== '.');- const pattern = parts.pop();- if (!pattern || parts.some(part => /[*?]/.test(part))) throw Error('Unsupported content layout wildcard.');- let folder = directory;- for (const part of parts) {- const matches = (await fs.readdir(folder)).filter(name => name.toLowerCase() === part.toLowerCase());- if (matches.length !== 1) throw Error('Missing or ambiguous content payload directory.');- folder = path.join(folder, matches[0]);- if (!(await fs.lstat(folder)).isDirectory()) throw Error('Invalid content payload directory.');- }- const match = contentPattern(pattern);- const names = await fs.readdir(folder);- if (names.length > 4096) throw Error('Content payload directory limit exceeded.');- let found = false;- for (const name of names.filter(name => match.test(name))) if ((await fs.lstat(path.join(folder, name))).isFile()) { found = true; break; }- if (!found) throw Error('A file group declared by the content layout is missing.');+ for (const group of fileGroups) {+ const result = await layoutPayload(directory, group, coordinate);+ if (result === 'ambiguous') throw Error('Missing or ambiguous content payload directory.');+ if (result === 'unsafe') throw Error('Invalid content payload directory.');+ if (result !== 'ok') throw Error('A file group declared by the content layout is missing.');}return { ...content, configuration };}