feat: add Schema.org Article JSON-LD metadata to thread and message views

Issue #466 requests the following:

  • Add a dedicated {% block head_meta %} in base.html to inject JSON-LD metadata into <head>.
  • Include Schema.org Article JSON-LD metadata (headline, UTC ISO 8601 datePublished, and author name) in thread.html and message.html for public lists.
  • Safely serialize JSON-LD to prevent XSS.
  • Add BreadcrumbList JSON-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 DjangoJSONEncoder and escapes <, > and & (\u003C, \u003E, \u0026) to prevent XSS, following Django's built-in json_script implementation.
  • Django's built-in json_script could not be used directly because it outputs type="application/json" instead of type="application/ld+json".

Article metadata builder

(get_article_metadata(email, subject=None) in hyperkitty/lib/view_helpers.py)
  • Omits author when sender_name is empty to avoid emitting invalid Person metadata (and avoids falling back to sender.address to prevent exposing raw email addresses in JSON-LD).
  • When sender_name contains @ (e.g., an email address used as the display name), replaces @ with the Unicode character \uff20 instead of leaving @ in plain text or dropping author, matching HyperKitty's existing feed.py anti-spam obfuscation.
  • Defaults naive datetimes to UTC and normalizes +00:00 to Z so datePublished always 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

  1. (hyperkitty/tests/lib/test_view_helpers.py)
  • test_basic_metadata: Tests happy path.
  • test_subject_override: Tests that an explicit subject overrides emai.subject.
  • test_naive_datetime_defaults_to_utc.
  • test_empty_sender_name_omits_author.
  • test_sender_email_address_is_escaped: Tests that @ in sender_name is 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_page since Dummy Sender and Dummy Subject now 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
    headline in JSON. Only added here since both thread and message views share the same json_ld filter.
  • 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):

image.png

image.png

After (rendered HTML with this MR):

image.png

image.png

Related to #466

Edited by Marco Uriel Román Espinoza

Merge request reports

Loading
Loading