Files
pipelines/app/lib/application/insecure_clipboard.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

133 lines
5.0 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
/// The browser's clipboard without the Clipboard API: on a page that is not
/// a secure context (plain http on a non-localhost address) the browser has
/// no `navigator.clipboard`, so Flutter's web engine refuses every
/// `Clipboard.setData` and `Clipboard.getData`.
abstract class InsecureClipboardHost {
/// Whether the Clipboard API is missing, so this host must answer.
bool get unavailable;
/// Writes [text] with the legacy copy command; false when the browser
/// refused it (no user activation).
bool copy(String text);
/// The text of the next browser `paste` event within [timeout] (which it
/// consumes, so nothing else pastes it), empty for a paste with no text,
/// or null when none came.
Future<String?> nextPaste(Duration timeout);
}
/// The error code of a clipboard reply refused because the page is not
/// secure; its message is the whole explanation.
const insecureClipboardCode = 'insecure_context';
bool _apple() =>
defaultTargetPlatform == TargetPlatform.macOS ||
defaultTargetPlatform == TargetPlatform.iOS;
/// Why a copy failed on a page that is not secure.
String insecureCopyMessage() =>
"Could not copy: this page isn't secure (http), so the browser blocks "
'the clipboard here. Open the https address to copy.';
/// Why a paste failed on a page that is not secure, and what works instead.
String insecurePasteMessage() {
final key = _apple() ? 'Cmd+V' : 'Ctrl+V';
return "Could not paste: this page isn't secure (http), so the browser "
'blocks reading the clipboard here. Paste with $key, or open the '
'https address.';
}
/// Answers the app's clipboard calls when the Clipboard API is missing.
///
/// - `Clipboard.setData` copies with the legacy copy command, which the
/// browser allows during a user activation (a key press or a click), as
/// every copy in the app is.
/// - `Clipboard.getData` asked for while a key event is being handled (the
/// terminal's Ctrl+V, Shift+Insert, the desktop's injected paste key)
/// takes the text of the browser's own `paste` event for that key: the
/// key event is reported to the engine as not handled, so the browser
/// still runs its paste. Asked for at any other time (a Paste button or
/// menu item) there is no paste event to read, and the reply says to use
/// the paste key.
///
/// On a secure page ([InsecureClipboardHost.unavailable] false) nothing is
/// answered here and the engine's Clipboard API path runs unchanged.
class InsecureClipboard {
InsecureClipboard(
this.host, {
this.pasteWait = const Duration(milliseconds: 500),
});
final InsecureClipboardHost host;
/// How long a paste key's clipboard read waits for its paste event.
final Duration pasteWait;
static const _methods = JSONMethodCodec();
static const _keys = JSONMessageCodec();
bool _inKeyEvent = false;
bool _readDuringKey = false;
/// The reply to platform-channel [call], or null when the engine answers.
Future<ByteData?>? answer(MethodCall call) {
if (!host.unavailable) return null;
switch (call.method) {
case 'Clipboard.setData':
final arguments = call.arguments;
final text = arguments is Map ? arguments['text'] : null;
if (text is! String) return null;
// Copied now, inside the key or click that asked for it; only the
// reply is asynchronous.
final copied = host.copy(text);
return Future.value(
copied
? _methods.encodeSuccessEnvelope(null)
: _refused(insecureCopyMessage()),
);
case 'Clipboard.getData':
final format = call.arguments;
if (format != null && format != Clipboard.kTextPlain) return null;
if (!_inKeyEvent) {
return Future.value(_refused(insecurePasteMessage()));
}
_readDuringKey = true;
return host
.nextPaste(pasteWait)
.then(
(text) => text == null
? _refused(insecurePasteMessage())
: _methods.encodeSuccessEnvelope({'text': text}),
);
}
return null;
}
static ByteData _refused(String message) =>
_methods.encodeErrorEnvelope(code: insecureClipboardCode, message: message);
/// [handler] for the engine's key events (`flutter/keyevent`), reporting a
/// key event whose handling read the clipboard as not handled, so the
/// browser does not cancel the paste it is about to run.
MessageHandler keyEventHandler(MessageHandler handler) {
return (message) {
if (!host.unavailable) return handler(message);
final outer = _inKeyEvent;
_inKeyEvent = true;
_readDuringKey = false;
final Future<ByteData?>? reply;
try {
reply = handler(message);
} finally {
_inKeyEvent = outer;
}
if (!_readDuringKey || reply == null) return reply;
_readDuringKey = false;
return reply.then((_) => _keys.encodeMessage({'handled': false}));
};
}
}