Loading
feat: add Schema.org Article JSON-LD metadata to thread and message views
Issue #466 requests the following:
- Add a dedicated
{% block head_meta %}inbase.htmlto inject JSON-LD metadata into<head>. - Include Schema.org
ArticleJSON-LD metadata (headline, UTC ISO 8601datePublished, andauthorname) inthread.htmlandmessage.htmlfor public lists. - Safely serialize JSON-LD to prevent XSS.
- Add
BreadcrumbListJSON-LD metadata to show the mailing list title in search results. - Include message text (
articleBody/description) and nested comment replies in thread JSON-LD.
Note: Since this is my first feature MR, I split the issue into smaller parts to ensure a good implementation. This MR covers the first three items, and I plan to work on the remaining requirements in follow-up MRs.
Note for the reviewer: I included the sections below because I needed them as a final review of my code (design rationale and test breakdown). Feel free to skip straight to "Verification" and the diff, and please let me know if you'd prefer a more concise format for my upcoming MRs.
Extra details
XSS-safe JSON-LD serialization
(json_ld(value) in hyperkitty/templatetags/hk_generic.py)
- Serializes metadata using
DjangoJSONEncoderand escapes<,>and&(\u003C,\u003E,\u0026) to prevent XSS, following Django's built-injson_scriptimplementation. - Django's built-in
json_scriptcould not be used directly because it outputstype="application/json"instead oftype="application/ld+json".
Article metadata builder
(get_article_metadata(email, subject=None) in hyperkitty/lib/view_helpers.py)
- Omits
authorwhensender_nameis empty to avoid emitting invalidPersonmetadata (and avoids falling back tosender.addressto prevent exposing raw email addresses in JSON-LD). - When
sender_namecontains@(e.g., an email address used as the display name), replaces@with the Unicode character\uff20instead of leaving@in plain text or droppingauthor, matching HyperKitty's existingfeed.pyanti-spam obfuscation. - Defaults naive datetimes to UTC and normalizes
+00:00toZsodatePublishedalways includes an ISO 8601 timezone.
Public thread and message views
(hyperkitty/views/message.py and hyperkitty/views/thread.py)
- Limited to public lists (
not mlist.is_private), as requested.
New tests
- (
hyperkitty/tests/lib/test_view_helpers.py)
test_basic_metadata: Tests happy path.test_subject_override: Tests that an explicitsubjectoverridesemai.subject.test_naive_datetime_defaults_to_utc.test_empty_sender_name_omits_author.test_sender_email_address_is_escaped: Tests that@insender_nameis replaced with\uff20.
2. (hyperkitty/tests/test_templatetags.py)
test_basic_serialization: Tests that a dict is serialized into a<script type="application/ld+json">tag with valid JSON.test_xss_escaping: Tests that<,>, and&are escaped as Unicode sequences without altering the decoded JSON data.
3. (hyperkitty/tests/views/test_message.py)
test_json_ld_metadata: Tests that the message view renders the expected Article JSON-LD<script>in<head>for public lists.test_json_ld_private_list: Tests that no JSON-LD<script>is emitted for private lists, even for authorized users.- Also updated expected string counts in
test_message_pagesinceDummy SenderandDummy Subjectnow also appear inside the JSON-LD<script>.
4. (hyperkitty/tests/views/test_thread.py)
test_json_ld_metadata: Tests that the thread view renders the Article JSON-LD<script>in<head>.test_json_ld_xss_escaping: Verifies that unescaped</script><script>payloads do not appear in the rendered HTML while preserving the original
headlinein JSON. Only added here since boththreadandmessageviews share the samejson_ldfilter.test_json_ld_private_list: Tests that no JSON-LD<script>is emitted for private lists on the thread view.
Verification
- All tests passed (local checks and GitLab CI/CD pipeline).
- Validated rendered HTML in Google Rich Results Test and Schema.org validator.
Before (live thread in production):
After (rendered HTML with this MR):
Related to #466
Edited by Marco Uriel Román Espinoza



