mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-11-22 13:37:32 +08:00
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>
100 lines
3.6 KiB
Python
100 lines
3.6 KiB
Python
"""Stable URL and asset-materialisation behaviour."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
|
|
from stickerlib import media_path
|
|
|
|
PNG = b"\x89PNG\r\n\x1a\n" + b"\x00" * 16
|
|
PNG2 = b"\x89PNG\r\n\x1a\n" + b"\x01" * 16
|
|
|
|
|
|
def run(coro):
|
|
return asyncio.run(coro)
|
|
|
|
|
|
def test_media_url_is_absolute_when_base_configured(make_plugin):
|
|
async def scenario():
|
|
plugin, _ = make_plugin({"public_base_url": "http://localhost:5300"})
|
|
await plugin.add_sticker("cat", PNG)
|
|
url = await plugin.media_url("cat")
|
|
assert url.startswith(
|
|
"http://localhost:5300/api/v1/plugins/langbot-team/StickerHub/assets/stickers/"
|
|
)
|
|
assert url.endswith(".png")
|
|
run(scenario())
|
|
|
|
|
|
def test_media_url_is_stable_for_the_same_sticker(make_plugin):
|
|
async def scenario():
|
|
plugin, _ = make_plugin({"public_base_url": "http://localhost:5300"})
|
|
first = await plugin.add_sticker("cat", PNG)
|
|
again = await plugin.add_sticker("cat", PNG)
|
|
assert first["url"] == again["url"]
|
|
assert first["filename"] == again["filename"]
|
|
|
|
# Different names are distinct stickers, so their URLs differ; both
|
|
# still point at the same content-addressed blob.
|
|
other = await plugin.add_sticker("dog", PNG)
|
|
assert other["filename"] != first["filename"]
|
|
assert other["sha256"] == first["sha256"]
|
|
assert await plugin.media_url("cat") == first["url"]
|
|
run(scenario())
|
|
|
|
|
|
def test_media_url_changes_when_content_changes(make_plugin):
|
|
async def scenario():
|
|
plugin, _ = make_plugin({"public_base_url": "http://localhost:5300"})
|
|
first = await plugin.add_sticker("cat", PNG)
|
|
second = await plugin.add_sticker("cat", PNG2)
|
|
assert first["url"] != second["url"]
|
|
assert await plugin.media_url("cat") == second["url"]
|
|
run(scenario())
|
|
|
|
|
|
def test_media_url_missing_returns_none(make_plugin):
|
|
async def scenario():
|
|
plugin, _ = make_plugin()
|
|
assert await plugin.media_url("missing") is None
|
|
run(scenario())
|
|
|
|
|
|
def test_served_path_matches_asset_path(make_plugin):
|
|
plugin, _ = make_plugin()
|
|
assert plugin.served_path("cat-abc.png") == media_path("cat-abc.png")
|
|
assert plugin.served_path("cat-abc.png") == "assets/stickers/cat-abc.png"
|
|
|
|
|
|
def test_materialize_writes_asset_file(make_plugin, tmp_path, monkeypatch):
|
|
async def scenario():
|
|
monkeypatch.chdir(tmp_path)
|
|
plugin, _ = make_plugin({"materialize_to_assets": True})
|
|
item = await plugin.add_sticker("cat", PNG)
|
|
written = tmp_path / "assets" / "stickers" / item["filename"]
|
|
assert written.is_file()
|
|
assert written.read_bytes() == PNG
|
|
# No temp file left behind.
|
|
assert not list((tmp_path / "assets" / "stickers").glob("*.tmp"))
|
|
run(scenario())
|
|
|
|
|
|
def test_materialize_is_disabled_by_config(make_plugin, tmp_path, monkeypatch):
|
|
async def scenario():
|
|
monkeypatch.chdir(tmp_path)
|
|
plugin, _ = make_plugin({"materialize_to_assets": False})
|
|
item = await plugin.add_sticker("cat", PNG)
|
|
assert not (tmp_path / "assets" / "stickers" / item["filename"]).exists()
|
|
run(scenario())
|
|
|
|
|
|
def test_materialize_never_escapes_sticker_dir(make_plugin, tmp_path, monkeypatch):
|
|
async def scenario():
|
|
monkeypatch.chdir(tmp_path)
|
|
plugin, _ = make_plugin()
|
|
await plugin.add_sticker("cat", PNG)
|
|
# A filename containing path separators cannot escape the directory.
|
|
assert await plugin._materialize({"filename": "../evil.png", "slug": "cat"}) is False
|
|
assert not (tmp_path / "evil.png").exists()
|
|
run(scenario())
|