Skip to content

Commit 39d76b9

Browse files
authored
Merge pull request #269 from gitonomy/1.7.1-security
Backport the security fixes of 2.0
2 parents 02f796e + 5ea2a9e commit 39d76b9

17 files changed

Lines changed: 1346 additions & 52 deletions

‎CHANGELOG.md‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,71 @@
22

33
All notable changes to this project are documented in this file.
44

5+
## [v1.7.1] - Unreleased
6+
7+
Changes since [v1.7.0](https://github.com/gitonomy/gitlib/releases/tag/v1.7.0).
8+
9+
1.7.1 has the security fixes of 2.0 that break nothing of 1.6. Where 2.0 refuses every option,
10+
1.7.1 only refuses what reads or writes a file: the other options of `git log` as revisions stay
11+
deprecated, and work. The checks of a URL keep returning `false`, and clones keep throwing a
12+
`RuntimeException`, where 2.0 throws an `InvalidArgumentException`.
13+
14+
### Security
15+
16+
- Refuse `--output` as a revision, which made git write a file: `getRevision('--output=/path')`
17+
and everything reading a revision through it, `getLog()`, `getDiff()`, `getMergeBase()` or
18+
`Branch::moveTo()` for instance.
19+
- Refuse the options of `git blame` reading or writing a file as a revision:
20+
`getBlame('--contents=/path', 'README.md')` returned the lines of any file, and the error of
21+
`--ignore-revs-file` or `-S` printed its first line.
22+
- Refuse a URL starting with a dash, which git read as an option:
23+
`Admin::isValidRepository('--upload-pack=...')` ran the command in a repository with a remote,
24+
`Admin::isValidRepositoryAndBranch()` and the clones when the second argument was a
25+
repository. A path may start with a dash: `init()` and the clones end the options of git with
26+
`--`, as `cloneFrom($url, '--upload-pack=...')` ran the command too.
27+
- Refuse a hash starting with a dash in `ReferenceBag::createBranch()` and `createTag()`:
28+
`createBranch('foo', '-d')` deleted the branch. So do the branch and the start point of
29+
`WorkingCopy::checkoutNewBranch()`.
30+
- Refuse an `fd::` URL, which makes git talk through a file descriptor it inherits from PHP, like
31+
the socket of a database, and wait on it.
32+
- Refuse an ssh URL whose host or user starts with a dash, even encoded (`%2D`), like
33+
`ssh://-oProxyCommand=.../repository`: git before 2.14.1 passes it to ssh, which reads it as an
34+
option and runs the command (CVE-2017-1000117). A `git://` URL whose host starts with a dash
35+
is refused too: git passes it to the command of `core.gitProxy`.
36+
- Hide the credentials of URLs (`https://user:token@host`), a password holding a `#` or a `?`
37+
included, in the messages of `ProcessException` and of failed clones, in the logs, and in the
38+
messages of the exceptions of Symfony Process, a timeout for instance, which keep their class.
39+
From PHP 8.2, the traces hide them too (`#[\SensitiveParameter]`).
40+
- Refuse a hook name leading out of the hooks directory: `Hooks::set('../../file', ...)`,
41+
`remove('../config')` and `get('../config')` wrote, deleted and read files of the repository.
42+
The names staying in the directory, like `post-receive.d/notify`, still work.
43+
- Fix `Hooks` writing through a symbolic link to a missing file, out of the hooks directory:
44+
`has()` returned `false` for it, and `set()` followed it.
45+
- Ignore `core.fsmonitor`: git ran the command the configuration of a repository names there to
46+
read the working tree, and even to diff a commit.
47+
- Fix deleting a symbolic reference, which deleted the branch it points to instead:
48+
`ReferenceBag::delete('refs/remotes/origin/HEAD')` deleted `origin/main`.
49+
- Fix `ReferenceBag::delete('HEAD')` deleting a detached `HEAD`, which left no repository: it
50+
throws an `InvalidArgumentException`.
51+
- Require `symfony/process` 5.4.51, 6.4.33, 7.3.11, 7.4.5 or 8.0.5 and later: the older versions
52+
escape the arguments wrongly on Windows (CVE-2024-51736, CVE-2026-24739).
53+
54+
### Not fixed
55+
56+
These fixes of 2.0 would break code written for 1.6. An application giving gitlib values it can't
57+
trust has to check them itself:
58+
59+
- `ext::` URLs stay accepted, as an application may use this transport on purpose. git refuses
60+
them by default since 2.12: before that, `ext::sh -c ...` runs the command.
61+
- The replacements of objects (`git replace`, `refs/replace/`) stay read, as `git replace --graft`
62+
relies on them: whoever can push such a reference changes the message of a commit, or the
63+
content of a blob, gitlib reads for a hash. Set `GIT_NO_REPLACE_OBJECTS` to `1` in the
64+
`environmentVariables` of `CliDriver` to ignore them.
65+
- `WorkingCopy::checkout()` still takes a path or an option: `checkout('README.md')` discards the
66+
changes of the file, and `checkout('--force')` all of them.
67+
- Moving a symbolic reference, with `Branch::moveTo()` or `ReferenceBag::update()`, still moves
68+
the branch it points to.
69+
570
## [v1.7.0] - 2026-10-15
671

772
Changes since [v1.6.0](https://github.com/gitonomy/gitlib/releases/tag/v1.6.0).
@@ -100,5 +165,6 @@ _Earlier versions are not documented here. See the
100165
[GitHub releases](https://github.com/gitonomy/gitlib/releases) and
101166
[commit history](https://github.com/gitonomy/gitlib/commits/v1.6.0) for prior changes._
102167

168+
[v1.7.1]: https://github.com/gitonomy/gitlib/compare/v1.7.0...v1.7.1
103169
[v1.7.0]: https://github.com/gitonomy/gitlib/compare/v1.6.0...v1.7.0
104170
[v1.6.0]: https://github.com/gitonomy/gitlib/releases/tag/v1.6.0

‎composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"ext-pcre": "*",
4040
"symfony/deprecation-contracts": "^2.5 || ^3",
4141
"symfony/polyfill-mbstring": "^1.7",
42-
"symfony/process": "^5.4 || ^6.0 || ^7.0 || ^8.0"
42+
"symfony/process": "^5.4.51 || ^6.4.33 || ~7.3.11 || ^7.4.5 || ^8.0.5"
4343
},
4444
"require-dev": {
4545
"ext-fileinfo": "*",

‎doc/repository.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ option is the second argument, and its `logger`, `command`,
4949
`CliDriver`. Setting its `debug` option to `false` is deprecated: gitlib
5050
2.0 always throws when git fails.
5151

52+
Ignore the replacements of objects
53+
----------------------------------
54+
55+
git reads the replacements of a repository (`git replace`): whoever can
56+
push one changes the message of a commit, or the content of a blob, you
57+
read for a hash. For a repository you don't trust, ignore them:
58+
59+
```php
60+
use Gitonomy\Git\Driver\Cli\CliDriver;
61+
use Gitonomy\Git\Repository;
62+
63+
$repository = new Repository('/path/to/repo', driver: new CliDriver(
64+
environmentVariables: ['GIT_NO_REPLACE_OBJECTS' => '1'],
65+
));
66+
```
67+
68+
gitlib 2.0 always ignores them.
69+
5270
Test if a repository is bare
5371
----------------------------
5472

‎src/Gitonomy/Git/Admin.php‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
use Gitonomy\Git\Driver\Cli\CliDriver;
1616
use Gitonomy\Git\Exception\RuntimeException;
17+
use Gitonomy\Git\Util\Credentials;
1718
use Gitonomy\Git\Util\LegacyOverride;
19+
use Gitonomy\Git\Util\RemoteUrl;
1820

1921
/**
2022
* Administration class for Git repositories.
@@ -68,14 +70,18 @@ public static function init($path, $bare = true, array $options = [])
6870
*
6971
* @deprecated since gitonomy/gitlib 1.7, use Repository::isValidRepository() instead
7072
*/
71-
public static function isValidRepository($url, array $options = [])
73+
public static function isValidRepository(#[\SensitiveParameter] $url, array $options = [])
7274
{
7375
trigger_deprecation('gitonomy/gitlib', '1.7', 'The "%s()" method is deprecated, use "%s::isValidRepository()" instead.', __METHOD__, Repository::class);
7476

77+
if (null !== RemoteUrl::unsafeReason((string) $url)) {
78+
return false;
79+
}
80+
7581
// Unlike Repository::isValidRepository(), in the working directory of PHP, where a relative path may name a remote.
7682
$process = CliDriver::fromOptions($options)->createProcess('ls-remote', [$url]);
7783

78-
$process->run();
84+
Credentials::runProcess($process);
7985

8086
return $process->isSuccessFul();
8187
}
@@ -96,15 +102,19 @@ public static function isValidRepository($url, array $options = [])
96102
*
97103
* @deprecated since gitonomy/gitlib 1.7, use Repository::isValidRepositoryAndBranch() instead
98104
*/
99-
public static function isValidRepositoryAndBranch($url, $branchName, array $options = [])
105+
public static function isValidRepositoryAndBranch(#[\SensitiveParameter] $url, $branchName, array $options = [])
100106
{
101107
trigger_deprecation('gitonomy/gitlib', '1.7', 'The "%s()" method is deprecated, use "%s::isValidRepositoryAndBranch()" instead.', __METHOD__, Repository::class);
102108

109+
if (null !== RemoteUrl::unsafeReason((string) $url)) {
110+
return false;
111+
}
112+
103113
// Unlike Repository::isValidRepositoryAndBranch(), "release" matches "feature/release", and "rel" "release",
104114
// in the working directory of PHP.
105115
$process = CliDriver::fromOptions($options)->createProcess('ls-remote', ['--heads', $url, $branchName]);
106116

107-
$process->run();
117+
Credentials::runProcess($process);
108118
$processOutput = $process->getOutput();
109119

110120
return $process->isSuccessFul() && strpos($processOutput, $branchName) !== false;
@@ -122,7 +132,7 @@ public static function isValidRepositoryAndBranch($url, $branchName, array $opti
122132
*
123133
* @deprecated since gitonomy/gitlib 1.7, use Repository::cloneBareFrom() or Repository::cloneFrom() instead
124134
*/
125-
public static function cloneTo($path, $url, $bare = true, array $options = [])
135+
public static function cloneTo($path, #[\SensitiveParameter] $url, $bare = true, array $options = [])
126136
{
127137
trigger_deprecation('gitonomy/gitlib', '1.7', 'The "%s()" method is deprecated, use "%s::%s()" instead, which takes the URL before the path.', __METHOD__, Repository::class, $bare ? 'cloneBareFrom' : 'cloneFrom');
128138

@@ -144,7 +154,7 @@ public static function cloneTo($path, $url, $bare = true, array $options = [])
144154
*
145155
* @deprecated since gitonomy/gitlib 1.7, use Repository::cloneBareFrom() or Repository::cloneFrom() with their "branch" argument instead
146156
*/
147-
public static function cloneBranchTo($path, $url, $branch, $bare = true, $options = [])
157+
public static function cloneBranchTo($path, #[\SensitiveParameter] $url, $branch, $bare = true, $options = [])
148158
{
149159
trigger_deprecation('gitonomy/gitlib', '1.7', 'The "%s()" method is deprecated, use "%s::%s()" with its "branch" argument instead, which takes the URL before the path.', __METHOD__, Repository::class, $bare ? 'cloneBareFrom' : 'cloneFrom');
150160

@@ -167,7 +177,7 @@ public static function cloneBranchTo($path, $url, $branch, $bare = true, $option
167177
*
168178
* @deprecated since gitonomy/gitlib 1.7, use Repository::mirrorFrom() instead
169179
*/
170-
public static function mirrorTo($path, $url, array $options = [])
180+
public static function mirrorTo($path, #[\SensitiveParameter] $url, array $options = [])
171181
{
172182
trigger_deprecation('gitonomy/gitlib', '1.7', 'The "%s()" method is deprecated, use "%s::mirrorFrom()" instead, which takes the URL before the path.', __METHOD__, Repository::class);
173183

@@ -186,7 +196,7 @@ public static function mirrorTo($path, $url, array $options = [])
186196
*
187197
* @deprecated since gitonomy/gitlib 1.7, use Repository::cloneFrom(), Repository::cloneBareFrom() or Repository::mirrorFrom() instead
188198
*/
189-
public static function cloneRepository($path, $url, array $args = [], array $options = [])
199+
public static function cloneRepository($path, #[\SensitiveParameter] $url, array $args = [], array $options = [])
190200
{
191201
trigger_deprecation('gitonomy/gitlib', '1.7', 'The "%1$s()" method is deprecated, use "%2$s::cloneFrom()", "%2$s::cloneBareFrom()" or "%2$s::mirrorFrom()" instead, which take the URL before the path; they support no other argument of "git clone".', __METHOD__, Repository::class);
192202

@@ -195,7 +205,7 @@ public static function cloneRepository($path, $url, array $args = [], array $opt
195205
return new Repository($path, $options);
196206
}
197207

198-
private static function cloneWith($path, $url, array $args, array $options)
208+
private static function cloneWith($path, #[\SensitiveParameter] $url, array $args, array $options)
199209
{
200210
// Like in 1.6, a subclass overriding cloneRepository() clones the repository.
201211
if (LegacyOverride::isOverridden(static::class, self::class, 'cloneRepository')) {

‎src/Gitonomy/Git/Blame.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Gitonomy\Git\Blame\Line;
1717
use Gitonomy\Git\Exception\InvalidArgumentException;
1818
use Gitonomy\Git\Parser\BlameParser;
19+
use Gitonomy\Git\Util\LeadingDash;
1920

2021
/**
2122
* @author Alexandre Salomé <alexandre.salome@gmail.com>
@@ -167,6 +168,8 @@ public function getLines()
167168
return $this->lines;
168169
}
169170

171+
LeadingDash::assertNoBlameFileOption($this->revision->getRevision());
172+
170173
$args = ['-p'];
171174

172175
if (null !== $this->lineRange) {

‎src/Gitonomy/Git/Driver/Cli/CliDriver.php‎

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
namespace Gitonomy\Git\Driver\Cli;
1414

1515
use Gitonomy\Git\Exception\RuntimeException;
16+
use Gitonomy\Git\Util\Credentials;
17+
use Gitonomy\Git\Util\RemoteUrl;
1618
use Psr\Log\LoggerInterface;
1719
use Symfony\Component\Process\Process;
1820

@@ -77,7 +79,8 @@ public function getOptions(): array
7779
*/
7880
public function init(string $path): void
7981
{
80-
$this->initRepository(['-q', $path]);
82+
// --: a path may start with a dash.
83+
$this->initRepository(['-q', '--', $path]);
8184
}
8285

8386
/**
@@ -87,15 +90,15 @@ public function init(string $path): void
8790
*/
8891
public function initBare(string $path): void
8992
{
90-
$this->initRepository(['-q', '--bare', $path]);
93+
$this->initRepository(['-q', '--bare', '--', $path]);
9194
}
9295

9396
/**
9497
* @internal For Repository::cloneFrom()
9598
*
9699
* @throws RuntimeException
97100
*/
98-
public function cloneFrom(string $url, string $path, ?string $branch): void
101+
public function cloneFrom(#[\SensitiveParameter] string $url, string $path, ?string $branch): void
99102
{
100103
$this->cloneRepository($url, $path, null === $branch ? [] : ['--branch', $branch]);
101104
}
@@ -105,7 +108,7 @@ public function cloneFrom(string $url, string $path, ?string $branch): void
105108
*
106109
* @throws RuntimeException
107110
*/
108-
public function cloneBareFrom(string $url, string $path, ?string $branch): void
111+
public function cloneBareFrom(#[\SensitiveParameter] string $url, string $path, ?string $branch): void
109112
{
110113
$this->cloneRepository($url, $path, null === $branch ? ['--bare'] : ['--branch', $branch, '--bare']);
111114
}
@@ -115,30 +118,38 @@ public function cloneBareFrom(string $url, string $path, ?string $branch): void
115118
*
116119
* @throws RuntimeException
117120
*/
118-
public function mirrorFrom(string $url, string $path): void
121+
public function mirrorFrom(#[\SensitiveParameter] string $url, string $path): void
119122
{
120123
$this->cloneRepository($url, $path, ['--mirror']);
121124
}
122125

123126
/**
124127
* @internal For Repository::isValidRepository()
125128
*/
126-
public function isValidRepository(string $url): bool
129+
public function isValidRepository(#[\SensitiveParameter] string $url): bool
127130
{
131+
if (null !== RemoteUrl::unsafeReason($url)) {
132+
return false;
133+
}
134+
128135
$process = $this->createLsRemoteProcess([], $url, []);
129-
$process->run();
136+
Credentials::runProcess($process);
130137

131138
return $process->isSuccessful();
132139
}
133140

134141
/**
135142
* @internal For Repository::isValidRepositoryAndBranch()
136143
*/
137-
public function isValidRepositoryAndBranch(string $url, string $branch): bool
144+
public function isValidRepositoryAndBranch(#[\SensitiveParameter] string $url, string $branch): bool
138145
{
146+
if (null !== RemoteUrl::unsafeReason($url)) {
147+
return false;
148+
}
149+
139150
// git lists the branches whose name ends with the pattern: "release" matches "feature/release".
140151
$process = $this->createLsRemoteProcess(['--heads'], $url, ['refs/heads/'.$branch]);
141-
$process->run();
152+
Credentials::runProcess($process);
142153
if (!$process->isSuccessful()) {
143154
return false;
144155
}
@@ -159,13 +170,19 @@ public function isValidRepositoryAndBranch(string $url, string $branch): bool
159170
*
160171
* @throws RuntimeException
161172
*/
162-
public function cloneRepository(string $url, string $path, array $args): void
173+
public function cloneRepository(#[\SensitiveParameter] string $url, string $path, array $args): void
163174
{
164-
$process = $this->createProcess('clone', array_merge(['-q'], $args, [$url, $path]));
165-
$process->run();
175+
$reason = RemoteUrl::unsafeReason($url);
176+
if (null !== $reason) {
177+
throw new RuntimeException(sprintf('Error while initializing repository: %s', $reason));
178+
}
179+
180+
// --: a path may start with a dash.
181+
$process = $this->createProcess('clone', array_merge(['-q'], $args, ['--', $url, $path]));
182+
Credentials::runProcess($process);
166183

167184
if (!$process->isSuccessful()) {
168-
throw new RuntimeException(sprintf('Error while initializing repository: %s', $process->getErrorOutput()));
185+
throw new RuntimeException(sprintf('Error while initializing repository: %s', Credentials::redact($process->getErrorOutput())));
169186
}
170187
}
171188

‎src/Gitonomy/Git/Exception/ProcessException.php‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Gitonomy\Git\Exception;
44

5+
use Gitonomy\Git\Util\Credentials;
56
use Symfony\Component\Process\Process;
67

78
/**
@@ -11,16 +12,20 @@ class ProcessException extends RuntimeException implements GitExceptionInterface
1112
{
1213
protected $process;
1314

15+
/**
16+
* The message hides the credentials of the URLs, unlike getErrorOutput()
17+
* and getOutput().
18+
*/
1419
public function __construct(Process $process)
1520
{
16-
parent::__construct(
21+
parent::__construct(Credentials::redact(
1722
"Error while running git command:\n".
1823
$process->getCommandLine()."\n".
1924
"\n".
2025
$process->getErrorOutput()."\n".
2126
"\n".
2227
$process->getOutput()
23-
);
28+
));
2429

2530
$this->process = $process;
2631
}

0 commit comments

Comments
 (0)