[tauri] TauriPlatforms: The shell around the built game, the Android privacy patch, the shell build of the game, and the device check

This commit is contained in:
2026-11-17 18:45:37 +01:00
parent 3f95a1b37d
commit a93ef09956
33 changed files with 5902 additions and 86 deletions
@@ -0,0 +1,138 @@
/**
* The Android privacy patch (tauri-platforms.md D6), on Tauri 2.12.5's stock manifest exactly as
* `tauri android init` wrote it (fixtures/AndroidManifest.stock.xml), and on the committed rule files.
*/
import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { describe, expect, it } from 'vitest';
import {
APPLICATION_ATTRS,
DOMAINS,
manifestProblems,
META_DATA,
patchManifest,
RULE_FILES,
RULE_SECTIONS,
ruleProblems
} from '../src/android-privacy.js';
const HERE = dirname(fileURLToPath(import.meta.url));
const STOCK = readFileSync(join(HERE, 'fixtures', 'AndroidManifest.stock.xml'), 'utf8').replaceAll('\r\n', '\n');
const rules = (f: string): string => readFileSync(join(HERE, '..', 'android', 'res', 'xml', f), 'utf8');
describe('patchManifest', () => {
const patched = patchManifest(STOCK);
it("removes Tauri's INTERNET permission: the kernel then refuses the app any socket", () => {
expect(STOCK).toContain('android.permission.INTERNET');
expect(patched).not.toContain('android.permission.INTERNET');
});
it('turns backup and device transfer off on <application>', () => {
const tag = /<application\b[^>]*>/.exec(patched)?.[0] ?? '';
for (const [name, value] of Object.entries(APPLICATION_ATTRS)) expect(tag).toContain(`${name}="${value}"`);
// What Tauri set is kept.
expect(tag).toContain('android:icon="@mipmap/ic_launcher"');
expect(tag).toContain('android:usesCleartextTraffic="${usesCleartextTraffic}"');
});
it("switches the WebView's metrics and Safe Browsing off, inside <application>", () => {
const inside = /<application\b[\s\S]*<\/application>/.exec(patched)?.[0] ?? '';
for (const [name, value] of Object.entries(META_DATA)) {
expect(inside).toContain(`<meta-data android:name="${name}" android:value="${value}" />`);
}
});
it('leaves the rest of the manifest as it was', () => {
for (const keep of ['android:name=".MainActivity"', 'android.intent.category.LAUNCHER', 'androidx.core.content.FileProvider']) {
expect(patched).toContain(keep);
}
});
it('changes nothing on a second run', () => {
expect(patchManifest(patched)).toBe(patched);
});
it('keeps Windows line endings when the file has them', () => {
const crlf = patchManifest(STOCK.replaceAll('\n', '\r\n'));
expect(crlf).toBe(patched.replaceAll('\n', '\r\n'));
});
it('overwrites an allowBackup="true" rather than adding a second one', () => {
const on = STOCK.replace('<application', '<application android:allowBackup="true"');
const out = patchManifest(on);
expect(out).toContain('android:allowBackup="false"');
expect(out).not.toContain('android:allowBackup="true"');
expect(out.match(/android:allowBackup=/g)).toHaveLength(1);
});
it('refuses a manifest whose shape it does not know, rather than guessing', () => {
expect(() => patchManifest(STOCK.replace('</application>', '</application><application></application>'))).toThrow(/application/);
expect(() => patchManifest(STOCK.replace(/<application[\s\S]*<\/application>/, '<application />'))).toThrow();
// INTERNET asked for in a form the line pattern does not match.
const odd = STOCK.replace(
'<uses-permission android:name="android.permission.INTERNET" />',
'<uses-permission android:name="android.permission.INTERNET" android:maxSdkVersion="99" />'
);
expect(() => patchManifest(odd)).toThrow(/INTERNET/);
});
it('removes INTERNET written over two lines too', () => {
const split = STOCK.replace(
'<uses-permission android:name="android.permission.INTERNET" />',
'<uses-permission\n android:name="android.permission.INTERNET" />'
);
expect(patchManifest(split)).not.toContain('INTERNET');
});
});
describe('manifestProblems', () => {
it("names everything missing from Tauri's stock manifest", () => {
const problems = manifestProblems(STOCK);
expect(problems).toContain('android.permission.INTERNET is requested');
for (const name of Object.keys(APPLICATION_ATTRS)) expect(problems.join('\n')).toContain(name);
for (const name of Object.keys(META_DATA)) expect(problems.join('\n')).toContain(name);
expect(problems).toHaveLength(1 + Object.keys(APPLICATION_ATTRS).length + Object.keys(META_DATA).length);
});
it('finds nothing missing once patched', () => {
expect(manifestProblems(patchManifest(STOCK))).toEqual([]);
});
it('notices a single switch turned back on', () => {
const patched = patchManifest(STOCK);
expect(manifestProblems(patched.replace('android:allowBackup="false"', 'android:allowBackup="true"'))).toEqual([
'<application> lacks android:allowBackup="false"'
]);
expect(manifestProblems(patched.replace('android:value="true"', 'android:value="false"'))).toEqual([
'no meta-data android.webkit.WebView.MetricsOptOut=true'
]);
});
});
describe('the committed rule files', () => {
for (const f of RULE_FILES) {
it(`${f} excludes every domain, in every section, and includes nothing`, () => {
expect(ruleProblems(rules(f), RULE_SECTIONS[f])).toEqual([]);
});
}
it('a file that forgets one domain is caught, by name', () => {
const f = 'rosea_data_extraction_rules.xml' as const;
const text = rules(f);
const firstDb = text.indexOf('<exclude domain="database" path="." />');
const broken = text.slice(0, firstDb) + text.slice(firstDb + '<exclude domain="database" path="." />'.length);
expect(ruleProblems(broken, RULE_SECTIONS[f])).toEqual(['cloud-backup: database is not excluded']);
});
it('a file that includes something is caught', () => {
const f = 'rosea_backup_rules.xml' as const;
const broken = rules(f).replace('</full-backup-content>', '<include domain="file" path="." />\n</full-backup-content>');
expect(ruleProblems(broken, RULE_SECTIONS[f])).toEqual(['includes something']);
});
it('knows all nine storage domains', () => {
expect(DOMAINS).toHaveLength(9);
});
});
+126
View File
@@ -0,0 +1,126 @@
/**
* The shell's configuration holds what tauri-platforms.md decided (D3–D7, D10, D11). Each of
* these is a value that fails silently at runtime, or worse, only after a release: a changed
* identifier or origin leaves the player's rose behind in the old app.
*/
import { existsSync, readFileSync } from 'node:fs';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { describe, expect, it } from 'vitest';
import { CSP } from '../../rosea/csp.js';
import { PRODUCT_NAME } from '../../../packages/schema/src/constants.js';
const SRC_TAURI = join(dirname(fileURLToPath(import.meta.url)), '..', 'src-tauri');
/** The parts of a Tauri config these tests read. */
interface TauriConf {
productName?: string;
identifier?: string;
version?: string;
build: { frontendDist: string; beforeBuildCommand: string; beforeDevCommand: string; devUrl: string };
app: { windows: Record<string, unknown>[]; security: Record<string, unknown> };
bundle: { targets?: string[] };
plugins?: unknown;
}
const json = (name: string): TauriConf => JSON.parse(readFileSync(join(SRC_TAURI, name), 'utf8')) as TauriConf;
const conf = json('tauri.conf.json');
const window = conf.app.windows[0] ?? {};
describe('tauri.conf.json', () => {
it('names the app with the product name, in the bundle and the window title (D3)', () => {
expect(conf.productName).toBe(PRODUCT_NAME);
expect(window['title']).toBe(PRODUCT_NAME);
});
it('keeps the identifier it was released with: a new one is a different app (D3)', () => {
expect(conf.identifier).toBe('cz.mareknovak.domi.rosea');
});
it("reads its version from the game's package.json (D3)", () => {
expect(conf.version).toBe('../../rosea/package.json');
const pkg = JSON.parse(readFileSync(resolve(SRC_TAURI, String(conf.version)), 'utf8')) as Record<string, string>;
expect(pkg['name']).toBe('@rosea/app');
expect(pkg['version']).toMatch(/^\d+\.\d+\.\d+$/);
});
it("delivers the PWA's content-security policy, the same string (D4)", () => {
expect(conf.app.security['csp']).toBe(CSP);
});
it("wraps the game's shell build and nothing else (D1, D2)", () => {
expect(conf.build.frontendDist).toBe('../../rosea/dist-tauri');
expect(resolve(SRC_TAURI, conf.build.frontendDist)).toBe(resolve(SRC_TAURI, '..', '..', 'rosea', 'dist-tauri'));
expect(conf.build.beforeBuildCommand).toBe('pnpm --filter @rosea/app build:shell');
expect(conf.build.beforeDevCommand).toBe('pnpm --filter @rosea/app dev:shell');
});
it('runs its dev server on 127.0.0.1 only (CLAUDE.md §5.7)', () => {
expect(new URL(conf.build.devUrl).hostname).toBe('127.0.0.1');
const app = JSON.parse(readFileSync(join(SRC_TAURI, '..', '..', 'rosea', 'package.json'), 'utf8')) as { scripts: Record<string, string> };
expect(app['scripts']['dev:shell']).toContain('--host 127.0.0.1');
expect(app['scripts']['dev:shell']).toContain('--port 5173');
expect(conf.build.devUrl).toBe('http://127.0.0.1:5173');
});
it('keeps the https origin it was released with: IndexedDB lives per origin (D10)', () => {
expect(window['useHttpsScheme']).toBe(true);
});
it("switches WebView2's own traffic off (D7)", () => {
const args = String(window['additionalBrowserArgs']);
for (const flag of [
'--disable-background-networking',
'--disable-component-update',
'--disable-domain-reliability',
'--disable-sync',
'--no-pings',
'--disable-client-side-phishing-detection',
'--dns-prefetch-disable'
]) {
expect(args.split(' ')).toContain(flag);
}
const disabled = /--disable-features=(\S+)/.exec(args)?.[1]?.split(',') ?? [];
for (const feature of [
// Tauri's own defaults, which a custom list must repeat.
'msWebOOUI', 'msPdfOOUI', 'msSmartScreenProtection',
'DnsOverHttpsUpgrade', 'msExperimentationAndConfigurationService', 'AutofillServerCommunication', 'OptimizationHints'
]) {
expect(disabled).toContain(feature);
}
});
it('builds only what each system can make (D11)', () => {
expect(json('tauri.windows.conf.json').bundle.targets).toEqual(['nsis']);
expect(json('tauri.macos.conf.json').bundle.targets).toEqual(['app', 'dmg']);
expect(json('tauri.linux.conf.json').bundle.targets).toEqual(['deb', 'appimage']);
expect(conf.bundle.targets).toBeUndefined();
});
it('applies the Android privacy patch before the game is built for Android (D6)', () => {
const before = json('tauri.android.conf.json').build.beforeBuildCommand;
const patch = before.indexOf('pnpm --filter @rosea/tauri android:privacy');
expect(patch).toBe(0);
expect(before.indexOf('&& pnpm --filter @rosea/app build:shell')).toBeGreaterThan(patch);
});
});
describe('the Rust side (D5)', () => {
it('depends on tauri alone: no plugin, no SDK', () => {
const cargo = readFileSync(join(SRC_TAURI, 'Cargo.toml'), 'utf8');
const deps = /\n\[dependencies\]\n([\s\S]*?)(?:\n\[|$)/.exec(cargo)?.[1] ?? '';
const names = deps
.split('\n')
.map((l) => /^([A-Za-z0-9_-]+)\s*=/.exec(l)?.[1])
.filter((n): n is string => n !== undefined);
expect(names).toEqual(['tauri']);
expect(cargo).not.toMatch(/\[target\.[^\]]*dependencies\]/);
});
it('grants the page no IPC: there is no capabilities folder', () => {
expect(existsSync(join(SRC_TAURI, 'capabilities'))).toBe(false);
expect(conf.app.security['capabilities']).toBeUndefined();
});
it('declares no plugin configuration', () => {
expect(conf.plugins).toBeUndefined();
});
});
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<!-- AndroidTV support -->
<uses-feature android:name="android.software.leanback" android:required="false" />
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.rosea_shell"
android:usesCleartextTraffic="${usesCleartextTraffic}">
<activity
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
android:launchMode="singleTask"
android:label="@string/main_activity_title"
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<!-- AndroidTV support -->
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>
+63
View File
@@ -0,0 +1,63 @@
/**
* The game's build for the shell (`vite build --mode tauri`, tauri-platforms.md D2), made into a
* temporary folder and read: no service worker, no manifest, no <meta> policy, assets at the root,
* and the shell constant replaced. The PWA's own build is checked by the e2e suites.
*/
import { spawnSync } from 'node:child_process';
import { mkdtempSync, readdirSync, readFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
const APP = join(dirname(fileURLToPath(import.meta.url)), '..', '..', 'rosea');
let out = '';
let html = '';
let files: string[] = [];
let scripts = '';
beforeAll(() => {
out = mkdtempSync(join(tmpdir(), 'rosea-shell-build-'));
const build = spawnSync('pnpm', ['exec', 'vite', 'build', '--mode', 'tauri', '--outDir', out, '--emptyOutDir', '--logLevel', 'error'], {
cwd: APP,
encoding: 'utf8',
shell: process.platform === 'win32'
});
if (build.status !== 0) throw new Error(`vite build --mode tauri failed:\n${build.stdout}\n${build.stderr}`);
files = readdirSync(out, { recursive: true, encoding: 'utf8' }).map((f) => f.replaceAll('\\', '/'));
html = readFileSync(join(out, 'index.html'), 'utf8');
scripts = files.filter((f) => f.endsWith('.js')).map((f) => readFileSync(join(out, f), 'utf8')).join('\n');
}, 120_000);
afterAll(() => {
if (out) rmSync(out, { recursive: true, force: true });
});
describe('the game built for the shell', () => {
it('has no service worker and no web manifest', () => {
expect(files.filter((f) => /(^|\/)(sw|workbox-[^/]*|registerSW)\.js$/.test(f))).toEqual([]);
expect(files.filter((f) => f.endsWith('.webmanifest'))).toEqual([]);
expect(html).not.toContain('manifest');
expect(scripts).not.toContain('navigator.serviceWorker.register');
});
it('carries no <meta> content-security policy: Tauri delivers it (D4)', () => {
expect(html).not.toContain('Content-Security-Policy');
});
it('loads its assets from the root of the origin, not /Rosea/', () => {
const urls = [...html.matchAll(/(?:src|href)="([^"]+)"/g)].map((m) => m[1]!);
const assets = urls.filter((u) => u.includes('assets/'));
expect(assets.length).toBeGreaterThanOrEqual(2);
for (const u of assets) expect(u.startsWith('/assets/')).toBe(true);
expect(html).not.toContain('/Rosea/');
});
it('replaces the shell constant at build time: left in, it would throw in the webview', () => {
expect(scripts).not.toContain('__ROSEA_SHELL__');
});
it('ships no source maps', () => {
expect(files.filter((f) => f.endsWith('.map'))).toEqual([]);
});
});