Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

site: blog links only to safe addresses, and addresses with parentheses work

Pixelc9c7821e028c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f126a572..3af986cb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
tables that turn into one card per row on phones. Posts marked `draft: yes` are left out everywhere;
`--drafts` previews them locally. The Blog link in the header and footer appears with the first published
post, so the live site is unchanged until then. `check_site.py` checks blog pages and the feed.
+ Post links may only point to http(s), mailto, this site or an in-page anchor; anything else
+ (`javascript:`, `data:`, `//host`, …) stays plain text. Links whose address has parentheses work.
### Changed
- **kwant.si covers 0.5.** The home page says "New in 0.5" (bots bring receipts, status dots, smarter
diff --git a/scripts/build_site.py b/scripts/build_site.py
index c5957079..5ada1a9a 100644
--- a/scripts/build_site.py
+++ b/scripts/build_site.py
@@ -551,6 +551,11 @@ def slugify(text):
return re.sub(r"[^a-z0-9]+", "-", html.unescape(re.sub(r"<[^>]+>", "", text)).lower()).strip("-")
+# Link targets a post may use: web, mail, this site (root-relative, {{root}}-relative) and in-page anchors.
+# Anything else, javascript:, data:, protocol-relative //host and the rest, stays plain text.
+_SAFE_HREF = re.compile(r"(?:https?://[^\s/]|mailto:[^\s/]|/(?![/\\])|\{\{root\}\}(?![/\\])[^:]*$|#)", re.I)
+
+
def md_inline(text):
"""Inline Markdown to HTML: `code`, [text](url), **bold**, *italic*. Text is escaped; code spans are kept verbatim."""
codes = []
@@ -560,10 +565,12 @@ def md_inline(text):
text = re.sub(r"`([^`]+)`", keep, text)
text = html.escape(text, quote=False)
def link(m):
- href = m[2]
- ext = ' rel="noopener"' if re.match(r"https?://", href) and not href.startswith(SITE_URL) else ""
+ href = html.unescape(m[2])
+ if not _SAFE_HREF.match(href):
+ return m[0]
+ ext = ' rel="noopener"' if re.match(r"https?://", href, re.I) and not href.startswith(SITE_URL) else ""
return f'<a href="{html.escape(href, quote=True)}"{ext}>{m[1]}</a>'
- text = re.sub(r"\[([^\]]+)\]\(([^)\s]+)\)", link, text)
+ text = re.sub(r"\[([^\]]+)\]\(((?:[^()\s]|\([^()\s]*\))+)\)", link, text)
text = re.sub(r"\*\*(.+?)\*\*", r"<strong>\1</strong>", text)
text = re.sub(r"(?<![\w*])\*(?!\s)(.+?)(?<!\s)\*(?![\w*])", r"<em>\1</em>", text)
return re.sub(r"\x00(\d+)\x00", lambda m: codes[int(m[1])], text)
diff --git a/scripts/test_blog.py b/scripts/test_blog.py
index 68d17cac..f6afc89d 100644
--- a/scripts/test_blog.py
+++ b/scripts/test_blog.py
@@ -30,6 +30,40 @@ class Markdown(unittest.TestCase):
def test_text_is_escaped(self):
self.assertIn("&lt;script&gt;", site.md_inline("a <script> tag"))
+ def test_safe_links_render(self):
+ cases = {
+ "https://github.com/x": '<a href="https://github.com/x" rel="noopener">t</a>',
+ "HTTP://example.com/": '<a href="HTTP://example.com/" rel="noopener">t</a>',
+ "https://kwant.si/features": '<a href="https://kwant.si/features">t</a>',
+ "mailto:[email protected]": '<a href="mailto:[email protected]">t</a>',
+ "/docs/install": '<a href="/docs/install">t</a>',
+ "#setup": '<a href="#setup">t</a>',
+ "{{root}}docs/quickstart.html": '<a href="{{root}}docs/quickstart.html">t</a>',
+ "https://a.example/?x=1&y=2": '<a href="https://a.example/?x=1&amp;y=2" rel="noopener">t</a>',
+ }
+ for href, want in cases.items():
+ with self.subTest(href=href):
+ self.assertEqual(site.md_inline(f"[t]({href})"), want)
+
+ def test_unsafe_links_stay_text(self):
+ for href in ("javascript:alert(5)", "JavaScript:alert(1)", "data:text/html,hi", "vbscript:msgbox",
+ "//evil.example/x", "/\\evil.example", "{{root}}javascript:x", "file:///etc/passwd",
+ "docs/install.html", "https:/evil", "&#106;avascript:alert(1)", "java&#x09;script:x"):
+ with self.subTest(href=href):
+ out = site.md_inline(f"[t]({href})")
+ self.assertNotIn("<a", out)
+ self.assertTrue(out.startswith("[t]("), out)
+
+ def test_link_url_with_parentheses(self):
+ self.assertEqual(
+ site.md_inline("see [Foo](https://en.wikipedia.org/wiki/Foo_(bar)) now"),
+ 'see <a href="https://en.wikipedia.org/wiki/Foo_(bar)" rel="noopener">Foo</a> now',
+ )
+ self.assertEqual(
+ site.md_inline("([docs](/docs/install))"),
+ '(<a href="/docs/install">docs</a>)',
+ )
+
def test_h1_is_refused(self):
with self.assertRaises(SystemExit):
site.markdown("# Title")