diff --git a/app/lib/features/updates/app_updates.dart b/app/lib/features/updates/app_updates.dart index 37651484..a8a1c35e 100644 --- a/app/lib/features/updates/app_updates.dart +++ b/app/lib/features/updates/app_updates.dart @@ -18,6 +18,7 @@ class AppUpdate { this.headersFor, this.platform = 'android', this.sha256 = '', + this.signingCertificateSha256 = '', }); final int versionCode; @@ -27,6 +28,7 @@ class AppUpdate { final String sourceName; final String platform; final String sha256; + final String signingCertificateSha256; final Map Function(Uri)? headersFor; AppUpdate fromServer( @@ -40,6 +42,7 @@ class AppUpdate { platform: platform, sourceName: name, sha256: sha256, + signingCertificateSha256: signingCertificateSha256, headersFor: headers, ); @@ -49,6 +52,7 @@ class AppUpdate { Uri base, { String platform = 'android', String architecture = 'arm64', + String? installedSignature, }) { try { final json = jsonDecode(body); @@ -66,6 +70,19 @@ class AppUpdate { if (digest.isNotEmpty && !RegExp(r'^[a-fA-F0-9]{64}$').hasMatch(digest)) { return null; } + final signer = json['signingCertificateSha256']; + if (signer != null && + (signer is! String || + !RegExp(r'^[a-fA-F0-9]{64}$').hasMatch(signer))) { + return null; + } + if (platform == 'android' && + signer is String && + installedSignature != null && + signer.toLowerCase() != + installedSignature.replaceAll(':', '').trim().toLowerCase()) { + return null; + } if (platform == 'android' && json['platform'] != null && json['platform'] != 'android') { @@ -81,6 +98,7 @@ class AppUpdate { versionCode: code, platform: platform, sha256: '${json['sha256'] ?? ''}', + signingCertificateSha256: signer is String ? signer.toLowerCase() : '', versionName: '${json['versionName'] ?? ''}', apk: base.resolve(file), notes: '${json['notes'] ?? ''}', diff --git a/app/lib/features/updates/app_updates_io.dart b/app/lib/features/updates/app_updates_io.dart index ea4cc66a..6e2fe953 100644 --- a/app/lib/features/updates/app_updates_io.dart +++ b/app/lib/features/updates/app_updates_io.dart @@ -86,6 +86,9 @@ Future fetchLatestScoped( feed, platform: platform, architecture: nativeArchitecture, + installedSignature: platform == 'android' + ? (await PackageInfo.fromPlatform()).buildSignature + : null, ); if (update == null) { throw const FormatException('No compatible build is offered.'); diff --git a/app/notes/native-multi-server-updates.md b/app/notes/native-multi-server-updates.md index e9595c17..a82df72b 100644 --- a/app/notes/native-multi-server-updates.md +++ b/app/notes/native-multi-server-updates.md @@ -44,3 +44,9 @@ native-update capability override to the Root test host. Existing server-switch fixtures disable native network probing; production defaults still enable all supported native platforms. This resolved two pending-timer fixture failures introduced when Linux became supported. Analyzer clean (9sOpy3). + +Android feeds may advertise `signingCertificateSha256`. The app rejects malformed +fingerprints and compares valid ones with `PackageInfo.buildSignature` before +considering a build, so a newer APK signed independently on another machine does +not hide an installable peer. Legacy feeds without the field retain Android's +installer signature validation. Desktop archive feeds are unaffected. diff --git a/app/test/features/updates/multi_server_updates_test.dart b/app/test/features/updates/multi_server_updates_test.dart index e6522cc3..bacbc7e2 100644 --- a/app/test/features/updates/multi_server_updates_test.dart +++ b/app/test/features/updates/multi_server_updates_test.dart @@ -10,6 +10,65 @@ import 'package:pipelines_app/features/updates/app_updates.dart'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); + test('newer differently signed APK is skipped for compatible peer', () async { + SharedPreferences.setMockInitialValues({}); + final signer = 'a' * 64; + final compatible = SavedServer.validated( + id: 'compatible', + name: 'Mac', + endpoints: ['wss://compatible.test/connect'], + ); + final different = SavedServer.validated( + id: 'different', + name: 'Linux', + endpoints: ['wss://different.test/connect'], + ); + final update = await newestServerUpdate( + [different, compatible], + read: (base, headers) async { + final parsed = AppUpdate.parse( + jsonEncode({ + 'versionCode': base.host == 'different.test' ? 30 : 20, + 'apk': 'pipelines.apk', + 'signingCertificateSha256': base.host == 'different.test' + ? 'b' * 64 + : signer, + }), + base, + installedSignature: signer.toUpperCase(), + ); + if (parsed == null) throw const FormatException('Incompatible signer'); + return parsed; + }, + ); + expect(update.versionCode, 20); + expect(update.sourceName, 'Mac'); + expect(update.signingCertificateSha256, signer); + final base = Uri.parse('https://compatible.test/updates/'); + for (final invalid in ['', 'sha256:bad', 42, 'g' * 64]) { + expect( + AppUpdate.parse( + jsonEncode({ + 'versionCode': 40, + 'apk': 'pipelines.apk', + 'signingCertificateSha256': invalid, + }), + base, + installedSignature: signer, + ), + isNull, + ); + } + // Legacy feeds retain OS installer validation rather than disappearing. + expect( + AppUpdate.parse( + '{"versionCode":1,"apk":"pipelines.apk"}', + base, + installedSignature: signer, + ), + isNotNull, + ); + }); test( 'retained update refuses credentials after alias/profile removal', () async {