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 14
14
15
15
import sys
16
16
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
+
17
24
18
25
# [START functions_tips_terminate]
19
26
# [START functions_helloworld_get]
@@ -61,7 +68,7 @@ def hello_http(request):
61
68
"""
62
69
request_json = request .get_json ()
63
70
if request_json and 'name' in request_json :
64
- name = request_json ['name' ]
71
+ name = escape ( request_json ['name' ])
65
72
else :
66
73
name = 'World'
67
74
return 'Hello, {}!' .format (name )
@@ -121,7 +128,7 @@ def hello_content(request):
121
128
name = request .form .get ('name' )
122
129
else :
123
130
raise ValueError ("Unknown content type: {}" .format (content_type ))
124
- return 'Hello, {}!' .format (name )
131
+ return 'Hello, {}!' .format (escape ( name ) )
125
132
# [END functions_http_content]
126
133
127
134
Original file line number Diff line number Diff line change @@ -42,6 +42,12 @@ def test_hello_http_args(app):
42
42
assert 'Hello, test!' in res
43
43
44
44
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
+
45
51
def test_hello_content_json (app ):
46
52
with app .test_request_context (json = {'name' : 'test' }):
47
53
res = main .hello_content (flask .request )
@@ -56,6 +62,12 @@ def test_hello_content_urlencoded(app):
56
62
assert 'Hello, test!' in res
57
63
58
64
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
+
59
71
def test_hello_method (app ):
60
72
with app .test_request_context (method = 'GET' ):
61
73
res = main .hello_method (flask .request )
You can’t perform that action at this time.
0 commit comments