9f38e38f6a
Each installation generates a private Ed25519 key once (0600, under ~/.config/pipelines), signs version.json with a detached version.json.sig when publishing APK and desktop builds, and embeds its own and listed trusted public keys in app builds. The app verifies the feed signature before trusting the artifact sha256, refuses unsigned or foreign feeds when it has keys, and requires the sha256 and Android signer in signed feeds.
158 lines
5.7 KiB
Dart
158 lines
5.7 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/features/updates/app_updates.dart';
|
|
import 'package:pipelines_app/features/updates/app_updates_io.dart' as io;
|
|
import 'package:pipelines_app/features/updates/update_feed_signature.dart';
|
|
|
|
// Written by scripts/update_signing.py publish_feed with a throwaway key;
|
|
// scripts/test_update_signing.py checks the same files with OpenSSL.
|
|
const fixture = 'test/features/updates/fixtures/signed_feed';
|
|
|
|
void main() {
|
|
final feed = File('$fixture/version.json').readAsBytesSync();
|
|
final signature = File('$fixture/version.json.sig').readAsStringSync();
|
|
final key = File('$fixture/public_key.txt').readAsStringSync().trim();
|
|
final trusted = trustedUpdateKeys(key);
|
|
final otherKey = base64.encode(List<int>.generate(32, (i) => i));
|
|
|
|
test('a feed signed by the Python publisher verifies', () {
|
|
expect(trusted, hasLength(1));
|
|
expect(feedSignatureProblem(feed, signature, trusted), isNull);
|
|
// Trusted among several keys (this server's and another's).
|
|
expect(
|
|
feedSignatureProblem(
|
|
feed,
|
|
signature,
|
|
trustedUpdateKeys('$otherKey, $key'),
|
|
),
|
|
isNull,
|
|
);
|
|
});
|
|
|
|
test('a tampered feed or signature is refused', () {
|
|
final tampered = utf8.encode(utf8.decode(feed).replaceFirst('"ab', '"cd'));
|
|
expect(
|
|
feedSignatureProblem(tampered, signature, trusted),
|
|
contains('does not match'),
|
|
);
|
|
final document = jsonDecode(signature) as Map;
|
|
final bytes = base64.decode(document['signature'] as String)..[5] ^= 1;
|
|
final badSignature = jsonEncode({
|
|
...document,
|
|
'signature': base64.encode(bytes),
|
|
});
|
|
expect(
|
|
feedSignatureProblem(feed, badSignature, trusted),
|
|
contains('does not match'),
|
|
);
|
|
expect(
|
|
feedSignatureProblem(feed, '{"algorithm":"rsa"}', trusted),
|
|
contains('unreadable'),
|
|
);
|
|
expect(
|
|
feedSignatureProblem(feed, 'not json', trusted),
|
|
contains('unreadable'),
|
|
);
|
|
});
|
|
|
|
test('a missing signature is refused when the app has keys', () {
|
|
expect(feedSignatureProblem(feed, null, trusted), contains('not signed'));
|
|
// A development build without embedded keys keeps the old behaviour.
|
|
expect(feedSignatureProblem(feed, null, trustedUpdateKeys('')), isNull);
|
|
});
|
|
|
|
test('a signature by a key the app does not trust is refused', () {
|
|
expect(
|
|
feedSignatureProblem(feed, signature, trustedUpdateKeys(otherKey)),
|
|
contains('does not trust'),
|
|
);
|
|
// Swapping in the attacker's own key in the document does not help.
|
|
final document = jsonDecode(signature) as Map;
|
|
final swapped = jsonEncode({...document, 'publicKey': otherKey});
|
|
expect(
|
|
feedSignatureProblem(feed, swapped, trustedUpdateKeys('$key,$otherKey')),
|
|
contains('does not match'),
|
|
);
|
|
expect(trustedUpdateKeys('short, ${base64.encode([1])}'), isEmpty);
|
|
});
|
|
|
|
test('a signed feed must carry the artifact digest and Android signer', () {
|
|
final base = Uri.parse('https://mac.test/updates/');
|
|
final signer = 'a' * 64;
|
|
String android({bool digest = true, bool certificate = true}) =>
|
|
jsonEncode({
|
|
'versionCode': 9,
|
|
'apk': 'pipelines.apk',
|
|
if (digest) 'sha256': 'b' * 64,
|
|
if (certificate) 'signingCertificateSha256': signer,
|
|
});
|
|
AppUpdate? parse(String body, {bool signed = true}) =>
|
|
AppUpdate.parse(body, base, installedSignature: signer, signed: signed);
|
|
expect(parse(android()), isNotNull);
|
|
expect(parse(android(certificate: false)), isNull);
|
|
expect(parse(android(digest: false)), isNull);
|
|
expect(
|
|
AppUpdate.parse(android(), base, signed: true),
|
|
isNull,
|
|
reason: 'the installed signature is required to compare',
|
|
);
|
|
// Unsigned builds keep accepting legacy feeds; the OS installer checks.
|
|
expect(
|
|
parse(android(certificate: false, digest: false), signed: false),
|
|
isNotNull,
|
|
);
|
|
final linux = AppUpdate.parse(
|
|
utf8.decode(feed),
|
|
base,
|
|
platform: 'linux',
|
|
architecture: 'x64',
|
|
signed: true,
|
|
);
|
|
expect(linux?.sha256, 'ab' * 32);
|
|
});
|
|
|
|
test(
|
|
'the feed reader refuses unsigned and tampered feeds over HTTP',
|
|
() async {
|
|
final previous = HttpOverrides.current;
|
|
HttpOverrides.global = null;
|
|
final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
|
|
var body = feed;
|
|
String? served = signature;
|
|
server.listen((request) async {
|
|
final path = request.uri.path;
|
|
if (path.endsWith('/version.json')) {
|
|
request.response.add(body);
|
|
} else if (path.endsWith('/version.json.sig') && served != null) {
|
|
request.response.write(served);
|
|
} else {
|
|
request.response.statusCode = 404;
|
|
}
|
|
await request.response.close();
|
|
});
|
|
final base = Uri.parse('http://127.0.0.1:${server.port}/updates/');
|
|
Future<Object?> read() => io
|
|
.fetchLatestScoped(base, (_) => {}, trustedKeys: trusted)
|
|
.then<Object?>((update) => update, onError: (Object error) => error);
|
|
try {
|
|
served = null;
|
|
expect('${await read()}', contains('not signed'));
|
|
served = signature;
|
|
body = utf8.encode(utf8.decode(feed).replaceFirst('"ab', '"cd'));
|
|
expect('${await read()}', contains('does not match'));
|
|
body = feed;
|
|
// A genuine feed passes the signature; the host's platform may then
|
|
// still find the fixture's Linux build incompatible.
|
|
final result = await read();
|
|
expect('$result', isNot(contains('sign')));
|
|
expect('$result', isNot(contains('trust')));
|
|
} finally {
|
|
await server.close(force: true);
|
|
HttpOverrides.global = previous;
|
|
}
|
|
},
|
|
);
|
|
}
|