Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit aae8022

Browse files
committedJul 3, 2024·
🗓 Jul 2, 2024 11:59:14 PM
💚 build steps added/updated ✨ aws_account_id_from_access_key ✨ expand_alpha_range
1 parent 3bc516b commit aae8022

File tree

8 files changed

+114
-20
lines changed

8 files changed

+114
-20
lines changed
 

‎.github/workflows/tests_multi_os.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ jobs:
5656
5757
- name: Install test requirements
5858
run: |
59-
pip install sphinx recommonmark pytest==8.1.1 pytest-cov pyperclip
59+
pip install sphinx recommonmark pytest==8.1.1 pytest-cov==5.0.0 pyperclip
6060
6161
- name: Test with pytest
6262
run: |
63-
pytest -v --disable-pytest-warnings --cov-report=xml --cov=chepy --cov-config=.coveragerc tests/
63+
COVERAGE_CORE=sysmon pytest -v --disable-pytest-warnings --cov-report=xml --cov=chepy --cov-config=.coveragerc tests/
6464
coverage report -m
6565
6666
- name: Test plugins osx

‎Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33

44
test:
5-
python -m pytest --noconftest -v --disable-pytest-warnings --cov-report=xml --cov=chepy --cov-config=.coveragerc tests/
5+
COVERAGE_CORE=sysmon python -m pytest --noconftest -v --disable-pytest-warnings --cov-report=xml --cov=chepy --cov-config=.coveragerc tests/
66

77
test-all: test
8-
python -m pytest --noconftest -v --disable-pytest-warnings tests_plugins/
8+
COVERAGE_CORE=sysmon python -m pytest --noconftest -v --disable-pytest-warnings tests_plugins/
99

1010
# git log --format=%B 4.0.0..5.0.0 | sed '/^\s*$/d' | sort | uniq

‎chepy/modules/extractors.py

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import math
22
from binascii import unhexlify
3+
import base64
34
from typing import TypeVar, Union, List
45
from urllib.parse import urlparse as _pyurlparse
56
import lazy_import
@@ -684,3 +685,19 @@ def extract_html_comments(self):
684685
filter(lambda x: x != "", self._parsel_obj().xpath("//comment()").getall())
685686
)
686687
return self
688+
689+
@ChepyDecorators.call_stack
690+
def aws_account_id_from_access_key(self):
691+
"""Extract AWS account id from access key
692+
693+
Returns:
694+
Chepy: The Chepy object.
695+
"""
696+
trimmed_AWSKeyID = self._convert_to_str()[4:]
697+
x = base64.b32decode(trimmed_AWSKeyID)
698+
y = x[0:6]
699+
z = int.from_bytes(y, byteorder='big', signed=False)
700+
mask = int.from_bytes(unhexlify(b'7fffffffff80'), byteorder='big', signed=False)
701+
702+
self.state = (z & mask)>>7
703+
return self

‎chepy/modules/extractors.pyi

+1
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ class Extractors(ChepyCore):
4444
def css_selector(self: ExtractorsT, query: str) -> ExtractorsT: ...
4545
def extract_html_comments(self: ExtractorsT) -> ExtractorsT: ...
4646
def extract_html_tags(self: ExtractorsT, tag: List[str]) -> ExtractorsT: ...
47+
def aws_account_id_from_access_key(self: ExtractorsT) -> ExtractorsT: ...

‎chepy/modules/utils.py

+50
Original file line numberDiff line numberDiff line change
@@ -819,3 +819,53 @@ def pick(self, *values: Any):
819819
else: # pragma: no cover
820820
raise TypeError("Input should be a list, dictionary, string, or bytes")
821821
return self
822+
823+
@ChepyDecorators.call_stack
824+
def expand_alpha_range(self, join_by: Union[str, None] = None):
825+
"""Get all alphanumberic or hex chars for the specified range
826+
827+
Args:
828+
join_by (str, optional): Join by. Defaults to Union[str, None].
829+
830+
Returns:
831+
Chepy: The Chepy object.
832+
"""
833+
alph_str = self._convert_to_str()
834+
hold = []
835+
836+
def expand_range(start, end):
837+
return [str(x) for x in range(int(start), int(end) + 1)]
838+
839+
def expand_char_range(start, end):
840+
return [chr(x) for x in range(ord(start), ord(end) + 1)]
841+
842+
hold = []
843+
i = 0
844+
length = len(alph_str)
845+
846+
while i < length:
847+
# Match numerical ranges like 10-20
848+
num_match = re.match(r"(\d+)-(\d+)", alph_str[i:])
849+
if num_match:
850+
start, end = num_match.groups()
851+
hold.extend(expand_range(start, end))
852+
i += len(start) + len(end) + 1 # move past the number range
853+
elif i < length - 2 and alph_str[i + 1] == "-" and alph_str[i] != "\\":
854+
# Handle character ranges like a-z
855+
start = alph_str[i]
856+
end = alph_str[i + 2]
857+
hold.extend(expand_char_range(start, end))
858+
i += 2
859+
elif (
860+
i < length - 2 and alph_str[i] == "\\" and alph_str[i + 1] == "-"
861+
): # pragma: no cover
862+
hold.append("-")
863+
i += 1
864+
else:
865+
hold.append(alph_str[i])
866+
i += 1
867+
868+
if join_by is not None:
869+
hold = join_by.join(hold)
870+
self.state = hold
871+
return self

