Skip to content

Commit 56244a8

Browse files
author
Ace Nassri
authored
Fix XSS issue (#1809)
1 parent f320b90 commit 56244a8

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

functions/helloworld/main.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414

1515
import sys
1616

17+
# [START functions_helloworld_http]
18+
# [START functions_http_content]
19+
from flask import escape
20+
21+
# [END functions_helloworld_http]
22+
# [END functions_http_content]
23+
1724

1825
# [START functions_tips_terminate]
1926
# [START functions_helloworld_get]
@@ -61,7 +68,7 @@ def hello_http(request):
6168
"""
6269
request_json = request.get_json()
6370
if request_json and 'name' in request_json:
64-
name = request_json['name']
71+
name = escape(request_json['name'])
6572
else:
6673
name = 'World'
6774
return 'Hello, {}!'.format(name)
@@ -121,7 +128,7 @@ def hello_content(request):
121128
name = request.form.get('name')
122129
else:
123130
raise ValueError("Unknown content type: {}".format(content_type))
124-
return 'Hello, {}!'.format(name)
131+
return 'Hello, {}!'.format(escape(name))
125132
# [END functions_http_content]
126133

127134

functions/helloworld/main_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ def test_hello_http_args(app):
4242
assert 'Hello, test!' in res
4343

4444

45+
def test_hello_http_xss(app):
46+
with app.test_request_context(json={'name': '<script>alert(1)</script>'}):
47+
res = main.hello_http(flask.request)
48+
assert '<script>' not in res
49+
50+
4551
def test_hello_content_json(app):
4652
with app.test_request_context(json={'name': 'test'}):
4753
res = main.hello_content(flask.request)
@@ -56,6 +62,12 @@ def test_hello_content_urlencoded(app):
5662
assert 'Hello, test!' in res
5763

5864

65+
def test_hello_content_xss(app):
66+
with app.test_request_context(json={'name': '<script>alert(1)</script>'}):
67+
res = main.hello_content(flask.request)
68+
assert '<script>' not in res
69+
70+
5971
def test_hello_method(app):
6072
with app.test_request_context(method='GET'):
6173
res = main.hello_method(flask.request)

0 commit comments

Comments
 (0)