Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Mar 17, 2020. It is now read-only.

Commit 56244a8

Browse filesBrowse files
author
Ace Nassri
authored
Fix XSS issue (GoogleCloudPlatform#1809)
1 parent f320b90 commit 56244a8
Copy full SHA for 56244a8

File tree

Expand file treeCollapse file tree

2 files changed

+21
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+21
-2
lines changed

‎functions/helloworld/main.py

Copy file name to clipboardExpand all lines: functions/helloworld/main.py
+9-2Lines 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

Copy file name to clipboardExpand all lines: functions/helloworld/main_test.py
+12Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.