{"record":{"id":"c18d5be7290ce64e","repo":"BeyondDimension/SteamTools","slug":"malformed-input-0-is-an-invalid-input-length","errorCode":null,"errorMessage":"Malformed input: {0} is an invalid input length.","messagePattern":"Malformed input: (.+?) is an invalid input length\\.","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/BD.WTTS.Client.Plugins.Update/WebEncoders.cs","lineNumber":186,"sourceCode":"        }\n\n        var numPaddingCharsToAdd = GetNumBase64PaddingCharsToAddForDecode(count);\n\n        return checked(count + numPaddingCharsToAdd);\n    }\n\n    private static int GetNumBase64PaddingCharsToAddForDecode(int inputLength)\n    {\n        switch (inputLength % 4)\n        {\n            case 0:\n                return 0;\n            case 2:\n                return 2;\n            case 3:\n                return 1;\n            default:\n                throw new FormatException(\n                    string.Format(\n                        CultureInfo.CurrentCulture,\n                        \"Malformed input: {0} is an invalid input length.\",\n                        inputLength));\n        }\n    }\n}\n","sourceCodeStart":168,"sourceCodeEnd":194,"githubUrl":"https://github.com/BeyondDimension/SteamTools/blob/c16ffa08e03b192d23ada290c4969e77f9201f3d/src/BD.WTTS.Client.Plugins.Update/WebEncoders.cs#L168-L194","documentation":"Thrown by the base64 decoder's padding calculator when inputLength % 4 == 1. Valid base64 input length modulo 4 can only be 0, 2, or 3 (mapping to 0, 2, or 1 padding chars); a remainder of 1 is structurally impossible for correctly-encoded base64, so the input is malformed and cannot be decoded.","triggerScenarios":"Calling the WebEncoders base64 decode path with a string whose length mod 4 equals 1 — i.e. a character was lost/added, the string was truncated mid-group, or non-base64 characters were stripped leaving an invalid length.","commonSituations":"Truncated base64 (copy-paste cut off); URL-safe base64 with '-'/'_' not normalized back to '+'/'/' before length-sensitive decoding; whitespace/newlines stripped unevenly; a stray character inserted; mismatched encoder (e.g. hex mistaken for base64).","solutions":["Validate length % 4 != 1 before decoding and reject/repair the input with a clear message.","Re-copy the full base64 string from source without truncation.","Normalize URL-safe base64 ('-' -> '+', '_' -> '/') and re-add '=' padding before decoding.","Strip whitespace/newlines consistently, then re-check the length."],"exampleFix":"// before\nvar decoded = WebEncoders.Base64UrlDecode(input); // throws if input.Length % 4 == 1\n\n// after: pre-validate length and re-pad\ninput = input.Replace('-', '+').Replace('_', '/').Trim();\nswitch (input.Length % 4)\n{\n    case 2: input += \"==\"; break;\n    case 3: input += \"=\"; break;\n    case 1: throw new ArgumentException(\"Base64 input is malformed (length % 4 == 1).\", nameof(input));\n}\nvar decoded = WebEncoders.Base64UrlDecode(input);","handlingStrategy":"validation","validationCode":"// Normalize and length-validate base64 input BEFORE decoding.\nstatic string NormalizeBase64(string input)\n{\n    input = input.Trim().Replace('-', '+').Replace('_', '/');\n    input = input.TrimEnd('=');\n    var pad = input.Length % 4 switch\n    {\n        2 => \"==\",\n        3 => \"=\",\n        0 => \"\",\n        _ => throw new ArgumentException($\"Invalid base64 length {input.Length} (mod 4 == 1).\", nameof(input))\n    };\n    return input + pad;\n}\n\nvar decoded = WebEncoders.Base64UrlDecode(NormalizeBase64(input));","typeGuard":"bool IsValidBase64Length(string s) => s.Trim().Length % 4 != 1;","tryCatchPattern":"try { return WebEncoders.Base64UrlDecode(input); }\ncatch (FormatException ex) when (ex.Message.Contains(\"invalid input length\"))\n{\n    // Re-copy from source, normalize URL-safe chars, then retry; otherwise reject the input.\n    var normalized = NormalizeBase64(input);\n    if (IsValidBase64Length(normalized)) return WebEncoders.Base64UrlDecode(normalized);\n    throw new ArgumentException(\"Base64 input is malformed and cannot be recovered.\", nameof(input), ex);\n}","preventionTips":["Always validate length % 4 != 1 before decoding base64 and reject early.","Normalize URL-safe base64 ('-','_') and re-add '=' padding before length-sensitive decode.","Strip whitespace/newlines consistently before validating length.","Re-copy base64 strings from source in full to avoid truncation."],"tags":["base64","encoding","validation","update"],"backgroundTag":null,"analyzedSha":"c16ffa08e03b192d23ada290c4969e77f9201f3d","analyzedAt":"2026-08-13T11:52:20.410Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}