Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for Anchor & Alias Tags in yaml Transformer #679

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions detect_secrets/transformers/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,17 @@ def parse_file(self, file: NamedIO) -> List[str]:
except yaml.YAMLError:
raise ParsingError

seen = set()

lines: List[str] = []
for item in items:
# Filter out previous lines seen before. This removes duplicates when it comes
# to anchor & and alias * tags.
if item in seen:
continue
else:
seen.add(item)

while len(lines) < item.line_number - 1:
lines.append('')

Expand Down
114 changes: 114 additions & 0 deletions tests/transformers/yaml_transformer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,43 @@ def test_multi_line_flow_mapping():
'keyD: "valueD"',
]

@staticmethod
def test_single_anchor_tag():
file = mock_file_object(
textwrap.dedent("""
keyA: &test
keyB: string # with comments
""")[1:-1],
)

assert YAMLTransformer().parse_file(file) == [
'',
'keyB: "string" # with comments',
]

@staticmethod
def test_anchor_tag_alias_combination():
file = mock_file_object(
textwrap.dedent("""
groupA: &groupA
keyA: valueA
keyB: valueB

groupB: &groupB
keyC: valueC
keyD: *groupA
""")[1:-1],
)

assert YAMLTransformer().parse_file(file) == [
'',
'keyA: "valueA"',
'keyB: "valueB"',
'',
'',
'keyC: "valueC"',
]


class TestYAMLFileParser:
@staticmethod
Expand Down Expand Up @@ -428,3 +465,80 @@ def test_inline_mapping_single_line_multikey_line_numbers():
'__original_key__': 'd',
},
}

@staticmethod
def test_single_anchor_tag():
file = mock_file_object(
textwrap.dedent("""
keyA: &test
keyB: string # with comments
keyC:
keyD: string
""")[1:-1],
)

assert YAMLFileParser(file).json() == {
'keyA': {
'keyB': {
'__value__': 'string',
'__line__': 2,
'__original_key__': 'keyB',
},
'keyC': {
'keyD': {
'__value__': 'string',
'__line__': 4,
'__original_key__': 'keyD',
},
},
},
}

@staticmethod
def test_anchor_tag_alias_combination():
file = mock_file_object(
textwrap.dedent("""
groupA: &groupA
keyA: valueA
keyB: valueB

groupB: &groupB
keyC: valueC
keyD: *groupA
""")[1:-1],
)

temp = YAMLFileParser(file).json()
assert temp == {
'groupA': {
'keyA': {
'__value__': 'valueA',
'__line__': 2,
'__original_key__': 'keyA',
},
'keyB': {
'__value__': 'valueB',
'__line__': 3,
'__original_key__': 'keyB',
},
},
'groupB': {
'keyC': {
'__value__': 'valueC',
'__line__': 6,
'__original_key__': 'keyC',
},
'keyD': {
'keyA': {
'__value__': 'valueA',
'__line__': 2,
'__original_key__': 'keyA',
},
'keyB': {
'__value__': 'valueB',
'__line__': 3,
'__original_key__': 'keyB',
},
},
},
}