Files
Rosea/apps/rosea-tauri/test/android-privacy.test.ts

181 lines
8.0 KiB
TypeScript

/**
* 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 {
apkProblems,
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);
});
});
describe('apkProblems, on aapt2 output of the APK built 2026-11-17 (Tauri 2.12.5, AGP 9.3.1)', () => {
const permissions = readFileSync(join(HERE, 'fixtures', 'apk-permissions.txt'), 'utf8');
const tree = readFileSync(join(HERE, 'fixtures', 'apk-manifest.xmltree.txt'), 'utf8');
it('passes the patched APK: no permission asked but androidx own receiver guard', () => {
expect(apkProblems(permissions, tree)).toEqual([]);
});
it('names INTERNET, or any permission a library merges in', () => {
const more = `${permissions.trimEnd()}\nuses-permission: name='android.permission.INTERNET'\nuses-permission: name='android.permission.ACCESS_NETWORK_STATE'\n`;
expect(apkProblems(more, tree)).toEqual([
'the APK asks for android.permission.INTERNET',
'the APK asks for android.permission.ACCESS_NETWORK_STATE'
]);
});
it("does not let another app's receiver guard through", () => {
const other = `${permissions.trimEnd()}\nuses-permission: name='com.example.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION'\n`;
expect(apkProblems(other, tree)).toEqual(['the APK asks for com.example.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION']);
});
it('names backup left on', () => {
expect(apkProblems(permissions, tree.replace(/allowBackup\((0x[0-9a-f]+)\)=false/, 'allowBackup($1)=true'))).toEqual([
'the APK does not set allowBackup=false'
]);
});
it("reads each WebView switch's own value, not its neighbour's", () => {
const flipped = tree.replace(
/("android\.webkit\.WebView\.EnableSafeBrowsing"[^\n]*\r?\n\s*A: [^\n]*:value\(0x[0-9a-f]+\))=false/,
'$1=true'
);
expect(flipped).not.toBe(tree);
expect(apkProblems(permissions, flipped)).toEqual([
"the APK's meta-data android.webkit.WebView.EnableSafeBrowsing is true, not false"
]);
const gone = tree.replace('"android.webkit.WebView.MetricsOptOut"', '"android.webkit.WebView.Something"');
expect(apkProblems(permissions, gone)).toEqual(['the APK has no meta-data android.webkit.WebView.MetricsOptOut']);
});
});