Files
LangBot/plugins/misc/StickerHub/tests/test_copy_confirm.py
TyperBodyandmultica-agent a91210c43b fix(plugins/misc/StickerHub): copy the URL the page actually holds, inside the real sandbox
Copy was broken twice over. `langbot.api()` already resolves the unwrapped
payload (PageResponse.ok -> HTTP data -> resp.data.data -> the SDK's
resolve(data.data)), so `item.data.url` was always `undefined` and the page
copied an empty string. `writeText('')` resolves without throwing, so it also
never reached the fallback: the UI said "copied" while the clipboard stayed
empty.

The iframe is worse than assumed. It is a `blob:` document under
`sandbox="allow-scripts allow-forms"`, so its origin is opaque: `location.origin`
is "null", relative asset URLs do not load, and `navigator.clipboard` rejects
with NotAllowedError. `/list` returns relative URLs while `public_base_url` is
empty, so even a correct value would have pasted as an unusable path. URLs are
now made absolute against `location.ancestorOrigins[0]` before either the
clipboard or an `<img src>`, and copy reads `item.url` from the list cache
instead of making a redundant `/item` round trip.

Every other call had the same layer mistake: `res.error` is never set on a
resolved value, because failures reject the promise, so no failure ever
surfaced. All call sites now catch, and an empty URL toasts an error rather
than claiming success.

Verified in a real blob: iframe with the real sandbox: relative url -> absolute
-> clipboard read back byte for byte, and the same URL loads as the preview
(0 `/image` calls). Also passes with `navigator.clipboard` removed, proving the
execCommand tier is what carries it.

The host is untouched now, so this is plugin-only; the `allow-modals` flip is
dropped and the clear confirmation stays in-page.

Co-authored-by: multica-agent <github@multica.ai>
2026-11-07 15:09:17 +08:00

59 lines
2.2 KiB
Python

"""The page must survive a sandboxed iframe without ``allow-modals``.
The host renders plugin pages in ``sandbox="allow-scripts allow-forms"``. In
that frame ``window.prompt`` / ``window.confirm`` are silently ignored and
``navigator.clipboard`` usually rejects on the opaque origin, so the page has
to carry its own copy fallbacks and its own confirmation sheet.
"""
from __future__ import annotations
import re
from pathlib import Path
PAGE = Path(__file__).resolve().parents[1] / "components" / "pages" / "hub" / "index.html"
def script() -> str:
return "\n".join(re.findall(r"<script>(.*?)</script>", PAGE.read_text(encoding="utf-8"), re.S))
def test_copy_never_depends_on_a_modal_dialog():
body = script()
# Strip comments: they mention the ignored APIs by name.
code = re.sub(r"//[^\n]*|/\*.*?\*/", "", body, flags=re.S)
assert "window.prompt" not in code
assert not re.search(r"(?<![\w.])confirm\(", code)
def test_copy_tries_clipboard_then_execcommand_then_sheet():
body = script()
copy = body[body.index("async function copyText"):]
copy = copy[: copy.index("async function loadStats")]
clipboard = copy.find("navigator.clipboard.writeText")
legacy = copy.find("legacyCopy(")
sheet = copy.find("openSheet(T('copyPrompt'")
assert -1 not in (clipboard, legacy, sheet), "all three copy tiers must exist"
assert clipboard < legacy < sheet, "the copy tiers must run in fallback order"
# The execCommand tier itself must be the real thing, not a stub.
assert "document.execCommand('copy')" in body
def test_every_copy_tier_that_succeeds_toasts():
body = script()
matches = list(re.finditer(r"toast\(T\('copied'", body))
assert len(matches) >= 2, "each successful copy tier should confirm with a toast"
def test_clear_confirms_in_an_in_page_sheet():
body = script()
clear = body[body.index("getElementById('clear')"):]
clear = clear[: clear.index("addEventListener('click', async", 0) + 400]
assert "openSheet(" in clear, "clear must ask through the page's own sheet"
assert "'/clear'" in clear
def test_clear_aborts_when_the_sheet_is_dismissed():
body = script()
assert re.search(r"if \(!ok\) \{ return; \}", body), "a declined sheet must abort"