mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Merge branch 'develop' into feature
This commit is contained in:
@@ -3,6 +3,7 @@ import json
|
||||
from django import forms
|
||||
from django.db.models import Count
|
||||
from django.forms.fields import JSONField as _JSONField, InvalidJSONInput
|
||||
from django.templatetags.static import static
|
||||
from netaddr import AddrFormatError, EUI
|
||||
|
||||
from utilities.forms import widgets
|
||||
@@ -26,10 +27,9 @@ class CommentField(forms.CharField):
|
||||
A textarea with support for Markdown rendering. Exists mostly just to add a standard `help_text`.
|
||||
"""
|
||||
widget = forms.Textarea
|
||||
# TODO: Port Markdown cheat sheet to internal documentation
|
||||
help_text = """
|
||||
help_text = f"""
|
||||
<i class="mdi mdi-information-outline"></i>
|
||||
<a href="https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet" target="_blank" tabindex="-1">
|
||||
<a href="{static('docs/reference/markdown/')}" target="_blank" tabindex="-1">
|
||||
Markdown</a> syntax is supported
|
||||
"""
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ from markdown import markdown
|
||||
|
||||
from netbox.config import get_config
|
||||
from utilities.markdown import StrikethroughExtension
|
||||
from utilities.utils import foreground_color
|
||||
from utilities.utils import clean_html, foreground_color
|
||||
|
||||
register = template.Library()
|
||||
|
||||
@@ -144,18 +144,6 @@ def render_markdown(value):
|
||||
|
||||
{{ md_source_text|markdown }}
|
||||
"""
|
||||
schemes = '|'.join(get_config().ALLOWED_URL_SCHEMES)
|
||||
|
||||
# Strip HTML tags
|
||||
value = strip_tags(value)
|
||||
|
||||
# Sanitize Markdown links
|
||||
pattern = fr'\[([^\]]+)\]\(\s*(?!({schemes})).*:(.+)\)'
|
||||
value = re.sub(pattern, '[\\1](\\3)', value, flags=re.IGNORECASE)
|
||||
|
||||
# Sanitize Markdown reference links
|
||||
pattern = fr'\[([^\]]+)\]:\s*(?!({schemes}))\w*:(.+)'
|
||||
value = re.sub(pattern, '[\\1]: \\3', value, flags=re.IGNORECASE)
|
||||
|
||||
# Render Markdown
|
||||
html = markdown(value, extensions=['def_list', 'fenced_code', 'tables', StrikethroughExtension()])
|
||||
@@ -164,6 +152,11 @@ def render_markdown(value):
|
||||
if html:
|
||||
html = f'<div class="rendered-markdown">{html}</div>'
|
||||
|
||||
schemes = get_config().ALLOWED_URL_SCHEMES
|
||||
|
||||
# Sanitize HTML
|
||||
html = clean_html(html, schemes)
|
||||
|
||||
return mark_safe(html)
|
||||
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ from collections import OrderedDict
|
||||
from decimal import Decimal
|
||||
from itertools import count, groupby
|
||||
|
||||
import bleach
|
||||
from django.core.serializers import serialize
|
||||
from django.db.models import Count, OuterRef, Subquery
|
||||
from django.db.models.functions import Coalesce
|
||||
@@ -14,6 +15,7 @@ from mptt.models import MPTTModel
|
||||
from dcim.choices import CableLengthUnitChoices
|
||||
from extras.plugins import PluginConfig
|
||||
from extras.utils import is_taggable
|
||||
from netbox.config import get_config
|
||||
from utilities.constants import HTTP_REQUEST_META_SAFE_COPY
|
||||
|
||||
|
||||
@@ -257,7 +259,9 @@ def render_jinja2(template_code, context):
|
||||
"""
|
||||
Render a Jinja2 template with the provided context. Return the rendered content.
|
||||
"""
|
||||
return SandboxedEnvironment().from_string(source=template_code).render(**context)
|
||||
environment = SandboxedEnvironment()
|
||||
environment.filters.update(get_config().JINJA2_FILTERS)
|
||||
return environment.from_string(source=template_code).render(**context)
|
||||
|
||||
|
||||
def prepare_cloned_fields(instance):
|
||||
@@ -382,3 +386,33 @@ def copy_safe_request(request):
|
||||
'path': request.path,
|
||||
'id': getattr(request, 'id', None), # UUID assigned by middleware
|
||||
})
|
||||
|
||||
|
||||
def clean_html(html, schemes):
|
||||
"""
|
||||
Sanitizes HTML based on a whitelist of allowed tags and attributes.
|
||||
Also takes a list of allowed URI schemes.
|
||||
"""
|
||||
|
||||
ALLOWED_TAGS = [
|
||||
"div", "pre", "code", "blockquote", "del",
|
||||
"hr", "h1", "h2", "h3", "h4", "h5", "h6",
|
||||
"ul", "ol", "li", "p", "br",
|
||||
"strong", "em", "a", "b", "i", "img",
|
||||
"table", "thead", "tbody", "tr", "th", "td",
|
||||
"dl", "dt", "dd",
|
||||
]
|
||||
|
||||
ALLOWED_ATTRIBUTES = {
|
||||
"div": ['class'],
|
||||
"h1": ["id"], "h2": ["id"], "h3": ["id"], "h4": ["id"], "h5": ["id"], "h6": ["id"],
|
||||
"a": ["href", "title"],
|
||||
"img": ["src", "title", "alt"],
|
||||
}
|
||||
|
||||
return bleach.clean(
|
||||
html,
|
||||
tags=ALLOWED_TAGS,
|
||||
attributes=ALLOWED_ATTRIBUTES,
|
||||
protocols=schemes
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user