Skip to content

DetachedEnvVarValidator incorrectly rejects optional secret env vars when not provided #5688

Closed

Description

@danbarr

Bug description

When installing a registry MCP server via the API (e.g. from ToolHive Studio) that has optional secret environment variables (like oci-registry's OCI_TOKEN or context7's CONTEXT7_API_KEY), the daemon rejects the workload creation with:

failed to build run config: failed to validate environment variables: missing required secret environment variable: OCI_TOKEN

Both variables are defined in the registry with required: false, secret: true. The registry API correctly returns them that way. The error is wrong.

Root cause

DetachedEnvVarValidator.Validate in pkg/runner/env.go has an else if envVar.Secret branch that fires unconditionally for any unset secret env var, without checking envVar.Required:

} else if envVar.Required {
    return nil, fmt.Errorf("missing required environment variable: %s", envVar.Name)
} else if envVar.Secret {
    // fires even when required=false
    return nil, fmt.Errorf("missing required secret environment variable: %s", envVar.Name)
} else if envVar.Default != "" {
    addAsEnvironmentVariable(envVar, envVar.Default, &suppliedEnvVars)
}

CLIEnvVarValidator.Validate handles this correctly — it only enforces envVar.Required and silently skips unset optional secrets. The two validators diverged.

Fix

Remove the else if envVar.Secret branch from DetachedEnvVarValidator.Validate:

} else if envVar.Required {
    return nil, fmt.Errorf("missing required environment variable: %s", envVar.Name)
} else if envVar.Default != "" {
    addAsEnvironmentVariable(envVar, envVar.Default, &suppliedEnvVars)
}
// optional secrets with no value and no default: skip silently

Affected versions

Confirmed present in v0.31.0 and current main.

Steps to reproduce

  1. Open ToolHive Studio and install oci-registry or context7 from the registry
  2. Leave OCI_TOKEN / CONTEXT7_API_KEY blank (they are optional)
  3. Submit — the server fails to start with the error above

Activity

  1. added
    bug
    on Jun 30, 2026
  2. added
    needs-triage
    on Jun 30, 2026
  3. self-assigned this
    on Jun 30, 2026
  4. danbarr commented on Jun 30, 2026

    @danbarr
    CollaboratorAuthor

    Correction to the original report: after tracing through the git history of both the CLI and the catalog, the bug has been present since day one and is not specific to v0.31.0. The else if envVar.Secret branch in DetachedEnvVarValidator has never been correct, and the catalog has had isSecret: true on these env vars from their first commit. There is no code change that activated this at a particular version — it was always broken for any registry server with an optional secret env var left blank.

  5. danbarr commented on Jun 30, 2026

    @danbarr
    CollaboratorAuthor

    Further correction to narrow down the trigger: the broken branch in DetachedEnvVarValidator is only reachable when metadata != nil. Before the registry-aware workload creation change (commit 829c4bd, April 21, 2026), Studio sent fully-resolved requests (image URL + env vars already extracted client-side), so metadata was nil in Validate() and the loop never ran. Once Studio started sending {registry: "default", server: "oci-registry"} and letting the daemon resolve metadata server-side, metadata became non-nil and the bug surfaced for any server with an optional secret env var left blank.

  6. added a commit that references this issue on Jul 7, 2026
    748d43c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugneeds-triage

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions