@@ -33,9 +33,33 @@ def _add_field_value(self, field_name: str, value: Union[str, bytes]) -> None:
33
33
else :
34
34
self ._storage [field_name ].append (value )
35
35
36
- def get (self , field_name : str , default : Any = None ) -> Union [str , bytes , None ]:
36
+ def _html_output_encode (self , value ):
37
+ return (
38
+ str (value )
39
+ .replace ("&" , "&" )
40
+ .replace ("<" , "<" )
41
+ .replace (">" , ">" )
42
+ .replace ('"' , """ )
43
+ .replace ("'" , "'" )
44
+ )
45
+
46
+ def _debug_warning_nonencoded_output (self ):
47
+ """Warns about exposing all files on the device."""
48
+ print (
49
+ f"WARNING: Setting html_output_encode to False will make XSS vulnerabilities possible by "
50
+ "allowing access to raw untrusted values submitted by users. If this data is reflected "
51
+ "or shown within HTML without proper encoding it could enable Cross-Site Scripting attacks."
52
+ )
53
+
54
+ def get (
55
+ self , field_name : str , default : Any = None , html_output_encode = True
56
+ ) -> Union [str , bytes , None ]:
37
57
"""Get the value of a field."""
38
- return self ._storage .get (field_name , [default ])[0 ]
58
+ if html_output_encode :
59
+ return self ._html_output_encode (self ._storage .get (field_name , [default ])[0 ])
60
+ else :
61
+ self ._debug_warning_nonencoded_output ()
62
+ return self ._storage .get (field_name , [default ])[0 ]
39
63
40
64
def get_list (self , field_name : str ) -> List [Union [str , bytes ]]:
41
65
"""Get the list of values of a field."""
0 commit comments