Skip to content

Commit e28ea52

Browse files
pieternIsaac
andcommitted
Strip credentials from origin URL in deploy telemetry
bundleGitInfo assumed load_git_details had already stripped userinfo from bundle.git.origin_url, but that only happens on the local .git route. A URL set in databricks.yml, or returned by the workspace API for a Git folder, was sent to telemetry as written, including any embedded token. Strip userinfo when building the telemetry git info so every route is covered. Extract the stripping into git.StripURLCredentials and use it from Repository.OriginUrl and the uv pip.conf debug log, which carried their own copies. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 2534bb0 commit e28ea52

10 files changed

Lines changed: 74 additions & 34 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
bundle:
2+
name: test-bundle
3+
git:
4+
origin_url: https://user:token@github.com/databricks/cli
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Cloud = false
2+
EnvMatrix.DMS = [""]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
>>> [CLI] bundle deploy
3+
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files...
4+
Files: 1 uploaded, 0 deleted
5+
Resources: 0 created, 0 changed, 0 deleted, 0 unchanged
6+
7+
>>> cat [OUTPUT_DIR]/out.requests.txt
8+
{
9+
"git": {
10+
"origin_url": "https://github.com/databricks/cli"
11+
}
12+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
trace $CLI bundle deploy
2+
3+
trace cat "$OUT_REQUESTS" | jq 'select(has("path") and .path == "/telemetry-ext") | .body.protoLogs[] | fromjson | .entry.databricks_cli_log.bundle_deploy_event | {git}'
4+
5+
rm "$OUT_REQUESTS"

‎bundle/phases/telemetry.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/databricks/cli/bundle/metrics"
1919
"github.com/databricks/cli/libs/dbr"
2020
"github.com/databricks/cli/libs/dyn"
21+
libgit "github.com/databricks/cli/libs/git"
2122
"github.com/databricks/cli/libs/log"
2223
"github.com/databricks/cli/libs/telemetry"
2324
"github.com/databricks/cli/libs/telemetry/protos"
@@ -232,15 +233,16 @@ func pydabsMetrics(b *bundle.Bundle) *protos.BundleDeployPydabs {
232233
}
233234

234235
// bundleGitInfo builds the git metadata for the deploy event, or nil when the
235-
// bundle root is not inside a git repository. OriginURL already has credentials
236-
// stripped by the load_git_details mutator.
236+
// bundle root is not inside a git repository. OriginURL is stripped here because
237+
// only the local .git route strips it upstream; a value from bundle.git.origin_url
238+
// or from the workspace API for a Git folder is passed through as written.
237239
func bundleGitInfo(b *bundle.Bundle) *protos.BundleGitInfo {
238240
git := b.Config.Bundle.Git
239241
if git.Branch == "" && git.Commit == "" && git.OriginURL == "" {
240242
return nil
241243
}
242244
return &protos.BundleGitInfo{
243-
OriginURL: git.OriginURL,
245+
OriginURL: libgit.StripURLCredentials(git.OriginURL),
244246
Branch: git.Branch,
245247
Commit: git.Commit,
246248
}

‎bundle/phases/telemetry_test.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,20 @@ func TestPydabsMetrics(t *testing.T) {
204204
b := &bundle.Bundle{Config: config.Root{Experimental: &config.Experimental{Python: pythonConfig}}, Metrics: metrics}
205205
assert.Equal(t, expected, pydabsMetrics(b))
206206
}
207+
208+
func TestBundleGitInfoStripsOriginURLCredentials(t *testing.T) {
209+
tests := []struct {
210+
originURL string
211+
want string
212+
}{
213+
{"https://user:token@github.com/databricks/cli", "https://github.com/databricks/cli"},
214+
{"https://github.com/databricks/cli", "https://github.com/databricks/cli"},
215+
}
216+
for _, tt := range tests {
217+
t.Run(tt.originURL, func(t *testing.T) {
218+
b := &bundle.Bundle{}
219+
b.Config.Bundle.Git.OriginURL = tt.originURL
220+
assert.Equal(t, tt.want, bundleGitInfo(b).OriginURL)
221+
})
222+
}
223+
}

‎libs/git/repository.go‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,11 @@ func (r *Repository) LatestCommit() (string, error) {
107107

108108
// return origin url if it's defined, otherwise an empty string
109109
func (r *Repository) OriginUrl() string {
110-
rawUrl := r.config.variables["remote.origin.url"]
110+
return StripURLCredentials(r.config.variables["remote.origin.url"])
111+
}
111112

112-
// Remove username and password from the URL.
113+
// StripURLCredentials removes the username and password from a URL.
114+
func StripURLCredentials(rawUrl string) string {
113115
parsedUrl, err := url.Parse(rawUrl)
114116
if err != nil {
115117
// Git supports https URLs and non standard URLs like "ssh://" or "file://".
@@ -118,6 +120,11 @@ func (r *Repository) OriginUrl() string {
118120
// because passwords cannot be included in the URL.
119121
return rawUrl
120122
}
123+
// Leave URLs without credentials as written; re-serializing can rewrite
124+
// them (e.g. escaping spaces).
125+
if parsedUrl.User == nil {
126+
return rawUrl
127+
}
121128
// Setting User to nil removes the username and password from the URL when
122129
// .String() is called.
123130
// See: https://pkg.go.dev/net/url#URL.String

‎libs/git/repository_test.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,21 @@ func TestRepositoryOriginUrlRemovesUserCreds(t *testing.T) {
231231
repo.assertOriginUrl(tc.expected)
232232
}
233233
}
234+
235+
func TestStripURLCredentials(t *testing.T) {
236+
cases := []struct {
237+
in string
238+
want string
239+
}{
240+
{"https://user:pass@proxy.example/simple", "https://proxy.example/simple"},
241+
{"https://token@proxy.example/simple", "https://proxy.example/simple"},
242+
{"https://proxy.example/simple", "https://proxy.example/simple"},
243+
{"ssh://git@github.com/databricks/cli.git", "ssh://github.com/databricks/cli.git"},
244+
{"git@github.com:databricks/cli.git", "git@github.com:databricks/cli.git"},
245+
{"not a url", "not a url"},
246+
{"", ""},
247+
}
248+
for _, tc := range cases {
249+
assert.Equal(t, tc.want, StripURLCredentials(tc.in))
250+
}
251+
}

‎libs/localenv/uv.go‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bufio"
55
"context"
66
"errors"
7-
"net/url"
87
"os"
98
"os/exec"
109
"path/filepath"
@@ -14,6 +13,7 @@ import (
1413

1514
"github.com/databricks/cli/libs/cmdio"
1615
"github.com/databricks/cli/libs/env"
16+
"github.com/databricks/cli/libs/git"
1717
"github.com/databricks/cli/libs/log"
1818
"github.com/databricks/cli/libs/process"
1919
)
@@ -437,25 +437,13 @@ func (m *uvManager) resolveIndexURL(ctx context.Context) string {
437437
if url != "" {
438438
// Redact any embedded credentials: private PyPI proxies often carry
439439
// userinfo (https://user:pass@host/simple) that must not reach debug logs.
440-
log.Debugf(ctx, "uv: using package index %s from pip.conf", redactURLCredentials(url))
440+
log.Debugf(ctx, "uv: using package index %s from pip.conf", git.StripURLCredentials(url))
441441
} else {
442442
log.Debugf(ctx, "uv: no UV_INDEX_URL and no index-url in pip.conf; uv will use its default index (pypi.org)")
443443
}
444444
return url
445445
}
446446

447-
// redactURLCredentials strips userinfo from a URL so it is safe to log. A value
448-
// that does not parse as a URL is returned unchanged (it carries no parseable
449-
// userinfo to leak).
450-
func redactURLCredentials(raw string) string {
451-
u, err := url.Parse(raw)
452-
if err != nil || u.User == nil {
453-
return raw
454-
}
455-
u.User = nil
456-
return u.String()
457-
}
458-
459447
// uvFailure builds a PipelineError from a failed uv invocation, appending uv's
460448
// stderr to the message so callers can see the actual failure reason (e.g.
461449
// "Connection refused") rather than just the exit code.

‎libs/localenv/uv_test.go‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -301,21 +301,6 @@ func TestPipConfIndexURLReadsOSSpecificPath(t *testing.T) {
301301
assert.Equal(t, "https://proxy.example/simple", pipConfIndexURL(ctx))
302302
}
303303

304-
func TestRedactURLCredentials(t *testing.T) {
305-
cases := []struct {
306-
in string
307-
want string
308-
}{
309-
{"https://user:pass@proxy.example/simple", "https://proxy.example/simple"},
310-
{"https://token@proxy.example/simple", "https://proxy.example/simple"},
311-
{"https://proxy.example/simple", "https://proxy.example/simple"},
312-
{"not a url", "not a url"},
313-
}
314-
for _, tc := range cases {
315-
assert.Equal(t, tc.want, redactURLCredentials(tc.in))
316-
}
317-
}
318-
319304
func TestPipConfIndexURLReadsLegacyPath(t *testing.T) {
320305
if runtime.GOOS == "windows" {
321306
t.Skip("legacy ~/.pip path is Unix/macOS only")

0 commit comments

Comments
 (0)