Skip to content

Commit

Permalink
🗓 Mar 17, 2024 9:32:53 PM
Browse files Browse the repository at this point in the history
🐙 update to_html_entity to reflect cyberchef. closes #33
🧪 tests added/updated
  • Loading branch information
securisec committed Mar 18, 2024
1 parent 37cc188 commit c2a3876
Show file tree
Hide file tree
Showing 6 changed files with 1,490 additions and 33 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ New ideas:
☐ register support in callstack
☐ extract phone numbers. r'^(?:\+\d{1,3}|0\d{1,3}|00\d{1,2})?(?:\s?\(\d+\))?(?:[-\/\s.]|\d)+(x\d+)?$'
☐ load csv data
☐ 🔥 update python_requires in setup.py on python version change

Bug:

Expand Down
2 changes: 1 addition & 1 deletion chepy/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "6.6.1" # pragma: no cover
__version__ = "6.7.0" # pragma: no cover
__author__ = "@securisec" # pragma: no cover
24 changes: 20 additions & 4 deletions chepy/modules/dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
Rotate,
Uint1Array,
UUEncoderDecoder,
LZ77Compressor,
)

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

@ChepyDecorators.call_stack
def yaml_to_json(self) -> DataFormatT:
def yaml_to_json(self) -> DataFormatT: # pragma: no cover
"""Convert yaml to a json string
Returns:
Expand Down Expand Up @@ -1034,19 +1033,36 @@ def from_octal(self, delimiter: str = None, join_by: str = "") -> DataFormatT:
return self

@ChepyDecorators.call_stack
def to_html_entity(self) -> DataFormatT:
def to_html_entity(self, format="named", all_chars=False) -> DataFormatT:
"""Encode html entities
Encode special html characters like & > < etc
Args:
format (str): Encoding format. Valid formats are named, numeric and hex. Defaults to named
all_chars (bool): If all chars should be encoded. By default a-ZA-Z0-9 are skipped. Defaults to False
Returns:
Chepy: The Chepy object.
Examples:
>>> Chepy('https://google.com&a="lol"').to_html_entity().o
"https://google.com&amp;a=&quot;lol&quot;"
"""
self.state = html.escape(self._convert_to_str())
data = self._convert_to_str()
chars = {k: 1 for k in string.ascii_letters + string.digits}
hold = ""
for d in data:
if not all_chars and chars.get(d) is not None:
hold += d
continue
if format == "named":
hold += Encoding.BYTE_TO_ENTITY.get(ord(d), f"&#{ord(d)};")
elif format == "hex":
hold += f"&#x{d.encode().hex()};"
elif format == "numeric":
hold += f"&#{ord(d)};"
self.state = hold
return self

@ChepyDecorators.call_stack
Expand Down
2 changes: 1 addition & 1 deletion chepy/modules/dataformat.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class DataFormat(ChepyCore):
def from_binary(self: DataFormatT, delimiter: Union[str, None]=None, byte_length: int=...) -> DataFormatT: ...
def to_octal(self: DataFormatT, join_by: str=...) -> DataFormatT: ...
def from_octal(self: DataFormatT, delimiter: str=..., join_by: str=...) -> DataFormatT: ...
def to_html_entity(self: DataFormatT) -> DataFormatT: ...
def to_html_entity(self: DataFormatT, format:Literal['named', 'numeric', 'hex']='named', all_chars:bool=False) -> DataFormatT: ...
def from_html_entity(self: DataFormatT) -> DataFormatT: ...
def to_punycode(self: DataFormatT) -> DataFormatT: ...
def from_punycode(self: DataFormatT) -> DataFormatT: ...
Expand Down
Loading

0 comments on commit c2a3876

Please sign in to comment.