Skip to content

Commit

Permalink
views: sanitize HTML output in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
slint authored and lnielsen committed Jul 15, 2019
1 parent 5c4de67 commit 505da72
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 24 deletions.
38 changes: 38 additions & 0 deletions invenio_communities/config.py
Expand Up @@ -120,3 +120,41 @@
COMMUNITIES_URL_COMMUNITY_VIEW = \
'{protocol}://{host}/communities/{community_id}/'
"""String pattern to generate the URL for the view of a community."""

COMMUNITIES_ALLOWED_TAGS = [
'a',
'abbr',
'acronym',
'b',
'blockquote',
'br',
'code',
'div',
'em',
'h1',
'h2',
'h3',
'h4',
'h5',
'i',
'li',
'ol',
'p',
'pre',
'span',
'strike',
'strong',
'sub',
'sup',
'u',
'ul',
]
"""List of allowed tags used to sanitize HTML output for communities."""

COMMUNITIES_ALLOWED_ATTRS = {
'*': ['class'],
'a': ['href', 'title', 'name', 'class', 'rel'],
'abbr': ['title'],
'acronym': ['title'],
}
"""List of allowed attributes used to sanitize HTML output for communities."""
Expand Up @@ -27,7 +27,7 @@
<div class="container">
<div class="row">
<div class="col-md-8">
{{community.page|safe}}
{{ community.page | sanitize_html | safe }}
</div>
<div class="col-md-4">
<div class="well">{% include "invenio_communities/portalbox_main.html" %}</div>
Expand Down
Expand Up @@ -32,7 +32,7 @@
{%- endif %}
<h4>{{community.title}}</h4>
{%- if community.description %}
{{ community.description|safe }}
{{ community.description | sanitize_html | safe }}
{%- endif %}
{%- if community.page %}
<a href="{{ url_for('invenio_communities.about', community_id=community.id) }}" class="pull-right">
Expand All @@ -45,7 +45,7 @@ <h4>{{community.title}}</h4>
{%- if community.owner.profile and community.owner.profile.username %}
<dt>{{ _('Curated by:') }}</dt><dd>{{ community.owner.profile.username }}</dd>
{%- endif %}
<dt>{{ _('Curation policy:') }}</dt><dd>{{ community.curation_policy|safe|default(_('Not specified'), true) }}</dd>
<dt>{{ _('Curation policy:') }}</dt><dd>{{ community.curation_policy | sanitize_html | safe | default(_('Not specified'), true) }}</dd>
<dt>{{ _('Created:') }}</dt><dd>{{ community.created|dateformat(format='long') }}</dd>
<dt>{{ _('Harvesting API:') }}</dt><dd><a href="{{ community.oaiset_url }}">{{ _('OAI-PMH Interface') }}</a></dd>
</dl>
12 changes: 12 additions & 0 deletions invenio_communities/views/ui.py
Expand Up @@ -29,6 +29,7 @@
import copy
from functools import wraps

import bleach
from flask import Blueprint, abort, current_app, flash, jsonify, redirect, \
render_template, request, url_for
from flask_babelex import gettext as _
Expand All @@ -53,6 +54,17 @@
)


@blueprint.app_template_filter('sanitize_html')
def sanitize_html(value):
"""Sanitizes HTML using the bleach library."""
return bleach.clean(
value,
tags=current_app.config['COMMUNITIES_ALLOWED_TAGS'],
attributes=current_app.config['COMMUNITIES_ALLOWED_ATTRS'],
strip=True,
).strip()


def pass_community(f):
"""Decorator to pass community."""
@wraps(f)
Expand Down
43 changes: 22 additions & 21 deletions setup.py
Expand Up @@ -34,19 +34,19 @@
history = open('CHANGES.rst').read()

tests_require = [
'Flask-CeleryExt>=0.2.2',
'Flask-CeleryExt>=0.3.2',
'SQLAlchemy-Continuum>=1.2.1',
'check-manifest>=0.25',
'coverage>=4.0',
'invenio-mail>=1.0.0a3',
'invenio-oaiserver>=1.0.0a9',
'coverage>=4.5.3',
'invenio-mail>=1.0.2',
'invenio-oaiserver>=1.0.3',
'isort>=4.3.3',
'mock>=1.3.0',
'pydocstyle>=1.0.0',
'pytest-cache>=1.0',
'pytest-cov>=1.8.0',
'pytest-cov>=2.7.1',
'pytest-pep8>=1.0.6',
'pytest>=2.8.0,!=3.3.0',
'pytest>=4.6.4,<5.0.0',
]

extras_require = {
Expand All @@ -60,13 +60,13 @@
'Flask-Mail>=0.9.1',
],
'oai': [
'invenio-oaiserver>=1.0.0a8',
'invenio-oaiserver>=1.0.3',
],
'mysql': [
'invenio-db[mysql]>=1.0.0b3',
'invenio-db[mysql]>=1.0.3',
],
'postgresql': [
'invenio-db[postgresql]>=1.0.0b3',
'invenio-db[postgresql]>=1.0.3',
],
'sqlite': [
'invenio-db>=1.0.0b3',
Expand All @@ -86,20 +86,21 @@
]

install_requires = [
'Flask-BabelEx>=0.9.3',
'Flask>=0.11.1',
'bleach>=2.1.3',
'elasticsearch-dsl>=2.0.0,<3.0.0',
'elasticsearch>=2.0.0,<3.0.0',
'invenio-access>=1.0.0a11',
'invenio-accounts>=1.0.0b1',
'invenio-assets>=1.0.0b2',
'invenio-files-rest>=1.0.0.a14',
'invenio-indexer>=1.0.0a8',
'invenio-pidstore>=1.0.0b1',
'invenio-records>=1.0.0b1',
'invenio-rest[cors]>=1.0.0a9',
'invenio-search>=1.0.0a9',
'marshmallow>=2.15.0',
'Flask-BabelEx>=0.9.3',
'Flask>=0.11.1',
'invenio-access>=1.1.0',
'invenio-accounts>=1.1.0',
'invenio-assets>=1.1.2',
'invenio-files-rest>=1.0.0b1',
'invenio-indexer>=1.0.2',
'invenio-pidstore>=1.0.0',
'invenio-records>=1.2.0',
'invenio-rest[cors]>=1.0.0',
'invenio-search>=1.1.0',
'marshmallow>=2.15.0,<3',
]

packages = find_packages()
Expand Down

0 comments on commit 505da72

Please sign in to comment.