Skip to content

Commit a55bfe9

Browse files
committed
move debug def, rename arg to safe, rename encoding def, use more descriptive encoded value.
1 parent 96a6d0e commit a55bfe9

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

adafruit_httpserver/request.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,34 +34,25 @@ def _add_field_value(self, field_name: str, value: Union[str, bytes]) -> None:
3434
self._storage[field_name].append(value)
3535

3636
@staticmethod
37-
def _html_output_encode(value):
37+
def _encode_html_entities(value):
3838
"""Encodes unsafe HTML characters."""
3939
return (
4040
str(value)
4141
.replace("&", "&")
4242
.replace("<", "&lt;")
4343
.replace(">", "&gt;")
4444
.replace('"', "&quot;")
45-
.replace("'", "&#x27;")
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("'", "&apos;")
5546
)
5647

5748
def get(
58-
self, field_name: str, default: Any = None, html_output_encode=True
49+
self, field_name: str, default: Any = None, *, safe=True
5950
) -> Union[str, bytes, None]:
6051
"""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])
6354

64-
self._debug_warning_nonencoded_output()
55+
_debug_warning_nonencoded_output()
6556
return self._storage.get(field_name, [default])[0]
6657

6758
def get_list(self, field_name: str) -> List[Union[str, bytes]]:
@@ -375,3 +366,12 @@ def _parse_headers(header_bytes: bytes) -> Headers:
375366
for name, value in [header_line.split(": ", 1)]
376367
}
377368
)
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

Comments
 (0)