fix the clipboard over http, and audit the rest
navigator.clipboard is secure-context only, like crypto.randomUUID before it —
over plain http on a tailnet address the object does not exist. Twenty call
sites across eighteen files, in three states that all looked fine in review:
bare calls that threw and killed the handler, optional-chained calls that
silently did nothing, and one carrying the comment "Officer is always behind
HTTPS", which it is not.
The optional-chained ones are the worst of the three: a copy button that reports
success and copies nothing is indistinguishable from a working one until someone
pastes.
helpers/clipboard.ts falls back to document.execCommand('copy') over an
off-screen textarea — deprecated, and it works on any origin because it predates
the secure-context rule. Off-screen rather than hidden, because display:none and
visibility:hidden elements cannot be selected and the copy fails silently.
Reading the clipboard has no equivalent: execCommand('paste') was never permitted
from script. The file browser's paste-a-file path now checks canReadClipboard()
and explains itself instead of throwing.
docs/http-secure-context-audit.md is the full sweep the owner asked for: what was
fixed, what cannot be, and what was checked and found clear. crypto.subtle is
used nowhere in the frontend, which was the one worth confirming since it has no
cheap fallback. Notification's six matches are type names, not the API.
geolocation and navigator.share are already guarded. getUserMedia is in four
files and is being removed — but QrTransfer uses it for the CAMERA, not a
microphone, so "remove audio" does not cover it.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* Copy text, in a browser that may not be in a secure context.
|
||||
*
|
||||
* ── Why this exists ──
|
||||
*
|
||||
* `navigator.clipboard` is SECURE-CONTEXT ONLY. Over plain http on anything that
|
||||
* is not localhost the whole object is undefined, so `navigator.clipboard.writeText`
|
||||
* throws `TypeError: Cannot read properties of undefined`.
|
||||
*
|
||||
* Officer is reached over the tailnet — `http://officer-dev:9000` — which is
|
||||
* neither https nor localhost. Twenty call sites were affected, in three states
|
||||
* that all looked fine in review:
|
||||
*
|
||||
* bare `navigator.clipboard.writeText(x)` threw, killing the handler
|
||||
* `navigator.clipboard?.writeText(x)` did nothing, silently
|
||||
* one carrying the comment "Officer is always behind HTTPS"
|
||||
*
|
||||
* The middle one is the worst: a copy button that reports success and copies
|
||||
* nothing is indistinguishable from a working one until you paste.
|
||||
*
|
||||
* ── The fallback ──
|
||||
*
|
||||
* `document.execCommand('copy')` over a temporary, off-screen textarea. It is
|
||||
* deprecated and it works in every browser that runs this app, on any origin,
|
||||
* because it predates the secure-context rule. Deprecated-and-working beats
|
||||
* modern-and-absent.
|
||||
*
|
||||
* The textarea is positioned off-screen rather than hidden: `display:none` and
|
||||
* `visibility:hidden` elements cannot be selected, so the copy silently fails.
|
||||
* `readOnly` stops a mobile keyboard appearing for the instant it is focused.
|
||||
*
|
||||
* Returns whether it worked, so a caller can say "copied" only when it did.
|
||||
* Reading the clipboard has no equivalent fallback — see `canReadClipboard`.
|
||||
*/
|
||||
export async function copyToClipboard(text: string): Promise<boolean> {
|
||||
if (navigator.clipboard?.writeText) {
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
return true;
|
||||
} catch {
|
||||
// Permission refused, or a document that is not focused. Fall through.
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof document === 'undefined') return false;
|
||||
|
||||
const area = document.createElement('textarea');
|
||||
area.value = text;
|
||||
area.setAttribute('readonly', '');
|
||||
area.style.position = 'fixed';
|
||||
area.style.top = '-9999px';
|
||||
area.style.opacity = '0';
|
||||
document.body.appendChild(area);
|
||||
|
||||
try {
|
||||
area.select();
|
||||
area.setSelectionRange(0, text.length);
|
||||
return document.execCommand('copy');
|
||||
} catch {
|
||||
return false;
|
||||
} finally {
|
||||
document.body.removeChild(area);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the clipboard can be READ on demand.
|
||||
*
|
||||
* There is no fallback for this one. `document.execCommand('paste')` was never
|
||||
* permitted from script, so on an insecure origin the only way to get clipboard
|
||||
* contents is a real paste event the user initiates — which is a different
|
||||
* interaction, not a drop-in.
|
||||
*
|
||||
* Callers should use this to hide a "paste" affordance rather than offer one
|
||||
* that cannot work.
|
||||
*/
|
||||
export function canReadClipboard(): boolean {
|
||||
return typeof navigator !== 'undefined' && typeof navigator.clipboard?.read === 'function';
|
||||
}
|
||||
Reference in New Issue
Block a user