Skip to content

Commit 48dca56

Browse files
committed
initial hardening
1 parent e0b2ba9 commit 48dca56

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

adafruit_httpserver/request.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,33 @@ def _add_field_value(self, field_name: str, value: Union[str, bytes]) -> None:
3333
else:
3434
self._storage[field_name].append(value)
3535

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("<", "&lt;")
41+
.replace(">", "&gt;")
42+
.replace('"', "&quot;")
43+
.replace("'", "&#x27;")
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]:
3757
"""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]
3963

4064
def get_list(self, field_name: str) -> List[Union[str, bytes]]:
4165
"""Get the list of values of a field."""

0 commit comments

Comments
 (0)