Skip to content
Merged
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
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

Version 0.8
-----------

- Reject ``next`` URLs containing linebreaks gracefully

Version 0.7
-----------

Expand Down
9 changes: 8 additions & 1 deletion flask_multipass/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from urllib.parse import urlsplit

from flask import current_app, flash, redirect, render_template, request, session, url_for
from werkzeug.datastructures import ImmutableDict
from werkzeug.datastructures import Headers, ImmutableDict
from werkzeug.exceptions import NotFound

from flask_multipass.auth import AuthProvider
Expand Down Expand Up @@ -145,6 +145,13 @@ def validate_next_url(self, url):
If you override this and want to allow more hosts, make sure to use
a whitelist of trusted hosts to avoid creating an open redirector.
"""
# next_url comes as URL param, so it can have newline chars (as %0a and %0d).
# In redirect response it goes into header (Location).
# Werkzeug doesn't tolerate newline chars in header and raises ValueError.
try:
Headers([('test-header', url)])
except ValueError:
return False
# Browsers treat backslashes like forward slashes, while urllib doesn't.
# Since we just want to validate scheme and netloc here, we normalize
# slashes to those recognized by urllib.
Expand Down
4 changes: 4 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ def test_next_url_invalid():
('//localhost/foo', True),
('http://localhost', True),
('https://localhost/', True),
('\n', False),
('\r', False),
('\n\r', False),
('evil\n', False),
('//evil', False),
('//evil.com', False),
('//evil.com:80', False),
Expand Down