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

fix: Prevent XSS attacks in the Reports and Failures views #865

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 18 additions & 0 deletions puppetboard/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from flask import Flask
from flask_caching import Cache
from flask_apscheduler import APScheduler
from markupsafe import escape
from pypuppetdb import connect

from puppetboard.utils import (get_or_abort, jsonprint,
Expand Down Expand Up @@ -247,3 +248,20 @@ def to_html(message: str) -> str:
r"(file: \1, line: \2)", message)

return message


def get_error_html(node_name, source, message, show_error_as):

# sanitize the data from PuppetDB first,
# as we will add some HTML tags in the functions below and don't want these
# to be escaped
safe_source = escape(source)
safe_message = escape(message)

# enrich the message using HTML
if show_error_as == 'friendly':
error_html = to_html(get_friendly_error(safe_source, safe_message, node_name))
else:
error_html = get_raw_error(safe_source, safe_message)

return error_html
10 changes: 5 additions & 5 deletions puppetboard/templates/report.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ <h1 id="metrics">Metrics</h1>
}
},
{ data: 'level', name: 'level' },
{ data: 'source', name: 'source', visible: false },
{ data: 'source', name: 'source', visible: false, render: DataTable.render.text() },
{ data: 'tags', name: 'tags', visible: false },
{ data: 'message', name: 'message' },
{ data: 'message', name: 'message' }, // HTML for this column is sanitized in get_message()
// see also the comment about these columns in reports.py
{ data: 'location', name: 'location', visible: false },
{ data: 'short_location', name: 'short_location' },
Expand Down Expand Up @@ -176,10 +176,10 @@ <h1 id="metrics">Metrics</h1>

"data": events,
'columns': [
{ data: 'resource' },
{ data: 'resource', render: DataTable.render.text() },
{ data: 'status' },
{ data: 'old' },
{ data: 'new' },
{ data: 'old', render: DataTable.render.text() },
{ data: 'new', render: DataTable.render.text() },
],

"ordering": false,
Expand Down
11 changes: 4 additions & 7 deletions puppetboard/views/failures.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from flask import Response, stream_with_context, abort
from pypuppetdb.QueryBuilder import AndOperator, EqualsOperator

from puppetboard.core import get_app, get_puppetdb, environments, stream_template, to_html, \
get_friendly_error, get_raw_error
from puppetboard.core import get_app, get_puppetdb, environments, \
stream_template, get_error_html
from puppetboard.utils import check_env, yield_or_stop

app = get_app()
Expand Down Expand Up @@ -55,12 +55,9 @@ def failures(env: str, show_error_as: str):
break

if source and message:
if show_error_as == 'friendly':
error = to_html(get_friendly_error(source, message, node.name))
else:
error = get_raw_error(source, message)
error = get_error_html(node.name, source, message, show_error_as)
else:
error = to_html(f'Node {node.name} is failing but we could not find the errors')
error = f'Node {node.name} is failing but we could not find the errors'

failure = {
'certname': node.name,
Expand Down
12 changes: 3 additions & 9 deletions puppetboard/views/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
EqualsOperator, OrOperator,
LessEqualOperator, RegexOperator, GreaterEqualOperator)

from puppetboard.core import get_app, get_puppetdb, environments, REPORTS_COLUMNS, to_html, \
get_raw_error, get_friendly_error
from puppetboard.core import get_app, get_puppetdb, environments, \
REPORTS_COLUMNS, get_error_html
from puppetboard.utils import (check_env, get_or_abort)

app = get_app()
Expand Down Expand Up @@ -179,12 +179,6 @@ def get_short_location(location: str) -> str:
return location


def get_message(node_name, log, show_error_as):
if show_error_as == 'friendly':
error = to_html(get_friendly_error(log['source'], log['message'], node_name))
else:
error = get_raw_error(log['source'], log['message'])
return error


@app.route('/report/<node_name>/<report_id>',
Expand Down Expand Up @@ -254,7 +248,7 @@ def report(env, node_name, report_id, show_error_as):
'level': log["level"],
'source': log['source'],
'tags': ', '.join(log['tags']),
'message': get_message(node_name, log, show_error_as),
'message': get_error_html(node_name, log['source'], log['message'], show_error_as),
'location': get_location(log),
# this could be also done with a different rendered in DataTables,
# - feel free to refactor it into that if you know how
Expand Down