{"record":{"id":"90311f5b7da09e82","repo":"Tencent/WeKnora","slug":"environment-variable-s-is-not-allowed-for-secur","errorCode":null,"errorMessage":"environment variable '%s' is not allowed for security reasons","messagePattern":"environment variable '(.+?)' is not allowed for security reasons","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/utils/security.go","lineNumber":598,"sourceCode":"\t\t\treturn fmt.Errorf(\"argument %d contains null bytes\", i)\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// ValidateStdioEnvVars validates environment variables for MCP stdio transport\n// Returns an error if any env var name or value is dangerous\nfunc ValidateStdioEnvVars(envVars map[string]string) error {\n\tif len(envVars) == 0 {\n\t\treturn nil\n\t}\n\n\tfor key, value := range envVars {\n\t\t// Check key against dangerous patterns\n\t\tfor _, pattern := range DangerousEnvVarPatterns {\n\t\t\tif pattern.MatchString(key) {\n\t\t\t\treturn fmt.Errorf(\"environment variable '%s' is not allowed for security reasons\", key)\n\t\t\t}\n\t\t}\n\n\t\t// Check key length\n\t\tif len(key) > 256 {\n\t\t\treturn fmt.Errorf(\"environment variable name '%s' exceeds maximum length\", SanitizeForLog(key[:50]))\n\t\t}\n\n\t\t// Check value length\n\t\tif len(value) > 4096 {\n\t\t\treturn fmt.Errorf(\"environment variable '%s' value exceeds maximum length\", key)\n\t\t}\n\n\t\t// Check for null bytes in value\n\t\tif strings.Contains(value, \"\\x00\") {\n\t\t\treturn fmt.Errorf(\"environment variable '%s' value contains null bytes\", key)\n\t\t}\n","sourceCodeStart":580,"sourceCodeEnd":616,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/utils/security.go#L580-L616","documentation":"ValidateStdioEnvVars checks each environment-variable KEY against DangerousEnvVarPatterns regexes before an MCP stdio process is spawned. Keys matching dangerous patterns (typically sensitive or privileged variable names like LD_PRELOAD, IFS, shell/function overrides) are rejected for security reasons, regardless of value.","triggerScenarios":"ValidateStdioConfig called with an env map whose key matches a dangerous pattern, e.g. \"LD_PRELOAD\", \"BASH_ENV\", \"PATH=...\" style malformed keys, or keys containing shell metacharacters.","commonSituations":"Config copied from a dev machine exporting loader/shell hijack vars; dynamically generated env keys; attempting to tweak dynamic linking or shell behavior for the child process; keys with spaces or '=' accidentally included.","solutions":["Remove the offending variable from the env map; most stdio servers need only a few app-specific vars","If a var is genuinely needed but blocked (e.g. LD_LIBRARY_PATH), consult DangerousEnvVarPatterns and find a supported alternative mechanism","Fix malformed keys (spaces, '=' inside the key, empty keys) that trip the dangerous patterns","Whitelist exactly the vars the server documents as required (e.g. API keys with safe names)"],"exampleFix":"// before\n\"env\": {\"LD_PRELOAD\": \"/tmp/hook.so\", \"API_KEY\": \"...\"}\n// after\n\"env\": {\"API_KEY\": \"...\"}","handlingStrategy":"validation","validationCode":"var dangerousEnv = regexp.MustCompile(`(?i)^(LD_PRELOAD|LD_LIBRARY_PATH|BASH_ENV|ENV|IFS|SHELL|PATH)$`)\nfor k := range cfg.Env {\n    if dangerousEnv.MatchString(k) || strings.ContainsAny(k, \" =\\x00\") {\n        return fmt.Errorf(\"env key %q not allowed\", k)\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := ValidateStdioConfig(cfg); err != nil {\n    if strings.Contains(err.Error(), \"is not allowed for security reasons\") {\n        return fmt.Errorf(\"remove blocked env var (loader/shell vars are forbidden): %w\", err)\n    }\n    return err\n}","preventionTips":["Only set environment variables the server explicitly documents","Never set dynamic-loader or shell variables (LD_PRELOAD, BASH_ENV, IFS) for child processes","Validate env keys against ^[A-Za-z_][A-Za-z0-9_]*$ at config load"],"tags":["security","env-vars","mcp","stdio"],"backgroundTag":"forbidden-environment-variable","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}