Skip to content

Commit c2a3876

Browse files
committed
🗓 Mar 17, 2024 9:32:53 PM
🐙 update to_html_entity to reflect cyberchef. closes #33 🧪 tests added/updated
1 parent 37cc188 commit c2a3876

6 files changed

Lines changed: 1490 additions & 33 deletions

File tree

TODO

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ New ideas:
3030
☐ register support in callstack
3131
☐ extract phone numbers. r'^(?:\+\d{1,3}|0\d{1,3}|00\d{1,2})?(?:\s?\(\d+\))?(?:[-\/\s.]|\d)+(x\d+)?$'
3232
☐ load csv data
33+
☐ 🔥 update python_requires in setup.py on python version change
3334

3435
Bug:
3536

chepy/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
__version__ = "6.6.1" # pragma: no cover
1+
__version__ = "6.7.0" # pragma: no cover
22
__author__ = "@securisec" # pragma: no cover

chepy/modules/dataformat.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
Rotate,
1818
Uint1Array,
1919
UUEncoderDecoder,
20-
LZ77Compressor,
2120
)
2221

2322
yaml = lazy_import.lazy_module("yaml")
@@ -176,7 +175,7 @@ def dict_get_items(self, *keys) -> DataFormatT:
176175
return self
177176

178177
@ChepyDecorators.call_stack
179-
def yaml_to_json(self) -> DataFormatT:
178+
def yaml_to_json(self) -> DataFormatT: # pragma: no cover
180179
"""Convert yaml to a json string
181180
182181
Returns:
@@ -1034,19 +1033,36 @@ def from_octal(self, delimiter: str = None, join_by: str = "") -> DataFormatT:
10341033
return self
10351034

10361035
@ChepyDecorators.call_stack
1037-
def to_html_entity(self) -> DataFormatT:
1036+
def to_html_entity(self, format="named", all_chars=False) -> DataFormatT:
10381037
"""Encode html entities
10391038
10401039
Encode special html characters like & > < etc
10411040
1041+
Args:
1042+
format (str): Encoding format. Valid formats are named, numeric and hex. Defaults to named
1043+
all_chars (bool): If all chars should be encoded. By default a-ZA-Z0-9 are skipped. Defaults to False
1044+
10421045
Returns:
10431046
Chepy: The Chepy object.
10441047
10451048
Examples:
10461049
>>> Chepy('https://google.com&a="lol"').to_html_entity().o
10471050
"https://google.com&amp;a=&quot;lol&quot;"
10481051
"""
1049-
self.state = html.escape(self._convert_to_str())
1052+
data = self._convert_to_str()
1053+
chars = {k: 1 for k in string.ascii_letters + string.digits}
1054+
hold = ""
1055+
for d in data:
1056+
if not all_chars and chars.get(d) is not None:
1057+
hold += d
1058+
continue
1059+
if format == "named":
1060+
hold += Encoding.BYTE_TO_ENTITY.get(ord(d), f"&#{ord(d)};")
1061+
elif format == "hex":
1062+
hold += f"&#x{d.encode().hex()};"
1063+
elif format == "numeric":
1064+
hold += f"&#{ord(d)};"
1065+
self.state = hold
10501066
return self
10511067

10521068
@ChepyDecorators.call_stack

chepy/modules/dataformat.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class DataFormat(ChepyCore):
5757
def from_binary(self: DataFormatT, delimiter: Union[str, None]=None, byte_length: int=...) -> DataFormatT: ...
5858
def to_octal(self: DataFormatT, join_by: str=...) -> DataFormatT: ...
5959
def from_octal(self: DataFormatT, delimiter: str=..., join_by: str=...) -> DataFormatT: ...
60-
def to_html_entity(self: DataFormatT) -> DataFormatT: ...
60+
def to_html_entity(self: DataFormatT, format:Literal['named', 'numeric', 'hex']='named', all_chars:bool=False) -> DataFormatT: ...
6161
def from_html_entity(self: DataFormatT) -> DataFormatT: ...
6262
def to_punycode(self: DataFormatT) -> DataFormatT: ...
6363
def from_punycode(self: DataFormatT) -> DataFormatT: ...

0 commit comments

Comments
 (0)