Merge branch 'worktree-agent-ac54c8919e940163a'
# Conflicts: # scripts/setup-native.py
This commit is contained in:
@@ -113,11 +113,16 @@ class DiscoveredServer {
|
||||
DiscoveredAddress(route: DiscoveredRoute.public, endpoint: public),
|
||||
);
|
||||
}
|
||||
// lan=0: the web proxy refuses LAN connections, so the address record is
|
||||
// not a LAN route; the first tailnet or public route is the endpoint.
|
||||
if (attributes['lan'] == '0') routes.removeAt(0);
|
||||
if (routes.isEmpty) return null;
|
||||
final first = routes.first.endpoint;
|
||||
return DiscoveredServer(
|
||||
name: name.isEmpty ? instance : name,
|
||||
host: host,
|
||||
port: port,
|
||||
secure: secure,
|
||||
host: first.host,
|
||||
port: first.port,
|
||||
secure: first.scheme == 'wss',
|
||||
path: cleanPath,
|
||||
passwordRequired: attributes['auth'] != 'none',
|
||||
version: attributes['v'],
|
||||
|
||||
@@ -47,12 +47,18 @@ class AppUpdate {
|
||||
);
|
||||
|
||||
/// Reads `version.json`; the APK path is relative to [base].
|
||||
///
|
||||
/// A [signed] feed (its signature verified with a key the build trusts)
|
||||
/// must name the artifact's sha256 and, for Android, its signing
|
||||
/// certificate: an unsigned build tolerates their absence, a signed one
|
||||
/// does not.
|
||||
static AppUpdate? parse(
|
||||
String body,
|
||||
Uri base, {
|
||||
String platform = 'android',
|
||||
String architecture = 'arm64',
|
||||
String? installedSignature,
|
||||
bool signed = false,
|
||||
}) {
|
||||
try {
|
||||
final json = jsonDecode(body);
|
||||
@@ -67,6 +73,7 @@ class AppUpdate {
|
||||
return null;
|
||||
}
|
||||
final digest = '${json['sha256'] ?? ''}';
|
||||
if (signed && digest.isEmpty) return null;
|
||||
if (digest.isNotEmpty && !RegExp(r'^[a-fA-F0-9]{64}$').hasMatch(digest)) {
|
||||
return null;
|
||||
}
|
||||
@@ -76,6 +83,11 @@ class AppUpdate {
|
||||
!RegExp(r'^[a-fA-F0-9]{64}$').hasMatch(signer))) {
|
||||
return null;
|
||||
}
|
||||
if (signed &&
|
||||
platform == 'android' &&
|
||||
(signer is! String || installedSignature == null)) {
|
||||
return null;
|
||||
}
|
||||
if (platform == 'android' &&
|
||||
signer is String &&
|
||||
installedSignature != null &&
|
||||
|
||||
@@ -11,6 +11,7 @@ import 'package:path_provider/path_provider.dart';
|
||||
import '../../core/session/server_session.dart';
|
||||
import 'app_updates.dart';
|
||||
import 'linux_self_update.dart';
|
||||
import 'update_feed_signature.dart';
|
||||
|
||||
bool get supported =>
|
||||
Platform.isAndroid || Platform.isLinux || Platform.isMacOS;
|
||||
@@ -57,10 +58,42 @@ Future<HttpClientResponse> _get(
|
||||
Future<AppUpdate> fetchLatest(Uri base) =>
|
||||
fetchLatestScoped(base, ServerSession.headersFor);
|
||||
|
||||
Future<AppUpdate> fetchLatestScoped(
|
||||
Uri base,
|
||||
/// Reads at most [limit] bytes of [response].
|
||||
Future<List<int>> _body(HttpClientResponse response, int limit) async {
|
||||
final chunks = <int>[];
|
||||
await for (final chunk in response.timeout(const Duration(seconds: 10))) {
|
||||
if (chunks.length + chunk.length > limit) {
|
||||
throw const FormatException('Update metadata is too large.');
|
||||
}
|
||||
chunks.addAll(chunk);
|
||||
}
|
||||
return chunks;
|
||||
}
|
||||
|
||||
/// The detached feed signature, or null when the server offers none.
|
||||
Future<String?> _signature(
|
||||
HttpClient client,
|
||||
Uri feed,
|
||||
Map<String, String> Function(Uri) headers,
|
||||
) async {
|
||||
try {
|
||||
final response = await _get(
|
||||
client,
|
||||
feed.resolve('version.json.sig'),
|
||||
headers,
|
||||
);
|
||||
return utf8.decode(await _body(response, 4096));
|
||||
} on HttpException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<AppUpdate> fetchLatestScoped(
|
||||
Uri base,
|
||||
Map<String, String> Function(Uri) headers, {
|
||||
List<List<int>>? trustedKeys,
|
||||
}) async {
|
||||
final trusted = trustedKeys ?? trustedUpdateKeys();
|
||||
final platform = nativePlatform;
|
||||
if (platform == 'linux' && nativeArchitecture != 'x64') {
|
||||
throw const FormatException(
|
||||
@@ -73,15 +106,15 @@ Future<AppUpdate> fetchLatestScoped(
|
||||
final client = HttpClient()..connectionTimeout = const Duration(seconds: 10);
|
||||
try {
|
||||
final response = await _get(client, feed.resolve('version.json'), headers);
|
||||
var bytes = 0;
|
||||
final chunks = <int>[];
|
||||
await for (final chunk in response.timeout(const Duration(seconds: 10))) {
|
||||
bytes += chunk.length;
|
||||
if (bytes > 65536) {
|
||||
throw const FormatException('Update metadata is too large.');
|
||||
}
|
||||
chunks.addAll(chunk);
|
||||
}
|
||||
final chunks = await _body(response, 65536);
|
||||
// The signature is checked before anything in the feed (the artifact's
|
||||
// sha256 above all) is believed.
|
||||
final problem = feedSignatureProblem(
|
||||
chunks,
|
||||
trusted.isEmpty ? null : await _signature(client, feed, headers),
|
||||
trusted,
|
||||
);
|
||||
if (problem != null) throw FormatException(problem);
|
||||
final update = AppUpdate.parse(
|
||||
utf8.decode(chunks),
|
||||
feed,
|
||||
@@ -90,6 +123,7 @@ Future<AppUpdate> fetchLatestScoped(
|
||||
installedSignature: platform == 'android'
|
||||
? (await PackageInfo.fromPlatform()).buildSignature
|
||||
: null,
|
||||
signed: trusted.isNotEmpty,
|
||||
);
|
||||
if (update == null) {
|
||||
throw const FormatException('No compatible build is offered.');
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:crypto/crypto.dart' as crypto;
|
||||
|
||||
/// Ed25519 signature verification (RFC 8032 section 5.1.7), verify only.
|
||||
///
|
||||
/// The update feed is checked once per update, so plain [BigInt] arithmetic
|
||||
/// is fast enough and keeps this small and auditable; it never handles a
|
||||
/// private key, so constant-time arithmetic is not required. Tested against
|
||||
/// the RFC 8032 vectors in `test/features/updates/ed25519_test.dart`.
|
||||
bool ed25519Verify({
|
||||
required List<int> publicKey,
|
||||
required List<int> message,
|
||||
required List<int> signature,
|
||||
}) {
|
||||
if (publicKey.length != 32 || signature.length != 64) return false;
|
||||
final a = _decode(publicKey);
|
||||
final r = _decode(signature.sublist(0, 32));
|
||||
if (a == null || r == null) return false;
|
||||
final s = _littleEndian(signature.sublist(32));
|
||||
if (s >= _l) return false;
|
||||
final digest = crypto.sha512.convert([
|
||||
...signature.sublist(0, 32),
|
||||
...publicKey,
|
||||
...message,
|
||||
]);
|
||||
final k = _littleEndian(digest.bytes) % _l;
|
||||
final left = _multiply(_base, s);
|
||||
final right = _add(r, _multiply(a, k));
|
||||
return _equal(_encode(left), _encode(right));
|
||||
}
|
||||
|
||||
final BigInt _p = (BigInt.one << 255) - BigInt.from(19);
|
||||
final BigInt _l =
|
||||
(BigInt.one << 252) +
|
||||
BigInt.parse('27742317777372353535851937790883648493');
|
||||
final BigInt _d =
|
||||
(BigInt.from(-121665) * BigInt.from(121666).modInverse(_p)) % _p;
|
||||
final BigInt _sqrtMinusOne = BigInt.two.modPow((_p - BigInt.one) >> 2, _p);
|
||||
final _Point _base = _decode(_baseEncoding)!;
|
||||
// The base point: y = 4/5, x even.
|
||||
final List<int> _baseEncoding = _bytes(
|
||||
(BigInt.from(4) * BigInt.from(5).modInverse(_p)) % _p,
|
||||
);
|
||||
|
||||
/// Extended coordinates (X:Y:Z:T) with x = X/Z, y = Y/Z, x*y = T/Z.
|
||||
class _Point {
|
||||
const _Point(this.x, this.y, this.z, this.t);
|
||||
final BigInt x, y, z, t;
|
||||
}
|
||||
|
||||
final _Point _identity = _Point(
|
||||
BigInt.zero,
|
||||
BigInt.one,
|
||||
BigInt.one,
|
||||
BigInt.zero,
|
||||
);
|
||||
|
||||
BigInt _littleEndian(List<int> bytes) {
|
||||
var value = BigInt.zero;
|
||||
for (var i = bytes.length - 1; i >= 0; i--) {
|
||||
value = (value << 8) | BigInt.from(bytes[i]);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
List<int> _bytes(BigInt value) {
|
||||
final out = Uint8List(32);
|
||||
var rest = value;
|
||||
for (var i = 0; i < 32; i++) {
|
||||
out[i] = (rest & BigInt.from(0xff)).toInt();
|
||||
rest >>= 8;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
_Point? _decode(List<int> encoded) {
|
||||
final bytes = List<int>.of(encoded);
|
||||
final sign = bytes[31] >> 7;
|
||||
bytes[31] &= 0x7f;
|
||||
final y = _littleEndian(bytes);
|
||||
if (y >= _p) return null;
|
||||
final y2 = y * y % _p;
|
||||
final u = (y2 - BigInt.one) % _p;
|
||||
final v = (_d * y2 + BigInt.one) % _p;
|
||||
final v3 = v * v % _p * v % _p;
|
||||
final v7 = v3 * v3 % _p * v % _p;
|
||||
var x =
|
||||
u * v3 % _p * (u * v7 % _p).modPow((_p - BigInt.from(5)) >> 3, _p) % _p;
|
||||
final check = v * x % _p * x % _p;
|
||||
if (check == u) {
|
||||
// x is a square root of u/v.
|
||||
} else if (check == (_p - u) % _p) {
|
||||
x = x * _sqrtMinusOne % _p;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
if (x == BigInt.zero && sign == 1) return null;
|
||||
if (x.isOdd != (sign == 1)) x = _p - x;
|
||||
return _Point(x, y, BigInt.one, x * y % _p);
|
||||
}
|
||||
|
||||
List<int> _encode(_Point point) {
|
||||
final inverse = point.z.modInverse(_p);
|
||||
final x = point.x * inverse % _p;
|
||||
final y = point.y * inverse % _p;
|
||||
final out = _bytes(y);
|
||||
if (x.isOdd) out[31] |= 0x80;
|
||||
return out;
|
||||
}
|
||||
|
||||
// The unified addition law (RFC 8032 section 5.1.4); it also doubles.
|
||||
_Point _add(_Point p, _Point q) {
|
||||
final a = (p.y - p.x) * (q.y - q.x) % _p;
|
||||
final b = (p.y + p.x) * (q.y + q.x) % _p;
|
||||
final c = p.t * BigInt.two * _d % _p * q.t % _p;
|
||||
final d = p.z * BigInt.two * q.z % _p;
|
||||
final e = (b - a) % _p, f = (d - c) % _p, g = (d + c) % _p, h = (b + a) % _p;
|
||||
return _Point(e * f % _p, g * h % _p, f * g % _p, e * h % _p);
|
||||
}
|
||||
|
||||
_Point _multiply(_Point point, BigInt scalar) {
|
||||
var result = _identity;
|
||||
var addend = point;
|
||||
var rest = scalar;
|
||||
while (rest > BigInt.zero) {
|
||||
if (rest.isOdd) result = _add(result, addend);
|
||||
addend = _add(addend, addend);
|
||||
rest >>= 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool _equal(List<int> a, List<int> b) {
|
||||
if (a.length != b.length) return false;
|
||||
for (var i = 0; i < a.length; i++) {
|
||||
if (a[i] != b[i]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'ed25519.dart';
|
||||
|
||||
/// The Ed25519 public keys this build trusts to sign update feeds, base64
|
||||
/// and comma separated (`--dart-define=PIPELINES_UPDATE_PUBLIC_KEY`). The
|
||||
/// installer embeds its own key and any listed in
|
||||
/// `~/.config/pipelines/update-trusted-keys`; see
|
||||
/// docs/notes/network-and-update-signing.md.
|
||||
const embeddedUpdateKeys = String.fromEnvironment(
|
||||
'PIPELINES_UPDATE_PUBLIC_KEY',
|
||||
);
|
||||
|
||||
/// The trusted keys in [configured]; a malformed entry is dropped (the
|
||||
/// installer refuses to build with one), and an empty result means this is
|
||||
/// an unsigned development build.
|
||||
List<List<int>> trustedUpdateKeys([String configured = embeddedUpdateKeys]) {
|
||||
final keys = <List<int>>[];
|
||||
for (final entry in configured.split(',')) {
|
||||
final text = entry.trim();
|
||||
if (text.isEmpty) continue;
|
||||
try {
|
||||
final key = base64.decode(text);
|
||||
if (key.length == 32) keys.add(key);
|
||||
} on FormatException {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
/// Why the [feed] bytes cannot be trusted, or null when they can.
|
||||
///
|
||||
/// [signatureDocument] is the detached `version.json.sig`
|
||||
/// (`{"algorithm":"ed25519","publicKey":…,"signature":…}`), null when the
|
||||
/// server offers none. A build with no trusted keys accepts any feed, as
|
||||
/// before signing existed; a build with keys requires a valid signature by
|
||||
/// one of them.
|
||||
String? feedSignatureProblem(
|
||||
List<int> feed,
|
||||
String? signatureDocument,
|
||||
List<List<int>> trusted,
|
||||
) {
|
||||
if (trusted.isEmpty) return null;
|
||||
if (signatureDocument == null) {
|
||||
return 'The update feed is not signed, so it was not trusted.';
|
||||
}
|
||||
final Object? document;
|
||||
try {
|
||||
document = jsonDecode(signatureDocument);
|
||||
} on FormatException {
|
||||
return 'The update feed signature is unreadable.';
|
||||
}
|
||||
if (document is! Map || document['algorithm'] != 'ed25519') {
|
||||
return 'The update feed signature is unreadable.';
|
||||
}
|
||||
final List<int> key, signature;
|
||||
try {
|
||||
key = base64.decode('${document['publicKey'] ?? ''}');
|
||||
signature = base64.decode('${document['signature'] ?? ''}');
|
||||
} on FormatException {
|
||||
return 'The update feed signature is unreadable.';
|
||||
}
|
||||
if (!trusted.any((candidate) => _same(candidate, key))) {
|
||||
return 'The update feed is signed by a key this app does not trust.';
|
||||
}
|
||||
if (!ed25519Verify(publicKey: key, message: feed, signature: signature)) {
|
||||
return 'The update feed signature does not match; it was not trusted.';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
bool _same(List<int> a, List<int> b) {
|
||||
if (a.length != b.length) return false;
|
||||
for (var i = 0; i < a.length; i++) {
|
||||
if (a[i] != b[i]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -125,4 +125,35 @@ void main() {
|
||||
expect(DiscoveredServer.isTailscaleIPv4('100.127.0.1'), isTrue);
|
||||
expect(DiscoveredServer.isTailscaleIPv4('100.128.0.1'), isFalse);
|
||||
});
|
||||
|
||||
test('lan=0 drops the LAN route and dials the tailnet first', () {
|
||||
final server = DiscoveredServer.fromRecord(
|
||||
instance: 'Pipelines-box',
|
||||
addresses: ['100.85.25.33'],
|
||||
port: 8083,
|
||||
attributes: {
|
||||
'name': 'box',
|
||||
'path': '/connect',
|
||||
'tls': '0',
|
||||
'auth': 'password',
|
||||
'lan': '0',
|
||||
'ts': '100.85.25.33',
|
||||
'public': 'https://box.tail1.ts.net:8443',
|
||||
},
|
||||
)!;
|
||||
expect(server.addresses.map((a) => a.route), [
|
||||
DiscoveredRoute.tailscale,
|
||||
DiscoveredRoute.public,
|
||||
]);
|
||||
expect(server.endpoint, Uri.parse('ws://100.85.25.33:8083/connect'));
|
||||
expect(
|
||||
DiscoveredServer.fromRecord(
|
||||
instance: 'Pipelines-box',
|
||||
addresses: ['100.85.25.33'],
|
||||
port: 8083,
|
||||
attributes: {'lan': '0'},
|
||||
),
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/updates/ed25519.dart';
|
||||
|
||||
List<int> hex(String text) => [
|
||||
for (var i = 0; i < text.length; i += 2)
|
||||
int.parse(text.substring(i, i + 2), radix: 16),
|
||||
];
|
||||
|
||||
// RFC 8032 section 7.1, tests 1-3.
|
||||
const vectors = [
|
||||
(
|
||||
'd75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a',
|
||||
'',
|
||||
'e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e06522490155'
|
||||
'5fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b',
|
||||
),
|
||||
(
|
||||
'3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c',
|
||||
'72',
|
||||
'92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da'
|
||||
'085ac1e43e15996e458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c00',
|
||||
),
|
||||
(
|
||||
'fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025',
|
||||
'af82',
|
||||
'6291d657deec24024827e69c3abe01a30ce548a284743a445e3680d7db5ac3ac'
|
||||
'18ff9b538d16f290ae67f760984dc6594a7c15e9716ed28dc027beceea1ec40a',
|
||||
),
|
||||
];
|
||||
|
||||
void main() {
|
||||
test('RFC 8032 vectors verify', () {
|
||||
for (final (key, message, signature) in vectors) {
|
||||
expect(
|
||||
ed25519Verify(
|
||||
publicKey: hex(key),
|
||||
message: hex(message),
|
||||
signature: hex(signature),
|
||||
),
|
||||
isTrue,
|
||||
reason: key,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('a changed message, signature or key is refused', () {
|
||||
final (key, message, signature) = vectors[2];
|
||||
bool verify(List<int> k, List<int> m, List<int> s) =>
|
||||
ed25519Verify(publicKey: k, message: m, signature: s);
|
||||
expect(verify(hex(key), hex('af83'), hex(signature)), isFalse);
|
||||
expect(verify(hex(key), hex(''), hex(signature)), isFalse);
|
||||
for (final index in [0, 31, 32, 63]) {
|
||||
final changed = hex(signature)..[index] ^= 1;
|
||||
expect(verify(hex(key), hex(message), changed), isFalse);
|
||||
}
|
||||
final otherKey = hex(vectors[1].$1);
|
||||
expect(verify(otherKey, hex(message), hex(signature)), isFalse);
|
||||
final changedKey = hex(key)..[0] ^= 1;
|
||||
expect(verify(changedKey, hex(message), hex(signature)), isFalse);
|
||||
});
|
||||
|
||||
test('malformed inputs are refused rather than thrown', () {
|
||||
final (key, message, signature) = vectors[1];
|
||||
expect(
|
||||
ed25519Verify(
|
||||
publicKey: hex(key).sublist(1),
|
||||
message: hex(message),
|
||||
signature: hex(signature),
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
expect(
|
||||
ed25519Verify(
|
||||
publicKey: hex(key),
|
||||
message: hex(message),
|
||||
signature: hex(signature).sublist(1),
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
// S must be below the group order L (no malleable second form).
|
||||
final high = hex(signature)..fillRange(32, 64, 0xff);
|
||||
expect(
|
||||
ed25519Verify(
|
||||
publicKey: hex(key),
|
||||
message: hex(message),
|
||||
signature: high,
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
// y = p (non-canonical) does not decode as a key.
|
||||
final invalidKey = List<int>.filled(32, 0xff)..[0] = 0xed;
|
||||
invalidKey[31] = 0x7f;
|
||||
expect(
|
||||
ed25519Verify(
|
||||
publicKey: invalidKey,
|
||||
message: hex(message),
|
||||
signature: hex(signature),
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
6Jo+xjitfGvKp28mdXIziIyDfs5UUP4xzFl92+xrAAU=
|
||||
@@ -0,0 +1 @@
|
||||
{"versionCode": 29400000, "versionName": "0.1.29400000", "platform": "linux", "architectures": ["x64"], "file": "pipelines-linux-x64-29400000-0123abcd.tar.gz", "sha256": "abababababababababababababababababababababababababababababababab", "notes": "fixture"}
|
||||
@@ -0,0 +1 @@
|
||||
{"algorithm": "ed25519", "publicKey": "6Jo+xjitfGvKp28mdXIziIyDfs5UUP4xzFl92+xrAAU=", "signature": "TpyvCl63pcKcO2Nu4uVI3tCLEN7w/SZe/yQWrs82OqY8C/2cP/o/uFWZ6aRJCo0yaoyTa30wQPSExhUJ08swDg=="}
|
||||
@@ -0,0 +1,157 @@
|
||||
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;
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -39,6 +39,7 @@ func main() {
|
||||
}
|
||||
db := flag.String("db", defaultDB, "SQLite database path (honors XDG_DATA_HOME)")
|
||||
mdnsOn := flag.Bool("mdns", envEnabled(os.Getenv("PIPELINES_MDNS")), "Advertise the server on the LAN with mDNS/DNS-SD ("+discovery.ServiceType+"); PIPELINES_MDNS=off disables")
|
||||
mdnsLAN := flag.Bool("mdns-lan", true, "Advertise the LAN route; false when the web proxy refuses LAN connections (only the tailnet and public routes are advertised)")
|
||||
mdnsPort := flag.Int("mdns-port", envPort(os.Getenv("PIPELINES_MDNS_PORT")), "Port LAN clients dial: the authenticated web proxy fronting this server (default: the -addr port when it is not loopback)")
|
||||
tokenFile := flag.String("token-file", os.Getenv(servertoken.EnvKey), "Require the shared secret in this private file on every request (the installer's server-token; PIPELINES_SERVER_TOKEN_FILE)")
|
||||
flag.Parse()
|
||||
@@ -217,7 +218,7 @@ func main() {
|
||||
chatNames = chats.Backends()
|
||||
}
|
||||
if *mdnsOn {
|
||||
defer keepAdvertising(*addr, *mdnsPort)()
|
||||
defer keepAdvertising(*addr, *mdnsPort, *mdnsLAN)()
|
||||
}
|
||||
printBanner(startup{Build: buildinfo.Current(), Addr: *addr, Database: *db, Artifacts: artifactsRoot,
|
||||
Authoring: *allowAuthoring, OpenCode: openCodeStatus, Chat: chatNames})
|
||||
|
||||
@@ -16,17 +16,17 @@ import (
|
||||
// advertise publishes the server on the LAN. Any failure is logged and the
|
||||
// server carries on: discovery is a convenience, never a startup requirement.
|
||||
// The returned stop (nil when nothing was published) withdraws the advert.
|
||||
func advertise(addr string, proxyPort int) func() {
|
||||
return advertiseWith(addr, proxyPort, discovery.LANAddresses(), discovery.TailscaleAddresses())
|
||||
func advertise(addr string, proxyPort int, lanRoute bool) func() {
|
||||
return advertiseWith(addr, proxyPort, lanRoute, discovery.LANAddresses(), discovery.TailscaleAddresses())
|
||||
}
|
||||
|
||||
func advertiseWith(addr string, proxyPort int, lan, tailnet []net.IP) func() {
|
||||
func advertiseWith(addr string, proxyPort int, lanRoute bool, lan, tailnet []net.IP) func() {
|
||||
hostname, _ := os.Hostname()
|
||||
// The installer exports the TLS front's origin (tailscale serve) for the
|
||||
// same proxy; it is advertised so clients can choose that route by name.
|
||||
advert, err := discovery.Plan(discovery.Config{ListenAddr: addr, ProxyPort: proxyPort,
|
||||
Version: buildinfo.Current().Version, Hostname: hostname, IPs: lan,
|
||||
TailscaleIPs: tailnet, PublicURL: os.Getenv("PIPELINES_WEB_PUBLIC_URL")})
|
||||
TailscaleIPs: tailnet, PublicURL: os.Getenv("PIPELINES_WEB_PUBLIC_URL"), NoLAN: !lanRoute})
|
||||
if err != nil {
|
||||
log.Printf("mDNS: not advertising: %v", err)
|
||||
return nil
|
||||
@@ -66,9 +66,9 @@ const addressRecheck = 30 * time.Second
|
||||
|
||||
// keepAdvertising publishes the server now and republishes whenever the
|
||||
// host's LAN or tailnet addresses change. The returned stop ends both.
|
||||
func keepAdvertising(addr string, proxyPort int) func() {
|
||||
func keepAdvertising(addr string, proxyPort int, lanRoute bool) func() {
|
||||
lan, tailnet := discovery.LANAddresses(), discovery.TailscaleAddresses()
|
||||
stop := advertiseWith(addr, proxyPort, lan, tailnet)
|
||||
stop := advertiseWith(addr, proxyPort, lanRoute, lan, tailnet)
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
ticker := time.NewTicker(addressRecheck)
|
||||
@@ -87,7 +87,7 @@ func keepAdvertising(addr string, proxyPort int) func() {
|
||||
}
|
||||
lan, tailnet = nextLAN, nextTailnet
|
||||
log.Printf("mDNS: addresses changed to %s; republishing", joinIPs(lan))
|
||||
stop = advertiseWith(addr, proxyPort, lan, tailnet)
|
||||
stop = advertiseWith(addr, proxyPort, lanRoute, lan, tailnet)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -21,7 +21,7 @@ func TestMDNSEnvironmentSwitches(t *testing.T) {
|
||||
// A loopback-only server with no proxy port publishes nothing and does not
|
||||
// fail: the caller only defers a non-nil stop.
|
||||
func TestAdvertiseSkipsLoopbackWithoutFailing(t *testing.T) {
|
||||
if stop := advertise("127.0.0.1:0", 0); stop != nil {
|
||||
if stop := advertise("127.0.0.1:0", 0, true); stop != nil {
|
||||
stop()
|
||||
t.Fatal("loopback-only server was advertised")
|
||||
}
|
||||
@@ -44,6 +44,6 @@ func TestSameAddressesIgnoresOrder(t *testing.T) {
|
||||
// keepAdvertising on a loopback-only server publishes nothing, keeps its
|
||||
// watcher running and stops cleanly.
|
||||
func TestKeepAdvertisingStopsCleanly(t *testing.T) {
|
||||
stop := keepAdvertising("127.0.0.1:0", 0)
|
||||
stop := keepAdvertising("127.0.0.1:0", 0, true)
|
||||
stop()
|
||||
}
|
||||
|
||||
+11
-5
@@ -88,11 +88,13 @@ one; it does not touch that binary.
|
||||
|
||||
| Service | Default | Flag |
|
||||
| --- | --- | --- |
|
||||
| Web proxy (sign-in, web app, `/updates/`) | `0.0.0.0:8082` | `--web-port`, `--web-host` |
|
||||
| Web proxy (sign-in, web app, `/updates/`) | port 8082, loopback + tailnet only | `--web-port`, `--web-host`, `--web-lan` |
|
||||
| Server API (no auth) | `127.0.0.1:8080` | `--port`, `--bind-address` |
|
||||
| ntfy | `:8090` | `--ntfy-port`, `--ntfy-url` |
|
||||
|
||||
Expose only the web proxy. A reinstall keeps the saved ports and bind
|
||||
Expose only the web proxy. It listens on loopback and this machine's Tailscale
|
||||
addresses unless you opt into the LAN; see
|
||||
[network defaults](notes/network-and-update-signing.md). A reinstall keeps the saved ports and bind
|
||||
addresses unless you pass the flag again, so an installation set up with
|
||||
`--web-port 8083` stays on 8083.
|
||||
|
||||
@@ -101,7 +103,8 @@ so the phone app's "Find servers on this network" lists it. The advertised port
|
||||
is the web proxy's (when it listens beyond loopback; the supervisor passes
|
||||
`-mdns-port`), or the server's own `-addr` port when that is a LAN address.
|
||||
TXT records: `v` (version), `name` (host), `path=/connect`, `tls=0`,
|
||||
`auth=password|none`. Set `PIPELINES_MDNS=off` (or `-mdns=false`) to disable
|
||||
`auth=password|none`, plus `ts=`/`public=` routes; without `--web-lan` the
|
||||
advert carries no LAN route (`lan=0`, address records are the tailnet IPs). Set `PIPELINES_MDNS=off` (or `-mdns=false`) to disable
|
||||
it; a failure to advertise is logged and never stops the server.
|
||||
|
||||
`PIPELINES_WEB_PUBLIC_URL` is the address phones and notification links use.
|
||||
@@ -111,7 +114,8 @@ Set it once; reinstalls keep it:
|
||||
PIPELINES_WEB_PUBLIC_URL=https://mymac.tailnet-name.ts.net ./install
|
||||
```
|
||||
|
||||
Without it the installer records `http://<LAN IP>:<web-port>`. Use HTTPS or a
|
||||
Without it the installer records the tailnet IP (or `127.0.0.1`), or
|
||||
`http://<LAN IP>:<web-port>` where LAN sign-in is allowed. Use HTTPS or a
|
||||
VPN for remote access; plain HTTP does not protect the password.
|
||||
|
||||
## Flags
|
||||
@@ -124,7 +128,9 @@ From `arguments()` in `scripts/setup-native.py`:
|
||||
| `--port N` | server API port (default 8080) |
|
||||
| `--bind-address IP` | server API bind (default `127.0.0.1`) |
|
||||
| `--web-port N` | web proxy port (default 8082) |
|
||||
| `--web-host IP` | web proxy bind (default `0.0.0.0`) |
|
||||
| `--web-host tailnet\|IP` | web proxy access: `tailnet` (default for new installs: loopback and Tailscale addresses only) or a literal IPv4 bind such as `0.0.0.0`; existing installs keep theirs and get a warning |
|
||||
| `--web-lan` | also accept plain-HTTP LAN connections; sign-in over them stays refused |
|
||||
| `--web-lan-plain-login` | allow sign-in over plain-HTTP LAN (unencrypted password and cookie; trusted networks only) |
|
||||
| `--ntfy-port N` | ntfy port (default 8090) |
|
||||
| `--ntfy-url URL` | ntfy address given to phones (default `http://<LAN IP>:<ntfy-port>`) |
|
||||
| `--timezone TZ` | override the detected timezone (fallback UTC) |
|
||||
|
||||
@@ -0,0 +1,248 @@
|
||||
# Network defaults and update-feed signing
|
||||
|
||||
## Where the web proxy listens
|
||||
|
||||
Before this change `./install` bound the web proxy to `0.0.0.0` over plain
|
||||
HTTP. With the macOS firewall off, anyone on the same café or hotel Wi-Fi could
|
||||
reach the sign-in page, and the password and the 30-day session cookie crossed
|
||||
that network unencrypted. The operator reaches the Mac over Tailscale
|
||||
(`https://<host>.ts.net:8443` through `tailscale serve` to `127.0.0.1:8083`,
|
||||
and `ws://100.x:8083` from tank), so the LAN listener was not needed.
|
||||
|
||||
### Defaults and opt-ins
|
||||
|
||||
`web.host` in `pipelines-install.json` takes one of two forms:
|
||||
|
||||
| `web.host` | Proxy flags (`manage.web_listen_args`) | Who can connect |
|
||||
| --- | --- | --- |
|
||||
| `tailnet` (new installs) | `--host :: --allow loopback,tailnet` | loopback and this machine's Tailscale IPv4/IPv6 addresses |
|
||||
| `tailnet` with `web.lan: true` | `... --allow loopback,tailnet,lan` | also the LAN, but no sign-in or signed-in pages over plain HTTP |
|
||||
| `tailnet` with `web.lan: true`, `web.lan_plain_login: true` | `... --lan-plain-login` | the LAN, with plain-HTTP sign-in (the old behaviour) |
|
||||
| an IPv4 address, e.g. `0.0.0.0` | `--host 0.0.0.0` | whatever the bind reaches, unfiltered (the old behaviour) |
|
||||
|
||||
Installer flags: `--web-host tailnet|IPv4`, `--web-lan` / `--no-web-lan`,
|
||||
`--web-lan-plain-login` / `--no-web-lan-plain-login`. A reinstall keeps the
|
||||
saved values unless you pass the flag again. `--web-lan` on an install that
|
||||
still has a literal host moves it to `tailnet`. `--web-lan` together with an
|
||||
explicit IPv4 `--web-host` is refused.
|
||||
|
||||
On an opted-in LAN without `lan_plain_login`, a plain-HTTP LAN connection gets
|
||||
`403` with an explanation for `POST /login`, `GET /login`, every signed-in
|
||||
page, `/updates/`, `/api/*` and websocket upgrades. Only the public icons and
|
||||
manifest are served. **Risk of `web.lan_plain_login`:** anyone on that network
|
||||
who can watch the traffic sees the password and session cookie, and the cookie
|
||||
works for 30 days. Use it only on a network you trust. `X-Forwarded-Proto` is
|
||||
not trusted from the LAN, because a client can set it to anything.
|
||||
|
||||
### Why the proxy binds the wildcard and filters each connection
|
||||
|
||||
Tailscale often starts after the proxy (both start at login), and its address
|
||||
can change. Binding the tailnet IP itself would fail at boot, and keeping it
|
||||
working would need retry and rebind logic. Instead the proxy binds `::`, which
|
||||
on macOS and Linux also accepts IPv4 (it falls back to `0.0.0.0` on a host
|
||||
without IPv6). On every `connection` it drops the socket, before any HTTP is
|
||||
read, unless **both** ends are loopback (`127/8`, `::1`) or **both** are
|
||||
tailnet (`100.64.0.0/10`, `fd7a:115c:a1e0::/48`); see
|
||||
`web/src/listenPolicy.js`.
|
||||
|
||||
Both ends are checked because macOS uses the weak host model: it accepts a
|
||||
packet addressed to its tailnet IP on any interface. A LAN host that spoofs a
|
||||
tailnet source address cannot finish the TCP handshake, because the reply is
|
||||
routed into the tunnel.
|
||||
|
||||
The cost: a LAN scan sees the port open and immediately reset, and no HTTP is
|
||||
served. One edge case is not handled: Tailscale subnet routes without SNAT
|
||||
deliver peers with non-tailnet source addresses, and those are treated as LAN.
|
||||
|
||||
### Discovery (mDNS)
|
||||
|
||||
`manage.mdns_args` passes `-mdns-lan=false` when `web.host` is `tailnet`
|
||||
without `web.lan`. `discovery.Plan` with `NoLAN` then makes these changes:
|
||||
|
||||
- The A records are the tailnet IPv4 addresses, not the LAN ones.
|
||||
- TXT gains `lan=0`, and `ts=` and `public=` stay.
|
||||
- The port is still the proxy's, which answers on the tailnet.
|
||||
- With no tailnet address, nothing is published (`ErrNotReachable`).
|
||||
|
||||
The advert is still multicast on the LAN, so a tailnet client on the same Wi-Fi
|
||||
finds the machine. `Browse` (Go, also behind `/api/discovery`) and the app's
|
||||
`DiscoveredServer.fromRecord` drop the LAN route when `lan=0`, so the endpoint
|
||||
becomes the first tailnet or public route. An older client still sees a "LAN"
|
||||
route, but it points at the tailnet IP, which works from the tailnet.
|
||||
|
||||
### Firewall
|
||||
|
||||
The installer turns nothing on at the OS level. It only reads
|
||||
`socketfilterfw --getglobalstate` and prints a recommendation when the firewall
|
||||
is off. On this Mac on 2026-11-10 it reported `Firewall is disabled.
|
||||
(State = 0)`. Recommendation: turn it on (System Settings > Network >
|
||||
Firewall). The proxy no longer depends on it, but other local services
|
||||
(ntfy on `:8090`, development servers) do.
|
||||
|
||||
### Migration
|
||||
|
||||
- **Live Mac** (`web.host: 0.0.0.0`, port 8083, public URL
|
||||
`https://alans-macbook-pro.tail6bf4dc.ts.net:8443`): a plain `./install`
|
||||
keeps `0.0.0.0` and prints
|
||||
`WARNING: the web proxy listens on 0.0.0.0 over plain HTTP ... Recommended: ./install --web-host tailnet ...`.
|
||||
To switch, run `./install --web-host tailnet` while no run or chat is active
|
||||
(the install restarts the services). `tailscale serve` still reaches
|
||||
`127.0.0.1:8083`, and tank's `ws://100.85.25.33:8083` is a tailnet
|
||||
connection, so both keep working. Phones on the home Wi-Fi that saved
|
||||
`ws://10.0.0.x:8083` must switch to the tailnet route (rediscover, or pick
|
||||
the Tailscale address).
|
||||
- **tank:** the same `./install --web-host tailnet`. Check first that every
|
||||
client reaches it through Tailscale.
|
||||
- An install whose recorded `PIPELINES_WEB_PUBLIC_URL` is a plain-HTTP LAN
|
||||
address gets a warning to set it to the tailnet HTTPS address.
|
||||
|
||||
### Verification (network)
|
||||
|
||||
- `cd web && node --test test/`: `tests 24, pass 24, fail 0`. This includes
|
||||
`listenPolicy.test.mjs`, which uses real sockets on this Mac's LAN (10.0.0.x)
|
||||
and tailnet (100.85.25.33) addresses.
|
||||
- `umask 022 && go test ./internal/discovery ./cmd/pipelines-server ./internal/server`:
|
||||
all `ok`. This includes `TestPlanWithoutLANAdvertisesOnlyTailnetAndPublicRoutes`
|
||||
and `TestBrowseOmitsTheLANRouteWhenTheAdvertSaysLan0`.
|
||||
- `cd scripts && python3 -m unittest`: `Ran 229 tests ... OK (skipped=1)`.
|
||||
This includes `test_web_access` and
|
||||
`test_upgrade_keeps_an_all_interfaces_web_host_with_a_warning_and_lan_is_opt_in`.
|
||||
- App: `tool/memwatch/full.sh 'test/features/updates test/features/onboarding test/features/settings'`
|
||||
gave `PASS: 212 passed, 0 failed`, and `tool/analyze.sh` was clean.
|
||||
- Scratch end-to-end: an isolated `HOME`, an OS-chosen port, real sign-in, and
|
||||
the proxy started with `manage.web_listen_args` output:
|
||||
|
||||
```
|
||||
== default loopback + tailnet: serve.mjs --port 53780 --host :: --allow loopback,tailnet
|
||||
GET http://127.0.0.1:53780/login -> 200 POST -> 303
|
||||
GET http://100.85.25.33:53780/login -> 200 POST -> 303
|
||||
GET http://10.0.0.249:53780/login -> 000 (connection reset, curl exit 56)
|
||||
== LAN opted in, no plain login: ... --allow loopback,tailnet,lan
|
||||
GET/POST via 127.0.0.1 and 100.85.25.33 -> 200/303; via 10.0.0.249 -> 403/403
|
||||
== LAN with lan_plain_login: ... --lan-plain-login
|
||||
GET/POST via 10.0.0.249 -> 200/303
|
||||
```
|
||||
|
||||
The live mDNS advert was not exercised, because a scratch server would publish
|
||||
the same `Pipelines-<host>` instance name as the live one. The records are
|
||||
covered by `Plan`/`Zone` tests.
|
||||
|
||||
## Update feed signing
|
||||
|
||||
Before this change the app trusted whatever `/updates/` served: the Linux
|
||||
self-update installed and relaunched an archive whose sha256 came from the same
|
||||
`version.json`, Android skipped its signer check when the feed omitted the
|
||||
field, and the macOS zip had no check beyond that sha256. A server (or anyone
|
||||
able to write its update directory, or answer for its address) could hand every
|
||||
client another build.
|
||||
|
||||
### What is signed
|
||||
|
||||
- Publishing (`./install --apk`, `./install --linux|--macos --publish`) writes
|
||||
`version.json` and, beside it, a detached `version.json.sig`:
|
||||
`{"algorithm":"ed25519","publicKey":"<base64 32 bytes>","signature":"<base64 64 bytes>"}`.
|
||||
The signature covers the exact bytes of `version.json`, which carry the
|
||||
artifact `sha256` (and, for Android, `signingCertificateSha256`). The
|
||||
signature is written first, then the feed, both atomically; a check racing a
|
||||
publish fails once and succeeds on the next check.
|
||||
- Code: `scripts/update_signing.py` (keys, signing, `publish_feed`),
|
||||
`scripts/app_build.py` (`build_apk`, web and desktop defines),
|
||||
`scripts/app_desktop_build.py` (`publish_desktop(..., signer=...)`).
|
||||
|
||||
### Key management
|
||||
|
||||
- Each installation has one Ed25519 key in
|
||||
`~/.config/pipelines/update-signing-key.pem` (mode 0600, directory 0700),
|
||||
generated once on the first `./install` or app build/publish and never
|
||||
replaced. `PIPELINES_UPDATE_SIGNING_KEY` names another path. A key file that
|
||||
is a symlink or readable by others is refused. It is never in the repository.
|
||||
`scripts/uninstall.py` (full reset) removes `~/.config/pipelines`, and so
|
||||
the key; see "Lost or replaced key" below.
|
||||
- Cryptography is the OpenSSL 3 command line (`genpkey`, `pkey`,
|
||||
`pkeyutl -sign -rawin`). The installer's Python has no `cryptography`
|
||||
package, and macOS's `/usr/bin/openssl` is LibreSSL 3.3.6, which has no
|
||||
Ed25519 (`unable to load Private Key`). The tool is chosen as
|
||||
`PIPELINES_OPENSSL`, then `/opt/homebrew/opt/openssl@3/bin/openssl`,
|
||||
`/usr/local/opt/openssl@3/bin/openssl`, then `openssl` on PATH, taking the
|
||||
first that can generate an Ed25519 key. Without one the install stops with
|
||||
`Update signing needs OpenSSL 3 with Ed25519 ...` (`brew install openssl@3`).
|
||||
On this Mac `openssl` on PATH is Homebrew OpenSSL 3.6.4.
|
||||
- Print this installation's public key:
|
||||
`python3 scripts/update_signing.py`.
|
||||
|
||||
### Trust model
|
||||
|
||||
- Every app build embeds the keys it trusts as
|
||||
`--dart-define=PIPELINES_UPDATE_PUBLIC_KEY=<key>[,<key>...]` (apk, linux,
|
||||
macos and web builds; the define is part of the build cache key, so a key
|
||||
change rebuilds). The list is this installation's own key first, then each
|
||||
base64 key in `~/.config/pipelines/update-trusted-keys` (one per line, `#`
|
||||
comments allowed; a malformed line stops the build).
|
||||
- Each server signs its own feed with its own key. A client accepts a feed
|
||||
only when `version.json.sig` names an embedded key and the signature over
|
||||
the served bytes verifies with it; the client checks the signature before it
|
||||
believes anything in the feed. So an app built on the Mac updates from tank
|
||||
only if tank's key was listed in the Mac's `update-trusted-keys` when that
|
||||
app was built (and vice versa). There is no trust-on-first-use pinning:
|
||||
trust comes only from the build, which keeps the model to one rule.
|
||||
- With keys embedded, the app refuses: an unsigned feed (no `.sig`), an
|
||||
unreadable signature, a key it does not embed, a bad signature, a feed
|
||||
without `sha256`, and an Android feed without `signingCertificateSha256`
|
||||
(or when the installed signature is unknown). The Android certificate must
|
||||
still match the installed app's, as before.
|
||||
- A build with no embedded key (a developer `flutter run`/`flutter build`
|
||||
outside the installer) keeps the previous unsigned behaviour. Installer
|
||||
builds always embed a key.
|
||||
- Verification in the app is pure Dart (`app/lib/features/updates/ed25519.dart`,
|
||||
RFC 8032 verify only, BigInt arithmetic, checked against RFC 8032 test
|
||||
vectors 1-3; no new package) and
|
||||
`app/lib/features/updates/update_feed_signature.dart`; the reader is
|
||||
`fetchLatestScoped` in `app_updates_io.dart`. The web Settings "Android
|
||||
download" link is a browser download the operator installs by hand and is
|
||||
unchanged.
|
||||
|
||||
### Migration (the live Mac install and tank)
|
||||
|
||||
Not done by this change; nothing was deployed. In order, on each machine:
|
||||
|
||||
1. `./install` (or the next `./install --apk` / desktop publish) generates
|
||||
`~/.config/pipelines/update-signing-key.pem`. Check:
|
||||
`ls -l ~/.config/pipelines/update-signing-key.pem` shows `-rw-------`.
|
||||
2. To let one machine's apps update from the other, exchange keys before
|
||||
building: on tank run `python3 scripts/update_signing.py` and append the
|
||||
line to the Mac's `~/.config/pipelines/update-trusted-keys`; do the
|
||||
reverse on tank. Skip this if each machine's apps only update from it.
|
||||
3. Rebuild and publish: `./install --apk`, `./install --macos --publish
|
||||
--flutter <flutter>`, `./install --linux --publish`. Each feed directory
|
||||
then holds `version.json.sig`.
|
||||
4. Installed apps from before this change do not check signatures; they take
|
||||
the next signed release normally (the `.sig` is ignored), and from then on
|
||||
they refuse unsigned or foreign feeds. Phones whose feed came from the other
|
||||
machine need step 2 before step 3.
|
||||
5. Lost or replaced key (for example after a full uninstall): apps built with
|
||||
the old key refuse the new feeds. Install the new build once by hand (APK
|
||||
download from Settings in the browser, or the desktop archive), or restore
|
||||
the old key file from backup.
|
||||
|
||||
### Verification output
|
||||
|
||||
- `cd scripts && python3 -m unittest test_update_signing test_app_build
|
||||
test_app_desktop_build test_setup_native`: `Ran 81 tests ... OK`. Covers key
|
||||
generation once at 0600 in a scratch HOME, refusal of readable/symlinked
|
||||
keys, LibreSSL-style tools skipped, signatures checked independently with
|
||||
`openssl pkeyutl -verify` (and a tampered message refused), feed plus `.sig`
|
||||
publication, trusted-key list parsing, APK publication signed with the
|
||||
generated key, desktop publication passing the exact feed bytes to the
|
||||
signer, and the committed Dart fixture being a valid OpenSSL signature.
|
||||
- `cd app && tool/memwatch/full.sh 'test/features/updates test/features/settings'`:
|
||||
`memwatch: PASS: 187 passed, 0 failed, 0 skipped`. New:
|
||||
`test/features/updates/ed25519_test.dart` (RFC 8032 vectors; changed
|
||||
message, signature, key; short inputs; S >= L; non-canonical key) and
|
||||
`test/features/updates/update_feed_signature_test.dart` (the
|
||||
Python-signed fixture `test/features/updates/fixtures/signed_feed/`
|
||||
verifies; tampered feed and signature refused; missing signature refused
|
||||
with keys, accepted without; untrusted and swapped keys refused; signed
|
||||
feeds require sha256 and the Android signer; and the HTTP reader
|
||||
`fetchLatestScoped` against a loopback server refuses an unsigned and a
|
||||
tampered feed and lets the genuine one past the signature check).
|
||||
`test/core/session`: `PASS: 18 passed`. `tool/analyze.sh`: `analyze: clean`.
|
||||
@@ -42,6 +42,11 @@ type Config struct {
|
||||
// PublicURL is the HTTPS origin a TLS front (tailscale serve) exposes for
|
||||
// the same web proxy, for example https://box.tail1.ts.net:8443.
|
||||
PublicURL string
|
||||
// NoLAN means the proxy refuses LAN connections (the installer's loopback
|
||||
// + tailnet default). The advert then carries no LAN route: its address
|
||||
// records are the tailnet IPv4 addresses, TXT says lan=0, and tailnet
|
||||
// clients on the same LAN still find the machine through ts= and public=.
|
||||
NoLAN bool
|
||||
}
|
||||
|
||||
// Advert is the record set the server publishes.
|
||||
@@ -77,7 +82,18 @@ func Plan(cfg Config) (Advert, error) {
|
||||
if port <= 0 || port > 65535 {
|
||||
return Advert{}, fmt.Errorf("advertised port %d is out of range", port)
|
||||
}
|
||||
if len(cfg.IPs) == 0 {
|
||||
ips := cfg.IPs
|
||||
if cfg.NoLAN {
|
||||
ips = nil
|
||||
for _, ip := range cfg.TailscaleIPs {
|
||||
if IsTailscaleIP(ip) {
|
||||
ips = append(ips, ip.To4())
|
||||
}
|
||||
}
|
||||
if len(ips) == 0 {
|
||||
return Advert{}, fmt.Errorf("LAN connections are refused and there is no tailnet address: %w", ErrNotReachable)
|
||||
}
|
||||
} else if len(ips) == 0 {
|
||||
return Advert{}, fmt.Errorf("no LAN IPv4 address: %w", ErrNotReachable)
|
||||
}
|
||||
label := hostLabel(cfg.Hostname)
|
||||
@@ -96,6 +112,9 @@ func Plan(cfg Config) (Advert, error) {
|
||||
"tls=0",
|
||||
"auth=" + auth,
|
||||
}
|
||||
if cfg.NoLAN {
|
||||
txt = append(txt, "lan=0")
|
||||
}
|
||||
if ts := joinIPs(cfg.TailscaleIPs); ts != "" {
|
||||
txt = append(txt, "ts="+ts)
|
||||
}
|
||||
@@ -106,7 +125,7 @@ func Plan(cfg Config) (Advert, error) {
|
||||
Instance: "Pipelines-" + label,
|
||||
Host: label + ".local.",
|
||||
Port: port,
|
||||
IPs: cfg.IPs,
|
||||
IPs: ips,
|
||||
TXT: txt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -157,3 +157,39 @@ func TestPlanAdvertisesTailnetAndPublicRoutes(t *testing.T) {
|
||||
t.Fatal("tailnet range check")
|
||||
}
|
||||
}
|
||||
|
||||
// With LAN connections refused the advert names no LAN route: the address
|
||||
// records are the tailnet IPs and TXT says lan=0, so tailnet clients on the
|
||||
// same LAN still find the machine on the proxy port that answers them.
|
||||
func TestPlanWithoutLANAdvertisesOnlyTailnetAndPublicRoutes(t *testing.T) {
|
||||
tailnetIPs := []net.IP{net.ParseIP("100.85.25.33"), net.ParseIP("fd7a:115c:a1e0::d501:199e")}
|
||||
advert, err := Plan(Config{ListenAddr: "127.0.0.1:8080", ProxyPort: 8083, Version: "v1", Hostname: "box", IPs: lan,
|
||||
TailscaleIPs: tailnetIPs, PublicURL: "https://box.tail1.ts.net:8443", NoLAN: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if advert.Port != 8083 || len(advert.IPs) != 1 || !advert.IPs[0].Equal(net.ParseIP("100.85.25.33")) {
|
||||
t.Fatalf("advert = %+v", advert)
|
||||
}
|
||||
for _, want := range []string{"lan=0", "ts=100.85.25.33", "public=https://box.tail1.ts.net:8443"} {
|
||||
if !slices.Contains(advert.TXT, want) {
|
||||
t.Fatalf("TXT %q lacks %s", advert.TXT, want)
|
||||
}
|
||||
}
|
||||
zone, err := advert.Zone()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
records := zone.Records(dns.Question{Name: advert.Host, Qtype: dns.TypeA, Qclass: dns.ClassINET})
|
||||
if len(records) != 1 || !records[0].(*dns.A).A.Equal(net.ParseIP("100.85.25.33")) {
|
||||
t.Fatalf("A records = %v", records)
|
||||
}
|
||||
if _, err := Plan(Config{ListenAddr: "127.0.0.1:8080", ProxyPort: 8083, Hostname: "box", IPs: lan, NoLAN: true,
|
||||
PublicURL: "https://box.tail1.ts.net:8443"}); !errors.Is(err, ErrNotReachable) {
|
||||
t.Fatalf("no tailnet address must not publish: %v", err)
|
||||
}
|
||||
withLAN, _ := Plan(Config{ListenAddr: "127.0.0.1:8080", ProxyPort: 8083, Hostname: "box", IPs: lan, TailscaleIPs: tailnetIPs})
|
||||
if slices.Contains(withLAN.TXT, "lan=0") || !withLAN.IPs[0].Equal(lan[0]) {
|
||||
t.Fatalf("LAN advert changed: %+v", withLAN)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,6 +121,10 @@ func discovered(entry *mdns.ServiceEntry) (FoundServer, bool) {
|
||||
}
|
||||
endpoint := (&url.URL{Scheme: scheme, Host: net.JoinHostPort(ip.String(), strconv.Itoa(entry.Port)), Path: path}).String()
|
||||
addresses := []FoundAddress{{Kind: "lan", Endpoint: endpoint}}
|
||||
if fields["lan"] == "0" {
|
||||
// The proxy refuses LAN connections; the address record is a tailnet IP.
|
||||
addresses = nil
|
||||
}
|
||||
for _, value := range strings.Split(fields["ts"], ",") {
|
||||
ts := net.ParseIP(strings.TrimSpace(value))
|
||||
if ts == nil || !IsTailscaleIP(ts) || len(addresses) > 5 {
|
||||
@@ -137,5 +141,8 @@ func discovered(entry *mdns.ServiceEntry) (FoundServer, bool) {
|
||||
}
|
||||
addresses = append(addresses, FoundAddress{Kind: "public", Endpoint: (&url.URL{Scheme: wsScheme, Host: u.Host, Path: path}).String()})
|
||||
}
|
||||
return FoundServer{Name: name, Endpoint: endpoint, Auth: auth, Version: fields["v"], Addresses: addresses}, true
|
||||
if len(addresses) == 0 {
|
||||
return FoundServer{}, false
|
||||
}
|
||||
return FoundServer{Name: name, Endpoint: addresses[0].Endpoint, Auth: auth, Version: fields["v"], Addresses: addresses}, true
|
||||
}
|
||||
|
||||
@@ -87,3 +87,21 @@ func TestBrowseListsEveryRouteToTheSameMachine(t *testing.T) {
|
||||
t.Fatalf("unsafe routes accepted: %+v", found.Addresses)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBrowseOmitsTheLANRouteWhenTheAdvertSaysLan0(t *testing.T) {
|
||||
item := service("100.85.25.33")
|
||||
item.InfoFields = append(item.InfoFields, "lan=0", "ts=100.85.25.33", "public=https://box.tail1.ts.net:8443")
|
||||
found, ok := discovered(item)
|
||||
want := []FoundAddress{
|
||||
{Kind: "tailscale", Endpoint: "ws://100.85.25.33:8083/connect"},
|
||||
{Kind: "public", Endpoint: "wss://box.tail1.ts.net:8443/connect"},
|
||||
}
|
||||
if !ok || !slices.Equal(found.Addresses, want) || found.Endpoint != want[0].Endpoint {
|
||||
t.Fatalf("found = %+v (%v)", found, ok)
|
||||
}
|
||||
bare := service("100.85.25.33")
|
||||
bare.InfoFields = append(bare.InfoFields, "lan=0")
|
||||
if _, ok := discovered(bare); ok {
|
||||
t.Fatal("an advert with no usable route was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
+16
-8
@@ -27,6 +27,9 @@ from urllib.parse import urlsplit
|
||||
REPO = Path(__file__).resolve().parents[1]
|
||||
APP = REPO / "app"
|
||||
DEFAULT_PREFIX = Path.home() / ".local/share/pipelines"
|
||||
_signing_spec = importlib.util.spec_from_file_location("pipelines_update_signing", Path(__file__).with_name("update_signing.py"))
|
||||
update_signing = importlib.util.module_from_spec(_signing_spec)
|
||||
_signing_spec.loader.exec_module(update_signing)
|
||||
|
||||
def default_cache():
|
||||
return Path(os.environ.get("PIPELINES_APP_CACHE") or Path.home() / ".cache/pipelines/app-builds")
|
||||
@@ -346,7 +349,10 @@ def build_apk(prefix, server_url=None, notes="", runner=subprocess.run, log=prin
|
||||
server = server_url or phone_server_url(manifest)
|
||||
# The build number is the source key's build date, so the same sources
|
||||
# reuse one APK and every rebuild is newer than the last.
|
||||
arguments = (f"--dart-define=PIPELINES_SERVER_URL={server}",)
|
||||
# The feed is signed and the app embeds the keys it trusts (see
|
||||
# scripts/update_signing.py), so a server cannot hand phones another APK.
|
||||
signer = update_signing.Signer()
|
||||
arguments = (f"--dart-define=PIPELINES_SERVER_URL={server}", *update_signing.dart_defines())
|
||||
host_flutter, sdk = native_flutter(flutter), android_sdk_dir()
|
||||
use_native = native if native is not None else bool(host_flutter and sdk)
|
||||
if use_native:
|
||||
@@ -365,9 +371,9 @@ def build_apk(prefix, server_url=None, notes="", runner=subprocess.run, log=prin
|
||||
target.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copyfile(output / "pipelines.apk", target / "pipelines.apk.next")
|
||||
os.replace(target / "pipelines.apk.next", target / "pipelines.apk")
|
||||
(target / "version.json").write_text(json.dumps({
|
||||
update_signing.publish_feed(target, {
|
||||
"versionCode": code, "versionName": f"0.1.{code}", "apk": "pipelines.apk", "notes": notes,
|
||||
"signingCertificateSha256": fingerprint, "sha256": apk_sha256}) + "\n")
|
||||
"signingCertificateSha256": fingerprint, "sha256": apk_sha256}, signer)
|
||||
log(f"APK ready: {target / 'pipelines.apk'} (build {code}, server {server}).")
|
||||
log("Phones get it from Settings → App updates → Check for updates, or download "
|
||||
f"{server.replace('wss://', 'https://').replace('ws://', 'http://').removesuffix('/connect')}/updates/pipelines.apk")
|
||||
@@ -380,9 +386,10 @@ def print_web_urls(manifest, lan_host=None):
|
||||
if not web.get("port"):
|
||||
return
|
||||
host = web.get("host", "0.0.0.0")
|
||||
local = "localhost" if host in ("0.0.0.0", "127.0.0.1") else host
|
||||
local = "localhost" if host in ("0.0.0.0", "127.0.0.1", "tailnet") else host
|
||||
urls = [f"http://{local}:{web['port']}/"]
|
||||
if host == "0.0.0.0":
|
||||
# The LAN URL only where a LAN browser can sign in (see web_access.py).
|
||||
if host == "0.0.0.0" or (host == "tailnet" and web.get("lan") is True and web.get("lan_plain_login") is True):
|
||||
lan_host = lan_host or urlsplit(manifest.get("public_url", "")).hostname
|
||||
if lan_host and lan_host not in ("localhost", "127.0.0.1", "0.0.0.0"):
|
||||
urls.append(f"http://{lan_host}:{web['port']}/")
|
||||
@@ -416,13 +423,14 @@ def main(argv=None):
|
||||
bundle, archive = desktop.build_desktop(args.kind, app=APP, cache=default_cache(), runner=subprocess.run,
|
||||
source_digest=digest, docker_build=build, docker_ready=docker_available,
|
||||
server_url=args.server_url, flutter=args.flutter, output=args.output,
|
||||
prune_cache=prune)
|
||||
prune_cache=prune, update_keys=update_signing.dart_defines())
|
||||
if args.publish:
|
||||
manifest = load_manifest(args.prefix)
|
||||
updates = (manifest.get("web") or {}).get("updates")
|
||||
if not updates:
|
||||
raise ValueError("This installation has no app update directory; rerun ./install")
|
||||
desktop.publish_desktop(args.kind, bundle, archive, Path(updates), notes=args.notes)
|
||||
desktop.publish_desktop(args.kind, bundle, archive, Path(updates), notes=args.notes,
|
||||
signer=update_signing.Signer())
|
||||
return
|
||||
if args.kind == "apk":
|
||||
build_apk(args.prefix, args.server_url, args.notes, flutter=args.flutter, native=args.native)
|
||||
@@ -434,7 +442,7 @@ def main(argv=None):
|
||||
web = manifest.get("web") or {}
|
||||
if not web.get("root"):
|
||||
raise ValueError("This installation does not serve the app; rerun ./install")
|
||||
install_tree(build("web"), Path(web["root"]))
|
||||
install_tree(build("web", arguments=update_signing.dart_defines()), Path(web["root"]))
|
||||
print(f"Web app installed at {web['root']}; reload the page.")
|
||||
print_web_urls(manifest)
|
||||
|
||||
|
||||
@@ -120,8 +120,8 @@ def _macos_build(*, app, cache, source_digest, runner, arguments, flutter, log):
|
||||
|
||||
def build_desktop(kind, *, app, cache, runner, source_digest, docker_build,
|
||||
docker_ready, server_url=None, flutter=None, output=None, log=print,
|
||||
prune_cache=None):
|
||||
arguments = _arguments(server_url)
|
||||
prune_cache=None, update_keys=()):
|
||||
arguments = _arguments(server_url) + tuple(update_keys)
|
||||
if kind == "macos":
|
||||
entry, bundle, archive = _macos_build(app=app, cache=cache, runner=runner,
|
||||
source_digest=source_digest, arguments=arguments, flutter=flutter, log=log)
|
||||
@@ -154,8 +154,10 @@ def build_desktop(kind, *, app, cache, runner, source_digest, docker_build,
|
||||
return bundle, archive
|
||||
|
||||
|
||||
def publish_desktop(kind, bundle, archive, updates, *, notes="", log=print):
|
||||
"""Publish archive before its feed; versioned names preserve in-flight reads."""
|
||||
def publish_desktop(kind, bundle, archive, updates, *, signer, notes="", log=print):
|
||||
"""Publish archive before its feed; versioned names preserve in-flight reads.
|
||||
|
||||
[signer] returns the detached `version.json.sig` for the feed bytes."""
|
||||
entry = bundle.parent if kind == "macos" else bundle
|
||||
complete = json.loads((entry / ".complete").read_text())
|
||||
code = int(complete["built"]) // 60
|
||||
@@ -176,8 +178,11 @@ def publish_desktop(kind, bundle, archive, updates, *, notes="", log=print):
|
||||
previous = json.loads(feed.read_text())
|
||||
if previous.get("versionCode", 0) > code:
|
||||
raise ValueError("Refusing to publish an older build over a newer update")
|
||||
data = (json.dumps(metadata) + "\n").encode()
|
||||
# The signature lands before the feed it covers, after the archive.
|
||||
for destination, content in ((target / filename, archive.read_bytes()),
|
||||
(feed, (json.dumps(metadata) + "\n").encode())):
|
||||
(target / "version.json.sig", signer(data)),
|
||||
(feed, data)):
|
||||
descriptor, temporary = tempfile.mkstemp(prefix=destination.name + ".", dir=target)
|
||||
try:
|
||||
with os.fdopen(descriptor, "wb") as output:
|
||||
|
||||
+18
-4
@@ -52,10 +52,24 @@ def mdns_args(config):
|
||||
"""
|
||||
web = config.get("web") or {}
|
||||
if web.get("port") and web.get("host") not in (None, "", "127.0.0.1", "localhost", "::1"):
|
||||
return ["-mdns-port", str(web["port"])]
|
||||
# Loopback + tailnet: the advert carries only the tailnet/public routes.
|
||||
lan = web.get("host") != "tailnet" or web.get("lan") is True
|
||||
return ["-mdns-port", str(web["port"])] + ([] if lan else ["-mdns-lan=false"])
|
||||
return []
|
||||
|
||||
|
||||
def web_listen_args(web):
|
||||
"""The proxy's bind and access flags. "tailnet" (the installer default)
|
||||
binds the wildcard and admits only loopback and tailnet connections, plus
|
||||
the LAN when web.lan is set; sign-in over plain-HTTP LAN additionally needs
|
||||
web.lan_plain_login. A literal address is bound as before, unfiltered."""
|
||||
if web.get("host") != "tailnet":
|
||||
return ["--host", web["host"]]
|
||||
lan = web.get("lan") is True
|
||||
return (["--host", "::", "--allow", "loopback,tailnet" + (",lan" if lan else "")]
|
||||
+ (["--lan-plain-login"] if lan and web.get("lan_plain_login") is True else []))
|
||||
|
||||
|
||||
def load(prefix):
|
||||
prefix = Path(prefix).expanduser().resolve()
|
||||
path = prefix / MARKER
|
||||
@@ -224,7 +238,7 @@ def start(config):
|
||||
wait_health(f"http://127.0.0.1:{config['server_port']}/healthz", timeout=75)
|
||||
if config.get("web"):
|
||||
web = config["web"]
|
||||
host = "127.0.0.1" if web["host"] == "0.0.0.0" else web["host"]
|
||||
host = "127.0.0.1" if web["host"] in ("0.0.0.0", "tailnet") else web["host"]
|
||||
end = time.monotonic() + 20
|
||||
while True:
|
||||
try:
|
||||
@@ -325,7 +339,7 @@ def report(config):
|
||||
if web:
|
||||
public = (config.get("opencode_env") or {}).get("PIPELINES_WEB_PUBLIC_URL")
|
||||
print("== Access")
|
||||
address = public or f"http://{web['host']}:{web['port']}"
|
||||
address = public or f"http://{'127.0.0.1' if web['host'] == 'tailnet' else web['host']}:{web['port']}"
|
||||
print(f" {'Web':<10} {address} (password in {web['auth_file']})")
|
||||
root = Path(web["root"]) if web.get("root") else None
|
||||
built = root is not None and (root / "index.html").is_file()
|
||||
@@ -440,7 +454,7 @@ def supervise(config):
|
||||
with (root / "logs/web.log").open("a") as web_log:
|
||||
web_child = subprocess.Popen(
|
||||
[web["node"], web["script"], "--port", str(web["port"]),
|
||||
"--host", web["host"], "--pipelines", f"127.0.0.1:{config['server_port']}",
|
||||
*web_listen_args(web), "--pipelines", f"127.0.0.1:{config['server_port']}",
|
||||
*(["--root", web["root"]] if web.get("root") else []),
|
||||
*(["--updates", web["updates"]] if web.get("updates") else [])],
|
||||
env=web_env, stdout=web_log, stderr=web_log)
|
||||
|
||||
+30
-9
@@ -44,6 +44,7 @@ lan = module("lan_address", "lan-address.py")
|
||||
release_lookup = module("resolve_releases", "resolve-releases.py")
|
||||
tokens = module("save_ntfy_token", "save-ntfy-token.py")
|
||||
app_build = module("app_build", "app_build.py")
|
||||
web_access = module("web_access", "web_access.py")
|
||||
|
||||
DEFAULT_PREFIX = Path.home() / ".local/share/pipelines"
|
||||
MARKER = "pipelines-install.json"
|
||||
@@ -310,10 +311,12 @@ class Setup:
|
||||
saved = {"port": existing.get("server_port"),
|
||||
"ntfy_port": existing.get("ntfy_port"),
|
||||
"bind_address": existing.get("bind_address"),
|
||||
"web_port": web.get("port"), "web_host": web.get("host")}
|
||||
"web_port": web.get("port"), "web_host": web.get("host"),
|
||||
"web_lan": web.get("lan"), "web_lan_plain_login": web.get("lan_plain_login")}
|
||||
supplied = {argument.split("=", 1)[0] for argument in args.argv}
|
||||
for name, value in saved.items():
|
||||
if value is not None and "--" + name.replace("_", "-") not in supplied:
|
||||
flag = name.replace("_", "-")
|
||||
if value is not None and not {"--" + flag, "--no-" + flag} & supplied:
|
||||
setattr(args, name, value)
|
||||
|
||||
# --- prefix layout ---------------------------------------------------
|
||||
@@ -368,6 +371,7 @@ class Setup:
|
||||
return {"node": node, "script": str(target / "serve.mjs"),
|
||||
"auth_file": str(auth), "host": args.web_host, "port": args.web_port,
|
||||
"server_token_file": str(server_token),
|
||||
"lan": args.web_lan, "lan_plain_login": args.web_lan_plain_login,
|
||||
"root": self.install_app_web(prefix, args, web_build), "updates": str(updates)}
|
||||
|
||||
def ensure_server_token(self, path):
|
||||
@@ -416,7 +420,8 @@ class Setup:
|
||||
raise ValueError("Docker is not running, so the web app cannot be built. The server is never "
|
||||
"installed without it; start Docker and rerun ./install.")
|
||||
try:
|
||||
built = app_build.build("web", runner=self.runner)
|
||||
built = app_build.build("web", runner=self.runner,
|
||||
arguments=app_build.update_signing.dart_defines(self.environ))
|
||||
except RuntimeError as error:
|
||||
raise ValueError(f"The web app build failed ({error}); nothing was installed") from None
|
||||
if not (Path(built) / "index.html").is_file():
|
||||
@@ -822,9 +827,11 @@ class Setup:
|
||||
raise ValueError("Web port must be between 1024 and 65535 and differ from server/ntfy ports")
|
||||
try:
|
||||
ipaddress.IPv4Address(args.bind_address)
|
||||
ipaddress.IPv4Address(args.web_host)
|
||||
except ValueError:
|
||||
raise ValueError("Invalid API/web bind address; use an IPv4 address such as 0.0.0.0 or 127.0.0.1") from None
|
||||
raise ValueError("Invalid API bind address; use an IPv4 address such as 127.0.0.1") from None
|
||||
listen, web_warnings = web_access.resolve(args.web_host, args.web_lan, args.web_lan_plain_login,
|
||||
{argument.split("=", 1)[0] for argument in args.argv})
|
||||
args.web_host, args.web_lan, args.web_lan_plain_login = listen["host"], listen["lan"], listen["lan_plain_login"]
|
||||
allow_authoring = self.authoring_role(prefix, args.allow_authoring)
|
||||
web_public_url = self.environ.get("PIPELINES_WEB_PUBLIC_URL", "")
|
||||
previous = prefix / MARKER
|
||||
@@ -886,7 +893,8 @@ class Setup:
|
||||
if self.environ.get(key):
|
||||
opencode_env[key] = self.environ[key]
|
||||
opencode_env["PIPELINES_DEFAULT_MODEL"] = args.model or pipeline_model
|
||||
opencode_env["PIPELINES_WEB_PUBLIC_URL"] = web_public_url or f"http://{lan_ip if args.web_host == '0.0.0.0' else args.web_host}:{args.web_port}"
|
||||
web_url = f"http://{web_access.default_url_host(web, lan_ip, web_access.tailnet_ipv4(self.runner))}:{args.web_port}"
|
||||
opencode_env["PIPELINES_WEB_PUBLIC_URL"] = web_public_url or web_url
|
||||
path_value = str(prefix / "bin") + os.pathsep + self.environ.get("PATH", "")
|
||||
manifest = {
|
||||
"format": 1,
|
||||
@@ -943,8 +951,13 @@ class Setup:
|
||||
self.serve_on_tailnet(opencode_env["PIPELINES_WEB_PUBLIC_URL"], args.web_port)
|
||||
print(f"ntfy is configured for {base_url}; server, web proxy and ntfy have login autostart ({service_path}).")
|
||||
print("Phone notifications: subscribe to the 'pipelines' topic as phone-reader with the web password.")
|
||||
print(f"Web: http://{lan_ip if args.web_host == '0.0.0.0' else args.web_host}:{args.web_port}/ "
|
||||
f"(sign-in password in {web['auth_file']}; existing credentials retained).")
|
||||
print(f"Web: {web_url}/ (sign-in password in {web['auth_file']}; existing credentials retained).")
|
||||
print("Web proxy access: " + ("loopback + tailnet" + (" + LAN" if args.web_lan else "")
|
||||
if args.web_host == web_access.TAILNET else f"bound to {args.web_host}"))
|
||||
for note in (*web_warnings, web_access.public_url_warning(opencode_env["PIPELINES_WEB_PUBLIC_URL"], web),
|
||||
web_access.firewall_note(self.runner)):
|
||||
if note:
|
||||
print(note)
|
||||
print("Login autostart is already enabled. To enable/start it again, run:\n "
|
||||
+ shlex.join([str(prefix / "bin/pipelines"), "service", "enable"]))
|
||||
print("Android app (optional, slower): ./install --apk builds it in Docker and offers it at "
|
||||
@@ -968,7 +981,15 @@ def arguments(argv=None):
|
||||
parser.add_argument("--timezone", help="Override the detected timezone (fallback: UTC)")
|
||||
parser.add_argument("--port", type=int, default=DEFAULT_SERVER_PORT, help="Pipelines server port")
|
||||
parser.add_argument("--web-port", type=int, default=8082, help="Authenticated web proxy port (default 8082)")
|
||||
parser.add_argument("--web-host", default="0.0.0.0", help="Web proxy bind address (default 0.0.0.0)")
|
||||
parser.add_argument("--web-host", help="Web proxy access: 'tailnet' (default for new installs: loopback and "
|
||||
"this machine's Tailscale addresses only) or an IPv4 bind address such as 0.0.0.0 "
|
||||
"(plain HTTP on every interface); reinstalls keep the saved value")
|
||||
parser.add_argument("--web-lan", action=argparse.BooleanOptionalAction, default=None,
|
||||
help="Also accept plain-HTTP connections from the LAN (with --web-host tailnet); "
|
||||
"sign-in over them still needs --web-lan-plain-login")
|
||||
parser.add_argument("--web-lan-plain-login", action=argparse.BooleanOptionalAction, default=None,
|
||||
help="Allow the password and signed-in pages over plain-HTTP LAN connections "
|
||||
"(unencrypted; only on a trusted network)")
|
||||
parser.add_argument("--bind-address", default=DEFAULT_BIND_ADDRESS,
|
||||
help="Unauthenticated server bind address (default 127.0.0.1; expose only the web proxy)")
|
||||
parser.add_argument("--ntfy-port", type=int, default=DEFAULT_NTFY_PORT, help="ntfy listen port")
|
||||
|
||||
@@ -83,6 +83,11 @@ class AppBuildTests(unittest.TestCase):
|
||||
feed = json.loads((updates / "version.json").read_text())
|
||||
self.assertEqual(feed["signingCertificateSha256"], "ab" * 32)
|
||||
self.assertEqual(feed["sha256"], app_build.hashlib.sha256((updates / "pipelines.apk").read_bytes()).hexdigest())
|
||||
# Signed with the installation key generated (0600) in the scratch home.
|
||||
key = Path(self.home.name) / ".config/pipelines/update-signing-key.pem"
|
||||
self.assertEqual(key.stat().st_mode & 0o777, 0o600)
|
||||
signature = json.loads((updates / "version.json.sig").read_text())
|
||||
self.assertEqual(signature["publicKey"], app_build.update_signing.public_key(key))
|
||||
(output / "signing-certificate-sha256.txt").write_text("invalid")
|
||||
with patch.object(app_build, "load_manifest", return_value=manifest), patch.object(app_build, "build", return_value=output):
|
||||
with self.assertRaisesRegex(ValueError, "exactly one"):
|
||||
|
||||
@@ -160,24 +160,36 @@ class DesktopBuildTests(unittest.TestCase):
|
||||
def test_linux_publication_has_matching_hash_and_rejects_downgrade(self):
|
||||
bundle, archive = self.build("linux")
|
||||
updates = self.root / "updates"
|
||||
metadata = desktop.publish_desktop("linux", bundle, archive, updates, log=lambda *_: None)
|
||||
signed = []
|
||||
signer = lambda data: signed.append(data) or b"signature document\n"
|
||||
metadata = desktop.publish_desktop("linux", bundle, archive, updates, signer=signer, log=lambda *_: None)
|
||||
target = updates / "linux-x64"
|
||||
self.assertEqual(json.loads((target / "version.json").read_text()), metadata)
|
||||
# The detached signature covers the exact published feed bytes.
|
||||
self.assertEqual(signed, [(target / "version.json").read_bytes()])
|
||||
self.assertEqual((target / "version.json.sig").read_bytes(), b"signature document\n")
|
||||
self.assertEqual(metadata["architectures"], ["x64"])
|
||||
self.assertEqual(metadata["sha256"], desktop.hashlib.sha256((target / metadata["file"]).read_bytes()).hexdigest())
|
||||
metadata["versionCode"] += 1
|
||||
(target / "version.json").write_text(json.dumps(metadata))
|
||||
with self.assertRaisesRegex(ValueError, "older build"):
|
||||
desktop.publish_desktop("linux", bundle, archive, updates)
|
||||
desktop.publish_desktop("linux", bundle, archive, updates, signer=signer)
|
||||
|
||||
@patch.object(desktop.platform, "system", return_value="Darwin")
|
||||
def test_macos_feed_uses_actual_architectures_and_native_build_stamp(self, _):
|
||||
bundle, archive = self.build("macos", flutter="/fixture/flutter")
|
||||
metadata = desktop.publish_desktop("macos", bundle, archive, self.root / "updates", log=lambda *_: None)
|
||||
metadata = desktop.publish_desktop("macos", bundle, archive, self.root / "updates",
|
||||
signer=lambda data: b"{}\n", log=lambda *_: None)
|
||||
self.assertTrue((self.root / "updates/macos/version.json.sig").is_file())
|
||||
self.assertEqual(metadata["architectures"], ["arm64", "x64"])
|
||||
command = next(c for c in self.runner.calls if c[1:3] == ["build", "macos"])
|
||||
self.assertIn(f"--dart-define=PIPELINES_NATIVE_BUILD_CODE={metadata['versionCode']}", command)
|
||||
|
||||
def test_desktop_builds_embed_the_trusted_update_keys(self):
|
||||
self.build("linux", update_keys=("--dart-define=PIPELINES_UPDATE_PUBLIC_KEY=abc",))
|
||||
run = next(c for c in self.runner.calls if c[:2] == ["docker", "run"])
|
||||
self.assertIn("--dart-define=PIPELINES_UPDATE_PUBLIC_KEY=abc", run)
|
||||
|
||||
def test_desktop_server_prefill_rejects_embedded_credentials(self):
|
||||
with self.assertRaisesRegex(ValueError, "embedded credentials"):
|
||||
self.build("linux", server_url="wss://secret@example.com/connect")
|
||||
|
||||
@@ -463,7 +463,11 @@ class SetupNativeTests(unittest.TestCase):
|
||||
self.assertEqual(config["opencode"], "/usr/bin/opencode")
|
||||
self.assertEqual(config["pi"], "")
|
||||
self.assertEqual(config["opencode_env"]["PIPELINES_DEFAULT_MODEL"], "fixture/host-default")
|
||||
self.assertEqual(config["opencode_env"]["PIPELINES_WEB_PUBLIC_URL"], "http://192.168.1.20:8082")
|
||||
# A fresh install's proxy admits loopback and tailnet only, so the
|
||||
# default web URL is not the LAN address.
|
||||
self.assertEqual(config["opencode_env"]["PIPELINES_WEB_PUBLIC_URL"], "http://127.0.0.1:8082")
|
||||
self.assertEqual((config["web"]["host"], config["web"]["lan"], config["web"]["lan_plain_login"]),
|
||||
("tailnet", False, False))
|
||||
host = json.loads(installer.harness_config_path().read_text())["profiles"]["opencode"]
|
||||
self.assertEqual(host["model"], "fixture/host-default")
|
||||
self.assertEqual(host["pipeline_model"], "fixture/host-default")
|
||||
@@ -509,11 +513,51 @@ class SetupNativeTests(unittest.TestCase):
|
||||
self.assertEqual((prefix / "secrets/publisher-token").read_text(), before)
|
||||
self.assertEqual(manage.load(prefix)["web"]["port"], 8096)
|
||||
self.assertEqual(output.getvalue().splitlines()[-4:], [
|
||||
" http://localhost:8096/", " http://192.168.1.20:8096/",
|
||||
" http://localhost:8096/", " http://127.0.0.1:8096/",
|
||||
"OpenCode is required (default stage and chat harness) and is set up by ./install.",
|
||||
"Next: set up your agent harnesses: pipelines harness setup claude|codex|opencode|pi"])
|
||||
self.assertFalse(any("token" in call for call in recorder.calls), "reinstall re-created the token")
|
||||
|
||||
def test_upgrade_keeps_an_all_interfaces_web_host_with_a_warning_and_lan_is_opt_in(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
installer = self.installer(directory, Recorder())
|
||||
prefix = Path(directory) / "prefix"
|
||||
|
||||
def install(argv):
|
||||
with redirect_stdout(io.StringIO()) as output:
|
||||
installer.run(setup_native.arguments(argv))
|
||||
return manage.load(prefix)["web"], output.getvalue()
|
||||
|
||||
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \
|
||||
patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \
|
||||
patch.object(setup_native.configure, "host_timezone", return_value="Europe/London"):
|
||||
# An install from before: the proxy on every interface.
|
||||
install(["--web-host", "0.0.0.0"])
|
||||
web, text = install([])
|
||||
self.assertEqual(web["host"], "0.0.0.0")
|
||||
self.assertIn("WARNING: the web proxy listens on 0.0.0.0 over plain HTTP", text)
|
||||
self.assertIn("./install --web-host tailnet", text)
|
||||
self.assertIn(" http://192.168.1.20:8082/", text)
|
||||
# Opting into the LAN listener selects the new model; sign-in
|
||||
# over the plain-HTTP LAN stays off until asked for.
|
||||
web, text = install(["--web-lan"])
|
||||
self.assertEqual((web["host"], web["lan"], web["lan_plain_login"]), ("tailnet", True, False))
|
||||
self.assertNotIn("WARNING: the web proxy listens", text)
|
||||
self.assertIn("Web proxy access: loopback + tailnet + LAN", text)
|
||||
# The URL recorded by the old install is kept, and flagged.
|
||||
self.assertIn("WARNING: the recorded web URL http://192.168.1.20:8082 is a plain-HTTP LAN", text)
|
||||
web, text = install(["--web-lan-plain-login"])
|
||||
self.assertEqual((web["host"], web["lan"], web["lan_plain_login"]), ("tailnet", True, True))
|
||||
self.assertIn("WARNING: web.lan_plain_login is on", text)
|
||||
web, _ = install(["--no-web-lan"])
|
||||
self.assertEqual((web["host"], web["lan"], web["lan_plain_login"]), ("tailnet", False, False))
|
||||
web, _ = install([])
|
||||
self.assertEqual((web["host"], web["lan"]), ("tailnet", False))
|
||||
with self.assertRaisesRegex(ValueError, "--web-lan applies to --web-host tailnet"):
|
||||
installer.run(setup_native.arguments(["--web-host", "0.0.0.0", "--web-lan"]))
|
||||
with self.assertRaisesRegex(ValueError, "Invalid --web-host"):
|
||||
installer.run(setup_native.arguments(["--web-host", "everywhere"]))
|
||||
|
||||
def test_ntfy_is_built_from_the_pinned_module(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
recorder = Recorder()
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
import base64
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
def load(name):
|
||||
spec = importlib.util.spec_from_file_location(name, Path(__file__).with_name(name + ".py"))
|
||||
result = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(result)
|
||||
return result
|
||||
|
||||
|
||||
signing = load("update_signing")
|
||||
FIXTURE = Path(__file__).resolve().parents[1] / "app/test/features/updates/fixtures/signed_feed"
|
||||
|
||||
|
||||
def verify(tool, public, data, signature):
|
||||
"""Independent check with `openssl pkeyutl -verify` against the raw key."""
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
(root / "pub.der").write_bytes(signing.SPKI_PREFIX + base64.b64decode(public))
|
||||
(root / "data").write_bytes(data)
|
||||
(root / "sig").write_bytes(signature)
|
||||
result = subprocess.run([tool, "pkeyutl", "-verify", "-pubin", "-keyform", "DER", "-inkey",
|
||||
str(root / "pub.der"), "-rawin", "-in", str(root / "data"),
|
||||
"-sigfile", str(root / "sig")], capture_output=True)
|
||||
return result.returncode == 0
|
||||
|
||||
|
||||
class UpdateSigningTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.home = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.home.cleanup)
|
||||
patcher = patch.object(signing.Path, "home", return_value=Path(self.home.name))
|
||||
patcher.start()
|
||||
self.addCleanup(patcher.stop)
|
||||
self.environ = {}
|
||||
self.tool = signing.openssl(self.environ)
|
||||
|
||||
def test_key_is_generated_once_private_under_the_config_directory(self):
|
||||
key = signing.ensure_key(self.environ)
|
||||
self.assertEqual(key, Path(self.home.name) / ".config/pipelines/update-signing-key.pem")
|
||||
self.assertEqual(key.stat().st_mode & 0o777, 0o600)
|
||||
self.assertEqual(key.parent.stat().st_mode & 0o777, 0o700)
|
||||
before = key.read_bytes()
|
||||
self.assertEqual(signing.ensure_key(self.environ), key)
|
||||
self.assertEqual(key.read_bytes(), before)
|
||||
self.assertEqual([p.name for p in key.parent.iterdir()], [key.name])
|
||||
|
||||
def test_readable_or_symlinked_key_is_refused(self):
|
||||
key = signing.ensure_key(self.environ)
|
||||
os.chmod(key, 0o644)
|
||||
with self.assertRaisesRegex(ValueError, "chmod 600"):
|
||||
signing.ensure_key(self.environ)
|
||||
os.chmod(key, 0o600)
|
||||
link = key.with_name("link.pem")
|
||||
link.symlink_to(key)
|
||||
with self.assertRaisesRegex(ValueError, "regular file"):
|
||||
signing.ensure_key({"PIPELINES_UPDATE_SIGNING_KEY": str(link)})
|
||||
|
||||
def test_openssl_without_ed25519_is_skipped_or_reported(self):
|
||||
missing = {"PIPELINES_OPENSSL": "/nonexistent/openssl"}
|
||||
self.assertEqual(signing.openssl(missing), self.tool)
|
||||
with patch.object(signing, "CANDIDATES", ()), \
|
||||
patch.object(signing.shutil, "which", return_value="/usr/bin/false"):
|
||||
with self.assertRaisesRegex(ValueError, "OpenSSL 3"):
|
||||
signing.openssl({})
|
||||
|
||||
def test_signature_verifies_and_tampering_does_not(self):
|
||||
key = signing.ensure_key(self.environ)
|
||||
data = b'{"versionCode": 7}\n'
|
||||
document = json.loads(signing.signature_document(data, key, self.environ))
|
||||
self.assertEqual(document["algorithm"], "ed25519")
|
||||
public = document["publicKey"]
|
||||
signature = base64.b64decode(document["signature"])
|
||||
self.assertEqual((len(base64.b64decode(public)), len(signature)), (32, 64))
|
||||
self.assertTrue(verify(self.tool, public, data, signature))
|
||||
self.assertFalse(verify(self.tool, public, b'{"versionCode": 8}\n', signature))
|
||||
|
||||
def test_publish_writes_feed_and_detached_signature(self):
|
||||
target = Path(self.home.name) / "updates"
|
||||
target.mkdir()
|
||||
data = signing.publish_feed(target, {"versionCode": 3}, signing.Signer(environ=self.environ))
|
||||
self.assertEqual((target / "version.json").read_bytes(), data)
|
||||
document = json.loads((target / "version.json.sig").read_text())
|
||||
self.assertTrue(verify(self.tool, document["publicKey"], data, base64.b64decode(document["signature"])))
|
||||
self.assertEqual((target / "version.json").stat().st_mode & 0o777, 0o644)
|
||||
|
||||
def test_build_embeds_own_key_first_then_listed_trusted_keys(self):
|
||||
own = signing.public_key(signing.ensure_key(self.environ))
|
||||
other = base64.b64encode(bytes(range(32))).decode()
|
||||
listed = Path(self.home.name) / ".config/pipelines/update-trusted-keys"
|
||||
listed.write_text(f"# tank\n{other}\n\n{own}\n")
|
||||
self.assertEqual(signing.dart_defines(self.environ),
|
||||
(f"--dart-define=PIPELINES_UPDATE_PUBLIC_KEY={own},{other}",))
|
||||
listed.write_text("not-a-key\n")
|
||||
with self.assertRaisesRegex(ValueError, "update-trusted-keys:1"):
|
||||
signing.trusted_keys(self.environ)
|
||||
|
||||
def test_committed_dart_fixture_is_a_valid_python_signature(self):
|
||||
# The Dart verifier test reads this fixture; publish_feed produced it
|
||||
# with a throwaway key that was never kept.
|
||||
public = (FIXTURE / "public_key.txt").read_text().strip()
|
||||
document = json.loads((FIXTURE / "version.json.sig").read_text())
|
||||
self.assertEqual(document["publicKey"], public)
|
||||
self.assertTrue(verify(self.tool, public, (FIXTURE / "version.json").read_bytes(),
|
||||
base64.b64decode(document["signature"])))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,98 @@
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
spec = importlib.util.spec_from_file_location("web_access", Path(__file__).with_name("web_access.py"))
|
||||
web_access = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(web_access)
|
||||
manage_spec = importlib.util.spec_from_file_location("manage_web", Path(__file__).with_name("manage.py"))
|
||||
manage = importlib.util.module_from_spec(manage_spec)
|
||||
manage_spec.loader.exec_module(manage)
|
||||
|
||||
|
||||
def reply(stdout="", code=0):
|
||||
return subprocess.CompletedProcess([], code, stdout=stdout, stderr="")
|
||||
|
||||
|
||||
class ResolveTests(unittest.TestCase):
|
||||
def test_fresh_install_defaults_to_loopback_and_tailnet(self):
|
||||
self.assertEqual(web_access.resolve(None, None, None, set()),
|
||||
({"host": "tailnet", "lan": False, "lan_plain_login": False}, []))
|
||||
|
||||
def test_an_existing_all_interfaces_host_is_kept_with_a_warning(self):
|
||||
listen, warnings = web_access.resolve("0.0.0.0", None, None, set())
|
||||
self.assertEqual(listen["host"], "0.0.0.0")
|
||||
self.assertEqual(len(warnings), 1)
|
||||
self.assertIn("cross it unencrypted", warnings[0])
|
||||
self.assertIn("--web-host tailnet", warnings[0])
|
||||
self.assertEqual(web_access.resolve("127.0.0.1", None, None, set())[1], [])
|
||||
|
||||
def test_lan_is_opt_in_and_plain_login_needs_lan(self):
|
||||
listen, warnings = web_access.resolve("0.0.0.0", True, None, {"--web-lan"})
|
||||
self.assertEqual(listen, {"host": "tailnet", "lan": True, "lan_plain_login": False})
|
||||
self.assertEqual(warnings, [])
|
||||
listen, warnings = web_access.resolve("tailnet", True, True, set())
|
||||
self.assertTrue(listen["lan_plain_login"])
|
||||
self.assertIn("lan_plain_login is on", warnings[0])
|
||||
self.assertFalse(web_access.resolve("tailnet", False, True, set())[0]["lan_plain_login"])
|
||||
with self.assertRaisesRegex(ValueError, "applies to --web-host tailnet"):
|
||||
web_access.resolve("0.0.0.0", True, None, {"--web-host", "--web-lan"})
|
||||
with self.assertRaisesRegex(ValueError, "Invalid --web-host"):
|
||||
web_access.resolve("0.0.0.0\nX=1", None, None, {"--web-host"})
|
||||
|
||||
def test_urls_follow_where_a_browser_can_sign_in(self):
|
||||
tailnet = {"host": "tailnet", "lan": False}
|
||||
self.assertEqual(web_access.default_url_host(tailnet, "192.168.1.20", "100.85.25.33"), "100.85.25.33")
|
||||
self.assertEqual(web_access.default_url_host(tailnet, "192.168.1.20", None), "127.0.0.1")
|
||||
self.assertEqual(web_access.default_url_host({"host": "tailnet", "lan": True, "lan_plain_login": True},
|
||||
"192.168.1.20", None), "192.168.1.20")
|
||||
self.assertEqual(web_access.default_url_host({"host": "0.0.0.0"}, "192.168.1.20", None), "192.168.1.20")
|
||||
self.assertIsNotNone(web_access.public_url_warning("http://192.168.1.20:8083", tailnet))
|
||||
for url in ("https://box.tail1.ts.net:8443", "http://100.85.25.33:8083", "http://127.0.0.1:8083", ""):
|
||||
self.assertIsNone(web_access.public_url_warning(url, tailnet), url)
|
||||
self.assertIsNone(web_access.public_url_warning("http://192.168.1.20:8083", {"host": "0.0.0.0"}))
|
||||
|
||||
def test_tailnet_address_and_firewall_are_read_only_probes(self):
|
||||
calls = []
|
||||
|
||||
def runner(command, **_):
|
||||
calls.append(command)
|
||||
return reply("100.85.25.33\n") if command[-1] == "-4" else reply("Firewall is disabled. (State = 0)\n")
|
||||
|
||||
with patch.object(web_access.shutil, "which", return_value="/usr/bin/tailscale"):
|
||||
self.assertEqual(web_access.tailnet_ipv4(runner), "100.85.25.33")
|
||||
self.assertIsNone(web_access.tailnet_ipv4(lambda *a, **k: reply("10.0.0.5\n")))
|
||||
self.assertIsNone(web_access.tailnet_ipv4(lambda *a, **k: reply("", 1)))
|
||||
with patch.object(web_access.shutil, "which", return_value=None):
|
||||
self.assertIsNone(web_access.tailnet_ipv4(runner))
|
||||
with patch.object(web_access.platform, "system", return_value="Darwin"), \
|
||||
patch.object(web_access, "FIREWALL", Path(__file__)):
|
||||
self.assertIn("firewall is off", web_access.firewall_note(runner))
|
||||
self.assertIsNone(web_access.firewall_note(lambda *a, **k: reply("Firewall is enabled. (State = 1)\n")))
|
||||
self.assertEqual(calls[-1], [__file__, "--getglobalstate"])
|
||||
|
||||
|
||||
class ManageWebTests(unittest.TestCase):
|
||||
def test_proxy_flags_follow_the_manifest(self):
|
||||
self.assertEqual(manage.web_listen_args({"host": "0.0.0.0"}), ["--host", "0.0.0.0"])
|
||||
self.assertEqual(manage.web_listen_args({"host": "tailnet"}),
|
||||
["--host", "::", "--allow", "loopback,tailnet"])
|
||||
self.assertEqual(manage.web_listen_args({"host": "tailnet", "lan": True}),
|
||||
["--host", "::", "--allow", "loopback,tailnet,lan"])
|
||||
self.assertEqual(manage.web_listen_args({"host": "tailnet", "lan": True, "lan_plain_login": True}),
|
||||
["--host", "::", "--allow", "loopback,tailnet,lan", "--lan-plain-login"])
|
||||
self.assertEqual(manage.web_listen_args({"host": "tailnet", "lan_plain_login": True}),
|
||||
["--host", "::", "--allow", "loopback,tailnet"])
|
||||
|
||||
def test_mdns_omits_the_lan_route_when_the_proxy_refuses_the_lan(self):
|
||||
port = {"port": 8083}
|
||||
self.assertEqual(manage.mdns_args({"web": {**port, "host": "tailnet"}}), ["-mdns-port", "8083", "-mdns-lan=false"])
|
||||
self.assertEqual(manage.mdns_args({"web": {**port, "host": "tailnet", "lan": True}}), ["-mdns-port", "8083"])
|
||||
self.assertEqual(manage.mdns_args({"web": {**port, "host": "0.0.0.0"}}), ["-mdns-port", "8083"])
|
||||
self.assertEqual(manage.mdns_args({"web": {**port, "host": "127.0.0.1"}}), [])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,189 @@
|
||||
"""Ed25519 signing for the app update feed.
|
||||
|
||||
Each installation owns one private key, generated once into a 0600 file under
|
||||
~/.config/pipelines/ (never in the repository). App builds embed the public key
|
||||
(`--dart-define=PIPELINES_UPDATE_PUBLIC_KEY`, comma separated) together with any
|
||||
other servers' keys listed one per line in
|
||||
~/.config/pipelines/update-trusted-keys. Publishing writes `version.json` and a
|
||||
detached `version.json.sig` over its exact bytes; the app verifies that
|
||||
signature with an embedded key before it trusts the artifact sha256 inside.
|
||||
|
||||
The OpenSSL 3 command line does the cryptography (the system Python has no
|
||||
`cryptography` package and macOS's LibreSSL has no Ed25519), so the tool is
|
||||
looked up explicitly: PIPELINES_OPENSSL, Homebrew's openssl@3, then PATH.
|
||||
"""
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
KEY_NAME = "update-signing-key.pem"
|
||||
TRUSTED_NAME = "update-trusted-keys"
|
||||
SIGNATURE_SUFFIX = ".sig"
|
||||
DEFINE = "PIPELINES_UPDATE_PUBLIC_KEY"
|
||||
CANDIDATES = ("/opt/homebrew/opt/openssl@3/bin/openssl", "/usr/local/opt/openssl@3/bin/openssl")
|
||||
# DER SubjectPublicKeyInfo for Ed25519 is this 12-byte prefix plus the raw key.
|
||||
SPKI_PREFIX = bytes.fromhex("302a300506032b6570032100")
|
||||
|
||||
|
||||
def config_dir():
|
||||
return Path.home() / ".config/pipelines"
|
||||
|
||||
|
||||
def key_path(environ=None):
|
||||
environ = os.environ if environ is None else environ
|
||||
explicit = environ.get("PIPELINES_UPDATE_SIGNING_KEY")
|
||||
return Path(explicit).expanduser() if explicit else config_dir() / KEY_NAME
|
||||
|
||||
|
||||
def _supports_ed25519(tool):
|
||||
try:
|
||||
result = subprocess.run([tool, "genpkey", "-algorithm", "ed25519"],
|
||||
capture_output=True, timeout=30)
|
||||
except (OSError, subprocess.TimeoutExpired):
|
||||
return False
|
||||
return result.returncode == 0 and b"PRIVATE KEY" in result.stdout
|
||||
|
||||
|
||||
def openssl(environ=None):
|
||||
"""An OpenSSL executable with Ed25519 support, or a clear error."""
|
||||
environ = os.environ if environ is None else environ
|
||||
found = [environ.get("PIPELINES_OPENSSL"), *CANDIDATES, shutil.which("openssl")]
|
||||
for tool in dict.fromkeys(t for t in found if t):
|
||||
if _supports_ed25519(tool):
|
||||
return tool
|
||||
raise ValueError("Update signing needs OpenSSL 3 with Ed25519 (LibreSSL lacks it): "
|
||||
"install openssl@3 (brew install openssl@3) or set PIPELINES_OPENSSL")
|
||||
|
||||
|
||||
def _check_private(path):
|
||||
if path.is_symlink() or not path.is_file():
|
||||
raise ValueError(f"Update signing key must be a regular file: {path}")
|
||||
if path.stat().st_mode & 0o077:
|
||||
raise ValueError(f"Update signing key must be private (chmod 600): {path}")
|
||||
|
||||
|
||||
def ensure_key(environ=None):
|
||||
"""The installation's signing key, generated once (0600) when absent."""
|
||||
path = key_path(environ)
|
||||
if path.exists() or path.is_symlink():
|
||||
_check_private(path)
|
||||
return path
|
||||
tool = openssl(environ)
|
||||
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
descriptor, temporary = tempfile.mkstemp(prefix=".update-key-", dir=path.parent)
|
||||
os.close(descriptor)
|
||||
try:
|
||||
os.chmod(temporary, 0o600)
|
||||
result = subprocess.run([tool, "genpkey", "-algorithm", "ed25519", "-out", temporary],
|
||||
capture_output=True, text=True, timeout=30)
|
||||
if result.returncode:
|
||||
raise ValueError("Could not generate the update signing key: " + result.stderr.strip())
|
||||
os.chmod(temporary, 0o600)
|
||||
# Never replace a key another process created meanwhile.
|
||||
os.link(temporary, path)
|
||||
finally:
|
||||
Path(temporary).unlink(missing_ok=True)
|
||||
_check_private(path)
|
||||
return path
|
||||
|
||||
|
||||
def public_key(key, environ=None):
|
||||
"""The raw 32-byte public key, base64 encoded."""
|
||||
result = subprocess.run([openssl(environ), "pkey", "-in", str(key), "-pubout", "-outform", "DER"],
|
||||
capture_output=True, timeout=30)
|
||||
der = result.stdout
|
||||
if result.returncode or len(der) != 44 or not der.startswith(SPKI_PREFIX):
|
||||
raise ValueError(f"Could not read the Ed25519 public key from {key}")
|
||||
return base64.b64encode(der[len(SPKI_PREFIX):]).decode()
|
||||
|
||||
|
||||
def valid_public_key(text):
|
||||
try:
|
||||
return len(base64.b64decode(text, validate=True)) == 32
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
|
||||
def trusted_keys(environ=None):
|
||||
"""This installation's key first, then other servers' keys it trusts."""
|
||||
keys = [public_key(ensure_key(environ), environ)]
|
||||
listed = config_dir() / TRUSTED_NAME
|
||||
if listed.is_file():
|
||||
for number, line in enumerate(listed.read_text().splitlines(), 1):
|
||||
line = line.split("#", 1)[0].strip()
|
||||
if not line:
|
||||
continue
|
||||
if not valid_public_key(line):
|
||||
raise ValueError(f"{listed}:{number} is not a base64 Ed25519 public key")
|
||||
keys.append(line)
|
||||
return list(dict.fromkeys(keys))
|
||||
|
||||
|
||||
def dart_defines(environ=None):
|
||||
"""The build argument embedding the trusted update keys."""
|
||||
return (f"--dart-define={DEFINE}={','.join(trusted_keys(environ))}",)
|
||||
|
||||
|
||||
def sign(data, key, environ=None):
|
||||
"""A raw 64-byte Ed25519 signature over [data]."""
|
||||
with tempfile.NamedTemporaryFile(prefix="pipelines-feed-") as message:
|
||||
message.write(data)
|
||||
message.flush()
|
||||
result = subprocess.run([openssl(environ), "pkeyutl", "-sign", "-rawin", "-inkey", str(key),
|
||||
"-in", message.name], capture_output=True, timeout=30)
|
||||
if result.returncode or len(result.stdout) != 64:
|
||||
raise ValueError("Could not sign the update feed: " + result.stderr.decode(errors="replace").strip())
|
||||
return result.stdout
|
||||
|
||||
|
||||
def signature_document(data, key, environ=None):
|
||||
"""The detached `version.json.sig` content for feed bytes [data]."""
|
||||
return (json.dumps({"algorithm": "ed25519", "publicKey": public_key(key, environ),
|
||||
"signature": base64.b64encode(sign(data, key, environ)).decode()}) + "\n").encode()
|
||||
|
||||
|
||||
class Signer:
|
||||
"""Signs feeds with one key file; the installer's key unless given."""
|
||||
|
||||
def __init__(self, key=None, environ=None):
|
||||
self.environ = environ
|
||||
self.key = Path(key) if key else ensure_key(environ)
|
||||
_check_private(self.key)
|
||||
|
||||
def __call__(self, data):
|
||||
return signature_document(data, self.key, self.environ)
|
||||
|
||||
|
||||
def write_atomic(path, content, mode=0o644):
|
||||
descriptor, temporary = tempfile.mkstemp(prefix=path.name + ".", dir=path.parent)
|
||||
try:
|
||||
with os.fdopen(descriptor, "wb") as output:
|
||||
output.write(content)
|
||||
output.flush()
|
||||
os.fsync(output.fileno())
|
||||
os.chmod(temporary, mode)
|
||||
os.replace(temporary, path)
|
||||
finally:
|
||||
Path(temporary).unlink(missing_ok=True)
|
||||
|
||||
|
||||
def publish_feed(directory, metadata, signer):
|
||||
"""Writes the signature, then the feed it covers, both atomically.
|
||||
|
||||
A reader racing a publish may see a new signature with the old feed for a
|
||||
moment; that verification fails and the next check succeeds."""
|
||||
data = (json.dumps(metadata) + "\n").encode()
|
||||
signature = signer(data)
|
||||
write_atomic(directory / ("version.json" + SIGNATURE_SUFFIX), signature)
|
||||
write_atomic(directory / "version.json", data)
|
||||
return data
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Prints this installation's public key, for another server's
|
||||
# update-trusted-keys file (generating the key on first use).
|
||||
print(public_key(ensure_key()))
|
||||
@@ -0,0 +1,113 @@
|
||||
"""Where the authenticated web proxy listens, as the native installer decides it.
|
||||
|
||||
`web.host` in the installation manifest is either "tailnet" (the default for
|
||||
new installs: the proxy admits only loopback and this machine's Tailscale
|
||||
addresses, see web/src/listenPolicy.js) or a literal IPv4 bind address (the
|
||||
behaviour before; existing installs keep it, with a warning when it reaches
|
||||
beyond loopback). A plain-HTTP LAN listener is opt-in with `web.lan`, and
|
||||
signing in over it additionally needs `web.lan_plain_login`.
|
||||
"""
|
||||
import ipaddress
|
||||
import platform
|
||||
import shutil
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
TAILNET = "tailnet"
|
||||
FIREWALL = Path("/usr/libexec/ApplicationFirewall/socketfilterfw")
|
||||
|
||||
|
||||
def resolve(host, lan, lan_plain_login, supplied):
|
||||
"""The web settings for this install and the warnings to print.
|
||||
|
||||
host/lan/lan_plain_login are the parsed flags after the saved settings were
|
||||
applied (None when neither gave a value); supplied is the set of flag names
|
||||
on the command line."""
|
||||
if lan and "--web-lan" in supplied and "--web-host" not in supplied:
|
||||
host = TAILNET # opting into the LAN listener selects the new model
|
||||
host = host or TAILNET
|
||||
if host != TAILNET:
|
||||
try:
|
||||
ipaddress.IPv4Address(host)
|
||||
except ValueError:
|
||||
raise ValueError("Invalid --web-host; use 'tailnet' (loopback + Tailscale) "
|
||||
"or an IPv4 address such as 127.0.0.1") from None
|
||||
if lan and "--web-lan" in supplied:
|
||||
raise ValueError("--web-lan applies to --web-host tailnet; a literal --web-host binds that address only")
|
||||
lan = bool(lan) and host == TAILNET
|
||||
plain = bool(lan_plain_login) and lan
|
||||
warnings = []
|
||||
if host != TAILNET and not ipaddress.IPv4Address(host).is_loopback:
|
||||
warnings.append(
|
||||
f"WARNING: the web proxy listens on {host} over plain HTTP, so on an untrusted network "
|
||||
"(cafe or hotel Wi-Fi) the web password and the 30-day session cookie cross it unencrypted. "
|
||||
"Recommended: ./install --web-host tailnet (loopback and Tailscale only; add --web-lan "
|
||||
"for a trusted LAN).")
|
||||
if plain:
|
||||
warnings.append(
|
||||
"WARNING: web.lan_plain_login is on: the web password and session cookie are accepted over "
|
||||
"plain HTTP on the LAN. Use it only on a network you trust; prefer the tailnet HTTPS address.")
|
||||
return {"host": host, "lan": lan, "lan_plain_login": plain}, warnings
|
||||
|
||||
|
||||
def lan_sign_in(web):
|
||||
"""Whether a browser on the LAN can sign in over plain HTTP."""
|
||||
if web.get("host") == TAILNET:
|
||||
return web.get("lan") is True and web.get("lan_plain_login") is True
|
||||
return not ipaddress.IPv4Address(web["host"]).is_loopback
|
||||
|
||||
|
||||
def tailnet_ipv4(runner=subprocess.run):
|
||||
"""This machine's Tailscale IPv4 address, or None."""
|
||||
tailscale = shutil.which("tailscale")
|
||||
if not tailscale:
|
||||
return None
|
||||
try:
|
||||
result = runner([tailscale, "ip", "-4"], capture_output=True, text=True, timeout=10)
|
||||
except (OSError, subprocess.SubprocessError):
|
||||
return None
|
||||
if result.returncode:
|
||||
return None
|
||||
for line in (result.stdout or "").split():
|
||||
try:
|
||||
if ipaddress.IPv4Address(line) in ipaddress.IPv4Network("100.64.0.0/10"):
|
||||
return line
|
||||
except ValueError:
|
||||
continue
|
||||
return None
|
||||
|
||||
|
||||
def default_url_host(web, lan_ip, tailnet_ip):
|
||||
"""The host of the default web URL when PIPELINES_WEB_PUBLIC_URL is unset."""
|
||||
if web["host"] == TAILNET:
|
||||
return lan_ip if lan_sign_in(web) else (tailnet_ip or "127.0.0.1")
|
||||
return lan_ip if web["host"] == "0.0.0.0" else web["host"]
|
||||
|
||||
|
||||
def public_url_warning(url, web):
|
||||
"""A recorded plain-HTTP LAN web URL that the proxy now refuses."""
|
||||
parsed = urlsplit(url or "")
|
||||
try:
|
||||
address = ipaddress.ip_address(parsed.hostname or "")
|
||||
except ValueError:
|
||||
return None
|
||||
if (parsed.scheme == "http" and not address.is_loopback
|
||||
and address not in ipaddress.ip_network("100.64.0.0/10") and not lan_sign_in(web)):
|
||||
return (f"WARNING: the recorded web URL {url} is a plain-HTTP LAN address that no longer accepts "
|
||||
"sign-in. Set PIPELINES_WEB_PUBLIC_URL to the tailnet HTTPS address and rerun ./install.")
|
||||
return None
|
||||
|
||||
|
||||
def firewall_note(runner=subprocess.run):
|
||||
"""A read-only report of the macOS application firewall (never changed)."""
|
||||
if platform.system() != "Darwin" or not FIREWALL.exists():
|
||||
return None
|
||||
try:
|
||||
result = runner([str(FIREWALL), "--getglobalstate"], capture_output=True, text=True, timeout=10)
|
||||
except (OSError, subprocess.SubprocessError):
|
||||
return None
|
||||
if "disabled" in (result.stdout or "").lower():
|
||||
return ("Note: the macOS firewall is off. The installer does not change it; consider turning it on "
|
||||
"(System Settings > Network > Firewall) so other local services are not exposed either.")
|
||||
return None
|
||||
+26
-3
@@ -2,6 +2,7 @@
|
||||
// web app, and a proxy to the Pipelines server, with no dependencies.
|
||||
// node serve.mjs [--port 8090] [--host 0.0.0.0] [--pipelines 127.0.0.1:8080]
|
||||
// [--root DIR] [--updates DIR]
|
||||
// [--allow loopback,tailnet[,lan]] [--lan-plain-login]
|
||||
//
|
||||
// Serves the app (--root, default ../app/build/web) and proxies /connect,
|
||||
// /artifacts/, /files and /chat-uploads to the Pipelines server, so the page, the
|
||||
@@ -29,6 +30,7 @@ import net from "node:net";
|
||||
import { sameOriginUpgrade } from "./src/proxySecurity.js";
|
||||
import { withServerToken } from "./src/serverToken.js";
|
||||
import { PeerRelay, proxyDiscovery } from "./src/peerRelay.js";
|
||||
import { accessPolicy, PLAIN_LAN_REFUSAL } from "./src/listenPolicy.js";
|
||||
|
||||
// The proxy's own directory holds the sign-in page's icons.
|
||||
const HERE = resolve(fileURLToPath(new URL(".", import.meta.url)));
|
||||
@@ -36,6 +38,8 @@ const HERE = resolve(fileURLToPath(new URL(".", import.meta.url)));
|
||||
// the checkout's app/build/web. `--updates DIR` serves the Android app's
|
||||
// version.json and APK at /updates/, behind the same sign-in.
|
||||
const ROOT = resolve(flag(process.argv, "--root", "") || join(HERE, "..", "app", "build", "web"));
|
||||
// Which networks may connect (src/listenPolicy.js).
|
||||
const access = accessPolicy(process.argv);
|
||||
const UPDATES = flag(process.argv, "--updates", "") ? resolve(flag(process.argv, "--updates", "")) : null;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -420,6 +424,10 @@ const server = createServer(async (req, res) => {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (access.plainLanBlocked(req)) {
|
||||
res.writeHead(403, { "Content-Type": "text/plain; charset=utf-8", "Cache-Control": "no-store" }).end(PLAIN_LAN_REFUSAL);
|
||||
return;
|
||||
}
|
||||
if (auth) {
|
||||
if (pathname === "/login" && req.method === "GET") {
|
||||
if (isAuthed(req)) {
|
||||
@@ -580,7 +588,7 @@ server.on("upgrade", (req, clientSocket, clientHead) => {
|
||||
return;
|
||||
}
|
||||
const pathname = (req.url ?? "").split("?")[0];
|
||||
if (!sameOriginUpgrade(req.headers.origin, req.headers.host)) {
|
||||
if (access.plainLanBlocked(req) || !sameOriginUpgrade(req.headers.origin, req.headers.host)) {
|
||||
clientSocket.write("HTTP/1.1 403 Forbidden\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");
|
||||
clientSocket.destroy();
|
||||
return;
|
||||
@@ -598,12 +606,27 @@ server.on("upgrade", (req, clientSocket, clientHead) => {
|
||||
proxyUpgrade(req, clientSocket, clientHead);
|
||||
});
|
||||
|
||||
// Connections from a network the policy does not admit are dropped before
|
||||
// any HTTP is read.
|
||||
server.on("connection", (socket) => {
|
||||
if (!access.admits(socket)) socket.destroy();
|
||||
});
|
||||
|
||||
const port = parsePort(process.argv);
|
||||
const host = flag(process.argv, "--host", "127.0.0.1");
|
||||
let host = flag(process.argv, "--host", "127.0.0.1");
|
||||
// "::" also reaches a tailnet IPv6 address; a host without IPv6 falls back.
|
||||
function noIPv6(error) {
|
||||
if (host !== "::" || !["EAFNOSUPPORT", "EADDRNOTAVAIL"].includes(error.code)) throw error;
|
||||
host = "0.0.0.0";
|
||||
server.listen(port, host);
|
||||
}
|
||||
server.once("error", noIPv6);
|
||||
server.listen(port, host, () => {
|
||||
const shown = host === "0.0.0.0" ? "127.0.0.1" : host;
|
||||
server.off("error", noIPv6);
|
||||
const shown = host === "0.0.0.0" || host === "::" ? "127.0.0.1" : host;
|
||||
console.log(`web spike: http://${shown}:${port}/ (bound ${host}:${port})`);
|
||||
console.log(`proxying /connect (ws), /artifacts/ and /files (http) -> ${targetHost}:${targetPort}`);
|
||||
console.log(auth ? "auth: sign-in required" : "auth: DISABLED (tests)");
|
||||
console.log(access.describe());
|
||||
console.log(`serving ${ROOT}${UPDATES ? `; app updates from ${UPDATES}` : ""}`);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
// Which networks may reach the web proxy, decided per connection.
|
||||
//
|
||||
// The installer's default is "loopback + tailnet": the proxy binds the
|
||||
// wildcard address but drops every connection that did not arrive on, and
|
||||
// come from, a loopback or Tailscale address. Binding the wildcard and
|
||||
// filtering is deliberate: Tailscale often comes up after the proxy (login
|
||||
// autostart) and its address can change, and a filter needs no rebinding or
|
||||
// retry loop. Both ends are checked: macOS accepts a packet for the tailnet
|
||||
// address on any interface (weak host model), but a LAN peer cannot complete a
|
||||
// TCP handshake from a tailnet source address because the reply is routed into
|
||||
// the tunnel.
|
||||
//
|
||||
// A plain-HTTP LAN listener is opt-in (`--allow ...,lan`). Even then the
|
||||
// password, the session cookie and every signed-in page stay off the LAN's
|
||||
// cleartext unless `--lan-plain-login` says otherwise.
|
||||
import net from "node:net";
|
||||
|
||||
const tailnet = new net.BlockList();
|
||||
tailnet.addSubnet("100.64.0.0", 10, "ipv4"); // Tailscale's CGNAT range
|
||||
tailnet.addSubnet("fd7a:115c:a1e0::", 48, "ipv6"); // Tailscale's ULA prefix
|
||||
const loopback = new net.BlockList();
|
||||
loopback.addSubnet("127.0.0.0", 8, "ipv4");
|
||||
loopback.addAddress("::1", "ipv6");
|
||||
|
||||
export const ROUTES = ["loopback", "tailnet", "lan"];
|
||||
|
||||
/** "loopback", "tailnet" or "lan" for one address (IPv4-mapped IPv6 unwrapped). */
|
||||
export function addressRoute(address) {
|
||||
let value = String(address ?? "").replace(/%.*$/, "");
|
||||
if (value.toLowerCase().startsWith("::ffff:") && net.isIPv4(value.slice(7))) value = value.slice(7);
|
||||
const family = net.isIPv4(value) ? "ipv4" : net.isIPv6(value) ? "ipv6" : null;
|
||||
if (!family) return "lan";
|
||||
if (loopback.check(value, family)) return "loopback";
|
||||
if (tailnet.check(value, family)) return "tailnet";
|
||||
return "lan";
|
||||
}
|
||||
|
||||
/** The route of a connection: loopback or tailnet only when both ends agree. */
|
||||
export function socketRoute(socket) {
|
||||
const local = addressRoute(socket?.localAddress);
|
||||
return local !== "lan" && local === addressRoute(socket?.remoteAddress) ? local : "lan";
|
||||
}
|
||||
|
||||
/**
|
||||
* The policy from the proxy's flags. Without `--allow` every route is
|
||||
* admitted with plain-HTTP sign-in, as before (a direct `node serve.mjs`
|
||||
* binds 127.0.0.1 by default); the installer always passes `--allow`.
|
||||
*/
|
||||
export function accessPolicy(argv) {
|
||||
const i = argv.indexOf("--allow");
|
||||
const given = i !== -1 && argv[i + 1] ? argv[i + 1] : null;
|
||||
const allow = new Set(given === null ? ROUTES : given.split(",").map((part) => part.trim()).filter(Boolean));
|
||||
for (const route of allow) {
|
||||
if (!ROUTES.includes(route)) throw new Error(`--allow: unknown route ${route} (use ${ROUTES.join(",")})`);
|
||||
}
|
||||
allow.add("loopback"); // the health check, tailscale serve and local tools
|
||||
const lanPlainLogin = given === null || argv.includes("--lan-plain-login");
|
||||
return {
|
||||
allow,
|
||||
lanPlainLogin,
|
||||
/** Whether a new connection may proceed at all. */
|
||||
admits(socket) {
|
||||
return allow.has(socketRoute(socket));
|
||||
},
|
||||
/** Whether this request must not carry the password or a signed-in page. */
|
||||
plainLanBlocked(req) {
|
||||
return !lanPlainLogin && socketRoute(req.socket) === "lan";
|
||||
},
|
||||
describe() {
|
||||
return `access: ${[...allow].join(" + ")}` +
|
||||
(allow.has("lan") ? (lanPlainLogin ? " (plain-HTTP LAN sign-in ALLOWED)" : " (no sign-in over plain-HTTP LAN)") : "");
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export const PLAIN_LAN_REFUSAL =
|
||||
"Signing in over plain HTTP on this network is disabled: the password and session cookie would cross it unencrypted.\n" +
|
||||
"Use this machine's Tailscale address (for example its https://<name>.ts.net address), or set web.lan_plain_login in the installation.\n";
|
||||
@@ -0,0 +1,134 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { spawn } from "node:child_process";
|
||||
import { mkdtemp, mkdir, writeFile } from "node:fs/promises";
|
||||
import { networkInterfaces, tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import net from "node:net";
|
||||
import { accessPolicy, addressRoute, socketRoute } from "../src/listenPolicy.js";
|
||||
|
||||
const here = fileURLToPath(new URL("..", import.meta.url));
|
||||
|
||||
test("classifies loopback, tailnet and LAN addresses", () => {
|
||||
for (const address of ["127.0.0.1", "127.8.0.2", "::1", "::ffff:127.0.0.1"]) assert.equal(addressRoute(address), "loopback", address);
|
||||
for (const address of ["100.64.0.1", "100.101.7.9", "100.127.255.254", "::ffff:100.100.1.2", "fd7a:115c:a1e0::1", "fd7a:115c:a1e0:ab12::9"]) {
|
||||
assert.equal(addressRoute(address), "tailnet", address);
|
||||
}
|
||||
for (const address of ["192.168.1.20", "10.0.0.5", "100.63.255.255", "100.128.0.1", "fd7a:115c:a1e1::1", "fe80::1%en0", "", undefined, "nonsense"]) {
|
||||
assert.equal(addressRoute(address), "lan", String(address));
|
||||
}
|
||||
});
|
||||
|
||||
test("a connection is tailnet or loopback only when both ends are", () => {
|
||||
assert.equal(socketRoute({ localAddress: "100.101.1.1", remoteAddress: "100.90.2.2" }), "tailnet");
|
||||
assert.equal(socketRoute({ localAddress: "127.0.0.1", remoteAddress: "127.0.0.1" }), "loopback");
|
||||
// A LAN peer aiming at the tailnet address (macOS weak host model) is LAN.
|
||||
assert.equal(socketRoute({ localAddress: "100.101.1.1", remoteAddress: "192.168.1.9" }), "lan");
|
||||
assert.equal(socketRoute({ localAddress: "127.0.0.1", remoteAddress: "100.90.2.2" }), "lan");
|
||||
assert.equal(socketRoute({ localAddress: "192.168.1.20", remoteAddress: "192.168.1.9" }), "lan");
|
||||
});
|
||||
|
||||
test("the policy admits only allowed routes and gates LAN sign-in", () => {
|
||||
const lan = { localAddress: "192.168.1.20", remoteAddress: "192.168.1.9" };
|
||||
const ts = { localAddress: "100.101.1.1", remoteAddress: "100.90.2.2" };
|
||||
const local = { localAddress: "127.0.0.1", remoteAddress: "127.0.0.1" };
|
||||
const strict = accessPolicy(["--allow", "tailnet"]);
|
||||
assert.deepEqual([strict.admits(local), strict.admits(ts), strict.admits(lan)], [true, true, false]);
|
||||
const withLan = accessPolicy(["--allow", "loopback,tailnet,lan"]);
|
||||
assert.equal(withLan.admits(lan), true);
|
||||
assert.equal(withLan.plainLanBlocked({ socket: lan }), true);
|
||||
assert.equal(withLan.plainLanBlocked({ socket: ts }), false);
|
||||
assert.equal(withLan.plainLanBlocked({ socket: local }), false);
|
||||
assert.equal(accessPolicy(["--allow", "loopback,tailnet,lan", "--lan-plain-login"]).plainLanBlocked({ socket: lan }), false);
|
||||
const legacy = accessPolicy([]);
|
||||
assert.equal(legacy.admits(lan) && !legacy.plainLanBlocked({ socket: lan }), true);
|
||||
assert.throws(() => accessPolicy(["--allow", "everything"]), /unknown route/);
|
||||
});
|
||||
|
||||
function hostAddress(match) {
|
||||
for (const entries of Object.values(networkInterfaces())) {
|
||||
for (const entry of entries ?? []) {
|
||||
if (entry.family === "IPv4" && !entry.internal && match(entry.address)) return entry.address;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
const lanAddress = hostAddress((address) => addressRoute(address) === "lan");
|
||||
const tailnetAddress = hostAddress((address) => addressRoute(address) === "tailnet");
|
||||
|
||||
async function freePort() {
|
||||
const server = net.createServer().listen(0, "127.0.0.1");
|
||||
await new Promise((done) => server.once("listening", done));
|
||||
const { port } = server.address();
|
||||
await new Promise((done) => server.close(done));
|
||||
return port;
|
||||
}
|
||||
|
||||
async function start(args) {
|
||||
const dir = await mkdtemp(join(tmpdir(), "listen-"));
|
||||
await mkdir(join(dir, "app"));
|
||||
await writeFile(join(dir, "app", "index.html"), "<html><head></head><body>app</body></html>");
|
||||
const port = await freePort();
|
||||
const child = spawn(process.execPath, ["serve.mjs", "--port", String(port), "--root", join(dir, "app"), ...args], {
|
||||
cwd: here,
|
||||
env: { ...process.env, PIPELINES_WEB_AUTH: "disabled" },
|
||||
stdio: ["ignore", "pipe", "inherit"],
|
||||
});
|
||||
let output = "";
|
||||
await new Promise((done) => child.stdout.on("data", (chunk) => {
|
||||
output += chunk;
|
||||
if (output.includes("access:")) done();
|
||||
}));
|
||||
return { port, output, stop: () => child.kill() };
|
||||
}
|
||||
|
||||
/** Raw HTTP so a dropped connection is observed as such, not retried. */
|
||||
function rawRequest(address, port, text) {
|
||||
return new Promise((done) => {
|
||||
const socket = net.connect(port, address, () => socket.write(text));
|
||||
let reply = "";
|
||||
socket.on("data", (chunk) => { reply += chunk; });
|
||||
socket.on("error", () => done({ dropped: true, reply }));
|
||||
socket.on("close", () => done({ dropped: reply === "", reply }));
|
||||
socket.setTimeout(3000, () => socket.destroy());
|
||||
});
|
||||
}
|
||||
const get = (path) => `GET ${path} HTTP/1.1\r\nHost: x\r\nAccept: text/html\r\nConnection: close\r\n\r\n`;
|
||||
const login = "POST /login HTTP/1.1\r\nHost: x\r\nContent-Type: application/x-www-form-urlencoded\r\n" +
|
||||
"Content-Length: 17\r\nConnection: close\r\n\r\npassword=whatever";
|
||||
|
||||
test("loopback + tailnet drops a LAN connection and serves loopback and tailnet", { skip: !lanAddress && "no LAN address" }, async (t) => {
|
||||
const proxy = await start(["--host", "::", "--allow", "loopback,tailnet"]);
|
||||
t.after(proxy.stop);
|
||||
const lan = await rawRequest(lanAddress, proxy.port, get("/"));
|
||||
assert.equal(lan.dropped, true, `LAN ${lanAddress} got ${lan.reply}`);
|
||||
const local = await rawRequest("127.0.0.1", proxy.port, get("/"));
|
||||
assert.match(local.reply, /^HTTP\/1.1 200/);
|
||||
if (tailnetAddress) {
|
||||
const ts = await rawRequest(tailnetAddress, proxy.port, get("/"));
|
||||
assert.match(ts.reply, /^HTTP\/1.1 200/, `tailnet ${tailnetAddress}`);
|
||||
}
|
||||
});
|
||||
|
||||
test("an opted-in LAN refuses sign-in and signed-in pages over plain HTTP", { skip: !lanAddress && "no LAN address" }, async (t) => {
|
||||
const proxy = await start(["--host", "0.0.0.0", "--allow", "loopback,tailnet,lan"]);
|
||||
t.after(proxy.stop);
|
||||
for (const request of [login, get("/login"), get("/"), get("/updates/version.json")]) {
|
||||
const reply = await rawRequest(lanAddress, proxy.port, request);
|
||||
assert.match(reply.reply, /^HTTP\/1.1 403/, request.split("\r\n")[0]);
|
||||
assert.match(reply.reply, /plain HTTP on this network is disabled/);
|
||||
}
|
||||
assert.match((await rawRequest(lanAddress, proxy.port, get("/favicon.svg"))).reply, /^HTTP\/1.1 200/);
|
||||
const upgrade = await rawRequest(lanAddress, proxy.port, "GET /connect HTTP/1.1\r\nHost: x\r\nConnection: Upgrade\r\n" +
|
||||
"Upgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\r\n");
|
||||
assert.match(upgrade.reply, /^HTTP\/1.1 403/);
|
||||
assert.match((await rawRequest("127.0.0.1", proxy.port, get("/"))).reply, /^HTTP\/1.1 200/);
|
||||
});
|
||||
|
||||
test("web.lan_plain_login restores plain-HTTP LAN sign-in explicitly", { skip: !lanAddress && "no LAN address" }, async (t) => {
|
||||
const proxy = await start(["--host", "0.0.0.0", "--allow", "loopback,tailnet,lan", "--lan-plain-login"]);
|
||||
t.after(proxy.stop);
|
||||
assert.match(proxy.output, /plain-HTTP LAN sign-in ALLOWED/);
|
||||
assert.match((await rawRequest(lanAddress, proxy.port, get("/"))).reply, /^HTTP\/1.1 200/);
|
||||
});
|
||||
Reference in New Issue
Block a user