Skip to content

Commit 4c27334

Browse files
committed
Webserver: Further Sanitize values passed to origin param (apache#12459)
Follow-up of apache#10334 (cherry picked from commit 9818932)
1 parent e87277d commit 4c27334

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

airflow/www/views.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
from pygments import highlight, lexers
5656
import six
5757
from pygments.formatters.html import HtmlFormatter
58-
from six.moves.urllib.parse import quote, unquote, urlparse
58+
from six.moves.urllib.parse import parse_qsl, quote, unquote, urlencode, urlparse
5959

6060
from sqlalchemy import or_, desc, and_, union_all
6161
from wtforms import (
@@ -335,7 +335,13 @@ def get_safe_url(url):
335335
valid_schemes = ['http', 'https', '']
336336
valid_netlocs = [request.host, '']
337337

338+
if not url:
339+
return "/admin/"
340+
338341
parsed = urlparse(url)
342+
343+
query = parse_qsl(parsed.query, keep_blank_values=True)
344+
url = parsed._replace(query=urlencode(query)).geturl()
339345
if parsed.scheme in valid_schemes and parsed.netloc in valid_netlocs:
340346
return url
341347
except Exception as e: # pylint: disable=broad-except

airflow/www_rbac/views.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
import pkg_resources
3434
import six
35-
from six.moves.urllib.parse import quote, urlparse
35+
from six.moves.urllib.parse import parse_qsl, quote, urlencode, urlparse
3636

3737
import pendulum
3838
import sqlalchemy as sqla
@@ -96,7 +96,13 @@ def get_safe_url(url):
9696
valid_schemes = ['http', 'https', '']
9797
valid_netlocs = [request.host, '']
9898

99+
if not url:
100+
return url_for('Airflow.index')
101+
99102
parsed = urlparse(url)
103+
104+
query = parse_qsl(parsed.query, keep_blank_values=True)
105+
url = parsed._replace(query=urlencode(query)).geturl()
100106
if parsed.scheme in valid_schemes and parsed.netloc in valid_netlocs:
101107
return url
102108
except Exception as e: # pylint: disable=broad-except

tests/www/test_views.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,11 @@ def test_trigger_serialized_dag(self, mock_os_isfile, mock_dagrun):
11191119
@parameterized.expand([
11201120
("javascript:alert(1)", "/admin/"),
11211121
("http://google.com", "/admin/"),
1122+
(
1123+
"%2Fadmin%2Fairflow%2Ftree%3Fdag_id%3Dexample_bash_operator"
1124+
"&dag_id=example_bash_operator';alert(33)//",
1125+
"/admin/airflow/tree?dag_id=example_bash_operator"
1126+
),
11221127
(
11231128
"%2Fadmin%2Fairflow%2Ftree%3Fdag_id%3Dexample_bash_operator&dag_id=example_bash_operator",
11241129
"/admin/airflow/tree?dag_id=example_bash_operator"
@@ -1127,7 +1132,6 @@ def test_trigger_serialized_dag(self, mock_os_isfile, mock_dagrun):
11271132
"%2Fadmin%2Fairflow%2Fgraph%3Fdag_id%3Dexample_bash_operator&dag_id=example_bash_operator",
11281133
"/admin/airflow/graph?dag_id=example_bash_operator"
11291134
),
1130-
("", ""),
11311135
])
11321136
def test_trigger_dag_form_origin_url(self, test_origin, expected_origin):
11331137
test_dag_id = "example_bash_operator"

tests/www_rbac/test_views.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2247,9 +2247,12 @@ def test_trigger_serialized_dag(self, mock_os_isfile, mock_dagrun):
22472247
@parameterized.expand([
22482248
("javascript:alert(1)", "/home"),
22492249
("http://google.com", "/home"),
2250+
(
2251+
"%2Ftree%3Fdag_id%3Dexample_bash_operator';alert(33)//",
2252+
"/tree?dag_id=example_bash_operator%27&alert%2833%29%2F%2F=",
2253+
),
22502254
("%2Ftree%3Fdag_id%3Dexample_bash_operator", "/tree?dag_id=example_bash_operator"),
22512255
("%2Fgraph%3Fdag_id%3Dexample_bash_operator", "/graph?dag_id=example_bash_operator"),
2252-
("", ""),
22532256
])
22542257
def test_trigger_dag_form_origin_url(self, test_origin, expected_origin):
22552258
test_dag_id = "example_bash_operator"

0 commit comments

Comments
 (0)