Skip to content

Incorrect handling of backslashes and quotes in GitConfigParser #46

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

Merged
merged 3 commits into from
May 30, 2012
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
30 changes: 27 additions & 3 deletions git/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,28 @@ def _read(self, fp, fpname):
if pos != -1 and optval[pos-1].isspace():
optval = optval[:pos]
optval = optval.strip()
if optval == '""':
optval = ''

# Remove paired unescaped-quotes
unquoted_optval = ''
escaped = False
in_quote = False
for c in optval:
if not escaped and c == '"':
in_quote = not in_quote
else:
escaped = (c == '\\') and not escaped
unquoted_optval += c

if in_quote:
if not e:
e = cp.ParsingError(fpname)
e.append(lineno, repr(line))

optval = unquoted_optval

optval = optval.replace('\\\\', '\\') # Unescape backslashes
optval = optval.replace(r'\"', '"') # Unescape quotes

optname = self.optionxform(optname.rstrip())
cursect[optname] = optval
else:
Expand Down Expand Up @@ -303,7 +323,11 @@ def write_section(name, section_dict):
fp.write("[%s]\n" % name)
for (key, value) in section_dict.items():
if key != "__name__":
fp.write("\t%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
value = str(value)
value = value.replace('\\', '\\\\') # Escape backslashes
value = value.replace('"', r'\"') # Escape quotes
value = value.replace('\n', '\n\t')
fp.write("\t%s = %s\n" % (key, value))
# END if key is not __name__
# END section writing

Expand Down
4 changes: 4 additions & 0 deletions git/test/fixtures/git_config_values
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[values]
backslash = some\\data
quote = this is a \"quoted value\"
quoted = "all" "your \"quotes\" a"re bel"ong to """"us"
26 changes: 25 additions & 1 deletion git/test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,28 @@ def test_base(self):
# it raises if there is no default though
self.failUnlessRaises(NoSectionError, r_config.get_value, "doesnt", "exist")


def test_values(self):
file_obj = self._to_memcache(fixture_path("git_config_values"))
w_config = GitConfigParser(file_obj, read_only = False)
w_config.write() # enforce writing
orig_value = file_obj.getvalue()

# Reading must unescape backslashes
backslash = w_config.get('values', 'backslash')
assert backslash == r'some\data'

# Reading must unescape quotes
quote = w_config.get('values', 'quote')
assert quote == 'this is a "quoted value"'

# Reading must remove surrounding quotes
quoted = w_config.get('values', 'quoted')
assert quoted == 'all your "quotes" are belong to us'

# Writing must escape backslashes and quotes
w_config.set('values', 'backslash', backslash)
w_config.set('values', 'quote', quote)
w_config.write() # enforce writing

# Contents shouldn't differ
assert file_obj.getvalue() == orig_value