Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown Security Patch #280

Merged
merged 36 commits into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
4b42b8c
some progress
AndrewKe Apr 6, 2020
db9edb4
more stuff
AndrewKe Apr 6, 2020
c4d6bfe
Merge pull request #1 from AndrewKe/master
ochan1 Apr 12, 2020
f612c09
Exam Upload (text labels only)
ochan1 Apr 18, 2020
8b05916
upload working
AndrewKe Apr 19, 2020
9b34afe
Merge pull request #2 from AndrewKe/master
ochan1 Apr 19, 2020
64612d0
Merge pull request #3 from ochan1/master
ochan1 Apr 19, 2020
3fa0202
Search Objects Made
ochan1 Apr 20, 2020
e2e4c96
Merge pull request #4 from ochan1/exams-oscar
ochan1 Apr 26, 2020
8228e0a
hkn web changes
ochan1 Apr 26, 2020
6ac9c4f
Merge pull request #5 from ochan1/exams-oscar
ochan1 Apr 26, 2020
15cfeaa
Pipfile changes
ochan1 Apr 26, 2020
371b3c0
Update Pipfile.lock w/ an Enter
ochan1 Apr 26, 2020
c19f087
Style Edits from Pull Request
ochan1 Jun 10, 2020
91be8d4
Merge branch 'master' of https://github.com/ochan1/hknweb
ochan1 Jun 10, 2020
e1d54ee
Exam Autocomplete edited with commented code
ochan1 Jun 12, 2020
124872d
Exam Autocomplete - cleaned up
ochan1 Jun 12, 2020
64a2c21
Semester search added on Exam Search
ochan1 Jun 12, 2020
e8fa044
Used get instead of filter and get in department Exams
ochan1 Jun 12, 2020
2b52bec
Used get instead of filter and get in department and course Exams
ochan1 Jun 12, 2020
c679f03
Style Check fix
ochan1 Jun 12, 2020
82e6536
HKN Web exam fixes - Hidden Search for Course, Message Duplicate exam…
ochan1 Oct 31, 2020
7648ce7
Removed Testing True for forcing a form fail and added admin permissions
ochan1 Oct 31, 2020
2c5bc2d
Removed Testing True for forcing a form fail and added admin permissi…
ochan1 Oct 31, 2020
a909b45
Removed stray print statement in Exam Views index for departments
ochan1 Nov 1, 2020
81b8208
Merge pull request #6 from compserv/master
ochan1 Nov 13, 2020
b5668ca
remove code for select2
ochan1 Nov 13, 2020
ede50cd
Sending Migration File
ochan1 Nov 22, 2020
5fb0f73
Revert exam index.html
ochan1 Nov 22, 2020
f94e616
Merge branch 'master' of https://github.com/compserv/hknweb
ochan1 Dec 22, 2020
512b4ad
Markdown Security Patch
ochan1 Jan 1, 2021
6cf95c3
Delete 0010_auto_20201112_1923.py
ochan1 Jan 1, 2021
40063cc
Markdown Security Patch Styling
ochan1 Jan 1, 2021
c9f2492
Merge branch 'eventsMarkdownPatch' of https://github.com/ochan1/hknwe…
ochan1 Jan 1, 2021
458d932
Location Events HTML Patch
ochan1 Jan 1, 2021
4cdacb8
Remove Safe tag from Main Calendar in Review Session and Events
ochan1 Jan 2, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hknweb/events/templates/events/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
// Events
events: [{% for event in events %}
{
title: "{{ event.name|safe }}",
title: "{{ event.name }}",
start: '{{ event.start_time|date:"c" }}',
end: '{{ event.end_time|date:"c" }}',
url: '{{ event.id }}',
Expand Down
4 changes: 4 additions & 0 deletions hknweb/events/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from django import forms
from django.core.validators import URLValidator

from hknweb.utils import markdownify


from .constants import (
ATTR,
DAY_ATTRIBUTE_NAME,
Expand Down Expand Up @@ -105,6 +108,7 @@ def get_access_level(user):

def format_url(s: str, max_width: int=None) -> str:
url_validator = URLValidator()
s = markdownify(s)
try:
url_validator(s)
return "<a href='{link}' style='background-color: white'> {link} </a>".format(link=s)
Expand Down
2 changes: 1 addition & 1 deletion hknweb/events/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django.views.generic.edit import UpdateView
from django.utils import timezone

from markdownx.utils import markdownify
from hknweb.utils import markdownify

from hknweb.utils import (
login_and_permission,
Expand Down
2 changes: 1 addition & 1 deletion hknweb/markdown_pages/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponseRedirect
from markdownx.utils import markdownify
from hknweb.utils import markdownify

from .models import MarkdownPage
from .forms import EditPageForm
Expand Down
2 changes: 1 addition & 1 deletion hknweb/reviewsessions/templates/reviewsessions/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
// Review sessions
events: [{% for reviewsession in reviewsessions %}
{
title: "{{ reviewsession.name|safe }}",
title: "{{ reviewsession.name }}",
start: '{{ reviewsession.start_time|date:"c" }}',
end: '{{ reviewsession.end_time|date:"c" }}',
url: '{{ reviewsession.id }}',
Expand Down
42 changes: 41 additions & 1 deletion hknweb/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
# Application definition
Expand Down Expand Up @@ -219,3 +218,44 @@
INTERACTIVITIES_ATTRIBUTE_NAME = "interactivities"

# Note: both candidate and officer group should have permission to add officer challenges

# Markdown settings
MARKDOWNX_MARKDOWNIFY_FUNCTION = 'hknweb.utils.markdownify'
MARKDOWNIFY_STRIP = False

## markdownify
MARKDOWNIFY_WHITELIST_TAGS = [
'a',
'abbr',
'acronym',
'b',
'blockquote',
'em',
'i',
'li',
'ol',
'p',
'strong',
'ul',
'pre',
'code',
'img'
]
MARKDOWNIFY_WHITELIST_PROTOCOLS = [
'http',
'https',
]
MARKDOWNIFY_LINKIFY_PARSE_EMAIL = True
MARKDOWNIFY_LINKIFY_SKIP_TAGS = ['pre', 'code', ]

# Allowing certain Attributes that define the behavior
# of a Tag
# Examples:
# -> <a href=...>, href is allowed here
# -> <img src="..." onload="...">), src is allowed here, but not onload
MARKDOWNIFY_WHITELIST_ATTRS = [
'href',
'src',
'alt',
'class',
]
58 changes: 58 additions & 0 deletions hknweb/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@

from pytz import timezone

### For Markdownx Security Patch
from functools import partial


from django.conf import settings
from django.utils.safestring import mark_safe

import markdown
import bleach
###


# constants

DATETIME_12_HOUR_FORMAT = '%m/%d/%Y %I:%M %p'
Expand Down Expand Up @@ -80,3 +92,49 @@ def export_model_as_csv(model, queryset):
writer.writerow([getattr(obj, field) for field in field_names])

return response

def markdownify(text):

# Bleach settings
whitelist_tags = getattr(settings, 'MARKDOWNIFY_WHITELIST_TAGS', bleach.sanitizer.ALLOWED_TAGS)
whitelist_attrs = getattr(settings, 'MARKDOWNIFY_WHITELIST_ATTRS', bleach.sanitizer.ALLOWED_ATTRIBUTES)
whitelist_styles = getattr(settings, 'MARKDOWNIFY_WHITELIST_STYLES', bleach.sanitizer.ALLOWED_STYLES)
whitelist_protocols = getattr(settings, 'MARKDOWNIFY_WHITELIST_PROTOCOLS', bleach.sanitizer.ALLOWED_PROTOCOLS)

# Markdown settings
strip = getattr(settings, 'MARKDOWNIFY_STRIP', True)
extensions = getattr(settings, 'MARKDOWNIFY_MARKDOWN_EXTENSIONS', [])

# Bleach Linkify
linkify = None
linkify_text = getattr(settings, 'MARKDOWNIFY_LINKIFY_TEXT', True)

if linkify_text:
linkify_parse_email = getattr(settings, 'MARKDOWNIFY_LINKIFY_PARSE_EMAIL', False)
linkify_callbacks = getattr(settings, 'MARKDOWNIFY_LINKIFY_CALLBACKS', None)
linkify_skip_tags = getattr(settings, 'MARKDOWNIFY_LINKIFY_SKIP_TAGS', None)
linkifyfilter = bleach.linkifier.LinkifyFilter

linkify = [partial(linkifyfilter,
callbacks=linkify_callbacks,
skip_tags=linkify_skip_tags,
parse_email=linkify_parse_email
)]

# Convert markdown to html
html = markdown.markdown(text, extensions=extensions)

# Sanitize html if wanted
if getattr(settings, 'MARKDOWNIFY_BLEACH', True):

cleaner = bleach.Cleaner(tags=whitelist_tags,
attributes=whitelist_attrs,
styles=whitelist_styles,
protocols=whitelist_protocols,
strip=strip,
filters=linkify,
)

html = cleaner.clean(html)

return mark_safe(html)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mysqlclient==1.4.5
whitenoise==4.1.1
pillow==6.2.0
django-markdownx==2.0.27
bleach==3.2.1
gunicorn==19.9.0
fabric==2.4.0
social-auth-app-django==3.1.0
Expand Down