Skip to content

Commit

Permalink
paired brackets refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
babenek committed Aug 30, 2024
1 parent acb6575 commit 84f26ee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
23 changes: 11 additions & 12 deletions credsweeper/credentials/line_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,17 @@ def clean_bash_parameters(self) -> None:
self.value = value_whsp[0]

def clean_toml_parameters(self) -> None:
"""Parenthesis, curly and squared brackets may be caught in TOML format and bash"""
while self.value and self.value[-1] in ('}', ']', ')'):
if self.value.endswith('}') and '{' not in self.value:
self.value = self.value[:-1]
elif self.value.endswith(']') and '[' not in self.value:
self.value = self.value[:-1]
elif (self.value.endswith(')') and '(' not in self.value
and self.line.rfind(')', 0, self.value_start) < self.line.rfind('(', 0, self.value_start)):
# let`s clear right parenthesis too with some additional checks
self.value = self.value[:-1]
else:
break
"""Parenthesis, curly and squared brackets may be caught in TOML format and bash. Simple clearing"""
dirty=self.value and self.value[-1] in ['}',']',')']
line_before_value=self.line[: self.value_start]
while dirty:
dirty=False
for left,right in [('{','}'), ('[',']'), ('(',')')]:
if self.value.endswith(right) and left not in self.value \
and line_before_value.count(left) > line_before_value.count(right):
# full match does not reasonable to implement due open character may be in other line
self.value = self.value[:-1]
dirty=True

def sanitize_variable(self) -> None:
"""Remove trailing spaces, dashes and quotations around the variable. Correct position."""
Expand Down
11 changes: 8 additions & 3 deletions tests/credentials/test_line_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ def test_toml_parenthesis_pass_sanitize_p(self) -> None:
"ieUW47@)",
LineData(None, "$(secure_cmd) password=ieUW47@)", 0, 1, "", "", "",
re.compile(r".*(?P<variable>password)(?P<separator>=)(?P<value>.+)")).value)
self.assertEqual(
"ieUW47@}",
LineData(None, "password: ieUW47@}", 0, 1, "", "", "",
re.compile(r".*(?P<variable>password)(?P<separator>:) (?P<value>.+)")).value)

def test_toml_quoted_sanitize_p(self) -> None:
self.assertEqual(
Expand All @@ -196,18 +200,19 @@ def test_toml_quoted_sanitize_p(self) -> None:

def test_toml_curly_brackets_sanitize_n(self) -> None:
self.assertEqual(
"ieUW47@",
"ieUW47@}",
LineData(None, "${secure_cmd} password=ieUW47@}", 0, 1, "", "", "",
re.compile(r".*(?P<variable>password)(?P<separator>=)(?P<value>.+)")).value)

def test_toml_square_brackets_sanitize_n(self) -> None:
self.assertEqual(
"ieUW47@",
"ieUW47@]",
LineData(None, "$[secure_cmd] password=ieUW47@]", 0, 1, "", "", "",
re.compile(r".*(?P<variable>password)(?P<separator>=)(?P<value>.+)")).value)

def test_toml_extra_sanitize_n(self) -> None:
# dummy variant with wrong order
self.assertEqual(
"",
LineData(None, "(extra-cleaned-value password=}])", 0, 1, "", "", "",
LineData(None, "[{(extra-cleaned-value password=}}]})]}}])", 0, 1, "", "", "",
re.compile(r".*(?P<variable>password)(?P<separator>=)(?P<value>.+)")).value)

0 comments on commit 84f26ee

Please sign in to comment.