From e13eaca7e4d9b2127f3c12222f74c4bf3bfc0a32 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Thu, 27 Aug 2026 12:25:19 +0800 Subject: [PATCH] fix(sandbox): make skill env capture fill blanks only MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Capture existed so a credential a user typed in chat survives the turn. It also overwrote values already stored, inferred the skill from a path in the command, and logged the command that carried the secret — so one hallucinated `export KEY=test` could permanently replace a working key, and the key ended up in the application log either way. Capture now only fills a blank: a name the workspace or the caller has already filled in is dropped before the write path sees it, the skill must be named explicitly, and assignment values are masked in logs. The path the error message advertised did not exist: a required variable was refused before the call's own env was considered, so an IM user, who has no settings page, could never get the skill to run. Values supplied by the call now satisfy the requirement, and the message names the tool that accepts them. Also take envs out of the generic UpdateSkill. Every install-progress write is a read-modify-write of the whole row, and the heartbeat reloads it every 30 seconds, so carrying envs along could put a stale declaration back over one just recorded. Encryption failures abort the write instead of storing plaintext in a queryable JSONB column, matching TenantAPIKey. Reverts an unrelated deletion of the no-volume-support warning. --- .../components/SandboxConfigEditorDrawer.vue | 3 + internal/agent/skills/env_resolver.go | 11 ++- internal/agent/tools/shell_env_extract.go | 46 +++------- .../agent/tools/shell_env_extract_test.go | 29 +++---- internal/agent/tools/shell_exec.go | 66 +++++++++++--- internal/agent/tools/shell_exec_test.go | 85 +++++++++++++++++- .../application/repository/tenant_skill.go | 55 +++++++++++- .../repository/tenant_skill_test.go | 50 +++++++++-- internal/application/service/agent_service.go | 2 +- .../application/service/tenant_skill_admin.go | 7 +- .../service/tenant_skill_admin_test.go | 5 +- .../service/tenant_skill_install.go | 4 +- .../service/tenant_skill_install_test.go | 86 ++++++++++++++++--- .../service/tenant_skill_reaper_test.go | 24 ++++++ internal/application/service/user_env.go | 17 ++-- internal/application/service/user_env_test.go | 14 ++- internal/types/tenant_env_vars.go | 42 ++++++--- internal/types/tenant_env_vars_test.go | 26 ++++++ migrations/versioned/000089_env_vars.up.sql | 4 +- 19 files changed, 455 insertions(+), 121 deletions(-) diff --git a/frontend/src/components/SandboxConfigEditorDrawer.vue b/frontend/src/components/SandboxConfigEditorDrawer.vue index 35d1833e..97b7ce14 100644 --- a/frontend/src/components/SandboxConfigEditorDrawer.vue +++ b/frontend/src/components/SandboxConfigEditorDrawer.vue @@ -472,6 +472,9 @@

{{ $t('settings.sandbox.checkPendingHint', { names: pendingCheckNames.join('、') }) }}

