@@ -34,34 +34,25 @@ def _add_field_value(self, field_name: str, value: Union[str, bytes]) -> None:
34
34
self ._storage [field_name ].append (value )
35
35
36
36
@staticmethod
37
- def _html_output_encode (value ):
37
+ def _encode_html_entities (value ):
38
38
"""Encodes unsafe HTML characters."""
39
39
return (
40
40
str (value )
41
41
.replace ("&" , "&" )
42
42
.replace ("<" , "<" )
43
43
.replace (">" , ">" )
44
44
.replace ('"' , """ )
45
- .replace ("'" , "'" )
46
- )
47
-
48
- @staticmethod
49
- def _debug_warning_nonencoded_output ():
50
- """Warns about XSS risks."""
51
- print (
52
- "WARNING: Setting html_output_encode to False makes XSS vulnerabilities possible by "
53
- "allowing access to raw untrusted values submitted by users. If this data is reflected "
54
- "or shown within HTML without proper encoding it could enable Cross-Site Scripting."
45
+ .replace ("'" , "'" )
55
46
)
56
47
57
48
def get (
58
- self , field_name : str , default : Any = None , html_output_encode = True
49
+ self , field_name : str , default : Any = None , * , safe = True
59
50
) -> Union [str , bytes , None ]:
60
51
"""Get the value of a field."""
61
- if html_output_encode :
62
- return self ._html_output_encode (self ._storage .get (field_name , [default ])[0 ])
52
+ if safe :
53
+ return self ._encode_html_entities (self ._storage .get (field_name , [default ])[0 ])
63
54
64
- self . _debug_warning_nonencoded_output ()
55
+ _debug_warning_nonencoded_output ()
65
56
return self ._storage .get (field_name , [default ])[0 ]
66
57
67
58
def get_list (self , field_name : str ) -> List [Union [str , bytes ]]:
@@ -375,3 +366,12 @@ def _parse_headers(header_bytes: bytes) -> Headers:
375
366
for name , value in [header_line .split (": " , 1 )]
376
367
}
377
368
)
369
+
370
+
371
+ def _debug_warning_nonencoded_output ():
372
+ """Warns about XSS risks."""
373
+ print (
374
+ "WARNING: Setting safe to False makes XSS vulnerabilities possible by "
375
+ "allowing access to raw untrusted values submitted by users. If this data is reflected "
376
+ "or shown within HTML without proper encoding it could enable Cross-Site Scripting."
377
+ )
0 commit comments