Skip to content

Commit 78bfce1

Browse files
authored
Merge pull request #1094 from GitGuardian/agateau/fix-censor
Fix censor_string() behavior on short strings
2 parents d1dd32a + cd026fb commit 78bfce1

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Fixed
2+
3+
- Fixed a bug in the way ggshield obfuscated secrets that caused a crash for short secrets (#1086).

ggshield/core/filter.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,19 @@ def censor_string(text: str) -> str:
114114
:return: the text censored
115115
"""
116116
len_match = len(text)
117-
start_privy_len = min(math.ceil(len_match / 6), MAXIMUM_CENSOR_LENGTH)
118-
end_privy_len = len_match - min(math.ceil(len_match / 6), MAXIMUM_CENSOR_LENGTH)
117+
118+
# Special cases for short lengths
119+
if len_match <= 2:
120+
return "*" * len_match
121+
if len_match == 3:
122+
return f"**{text[2]}"
123+
124+
censor_start = min(math.ceil(len_match / 6), MAXIMUM_CENSOR_LENGTH)
125+
censor_end = len_match - censor_start
119126

120127
censored = REGEX_MATCH_HIDE.sub("*", text)
121128

122-
return str(
123-
text[:start_privy_len]
124-
+ censored[start_privy_len:end_privy_len]
125-
+ text[end_privy_len:]
126-
)
129+
return text[:censor_start] + censored[censor_start:censor_end] + text[censor_end:]
127130

128131

129132
def censor_match(match: Match) -> str:

tests/unit/core/test_filter.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pygitguardian.models import Match, PolicyBreak
77
from snapshottest import Snapshot
88

9-
from ggshield.core.filter import censor_match, get_ignore_sha
9+
from ggshield.core.filter import censor_match, censor_string, get_ignore_sha
1010
from tests.unit.conftest import (
1111
_MULTILINE_SECRET,
1212
_MULTIPLE_SECRETS_SCAN_RESULT,
@@ -116,3 +116,18 @@ def test_censor_match(input_match: Match, expected_value: str) -> None:
116116
value = censor_match(input_match)
117117
assert len(value) == len(input_match.match)
118118
assert value == expected_value
119+
120+
121+
@pytest.mark.parametrize(
122+
["text", "expected"],
123+
(
124+
("hello world", "he*** ***ld"),
125+
("abcd", "a**d"),
126+
("abc", "**c"),
127+
("ab", "**"),
128+
("a", "*"),
129+
),
130+
)
131+
def test_censor_string(text: str, expected: str) -> None:
132+
censored = censor_string(text)
133+
assert censored == expected

0 commit comments

Comments
 (0)