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

Commit 1836e70

Browse files
authored
gitserver: Clean up output redaction (#61857)
Since we refactored VCSSyncer recently to take control of the remote URL, we now do redaction at this layer, so this TODO comment can be removed. Test plan: E2E test suite still passes.
1 parent 9d4b441 commit 1836e70

File tree

3 files changed

+29
-36
lines changed

3 files changed

+29
-36
lines changed

cmd/gitserver/internal/server.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -924,11 +924,6 @@ func (s *Server) doBackgroundRepoUpdate(repo api.RepoName, revspec string) error
924924

925925
dir := s.fs.RepoDir(repo)
926926

927-
remoteURL, err := s.getRemoteURL(ctx, repo)
928-
if err != nil {
929-
return errors.Wrap(err, "failed to determine Git remote URL")
930-
}
931-
932927
syncer, err := s.getVCSSyncer(ctx, repo)
933928
if err != nil {
934929
return errors.Wrap(err, "get VCS syncer")
@@ -942,11 +937,8 @@ func (s *Server) doBackgroundRepoUpdate(repo api.RepoName, revspec string) error
942937
defer git.CleanTmpPackFiles(s.logger, dir)
943938

944939
output, err := syncer.Fetch(ctx, repo, dir, revspec)
945-
// TODO: Move the redaction also into the VCSSyncer layer here, to be in line
946-
// with what clone does.
947-
redactedOutput := urlredactor.New(remoteURL).Redact(string(output))
948940
// best-effort update the output of the fetch
949-
if err := s.db.GitserverRepos().SetLastOutput(serverCtx, repo, redactedOutput); err != nil {
941+
if err := s.db.GitserverRepos().SetLastOutput(serverCtx, repo, string(output)); err != nil {
950942
s.logger.Warn("Setting last output in DB", log.Error(err))
951943
}
952944

@@ -955,7 +947,7 @@ func (s *Server) doBackgroundRepoUpdate(repo api.RepoName, revspec string) error
955947
return err
956948
}
957949
if output != nil {
958-
return errors.Wrapf(err, "failed to fetch repo %q with output %q", repo, redactedOutput)
950+
return errors.Wrapf(err, "failed to fetch repo %q with output %q", repo, string(output))
959951
} else {
960952
return errors.Wrapf(err, "failed to fetch repo %q", repo)
961953
}

cmd/gitserver/internal/vcssyncer/git.go

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package vcssyncer
33
import (
44
"bytes"
55
"context"
6-
"github.com/sourcegraph/sourcegraph/internal/gitserver/protocol"
7-
"github.com/sourcegraph/sourcegraph/internal/lazyregexp"
86
"io"
97
"os"
108
"os/exec"
@@ -13,12 +11,13 @@ import (
1311

1412
"github.com/sourcegraph/log"
1513

16-
"github.com/sourcegraph/sourcegraph/internal/api"
17-
1814
"github.com/sourcegraph/sourcegraph/cmd/gitserver/internal/common"
1915
"github.com/sourcegraph/sourcegraph/cmd/gitserver/internal/executil"
2016
"github.com/sourcegraph/sourcegraph/cmd/gitserver/internal/git"
2117
"github.com/sourcegraph/sourcegraph/cmd/gitserver/internal/urlredactor"
18+
"github.com/sourcegraph/sourcegraph/internal/api"
19+
"github.com/sourcegraph/sourcegraph/internal/gitserver/protocol"
20+
"github.com/sourcegraph/sourcegraph/internal/lazyregexp"
2221
"github.com/sourcegraph/sourcegraph/internal/wrexec"
2322
"github.com/sourcegraph/sourcegraph/lib/errors"
2423
)
@@ -63,33 +62,31 @@ func (s *gitRepoSyncer) IsCloneable(ctx context.Context, repoName api.RepoName)
6362
return errors.Wrapf(err, "failed to get remote URL source for %s", repoName)
6463
}
6564

66-
{
67-
remoteURL, err := source.RemoteURL(ctx)
68-
if err != nil {
69-
return errors.Wrapf(err, "failed to get remote URL for %s", repoName)
70-
}
65+
remoteURL, err := source.RemoteURL(ctx)
66+
if err != nil {
67+
return errors.Wrapf(err, "failed to get remote URL for %s", repoName)
68+
}
7169

72-
args := []string{"ls-remote", remoteURL.String(), "HEAD"}
73-
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
74-
defer cancel()
70+
args := []string{"ls-remote", remoteURL.String(), "HEAD"}
71+
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
72+
defer cancel()
7573

76-
r := urlredactor.New(remoteURL)
77-
cmd := exec.CommandContext(ctx, "git", args...)
74+
r := urlredactor.New(remoteURL)
75+
cmd := exec.CommandContext(ctx, "git", args...)
7876

79-
// Configure the command to be able to talk to a remote.
80-
executil.ConfigureRemoteGitCommand(cmd, remoteURL)
77+
// Configure the command to be able to talk to a remote.
78+
executil.ConfigureRemoteGitCommand(cmd, remoteURL)
8179

82-
out, err := s.recordingCommandFactory.WrapWithRepoName(ctx, log.NoOp(), repoName, cmd).WithRedactorFunc(r.Redact).CombinedOutput()
83-
if err != nil {
84-
if ctxerr := ctx.Err(); ctxerr != nil {
85-
err = ctxerr
86-
}
87-
if len(out) > 0 {
88-
redactedOutput := urlredactor.New(remoteURL).Redact(string(out))
89-
err = errors.Wrap(err, "failed to check remote access: "+redactedOutput)
90-
}
91-
return err
80+
out, err := s.recordingCommandFactory.WrapWithRepoName(ctx, log.NoOp(), repoName, cmd).WithRedactorFunc(r.Redact).CombinedOutput()
81+
if err != nil {
82+
if ctxerr := ctx.Err(); ctxerr != nil {
83+
err = ctxerr
84+
}
85+
if len(out) > 0 {
86+
redactedOutput := r.Redact(string(out))
87+
err = errors.Wrap(err, "failed to check remote access: "+redactedOutput)
9288
}
89+
return err
9390
}
9491

9592
return nil

cmd/gitserver/internal/vcssyncer/syncer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ type VCSSyncer interface {
6464
// to lazily fetch package versions. More details at
6565
// https://github.com/sourcegraph/sourcegraph/issues/37921#issuecomment-1184301885
6666
// Beware that the revspec parameter can be any random user-provided string.
67+
// 🚨 SECURITY:
68+
// Output returned from this function should NEVER contain sensitive information.
69+
// The VCSSyncer implementation is responsible of redacting potentially
70+
// sensitive data like secrets.
6771
Fetch(ctx context.Context, repoName api.RepoName, dir common.GitDir, revspec string) ([]byte, error)
6872
}
6973

0 commit comments

Comments
 (0)