{"record":{"id":"bbc1fd89707a41c7","repo":"git-ecosystem/git-credential-manager","slug":"state-value-cannot-contain-newline-or-nul-characte","errorCode":null,"errorMessage":"State value cannot contain newline or NUL characters.","messagePattern":"State value cannot contain newline or NUL characters\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Core/GitStateValidation.cs","lineNumber":120,"sourceCode":"        }\n    }\n\n    /// <summary>\n    /// Throws <see cref=\"ArgumentException\"/> if <paramref name=\"value\"/> is\n    /// not a legal state entry value.\n    /// </summary>\n    public static void ValidateValue(string value)\n    {\n        if (value is null)\n        {\n            throw new ArgumentNullException(nameof(value), \"State value cannot be null.\");\n        }\n\n        foreach (char c in value)\n        {\n            if (c == LF || c == NUL)\n            {\n                throw new ArgumentException(\n                    \"State value cannot contain newline or NUL characters.\",\n                    nameof(value));\n            }\n        }\n    }\n}\n\n","sourceCodeStart":102,"sourceCodeEnd":128,"githubUrl":"https://github.com/git-ecosystem/git-credential-manager/blob/e8ce762cd04b4100ae637b5fbf39ef9d0a96561e/src/Core/GitStateValidation.cs#L102-L128","documentation":"State values are emitted as single 'key=value' protocol lines. A value containing LF or NUL would break the line framing (and '=' is allowed in values because only the first '=' splits), so ValidateValue throws ArgumentException for newline or NUL characters.","triggerScenarios":"Calling SetState/WithState with a value containing '\\n', '\\r\\n', or '\\0' — commonly multi-line payloads, JSON blobs, or error text.","commonSituations":"Storing multi-line messages, base64 with newlines inserted, or raw process output as state; serializing objects without escaping.","solutions":["Flatten the value: replace newlines with a safe delimiter like ' ' or '\\\\n' literal.","Encode the value (Base64 or URL-encoding) before storing and decode when reading back.","Split multi-line content into multiple state entries."],"exampleFix":"// before\nresponse.SetState(\"last-error\", exception.ToString());\n// after\nresponse.SetState(\"last-error\", exception.ToString().Replace('\\n', ' ').Replace('\\r', ' '));","handlingStrategy":"validation","validationCode":"bool ok = value != null && value.IndexOfAny(new[] {'\\n', '\\r', '\\0'}) < 0;","typeGuard":"static bool IsSingleLineValue(string v) => v is not null && !v.Any(c => c == '\\n' || c == '\\0');","tryCatchPattern":"try { response.SetState(key, value); }\ncatch (ArgumentException e) when (e.Message.Contains(\"cannot contain\")) { response.SetState(key, Convert.ToBase64String(Encoding.UTF8.GetBytes(value))); }","preventionTips":["Flatten or Base64-encode any value that could be multi-line (stack traces, JSON).","Never pipe raw process output into state values.","Add a NormalizeStateValue helper that strips LF/NUL."],"tags":["csharp","validation","protocol-encoding","git-state-protocol"],"backgroundTag":"invalid-argument-format","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"}