fix: keep inline images in Micropub articles (v2.16.0)
- Content sent as both html and text is stored as the HTML; the text is usually a plain fallback, so images, links and headings were lost. - Images embedded from other sites are copied into the media library (SSRF-checked fetcher, best effort, max 20) and the text points at the copies, on create and on content replace. - HTML content is sanitized as is instead of going through Markdown, which added stray <br>s and empty paragraphs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
978c13358b
commit
ccb9f6dce2
5 files changed
+117
-14
No files matched your search
+64
-1
@@ -9,11 +9,12 @@ without its images; the admin editor has Title and Summary fields.
|
||||
import json
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
import httpx
|
||||
import mf2py
|
||||
|
||||
from starpunk.auth import create_session
|
||||
from starpunk.notes import create_note, get_note
|
||||
from tests.test_micropub_v240 import SITE, app, client, h # noqa: F401
|
||||
from tests.test_micropub_v240 import SITE, app, client, h, jpeg, source # noqa: F401
|
||||
from tests.test_micropub_v265 import upload
|
||||
|
||||
ATOM = {"a": "http://www.w3.org/2005/Atom"}
|
||||
@@ -130,3 +131,65 @@ class TestAdmin:
|
||||
from starpunk.notes import list_notes
|
||||
(note,) = list_notes()
|
||||
assert note.title == "Only a title" and note.content == ""
|
||||
|
||||
|
||||
class TestInlineImages:
|
||||
"""Articles posted with images in their text (v2.16.0)"""
|
||||
|
||||
def remote_host(self, monkeypatch, ok=("/a.jpg",)):
|
||||
fetched = []
|
||||
|
||||
def serve(request): # the fetcher connects to the pinned address, so match on path
|
||||
fetched.append(request.url.path)
|
||||
if request.url.path in ok:
|
||||
return httpx.Response(200, content=jpeg("green"))
|
||||
return httpx.Response(404)
|
||||
|
||||
monkeypatch.setattr("starpunk.webmention_ssrf.TRANSPORT", httpx.MockTransport(serve))
|
||||
monkeypatch.setattr("starpunk.webmention_ssrf.socket.getaddrinfo",
|
||||
lambda *a, **k: [(2, 1, 6, "", ("93.184.216.34", 0))])
|
||||
return fetched
|
||||
|
||||
def test_html_is_kept_over_text_fallback(self, app, client, h, monkeypatch):
|
||||
self.remote_host(monkeypatch)
|
||||
html = '<p>Intro <a href="https://x.example">link</a></p><img src="https://cdn.example/a.jpg" alt="A">'
|
||||
url = micropub(client, h, name=["T"], content=[{"html": html, "text": "Intro link"}])
|
||||
(content,) = source(client, h, url)["properties"]["content"]
|
||||
assert "html" in content and 'href="https://x.example"' in content["html"]
|
||||
assert "<img" in content["html"]
|
||||
|
||||
def test_remote_images_are_copied(self, app, client, h, monkeypatch):
|
||||
fetched = self.remote_host(monkeypatch)
|
||||
url = micropub(client, h, name=["T"], content=[
|
||||
"Intro\n\n\n\n\n\n"
|
||||
"Again "])
|
||||
with app.app_context():
|
||||
note = get_note(slug=url.rsplit("/", 1)[1])
|
||||
assert fetched.count("/a.jpg") == 1 # each image once
|
||||
assert "https://cdn.example/a.jpg" not in note.content
|
||||
assert note.content.count(f"]({SITE}/media/") == 2
|
||||
assert "https://cdn.example/gone.jpg" in note.content # couldn't fetch: left as is
|
||||
assert client.get(note.content.split(f"]({SITE}")[1].split(")")[0]).status_code == 200
|
||||
|
||||
def test_html_images_copied_and_rendered_cleanly(self, app, client, h, monkeypatch):
|
||||
self.remote_host(monkeypatch)
|
||||
url = micropub(client, h, name=["T"], content=[{"html":
|
||||
'<p>Intro</p><img src="https://cdn.example/a.jpg" alt="A"><p>More</p><script>x()</script>'}])
|
||||
with app.app_context():
|
||||
note = get_note(slug=url.rsplit("/", 1)[1])
|
||||
assert note.content_format == "html"
|
||||
assert note.html.startswith("<p>Intro</p><img") and f'src="{SITE}/media/' in note.html
|
||||
assert "<br" not in note.html and "<p></p>" not in note.html and "script" not in note.html
|
||||
|
||||
def test_own_images_not_fetched(self, app, client, h, monkeypatch):
|
||||
fetched = self.remote_host(monkeypatch)
|
||||
micropub(client, h, content=[f""])
|
||||
assert fetched == []
|
||||
|
||||
def test_update_copies_new_images(self, app, client, h, monkeypatch):
|
||||
self.remote_host(monkeypatch)
|
||||
url = micropub(client, h, name=["T"], content=["Before"])
|
||||
client.post("/micropub", json={"action": "update", "url": url,
|
||||
"replace": {"content": [""]}}, headers=h)
|
||||
with app.app_context():
|
||||
assert f"]({SITE}/media/" in get_note(slug=url.rsplit("/", 1)[1]).content
|
||||
Reference in new issue
Block a user