Files
LangBot/plugins/misc/StickerHub/tests/test_assets_readonly.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

96 lines
3.2 KiB
Python

"""Materialisation must work on an installed, read-only artifact tree.
``_make_tree_read_only()`` in the SDK chmods an installed ``code/`` tree to
``0o555``/``0o444``. ``mkdir`` then fails with ``EACCES`` even for the owner,
so every upload silently stayed in plugin storage and every stable URL was a
404. ``os.chmod`` still works on an owner-owned directory, so the writer has to
re-grant the write bit instead of giving up.
"""
from __future__ import annotations
import asyncio
import os
import stat
import pytest
PNG = b"\x89PNG\r\n\x1a\n" + b"\x00" * 16
def run(coro):
return asyncio.run(coro)
def make_read_only(path):
"""Mirror the SDK's read-only tree, including the stickers directory."""
stickers = path / "assets" / "stickers"
stickers.mkdir(parents=True, exist_ok=True)
(path / "assets" / "icon.svg").write_text("<svg/>", encoding="utf-8")
(stickers / ".gitkeep").write_text("", encoding="utf-8")
for entry in (stickers, path / "assets", path):
entry.chmod(0o555)
for entry in (path / "assets" / "icon.svg", stickers / ".gitkeep"):
entry.chmod(0o444)
return stickers
def test_missing_stickers_dir_is_created_in_a_read_only_tree(make_plugin, tmp_path, monkeypatch):
async def scenario():
# Installed artifacts ship no assets/stickers at all: the empty
# directory is gone, leaving only a read-only assets/.
(tmp_path / "assets").mkdir()
(tmp_path / "assets").chmod(0o555)
monkeypatch.chdir(tmp_path)
plugin, _ = make_plugin()
item = await plugin.add_sticker("cat", PNG)
written = tmp_path / "assets" / "stickers" / item["filename"]
assert written.is_file(), "the sticker never reached the served directory"
assert written.read_bytes() == PNG
run(scenario())
def test_existing_read_only_stickers_dir_still_accepts_writes(make_plugin, tmp_path, monkeypatch):
async def scenario():
make_read_only(tmp_path)
monkeypatch.chdir(tmp_path)
plugin, _ = make_plugin()
item = await plugin.add_sticker("cat", PNG)
written = tmp_path / "assets" / "stickers" / item["filename"]
assert written.is_file()
assert written.read_bytes() == PNG
assert not list(written.parent.glob("*.tmp"))
run(scenario())
def test_existing_asset_files_are_left_alone(make_plugin, tmp_path, monkeypatch):
async def scenario():
stickers = make_read_only(tmp_path)
monkeypatch.chdir(tmp_path)
plugin, _ = make_plugin()
await plugin.add_sticker("cat", PNG)
assert (tmp_path / "assets" / "icon.svg").read_text(encoding="utf-8") == "<svg/>"
assert (stickers / ".gitkeep").is_file()
run(scenario())
@pytest.mark.skipif(os.name != "posix", reason="POSIX permission bits only")
def test_write_bit_is_restored_not_dropped(make_plugin, tmp_path, monkeypatch):
async def scenario():
make_read_only(tmp_path)
monkeypatch.chdir(tmp_path)
plugin, _ = make_plugin()
await plugin.add_sticker("cat", PNG)
mode = stat.S_IMODE((tmp_path / "assets" / "stickers").stat().st_mode)
assert mode & stat.S_IWUSR, "the served directory must stay writable"
run(scenario())