Skip to content

Commit 94187c3

Browse files
Allow links in sanitation
Import and use GIT_REPO_URL in the nihongo module to render a clickable GitHub link for the Indev section. Revamp the sanitise filter: allow <a> tags with href/target/title, introduce ALLOWED_TAGS/ALLOWED_ATTRIBUTES, add re import, and post-process anchors to inject rel="noopener noreferrer" (case-insensitive). Also register the sanitise function as a Jinja2 template filter.
1 parent 2b6ef42 commit 94187c3

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

lingual/modules/nihongo/routes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import re
33
from flask import Blueprint, abort, current_app, flash, jsonify, redirect, render_template, request, session, url_for
44
from flask_login import current_user, login_required
5-
from lingual import db
5+
from lingual import db, GIT_REPO_URL
66
from lingual.modules.nihongo.utils.kanji_processor import Kanji
77
from lingual.modules.nihongo.utils import quiz_utils
88
from lingual.modules.nihongo.utils.grammar_lesson_processor import get_processor
@@ -87,7 +87,7 @@ def home():
8787
indev = HomeSection("Indev")
8888
indev.add_items(
8989
ItemParagraph(
90-
"These items are currently under development. If you encounter any issues, please make an issue on our GitHub Page!"
90+
f"These items are currently under development. If you encounter any issues, please make an issue on our <a href='{GIT_REPO_URL}' target='_blank'>GitHub Page</a>!"
9191
),
9292
ItemBox(
9393
title="Particles",

lingual/utils/filters.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,31 @@
22
Adds filters for Jinja2 templates for more customisable rendering of content
33
"""
44

5+
import re
56
import bleach
67

8+
ALLOWED_TAGS = ["b", "i", "strong", "em", "p", "br", "a"]
9+
ALLOWED_ATTRIBUTES = {"a": ["href", "target", "title"]}
10+
711
def sanitise(value):
8-
""" Sanitises input to prevent XSS attacks, allowing only basic formatting tags. """
9-
return bleach.clean(
10-
value,
11-
tags=["b", "i", "strong", "em", "p", "br"],
12-
attributes={},
13-
strip=True
12+
"""Sanitise input to prevent XSS attacks, while preserving basic formatting and safe links."""
13+
# Use bleach to clean the input value, allowing only specified tags and attributes, and stripping out any disallowed tags
14+
cleaned = bleach.clean(value, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES, strip=True)
15+
16+
# Regex sub generated by AI
17+
return re.sub(
18+
r'<a\b([^>]*)>',
19+
# If the anchor tag has an href attribute, add rel="noopener noreferrer" to it
20+
lambda match: f'{match.group(0)[:-1]} rel="noopener noreferrer">' if "href=" in match.group(1).lower() else match.group(0),
21+
cleaned,
22+
flags=re.IGNORECASE, # Make the regex case-insensitive to match 'href' in any case (e.g., HREF, Href, etc.)
1423
)
1524

1625
def init_app(app):
26+
# Initialise the sanitise filter for Jinja2 templates
1727
@app.template_filter("sanitise")
1828
def _sanitise_filter(value):
29+
# Sanitise the input value using the sanitise function defined above
1930
return sanitise(value)
2031

2132
return _sanitise_filter

0 commit comments

Comments
 (0)