Tencent/WeKnora · error

environment variable '%s' value contains potentially dangero

Error message

environment variable '%s' value contains potentially dangerous pattern

What it means

ValidateStdioEnvVars checks each environment variable value against the DangerousArgPatterns regex list and rejects values matching shell metacharacter / injection patterns. This prevents env values from being abused for shell injection when the child process or its launcher interpolates them into a shell command. It is a deliberate security gate, not a bug.

Source

Thrown at internal/utils/security.go:620

		// Check key length
		if len(key) > 256 {
			return fmt.Errorf("environment variable name '%s' exceeds maximum length", SanitizeForLog(key[:50]))
		}

		// Check value length
		if len(value) > 4096 {
			return fmt.Errorf("environment variable '%s' value exceeds maximum length", key)
		}

		// Check for null bytes in value
		if strings.Contains(value, "\x00") {
			return fmt.Errorf("environment variable '%s' value contains null bytes", key)
		}

		// Check value for shell injection patterns
		for _, pattern := range DangerousArgPatterns {
			if pattern.MatchString(value) {
				return fmt.Errorf("environment variable '%s' value contains potentially dangerous pattern", key)
			}
		}
	}

	return nil
}

// ValidateStdioConfig performs comprehensive validation of stdio configuration
// This should be called before creating or executing any stdio-based MCP client
func ValidateStdioConfig(command string, args []string, envVars map[string]string) error {
	// Validate command
	if err := ValidateStdioCommand(command); err != nil {
		return fmt.Errorf("invalid command: %w", err)
	}

	// Validate arguments
	if err := ValidateStdioArgs(args); err != nil {
		return fmt.Errorf("invalid arguments: %w", err)

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Remove shell metacharacters from the value; pass the literal value (the command is executed directly, not via a shell).
  2. Precompute the value (run the substitution yourself and store the result).
  3. Review DangerousArgPatterns in internal/utils/security.go to see which pattern matched and adjust the value.

Example fix

// before
envVars["LOG_DIR"] = "$(pwd)/logs"
err := secutils.ValidateStdioConfig(cmd, args, envVars)
// after
envVars["LOG_DIR"] = "/abs/path/to/logs"
err := secutils.ValidateStdioConfig(cmd, args, envVars)
Defensive patterns

Strategy: validation

Validate before calling

for _, v := range envVars {
    for _, p := range secutils.DangerousArgPatterns {
        if p.MatchString(v) { return fmt.Errorf("unsafe env value: %q", v) }
    }
}

Type guard

func isShellSafe(v string) bool {
    for _, p := range secutils.DangerousArgPatterns {
        if p.MatchString(v) { return false }
    }
    return true
}

Try / catch

err := secutils.ValidateStdioConfig(cmd, args, env)
if err != nil && strings.Contains(err.Error(), "dangerous pattern") {
    return fmt.Errorf("env var rejected by security policy: %w", err)
}

Prevention

When it happens

Trigger: Calling ValidateStdioConfig (or ValidateStdioEnvVars directly) where any env value matches one of the DangerousArgPatterns regexes (e.g. backticks, $(), ;, |, &&).

Common situations: Config files where values were meant to be executed by a shell, values copied from shell scripts with $(...) substitutions, or tampered/attacker-supplied configuration.

Related errors


AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02). Data as JSON: /api/errors/f68749257552e394. Report an issue: GitHub.