Skip to content

Commit aa4c700

Browse files
codexByron
authored andcommitted
Address review comments about quoted subsection validation
Review feedback: Section-name validation treated closing brackets as quoted data when an opening quote was unterminated, ended with a trailing escape, or was not a valid quoted-subsection opener. The regression matrix lacked those malformed forms. Only enter quoted-subsection state when the opening quote follows whitespace, and reject names whose quote state remains open after scanning. Extend the writer-entry-point regression matrix with invalid-position, unterminated, and trailing-escape cases while retaining the valid quoted-bracket case.
1 parent ecd6844 commit aa4c700

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

git/config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,15 +900,19 @@ def _assure_config_name_safe(self, name: "cp._SectionName", label: str) -> None:
900900
if label == "section" and isinstance(name, str):
901901
in_quotes = False
902902
escaped = False
903-
for char in name:
903+
for index, char in enumerate(name):
904904
if escaped:
905905
escaped = False
906906
elif in_quotes and char == "\\":
907907
escaped = True
908908
elif char == '"':
909+
if not in_quotes and (index == 0 or name[index - 1] not in " \t"):
910+
raise ValueError("Git config quoted subsection names must begin after whitespace")
909911
in_quotes = not in_quotes
910912
elif char == "]" and not in_quotes:
911913
raise ValueError("Git config section names must not contain an unquoted closing bracket")
914+
if in_quotes:
915+
raise ValueError("Git config section names must not contain an unterminated quote")
912916

913917
@needs_values
914918
@set_dirty_and_flush_changes

test/test_config.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,21 +197,27 @@ def test_set_value_rejects_unsafe_section_and_option_names(self, rw_dir):
197197
@with_rw_directory
198198
def test_writer_rejects_unquoted_section_terminators(self, rw_dir):
199199
config_path = osp.join(rw_dir, "config")
200-
bad_sections = ("user] [other", 'submodule "docs"] [other')
200+
bad_sections = (
201+
"user] [other",
202+
'user"] [other"',
203+
'submodule "docs"] [other',
204+
'submodule "docs] [other',
205+
'submodule "docs] [other\\',
206+
)
201207
safe_section = 'submodule "docs]archive"'
202208

203209
with GitConfigParser(config_path, read_only=False) as git_config:
204210
git_config.add_section("user")
205211
for bad_section in bad_sections:
206-
with pytest.raises(ValueError, match="closing bracket"):
212+
with pytest.raises(ValueError, match="section name"):
207213
git_config.add_section(bad_section)
208-
with pytest.raises(ValueError, match="closing bracket"):
214+
with pytest.raises(ValueError, match="section name"):
209215
git_config.set(bad_section, "name", "value")
210-
with pytest.raises(ValueError, match="closing bracket"):
216+
with pytest.raises(ValueError, match="section name"):
211217
git_config.set_value(bad_section, "name", "value")
212-
with pytest.raises(ValueError, match="closing bracket"):
218+
with pytest.raises(ValueError, match="section name"):
213219
git_config.add_value(bad_section, "name", "value")
214-
with pytest.raises(ValueError, match="closing bracket"):
220+
with pytest.raises(ValueError, match="section name"):
215221
git_config.rename_section("user", bad_section)
216222

217223
git_config.set_value("user", "name", "safe")

0 commit comments

Comments
 (0)