This repository was archived by the owner on Mar 17, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +21
-2
lines changed
Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Original file line number Diff line number Diff line change 1414
1515import 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
Original file line number Diff line number Diff 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+
4551def 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+
5971def test_hello_method (app ):
6072 with app .test_request_context (method = 'GET' ):
6173 res = main .hello_method (flask .request )
You can’t perform that action at this time.
0 commit comments