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
@@ -21,7 +21,14 @@ Future<ChatUploadReply> postChatUpload(
required String name,
required String mime,
required Uint8List bytes,
}) => platform.postChatUpload(uri, name: name, mime: mime, bytes: bytes);
Map<String, String> headers = const {},
}) => platform.postChatUpload(
uri,
name: name,
mime: mime,
bytes: bytes,
headers: headers,
);
/// The header that carries the file name; the server refuses a post without it.
const chatUploadNameHeader = 'X-Pipelines-Upload-Name';
@@ -10,6 +10,7 @@ Future<ChatUploadReply> postChatUpload(
required String name,
required String mime,
required Uint8List bytes,
Map<String, String> headers = const {},
}) async {
final client = HttpClient()..connectionTimeout = const Duration(seconds: 15);
try {
@@ -21,6 +22,7 @@ Future<ChatUploadReply> postChatUpload(
..set(chatUploadNameHeader, Uri.encodeComponent(name))
..set(HttpHeaders.contentTypeHeader, mime)
..contentLength = bytes.length;
headers.forEach(request.headers.set);
request.add(bytes);
final response = await request.close();
final body = await response.transform(utf8.decoder).join();
@@ -7,4 +7,5 @@ Future<ChatUploadReply> postChatUpload(
required String name,
required String mime,
required Uint8List bytes,
Map<String, String> headers = const {},
}) => throw UnsupportedError('Uploads are not supported on this platform.');
@@ -28,6 +28,7 @@ Future<ChatUploadReply> postChatUpload(
required String name,
required String mime,
required Uint8List bytes,
Map<String, String> headers = const {},
}) async {
final response = await _fetch(
uri.toString().toJS,
@@ -36,6 +37,7 @@ Future<ChatUploadReply> postChatUpload(
headers: {
chatUploadNameHeader: Uri.encodeComponent(name),
'Content-Type': mime,
...headers,
}.jsify()!,
body: bytes.toJS,
credentials: 'same-origin'.toJS,
@@ -1,5 +1,6 @@
import 'package:flutter/foundation.dart';
import '../../../core/api_gateway.dart';
import '../../../core/servers/elevation/elevating_gateway.dart';
import '../../../core/transport/pipelines_client.dart';
import '../data/directory_listing.dart';
import '../domain/chat_attachment_picker.dart';
@@ -113,7 +114,12 @@ class FilesController extends ChangeNotifier {
final failures = <String>[];
for (final file in files) {
try {
await uploadFileInto(endpoint, directory, file);
await uploadFileInto(
endpoint,
directory,
file,
elevation: elevationOf(gateway),
);
} on FilesUploadException catch (error) {
failures.add(error.message);
} finally {
+46 -12
View File
@@ -1,6 +1,7 @@
import 'dart:convert';
import 'dart:typed_data';
import '../../../core/servers/elevation/machine_elevation.dart';
import '../../../core/transport/server_url.dart';
import '../data/upload/chat_upload.dart';
import '../domain/chat_attachment_picker.dart';
@@ -12,6 +13,7 @@ typedef FilesUploadPost =
required String name,
required String mime,
required Uint8List bytes,
Map<String, String> headers,
});
/// The transport the Files panel uploads with.
@@ -33,11 +35,16 @@ class FilesUploadException implements Exception {
/// the stored path the server reports. Every failure is a
/// [FilesUploadException]; nothing is retried or renamed on the caller's
/// behalf, so an existing file is reported rather than replaced.
///
/// [elevation] is the machine's step-up state: its token rides on the
/// request as [elevationHeader], and a 403 `elevation_required` refusal asks
/// for the password once and retries.
Future<String> uploadFileInto(
String endpoint,
String directory,
ChatPickedFile file,
) async {
ChatPickedFile file, {
MachineElevation? elevation,
}) async {
final name = file.name.trim();
if (name.isEmpty) {
throw const FilesUploadException(
@@ -56,16 +63,25 @@ Future<String> uploadFileInto(
'No server is configured to upload files to.',
);
}
final ChatUploadReply reply;
try {
reply = await postFileUpload(
uri,
name: name,
mime: file.mime.trim().isEmpty ? 'application/octet-stream' : file.mime,
bytes: file.bytes,
);
} catch (error) {
throw FilesUploadException('"$name" could not be uploaded: $error');
Future<ChatUploadReply> post() async {
final token = elevation?.token;
try {
return await postFileUpload(
uri,
name: name,
mime: file.mime.trim().isEmpty ? 'application/octet-stream' : file.mime,
bytes: file.bytes,
headers: {elevationHeader: ?token},
);
} catch (error) {
throw FilesUploadException('"$name" could not be uploaded: $error');
}
}
var reply = await post();
if (elevation != null && refusedForElevation(reply)) {
elevation.clear();
if (await elevation.ensure()) reply = await post();
}
final reason = reply.body.trim();
switch (reply.status) {
@@ -75,6 +91,10 @@ Future<String> uploadFileInto(
throw const FilesUploadException(
'Sign in to upload files (Settings → Sign in).',
);
case 403 when refusedForElevation(reply):
throw FilesUploadException(
'"$name" was not uploaded: confirm the password to change files.',
);
case 409:
throw FilesUploadException('"$name" already exists in this folder.');
case 413:
@@ -97,3 +117,17 @@ Future<String> uploadFileInto(
}
throw FilesUploadException('"$name" upload returned no stored path.');
}
/// The request header carrying a confirmed connection's elevation token.
const elevationHeader = 'X-Pipelines-Elevation';
/// Whether [reply] is the server's 403 step-up refusal.
bool refusedForElevation(ChatUploadReply reply) {
if (reply.status != 403) return false;
try {
final decoded = jsonDecode(reply.body);
return decoded is Map && decoded['code'] == elevationRequiredCode;
} on FormatException {
return false;
}
}