New /prompts section extends the existing database (new Prompt model,
same SQLite file) and mirrors the Links experience: add a prompt, get
it auto-categorized and tagged by AI, filter by category, full-text
search, sort, edit, or have the AI re-categorize it. A header nav
switch ("Links" / "Prompts") toggles between the two collections; the
shared topbar/menu markup was factored into _topbar.html and
_topbar_actions.html so both pages (and settings) stay in sync.
Each prompt card has a dedicated copy-to-clipboard icon next to
edit/delete, so a stored prompt can be reused immediately. Copying
uses the raw markdown source (not the rendered HTML) so structure
survives when pasted into another AI tool. The copy handler tries the
async Clipboard API first and falls back to a hidden-textarea +
execCommand('copy') for plain-http/non-secure contexts, with clear
success/failure icon feedback either way.
Prompt content supports Markdown and is rendered server-side
(app/mdrender.py) for the card preview. Since this is user-supplied
HTML-adjacent content, rendering goes through two defenses: the raw
text is escaped (only '<' and '&', not '>', so blockquotes keep
working) before conversion so no raw tag can survive, and the
resulting HTML is passed through bleach with a tag/attribute/protocol
allowlist so Markdown-generated links can't carry a javascript: URL.
Verified against raw <script>, <img onerror>, and javascript: link
payloads. The edit form always shows the raw Markdown source, never
the rendered HTML.
Also bumps the default APP_VERSION (shown in the footer) from 1.0.0
to 2.0.0 to mark this feature addition.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
54 lines
1.8 KiB
Python
54 lines
1.8 KiB
Python
"""Sicheres Markdown-Rendering für Prompt-Inhalte.
|
||
|
||
Nutzer geben rohes Markdown ein (**fett**, Listen, Codeblöcke ...), das in
|
||
der Karten-Vorschau als HTML dargestellt werden soll. Zwei Schutzschichten
|
||
gegen Stored-XSS:
|
||
|
||
1. Der Eingabetext wird vor der Konvertierung HTML-escaped. Echte
|
||
Markdown-Syntax verwendet nur ASCII-Satzzeichen (*, #, -, `, [] ...),
|
||
keine spitzen Klammern – jeder Versuch, rohes HTML/<script>
|
||
einzuschleusen, landet dadurch als reiner Text statt als Markup.
|
||
2. Das von Markdown selbst erzeugte HTML (insbesondere <a href="...">
|
||
aus [text](url)-Links) wird zusätzlich mit bleach bereinigt, da ein
|
||
Link mit z.B. "javascript:"-Schema sonst trotz Schritt 1 möglich wäre.
|
||
"""
|
||
|
||
import bleach
|
||
import markdown as _markdown
|
||
|
||
_converter = _markdown.Markdown(
|
||
extensions=["extra", "sane_lists", "nl2br"], output_format="html"
|
||
)
|
||
|
||
|
||
def _escape_tags(text: str) -> str:
|
||
"""Nur '&' und '<' escapen (nicht '>' – das braucht Markdown für Zitate).
|
||
|
||
Eine HTML-Tag-Injection benötigt immer '<...>'; wird bereits '<' zu
|
||
'<', kann kein Tag mehr entstehen, unabhängig davon, ob ein
|
||
einzelnes '>' im Text steht.
|
||
"""
|
||
return text.replace("&", "&").replace("<", "<")
|
||
|
||
_ALLOWED_TAGS = [
|
||
"p", "br", "hr", "strong", "em", "del", "code", "pre",
|
||
"ul", "ol", "li", "blockquote",
|
||
"h1", "h2", "h3", "h4", "h5", "h6",
|
||
"a", "table", "thead", "tbody", "tr", "th", "td",
|
||
]
|
||
_ALLOWED_ATTRS = {"a": ["href", "title"]}
|
||
_ALLOWED_PROTOCOLS = ["http", "https", "mailto"]
|
||
|
||
|
||
def render(text: str) -> str:
|
||
_converter.reset()
|
||
escaped = _escape_tags(text or "")
|
||
raw_html = _converter.convert(escaped)
|
||
return bleach.clean(
|
||
raw_html,
|
||
tags=_ALLOWED_TAGS,
|
||
attributes=_ALLOWED_ATTRS,
|
||
protocols=_ALLOWED_PROTOCOLS,
|
||
strip=True,
|
||
)
|