{"record":{"id":"2afc1a34bb3a7558","repo":"siyuan-note/siyuan","slug":"variable-q-contains-nul","errorCode":null,"errorMessage":"variable %q contains NUL","messagePattern":"variable %q contains NUL","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/mcp/client/mcp.go","lineNumber":564,"sourceCode":"func validateMCPServerEnvironment(server conf.MCPServer, goos string) error {\n\tinherited := map[string]bool{}\n\tfor _, name := range server.InheritEnv {\n\t\tif err := validateEnvironmentName(name); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tkey := environmentKey(name, goos)\n\t\tif inherited[key] {\n\t\t\treturn fmt.Errorf(\"duplicate inherited variable %q\", name)\n\t\t}\n\t\tinherited[key] = true\n\t}\n\texplicit := map[string]bool{}\n\tfor name, value := range server.Env {\n\t\tif err := validateEnvironmentName(name); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif strings.ContainsRune(value, '\\x00') {\n\t\t\treturn fmt.Errorf(\"variable %q contains NUL\", name)\n\t\t}\n\t\tkey := environmentKey(name, goos)\n\t\tif explicit[key] {\n\t\t\treturn fmt.Errorf(\"duplicate variable %q\", name)\n\t\t}\n\t\texplicit[key] = true\n\t}\n\treturn nil\n}\n\n// ValidateMCPServerEnvironment 校验当前平台上的 stdio 环境变量配置。\nfunc ValidateMCPServerEnvironment(server conf.MCPServer) error {\n\treturn validateMCPServerEnvironment(server, runtime.GOOS)\n}\n\nfunc defaultMCPEnvironmentNames(goos string) []string {\n\tif goos == \"windows\" {\n\t\treturn []string{\"APPDATA\", \"HOMEDRIVE\", \"HOMEPATH\", \"LOCALAPPDATA\", \"PATH\", \"PATHEXT\",","sourceCodeStart":546,"sourceCodeEnd":582,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/mcp/client/mcp.go#L546-L582","documentation":"A value in conf.MCPServer.Env contains a NUL byte ('\\x00'). Environment blocks passed to exec are NUL-delimited 'NAME=value' strings, so a NUL inside a value would truncate or corrupt the variable for the child stdio MCP server process. validateMCPServerEnvironment rejects it before spawning.","triggerScenarios":"A value of an entry in server.Env contains a literal NUL character when ValidateMCPServerEnvironment runs or buildStdioEnvironment prepares the child environment.","commonSituations":"Pasting binary data or a corrupted token into an env value field; config file corruption introducing NUL bytes; programmatic config generation embedding a NUL-terminated string.","solutions":["Open the MCP server settings and re-enter the variable value without the invisible NUL character (delete and retype the value)","If editing conf.json, locate the value containing \\u0000 and clean or regenerate it","Verify the source of the value (token file, clipboard) is plain text and re-copy it"],"exampleFix":"// before\nserver.Env[\"TOKEN\"] = \"abc\\x00def\"\n// after\nserver.Env[\"TOKEN\"] = \"abcdef\"","handlingStrategy":"validation","validationCode":"for name, v := range server.Env {\n    if strings.ContainsRune(v, '\\x00') {\n        return fmt.Errorf(\"value of %q contains NUL\", name)\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat env values as plain text; strip control characters on input","Regenerate tokens/keys from their source rather than pasting from binary contexts","Validate values with ValidateMCPServerEnvironment before persisting config"],"tags":["mcp","environment-variables","nul-byte"],"backgroundTag":"invalid-env-var-value","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}