Skip to content

Commit 3765f8a

Browse files
committed
Merge dev into main
2 parents 8013939 + e2075df commit 3765f8a

File tree

6 files changed

+37
-14
lines changed

6 files changed

+37
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Powerful Python util methods and classes that simplify common apis and tasks.
44

5-
![Current Release](https://img.shields.io/badge/release-v2.57.0-blue)
5+
![Current Release](https://img.shields.io/badge/release-v2.57.2-blue)
66
[![codecov](https://codecov.io/gh/owasp-sbot/OSBot-Utils/graph/badge.svg?token=GNVW0COX1N)](https://codecov.io/gh/owasp-sbot/OSBot-Utils)
77

88

osbot_utils/helpers/safe_str/Safe_Str.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ def __new__(cls, value: Optional[str] = None) -> 'Safe_Str':
3737
elif not cls.exact_length and len(value) > cls.max_length: # Check max length
3838
raise ValueError(f"Value exceeds maximum length of {cls.max_length} characters (was {len(value)})")
3939

40+
if cls.allow_empty and value =='':
41+
return str.__new__(cls, '')
42+
4043
if cls.strict_validation: # If using strict validation, check if the value matches the regex pattern exactly
4144
if not cls.regex.search(value) is None: # If there are non-matching characters
4245
raise ValueError(f"Value contains invalid characters (must match pattern: {cls.regex.pattern})")

osbot_utils/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v2.57.0
1+
v2.57.2

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "osbot_utils"
3-
version = "v2.57.0"
3+
version = "v2.57.2"
44
description = "OWASP Security Bot - Utils"
55
authors = ["Dinis Cruz <[email protected]>"]
66
license = "MIT"

tests/unit/helpers/safe_str/test_Safe_Str.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,26 @@ class Hash_Like_Str(Safe_Str):
217217

218218
# Spaces should be trimmed before validation
219219
with pytest.raises(ValueError, match="Value must be exactly 10 characters long") as exc_info:
220-
Hash_Like_Str(' 123456789 ') # After trimming, it's only 9 chars
220+
Hash_Like_Str(' 123456789 ') # After trimming, it's only 9 chars
221+
222+
def test__check__trim_whitespace__allow_all_replacement_char(self):
223+
class An_Safe_Str(Safe_Str):
224+
allow_empty = True
225+
trim_whitespace = True
226+
allow_all_replacement_char = False
227+
assert An_Safe_Str() == ''
228+
expected_message = "Sanitized value consists entirely of '_' characters"
229+
with pytest.raises(ValueError, match=expected_message):
230+
An_Safe_Str('*')
231+
232+
def test__regression__regex__allow_all_replacement_char(self):
233+
class An_Safe_Str(Safe_Str):
234+
regex = re.compile(r'^(?!https?://)') # on this type of regex
235+
allow_all_replacement_char = False # this will trigger the exception bellow
236+
237+
#expected_message = "Sanitized value consists entirely of '_' characters"
238+
# with pytest.raises(ValueError, match=expected_message):
239+
# An_Safe_Str("") # BUG: should had not raised
240+
assert An_Safe_Str() == '' # FIXED: correct behaviour
241+
assert An_Safe_Str(None) == ''
242+
assert An_Safe_Str('') == ''

tests/unit/helpers/safe_str/test_Safe_Str__Url.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,11 @@ def test_Safe_Str__Url_class(self):
5050
assert str(Safe_Str__Url('https://example.com/path?param=value+with+spaces' )) == 'https://example.com/path?param=value+with+spaces'
5151
assert str(Safe_Str__Url('https://example.com/search?q=test&param="invalid"' )) == 'https://example.com/search?q=test&param=_invalid_'
5252

53-
54-
# Edge cases: exceptions with specific error message checks
55-
with pytest.raises(ValueError) as exc_info:
56-
Safe_Str__Url(None)
57-
assert "Sanitized value consists entirely of '_' characters" in str(exc_info.value) # todo : find better way to handle this scenario (this error comes from Safe_Str
58-
59-
with pytest.raises(ValueError) as exc_info:
60-
Safe_Str__Url('')
61-
assert "Sanitized value consists entirely of '_' characters" in str(exc_info.value)
53+
# Empty values allowed
54+
assert Safe_Str__Url(None) == ''
55+
assert Safe_Str__Url('' ) == ''
6256

57+
# Edge cases: exceptions with specific error message checks
6358
with pytest.raises(ValueError) as exc_info:
6459
Safe_Str__Url('example.com') # Missing scheme
6560
assert "Sanitized value consists entirely of '_' characters" in str(exc_info.value) # todo : find better way to handle this scenario (this error comes from Safe_Str
@@ -106,4 +101,7 @@ def test_url_encoding(self):
106101

107102
# Special characters in URL paths
108103
assert str(Safe_Str__Url('https://example.com/%7Euser/profile' )) == 'https://example.com/%7Euser/profile'
109-
assert str(Safe_Str__Url('https://example.com/caf%C3%A9' )) == 'https://example.com/caf%C3%A9'
104+
assert str(Safe_Str__Url('https://example.com/caf%C3%A9' )) == 'https://example.com/caf%C3%A9'
105+
106+
def test_edge_cases(self):
107+
assert Safe_Str__Url() == ''

0 commit comments

Comments
 (0)