Ask for the password once and retry when a machine requires elevation

Carry the protocol error code, wrap each machine's gateway so an
elevation_required refusal shows one native password dialog, elevates that
connection and retries; send the upload token with file uploads; add a Settings
sign-in security section with the weak-password warning and sign out
everywhere; accept the __Host- session cookie.
This commit is contained in:
2026-11-10 01:21:28 +00:00
parent e43f9fa50c
commit 9702d68d2d
30 changed files with 1464 additions and 31 deletions
@@ -56,7 +56,13 @@ void main() {
reply = (status: 200, body: '{"path":"/home/demo/new.txt"}');
gate = null;
postFileUpload =
(uri, {required name, required mime, required bytes}) async {
(
uri, {
required name,
required mime,
required bytes,
headers = const {},
}) async {
posted.add((uri: uri, name: name, mime: mime, size: bytes.length));
if (gate != null) await gate!.future;
return reply;
@@ -187,7 +193,13 @@ void main() {
) async {
var calls = 0;
postFileUpload =
(uri, {required name, required mime, required bytes}) async {
(
uri, {
required name,
required mime,
required bytes,
headers = const {},
}) async {
calls += 1;
return name == 'kept.txt'
? (status: 409, body: 'exists')
@@ -0,0 +1,123 @@
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/servers/elevation/machine_elevation.dart';
import 'package:pipelines_app/features/chat/domain/chat_attachment_picker.dart';
import 'package:pipelines_app/features/chat/files/files_upload.dart';
import '../../../core/servers/elevation/elevating_gateway_test.dart'
show FakeMachine;
void main() {
late FilesUploadPost original;
setUp(() => original = postFileUpload);
tearDown(() => postFileUpload = original);
final file = ChatPickedFile(
name: 'notes.txt',
mime: 'text/plain',
bytes: Uint8List.fromList([1, 2, 3]),
);
const refusal = (
status: 403,
body: '{"code":"elevation_required","error":"Confirm your password."}',
);
const stored = (status: 200, body: '{"path":"/home/demo/notes.txt"}');
test(
'a refused upload elevates its machine and retries with the token',
() async {
final machine = FakeMachine();
var prompts = 0;
final elevation = MachineElevation(
name: 'laptop',
gateway: machine,
prompt: (m) async {
prompts++;
return await m.submit('right') == null;
},
);
final sent = <Map<String, String>>[];
postFileUpload =
(
uri, {
required name,
required mime,
required bytes,
headers = const {},
}) async {
sent.add(headers);
return headers['X-Pipelines-Elevation'] == 'tok-1'
? stored
: refusal;
};
final path = await uploadFileInto(
'ws://example.test/connect',
'/home/demo',
file,
elevation: elevation,
);
expect(path, '/home/demo/notes.txt');
expect(prompts, 1);
expect(sent, [
<String, String>{},
{'X-Pipelines-Elevation': 'tok-1'},
]);
// A live elevation rides on the next upload without asking again.
await uploadFileInto(
'ws://example.test/connect',
'/home/demo',
file,
elevation: elevation,
);
expect(prompts, 1);
expect(sent.last, {'X-Pipelines-Elevation': 'tok-1'});
},
);
test('a cancelled confirmation reports the refusal', () async {
final machine = FakeMachine();
final elevation = MachineElevation(
name: 'laptop',
gateway: machine,
prompt: (_) async => false,
);
var posts = 0;
postFileUpload =
(
uri, {
required name,
required mime,
required bytes,
headers = const {},
}) async {
posts++;
return refusal;
};
await expectLater(
uploadFileInto(
'ws://example.test/connect',
'/home/demo',
file,
elevation: elevation,
),
throwsA(
isA<FilesUploadException>().having(
(e) => e.message,
'message',
contains('confirm the password'),
),
),
);
expect(posts, 1);
});
test('refusal detection needs the typed code', () {
expect(refusedForElevation(refusal), isTrue);
expect(
refusedForElevation((status: 403, body: 'folder access denied')),
isFalse,
);
expect(refusedForElevation((status: 200, body: refusal.body)), isFalse);
});
}
@@ -22,7 +22,13 @@ void main() {
String? postedName;
String? postedMime;
postFileUpload =
(uri, {required name, required mime, required bytes}) async {
(
uri, {
required name,
required mime,
required bytes,
headers = const {},
}) async {
posted = uri;
postedName = name;
postedMime = mime;
@@ -49,7 +55,13 @@ void main() {
test('a relay endpoint posts through its peer prefix', () async {
Uri? posted;
postFileUpload =
(uri, {required name, required mime, required bytes}) async {
(
uri, {
required name,
required mime,
required bytes,
headers = const {},
}) async {
posted = uri;
return (status: 200, body: '{"path":"/home/x/notes.txt"}');
};
@@ -64,8 +76,13 @@ void main() {
test('server refusals become plain messages', () async {
Future<String> message(int status, String body) async {
postFileUpload =
(uri, {required name, required mime, required bytes}) async =>
(status: status, body: body);
(
uri, {
required name,
required mime,
required bytes,
headers = const {},
}) async => (status: status, body: body);
try {
await uploadFileInto('ws://example.test/connect', '/home/demo', file);
} on FilesUploadException catch (error) {
@@ -103,8 +120,14 @@ void main() {
test(
'transport failures and blank names are reported, not swallowed',
() async {
postFileUpload = (uri, {required name, required mime, required bytes}) =>
throw StateError('socket closed');
postFileUpload =
(
uri, {
required name,
required mime,
required bytes,
headers = const {},
}) => throw StateError('socket closed');
await expectLater(
uploadFileInto('ws://example.test/connect', '/home/demo', file),
throwsA(
@@ -0,0 +1,113 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/session/server_session.dart';
import 'package:pipelines_app/features/settings/security/session_security_api.dart';
import 'package:pipelines_app/features/settings/security/settings_security_section.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
late SecurityRequest originalSend;
late void Function() originalOpen;
final requests = <String>[];
var weak = true;
setUp(() async {
originalSend = sendSecurityRequest;
originalOpen = openWebSignIn;
requests.clear();
weak = true;
SharedPreferences.setMockInitialValues({});
await ServerSession.load();
sendSecurityRequest = (method, uri) async {
requests.add('$method $uri');
if (uri.path == '/api/session') {
return (
status: 200,
body:
'{"weak_password":$weak,"password_rule":"Use 16 or more characters."}',
);
}
return (status: 200, body: '{"revoked":true}');
};
});
tearDown(() {
sendSecurityRequest = originalSend;
openWebSignIn = originalOpen;
});
Future<void> pump(
WidgetTester tester, {
bool web = false,
VoidCallback? onSignedOut,
}) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: SettingsSecuritySection(
endpoint: 'wss://box.ts.net:8443/connect',
onSignedOut: onSignedOut ?? () {},
web: web,
),
),
),
);
await tester.pumpAndSettle();
}
testWidgets('warns about a weak password with the rule', (tester) async {
await pump(tester);
expect(requests, ['GET https://box.ts.net:8443/api/session']);
final warning = find.byKey(const Key('settings-weak-password'));
expect(warning, findsOneWidget);
expect(
tester.widget<Text>(warning).data,
contains('Use 16 or more characters. Run ./password on the server'),
);
});
testWidgets('a strong password shows no warning', (tester) async {
weak = false;
await pump(tester);
expect(find.byKey(const Key('settings-weak-password')), findsNothing);
});
testWidgets('sign out everywhere confirms, revokes and signs out', (
tester,
) async {
var signedOut = 0;
await pump(tester, onSignedOut: () => signedOut++);
await tester.tap(find.byKey(const Key('settings-revoke-all')));
await tester.pumpAndSettle();
await tester.tap(find.text('Cancel'));
await tester.pumpAndSettle();
expect(requests, hasLength(1));
await tester.tap(find.byKey(const Key('settings-revoke-all')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('settings-revoke-all-confirm')));
await tester.pumpAndSettle();
expect(
requests.last,
'POST https://box.ts.net:8443/api/sessions/revoke-all',
);
expect(signedOut, 1);
expect(ServerSession.cookie, isNull);
expect(find.textContaining('Signed out everywhere'), findsOneWidget);
});
testWidgets('the web app leaves for the sign-in page', (tester) async {
var opened = 0;
openWebSignIn = () => opened++;
await pump(tester, web: true);
await tester.tap(find.byKey(const Key('settings-revoke-all')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('settings-revoke-all-confirm')));
await tester.pumpAndSettle();
expect(opened, 1);
});
testWidgets('a signed-out device says so', (tester) async {
sendSecurityRequest = (method, uri) async => (status: 401, body: '');
await pump(tester);
expect(find.textContaining('Sign in to see'), findsOneWidget);
});
}