Files
pipelines/app/test/application/insecure_clipboard_test.dart
T
alan 18807b8ac2 Clipboard on http: copy and paste work without the Clipboard API
Over plain http on a tailnet address the page is not a secure context and
navigator.clipboard is undefined, so the web engine refused every copy.
ClipboardReportingMessenger now answers clipboard calls through the
browser's legacy clipboard when the API is missing (web only, conditional
import): copy uses execCommand('copy') with a one-shot copy-event listener
(no focus or selection change); a paste key's Clipboard.getData takes the
browser's own paste event, the key reported not-handled so the browser
still fires it. A Paste button/menu says to use Ctrl+V or the https address.
Secure pages keep the engine path. Capture kit: --insecure and clipboard=.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 19:06:58 +00:00

229 lines
7.1 KiB
Dart

import 'dart:async';
import 'dart:ui' as ui;
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/application/clipboard_failures.dart';
import 'package:pipelines_app/application/insecure_clipboard.dart';
/// A browser page whose clipboard [unavailable] says whether it has the
/// Clipboard API; [copied] records legacy copies, [paste] answers reads.
class _Host implements InsecureClipboardHost {
_Host({this.unavailable = true, this.copies = true});
@override
final bool unavailable;
final bool copies;
final List<String> copied = [];
final List<Duration> waits = [];
Completer<String?> paste = Completer();
@override
bool copy(String text) {
copied.add(text);
return copies;
}
@override
Future<String?> nextPaste(Duration timeout) {
waits.add(timeout);
return paste.future;
}
}
/// The engine: records what reaches it and answers every call successfully;
/// keeps the handlers set on it.
class _Engine implements BinaryMessenger {
final List<String> methods = [];
final Map<String, MessageHandler> handlers = {};
static const codec = JSONMethodCodec();
@override
Future<ByteData?>? send(String channel, ByteData? message) async {
methods.add(codec.decodeMethodCall(message).method);
return codec.encodeSuccessEnvelope({'text': 'from the engine'});
}
@override
Future<void> handlePlatformMessage(
String channel,
ByteData? data,
ui.PlatformMessageResponseCallback? callback,
) async {}
@override
void setMessageHandler(String channel, MessageHandler? handler) {
if (handler == null) {
handlers.remove(channel);
} else {
handlers[channel] = handler;
}
}
}
void main() {
const keys = JSONMessageCodec();
late _Host host;
late _Engine engine;
late List<String> reports;
late MethodChannel platform;
Future<Object?>? pasted;
void setUpPage({bool unavailable = true, bool copies = true}) {
host = _Host(unavailable: unavailable, copies: copies);
engine = _Engine();
reports = [];
final messenger = ClipboardReportingMessenger(
engine,
report: reports.add,
insecure: InsecureClipboard(host),
);
platform = MethodChannel(
SystemChannels.platform.name,
const JSONMethodCodec(),
messenger,
);
// The framework's key handler: its handling of a key event may read the
// clipboard (the terminal's paste key) and it reports the key handled.
messenger.setMessageHandler(SystemChannels.keyEvent.name, (message) async {
final read = keys.decodeMessage(message) == 'paste';
if (read) {
pasted = platform.invokeMethod<Object?>(
'Clipboard.getData',
Clipboard.kTextPlain,
);
}
return keys.encodeMessage({'handled': true});
});
}
Future<Object?> pressKey(String what) async {
final reply = await engine.handlers[SystemChannels.keyEvent.name]!(
keys.encodeMessage(what),
);
return keys.decodeMessage(reply);
}
setUp(() {
pasted = null;
setUpPage();
});
test('a copy on an insecure page uses the legacy copy, not the engine', () async {
await platform.invokeMethod<void>('Clipboard.setData', {'text': 'hello'});
expect(host.copied, ['hello']);
expect(engine.methods, isEmpty);
expect(reports, isEmpty);
});
test('a refused legacy copy says the page is not secure', () async {
setUpPage(copies: false);
await expectLater(
platform.invokeMethod<void>('Clipboard.setData', {'text': 'hello'}),
throwsA(
isA<PlatformException>().having(
(e) => e.code,
'code',
insecureClipboardCode,
),
),
);
expect(reports, [insecureCopyMessage()]);
expect(insecureCopyMessage(), contains("isn't secure (http)"));
});
test('a secure page keeps the engine clipboard', () async {
setUpPage(unavailable: false);
await platform.invokeMethod<void>('Clipboard.setData', {'text': 'hello'});
final data = await platform.invokeMethod<Object?>(
'Clipboard.getData',
Clipboard.kTextPlain,
);
expect(host.copied, isEmpty);
expect(engine.methods, ['Clipboard.setData', 'Clipboard.getData']);
expect(data, {'text': 'from the engine'});
});
test('a read outside a key event (a Paste button) says to use the paste '
'key', () async {
await expectLater(
platform.invokeMethod<Object?>('Clipboard.getData', Clipboard.kTextPlain),
throwsA(isA<PlatformException>()),
);
expect(host.waits, isEmpty);
expect(reports, [insecurePasteMessage()]);
expect(insecurePasteMessage(), contains('Ctrl+V'));
});
group('a paste key', () {
test('reads its browser paste event and lets the browser paste', () async {
final reply = pressKey('paste');
// The read was asked for during the key event; it waits for the paste.
expect(host.waits, hasLength(1));
expect(await reply, {'handled': false});
host.paste.complete('pasted text');
expect(await pasted, {'text': 'pasted text'});
expect(engine.methods, isEmpty);
expect(reports, isEmpty);
});
test('other keys keep their handled reply', () async {
expect(await pressKey('other'), {'handled': true});
expect(host.waits, isEmpty);
});
test('a secure page leaves the key reply alone', () async {
setUpPage(unavailable: false);
expect(await pressKey('paste'), {'handled': true});
expect(await pasted, {'text': 'from the engine'});
});
});
test('a read during a key event yields the paste event text', () async {
final clipboard = InsecureClipboard(host);
Future<ByteData?>? read;
final handler = clipboard.keyEventHandler((message) {
read = clipboard.answer(
const MethodCall('Clipboard.getData', Clipboard.kTextPlain),
);
return Future.value(keys.encodeMessage({'handled': true}));
});
final reply = await handler(keys.encodeMessage('key'));
expect(keys.decodeMessage(reply), {'handled': false});
host.paste.complete('pasted');
final data = const JSONMethodCodec().decodeEnvelope((await read)!);
expect(data, {'text': 'pasted'});
});
test('a paste key with no paste event says why', () async {
final clipboard = InsecureClipboard(host);
Future<ByteData?>? read;
await clipboard.keyEventHandler((message) {
read = clipboard.answer(
const MethodCall('Clipboard.getData', Clipboard.kTextPlain),
);
return Future.value(keys.encodeMessage({'handled': true}));
})(keys.encodeMessage('key'));
host.paste.complete(null);
expect(
() async => const JSONMethodCodec().decodeEnvelope((await read)!),
throwsA(
isA<PlatformException>().having(
(e) => e.message,
'message',
insecurePasteMessage(),
),
),
);
});
test('other formats and methods are left to the engine', () {
final clipboard = InsecureClipboard(host);
expect(
clipboard.answer(const MethodCall('Clipboard.getData', 'text/html')),
isNull,
);
expect(clipboard.answer(const MethodCall('Clipboard.hasStrings')), isNull);
});
}