Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 1c7871c

Browse files
authored
manually backport #55657 into 5.1 (#56219)
1 parent 622059e commit 1c7871c

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ All notable changes to Sourcegraph are documented in this file.
2929
- OpenTelemetry Collector has been upgraded to v0.81, and OpenTelemetry packages have been upgraded to v1.16. [#54969](https://github.com/sourcegraph/sourcegraph/pull/54969), [#54999](https://github.com/sourcegraph/sourcegraph/pull/54999)
3030
- Bitbucket Cloud code host connections no longer automatically syncs the repository of the username used. The appropriate workspace name will have to be added to the `teams` list if repositories for that account need to be synced. [#55095](https://github.com/sourcegraph/sourcegraph/pull/55095)
3131
- The gRPC implementation for the Symbol service's `LocalCodeIntel` endpoint has been changed to stream its results. [#55242](https://github.com/sourcegraph/sourcegraph/pull/55242)
32-
- Pressing `Mod-f` will always select the input value in the file view search [#55546](https://github.com/sourcegraph/sourcegraph/pull/55546)
3332
- When using OpenAI or Azure OpenAI for Cody completions, code completions will be disabled - chat will continue to work. This is because we currently don't support code completions with OpenAI. [#55624](https://github.com/sourcegraph/sourcegraph/pull/55624)
3433
- Caddy has been updated to version 2.7.3 resolving a number of vulnerabilities. [#55606](https://github.com/sourcegraph/sourcegraph/pull/55606)
35-
- The commit message defined in a batch spec will now be passed to `git commit` on stdin using `--file=-` instead of being included inline with `git commit -m` to improve how the message is interpreted by `git` in certain edge cases, such as when the commit message begins with a dash, and to prevent extra quotes being added to the message. This may mean that previous escaping strategies will behave differently.
34+
- The commit message defined in a batch spec will now be passed to `git commit` on stdin using `--file=-` instead of being included inline with `git commit -m` to improve how the message is interpreted by `git` in certain edge cases, such as when the commit message begins with a dash, and to prevent extra quotes being added to the message. This may mean that previous escaping strategies will behave differently. [#55657](https://github.com/sourcegraph/sourcegraph/pull/55657)
3635

3736
### Fixed
3837

cmd/gitserver/server/patch.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,14 @@ func (s *Server) createCommitFromPatch(ctx context.Context, req protocol.CreateC
227227
committerEmail = authorEmail
228228
}
229229

230-
gitCommitArgs := []string{"commit"}
231-
for _, m := range messages {
232-
gitCommitArgs = append(gitCommitArgs, "-m", stylizeCommitMessage(m))
233-
}
234-
cmd = exec.CommandContext(ctx, "git", gitCommitArgs...)
230+
// Commit messages can be arbitrary strings, so using `-m` runs into problems.
231+
// Instead, feed the commit messages to stdin.
232+
cmd = exec.CommandContext(ctx, "git", "commit", "-F", "-")
233+
// NOTE: join messages with a blank line in between ("\n\n")
234+
// because the previous behavior was to use multiple -m arguments,
235+
// which concatenate with a blank line in between.
236+
// Gerrit is the only code host that uses multiple messages at the moment
237+
cmd.Stdin = strings.NewReader(strings.Join(messages, "\n\n"))
235238

236239
cmd.Dir = tmpRepoDir
237240
cmd.Env = append(os.Environ(), []string{
@@ -351,17 +354,6 @@ func (s *Server) createCommitFromPatch(ctx context.Context, req protocol.CreateC
351354
return http.StatusOK, resp
352355
}
353356

354-
func stylizeCommitMessage(message string) string {
355-
if styleMessage(message) {
356-
return fmt.Sprintf("%q", message)
357-
}
358-
return message
359-
}
360-
361-
func styleMessage(message string) bool {
362-
return !strings.HasPrefix(message, "Change-Id: I")
363-
}
364-
365357
func (s *Server) shelveChangelist(ctx context.Context, req protocol.CreateCommitFromPatchRequest, patchCommit string, remoteURL *vcs.URL, tmpGitPathEnv, altObjectsEnv string) (string, error) {
366358

367359
repo := string(req.Repo)

internal/gitserver/gitdomain/exec.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,18 @@ func IsAllowedGitCmd(logger log.Logger, args []string) bool {
141141
logger.Warn("command not allowed", log.String("cmd", cmd))
142142
return false
143143
}
144+
145+
// I hate state machines, but I hate them less than complicated multi-argument checking
146+
checkFileInput := false
144147
for i, arg := range args[1:] {
148+
if checkFileInput {
149+
if arg == "-" {
150+
checkFileInput = false
151+
continue
152+
}
153+
logger.Warn("IsAllowedGitCmd: unallowed file input for `git commit`", log.String("cmd", cmd), log.String("arg", arg))
154+
return false
155+
}
145156
if strings.HasPrefix(arg, "-") {
146157
// Special-case `git log -S` and `git log -G`, which interpret any characters
147158
// after their 'S' or 'G' as part of the query. There is no long form of this
@@ -162,6 +173,24 @@ func IsAllowedGitCmd(logger log.Logger, args []string) bool {
162173
continue // this arg is OK
163174
}
164175

176+
// For `git commit`, allow reading the commit message from stdin
177+
// but don't just blindly accept the `--file` or `-F` args
178+
// because they could be used to read arbitrary files.
179+
// Instead, accept only the forms that read from stdin.
180+
if cmd == "commit" {
181+
if arg == "--file=-" {
182+
continue
183+
}
184+
// checking `-F` requires a second check for `-` in the next argument
185+
// Instead of an obtuse check of next and previous arguments, set state and check it the next time around
186+
// Here's the alternative obtuse check of previous and next arguments:
187+
// (arg == "-F" && len(args) > i+2 && args[i+2] == "-") || (arg == "-" && args[i] == "-F")
188+
if arg == "-F" {
189+
checkFileInput = true
190+
continue
191+
}
192+
}
193+
165194
if !isAllowedGitArg(allowedArgs, arg) {
166195
logger.Warn("IsAllowedGitCmd.isAllowedGitArgcmd", log.String("cmd", cmd), log.String("arg", arg))
167196
return false

0 commit comments

Comments
 (0)