‎chepy/modules/utils.pyi

+1
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ class Utils(ChepyCore):
4141
def drop_bytes(self: UtilsT, start: int, length: int) -> UtilsT: ...
4242
def without(self: UtilsT, *values: Any) -> UtilsT: ...
4343
def pick(self: UtilsT, *values: Any) -> UtilsT: ...
44+
def expand_alpha_range(self, join_by: Union[str, None]=None) -> UtilsT: ...

‎tests/test_extractors.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,7 @@ def test_find_continuous_patterns():
239239

240240
def test_zero_with_chars_tags():
241241
assert (
242-
Chepy("this 󠁮󠁩󠁣is 󠁣󠁻󠀰just 󠁲󠁟󠀱a 󠀵󠁟󠀱simple 󠀷󠁽text file")
243-
.extract_zero_width_chars_tags()
244-
.o
242+
Chepy("this 󠁮󠁩󠁣is 󠁣󠁻󠀰just 󠁲󠁟󠀱a 󠀵󠁟󠀱simple 󠀷󠁽text file").extract_zero_width_chars_tags().o
245243
== b"nicc{0r_15_17}"
246244
)
247245

@@ -252,7 +250,7 @@ def test_decode_zero_width():
252250
"e2808be2808be2808be2808befbbbfe280ace2808b68656c6c6fe2808be2808be2808be2808befbbbfe2808be2808ce2808be2808be2808be2808be280acefbbbfefbbbfe2808be2808be2808be2808befbbbfe2808defbbbfe2808be2808be2808be2808befbbbfe2808be2808ce2808be2808be2808be2808befbbbfe280ace2808c"
253251
)
254252
.from_hex()
255-
.decode_zero_width("\u200B\u200c\u200d\u202c\ufeff")
253+
.decode_zero_width("\u200b\u200c\u200d\u202c\ufeff")
256254
.o["hidden"]
257255
== "secret"
258256
)
@@ -271,7 +269,7 @@ def test_decode_zero_width():
271269
)
272270
.from_hex()
273271
.decode_zero_width(
274-
"\u200B\u200C\u200D\u200E\u202A\u202C\u202D\u2062\u2063\ufeff"
272+
"\u200b\u200c\u200d\u200e\u202a\u202c\u202d\u2062\u2063\ufeff"
275273
)
276274
.o["hidden"]
277275
== "secret"
@@ -296,3 +294,9 @@ def test_html_tags():
296294
def test_html_comments():
297295
c = Chepy("tests/files/test.html").load_file().extract_html_comments()
298296
assert len(c.o) == 3
297+
298+
299+
def test_aws_access_key_from_account_id():
300+
assert (
301+
Chepy("ASIAQNZGKIQY56JQ7WML").aws_account_id_from_access_key().o == 29608264753
302+
)

‎tests/test_utils.py

+32-11
Original file line numberDiff line numberDiff line change
@@ -246,17 +246,38 @@ def test_shuffle():
246246
def test_drop_bytes():
247247
assert Chepy("hello").drop_bytes(2, 2).o == b"heo"
248248

249+
249250
def test_without_pick():
250-
data1 = 'hello'
251-
data2 = [1,2,'a', 'b']
252-
data3 = {'a':1, 2: 3}
251+
data1 = "hello"
252+
data2 = [1, 2, "a", "b"]
253+
data3 = {"a": 1, 2: 3}
253254
# test without
254-
assert Chepy(data1).without('ll').o == b'heo'
255-
assert Chepy(data1).without('l', b'l').o == b'heo'
256-
assert Chepy(data2).without(1, 'a').o == [2,'b']
257-
assert Chepy(data3).without('a').o == {2: 3}
255+
assert Chepy(data1).without("ll").o == b"heo"
256+
assert Chepy(data1).without("l", b"l").o == b"heo"
257+
assert Chepy(data2).without(1, "a").o == [2, "b"]
258+
assert Chepy(data3).without("a").o == {2: 3}
258259
# test pick
259-
assert Chepy(data1).pick('ll').o == b'll'
260-
assert Chepy(data1).pick('l', b'l').o == b'll'
261-
assert Chepy(data2).pick(1, 'a').o == [1,'a']
262-
assert Chepy(data3).pick('a').o == {'a': 1}
260+
assert Chepy(data1).pick("ll").o == b"ll"
261+
assert Chepy(data1).pick("l", b"l").o == b"ll"
262+
assert Chepy(data2).pick(1, "a").o == [1, "a"]
263+
assert Chepy(data3).pick("a").o == {"a": 1}
264+
265+
266+
def test_alpha_range():
267+
assert Chepy("a-e").expand_alpha_range().o == ["a", "b", "c", "d", "e"]
268+
assert Chepy("a-cA-C0-2").expand_alpha_range().o == [
269+
"a",
270+
"b",
271+
"c",
272+
"A",
273+
"B",
274+
"C",
275+
"0",
276+
"1",
277+
"2",
278+
]
279+
assert (
280+
Chepy("a-cA-C0-2").expand_alpha_range("").o
281+
== "".join(["a", "b", "c", "A", "B", "C", "0", "1", "2"]).encode()
282+
)
283+
assert Chepy(" a-c:").expand_alpha_range().o == [" ", "a", "b", "c", ":"]

0 commit comments

Comments
 (0)