fix(sandbox): allow skills to declare WEKNORA_* credential env vars

The installer reserved the entire WEKNORA_ prefix, so names like
WEKNORA_API_KEY were dropped while install still succeeded. Reserve only
the sandbox-injected names (and WEKNORA_SKILL_*), and keep that list in
one place.
main
wizardchen 2026-08-27 15:32:38 +08:00 committed by lyingbug
parent 46faca4619
commit d9b3ac3ab4
7 changed files with 102 additions and 19 deletions

View File

@ -3,7 +3,6 @@ package skills
import (
"context"
"errors"
"strings"
"testing"
"github.com/stretchr/testify/require"
@ -139,7 +138,11 @@ func TestExecuteScriptWithoutResolverInjectsNothingExtra(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 1, sandboxMgr.calls)
injected := map[string]bool{}
for _, name := range InjectedSandboxEnvVars() {
injected[name] = true
}
for key := range sandboxMgr.config.Env {
require.True(t, strings.HasPrefix(key, "WEKNORA_"), "unexpected env key %q", key)
require.True(t, injected[key], "injected env %q is not in InjectedSandboxEnvVars", key)
}
}

View File

@ -38,6 +38,19 @@ const artifactHistoryEnvVar = "WEKNORA_SKILL_HISTORY_ROOT"
// install-time verification pass exports the same name.
const skillDirEnvVar = "WEKNORA_SKILL_DIR"
// InjectedSandboxEnvVars is every name ExecuteScript writes into the sandbox
// environment. The skill-env declaration blacklist must reject these so a
// stored value cannot redirect artifacts, the skill directory, or the session
// input tree. Credential names such as WEKNORA_API_KEY are not in this list.
func InjectedSandboxEnvVars() []string {
return []string{
artifactOutputEnvVar,
sessionInputEnvVar,
artifactHistoryEnvVar,
skillDirEnvVar,
}
}
// defaultArtifactOutputDir is used when neither the environment variable
// (WEKNORA_SKILL_OUTPUT_DIR) nor the ExecuteConfig.Env has an override.
// /workspace/output sits inside the base sandbox image's writable tree and
@ -361,13 +374,14 @@ func (m *Manager) ExecuteScript(ctx context.Context, skillName, scriptPath strin
// so each turn's values must belong to that turn's speaker and must not
// linger where the next person could read them with `env`.
//
// The resolver runs after the two artifact keys above are seeded, so
// applyResolvedEnv's skip-existing rule protects exactly those two. It is
// NOT a blanket guarantee over every WEKNORA_ key: sessionInputEnvVar is
// only seeded when a session file store exists, and skillDirEnvVar is set
// later inside buildSkillExecuteConfig and so relies on that unconditional
// write instead. The write-time WEKNORA_ prefix blacklist
// (service.validateUserEnvName) is what covers those two.
// The resolver runs after the artifact keys above are seeded, so
// applyResolvedEnv's skip-existing rule protects WEKNORA_SKILL_OUTPUT_DIR
// and WEKNORA_SKILL_HISTORY_ROOT (always) plus WEKNORA_SESSION_INPUT_DIR
// (when a session file store exists). skillDirEnvVar is set later inside
// buildSkillExecuteConfig by an unconditional write. The write-time
// blacklist of skills.InjectedSandboxEnvVars (service.validateUserEnvName)
// is what covers a name that was not seeded on this path. Other WEKNORA_*
// names (API keys, base URLs) are ordinary credentials a skill may declare.
if m.envResolver != nil {
resolved, missing, err := m.envResolver.ResolveEnv(ctx, skillName)
if err != nil {

View File

@ -7,6 +7,7 @@ import (
"regexp"
"strings"
"github.com/Tencent/WeKnora/internal/agent/skills"
"github.com/Tencent/WeKnora/internal/types"
)
@ -52,10 +53,20 @@ var reservedEnvNames = map[string]bool{
"NODE_OPTIONS": true,
}
// reservedEnvPrefix is reserved because skills.Manager uses it to hand the
// skill its artifact directory. Letting it be overridden breaks output
// collection for reasons nobody would think to look for.
const reservedEnvPrefix = "WEKNORA_"
func init() {
// The names ExecuteScript actually writes, not a guessed prefix. Output
// dir, history root and skill dir currently share WEKNORA_SKILL_, but
// SESSION_INPUT_DIR does not; pulling the list from skills keeps the
// blacklist aligned when a fifth injected name appears.
for _, name := range skills.InjectedSandboxEnvVars() {
reservedEnvNames[name] = true
}
}
// reservedEnvPrefix covers future WEKNORA_SKILL_* names the sandbox may start
// injecting before the exact list above is updated. Credential names a skill
// reads — WEKNORA_API_KEY, WEKNORA_BASE_URL, WEKNORA_HOST — are outside it.
const reservedEnvPrefix = "WEKNORA_SKILL_"
// declaredSkillEnv is one entry as the installer agent wrote it.
//

View File

@ -5,6 +5,7 @@ import (
"fmt"
"testing"
"github.com/Tencent/WeKnora/internal/agent/skills"
"github.com/Tencent/WeKnora/internal/types"
"github.com/stretchr/testify/require"
)
@ -90,11 +91,27 @@ func TestValidateUserEnvNameFormatLayer(t *testing.T) {
func TestValidateUserEnvNameReservedLayer(t *testing.T) {
require.NoError(t, validateEnvNameNotReserved("TAVILY_API_KEY"))
require.NoError(t, validateEnvNameNotReserved("WEKNORA_API_KEY"),
"credential names a WeKnora skill reads are not sandbox-injected")
require.NoError(t, validateEnvNameNotReserved("WEKNORA_BASE_URL"))
require.NoError(t, validateEnvNameNotReserved("lowercase-is-a-format-concern"),
"reserved-name validation must remain independent of name format")
require.Error(t, validateEnvNameNotReserved("PATH"))
require.Error(t, validateEnvNameNotReserved("LD_PRELOAD"))
require.Error(t, validateEnvNameNotReserved("WEKNORA_SKILL_DIR"))
require.Error(t, validateEnvNameNotReserved("WEKNORA_SKILL_OUTPUT_DIR"))
require.Error(t, validateEnvNameNotReserved("WEKNORA_SKILL_HISTORY_ROOT"))
require.Error(t, validateEnvNameNotReserved("WEKNORA_SESSION_INPUT_DIR"))
}
// Every name ExecuteScript injects must be undeclarable. This is the lock that
// output dir, history root, skill dir and session input cannot drift out of
// the blacklist the way a hand-maintained prefix list can.
func TestReservedEnvNamesCoverEveryInjectedSandboxVar(t *testing.T) {
for _, name := range skills.InjectedSandboxEnvVars() {
require.Error(t, validateEnvNameNotReserved(name), name)
require.Error(t, validateUserEnvName(name), name)
}
}
func TestValidateEnvDeclarationBundleMatchLayerSearchesEveryFile(t *testing.T) {
@ -131,7 +148,7 @@ func TestValidateEnvDeclarationsAppliesThreeLayers(t *testing.T) {
want: nil,
},
{
name: "the WEKNORA_ prefix is reserved",
name: "sandbox-injected WEKNORA_SKILL_* names are reserved",
declared: []declaredSkillEnv{{Name: "WEKNORA_SKILL_DIR"}},
want: nil,
},
@ -208,6 +225,36 @@ func TestValidateEnvDeclarationsNeverCarriesAValue(t *testing.T) {
require.Equal(t, types.SkillEnvVars{{Name: "TAVILY_API_KEY"}}, got)
}
// Official WeKnora skills (and the ClawHub weknora skill) authenticate with
// WEKNORA_API_KEY / WEKNORA_BASE_URL. Those names must survive validation:
// only the sandbox-injected WEKNORA_SKILL_* / WEKNORA_SESSION_INPUT_DIR
// names are reserved.
func TestValidateEnvDeclarationsAllowsWeknoraCredentialNames(t *testing.T) {
bundle := &SkillBundle{
Name: "weknora",
Files: map[string][]byte{
"SKILL.md": []byte(
"export WEKNORA_BASE_URL WEKNORA_API_KEY WEKNORA_HOST WEKNORA_TOKEN WEKNORA_KB_ID; " +
"do not override WEKNORA_SKILL_DIR or WEKNORA_SESSION_INPUT_DIR.\n",
),
},
}
got := validateEnvDeclarations([]declaredSkillEnv{
{Name: "WEKNORA_BASE_URL", Required: true, Description: "API origin"},
{Name: "WEKNORA_API_KEY", Required: true},
{Name: "WEKNORA_HOST"},
{Name: "WEKNORA_SKILL_DIR"},
{Name: "WEKNORA_SESSION_INPUT_DIR"},
}, bundle)
require.Equal(t, types.SkillEnvVars{
{Name: "WEKNORA_BASE_URL", Description: "API origin", Required: true},
{Name: "WEKNORA_API_KEY", Required: true},
{Name: "WEKNORA_HOST"},
}, got)
}
func TestMergeEnvDeclarationKeepsTheAdminValueByName(t *testing.T) {
previous := types.SkillEnvVars{
{Name: "TAVILY_API_KEY", Description: "old text", Required: false, Value: "tvly-real"},
@ -251,6 +298,9 @@ func TestValidateUserEnvNameAcceptsAndRejects(t *testing.T) {
require.Error(t, validateUserEnvName("PATH"))
require.Error(t, validateUserEnvName("LD_PRELOAD"))
require.Error(t, validateUserEnvName("WEKNORA_SKILL_DIR"))
require.Error(t, validateUserEnvName("WEKNORA_SESSION_INPUT_DIR"))
require.NoError(t, validateUserEnvName("WEKNORA_API_KEY"))
require.NoError(t, validateUserEnvName("WEKNORA_BASE_URL"))
}
// The user path deliberately skips the bundle match: matching exists to catch

View File

@ -1476,6 +1476,9 @@ Hard requirements:
a value you invent would be stored as this workspace's real credential. If one environment
variable is required, set required to true; if it is optional, set required to false. If the
skill needs no environment variables, write {"env":[]}.
Do not declare WEKNORA_SKILL_DIR, WEKNORA_SKILL_OUTPUT_DIR, WEKNORA_SKILL_HISTORY_ROOT or
WEKNORA_SESSION_INPUT_DIR: the sandbox injects those. Other WEKNORA_* names the skill reads
(WEKNORA_API_KEY, WEKNORA_BASE_URL, WEKNORA_HOST, WEKNORA_TOKEN, WEKNORA_KB_ID) MUST be declared.
The server verifies the result itself before the image is kept, so report what
you did rather than whether it passed. Verification parses every script with

View File

@ -451,6 +451,8 @@ func TestBuildInstallPromptAsksForADeclarationWithoutValues(t *testing.T) {
require.Contains(t, prompt, `{"env":[]}`)
require.Contains(t, prompt, "Never write any value",
"a value the model invents would be stored as the workspace credential")
require.Contains(t, prompt, "WEKNORA_API_KEY",
"the installer must be told credential names are declarable, or it writes {\"env\":[]}")
}
func TestInstallSkillRepoNormalizesStoredUserEnvPrincipal(t *testing.T) {

View File

@ -270,7 +270,7 @@ func TestSetMineRejectsUnusableNames(t *testing.T) {
svc, repo := newUserEnvFixture(t)
ctx := userEnvCtx(userEnvTenantID, "alice")
for _, name := range []string{"PATH", "WEKNORA_ARTIFACT_DIR", "lower_case", "HAS SPACE", ""} {
for _, name := range []string{"PATH", "WEKNORA_SKILL_OUTPUT_DIR", "lower_case", "HAS SPACE", ""} {
t.Run(name, func(t *testing.T) {
require.Error(t, svc.SetMineSandbox(ctx, "cfg-1", name, "whatever"))
})
@ -431,10 +431,10 @@ func TestCaptureSkillEnvSkipsUndeclaredAndReservedNames(t *testing.T) {
require.NoError(t, svc.CaptureSkillEnv(
userEnvCtx(userEnvTenantID, "alice"), "cfg-1", "pdf-tools", map[string]string{
"USER_TOKEN": "keep-me",
"INVENTED": "no",
"PATH": "/bin",
"WEKNORA_HOME": "no",
"USER_TOKEN": "keep-me",
"INVENTED": "no",
"PATH": "/bin",
"WEKNORA_SKILL_DIR": "no",
}))
require.Len(t, repo.userEnvs, 1)