Skip to content

Commit db0c0e2

Browse files
authored
Git: don't expand envvars in Gitpython (#8263)
- Gitpython expands envvars by default We execute gitpython outside the containers. Currently there isn't a way to exploit this vulnerability (looks like gitpython is aware of this and may change the default to false any time https://github.com/gitpython-developers/GitPython/blob/617c09e70bfd54af1c88b4d2c892b8d287747542/git/repo/base.py#L142-L143) - In some places we were executing git commands outside the container (to get the commit), let's always use the current env (docker in production).
1 parent 1b1b035 commit db0c0e2

File tree

5 files changed

+31
-14
lines changed

5 files changed

+31
-14
lines changed

readthedocs/doc_builder/backends/mkdocs.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
.. _MkDocs: http://www.mkdocs.org/
55
"""
66

7-
import json
87
import logging
98
import os
109

@@ -17,7 +16,6 @@
1716
from readthedocs.projects.constants import MKDOCS, MKDOCS_HTML
1817
from readthedocs.projects.models import Feature
1918

20-
2119
log = logging.getLogger(__name__)
2220

2321

@@ -235,6 +233,14 @@ def generate_rtd_data(self, docs_dir, mkdocs_config):
235233
# http://www.mkdocs.org/user-guide/configuration/#google_analytics
236234
analytics_code = mkdocs_config['google_analytics'][0]
237235

236+
commit = (
237+
self.version.project.vcs_repo(
238+
version=self.version.slug,
239+
environment=self.build_env,
240+
)
241+
.commit,
242+
)
243+
238244
# Will be available in the JavaScript as READTHEDOCS_DATA.
239245
readthedocs_data = {
240246
'project': self.version.project.slug,
@@ -248,7 +254,7 @@ def generate_rtd_data(self, docs_dir, mkdocs_config):
248254
'source_suffix': '.md',
249255
'api_host': settings.PUBLIC_API_URL,
250256
'ad_free': not self.project.show_advertising,
251-
'commit': self.version.project.vcs_repo(self.version.slug).commit,
257+
'commit': commit,
252258
'global_analytics_code': (
253259
None if self.project.analytics_disabled else settings.GLOBAL_ANALYTICS_CODE
254260
),

readthedocs/doc_builder/backends/sphinx.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ def get_config_params(self):
160160
if self.version.is_external:
161161
vcs_url = self.version.vcs_url
162162

163+
commit = (
164+
self.project.vcs_repo(
165+
version=self.version.slug,
166+
environment=self.build_env,
167+
)
168+
.commit
169+
)
170+
163171
data = {
164172
'html_theme': 'sphinx_rtd_theme',
165173
'html_theme_import': 'sphinx_rtd_theme',
@@ -169,7 +177,7 @@ def get_config_params(self):
169177
'settings': settings,
170178
'conf_py_path': conf_py_path,
171179
'api_host': settings.PUBLIC_API_URL,
172-
'commit': self.project.vcs_repo(self.version.slug).commit,
180+
'commit': commit,
173181
'versions': versions,
174182
'downloads': downloads,
175183
'subproject_urls': subproject_urls,

readthedocs/projects/tasks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ def setup_vcs(self, environment):
927927
# Re raise the exception to stop the build at this point
928928
raise
929929

930-
commit = self.commit or self.project.vcs_repo(self.version.slug).commit
930+
commit = self.commit or self.get_vcs_repo(environment).commit
931931
if commit:
932932
self.build['commit'] = commit
933933

readthedocs/rtd_tests/tests/test_doc_builder.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
import tempfile
3-
from collections import namedtuple
43
from unittest import mock
54
from unittest.mock import patch
65

@@ -25,6 +24,7 @@
2524
SingleHtmlBuilder,
2625
)
2726
from readthedocs.doc_builder.config import load_yaml_config
27+
from readthedocs.doc_builder.environments import LocalBuildEnvironment
2828
from readthedocs.doc_builder.exceptions import MkDocsYAMLParseError
2929
from readthedocs.doc_builder.python_environments import Virtualenv
3030
from readthedocs.projects.constants import PRIVATE, PUBLIC
@@ -321,7 +321,7 @@ def setUp(self):
321321
self.project = get(Project, documentation_type='mkdocs', name='mkdocs')
322322
self.version = get(Version, project=self.project)
323323

324-
self.build_env = namedtuple('project', 'version')
324+
self.build_env = LocalBuildEnvironment(record=False)
325325
self.build_env.project = self.project
326326
self.build_env.version = self.version
327327

readthedocs/vcs_support/backends/git.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,16 @@ def update(self):
7272

7373
def repo_exists(self):
7474
try:
75-
git.Repo(self.working_dir)
75+
self._repo
7676
except (InvalidGitRepositoryError, NoSuchPathError):
7777
return False
7878
return True
7979

80+
@property
81+
def _repo(self):
82+
"""Get a `git.Repo` instance from the current `self.working_dir`."""
83+
return git.Repo(self.working_dir, expand_vars=False)
84+
8085
def are_submodules_available(self, config):
8186
"""Test whether git submodule checkout step should be performed."""
8287
submodules_in_config = (
@@ -232,7 +237,7 @@ def lsremote(self):
232237
@property
233238
def tags(self):
234239
versions = []
235-
repo = git.Repo(self.working_dir)
240+
repo = self._repo
236241

237242
# Build a cache of tag -> commit
238243
# GitPython is not very optimized for reading large numbers of tags
@@ -265,7 +270,7 @@ def tags(self):
265270

266271
@property
267272
def branches(self):
268-
repo = git.Repo(self.working_dir)
273+
repo = self._repo
269274
versions = []
270275
branches = []
271276

@@ -291,8 +296,7 @@ def commit(self):
291296

292297
@property
293298
def submodules(self):
294-
repo = git.Repo(self.working_dir)
295-
return list(repo.submodules)
299+
return list(self._repo.submodules)
296300

297301
def checkout(self, identifier=None):
298302
"""Checkout to identifier or latest."""
@@ -350,8 +354,7 @@ def find_ref(self, ref):
350354

351355
def ref_exists(self, ref):
352356
try:
353-
r = git.Repo(self.working_dir)
354-
if r.commit(ref):
357+
if self._repo.commit(ref):
355358
return True
356359
except (BadName, ValueError):
357360
return False

0 commit comments

Comments
 (0)