diff --git a/CLAUDE.md b/CLAUDE.md index c05f90ff..2945e5b1 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -105,6 +105,7 @@ devplace containers prune # reap orphan containers + dangling images devplace containers prune-builds # remove legacy per-project images + clear dockerfiles/builds tables (one-time) devplace containers gc-workspaces # remove workspace dirs with no instances devplace emoji-sync # regenerate static/js/emoji-shortcodes.js from the emoji library (run after an emoji dep bump) +devplace embeds-sync # regenerate static/js/embed-spec.js from devplacepy/embeds.py (run after editing the embed table) devplace migrate-data # relocate legacy runtime files into data/ (idempotent; --dry-run to preview) ``` @@ -240,6 +241,8 @@ The CLIENT pipeline (`ContentRenderer.js`, `dp-content`/`dp-title`) is retained **Emoji shortcodes are the full GitHub/Discord `:name:` set** (~4869 names), generated once from the `emoji` library by `rendering.py` `build_emoji_shortcodes()`; the frontend gets the identical map via the generated `static/js/emoji-shortcodes.js` (regenerate with `devplace emoji-sync` after bumping the dependency, never hand-edit). +**YouTube and YouTube Music embeds** have ONE specification, `devplacepy/embeds.py` (`EmbedParser.parse(url)` -> `Embed(kind, video_id, list_id, start, music)`, `EmbedParser.src(embed)`): exact host allowlists (never substring matching), query-order independent parsing, validated ids only (`[A-Za-z0-9_-]{11}` video, `{13,64}` list) so nothing but validated characters can reach an iframe `src`. YouTube Music has no player of its own and `music.youtube.com` sends `X-Frame-Options: SAMEORIGIN`, so a music link is YouTube's player (`www.youtube.com/embed/...`) wrapped in `
`
5. image URL -> `
`
-6. YouTube URL -> trusted iframe
+6. YouTube / YouTube Music URL -> trusted iframe (`EmbedParser.js`, constants from the generated `embed-spec.js`; music adds an "Open in YouTube Music" caption and is capped at 3 per item)
7. autolink remaining URLs
A `NodeIterator` skips `CODE`, `PRE`, `SCRIPT`, and `STYLE` so URLs inside code are never rewritten. Sanitization runs on the raw `marked` output before the trusted YouTube embeds are injected, so user content cannot inject script, event-handler, or iframe payloads while our own embeds survive.
diff --git a/devplacepy/templates/docs/component-dp-content.html b/devplacepy/templates/docs/component-dp-content.html
index 206028d8..21ff5f6c 100644
--- a/devplacepy/templates/docs/component-dp-content.html
+++ b/devplacepy/templates/docs/component-dp-content.html
@@ -2,7 +2,7 @@
# dp-content
Renders user-supplied text as safe rich content: emoji shortcodes, GitHub-flavored markdown,
-`DOMPurify` sanitisation, then media embeds (images, video, YouTube), mentions, and
+`DOMPurify` sanitisation, then media embeds (images, video, YouTube, YouTube Music), mentions, and
autolinks. It exposes the shared `contentRenderer` engine (`static/js/ContentRenderer.js`),
the same pipeline used by the site's `data-render` attribute, as a custom element.
diff --git a/devplacepy/templates/docs/privacy.html b/devplacepy/templates/docs/privacy.html
index f3ac7586..28e3cfdb 100644
--- a/devplacepy/templates/docs/privacy.html
+++ b/devplacepy/templates/docs/privacy.html
@@ -33,6 +33,7 @@ never used to build an advertising profile.
| Recipient | What is sent | When |
|---|---|---|
| {{ ai_provider_name() }} | The text you asked an assistant feature to process | **Only after you grant `ai_third_party` consent.** Default: not granted. |
+| YouTube (Google) | Your browser contacts YouTube directly when a post, comment or message contains a YouTube or YouTube Music link, which DevPlace shows as an inline player. YouTube then sees your IP address and browser details, as for any embedded video. DevPlace itself sends YouTube nothing | Only when you view content that contains such a link |
| Push services (Apple, browser push endpoints) | An encrypted notification payload to a device you registered | Only when you register a device and enable a push channel |
| Telegram | Messages in the Telegram conversation you paired | Only when you pair Telegram yourself |
| Software another member runs in a container | Your DevPlace API key and user id, injected as `DEVPLACE_API_KEY` and `DEVPLACE_USER_UID` | **Only after you grant `container_credentials` consent**, and only when the container is run by someone else. Your own containers use your own credentials with no separate consent. |
diff --git a/events.md b/events.md
index f5218cef..0166d76c 100644
--- a/events.md
+++ b/events.md
@@ -184,6 +184,7 @@ Every state-changing action in DevPlace records one append-only row through `dev
| `cli.devii.task.disable` | `cli/devii.py` |
| `cli.devii.task.prune` | `cli/devii.py` |
| `cli.emoji.sync` | `cli/migrate.py` |
+| `cli.embeds.sync` | `cli/migrate.py` |
| `cli.forks.clear` | `cli/jobs.py` |
| `cli.forks.prune` | `cli/jobs.py` |
| `cli.game.era.end` | `cli/game.py` |
diff --git a/pyproject.toml b/pyproject.toml
index f5aecb6f..f2a8481d 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
[project]
name = "devplacepy"
-version = "1.1.11"
+version = "1.1.12"
description = "DevPlace - The Developer Social Network"
requires-python = ">=3.12"
dependencies = [
diff --git a/tests/api/components/title.py b/tests/api/components/title.py
index d8ad47eb..136f4156 100644
--- a/tests/api/components/title.py
+++ b/tests/api/components/title.py
@@ -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
diff --git a/tests/e2e/components/title.py b/tests/e2e/components/title.py
index 6c8dfa52..3b1cd964 100644
--- a/tests/e2e/components/title.py
+++ b/tests/e2e/components/title.py
@@ -1,6 +1,7 @@
# retoor
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")
diff --git a/tests/unit/embeds.py b/tests/unit/embeds.py
new file mode 100644
index 00000000..bccc71f5
--- /dev/null
+++ b/tests/unit/embeds.py
@@ -0,0 +1,175 @@
+# retoor
+
+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'