forked from retoor/devplacepy
Inline YouTube Music embeds with one shared embed spec
YouTube Music has no player of its own and music.youtube.com sends X-Frame-Options SAMEORIGIN, so music links render YouTube's own player with an Open in YouTube Music caption. Tracks, playlists and OLAK5uy albums embed; browse, channel, search and library stay plain links. - embeds.py is the single URL spec (exact host allowlists, query-order independent, validated ids only); embed-spec.js is generated from it by the embeds-sync CLI command and drift is tested - rendering.py and ContentRenderer.js both follow it; the old duplicate YouTube regexes are removed - max 3 music embeds per content item, overflow stays a plain link - fixes evilyoutube.com matching and watch?feature=share&v=ID links - fixes the invalid unicode CSS escape that printed u2197 after every external link in rendered content - docs, privacy third-party row, parser table tests, server/client parity Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -222,6 +222,32 @@ def test_issue_detail_and_listing_render_title_backend():
|
||||
_gitea_off()
|
||||
|
||||
|
||||
def test_post_content_renders_music_embeds_over_http():
|
||||
owner = _owner()
|
||||
uid = str(uuid4())
|
||||
slug = make_combined_slug("Music Render Check", uid)
|
||||
album = "OLAK5uy_nZZjkBu_E4olFSb5Ey-fQ-4a0ZCqJICdQ"
|
||||
body = (
|
||||
"track https://music.youtube.com/watch?v=4D7u5KF7SP8\n\n"
|
||||
f"album https://music.youtube.com/playlist?list={album}\n\n"
|
||||
"browse https://music.youtube.com/browse/MPREb_abcdefghijk"
|
||||
)
|
||||
get_table("posts").insert(
|
||||
{
|
||||
"deleted_at": None, "deleted_by": None, "uid": uid, "user_uid": owner,
|
||||
"slug": slug, "title": "Music Render Check", "content": body,
|
||||
"topic": "general", "project_uid": None, "image": None, "stars": 0,
|
||||
"created_at": datetime.now(timezone.utc).isoformat(),
|
||||
}
|
||||
)
|
||||
html = _client().get(f"/posts/{slug}").text
|
||||
assert html.count('class="embed-music"') == 2
|
||||
assert "youtube.com/embed/4D7u5KF7SP8" in html
|
||||
assert f"youtube.com/embed/videoseries?list={album}" in html
|
||||
assert 'href="https://music.youtube.com/browse/MPREb_abcdefghijk"' in html
|
||||
assert "Open in YouTube Music" in html
|
||||
|
||||
|
||||
def test_post_content_rendered_backend_with_media():
|
||||
owner = _owner()
|
||||
from uuid import uuid4
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from tests.conftest import BASE_URL
|
||||
from tests.unit.embeds import EMBED_CASES, TRACK, VIDEO
|
||||
|
||||
ROCKET = "\U0001F680"
|
||||
FIRE = "\U0001F525"
|
||||
@@ -44,6 +45,58 @@ def test_content_youtube_embedded_server_side(alice):
|
||||
assert "youtube.com/embed/dQw4w9WgXcQ" in (iframe.get_attribute("src") or "")
|
||||
|
||||
|
||||
def test_content_music_embedded_server_side(alice):
|
||||
page, _ = alice
|
||||
_create_post(page, f"hear https://music.youtube.com/watch?v={TRACK} now", "Music host post")
|
||||
iframe = page.locator(".post-detail-content .embed-music .embed-youtube iframe").first
|
||||
iframe.wait_for(state="attached")
|
||||
assert f"youtube.com/embed/{TRACK}" in (iframe.get_attribute("src") or "")
|
||||
caption = page.locator(".post-detail-content .embed-music-caption").first
|
||||
caption.wait_for(state="visible")
|
||||
assert caption.text_content().strip() == "Open in YouTube Music"
|
||||
|
||||
|
||||
def test_client_embed_parser_matches_server_for_every_case(alice):
|
||||
page, _ = alice
|
||||
page.goto(f"{BASE_URL}/feed", wait_until="domcontentloaded")
|
||||
urls = [case[0] for case in EMBED_CASES]
|
||||
client = page.evaluate(
|
||||
"""async (urls) => {
|
||||
const mod = await import('/static/js/EmbedParser.js');
|
||||
return urls.map((u) => {
|
||||
const e = mod.EmbedParser.parse(u);
|
||||
return e ? [mod.EmbedParser.src(e), e.music] : null;
|
||||
});
|
||||
}""",
|
||||
urls,
|
||||
)
|
||||
for (url, expected, music), got in zip(EMBED_CASES, client):
|
||||
if expected is None:
|
||||
assert got is None, url
|
||||
else:
|
||||
assert got == [expected, music], url
|
||||
|
||||
|
||||
def test_client_renderer_embeds_music_and_caps_it(alice):
|
||||
page, _ = alice
|
||||
page.goto(f"{BASE_URL}/feed", wait_until="domcontentloaded")
|
||||
urls = " ".join(f"https://music.youtube.com/watch?v={VIDEO[:-1]}{c}" for c in "abcde")
|
||||
result = page.evaluate(
|
||||
"""async (text) => {
|
||||
const mod = await import('/static/js/ContentRenderer.js');
|
||||
const box = document.createElement('div');
|
||||
box.innerHTML = mod.contentRenderer.processMedia(text);
|
||||
return {
|
||||
frames: box.querySelectorAll('.embed-music iframe').length,
|
||||
captions: box.querySelectorAll('.embed-music-caption').length,
|
||||
links: box.querySelectorAll('a:not(.embed-music-caption)').length,
|
||||
};
|
||||
}""",
|
||||
urls,
|
||||
)
|
||||
assert result == {"frames": 3, "captions": 3, "links": 2}
|
||||
|
||||
|
||||
def test_content_code_block_has_copy_button(alice):
|
||||
page, _ = alice
|
||||
_create_post(page, "snippet\n\n```python\nprint('hi')\n```", "Code copy host post")
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import itertools
|
||||
import random
|
||||
import re
|
||||
|
||||
from devplacepy.embeds import (
|
||||
EMBED_JS_PATH,
|
||||
MAX_MUSIC_EMBEDS,
|
||||
MAX_START_SECONDS,
|
||||
EmbedParser,
|
||||
)
|
||||
from devplacepy.rendering import render_content
|
||||
|
||||
VIDEO = "dQw4w9WgXcQ"
|
||||
TRACK = "4D7u5KF7SP8"
|
||||
ALBUM = "OLAK5uy_nZZjkBu_E4olFSb5Ey-fQ-4a0ZCqJICdQ"
|
||||
PLAIN = "PLFgquLnL59alCl_2TQvOiD5Vgm1hCaGSI"
|
||||
BASE = "https://www.youtube.com/embed"
|
||||
|
||||
EMBED_CASES = [
|
||||
(f"https://music.youtube.com/watch?v={TRACK}", f"{BASE}/{TRACK}", True),
|
||||
(f"https://music.youtube.com/watch?v={TRACK}&list={ALBUM}", f"{BASE}/{TRACK}?list={ALBUM}", True),
|
||||
(f"https://music.youtube.com/watch?list={ALBUM}&si=abc&v={TRACK}", f"{BASE}/{TRACK}?list={ALBUM}", True),
|
||||
(f"https://music.youtube.com/watch?v=bad&list={ALBUM}", f"{BASE}/videoseries?list={ALBUM}", True),
|
||||
(f"https://music.youtube.com/playlist?list={ALBUM}", f"{BASE}/videoseries?list={ALBUM}", True),
|
||||
(f"https://music.youtube.com/watch?v={TRACK}&list=RDAMVM{TRACK}", f"{BASE}/{TRACK}?list=RDAMVM{TRACK}", True),
|
||||
(f"https://music.youtube.com/watch?v={TRACK}&t=45", f"{BASE}/{TRACK}?start=45", True),
|
||||
(f"https://www.youtube.com/playlist?list={PLAIN}", f"{BASE}/videoseries?list={PLAIN}", False),
|
||||
(f"https://www.youtube.com/watch?v={VIDEO}", f"{BASE}/{VIDEO}", False),
|
||||
(f"https://www.youtube.com/watch?feature=share&v={VIDEO}", f"{BASE}/{VIDEO}", False),
|
||||
(f"https://m.youtube.com/watch?v={VIDEO}&t=30s", f"{BASE}/{VIDEO}?start=30", False),
|
||||
(f"https://youtube.com/embed/{VIDEO}", f"{BASE}/{VIDEO}", False),
|
||||
(f"https://www.youtube-nocookie.com/embed/{VIDEO}", f"{BASE}/{VIDEO}", False),
|
||||
(f"https://www.youtube.com/v/{VIDEO}", f"{BASE}/{VIDEO}", False),
|
||||
(f"https://www.youtube.com/shorts/{VIDEO}", f"{BASE}/{VIDEO}", False),
|
||||
(f"https://youtu.be/{VIDEO}", f"{BASE}/{VIDEO}", False),
|
||||
(f"https://youtu.be/{VIDEO}?t=30", f"{BASE}/{VIDEO}?start=30", False),
|
||||
(f"http://youtu.be/{VIDEO}", f"{BASE}/{VIDEO}", False),
|
||||
(f"https://www.youtube.com/embed/videoseries?list={PLAIN}", f"{BASE}/videoseries?list={PLAIN}", False),
|
||||
(f"https://www.youtube.com/embed?listType=playlist&list={PLAIN}", f"{BASE}/videoseries?list={PLAIN}", False),
|
||||
(f"https://www.youtube.com/watch?v={VIDEO}&list=WL", f"{BASE}/{VIDEO}", False),
|
||||
(f"https://www.youtube.com/watch?v={VIDEO}&list=bad%22%3E%3Cscript%3E", f"{BASE}/{VIDEO}", False),
|
||||
(f"https://www.youtube.com/watch?v={VIDEO}&t={MAX_START_SECONDS + 1}", f"{BASE}/{VIDEO}", False),
|
||||
(f"https://www.youtube.com/watch?v={VIDEO}&t=1m30s", f"{BASE}/{VIDEO}", False),
|
||||
("https://music.youtube.com/browse/MPREb_abcdefghijk", None, False),
|
||||
("https://music.youtube.com/channel/UCabcdefghijklmnopqrstuv", None, False),
|
||||
("https://music.youtube.com/search?q=daft+punk", None, False),
|
||||
("https://music.youtube.com/library", None, False),
|
||||
("https://music.youtube.com/playlist", None, False),
|
||||
(f"https://music.youtube.com/embed/{TRACK}", None, False),
|
||||
(f"https://evilyoutube.com/watch?v={VIDEO}", None, False),
|
||||
(f"https://youtube.com.evil.example/watch?v={VIDEO}", None, False),
|
||||
(f"https://evil.example/https://www.youtube.com/watch?v={VIDEO}", None, False),
|
||||
(f"https://notyoutu.be/{VIDEO}", None, False),
|
||||
(f"https://youtu.be/{VIDEO}/extra", None, False),
|
||||
("https://www.youtube.com/watch?v=short", None, False),
|
||||
("https://www.youtube.com/watch", None, False),
|
||||
("https://www.youtube.com/embed/videoseries", None, False),
|
||||
(f"javascript://youtube.com/watch?v={VIDEO}", None, False),
|
||||
(f"ftp://youtube.com/watch?v={VIDEO}", None, False),
|
||||
("not a url", None, False),
|
||||
("", None, False),
|
||||
]
|
||||
|
||||
|
||||
def _iframe_srcs(rendered: str) -> list[str]:
|
||||
return re.findall(r'<iframe src="([^"]*)"', str(rendered))
|
||||
|
||||
|
||||
def test_parser_cases_table():
|
||||
for url, expected, music in EMBED_CASES:
|
||||
embed = EmbedParser.parse(url)
|
||||
if expected is None:
|
||||
assert embed is None, url
|
||||
continue
|
||||
assert embed is not None, url
|
||||
assert EmbedParser.src(embed) == expected, url
|
||||
assert embed.music is music, url
|
||||
|
||||
|
||||
def test_render_matches_parser_for_every_case():
|
||||
for url, expected, _ in EMBED_CASES:
|
||||
srcs = _iframe_srcs(render_content(url))
|
||||
if expected is None:
|
||||
assert srcs == [], url
|
||||
else:
|
||||
assert srcs == [expected.replace("&", "&")], url
|
||||
|
||||
|
||||
def test_music_embed_markup_and_caption():
|
||||
out = str(render_content(f"https://music.youtube.com/watch?v={TRACK}"))
|
||||
assert 'class="embed-music"' in out
|
||||
assert 'class="embed-youtube"' in out
|
||||
assert 'loading="lazy"' in out
|
||||
assert 'class="embed-music-caption"' in out
|
||||
assert "Open in YouTube Music" in out
|
||||
assert f'href="https://music.youtube.com/watch?v={TRACK}"' in out
|
||||
assert 'rel="noopener noreferrer"' in out
|
||||
|
||||
|
||||
def test_plain_youtube_keeps_old_markup():
|
||||
out = str(render_content(f"https://youtu.be/{VIDEO}"))
|
||||
assert 'class="embed-youtube"' in out
|
||||
assert "embed-music" not in out
|
||||
assert "loading=" not in out
|
||||
|
||||
|
||||
def test_music_embed_cap_turns_overflow_into_links():
|
||||
urls = [f"https://music.youtube.com/watch?v={TRACK[:-1]}{c}" for c in "abcde"]
|
||||
out = str(render_content("\n\n".join(urls)))
|
||||
assert out.count("<iframe") == MAX_MUSIC_EMBEDS
|
||||
assert out.count('class="embed-music"') == MAX_MUSIC_EMBEDS
|
||||
for url in urls[MAX_MUSIC_EMBEDS:]:
|
||||
assert f'<a href="{url}"' in out
|
||||
|
||||
|
||||
def test_music_cap_does_not_limit_plain_youtube():
|
||||
urls = [f"https://youtu.be/{VIDEO[:-1]}{c}" for c in "abcdefg"]
|
||||
out = str(render_content("\n\n".join(urls)))
|
||||
assert out.count("<iframe") == 7
|
||||
|
||||
|
||||
def test_music_cap_is_per_content_item():
|
||||
text = f"https://music.youtube.com/watch?v={TRACK}"
|
||||
for _ in range(3):
|
||||
assert str(render_content(text)).count("<iframe") == 1
|
||||
|
||||
|
||||
def test_urls_inside_code_and_links_are_not_embedded():
|
||||
url = f"https://music.youtube.com/watch?v={TRACK}"
|
||||
assert "<iframe" not in str(render_content(f"`{url}`"))
|
||||
assert "<iframe" not in str(render_content(f"[x]({url})"))
|
||||
assert "<iframe" not in str(render_content(f"```\n{url}\n```"))
|
||||
|
||||
|
||||
def test_trailing_punctuation_does_not_break_music_url():
|
||||
out = str(render_content(f"listen: https://music.youtube.com/watch?v={TRACK}."))
|
||||
assert f'src="{BASE}/{TRACK}"' in out
|
||||
assert out.count("<iframe") == 1
|
||||
|
||||
|
||||
def test_only_validated_characters_reach_the_iframe_src():
|
||||
rng = random.Random(7)
|
||||
alphabet = "abcXYZ019_-\"'<>&%/ ;()=:."
|
||||
for _ in range(2000):
|
||||
junk = "".join(rng.choice(alphabet) for _ in range(rng.randint(1, 40)))
|
||||
for url in (
|
||||
f"https://music.youtube.com/watch?v={junk}&list={junk}",
|
||||
f"https://music.youtube.com/playlist?list={junk}",
|
||||
f"https://youtu.be/{junk}",
|
||||
):
|
||||
embed = EmbedParser.parse(url)
|
||||
if embed is None:
|
||||
continue
|
||||
assert re.fullmatch(r"https://www\.youtube\.com/embed/[A-Za-z0-9_\-/?=&]+", EmbedParser.src(embed)), url
|
||||
|
||||
|
||||
def test_start_parsing_is_bounded_across_the_domain():
|
||||
for seconds, suffix in itertools.product(range(0, MAX_START_SECONDS + 5, 997), ("", "s")):
|
||||
embed = EmbedParser.parse(f"https://youtu.be/{VIDEO}?t={seconds}{suffix}")
|
||||
assert embed is not None
|
||||
assert 0 <= embed.start <= MAX_START_SECONDS
|
||||
assert embed.start in (0, seconds)
|
||||
|
||||
|
||||
def test_generated_js_spec_matches_the_python_spec():
|
||||
expected_path = EMBED_JS_PATH
|
||||
current = expected_path.read_text(encoding="utf-8")
|
||||
scratch = expected_path.with_name("embed-spec.check.js")
|
||||
try:
|
||||
EmbedParser.write_module(scratch)
|
||||
assert scratch.read_text(encoding="utf-8") == current
|
||||
finally:
|
||||
scratch.unlink(missing_ok=True)
|
||||
@@ -1,5 +1,6 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import re
|
||||
from html.parser import HTMLParser
|
||||
|
||||
from devplacepy.rendering import (
|
||||
@@ -321,6 +322,26 @@ def test_xss_legitimate_youtube_embed_survives_audit():
|
||||
assert_no_executable_html(out)
|
||||
|
||||
|
||||
def test_xss_legitimate_music_embed_survives_audit():
|
||||
out = str(render_content("hear https://music.youtube.com/watch?v=4D7u5KF7SP8 now"))
|
||||
assert "youtube.com/embed/4D7u5KF7SP8" in out
|
||||
assert "embed-music-caption" in out
|
||||
assert_no_executable_html(out)
|
||||
|
||||
|
||||
def test_xss_hostile_music_urls_never_reach_an_iframe():
|
||||
hostile = (
|
||||
"https://music.youtube.com/watch?v=4D7u5KF7SP8&list=x\"onload=alert(1)",
|
||||
"https://music.youtube.com/playlist?list=\"><script>alert(1)</script>",
|
||||
"https://music.youtube.com/watch?v=\"onload=alert(1)//",
|
||||
)
|
||||
for url in hostile:
|
||||
out = str(render_content(url))
|
||||
for src in re.findall(r'<iframe src="([^"]*)"', out):
|
||||
assert re.fullmatch(r"https://www\.youtube\.com/embed/[A-Za-z0-9_\-/?=&;a-z]+", src), src
|
||||
assert_no_executable_html(out)
|
||||
|
||||
|
||||
def test_xss_legitimate_image_embed_survives_audit():
|
||||
out = str(render_content("pic https://example.com/a.png end"))
|
||||
assert "<img" in out
|
||||
|
||||
Reference in New Issue
Block a user