+ diff --git a/internal/agent/skills/env_resolver.go b/internal/agent/skills/env_resolver.go index 89044787..5418dfe3 100644 --- a/internal/agent/skills/env_resolver.go +++ b/internal/agent/skills/env_resolver.go @@ -34,11 +34,16 @@ type MissingSkillEnvError struct { func (e *MissingSkillEnvError) Error() string { // English, like every other error in this codebase: the agent relays this // to the user and translates it into whatever language they are speaking. + // execute_skill_script has no env parameter, so it cannot take a value the + // user just typed. shell_exec can: naming the skill and passing the value + // in env runs the command and stores the value for the next run. Pointing + // at the settings page alone would strand IM users, who have no such page. return fmt.Sprintf( "skill %q needs the environment variable(s) %s, which nobody has set yet. "+ - "Set them under Settings → Environment variables and try again, "+ - "or just tell me the env directly.", - e.SkillName, strings.Join(e.Names, ", "), + "Ask the user for them, then run the skill through shell_exec with "+ + "skill_name=%q and the values in env — they are stored for that user "+ + "afterwards. They can also be set under Settings → Environment variables.", + e.SkillName, strings.Join(e.Names, ", "), e.SkillName, ) } diff --git a/internal/agent/tools/shell_env_extract.go b/internal/agent/tools/shell_env_extract.go index a10e3459..4ca3171e 100644 --- a/internal/agent/tools/shell_env_extract.go +++ b/internal/agent/tools/shell_env_extract.go @@ -3,9 +3,6 @@ package tools import ( "regexp" "strings" - "unicode" - - "github.com/Tencent/WeKnora/internal/sandbox" ) // assignmentPattern finds NAME=value in a model-built shell command. @@ -39,37 +36,16 @@ func collectUsedSkillEnv(command string, toolEnv map[string]string) map[string]s return out } -func inferSkillName(explicit, command string) string { - if sandbox.IsValidSkillName(strings.TrimSpace(explicit)) { - return strings.TrimSpace(explicit) - } - prefix := sandbox.SkillsImageRoot + "/" - from := 0 - for { - idx := strings.Index(command[from:], prefix) - if idx < 0 { - return "" +// maskCommandAssignments replaces the value of every NAME=value assignment with +// a placeholder. A command is logged at Info, and passing a credential inline is +// a documented way to hand a skill its key, so the raw string must never reach +// the log. +func maskCommandAssignments(command string) string { + return assignmentPattern.ReplaceAllStringFunc(command, func(match string) string { + eq := strings.Index(match, "=") + if eq < 0 { + return match } - idx += from - name, ok := firstPathSegment(command[idx+len(prefix):]) - if ok && sandbox.IsValidSkillName(name) { - return name - } - from = idx + len(prefix) - } -} - -func firstPathSegment(rest string) (string, bool) { - end := 0 - for end < len(rest) { - r := rune(rest[end]) - if r == '/' || unicode.IsSpace(r) || r == '"' || r == '\'' || r == ';' || r == '&' || r == '|' { - break - } - end++ - } - if end == 0 { - return "", false - } - return rest[:end], true + return match[:eq+1] + "***" + }) } diff --git a/internal/agent/tools/shell_env_extract_test.go b/internal/agent/tools/shell_env_extract_test.go index cdb3111b..aae72e5b 100644 --- a/internal/agent/tools/shell_env_extract_test.go +++ b/internal/agent/tools/shell_env_extract_test.go @@ -44,24 +44,15 @@ func TestCollectUsedSkillEnvOverlaysToolEnvOnExports(t *testing.T) { assert.Equal(t, "e", got["EXTRA"]) } -func TestInferSkillNamePrefersExplicitToolField(t *testing.T) { - command := `cd /opt/weknora/tenant/skills/other-skill && python x.py` +func TestMaskCommandAssignmentsHidesValues(t *testing.T) { + command := `export BIGMODEL_API_KEY="sk-abc.def"; TOKEN=plain ./run --model cogview-4` - assert.Equal(t, "pdf-tools", inferSkillName("pdf-tools", command)) -} - -func TestInferSkillNameFromCdIntoSkillsRoot(t *testing.T) { - command := `cd /opt/weknora/tenant/skills/bigmodel-image-video && .venv/bin/python scripts/generate.py "草原"` - - assert.Equal(t, "bigmodel-image-video", inferSkillName("", command)) -} - -func TestInferSkillNameFromScriptPath(t *testing.T) { - command := `/opt/weknora/tenant/skills/bigmodel-image-video/.venv/bin/python /opt/weknora/tenant/skills/bigmodel-image-video/scripts/generate.py` - - assert.Equal(t, "bigmodel-image-video", inferSkillName("", command)) -} - -func TestInferSkillNameEmptyWhenCommandDoesNotTouchSkillsRoot(t *testing.T) { - assert.Empty(t, inferSkillName("", "ls /workspace")) + got := maskCommandAssignments(command) + + assert.NotContains(t, got, "sk-abc.def") + assert.NotContains(t, got, "plain") + assert.Contains(t, got, "BIGMODEL_API_KEY=***") + assert.Contains(t, got, "TOKEN=***") + // Ordinary flags stay readable: a masked log still has to be a usable one. + assert.Contains(t, got, "--model cogview-4") } diff --git a/internal/agent/tools/shell_exec.go b/internal/agent/tools/shell_exec.go index 36a892ed..8ea7c2ac 100644 --- a/internal/agent/tools/shell_exec.go +++ b/internal/agent/tools/shell_exec.go @@ -361,7 +361,8 @@ func (t *ShellExecTool) Execute(ctx context.Context, args json.RawMessage) (*typ }, nil } if reason := checkShellExecBlacklist(command); reason != "" { - logger.Warnf(ctx, "[Tool][ShellExec] rejected by blacklist: %s command=%q", reason, command) + logger.Warnf(ctx, "[Tool][ShellExec] rejected by blacklist: %s command=%q", + reason, maskCommandAssignments(command)) return &types.ToolResult{ Success: false, Error: fmt.Sprintf("command rejected by shell_exec safety guard: %s", reason), @@ -404,13 +405,18 @@ func (t *ShellExecTool) Execute(ctx context.Context, args json.RawMessage) (*typ } logger.Infof(ctx, "[Tool][ShellExec] session=%s work_dir=%s timeout=%s command=%q", - sessionID, workDir, timeout, command) + sessionID, workDir, timeout, maskCommandAssignments(command)) // The caller's config-wide variables apply to every command; a skill's // declared credentials are added only when the model names the skill. // Values come from ctx (the current principal), live only for this process, // and are overlaid without displacing anything the model passed via env. env := input.Env + // supplied carries the values this one call brings with it, from the env + // parameter and from NAME=value assignments in the command. They satisfy a + // required variable that is not stored yet, which is what makes "tell me + // the key in chat" work, and they are the only values capture may persist. + supplied := collectUsedSkillEnv(input.Command, input.Env) if t.envResolver != nil { resolved, missing, rerr := t.envResolver.ResolveEnv(ctx, input.SkillName) if rerr != nil { @@ -419,12 +425,14 @@ func (t *ShellExecTool) Execute(ctx context.Context, args json.RawMessage) (*typ Error: fmt.Sprintf("failed to resolve environment variables: %v", rerr), }, nil } + missing = stillMissing(missing, supplied) if len(missing) > 0 { return &types.ToolResult{ Success: false, Error: fmt.Sprintf( "skill %q needs the environment variable(s) %s, which nobody has set yet. "+ - "Set them under Settings → Environment variables and try again.", + "Ask the user for them and pass them in this call's env, "+ + "or have them set the values under Settings → Environment variables.", input.SkillName, strings.Join(missing, ", ")), }, nil } @@ -432,6 +440,10 @@ func (t *ShellExecTool) Execute(ctx context.Context, args json.RawMessage) (*typ env = make(map[string]string) } overlayResolvedEnv(env, resolved) + // A name that already resolved is one the workspace or this caller has + // filled in. Capture must not touch it: otherwise a hallucinated + // `export KEY=test` would overwrite a working stored credential. + supplied = dropResolvedNames(supplied, resolved) } res, err := t.executor.ExecShellCommand(ctx, sessionID, command, workDir, timeout, env) if err != nil { @@ -443,7 +455,7 @@ func (t *ShellExecTool) Execute(ctx context.Context, args json.RawMessage) (*typ }, nil } - t.maybeCaptureSkillEnv(ctx, input, res) + t.maybeCaptureSkillEnv(ctx, input.SkillName, supplied, res) outputLimit := resolveShellOutputLimit(input.MaxOutputBytes) stderrLimit := resolveShellStderrLimit(input.MaxStderrBytes) @@ -540,8 +552,15 @@ func (t *ShellExecTool) Execute(ctx context.Context, args json.RawMessage) (*typ }, nil } +// maybeCaptureSkillEnv persists the credentials this call brought with it, so +// the next run of the same skill does not have to ask again. +// +// The skill must be named explicitly: inferring it from a path in the command +// would let any successful command that merely mentions a skill directory write +// into that skill's credentials. pairs has already had every resolved name +// removed, so this only ever fills a blank. func (t *ShellExecTool) maybeCaptureSkillEnv( - ctx context.Context, input ShellExecInput, res *sandbox.ExecuteResult, + ctx context.Context, skillName string, pairs map[string]string, res *sandbox.ExecuteResult, ) { if t == nil || t.envCapture == nil || t.isInstallMode() { return @@ -549,17 +568,42 @@ func (t *ShellExecTool) maybeCaptureSkillEnv( if res == nil || res.ExitCode != 0 { return } - skillName := inferSkillName(input.SkillName, input.Command) - if skillName == "" { - return - } - pairs := collectUsedSkillEnv(input.Command, input.Env) - if len(pairs) == 0 { + skillName = strings.TrimSpace(skillName) + if !sandbox.IsValidSkillName(skillName) || len(pairs) == 0 { return } t.envCapture(ctx, skillName, pairs) } +// stillMissing removes from missing every name this call supplied itself. +func stillMissing(missing []string, supplied map[string]string) []string { + if len(missing) == 0 || len(supplied) == 0 { + return missing + } + out := missing[:0:0] + for _, name := range missing { + if strings.TrimSpace(supplied[name]) == "" { + out = append(out, name) + } + } + return out +} + +// dropResolvedNames returns the supplied values that nothing has stored yet. +func dropResolvedNames(supplied, resolved map[string]string) map[string]string { + if len(supplied) == 0 || len(resolved) == 0 { + return supplied + } + out := make(map[string]string, len(supplied)) + for name, value := range supplied { + if _, stored := resolved[name]; stored { + continue + } + out[name] = value + } + return out +} + func (t *ShellExecTool) isInstallMode() bool { for _, root := range t.workDirRoots { if root == sandbox.SkillsImageRoot { diff --git a/internal/agent/tools/shell_exec_test.go b/internal/agent/tools/shell_exec_test.go index 43926ec1..24b4da00 100644 --- a/internal/agent/tools/shell_exec_test.go +++ b/internal/agent/tools/shell_exec_test.go @@ -211,12 +211,26 @@ func (r *recordedCapture) capture(_ context.Context, skillName string, pairs map r.pairs = pairs } +// stubEnvResolver stands in for the service-layer resolver: resolved is what +// the workspace or this caller already has stored, missing is what a required +// declaration still needs. +type stubEnvResolver struct { + resolved map[string]string + missing []string +} + +func (r stubEnvResolver) ResolveEnv( + _ context.Context, _ string, +) (map[string]string, []string, error) { + return r.resolved, r.missing, nil +} + func TestShellExecCapturesUsedEnvAfterSuccessfulCommand(t *testing.T) { recorder := &recordedCapture{} tool := NewShellExecTool(&fakeShellExecutor{}, nil).WithEnvCapture(recorder.capture) result, err := tool.Execute(shellExecTestContext(), json.RawMessage( - `{"command":"export USER_TOKEN=from-command; cd /opt/weknora/tenant/skills/pdf-tools && python x.py","env":{"EXTRA_TOKEN":"from-tool"}}`, + `{"command":"export USER_TOKEN=from-command; python x.py","skill_name":"pdf-tools","env":{"EXTRA_TOKEN":"from-tool"}}`, )) require.NoError(t, err) @@ -227,13 +241,76 @@ func TestShellExecCapturesUsedEnvAfterSuccessfulCommand(t *testing.T) { require.Equal(t, "from-tool", recorder.pairs["EXTRA_TOKEN"]) } +// Guessing the skill from a path would let any successful command that merely +// mentions a skill directory write into that skill's credentials. +func TestShellExecDoesNotCaptureWithoutAnExplicitSkillName(t *testing.T) { + recorder := &recordedCapture{} + tool := NewShellExecTool(&fakeShellExecutor{}, nil).WithEnvCapture(recorder.capture) + + result, err := tool.Execute(shellExecTestContext(), json.RawMessage( + `{"command":"export USER_TOKEN=from-command; cd /opt/weknora/tenant/skills/pdf-tools && python x.py"}`, + )) + + require.NoError(t, err) + require.True(t, result.Success) + require.Zero(t, recorder.calls) +} + +// A stored value belongs to whoever entered it. Capture may fill a blank, so a +// name that resolved is never handed to the write path. +func TestShellExecDoesNotCaptureAlreadyResolvedNames(t *testing.T) { + recorder := &recordedCapture{} + resolver := stubEnvResolver{resolved: map[string]string{"USER_TOKEN": "stored"}} + tool := NewShellExecTool(&fakeShellExecutor{}, resolver).WithEnvCapture(recorder.capture) + + result, err := tool.Execute(shellExecTestContext(), json.RawMessage( + `{"command":"python x.py","skill_name":"pdf-tools","env":{"USER_TOKEN":"model-made-this-up","NEW_TOKEN":"fresh"}}`, + )) + + require.NoError(t, err) + require.True(t, result.Success) + require.Equal(t, 1, recorder.calls) + require.NotContains(t, recorder.pairs, "USER_TOKEN") + require.Equal(t, "fresh", recorder.pairs["NEW_TOKEN"]) +} + +// Refusing a required variable the call itself carries would strand every user +// without a settings page, since the value can only arrive through the chat. +func TestShellExecRunsWhenTheCallSuppliesTheMissingRequiredValue(t *testing.T) { + recorder := &recordedCapture{} + resolver := stubEnvResolver{missing: []string{"USER_TOKEN"}} + tool := NewShellExecTool(&fakeShellExecutor{}, resolver).WithEnvCapture(recorder.capture) + + result, err := tool.Execute(shellExecTestContext(), json.RawMessage( + `{"command":"python x.py","skill_name":"pdf-tools","env":{"USER_TOKEN":"typed-in-chat"}}`, + )) + + require.NoError(t, err) + require.True(t, result.Success) + require.Equal(t, "typed-in-chat", recorder.pairs["USER_TOKEN"], + "the value that made the run possible is the one worth storing") +} + +func TestShellExecStillRefusesAMissingRequiredValueNobodySupplied(t *testing.T) { + resolver := stubEnvResolver{missing: []string{"USER_TOKEN"}} + tool := NewShellExecTool(&fakeShellExecutor{}, resolver) + + result, err := tool.Execute(shellExecTestContext(), json.RawMessage( + `{"command":"python x.py","skill_name":"pdf-tools"}`, + )) + + require.NoError(t, err) + require.False(t, result.Success) + require.Contains(t, result.Error, "USER_TOKEN") +} + func TestShellExecDoesNotCaptureWhenCommandFails(t *testing.T) { recorder := &recordedCapture{} tool := NewShellExecTool(&fakeShellExecutor{result: &sandbox.ExecuteResult{ExitCode: 1}}, nil). WithEnvCapture(recorder.capture) result, err := tool.Execute(shellExecTestContext(), json.RawMessage( - `{"command":"export USER_TOKEN=x; cd /opt/weknora/tenant/skills/pdf-tools && false"}`, + `{"command":"export USER_TOKEN=x; false","skill_name":"pdf-tools"}`, )) require.NoError(t, err) @@ -247,7 +324,7 @@ func TestShellExecDoesNotCaptureWhenExecutorErrors(t *testing.T) { WithEnvCapture(recorder.capture) result, err := tool.Execute(shellExecTestContext(), json.RawMessage( - `{"command":"export USER_TOKEN=x; cd /opt/weknora/tenant/skills/pdf-tools && true"}`, + `{"command":"export USER_TOKEN=x; true","skill_name":"pdf-tools"}`, )) require.NoError(t, err) @@ -260,7 +337,7 @@ func TestInstallShellExecDoesNotCapture(t *testing.T) { tool := NewInstallShellExecTool(&fakeInstallShellExecutor{}).WithEnvCapture(recorder.capture) result, err := tool.Execute(shellExecTestContext(), json.RawMessage( - `{"command":"export USER_TOKEN=x; cd /opt/weknora/tenant/skills/pdf-tools && pip install x"}`, + `{"command":"export USER_TOKEN=x; pip install x","skill_name":"pdf-tools"}`, )) require.NoError(t, err) diff --git a/internal/application/repository/tenant_skill.go b/internal/application/repository/tenant_skill.go index ce6c285d..6fc39e90 100644 --- a/internal/application/repository/tenant_skill.go +++ b/internal/application/repository/tenant_skill.go @@ -25,8 +25,22 @@ type TenantSkillRepository interface { // ListSkillsByTenant spans every sandbox config of one workspace, for the // views that are about the user rather than about a config. ListSkillsByTenant(ctx context.Context, tenantID uint64) ([]*types.TenantSkillEntity, error) - // UpdateSkill writes the mutable projection fields after install/remove state changes. + // UpdateSkill writes the mutable projection fields after install/remove + // state changes. It never touches envs; see the implementation. UpdateSkill(ctx context.Context, e *types.TenantSkillEntity) error + // UpdateSkillEnvs writes the declared environment variables alone. + UpdateSkillEnvs( + ctx context.Context, tenantID uint64, configID, skillID string, envs types.SkillEnvVars, + ) error + // UpdateSkillAdminState writes visibility and declared values together, as + // one admin request. + UpdateSkillAdminState( + ctx context.Context, + tenantID uint64, + configID, skillID string, + enabled bool, + envs types.SkillEnvVars, + ) error // DeleteSkill soft-deletes the metadata row and, in the same transaction, // hard-deletes the per-principal env values hanging off it. Image cleanup is // represented by snapshots. When the scoped key matches no skill, nothing is @@ -142,6 +156,13 @@ func (r *tenantSkillRepository) ListSkillsByTenant( // UpdateSkill writes the mutable columns explicitly so a zero-valued field on // the passed entity cannot silently wipe state written by a concurrent job. +// +// envs is deliberately absent: it is the one column an install-progress write +// has no opinion about, and every such write is a read-modify-write of a row +// the install heartbeat reloads every 30 seconds. Including it would let a +// heartbeat that read the row a moment before a declaration was recorded put +// the stale list — or a NULL — back. UpdateSkillEnvs and UpdateSkillAdminState +// are the only writers of that column. func (r *tenantSkillRepository) UpdateSkill(ctx context.Context, e *types.TenantSkillEntity) error { return r.db.WithContext(ctx). Model(&types.TenantSkillEntity{}). @@ -154,7 +175,6 @@ func (r *tenantSkillRepository) UpdateSkill(ctx context.Context, e *types.Tenant "bundle_ref": e.BundleRef, "bundle_sha256": e.BundleSHA256, "enabled": e.Enabled, - "envs": e.Envs, "installed_snapshot_id": e.InstalledSnapshotID, "install_session_id": e.InstallSessionID, "install_message_id": e.InstallMessageID, @@ -165,6 +185,37 @@ func (r *tenantSkillRepository) UpdateSkill(ctx context.Context, e *types.Tenant }).Error } +// UpdateSkillEnvs writes the declaration column alone, so recording what a +// skill needs cannot disturb the install state written next to it. +func (r *tenantSkillRepository) UpdateSkillEnvs( + ctx context.Context, tenantID uint64, configID, skillID string, envs types.SkillEnvVars, +) error { + return r.db.WithContext(ctx). + Model(&types.TenantSkillEntity{}). + Where("tenant_id = ? AND sandbox_config_id = ? AND id = ?", tenantID, configID, skillID). + Updates(map[string]any{"envs": envs, "updated_at": time.Now()}).Error +} + +// UpdateSkillAdminState writes the two columns an admin request owns, together, +// so a rotation that disables the skill and clears its value is one statement +// rather than two that can half apply. +func (r *tenantSkillRepository) UpdateSkillAdminState( + ctx context.Context, + tenantID uint64, + configID, skillID string, + enabled bool, + envs types.SkillEnvVars, +) error { + return r.db.WithContext(ctx). + Model(&types.TenantSkillEntity{}). + Where("tenant_id = ? AND sandbox_config_id = ? AND id = ?", tenantID, configID, skillID). + Updates(map[string]any{ + "enabled": enabled, + "envs": envs, + "updated_at": time.Now(), + }).Error +} + // DeleteSkill soft-deletes the skill and hard-deletes the user values that // belonged to it, in one transaction. The cleanup cannot be a cascading foreign // key: tenant_skills is soft-deleted, so ON DELETE CASCADE would never fire and diff --git a/internal/application/repository/tenant_skill_test.go b/internal/application/repository/tenant_skill_test.go index cf9f47f5..3beb3e69 100644 --- a/internal/application/repository/tenant_skill_test.go +++ b/internal/application/repository/tenant_skill_test.go @@ -186,19 +186,16 @@ func userEnvRow(p types.Principal, skillID, name, value string) *types.TenantUse } } -func TestSkillRepoUpdatePersistsEnvs(t *testing.T) { +func TestSkillRepoUpdateSkillEnvsPersistsTheDeclarationEncrypted(t *testing.T) { t.Setenv("SYSTEM_AES_KEY", skillEnvTestAESKey) repo, db := newSkillTestRepoWithDB(t) ctx := context.Background() require.NoError(t, repo.CreateSkill(ctx, skillRow("sk-a", "cfg-1", "pdf"))) - row, err := repo.GetSkill(ctx, 7, "cfg-1", "sk-a") - require.NoError(t, err) - row.Envs = types.SkillEnvVars{ + require.NoError(t, repo.UpdateSkillEnvs(ctx, 7, "cfg-1", "sk-a", types.SkillEnvVars{ {Name: "TAVILY_API_KEY", Description: "search key", Required: true, Value: "tvly-admin"}, {Name: "OPTIONAL_KEY"}, - } - require.NoError(t, repo.UpdateSkill(ctx, row)) + })) got, err := repo.GetSkill(ctx, 7, "cfg-1", "sk-a") require.NoError(t, err) @@ -214,6 +211,47 @@ func TestSkillRepoUpdatePersistsEnvs(t *testing.T) { require.Contains(t, stored, "TAVILY_API_KEY", "the declaration stays readable without a key") } +// The install heartbeat rewrites the whole row from a copy it read seconds +// earlier. If UpdateSkill carried envs, that copy would undo a declaration or +// an admin value stored in between. +func TestSkillRepoUpdateSkillLeavesEnvsAlone(t *testing.T) { + t.Setenv("SYSTEM_AES_KEY", skillEnvTestAESKey) + repo, _ := newSkillTestRepoWithDB(t) + ctx := context.Background() + require.NoError(t, repo.CreateSkill(ctx, skillRow("sk-a", "cfg-1", "pdf"))) + + stale, err := repo.GetSkill(ctx, 7, "cfg-1", "sk-a") + require.NoError(t, err) + require.NoError(t, repo.UpdateSkillEnvs(ctx, 7, "cfg-1", "sk-a", types.SkillEnvVars{ + {Name: "TAVILY_API_KEY", Required: true, Value: "tvly-admin"}, + })) + + stale.Status = types.SkillStatusInstalling + require.NoError(t, repo.UpdateSkill(ctx, stale)) + + got, err := repo.GetSkill(ctx, 7, "cfg-1", "sk-a") + require.NoError(t, err) + require.Equal(t, types.SkillStatusInstalling, got.Status) + require.Len(t, got.Envs, 1) + require.Equal(t, "tvly-admin", got.Envs[0].Value) +} + +func TestSkillRepoUpdateSkillAdminStateWritesBothColumns(t *testing.T) { + t.Setenv("SYSTEM_AES_KEY", skillEnvTestAESKey) + repo, _ := newSkillTestRepoWithDB(t) + ctx := context.Background() + require.NoError(t, repo.CreateSkill(ctx, skillRow("sk-a", "cfg-1", "pdf"))) + + require.NoError(t, repo.UpdateSkillAdminState(ctx, 7, "cfg-1", "sk-a", false, + types.SkillEnvVars{{Name: "TAVILY_API_KEY", Value: "tvly-admin"}})) + + got, err := repo.GetSkill(ctx, 7, "cfg-1", "sk-a") + require.NoError(t, err) + require.False(t, got.Enabled) + require.Len(t, got.Envs, 1) + require.Equal(t, "tvly-admin", got.Envs[0].Value) +} + func TestUpsertUserEnvIsIdempotentPerPrincipalSkillAndName(t *testing.T) { t.Setenv("SYSTEM_AES_KEY", skillEnvTestAESKey) repo, db := newSkillTestRepoWithDB(t) diff --git a/internal/application/service/agent_service.go b/internal/application/service/agent_service.go index 02a29a90..e0b48b97 100644 --- a/internal/application/service/agent_service.go +++ b/internal/application/service/agent_service.go @@ -608,7 +608,7 @@ func (s *agentService) registerSandboxShellTool( return } if executor := sessionSandboxShellExecutor(sandboxMgr); executor != nil { - resolver := s.userEnvResolver(ctx, config) // 现取,可能为 nil + resolver := s.userEnvResolver(ctx, config) toolRegistry.RegisterTool( tools.NewShellExecTool(executor, resolver).WithEnvCapture(s.skillEnvCapture(config)), ) diff --git a/internal/application/service/tenant_skill_admin.go b/internal/application/service/tenant_skill_admin.go index 6eff88c2..3aea6f7b 100644 --- a/internal/application/service/tenant_skill_admin.go +++ b/internal/application/service/tenant_skill_admin.go @@ -118,7 +118,12 @@ func (s *TenantSkillService) UpdateSkillAdmin( if !changed { return skill, nil } - if err := s.skills.UpdateSkill(ctx, skill); err != nil { + // Only the two admin-owned columns are written: the row may be mid-install, + // and the status the installer is keeping there is none of this request's + // business. + if err := s.skills.UpdateSkillAdminState( + ctx, tenantID, configID, skillID, skill.Enabled, skill.Envs, + ); err != nil { return nil, err } return skill, nil diff --git a/internal/application/service/tenant_skill_admin_test.go b/internal/application/service/tenant_skill_admin_test.go index ec2c87b5..06b131eb 100644 --- a/internal/application/service/tenant_skill_admin_test.go +++ b/internal/application/service/tenant_skill_admin_test.go @@ -93,10 +93,7 @@ func TestSetSkillEnabledReturnsNilForAnotherWorkspace(t *testing.T) { // finished install would have left it. func seedSkillEnvDeclaration(t *testing.T, fx *installFixture, envs types.SkillEnvVars) { t.Helper() - skill, err := fx.skillRepo.GetSkill(context.Background(), 7, "cfg-1", "sk-1") - require.NoError(t, err) - skill.Envs = envs - require.NoError(t, fx.skillRepo.UpdateSkill(context.Background(), skill)) + require.NoError(t, fx.skillRepo.UpdateSkillEnvs(context.Background(), 7, "cfg-1", "sk-1", envs)) } // A name outside the declaration is ignored rather than rejected: a stale UI diff --git a/internal/application/service/tenant_skill_install.go b/internal/application/service/tenant_skill_install.go index ce493542..3290952f 100644 --- a/internal/application/service/tenant_skill_install.go +++ b/internal/application/service/tenant_skill_install.go @@ -1698,8 +1698,8 @@ func (s *TenantSkillService) recordEnvDeclaration( logger.Warnf(ctx, "[skill] load %s to store its env declaration failed: %v", skillID, err) return } - skill.Envs = mergeEnvDeclaration(skill.Envs, envs) - if err := s.skills.UpdateSkill(ctx, skill); err != nil { + merged := mergeEnvDeclaration(skill.Envs, envs) + if err := s.skills.UpdateSkillEnvs(ctx, tenantID, configID, skillID, merged); err != nil { logger.Warnf(ctx, "[skill] store the env declaration of %s failed: %v", skillID, err) } } diff --git a/internal/application/service/tenant_skill_install_test.go b/internal/application/service/tenant_skill_install_test.go index 3ed231f3..8484ab93 100644 --- a/internal/application/service/tenant_skill_install_test.go +++ b/internal/application/service/tenant_skill_install_test.go @@ -355,12 +355,10 @@ func TestRunInstallSucceedsWhenEveryDeclaredEnvVarIsRejected(t *testing.T) { func TestRunInstallReinstallKeepsTheAdminValueAndDropsStaleVars(t *testing.T) { fx := newInstallFixture(t) fx.readsTavilyKey() - require.NoError(t, fx.svc.updateSkillFields(context.Background(), 7, "cfg-1", "sk-1", - func(e *types.TenantSkillEntity) { - e.Envs = types.SkillEnvVars{ - {Name: "TAVILY_API_KEY", Description: "old text", Value: "tvly-typed-by-admin"}, - {Name: "LEGACY_TOKEN", Required: true, Value: "stale"}, - } + require.NoError(t, fx.skillRepo.UpdateSkillEnvs(context.Background(), 7, "cfg-1", "sk-1", + types.SkillEnvVars{ + {Name: "TAVILY_API_KEY", Description: "old text", Value: "tvly-typed-by-admin"}, + {Name: "LEGACY_TOKEN", Required: true, Value: "stale"}, })) fx.declareEnvFile(`{"env":[{"name":"TAVILY_API_KEY","description":"new text","required":true}]}`) @@ -420,10 +418,8 @@ func TestRunInstallReinstallOnlyClearsEnvsForAnExplicitEmptyDeclaration(t *testi for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { fx := newInstallFixture(t) - require.NoError(t, fx.svc.updateSkillFields(context.Background(), 7, "cfg-1", "sk-1", - func(e *types.TenantSkillEntity) { - e.Envs = storedAdminEnv() - })) + require.NoError(t, fx.skillRepo.UpdateSkillEnvs( + context.Background(), 7, "cfg-1", "sk-1", storedAdminEnv())) if tc.writeFile { fx.declareEnvFile(tc.body) } @@ -841,6 +837,28 @@ func TestBeatInstallHeartbeatRestampsOnlyAnInstallingRow(t *testing.T) { "a row that left the installing status must not be stamped alive again") } +// The heartbeat reloads and rewrites the whole row every 30 seconds while an +// install runs, and a declaration or an admin value can be stored in between. +// Writing envs from that stale copy would put the old list back. +func TestBeatInstallHeartbeatLeavesTheDeclarationAlone(t *testing.T) { + fx := newInstallFixture(t) + ctx := context.Background() + stale := fx.now().Add(-time.Hour) + require.NoError(t, fx.svc.updateSkillFields(ctx, 7, "cfg-1", "sk-1", + func(e *types.TenantSkillEntity) { e.InstallingSince = &stale })) + + // Stands in for the beat that read the row before the declaration landed. + before, err := fx.skillRepo.GetSkill(ctx, 7, "cfg-1", "sk-1") + require.NoError(t, err) + require.NoError(t, fx.skillRepo.UpdateSkillEnvs(ctx, 7, "cfg-1", "sk-1", storedAdminEnv())) + require.NoError(t, fx.skillRepo.UpdateSkill(ctx, before)) + + skill, err := fx.skillRepo.GetSkill(ctx, 7, "cfg-1", "sk-1") + require.NoError(t, err) + require.Equal(t, storedAdminEnv(), skill.Envs, + "an install-progress write must not carry an old declaration back") +} + func TestStartInstallHeartbeatBeatsUntilStopped(t *testing.T) { fx := newInstallFixture(t) ctx := context.Background() @@ -1959,8 +1977,54 @@ func (r *installSkillRepo) UpdateSkill(ctx context.Context, e *types.TenantSkill if r.updateFailsWhen != nil && r.updateFailsWhen(e) { return errUpdateBoom } + key := skillKey(e.TenantID, e.SandboxConfigID, e.ID) cp := *e - r.skills[skillKey(e.TenantID, e.SandboxConfigID, e.ID)] = &cp + // The real UpdateSkill leaves the envs column alone, so a stale in-memory + // copy cannot put an old declaration back. The fake has to model that or + // the tests would pass on behaviour production does not have. + if stored := r.skills[key]; stored != nil { + cp.Envs = stored.Envs + } else { + cp.Envs = nil + } + r.skills[key] = &cp + return nil +} + +func (r *installSkillRepo) UpdateSkillEnvs( + ctx context.Context, tenantID uint64, configID, skillID string, envs types.SkillEnvVars, +) error { + if err := ctx.Err(); err != nil { + return err + } + r.mu.Lock() + defer r.mu.Unlock() + stored := r.skills[skillKey(tenantID, configID, skillID)] + if stored == nil { + return nil + } + stored.Envs = envs + return nil +} + +func (r *installSkillRepo) UpdateSkillAdminState( + ctx context.Context, + tenantID uint64, + configID, skillID string, + enabled bool, + envs types.SkillEnvVars, +) error { + if err := ctx.Err(); err != nil { + return err + } + r.mu.Lock() + defer r.mu.Unlock() + stored := r.skills[skillKey(tenantID, configID, skillID)] + if stored == nil { + return nil + } + stored.Enabled = enabled + stored.Envs = envs return nil } diff --git a/internal/application/service/tenant_skill_reaper_test.go b/internal/application/service/tenant_skill_reaper_test.go index 1a34d9ce..0029ea72 100644 --- a/internal/application/service/tenant_skill_reaper_test.go +++ b/internal/application/service/tenant_skill_reaper_test.go @@ -781,10 +781,34 @@ func (r *reaperSkillStore) GetSkill( func (r *reaperSkillStore) UpdateSkill(_ context.Context, e *types.TenantSkillEntity) error { cp := *e + if stored := r.rows[e.ID]; stored != nil { + cp.Envs = stored.Envs + } else { + cp.Envs = nil + } r.rows[e.ID] = &cp return nil } +func (r *reaperSkillStore) UpdateSkillEnvs( + _ context.Context, _ uint64, _, skillID string, envs types.SkillEnvVars, +) error { + if stored := r.rows[skillID]; stored != nil { + stored.Envs = envs + } + return nil +} + +func (r *reaperSkillStore) UpdateSkillAdminState( + _ context.Context, _ uint64, _, skillID string, enabled bool, envs types.SkillEnvVars, +) error { + if stored := r.rows[skillID]; stored != nil { + stored.Enabled = enabled + stored.Envs = envs + } + return nil +} + func (r *reaperSkillStore) ListSnapshotsByConfig( _ context.Context, tenantID uint64, configID string, ) ([]*types.TenantSkillSnapshotEntity, error) { diff --git a/internal/application/service/user_env.go b/internal/application/service/user_env.go index ddea21cf..80cdc477 100644 --- a/internal/application/service/user_env.go +++ b/internal/application/service/user_env.go @@ -223,10 +223,15 @@ func applyOwnValue(view *EnvVarView, own *types.TenantUserEnvVar) { } // CaptureSkillEnv stores values a successful skill run already used, for the -// current principal only. It writes only names that skill declared; reserved -// names and a value that already matches the stored user row or the workspace -// admin value are skipped. Unknown or unusable skills are a no-op so a -// mis-inferred name cannot create a row. +// current principal only. It writes only names that skill declared, and only +// names nothing has filled in yet: a value already stored by this caller or by +// the workspace is left alone. +// +// Filling blanks is the whole contract. Rotation belongs to the settings page, +// where a person chose the new value; a value read out of a command the model +// composed is not that, and letting it overwrite a working credential would +// turn one hallucinated `export KEY=test` into a permanently broken skill. +// Unknown or unusable skills are a no-op. func (s *UserEnvService) CaptureSkillEnv( ctx context.Context, configID, skillName string, pairs map[string]string, ) error { @@ -269,10 +274,10 @@ func (s *UserEnvService) CaptureSkillEnv( if !ok { continue } - if declared.Value != "" && declared.Value == value { + if declared.Value != "" { continue } - if existing := mine[name]; existing != nil && existing.Value == value { + if existing := mine[name]; existing != nil && existing.Value != "" { continue } if err := s.upsertMine(ctx, tenantID, principal, configID, skill.ID, name, value); err != nil { diff --git a/internal/application/service/user_env_test.go b/internal/application/service/user_env_test.go index faee2502..733aeddc 100644 --- a/internal/application/service/user_env_test.go +++ b/internal/application/service/user_env_test.go @@ -453,7 +453,10 @@ func TestCaptureSkillEnvDoesNotCopyAdminValueIntoUserSlot(t *testing.T) { require.Empty(t, repo.userEnvs) } -func TestCaptureSkillEnvWritesUserOverrideOfAdminValue(t *testing.T) { +// A workspace value is one an admin typed. A value read out of a command the +// model composed must not displace it for this caller — that is what the +// settings page is for. +func TestCaptureSkillEnvLeavesTheAdminValueInPlace(t *testing.T) { svc, repo := newUserEnvFixture(t) require.NoError(t, svc.CaptureSkillEnv( @@ -461,7 +464,7 @@ func TestCaptureSkillEnvWritesUserOverrideOfAdminValue(t *testing.T) { "API_TOKEN": "alice-override", })) - require.Equal(t, "alice-override", userEnvByName(t, repo.userEnvs, "API_TOKEN").Value) + require.Empty(t, repo.userEnvs) } func TestCaptureSkillEnvSkipsWhenUserValueAlreadyMatches(t *testing.T) { @@ -478,7 +481,10 @@ func TestCaptureSkillEnvSkipsWhenUserValueAlreadyMatches(t *testing.T) { require.Equal(t, "already-mine", repo.userEnvs[0].Value) } -func TestCaptureSkillEnvRotatesADifferentUserValue(t *testing.T) { +// Capture fills blanks and nothing else. A prompt injection, a hallucination or +// one `export TOKEN=test` must not be able to replace a key its owner entered, +// which nobody would notice until every later turn authenticated as nobody. +func TestCaptureSkillEnvNeverOverwritesAStoredUserValue(t *testing.T) { svc, repo := newUserEnvFixture(t) ctx := userEnvCtx(userEnvTenantID, "alice") require.NoError(t, svc.SetMineSkill(ctx, "sk-ready", "USER_TOKEN", "old-key")) @@ -488,7 +494,7 @@ func TestCaptureSkillEnvRotatesADifferentUserValue(t *testing.T) { })) require.Len(t, repo.userEnvs, 1) - require.Equal(t, "new-key", repo.userEnvs[0].Value) + require.Equal(t, "old-key", repo.userEnvs[0].Value) } func TestCaptureSkillEnvIgnoresUnknownOrUnusableSkills(t *testing.T) { diff --git a/internal/types/tenant_env_vars.go b/internal/types/tenant_env_vars.go index b9883a0e..9015c860 100644 --- a/internal/types/tenant_env_vars.go +++ b/internal/types/tenant_env_vars.go @@ -4,6 +4,7 @@ import ( "database/sql/driver" "encoding/json" "errors" + "fmt" "log" "time" @@ -65,11 +66,15 @@ func (v SkillEnvVars) Value() (driver.Value, error) { } rows := make([]skillEnvVarRow, len(v)) for i, entry := range v { + encrypted, err := encryptEnvValue(entry.Value) + if err != nil { + return nil, fmt.Errorf("encrypt skill_env_vars.%s: %w", entry.Name, err) + } rows[i] = skillEnvVarRow{ Name: entry.Name, Description: entry.Description, Required: entry.Required, - Value: encryptEnvValue(entry.Value), + Value: encrypted, } } return json.Marshal(rows) @@ -153,12 +158,22 @@ func (e *TenantUserEnvVar) BeforeCreate(tx *gorm.DB) error { if e.ID == "" { e.ID = uuid.New().String() } - e.Value = encryptEnvValue(e.Value) - return nil + return e.encryptValue() } func (e *TenantUserEnvVar) BeforeSave(tx *gorm.DB) error { - e.Value = encryptEnvValue(e.Value) + return e.encryptValue() +} + +// encryptValue is idempotent: EncryptAESGCM returns an already-prefixed string +// unchanged, so the BeforeSave/BeforeCreate pair GORM runs on a create does not +// encrypt twice. +func (e *TenantUserEnvVar) encryptValue() error { + encrypted, err := encryptEnvValue(e.Value) + if err != nil { + return fmt.Errorf("encrypt tenant_user_env_vars.%s: %w", e.Name, err) + } + e.Value = encrypted return nil } @@ -168,16 +183,23 @@ func (e *TenantUserEnvVar) AfterFind(tx *gorm.DB) error { } // encryptEnvValue returns the ciphertext, or the input unchanged when there is -// no key to encrypt with. -func encryptEnvValue(value string) string { +// no key configured to encrypt with — the same deployment-wide degradation +// every other secret column accepts. +// +// A key that exists but fails to encrypt is different: it means something is +// wrong with the cipher, and writing the plaintext into a queryable JSONB +// column instead would turn that into a silent leak. The error aborts the +// write, matching TenantAPIKey.BeforeSave. +func encryptEnvValue(value string) (string, error) { key := utils.GetAESKey() if key == nil || value == "" { - return value + return value, nil } - if encrypted, err := utils.EncryptAESGCM(value, key); err == nil { - return encrypted + encrypted, err := utils.EncryptAESGCM(value, key) + if err != nil { + return "", err } - return value + return encrypted, nil } // decryptEnvValue reports an unreadable secret as unset rather than failing the diff --git a/internal/types/tenant_env_vars_test.go b/internal/types/tenant_env_vars_test.go index f3329634..b7875cf5 100644 --- a/internal/types/tenant_env_vars_test.go +++ b/internal/types/tenant_env_vars_test.go @@ -142,6 +142,32 @@ func TestTenantUserEnvVarNeverSerialisesItsValue(t *testing.T) { require.Contains(t, string(raw), "TAVILY_API_KEY") } +// GORM runs BeforeSave and BeforeCreate one after the other on a create, and +// both encrypt. Encrypting twice would store a value nothing can read back. +func TestTenantUserEnvVarEncryptsOnceAcrossBothHooks(t *testing.T) { + t.Setenv("SYSTEM_AES_KEY", skillEnvAESKey) + row := &TenantUserEnvVar{Name: "TAVILY_API_KEY", Value: "tvly-super-secret"} + + require.NoError(t, row.BeforeSave(nil)) + require.NoError(t, row.BeforeCreate(nil)) + + require.NotEmpty(t, row.ID, "BeforeCreate still assigns the primary key") + require.True(t, strings.HasPrefix(row.Value, utils.EncPrefix)) + plain, ok := utils.DecryptStoredSecretLenient(row.Value) + require.True(t, ok) + require.Equal(t, "tvly-super-secret", plain) +} + +// Without a key configured there is nothing to encrypt with, which is the +// deployment-wide degradation every secret column in this repo accepts. +func TestTenantUserEnvVarKeepsTheValueWhenNoKeyIsConfigured(t *testing.T) { + t.Setenv("SYSTEM_AES_KEY", "") + row := &TenantUserEnvVar{Name: "TAVILY_API_KEY", Value: "tvly-super-secret"} + + require.NoError(t, row.BeforeSave(nil)) + require.Equal(t, "tvly-super-secret", row.Value) +} + func TestTenantUserEnvVarAfterFindTreatsUndecryptableValueAsUnset(t *testing.T) { t.Setenv("SYSTEM_AES_KEY", skillEnvAESKey) var logs bytes.Buffer diff --git a/migrations/versioned/000089_env_vars.up.sql b/migrations/versioned/000089_env_vars.up.sql index 2213f0e9..7e59f9f6 100644 --- a/migrations/versioned/000089_env_vars.up.sql +++ b/migrations/versioned/000089_env_vars.up.sql @@ -7,14 +7,14 @@ -- is injected into every execution on it; one with a skill_id is that skill's -- declared credential, injected only when a tool names the skill. The storage is -- shared because it is the same kind of thing — only the load timing differs. -DO $$ BEGIN RAISE NOTICE '[Migration 000088] Adding tenant_skills.envs'; END $$; +DO $$ BEGIN RAISE NOTICE '[Migration 000089] Adding tenant_skills.envs'; END $$; ALTER TABLE tenant_skills ADD COLUMN IF NOT EXISTS envs JSONB; COMMENT ON COLUMN tenant_skills.envs IS 'Installer-agent declaration [{name,description,required,value}]. value is the workspace-wide admin value and is AES-GCM encrypted; the rest is plaintext so the UI can render it without a key.'; -DO $$ BEGIN RAISE NOTICE '[Migration 000088] Creating tenant_user_env_vars'; END $$; +DO $$ BEGIN RAISE NOTICE '[Migration 000089] Creating tenant_user_env_vars'; END $$; CREATE TABLE IF NOT EXISTS tenant_user_env_vars ( id VARCHAR(36) PRIMARY KEY,