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

+9
View File
@@ -608,6 +608,15 @@ in-place procedure for custom service unit changes and for environments
missing a dependency required by the target version. From 0.11 onward, the missing a dependency required by the target version. From 0.11 onward, the
updater checks installed dependencies before stopping panel services. updater checks installed dependencies before stopping panel services.
Markdown release notes require `markdown-it-py` in the panel's virtual
environment. To use the web updater for the first release with this feature,
install the dependency before starting the update. The in-place procedure
installs it with the panel package.
```sh
sudo /opt/coulobre/bin/python -m pip install 'markdown-it-py>=4.3,<5'
```
The panel and its workers pause during the upgrade. Refresh **Updates** after The panel and its workers pause during the upgrade. Refresh **Updates** after
the panel returns. The latest update status names the recovery directory in the panel returns. The latest update status names the recovery directory in
`/var/lib/coulobre-update/`. The root-only directory holds the previous `/var/lib/coulobre-update/`. The root-only directory holds the previous
+1
View File
@@ -16,6 +16,7 @@ dependencies = [
"cryptography>=50", "cryptography>=50",
"dnspython>=2.9.2,<3", "dnspython>=2.9.2,<3",
"idna>=3.15,<4", "idna>=3.15,<4",
"markdown-it-py>=4.3,<5",
"qrcode>=8.2,<9", "qrcode>=8.2,<9",
] ]
@@ -1,4 +1,5 @@
{% extends "hosting/base.html" %} {% extends "hosting/base.html" %}
{% load release_notes %}
{% block title %}Updates · Coulobre Panel{% endblock %} {% block title %}Updates · Coulobre Panel{% endblock %}
{% block content %} {% block content %}
<header class="page-heading"> <header class="page-heading">
@@ -44,11 +45,13 @@
<section class="surface" aria-labelledby="available-updates"> <section class="surface" aria-labelledby="available-updates">
<header class="section-heading"><h2 id="available-updates">Compatible releases</h2></header> <header class="section-heading"><h2 id="available-updates">Compatible releases</h2></header>
{% for release in releases %} {% for release in releases %}
<details class="section-text"> <details class="section-text update-release">
<summary>{{ release.version }}{% if release.prerelease %} · Prerelease{% endif %}</summary> <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" %} {% 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 %} {% csrf_token %}
<input type="hidden" name="version" value="{{ release.version }}"> <input type="hidden" name="version" value="{{ release.version }}">
<input type="hidden" name="sha256" value="{{ release.sha256 }}"> <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/')