{"record":{"id":"975ecb09a5f01a32","repo":"git-ecosystem/git-credential-manager","slug":"state-key-cannot-be-null-or-whitespace","errorCode":null,"errorMessage":"State key cannot be null or whitespace.","messagePattern":"State key cannot be null or whitespace\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Core/GitStateValidation.cs","lineNumber":83,"sourceCode":"        {\n            if (c == LF || c == NUL)\n            {\n                return false;\n            }\n        }\n\n        return true;\n    }\n\n    /// <summary>\n    /// Throws <see cref=\"ArgumentException\"/> if <paramref name=\"key\"/> is not\n    /// a legal state entry key.\n    /// </summary>\n    public static void ValidateKey(string key)\n    {\n        if (string.IsNullOrWhiteSpace(key))\n        {\n            throw new ArgumentException(\"State key cannot be null or whitespace.\", nameof(key));\n        }\n\n        foreach (char c in key)\n        {\n            if (c == EQ || c == LF || c == NUL)\n            {\n                throw new ArgumentException(\n                    \"State key cannot contain '=', newline, or NUL characters.\",\n                    nameof(key));\n            }\n        }\n\n        if (key.StartsWith(Constants.CredentialProtocol.GcmStatePrefix, StringComparison.Ordinal))\n        {\n            throw new ArgumentException(\n                $\"State key cannot start with '{Constants.CredentialProtocol.GcmStatePrefix}'; \" +\n                \"the prefix is reserved and added automatically when state is emitted.\",\n                nameof(key));","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/git-ecosystem/git-credential-manager/blob/e8ce762cd04b4100ae637b5fbf39ef9d0a96561e/src/Core/GitStateValidation.cs#L65-L101","documentation":"GitStateValidation.ValidateKey validates state entry keys used by the Git credential-helper 'state' wire protocol. A key that is null, empty, or whitespace-only cannot be serialized as a protocol line, so the library throws ArgumentException immediately.","triggerScenarios":"Calling GitResponse.SetState(key, value) or WithState(key, value) with a null, empty (\"\") or whitespace-only key, which funnels into ValidateKey.","commonSituations":"Building state keys dynamically (e.g. from config values or dictionary keys) where the source can be missing or blank; typos passing \"\" as a literal.","solutions":["Ensure the key is a non-empty, non-whitespace string before calling SetState/WithState.","Validate inputs where the key is produced (config lookup, user input) and fail early with a clearer error.","If state is optional, skip SetState entirely when no key is available rather than passing null."],"exampleFix":"// before\nresponse.SetState(configValue, \"1\");\n// after\nif (!string.IsNullOrWhiteSpace(configValue))\n{\n    response.SetState(configValue, \"1\");\n}","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(key)) throw new ArgumentException(\"State key required\");","typeGuard":"static bool IsValidStateKey(string k) => !string.IsNullOrWhiteSpace(k);","tryCatchPattern":"try { response.SetState(key, value); }\ncatch (ArgumentException e) when (e.Message.Contains(\"State key\")) { /* log and skip state */ }","preventionTips":["Guard dynamic keys with string.IsNullOrWhiteSpace before use.","Avoid building keys from possibly-empty config values without defaults.","Unit-test state key construction with empty inputs."],"tags":["csharp","validation","git-state-protocol"],"backgroundTag":"empty-required-field","analyzedSha":"e8ce762cd04b4100ae637b5fbf39ef9d0a96561e","analyzedAt":"2026-09-11T17:15:08.753Z","contentChangedAt":"2026-09-11T17:15:08.753Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}