fix: harden config parsing boundaries by Byron · Pull Request #2211 · gitpython-developers/GitPython · GitHub
Skip to content
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
6 changes: 5 additions & 1 deletion git/config.py
2 changes: 1 addition & 1 deletion git/objects/submodule/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def _config_parser(
raise ValueError("Cannot write blobs of 'historical' submodule configurations")
# END handle writes of historical submodules

return SubmoduleConfigParser(fp_module, read_only=read_only)
return SubmoduleConfigParser(fp_module, read_only=read_only, merge_includes=False)

def _clear_cache(self) -> None:
"""Clear the possibly changed values."""
Expand Down
42 changes: 41 additions & 1 deletion test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io
import os
import os.path as osp
import subprocess
import sys
from unittest import mock

Expand All @@ -15,7 +16,6 @@
from git import GitConfigParser
from git.config import _OMD, cp
from git.util import cwd, rmfile

from test.lib import SkipTest, TestCase, fixture_path, with_rw_directory

_tc_lock_fpaths = osp.join(osp.dirname(__file__), "fixtures/*.lock")
Expand Down Expand Up @@ -150,6 +150,46 @@ def test_config_value_with_trailing_new_line(self):
git_config = GitConfigParser(config_file)
git_config.read() # This should not throw an exception

@with_rw_directory
def test_rewriting_multiline_value_does_not_create_option(self, rw_dir):
config_path = osp.join(rw_dir, "config")
with open(config_path, "wb") as config_file:
config_file.write(b'[core]\n\tzzz = "A\\nhooksPath = ../evil-hooks\\\n"\n')

with GitConfigParser(config_path, read_only=False) as git_config:
self.assertEqual(git_config.get_value("core", "zzz"), "A\nhooksPath = ../evil-hooks")
git_config.set_value("user", "name", "Test User")

with GitConfigParser(config_path, read_only=True) as git_config:
self.assertEqual(git_config.get_value("core", "zzz"), "A\nhooksPath = ../evil-hooks")
self.assertFalse(git_config.has_option("core", "hooksPath"))
self.assertEqual(
subprocess.run(["git", "config", "--file", config_path, "--get", "core.hooksPath"]).returncode, 1
)

@with_rw_directory
def test_writer_escapes_special_characters_without_newline(self, rw_dir):
config_path = osp.join(rw_dir, "config")
values = {"tab": "\tvalue\t", "backspace": "a\bb", "quote": 'a"b', "backslash": "a\\qb"}

with GitConfigParser(config_path, read_only=False) as git_config:
for key, value in values.items():
git_config.set_value("section", key, value)

with GitConfigParser(config_path, read_only=True) as git_config:
for key, value in values.items():
self.assertEqual(git_config.get_value("section", key), value)
self.assertEqual(
subprocess.run(
["git", "config", "--file", config_path, "--get", "section.%s" % key],
stdout=subprocess.PIPE,
check=True,
).stdout,
value.encode() + b"\n",
)
with open(config_path, "rb") as config_file:
self.assertNotIn(b"\x08", config_file.read())

@with_rw_directory
def test_set_value_rejects_config_injection(self, rw_dir):
config_path = osp.join(rw_dir, "config")
Expand Down
16 changes: 16 additions & 0 deletions test/test_submodule.py
Loading