From b16153960df4e222bff13b615db2759c083d5a0e Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Thu, 3 Dec 2026 09:49:43 -0600 Subject: [PATCH] fix(coderd): normalize workspace secret path comparisons (#32868) ## Why `~/.token` and `~//.token` point to the same file, but a workspace build could accept a different secret for each spelling. Replacing a secret on a later build could leave the old value active alongside its replacement. ## How Validation, carry-forward, and delivery now use the same key to compare file paths. The stored paths stay as entered, so existing secrets need no migration.
Decision and regression coverage Comparison is lexical: `~/../~/x` and `~/x` count as a collision even if they would resolve to different files on the agent. A regression test pins that behavior. The database still indexes the original spelling, so existing aliases can carry forward together until a new secret claims their target. Regression tests check the field-specific validation error and verify that only the replacement survives a later build. Original report: https://github.com/coder/coder/pull/30250#discussion_r4272372439 (RELAY-64).
Created by Coder Agents on behalf of @Emyrk. --- coderd/workspaces_test.go | 34 +++++++++++++++++---- coderd/workspacesecrets.go | 7 +++-- coderd/workspacesecrets/workspacesecrets.go | 12 +++++++- coderd/wsbuilder/wsbuilder.go | 10 ++++-- 4 files changed, 51 insertions(+), 12 deletions(-) diff --git a/coderd/workspaces_test.go b/coderd/workspaces_test.go index 57614b09873..d996f80d3ce 100644 --- a/coderd/workspaces_test.go +++ b/coderd/workspaces_test.go @@ -6830,20 +6830,23 @@ func TestWorkspaceSecrets(t *testing.T) { require.Len(t, cleared[build2.ID], 2) }) - t.Run("TargetTakeoverAndEmptyValue", func(t *testing.T) { + t.Run("TargetTakeoverWithPathAliasesAndEmptyValue", func(t *testing.T) { t.Parallel() ctx := testutil.Context(t, testutil.WaitLong) workspace := coderdtest.CreateWorkspace(t, client, template.ID, func(req *codersdk.CreateWorkspaceRequest) { req.Secrets = []codersdk.WorkspaceSecretInput{ {Name: "old-env", Value: new("a"), EnvName: "TOKEN"}, - {Name: "old-file", Value: new("b"), FilePath: "/home/coder/.token"}, + {Name: "old-absolute-file", Value: new("b"), FilePath: "/home//coder/.token"}, + {Name: "old-tilde-file", Value: new("g"), FilePath: "~/.token"}, {Name: "kept", Value: new("c"), EnvName: "KEPT"}, {Name: "both", Value: new("e"), EnvName: "BOTH", FilePath: "/home/coder/.both"}, } }) coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID) + // The extra slash is in the old absolute path and the new tilde path + // so both sides of the file path comparison must handle aliases. // New secrets under different names take over the previous secrets' // env var and file path, so those are not copied forward. Taking over // one target of a secret drops the whole secret. An empty string is @@ -6852,7 +6855,8 @@ func TestWorkspaceSecrets(t *testing.T) { Transition: codersdk.WorkspaceTransitionStart, Secrets: []codersdk.WorkspaceSecretInput{ {Name: "new-env", Value: new(""), EnvName: "TOKEN"}, - {Name: "new-file", Value: new("d"), FilePath: "/home/coder/.token"}, + {Name: "new-absolute-file", Value: new("d"), FilePath: "/home/coder/.token"}, + {Name: "new-tilde-file", Value: new("h"), FilePath: "~//.token"}, {Name: "new-both", Value: new("f"), EnvName: "BOTH"}, }, }) @@ -6860,14 +6864,16 @@ func TestWorkspaceSecrets(t *testing.T) { coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, build2.ID) secrets := listSecrets(ctx, t, build2.ID) - require.Len(t, secrets, 4) + require.Len(t, secrets, 5) require.Contains(t, secrets, "kept") require.NotContains(t, secrets, "old-env") - require.NotContains(t, secrets, "old-file") + require.NotContains(t, secrets, "old-absolute-file") + require.NotContains(t, secrets, "old-tilde-file") require.NotContains(t, secrets, "both", "a partial takeover drops the whole secret") require.True(t, secrets["new-env"].Value.Valid) require.Empty(t, secrets["new-env"].Value.String) - require.Equal(t, "d", secrets["new-file"].Value.String) + require.Equal(t, "d", secrets["new-absolute-file"].Value.String) + require.Equal(t, "h", secrets["new-tilde-file"].Value.String) }) t.Run("BuildSecrets", func(t *testing.T) { @@ -6994,6 +7000,22 @@ func TestWorkspaceSecrets(t *testing.T) { }, field: "secrets[1].file_path", }, + { + name: "AliasedFilePath", + secrets: []codersdk.WorkspaceSecretInput{ + {Name: "a", Value: new("v"), FilePath: "~/.shared"}, + {Name: "b", Value: new("v"), FilePath: "~//.shared"}, + }, + field: "secrets[1].file_path", + }, + { + name: "LiteralTildeDirectoryCollision", + secrets: []codersdk.WorkspaceSecretInput{ + {Name: "a", Value: new("v"), FilePath: "~/../~/x"}, + {Name: "b", Value: new("v"), FilePath: "~/x"}, + }, + field: "secrets[1].file_path", + }, { name: "RemovalWithTarget", secrets: []codersdk.WorkspaceSecretInput{{Name: "missing-value", EnvName: "API_KEY"}}, diff --git a/coderd/workspacesecrets.go b/coderd/workspacesecrets.go index 2a8508457ee..b403a0c25d1 100644 --- a/coderd/workspacesecrets.go +++ b/coderd/workspacesecrets.go @@ -54,13 +54,16 @@ func (api *API) validateWorkspaceSecretInputs(secrets []codersdk.WorkspaceSecret }) } seenEnvNames[secret.EnvName] = struct{}{} - if _, dup := seenFilePaths[secret.FilePath]; dup && secret.FilePath != "" { + key := workspacesecrets.FilePathKey(secret.FilePath) + if _, dup := seenFilePaths[key]; dup { fieldErrs = append(fieldErrs, codersdk.ValidationError{ Field: codersdk.UserSecretFilePathField, Detail: fmt.Sprintf("file_path %q is used by more than one secret in this request.", secret.FilePath), }) } - seenFilePaths[secret.FilePath] = struct{}{} + if key != "" { + seenFilePaths[key] = struct{}{} + } } validations = append(validations, prefixUserSecretValidationErrors(i, fieldErrs)...) } diff --git a/coderd/workspacesecrets/workspacesecrets.go b/coderd/workspacesecrets/workspacesecrets.go index cd94e0457c5..f7dbb62ae59 100644 --- a/coderd/workspacesecrets/workspacesecrets.go +++ b/coderd/workspacesecrets/workspacesecrets.go @@ -52,6 +52,16 @@ func (r Resolved) Delivered() bool { return r.DeliveredEnvName != "" || r.DeliveredFilePath != "" } +// FilePathKey compares secret targets lexically without expanding the agent's +// home directory. In particular, "~/../~/x" and "~/x" collide even though the +// former can point into a directory literally named "~". An empty path is unset. +func FilePathKey(filePath string) string { + if filePath == "" { + return "" + } + return path.Clean(filePath) +} + // Resolve returns the delivery outcome of the user secrets and the workspace // build secrets, each in input order. // @@ -71,7 +81,7 @@ func Resolve(user, build []Secret, policy FilePathPolicy) (resolvedUser, resolve if claim(envOwner, s.EnvName, s.ID, &r.EnvReplacedBy) { r.DeliveredEnvName = s.EnvName } - if policy == FilePathAllowed && s.FilePath != "" && claim(fileOwner, path.Clean(s.FilePath), s.ID, &r.FileReplacedBy) { + if policy == FilePathAllowed && claim(fileOwner, FilePathKey(s.FilePath), s.ID, &r.FileReplacedBy) { r.DeliveredFilePath = s.FilePath } return r diff --git a/coderd/wsbuilder/wsbuilder.go b/coderd/wsbuilder/wsbuilder.go index e6716beff97..9479dd73ba5 100644 --- a/coderd/wsbuilder/wsbuilder.go +++ b/coderd/wsbuilder/wsbuilder.go @@ -34,6 +34,7 @@ import ( "github.com/coder/coder/v2/coderd/tracing" "github.com/coder/coder/v2/coderd/util/ptr" "github.com/coder/coder/v2/coderd/util/slice" + "github.com/coder/coder/v2/coderd/workspacesecrets" "github.com/coder/coder/v2/codersdk" "github.com/coder/coder/v2/provisioner/terraform/tfparse" "github.com/coder/coder/v2/provisionersdk" @@ -628,6 +629,9 @@ func (b *Builder) buildTx(authFunc func(action policy.Action, object rbac.Object // build's agent may still be running until this build's job completes, so // provisionerdserver clears them on completion. // +// The database indexes paths as entered. Aliases from earlier builds can +// carry forward together until a new secret claims their comparison key. +// // Copying forward is bookkeeping, not a user writing secrets, // so it runs as the workspace secret manager. This lets autostart, the build // orchestrator, and other non-user initiators rebuild workspaces that hold @@ -645,8 +649,8 @@ func (b *Builder) persistSecrets(store database.Store, workspaceBuildID uuid.UUI if secret.EnvName != "" { claimedEnvNames[secret.EnvName] = struct{}{} } - if secret.FilePath != "" { - claimedFilePaths[secret.FilePath] = struct{}{} + if key := workspacesecrets.FilePathKey(secret.FilePath); key != "" { + claimedFilePaths[key] = struct{}{} } } @@ -686,7 +690,7 @@ func (b *Builder) persistSecrets(store database.Store, workspaceBuildID uuid.UUI if _, claimed := claimedEnvNames[prev.EnvName]; claimed && prev.EnvName != "" { continue } - if _, claimed := claimedFilePaths[prev.FilePath]; claimed && prev.FilePath != "" { + if _, claimed := claimedFilePaths[workspacesecrets.FilePathKey(prev.FilePath)]; claimed { continue } if err := b.insertSecret(managerCtx, store, workspaceBuildID, codersdk.WorkspaceSecretInput{