Merge branch 'worktree-agent-ab6edc0d99947642f'

This commit is contained in:
2026-11-09 00:11:40 +00:00
19 changed files with 1706 additions and 99 deletions
@@ -1,7 +1,10 @@
import 'package:flutter/foundation.dart';
import '../../../core/api_gateway.dart';
import '../../../core/transport/pipelines_client.dart';
import '../data/directory_listing.dart';
import '../domain/chat_attachment_picker.dart';
import 'files_models.dart';
import 'files_upload.dart';
class FilesController extends ChangeNotifier {
FilesController({required this.gateway, required this.initialPath});
@@ -18,7 +21,17 @@ class FilesController extends ChangeNotifier {
int _request = 0;
bool _disposed = false;
/// Files still being stored and the size of the batch they belong to.
int uploading = 0;
int uploadTotal = 0;
/// Why the last upload batch failed, until dismissed or a new batch starts.
String? uploadError;
bool get missingDirectory => initialPath.trim().isEmpty;
String get uploadLabel => uploadTotal <= 1
? 'Uploading…'
: 'Uploading ${uploadTotal - uploading + 1} of $uploadTotal…';
List<DirectoryEntry> get entries {
final current = listing;
if (current == null) return const [];
@@ -75,6 +88,54 @@ class FilesController extends ChangeNotifier {
}
Future<void> retry() => open(listing?.path ?? initialPath);
/// Stores [files] in the shown folder, one request each, then reloads that
/// folder so the new entries appear. Each refusal is kept in [uploadError]
/// in the server's words; the remaining files are still attempted.
Future<void> upload(List<ChatPickedFile> files) async {
final directory = listing?.path;
if (files.isEmpty) return;
if (directory == null) {
reportUploadError('Open a folder before uploading files.');
return;
}
final endpoint = configuredGatewayEndpoint(gateway);
uploadTotal += files.length;
uploading += files.length;
uploadError = null;
_notify();
final failures = <String>[];
for (final file in files) {
try {
await uploadFileInto(endpoint, directory, file);
} on FilesUploadException catch (error) {
failures.add(error.message);
} finally {
uploading -= 1;
}
if (_disposed) return;
_notify();
}
if (uploading == 0) uploadTotal = 0;
if (failures.isNotEmpty) uploadError = failures.join('\n');
_notify();
// The operator may have moved on while the batch was in flight; only the
// folder that received the files is reloaded.
if (listing?.path == directory) await retry();
}
/// Shows a host-side upload failure (no picker, unreadable drop) in the
/// same place as a server refusal.
void reportUploadError(String message) {
uploadError = message;
_notify();
}
void dismissUploadError() {
uploadError = null;
_notify();
}
void toggleHidden(bool value) {
showHidden = value;
_notify();
@@ -0,0 +1,79 @@
import 'dart:ui' show Offset;
import '../domain/chat_attachment_picker.dart';
import 'files_drop_stub.dart'
if (dart.library.js_interop) 'files_drop_web.dart'
as impl;
/// Stops observing the host's drag events for one drop area.
typedef FilesDropUnsubscribe = void Function();
/// Registers [listener] with the host and returns its cancellation.
typedef FilesDropSubscribe =
FilesDropUnsubscribe Function(FilesDropListener listener);
/// One drop area's view of a host drag that carries files.
///
/// Positions are logical pixels in the Flutter view's coordinate space, so a
/// listener compares them with its own render box. The host asks [accepts]
/// before it takes over the drag; a drag the area does not accept keeps the
/// host's default behaviour.
class FilesDropListener {
const FilesDropListener({
required this.accepts,
required this.onHover,
required this.onLeave,
required this.onDrop,
required this.onError,
});
/// Whether a drop at [position] would land on this area right now.
final bool Function(Offset position) accepts;
/// A file drag is over the area at [position].
final void Function(Offset position) onHover;
/// The drag left the area, ended or was dropped.
final void Function() onLeave;
/// Files were dropped on the area, in the host's order.
final void Function(List<ChatPickedFile> files) onDrop;
/// One dropped item whose bytes could not be read (a folder, for example).
final void Function(Object error) onError;
}
/// The file drag-and-drop seam for one host.
///
/// The web host observes the browser's own drag events on the document and
/// reads dropped files before reporting them as [ChatPickedFile] rows. A host
/// without file drops constructs [FilesDrop.unsupported] so the panel offers
/// the Upload button alone instead of pretending to listen. Tests inject a
/// fake [subscribe] and drive the listener directly.
class FilesDrop {
const FilesDrop({required FilesDropSubscribe this.subscribe})
: unsupportedMessage = null;
const FilesDrop.unsupported(String message)
: subscribe = null,
unsupportedMessage = message;
/// The host's drag listener registration, or null when unsupported.
final FilesDropSubscribe? subscribe;
/// Why this host cannot observe file drops, when [subscribe] is null.
final String? unsupportedMessage;
bool get isSupported => subscribe != null;
/// Starts observing the host's drag events. A host without the capability
/// throws [UnsupportedError] with [unsupportedMessage].
FilesDropUnsubscribe listen(FilesDropListener listener) {
final subscribe = this.subscribe;
if (subscribe == null) throw UnsupportedError(unsupportedMessage!);
return subscribe(listener);
}
}
/// The drop seam for the running host.
FilesDrop get hostFilesDrop => impl.hostFilesDrop;
@@ -0,0 +1,111 @@
import 'package:flutter/material.dart';
import '../domain/chat_attachment_picker.dart';
import 'files_drop.dart';
/// Makes [child] a drop target for host file drags: while a file drag is over
/// it, a highlight names the folder the files would land in, and a drop hands
/// the files to [onFiles]. Nothing is drawn when the host has no drop support
/// or [folderLabel] is null (no folder is open yet).
class FilesDropArea extends StatefulWidget {
const FilesDropArea({
super.key,
required this.drop,
required this.folderLabel,
required this.onFiles,
required this.onError,
required this.child,
});
final FilesDrop drop;
/// The shown folder's label, or null while there is no folder to drop into.
final String? folderLabel;
final ValueChanged<List<ChatPickedFile>> onFiles;
final ValueChanged<Object> onError;
final Widget child;
@override
State<FilesDropArea> createState() => _FilesDropAreaState();
}
class _FilesDropAreaState extends State<FilesDropArea> {
FilesDropUnsubscribe? _unsubscribe;
bool _hovering = false;
@override
void initState() {
super.initState();
if (widget.drop.isSupported) {
_unsubscribe = widget.drop.listen(
FilesDropListener(
accepts: _accepts,
onHover: (_) => _setHovering(true),
onLeave: () => _setHovering(false),
onDrop: _dropped,
onError: (error) {
if (mounted) widget.onError(error);
},
),
);
}
}
@override
void dispose() {
_unsubscribe?.call();
super.dispose();
}
bool _accepts(Offset position) {
if (!mounted || widget.folderLabel == null) return false;
final box = context.findRenderObject();
if (box is! RenderBox || !box.hasSize) return false;
return box.paintBounds.contains(box.globalToLocal(position));
}
void _setHovering(bool value) {
if (!mounted || _hovering == value) return;
setState(() => _hovering = value);
}
void _dropped(List<ChatPickedFile> files) {
_setHovering(false);
if (mounted && files.isNotEmpty) widget.onFiles(files);
}
@override
Widget build(BuildContext context) {
final scheme = Theme.of(context).colorScheme;
return Stack(
fit: StackFit.passthrough,
children: [
widget.child,
if (_hovering && widget.folderLabel != null)
Positioned.fill(
child: IgnorePointer(
child: Semantics(
label: 'Drop to upload to ${widget.folderLabel}',
child: DecoratedBox(
decoration: BoxDecoration(
color: scheme.primaryContainer.withValues(alpha: .6),
border: Border.all(color: scheme.primary, width: 2),
),
child: Center(
child: Padding(
padding: const EdgeInsets.all(24),
child: Text(
'Drop to upload to ${widget.folderLabel}',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.titleMedium,
),
),
),
),
),
),
),
],
);
}
}
@@ -0,0 +1,11 @@
import 'files_drop.dart';
/// Native fallback for file drag-and-drop in the Files panel.
///
/// Only the web host observes drag events in this slice; a native host keeps
/// the Upload button and reports the missing drop capability plainly.
const String filesDropUnsupportedMessage =
'Dropping files is not supported on this platform yet.';
FilesDrop get hostFilesDrop =>
const FilesDrop.unsupported(filesDropUnsupportedMessage);
@@ -0,0 +1,177 @@
import 'dart:async';
import 'dart:js_interop';
import 'dart:ui' show Offset;
import '../domain/chat_attachment_picker.dart';
import 'files_drop.dart';
/// Browser file drops observed at the document for each mounted Files panel.
/// A drag that carries files and sits over an accepting area is taken over
/// (so the browser does not navigate to the dropped file); everything else
/// keeps the browser's default. Uses dart:js_interop so the bridge supports
/// JavaScript and WebAssembly builds.
@JS('document')
external _Document get _document;
extension type _Document._(JSObject _) implements JSObject {
external void addEventListener(
JSString type,
JSFunction listener,
JSBoolean capture,
);
external void removeEventListener(
JSString type,
JSFunction listener,
JSBoolean capture,
);
external _Element? querySelector(JSString selectors);
}
extension type _Element._(JSObject _) implements JSObject {
external _DOMRect getBoundingClientRect();
}
extension type _DOMRect._(JSObject _) implements JSObject {
external double get left;
external double get top;
}
extension type _DragEvent._(JSObject _) implements JSObject {
external double get clientX;
external double get clientY;
external _DataTransfer? get dataTransfer;
external JSObject? get relatedTarget;
external void preventDefault();
}
extension type _DataTransfer._(JSObject _) implements JSObject {
external JSArray<JSString> get types;
external _FileList get files;
external set dropEffect(JSString value);
}
extension type _FileList._(JSObject _) implements JSObject {
external int get length;
external _File? item(int index);
}
extension type _File._(JSObject _) implements JSObject {
external JSString get name;
external JSString get type;
external JSPromise<JSArrayBuffer> arrayBuffer();
}
FilesDrop get hostFilesDrop => const FilesDrop(subscribe: _subscribe);
bool _carriesFiles(_DragEvent event) {
final data = event.dataTransfer;
if (data == null) return false;
for (final type in data.types.toDart) {
if (type.toDart == 'Files') return true;
}
return false;
}
/// The pointer in Flutter view coordinates. The view normally fills the page,
/// so this is the viewport position minus the view's own offset.
Offset _position(_DragEvent event) {
final view = _document.querySelector('flutter-view'.toJS);
if (view == null) return Offset(event.clientX, event.clientY);
final rect = view.getBoundingClientRect();
return Offset(event.clientX - rect.left, event.clientY - rect.top);
}
FilesDropUnsubscribe _subscribe(FilesDropListener listener) {
var hovering = false;
void leave() {
if (!hovering) return;
hovering = false;
listener.onLeave();
}
void handleOver(JSObject raw) {
final event = _DragEvent._(raw);
if (!_carriesFiles(event)) return;
final position = _position(event);
if (!listener.accepts(position)) {
leave();
return;
}
event.preventDefault();
event.dataTransfer!.dropEffect = 'copy'.toJS;
hovering = true;
listener.onHover(position);
}
void handleLeave(JSObject raw) {
// relatedTarget is null when the drag leaves the window; movement between
// page elements keeps reporting through dragover.
if (_DragEvent._(raw).relatedTarget == null) leave();
}
void handleDrop(JSObject raw) {
final event = _DragEvent._(raw);
if (!_carriesFiles(event)) return;
final accepted = listener.accepts(_position(event));
leave();
if (!accepted) return;
event.preventDefault();
final files = event.dataTransfer!.files;
final dropped = <_File>[];
for (var index = 0; index < files.length; index += 1) {
final file = files.item(index);
if (file != null) dropped.add(file);
}
unawaited(_readFiles(dropped, listener));
}
final over = handleOver.toJS;
final left = handleLeave.toJS;
final drop = handleDrop.toJS;
_document.addEventListener('dragenter'.toJS, over, true.toJS);
_document.addEventListener('dragover'.toJS, over, true.toJS);
_document.addEventListener('dragleave'.toJS, left, true.toJS);
_document.addEventListener('drop'.toJS, drop, true.toJS);
return () {
_document.removeEventListener('dragenter'.toJS, over, true.toJS);
_document.removeEventListener('dragover'.toJS, over, true.toJS);
_document.removeEventListener('dragleave'.toJS, left, true.toJS);
_document.removeEventListener('drop'.toJS, drop, true.toJS);
};
}
Future<void> _readFiles(List<_File> files, FilesDropListener listener) async {
final picked = <ChatPickedFile>[];
final failures = <Object>[];
for (final file in files) {
try {
final buffer = await file.arrayBuffer().toDart;
picked.add(
ChatPickedFile(
name: file.name.toDart,
mime: file.type.toDart,
bytes: buffer.toDart.asUint8List(),
),
);
} catch (error) {
// A dropped folder has no readable bytes; it must not hide the files.
failures.add(error);
}
}
if (picked.isNotEmpty) listener.onDrop(picked);
for (final failure in failures) {
listener.onError(failure);
}
}
@@ -0,0 +1,91 @@
import 'package:flutter/material.dart';
import '../data/directory_listing.dart';
import 'files_icons.dart';
import 'files_models.dart';
import 'files_thumbnail.dart';
/// The folder's entries as the list or the thumbnail grid. Tapping opens a
/// folder or a file through [onTap]; [menu] supplies a file's action menu.
class FilesEntries extends StatelessWidget {
const FilesEntries({
super.key,
required this.entries,
required this.view,
required this.endpoint,
required this.onTap,
required this.menu,
});
final List<DirectoryEntry> entries;
final FilesViewMode view;
final String endpoint;
final ValueChanged<DirectoryEntry> onTap;
final Widget Function(DirectoryEntry entry) menu;
@override
Widget build(BuildContext context) =>
view == FilesViewMode.grid ? _grid() : _list();
Widget _list() => ListView.builder(
itemCount: entries.length,
itemBuilder: (_, index) {
final entry = entries[index];
return ListTile(
onTap: () => onTap(entry),
leading: Icon(fileIcon(entry)),
title: Text(entry.name, maxLines: 2, overflow: TextOverflow.ellipsis),
subtitle: Text(
entry.isDir
? 'Folder'
: '${fileModifiedLabel(entry.modified)} · ${fileSizeLabel(entry.size)}',
),
trailing: entry.isDir
? null
: Row(
mainAxisSize: MainAxisSize.min,
children: [Text(fileKind(entry)), menu(entry)],
),
);
},
);
Widget _grid() => GridView.builder(
padding: const EdgeInsets.all(12),
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 180,
childAspectRatio: .85,
),
itemCount: entries.length,
itemBuilder: (_, index) {
final entry = entries[index];
return InkWell(
onTap: () => onTap(entry),
child: Card(
child: Padding(
padding: const EdgeInsets.all(12),
child: Column(
children: [
Align(
alignment: Alignment.centerRight,
child: entry.isDir ? const SizedBox(height: 48) : menu(entry),
),
Expanded(
child: Center(
child: FileThumbnail(entry: entry, endpoint: endpoint),
),
),
const SizedBox(height: 8),
Text(
entry.name,
maxLines: 2,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.center,
),
],
),
),
),
);
},
);
}
@@ -2,6 +2,7 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../ui/layout/app_overflow_menu.dart';
import '../data/directory_listing.dart';
import '../domain/chat_attachment_picker.dart';
import '../ui/sessions/chat_adaptive.dart';
import 'files_controller.dart';
import 'files_models.dart';
@@ -11,9 +12,23 @@ class FilesHeader extends StatelessWidget {
super.key,
required this.controller,
required this.filter,
required this.picker,
required this.onUpload,
});
final FilesController controller;
final TextEditingController filter;
/// The host's file picker; an unsupported host disables Upload with its
/// reason in the tooltip.
final ChatAttachmentPicker picker;
final VoidCallback onUpload;
bool get _canUpload =>
picker.isSupported &&
controller.listing != null &&
!controller.loading &&
controller.uploading == 0;
@override
Widget build(BuildContext context) {
final listing = controller.listing;
@@ -40,6 +55,13 @@ class FilesHeader extends StatelessWidget {
overflow: TextOverflow.ellipsis,
),
),
IconButton(
tooltip: picker.isSupported
? 'Upload files'
: 'Upload files: ${picker.unsupportedMessage}',
onPressed: _canUpload ? onUpload : null,
icon: const Icon(Icons.upload_file),
),
IconButton(
tooltip: 'Refresh',
onPressed: controller.loading ? null : controller.retry,
+80 -98
View File
@@ -3,11 +3,13 @@ import 'package:flutter/cupertino.dart';
import '../../../ui/adaptive/host.dart';
import '../../../core/transport/pipelines_client.dart';
import '../data/directory_listing.dart';
import '../domain/chat_attachment_picker.dart';
import '../ui/sessions/chat_adaptive.dart';
import 'files_controller.dart';
import 'files_icons.dart';
import 'files_models.dart';
import 'files_thumbnail.dart';
import 'files_drop.dart';
import 'files_drop_area.dart';
import 'files_entries.dart';
import 'files_upload_notice.dart';
import 'files_viewer.dart';
import 'files_header.dart';
import '../../../ui/layout/app_overflow_menu.dart';
@@ -28,20 +30,43 @@ class FilesPanel extends StatefulWidget {
required this.controller,
this.onOpen,
this.viewer = const FilesViewer(),
this.picker,
this.drop,
});
final FilesController controller;
final ValueChanged<DirectoryEntry>? onOpen;
/// Opens and downloads files; tests inject one that never fetches.
final FilesViewer viewer;
/// The Upload button's file picker and the host drag-and-drop seam; null
/// means the running host's. Tests inject fakes.
final ChatAttachmentPicker? picker;
final FilesDrop? drop;
@override
State<FilesPanel> createState() => _FilesPanelState();
}
class _FilesPanelState extends State<FilesPanel> {
late final TextEditingController _filter = TextEditingController();
late final ChatAttachmentPicker _picker =
widget.picker ?? hostChatAttachmentPicker;
late final FilesDrop _drop = widget.drop ?? hostFilesDrop;
FilesController get controller => widget.controller;
String get _endpoint => configuredGatewayEndpoint(controller.gateway);
Future<void> _pickAndUpload() async {
final List<ChatPickedFile>? files;
try {
files = await _picker.pick();
} catch (error) {
controller.reportUploadError('$error');
return;
}
if (files == null || files.isEmpty) return;
await controller.upload(files);
}
@override
void initState() {
super.initState();
@@ -80,109 +105,66 @@ class _FilesPanelState extends State<FilesPanel> {
child: Builder(
builder: (context) => Material(
color: Theme.of(context).colorScheme.surface,
child: Column(
children: [
FilesHeader(controller: controller, filter: _filter),
if (controller.error != null)
ChatErrorNotice(
message: controller.error!,
onRetry: controller.retry,
child: FilesDropArea(
drop: _drop,
folderLabel: listing == null
? null
: homeRelativeLabel(listing.home, listing.path),
onFiles: controller.upload,
onError: (error) => controller.reportUploadError(
'A dropped item could not be read: $error',
),
child: Column(
children: [
FilesHeader(
controller: controller,
filter: _filter,
picker: _picker,
onUpload: _pickAndUpload,
),
if (controller.loading && listing == null)
const Expanded(child: ChatLoadingIndicator())
else if (listing != null &&
entries.isEmpty &&
!controller.loading)
Expanded(
child: _FilesMessage(
controller.filter.isEmpty
? 'This folder is empty.'
: 'No files match your filter.',
if (controller.error != null)
ChatErrorNotice(
message: controller.error!,
onRetry: controller.retry,
),
)
else
Expanded(
child: controller.view == FilesViewMode.grid
? _grid(entries)
: _list(entries),
),
if (listing?.truncated == true)
const Padding(
padding: EdgeInsets.all(8),
child: Text('Some entries are not shown.'),
),
],
FilesUploadNotice(controller: controller),
if (controller.loading && listing == null)
const Expanded(child: ChatLoadingIndicator())
else if (listing != null &&
entries.isEmpty &&
!controller.loading)
Expanded(
child: _FilesMessage(
controller.filter.isEmpty
? 'This folder is empty.'
: 'No files match your filter.',
),
)
else
Expanded(
child: FilesEntries(
entries: entries,
view: controller.view,
endpoint: _endpoint,
onTap: (entry) => entry.isDir
? controller.open(entry.path)
: _open(entry),
menu: _menu,
),
),
if (listing?.truncated == true)
const Padding(
padding: EdgeInsets.all(8),
child: Text('Some entries are not shown.'),
),
],
),
),
),
),
);
}
Widget _list(List<DirectoryEntry> entries) => ListView.builder(
itemCount: entries.length,
itemBuilder: (_, index) {
final entry = entries[index];
return ListTile(
onTap: () => entry.isDir ? controller.open(entry.path) : _open(entry),
leading: Icon(fileIcon(entry)),
title: Text(entry.name, maxLines: 2, overflow: TextOverflow.ellipsis),
subtitle: Text(
entry.isDir
? 'Folder'
: '${fileModifiedLabel(entry.modified)} · ${fileSizeLabel(entry.size)}',
),
trailing: entry.isDir
? null
: Row(
mainAxisSize: MainAxisSize.min,
children: [Text(fileKind(entry)), _menu(entry)],
),
);
},
);
Widget _grid(List<DirectoryEntry> entries) => GridView.builder(
padding: const EdgeInsets.all(12),
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 180,
childAspectRatio: .85,
),
itemCount: entries.length,
itemBuilder: (_, index) {
final entry = entries[index];
return InkWell(
onTap: () => entry.isDir ? controller.open(entry.path) : _open(entry),
child: Card(
child: Padding(
padding: const EdgeInsets.all(12),
child: Column(
children: [
Align(
alignment: Alignment.centerRight,
child: entry.isDir
? const SizedBox(height: 48)
: _menu(entry),
),
Expanded(
child: Center(
child: FileThumbnail(entry: entry, endpoint: _endpoint),
),
),
const SizedBox(height: 8),
Text(
entry.name,
maxLines: 2,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.center,
),
],
),
),
),
);
},
);
/// Open and Download for one file; a folder has no menu (nothing to save).
Widget _menu(DirectoryEntry entry) => AppOverflowMenu<FileAction>(
label: 'File actions',
@@ -0,0 +1,99 @@
import 'dart:convert';
import 'dart:typed_data';
import '../../../core/transport/server_url.dart';
import '../data/upload/chat_upload.dart';
import '../domain/chat_attachment_picker.dart';
/// Posts one file's bytes to a `POST /files?path=<folder>` URL.
typedef FilesUploadPost =
Future<ChatUploadReply> Function(
Uri uri, {
required String name,
required String mime,
required Uint8List bytes,
});
/// The transport the Files panel uploads with.
///
/// Production keeps [postChatUpload] (raw body, name header, session cookie).
/// Widget tests swap in a recorder so no socket is opened. Restore it after a
/// test.
FilesUploadPost postFileUpload = postChatUpload;
/// Why one file could not be stored, in words the panel shows as they are.
class FilesUploadException implements Exception {
const FilesUploadException(this.message);
final String message;
@override
String toString() => message;
}
/// Uploads [file] into [directory] on the server behind [endpoint] and returns
/// 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.
Future<String> uploadFileInto(
String endpoint,
String directory,
ChatPickedFile file,
) async {
final name = file.name.trim();
if (name.isEmpty) {
throw const FilesUploadException(
'A file without a name cannot be uploaded.',
);
}
final Uri uri;
try {
uri = ServerUrl.resourceUri(
endpoint,
'/files',
query: <String, String>{'path': directory},
);
} on FormatException {
throw const FilesUploadException(
'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');
}
final reason = reply.body.trim();
switch (reply.status) {
case 200:
break;
case 401:
throw const FilesUploadException(
'Sign in to upload files (Settings → Sign in).',
);
case 409:
throw FilesUploadException('"$name" already exists in this folder.');
case 413:
throw FilesUploadException(
'"$name" is too large to upload (100 MiB limit).',
);
default:
throw FilesUploadException(
reason.isEmpty
? '"$name" could not be uploaded (HTTP ${reply.status}).'
: '"$name" could not be uploaded: $reason',
);
}
try {
final decoded = jsonDecode(reply.body);
final path = decoded is Map ? decoded['path'] : null;
if (path is String && path.isNotEmpty) return path;
} on FormatException {
// Reported below.
}
throw FilesUploadException('"$name" upload returned no stored path.');
}
@@ -0,0 +1,60 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../ui/sessions/chat_adaptive.dart';
import 'files_controller.dart';
/// The Files panel's upload feedback: a progress strip while files are being
/// stored and the server's refusal (exists, sign in, too large) until the
/// operator dismisses it. Upload failures are separate from the folder error,
/// whose Retry reloads the listing.
class FilesUploadNotice extends StatelessWidget {
const FilesUploadNotice({super.key, required this.controller});
final FilesController controller;
@override
Widget build(BuildContext context) {
final error = controller.uploadError;
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
return Column(
children: [
if (controller.uploading > 0)
Semantics(
label: controller.uploadLabel,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
child: Row(
children: [
Expanded(
child: cupertino
? const CupertinoActivityIndicator()
: const LinearProgressIndicator(),
),
const SizedBox(width: 12),
Text(controller.uploadLabel),
],
),
),
),
if (error != null)
cupertino
? ChatCupertinoMessage(
message: error,
icon: CupertinoIcons.exclamationmark_circle,
actionLabel: 'Dismiss',
onAction: controller.dismissUploadError,
)
: MaterialBanner(
content: Text(error),
actions: [
TextButton(
onPressed: controller.dismissUploadError,
child: const Text('Dismiss'),
),
],
),
],
);
}
}
+104
View File
@@ -0,0 +1,104 @@
# Files panel: upload into the shown folder
## Behaviour
- The chat Files panel header has an **Upload files** button (next to Refresh).
It opens the host file picker (`hostChatAttachmentPicker`: `file_picker` on
native, `<input type=file>` on web). On a host without a picker the button is
disabled and its tooltip carries the reason
(`Upload files: <unsupportedMessage>`).
- The whole panel is a drop target. On web, `files_drop_web.dart` observes the
document's `dragenter`/`dragover`/`dragleave`/`drop` events through
`dart:js_interop`; a drag that carries files and sits over the panel is taken
over (default prevented, `dropEffect = copy`) and the panel shows a highlight
reading `Drop to upload to <folder>`. Drags elsewhere keep the browser's
default. Native hosts use `FilesDrop.unsupported` and keep the button only.
- Every picked or dropped file is posted on its own to
`POST /files?path=<shown folder>` (the listing's `path`), with the name in
`X-Pipelines-Upload-Name` (percent-encoded) and the MIME type as
`Content-Type`, through the same transport as chat uploads (`postChatUpload`:
session cookie on native, same-origin fetch on web; relay endpoints post to
`/peers/<id>/files`). After the batch the folder that received the files is
reloaded.
- Progress: `Uploading…` / `Uploading n of m…` with a progress strip; the
Upload button is disabled while a batch is in flight.
- Refusals are shown in the panel until dismissed (not swallowed), one line per
failed file; the rest of the batch is still attempted:
- 409: `"name" already exists in this folder.` (the server never overwrites)
- 401: `Sign in to upload files (Settings → Sign in).`
- 413: `"name" is too large to upload (100 MiB limit).`
- other statuses: the server's text, or `(HTTP n)`
- transport error / unreadable dropped item (a folder): reported in words.
## Server and proxy
- `internal/server/home_files_upload.go`: `POST /files?path=<dir>`. The folder
is resolved like `GET /files` (`expandHomePath` + `resolveInsideHome`, so it
must be inside `$HOME`, symlinks resolved); special filesystems and
non-directories are refused; the name must be a plain file name (no
separators, not `.`/`..`, ≤255 bytes); the file is created with `O_EXCL` and
mode 0644 (set explicitly, independent of umask), so an existing name is
`409 Conflict`; the chat upload limit (100 MiB) applies on `Content-Length`
and on the streamed body (partial file removed). Reply:
`{"name","path","size"}`. `GET /files` is unchanged.
- `web/serve.mjs` already forwarded every `/files` method to the server behind
the sign-in gate; `web/src/peerRelay.js` now allows `POST /files` beside
`POST /chat-uploads` (allowlist + POST check) and forwards
`content-type`, `content-length`, `x-pipelines-upload-name`.
## Files
- `app/lib/features/chat/files/files_upload.dart` — `postFileUpload` seam,
`uploadFileInto`, `FilesUploadException`.
- `files_drop.dart`, `files_drop_stub.dart`, `files_drop_web.dart` — drop seam.
- `files_drop_area.dart` — drop target with highlight.
- `files_upload_notice.dart` — progress and refusal strip (Material banner /
Cupertino message with Dismiss).
- `files_entries.dart` — the list/grid builders moved out of `files_panel.dart`.
- `files_controller.dart` — `upload`, `uploading`, `uploadTotal`,
`uploadError`, `reportUploadError`, `dismissUploadError`.
- `files_header.dart` — Upload button; `files_panel.dart` — `picker`/`drop`
parameters, drop wrap, notice.
- Tests: `app/test/features/chat/files/files_upload_test.dart`,
`files_panel_upload_test.dart`; `internal/server/home_files_upload_test.go`;
`web/test/serve.test.mjs`, `web/test/peerRelay.test.mjs`.
## Verification (2026-11-09)
```
$ go test ./internal/server/ -run 'TestServeHomeFile|TestChatUpload' -v | grep -E "^(PASS|ok|FAIL)"
PASS
ok git.a-wan.net/alan/pipelines/internal/server 0.432s
$ go test ./internal/server/...
ok git.a-wan.net/alan/pipelines/internal/server 65.194s
$ cd web && node --test test/
ℹ tests 16
ℹ pass 16
ℹ fail 0
$ cd app && tool/analyze.sh
analyze: clean; 7s; exit 0
$ cd app && tool/memwatch/full.sh test/features/chat/files
memwatch: PASS: 37 passed, 0 failed, 0 skipped; peak 1263 MB; 6s; exit 0 (test/features/chat/files)
$ cd app && flutter build web --release
Compiling lib/main.dart for the Web... 25.0s
✓ Built build/web
```
## Not done / caveats
- Desktop (macOS/Linux/Windows) drag-and-drop: not implemented. It would need
the `desktop_drop` plugin (a new native dependency in `pubspec.yaml`) whose
desktop builds could not be verified here; desktop hosts get the Upload
button, which works through `file_picker`. Mobile gets the button only, as
specified.
- No real-browser capture of the drag-and-drop interaction was recorded in this
lane (the web build compiles the interop; the drop path is covered through
the seam in widget tests). A browser drop requires a DataTransfer with files,
which the capture kit does not synthesise.
- The web drop listener assumes the Flutter view fills the page (it subtracts
the `flutter-view` element's bounding rect from the pointer position).
@@ -0,0 +1,261 @@
import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/core/transport/pipelines_client.dart';
import 'package:pipelines_app/features/chat/domain/chat_attachment_picker.dart';
import 'package:pipelines_app/features/chat/files/files_controller.dart';
import 'package:pipelines_app/features/chat/files/files_drop.dart';
import 'package:pipelines_app/features/chat/files/files_panel.dart';
import 'package:pipelines_app/features/chat/files/files_upload.dart';
import 'package:pipelines_app/features/chat/files/files_urls.dart';
import 'package:pipelines_app/features/chat/files/files_viewer.dart';
/// Lists `/home/demo` and counts the listings so a refresh after an upload is
/// observable.
class Gateway extends PipelinesClient {
@override
Uri get endpoint => Uri.parse('ws://example.test/connect');
int listings = 0;
@override
Stream<JsonMap> get frames => const Stream.empty();
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
listings += 1;
return {
'directory_listing': {
'path': '/home/demo',
'home': '/home',
'entries': [
{'name': 'notes.txt', 'path': '/home/demo/notes.txt', 'size': 12},
],
},
};
}
}
/// One recorded `POST /files`.
typedef Posted = ({Uri uri, String name, String mime, int size});
ChatPickedFile picked(String name, [String mime = 'text/plain']) =>
ChatPickedFile(name: name, mime: mime, bytes: Uint8List.fromList([1, 2]));
void main() {
late FileUrlResolver originalUrls;
late FilesUploadPost originalPost;
final posted = <Posted>[];
var reply = (status: 200, body: '{"path":"/home/demo/new.txt"}');
Completer<void>? gate;
setUp(() {
originalUrls = resolveFileUrls;
originalPost = postFileUpload;
resolveFileUrls = (_, path) => fileUrls('ws://example.test/connect', path);
posted.clear();
reply = (status: 200, body: '{"path":"/home/demo/new.txt"}');
gate = null;
postFileUpload =
(uri, {required name, required mime, required bytes}) async {
posted.add((uri: uri, name: name, mime: mime, size: bytes.length));
if (gate != null) await gate!.future;
return reply;
};
});
tearDown(() {
resolveFileUrls = originalUrls;
postFileUpload = originalPost;
});
Future<({FilesController controller, Gateway gateway})> pump(
WidgetTester tester, {
ChatAttachmentPicker? picker,
FilesDrop? drop,
}) async {
final gateway = Gateway();
final controller = FilesController(
gateway: gateway,
initialPath: '/home/demo',
);
addTearDown(controller.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: FilesPanel(
controller: controller,
viewer: const FilesViewer(),
picker:
picker ??
ChatAttachmentPicker(select: () async => [picked('new.txt')]),
drop: drop ?? const FilesDrop.unsupported('no drops in tests'),
),
),
),
);
await tester.pumpAndSettle();
return (controller: controller, gateway: gateway);
}
testWidgets('Upload button picks files, posts them to the shown folder and '
'refreshes', (tester) async {
gate = Completer<void>();
final mounted = await pump(tester);
expect(mounted.gateway.listings, 1);
await tester.tap(find.byTooltip('Upload files'));
await tester.pump();
expect(find.text('Uploading…'), findsOneWidget);
expect(find.byType(LinearProgressIndicator), findsOneWidget);
expect(
tester
.widget<IconButton>(
find.ancestor(
of: find.byIcon(Icons.upload_file),
matching: find.byType(IconButton),
),
)
.onPressed,
isNull,
reason: 'a second batch waits for the first',
);
gate!.complete();
await tester.pumpAndSettle();
expect(posted, hasLength(1));
expect(posted.single.uri.path, '/files');
expect(posted.single.uri.queryParameters['path'], '/home/demo');
expect(posted.single.name, 'new.txt');
expect(posted.single.mime, 'text/plain');
expect(posted.single.size, 2);
expect(mounted.gateway.listings, 2, reason: 'the folder is reloaded');
expect(find.text('Uploading…'), findsNothing);
expect(mounted.controller.uploadError, isNull);
});
testWidgets('a cancelled picker uploads nothing', (tester) async {
final mounted = await pump(
tester,
picker: ChatAttachmentPicker(select: () async => null),
);
await tester.tap(find.byTooltip('Upload files'));
await tester.pumpAndSettle();
expect(posted, isEmpty);
expect(mounted.gateway.listings, 1);
});
testWidgets('an unsupported picker disables Upload with its reason', (
tester,
) async {
await pump(
tester,
picker: const ChatAttachmentPicker.unsupported('No picker here.'),
);
final button = tester.widget<IconButton>(
find.ancestor(
of: find.byIcon(Icons.upload_file),
matching: find.byType(IconButton),
),
);
expect(button.onPressed, isNull);
expect(find.byTooltip('Upload files: No picker here.'), findsOneWidget);
});
testWidgets('server refusals are shown until dismissed', (tester) async {
final mounted = await pump(tester);
for (final (status, body, message) in [
(409, 'exists', '"new.txt" already exists in this folder.'),
(
401,
'Sign in required',
'Sign in to upload files (Settings → Sign in).',
),
(413, 'too big', '"new.txt" is too large to upload (100 MiB limit).'),
]) {
reply = (status: status, body: body);
await tester.tap(find.byTooltip('Upload files'));
await tester.pumpAndSettle();
expect(find.byType(MaterialBanner), findsOneWidget);
expect(find.text(message), findsOneWidget);
expect(mounted.controller.uploadError, message);
await tester.tap(find.text('Dismiss'));
await tester.pumpAndSettle();
expect(find.byType(MaterialBanner), findsNothing);
}
expect(posted, hasLength(3));
});
testWidgets('a batch keeps going after one refusal and lists both', (
tester,
) async {
var calls = 0;
postFileUpload =
(uri, {required name, required mime, required bytes}) async {
calls += 1;
return name == 'kept.txt'
? (status: 409, body: 'exists')
: (status: 200, body: '{"path":"/home/demo/$name"}');
};
final mounted = await pump(
tester,
picker: ChatAttachmentPicker(
select: () async => [picked('kept.txt'), picked('other.txt')],
),
);
await tester.tap(find.byTooltip('Upload files'));
await tester.pumpAndSettle();
expect(calls, 2);
expect(
find.text('"kept.txt" already exists in this folder.'),
findsOneWidget,
);
expect(mounted.gateway.listings, 2);
});
testWidgets('a file drag over the panel highlights the folder and a drop '
'uploads there', (tester) async {
FilesDropListener? listener;
var unsubscribed = 0;
final drop = FilesDrop(
subscribe: (registered) {
listener = registered;
return () => unsubscribed += 1;
},
);
final mounted = await pump(tester, drop: drop);
expect(listener, isNotNull);
final inside = tester.getCenter(find.byType(FilesPanel));
expect(listener!.accepts(inside), isTrue);
expect(listener!.accepts(const Offset(-5, -5)), isFalse);
expect(find.text('Drop to upload to ~/demo'), findsNothing);
listener!.onHover(inside);
await tester.pump();
expect(find.text('Drop to upload to ~/demo'), findsOneWidget);
listener!.onLeave();
await tester.pump();
expect(find.text('Drop to upload to ~/demo'), findsNothing);
listener!.onHover(inside);
await tester.pump();
listener!.onDrop([picked('dropped.png', 'image/png')]);
await tester.pumpAndSettle();
expect(find.text('Drop to upload to ~/demo'), findsNothing);
expect(posted, hasLength(1));
expect(posted.single.name, 'dropped.png');
expect(posted.single.mime, 'image/png');
expect(posted.single.uri.queryParameters['path'], '/home/demo');
expect(mounted.gateway.listings, 2);
listener!.onError(StateError('is a directory'));
await tester.pump();
expect(
find.textContaining('A dropped item could not be read'),
findsOneWidget,
);
await tester.pumpWidget(const SizedBox());
expect(unsubscribed, 1);
});
testWidgets('an unsupported drop host keeps the panel and the button', (
tester,
) async {
await pump(tester, drop: const FilesDrop.unsupported('no drops'));
expect(find.text('notes.txt'), findsOneWidget);
expect(find.byTooltip('Upload files'), findsOneWidget);
});
}
@@ -0,0 +1,138 @@
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/domain/chat_attachment_picker.dart';
import 'package:pipelines_app/features/chat/files/files_upload.dart';
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]),
);
test(
'posts to /files with the shown folder and returns the stored path',
() async {
Uri? posted;
String? postedName;
String? postedMime;
postFileUpload =
(uri, {required name, required mime, required bytes}) async {
posted = uri;
postedName = name;
postedMime = mime;
return (
status: 200,
body:
'{"name":"notes.txt","path":"/home/demo/notes.txt","size":3}',
);
};
final path = await uploadFileInto(
'ws://example.test/connect',
'/home/demo',
file,
);
expect(path, '/home/demo/notes.txt');
expect(posted?.path, '/files');
expect(posted?.queryParameters, {'path': '/home/demo'});
expect(posted?.scheme, 'http');
expect(postedName, 'notes.txt');
expect(postedMime, 'text/plain');
},
);
test('a relay endpoint posts through its peer prefix', () async {
Uri? posted;
postFileUpload =
(uri, {required name, required mime, required bytes}) async {
posted = uri;
return (status: 200, body: '{"path":"/home/x/notes.txt"}');
};
await uploadFileInto(
'wss://web.test/peers/laptop/connect',
'/home/x',
file,
);
expect(posted?.path, '/peers/laptop/files');
});
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);
try {
await uploadFileInto('ws://example.test/connect', '/home/demo', file);
} on FilesUploadException catch (error) {
return error.message;
}
return 'no error';
}
expect(
await message(409, 'a file named notes.txt already exists'),
'"notes.txt" already exists in this folder.',
);
expect(
await message(401, 'Sign in required'),
'Sign in to upload files (Settings → Sign in).',
);
expect(
await message(413, 'file exceeds the 100 MiB limit'),
'"notes.txt" is too large to upload (100 MiB limit).',
);
expect(
await message(403, 'folder access denied'),
'"notes.txt" could not be uploaded: folder access denied',
);
expect(
await message(500, ''),
'"notes.txt" could not be uploaded (HTTP 500).',
);
expect(
await message(200, 'not json'),
'"notes.txt" upload returned no stored path.',
);
});
test(
'transport failures and blank names are reported, not swallowed',
() async {
postFileUpload = (uri, {required name, required mime, required bytes}) =>
throw StateError('socket closed');
await expectLater(
uploadFileInto('ws://example.test/connect', '/home/demo', file),
throwsA(
isA<FilesUploadException>().having(
(e) => e.message,
'message',
'"notes.txt" could not be uploaded: Bad state: socket closed',
),
),
);
await expectLater(
uploadFileInto(
'ws://example.test/connect',
'/home/demo',
ChatPickedFile(name: ' ', mime: '', bytes: Uint8List(0)),
),
throwsA(isA<FilesUploadException>()),
);
await expectLater(
uploadFileInto('', '/home/demo', file),
throwsA(
isA<FilesUploadException>().having(
(e) => e.message,
'message',
'No server is configured to upload files to.',
),
),
);
},
);
}
+149
View File
@@ -0,0 +1,149 @@
package server
import (
"encoding/json"
"errors"
"io"
"io/fs"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"unicode/utf8"
)
// maxHomeFileUploadBytes bounds one file stored through POST /files. It
// matches the chat upload limit so both raw-body routes behave alike.
const maxHomeFileUploadBytes = maxChatUploadBytes
type homeFileUploadReply struct {
Name string `json:"name"`
Path string `json:"path"`
Size int64 `json:"size"`
}
// serveHomeFileUpload stores the request body as a new file inside the
// folder named by ?path=, which must lie inside the operator's home. The name
// travels in X-Pipelines-Upload-Name; an existing file is never overwritten.
func serveHomeFileUpload(w http.ResponseWriter, r *http.Request) {
home, err := OperatorHomeDirectory()
if err != nil {
http.NotFound(w, r)
return
}
serveHomeFileUploadTo(w, r, home)
}
func serveHomeFileUploadTo(w http.ResponseWriter, r *http.Request, home string) {
fail := func(status int, message string) { http.Error(w, message, status) }
rawName := r.Header.Get(chatUploadHeader)
if rawName == "" {
fail(http.StatusBadRequest, "missing "+chatUploadHeader)
return
}
if r.Header.Get("Sec-Fetch-Site") == "cross-site" {
fail(http.StatusForbidden, "cross-site upload refused")
return
}
decoded, err := url.PathUnescape(rawName)
if err != nil {
decoded = rawName
}
name, err := uploadFileName(decoded)
if err != nil {
fail(http.StatusBadRequest, err.Error())
return
}
if r.ContentLength > maxHomeFileUploadBytes {
fail(http.StatusRequestEntityTooLarge, "file exceeds the 100 MiB limit")
return
}
home = filepath.Clean(home)
realHome, err := filepath.EvalSymlinks(home)
if err != nil {
fail(http.StatusNotFound, "home directory unavailable")
return
}
target, err := expandHomePath(home, r.URL.Query().Get("path"))
if err != nil {
fail(http.StatusNotFound, "folder not found")
return
}
_, realDir, err := resolveInsideHome(realHome, target)
if err != nil {
if strings.Contains(err.Error(), "outside the home directory") {
fail(http.StatusForbidden, "folder access denied")
} else {
fail(http.StatusNotFound, "folder not found")
}
return
}
info, err := os.Stat(realDir)
if err != nil || !info.IsDir() {
fail(http.StatusNotFound, "folder not found")
return
}
if specialFilesystem(realDir) {
fail(http.StatusForbidden, "folder access denied")
return
}
file, err := os.OpenFile(filepath.Join(realDir, name), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
if err != nil {
switch {
case errors.Is(err, fs.ErrExist):
fail(http.StatusConflict, "a file named "+name+" already exists")
case errors.Is(err, fs.ErrPermission):
fail(http.StatusForbidden, "folder access denied")
default:
fail(http.StatusInternalServerError, "write file: "+err.Error())
}
return
}
// The process umask may narrow the create mode; the stored file is meant
// to be readable like one the operator saved themselves.
size, err := int64(0), file.Chmod(0o644)
if err == nil {
size, err = io.Copy(file, io.LimitReader(r.Body, maxHomeFileUploadBytes+1))
}
if err == nil && size > maxHomeFileUploadBytes {
err = errors.New("file exceeds the 100 MiB limit")
}
if err == nil {
err = file.Sync()
}
if closeErr := file.Close(); err == nil {
err = closeErr
}
if err != nil {
_ = os.Remove(file.Name())
status := http.StatusBadRequest
if size > maxHomeFileUploadBytes {
status = http.StatusRequestEntityTooLarge
}
fail(status, err.Error())
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-store")
_ = json.NewEncoder(w).Encode(homeFileUploadReply{Name: name, Path: filepath.Join(target, name), Size: size})
}
// uploadFileName accepts one plain file name: no directory parts, no NUL,
// not "." or "..", and short enough for common filesystems. The name is kept
// as given rather than rewritten, so the stored file is the one the operator
// asked for.
func uploadFileName(value string) (string, error) {
name := strings.TrimSpace(value)
switch {
case name == "" || name == "." || name == "..":
return "", errors.New("file name is missing")
case strings.ContainsAny(name, "/\\\x00"):
return "", errors.New("file name must not contain path separators")
case !utf8.ValidString(name):
return "", errors.New("file name is not valid UTF-8")
case len(name) > 255:
return "", errors.New("file name is too long")
}
return name, nil
}
+153
View File
@@ -0,0 +1,153 @@
package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
func postHomeFile(t *testing.T, home, dir, name, body string) *httptest.ResponseRecorder {
t.Helper()
req := httptest.NewRequest(http.MethodPost, "/files?path="+dir, strings.NewReader(body))
if name != "" {
req.Header.Set(chatUploadHeader, name)
}
req.Header.Set("Content-Type", "text/plain")
rec := httptest.NewRecorder()
serveHomeFileUploadTo(rec, req, home)
return rec
}
func TestServeHomeFileUpload(t *testing.T) {
root := t.TempDir()
home := filepath.Join(root, "home")
outside := filepath.Join(root, "outside")
for _, dir := range []string{filepath.Join(home, "project"), outside} {
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
}
if err := os.WriteFile(filepath.Join(home, "project", "kept.txt"), []byte("original"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Symlink(outside, filepath.Join(home, "escape")); err != nil {
t.Fatal(err)
}
t.Run("stores a new file in the shown folder", func(t *testing.T) {
rec := postHomeFile(t, home, "~/project", "big%20photo.txt", "hello upload")
if rec.Code != http.StatusOK {
t.Fatalf("status = %d %s", rec.Code, rec.Body)
}
var reply homeFileUploadReply
if err := json.Unmarshal(rec.Body.Bytes(), &reply); err != nil {
t.Fatal(err)
}
want := filepath.Join(home, "project", "big photo.txt")
if reply.Name != "big photo.txt" || reply.Path != want || reply.Size != int64(len("hello upload")) {
t.Fatalf("reply = %+v", reply)
}
info, err := os.Stat(want)
if err != nil || info.Mode().Perm() != 0o644 {
t.Fatalf("stored file = %v %v", info, err)
}
data, _ := os.ReadFile(want)
if string(data) != "hello upload" {
t.Fatalf("contents = %q", data)
}
})
t.Run("operator home seam and absolute path", func(t *testing.T) {
originalHomeDirectory := operatorHomeDirectory
operatorHomeDirectory = func() (string, error) { return home, nil }
t.Cleanup(func() { operatorHomeDirectory = originalHomeDirectory })
req := httptest.NewRequest(http.MethodPost, "/files?path="+filepath.Join(home, "project"), strings.NewReader("x"))
req.Header.Set(chatUploadHeader, "seam.txt")
rec := httptest.NewRecorder()
serveHomeFileUpload(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d %s", rec.Code, rec.Body)
}
if _, err := os.Stat(filepath.Join(home, "project", "seam.txt")); err != nil {
t.Fatal(err)
}
})
t.Run("existing file is refused and kept", func(t *testing.T) {
rec := postHomeFile(t, home, "~/project", "kept.txt", "replacement")
if rec.Code != http.StatusConflict || !strings.Contains(rec.Body.String(), "already exists") {
t.Fatalf("response = %d %s", rec.Code, rec.Body)
}
data, _ := os.ReadFile(filepath.Join(home, "project", "kept.txt"))
if string(data) != "original" {
t.Fatalf("contents = %q, want original", data)
}
})
t.Run("empty file is stored", func(t *testing.T) {
rec := postHomeFile(t, home, "~", "empty.txt", "")
if rec.Code != http.StatusOK {
t.Fatalf("status = %d %s", rec.Code, rec.Body)
}
info, err := os.Stat(filepath.Join(home, "empty.txt"))
if err != nil || info.Size() != 0 {
t.Fatalf("stored file = %v %v", info, err)
}
})
refusals := []struct {
label string
dir string
name string
want int
}{
{"missing name", "~/project", "", http.StatusBadRequest},
{"name with slash", "~/project", "sub%2Fname.txt", http.StatusBadRequest},
{"dot dot name", "~/project", "..", http.StatusBadRequest},
{"outside home", filepath.Join(home, "..", "outside"), "x.txt", http.StatusForbidden},
{"symlink outside home", "~/escape", "x.txt", http.StatusForbidden},
{"missing folder", "~/nowhere", "x.txt", http.StatusNotFound},
{"file is not a folder", "~/project/kept.txt", "x.txt", http.StatusNotFound},
{"relative path", "project", "x.txt", http.StatusNotFound},
}
for _, tc := range refusals {
t.Run(tc.label, func(t *testing.T) {
rec := postHomeFile(t, home, tc.dir, tc.name, "x")
if rec.Code != tc.want {
t.Fatalf("status = %d %s, want %d", rec.Code, rec.Body, tc.want)
}
})
}
if entries, _ := os.ReadDir(outside); len(entries) != 0 {
t.Fatalf("outside directory gained %d entries", len(entries))
}
t.Run("declared size over the limit", func(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/files?path=~/project", strings.NewReader("x"))
req.Header.Set(chatUploadHeader, "huge.bin")
req.ContentLength = maxHomeFileUploadBytes + 1
rec := httptest.NewRecorder()
serveHomeFileUploadTo(rec, req, home)
if rec.Code != http.StatusRequestEntityTooLarge {
t.Fatalf("status = %d %s", rec.Code, rec.Body)
}
if _, err := os.Stat(filepath.Join(home, "project", "huge.bin")); !os.IsNotExist(err) {
t.Fatalf("oversized file was stored: %v", err)
}
})
t.Run("cross-site post is refused", func(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/files?path=~/project", strings.NewReader("x"))
req.Header.Set(chatUploadHeader, "cross.txt")
req.Header.Set("Sec-Fetch-Site", "cross-site")
rec := httptest.NewRecorder()
serveHomeFileUploadTo(rec, req, home)
if rec.Code != http.StatusForbidden {
t.Fatalf("status = %d %s", rec.Code, rec.Body)
}
})
}
+1
View File
@@ -155,6 +155,7 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
})
mux.HandleFunc("GET /discovery", serveServerDiscovery)
mux.HandleFunc("GET /files", serveHomeFile)
mux.HandleFunc("POST /files", serveHomeFileUpload)
mux.HandleFunc("POST /chat-uploads", serveChatUpload)
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
+3 -1
View File
@@ -17,6 +17,8 @@ const allowed = (path) =>
path === "/chat-uploads" ||
path.startsWith("/artifacts/") ||
path.startsWith("/updates/");
// Raw-body uploads: chat attachments and files stored into a home folder.
const postable = (path) => path === "/chat-uploads" || path === "/files";
export class PeerRelay extends PeerRegistry {
target(req, path) {
const parsed = new URL(req.url, "http://localhost");
@@ -45,7 +47,7 @@ export class PeerRelay extends PeerRegistry {
!target ||
!allowed(target.url.pathname) ||
!["GET", "HEAD", "POST"].includes(req.method) ||
(req.method === "POST" && target.url.pathname !== "/chat-uploads")
(req.method === "POST" && !postable(target.url.pathname))
) {
json(res, 404, { error: "Unsupported remote resource" });
return true;
+56
View File
@@ -76,6 +76,22 @@ async function fixture(t) {
return;
}
res.setHeader("Set-Cookie", "do-not-forward=private");
if (req.method === "POST") {
let body = "";
for await (const chunk of req) body += chunk;
res.end(
JSON.stringify({
name,
method: req.method,
url: req.url,
cookie: req.headers.cookie,
upload: req.headers["x-pipelines-upload-name"],
type: req.headers["content-type"],
body,
}),
);
return;
}
if (
req.url.startsWith("/artifacts/") ||
req.url.includes("untrusted.svg")
@@ -289,6 +305,46 @@ test("browser can authenticate both machines, relay files and keep local credent
);
});
test("file uploads relay to the chosen machine's home folder and other POSTs are refused", async (t) => {
const f = await fixture(t);
await f.register("one", {
endpoints: [f.remotes[0]],
password: "password-one",
});
const upload = (path, headers = {}) =>
fetch(f.base + `/peers/one${path}`, {
method: "POST",
headers: {
Cookie: f.cookie,
"Content-Type": "text/plain",
"X-Pipelines-Upload-Name": "notes%20two.txt",
...headers,
},
body: "hello upload",
});
const reply = await upload("/files?path=%2Fhome%2Fproject");
assert.equal(reply.status, 200);
assert.equal(reply.headers.get("set-cookie"), null);
assert.deepEqual(await reply.json(), {
name: "one",
method: "POST",
url: "/files?path=%2Fhome%2Fproject",
cookie: "pipelines_session=remote-one",
upload: "notes%20two.txt",
type: "text/plain",
body: "hello upload",
});
// The remote never sees a POST the relay does not allow.
const before = f.observations.length;
assert.equal((await upload("/artifacts/1/2/report.txt")).status, 404);
assert.equal((await upload("/discovery")).status, 404);
assert.equal(f.observations.length, before);
assert.equal(
(await upload("/files?path=%2Fhome", { Cookie: await f.login() })).status,
401,
);
});
test("websocket relay authenticates the chosen machine without forwarding browser Origin", async (t) => {
const f = await fixture(t);
await f.register("two", {
+50
View File
@@ -139,3 +139,53 @@ test("files require sign-in and proxy query, ranges and attachment headers", asy
{ method: "HEAD", url: path, range: undefined },
]);
});
test("file uploads require sign-in and reach the server with their name header", async (t) => {
const requests = [];
const upstream = createServer(async (req, res) => {
let body = "";
for await (const chunk of req) body += chunk;
requests.push({
method: req.method,
url: req.url,
upload: req.headers["x-pipelines-upload-name"],
type: req.headers["content-type"],
body,
});
res.writeHead(409, { "Content-Type": "text/plain; charset=utf-8" });
res.end("a file named notes.txt already exists");
}).listen(0, "127.0.0.1");
await once(upstream, "listening");
t.after(() => new Promise((done) => upstream.close(done)));
const dir = await mkdtemp(join(tmpdir(), "serve-upload-"));
const auth = join(dir, "auth.json");
await writeFile(auth, JSON.stringify({ password: "password-1", secret: "s".repeat(40) }));
const port = await freePort();
const child = spawn(process.execPath, ["serve.mjs", "--port", String(port),
"--pipelines", `127.0.0.1:${upstream.address().port}`], {
cwd: here,
env: { ...process.env, PIPELINES_WEB_AUTH: "", PIPELINES_WEB_AUTH_FILE: auth },
stdio: ["ignore", "pipe", "inherit"],
});
t.after(async () => { const exited = once(child, "exit"); child.kill(); await exited; });
await once(child.stdout, "data");
const base = `http://127.0.0.1:${port}`;
const path = "/files?path=%2Fhome%2Fproject";
const post = (headers) => fetch(base + path, {
method: "POST",
headers: { "Content-Type": "text/plain", "X-Pipelines-Upload-Name": "notes.txt", ...headers },
body: "hello upload",
});
assert.equal((await post({})).status, 401);
assert.equal(requests.length, 0);
const login = await fetch(base + "/login", {
method: "POST", body: new URLSearchParams({ password: "password-1" }), redirect: "manual",
});
const cookie = login.headers.get("set-cookie").split(";")[0];
const reply = await post({ Cookie: cookie });
assert.equal(reply.status, 409);
assert.equal(await reply.text(), "a file named notes.txt already exists");
assert.deepEqual(requests, [
{ method: "POST", url: path, upload: "notes.txt", type: "text/plain", body: "hello upload" },
]);
});