Render Markdown release notes safely in the updater

This commit is contained in:
parkel committed 2026-11-23 20:13:29 +01:00
1 parent d691e20b16
commit 74b1ccec2a
6 files changed
+101 -3

No files matched your search

@@ -1,4 +1,5 @@
{% extends "hosting/base.html" %}
{% load release_notes %}
{% block title %}Updates · Coulobre Panel{% endblock %}
{% block content %}
<header class="page-heading">
@@ -44,11 +45,13 @@
<section class="surface" aria-labelledby="available-updates">
<header class="section-heading"><h2 id="available-updates">Compatible releases</h2></header>
{% for release in releases %}
<details class="section-text">
<details class="section-text update-release">
<summary>{{ release.version }}{% if release.prerelease %} · Prerelease{% endif %}</summary>
{{ release.notes|linebreaks }}
{% if release.notes %}
<div class="release-notes">{{ release.notes|release_notes_html }}</div>
{% endif %}
{% if may_apply and release.version in applyable_versions and update_status.state != "running" and update_status.state != "recovery_required" %}
<form method="post" class="editor">
<form method="post" class="editor update-release-form">
{% csrf_token %}
<input type="hidden" name="version" value="{{ release.version }}">
<input type="hidden" name="sha256" value="{{ release.sha256 }}">
Whitespace-only changes.
@@ -0,0 +1,29 @@
from urllib.parse import urlsplit
from django import template
from django.utils.safestring import mark_safe
from markdown_it import MarkdownIt
register = template.Library()
class ReleaseNotesMarkdown(MarkdownIt):
def validateLink(self, url): # noqa: N802 - markdown-it-py hook
try:
link = urlsplit(url)
return (
link.scheme in {"https", "http"}
and bool(link.hostname)
and link.username is None
and link.password is None
)
except ValueError:
return False
markdown = ReleaseNotesMarkdown("js-default").disable("image")
@register.filter
def release_notes_html(notes):
return mark_safe(markdown.render(notes))
+56
View File
@@ -0,0 +1,56 @@
from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.core.cache import cache
from django.test import TestCase
from django.urls import reverse
from coulobre.release_updates import Release
class UpdateViewTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.admin = get_user_model().objects.create_superuser("operator")
def test_catalog_renders_markdown_and_escapes_untrusted_content(self):
checksum = "a" * 64
notes = f"""## Changes
- **Upgrade** from the previous release.
- Follow the [deployment guide](https://example.test/docs#upgrade).
```text
{checksum} coulobre_panel-0.11.1rc1.tar.gz
```
<script>Should stay visible as text.</script>
[Unsafe](javascript&#58;invalid) [Local action](/updates/)
![Remote image](https://example.test/image.png)
"""
release = Release("0.11.1rc1", notes, True, checksum)
self.client.force_login(self.admin)
cache.clear()
self.addCleanup(cache.clear)
path = "coulobre.hosting.update_views"
with (
patch(f"{path}.SOCKET", **{"is_socket.return_value": True}),
patch(f"{path}.inspect_linked_nodes", return_value={"nodes": []}),
patch(f"{path}.available_releases", return_value=[release]),
):
response = self.client.get(reverse("panel:updates"))
self.assertContains(response, "<h2>Changes</h2>", html=True)
self.assertContains(response, "<strong>Upgrade</strong>", html=True)
self.assertContains(response, '<pre><code class="language-text">')
self.assertContains(response, f"{checksum} coulobre_panel-")
self.assertContains(
response, 'href="https://example.test/docs#upgrade"'
)
self.assertContains(response, "&lt;script&gt;")
self.assertNotContains(response, "<script>Should stay")
self.assertNotContains(response, 'href="javascript:')
self.assertNotContains(response, 'href="/updates/">Local action')
self.assertNotContains(response, '<img src="https://example.test/')