{"record":{"id":"b012ce4e94a588cd","repo":"iOfficeAI/OfficeCLI","slug":"invalid-data-uri-empty-base64-payload-would-prod","errorCode":null,"errorMessage":"Invalid data URI: empty base64 payload (would produce a 0-byte image).","messagePattern":"Invalid data URI: empty base64 payload \\(would produce a 0-byte image\\)\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Core/ImageSource.cs","lineNumber":142,"sourceCode":"        var header = dataUri[..commaIdx]; // e.g. \"data:image/png;base64\"\n        var data = dataUri[(commaIdx + 1)..];\n\n        if (!header.Contains(\"base64\", StringComparison.OrdinalIgnoreCase))\n            throw new ArgumentException(\"Only base64-encoded data URIs are supported\");\n\n        // Extract MIME type\n        var mimeStart = header.IndexOf(':') + 1;\n        var mimeEnd = header.IndexOf(';');\n        var mime = mimeEnd > mimeStart ? header[mimeStart..mimeEnd] : header[mimeStart..];\n\n        var contentType = MimeToContentType(mime);\n        var bytes = Convert.FromBase64String(data);\n        // An empty payload decodes \"successfully\" to zero bytes and would\n        // flow all the way into a 0-byte media part — schema-valid, but the\n        // picture renders broken in Word/PowerPoint. Reject up front, like\n        // the extpart carrier does for empty data.\n        if (bytes.Length == 0)\n            throw new ArgumentException(\"Invalid data URI: empty base64 payload (would produce a 0-byte image).\");\n        return (new MemoryStream(bytes), contentType);\n    }\n\n    private static (Stream, PartTypeInfo) ResolveUrl(string url)\n    {\n        // SSRF guard lives in the shared SsrfGuard so image and file fetch can\n        // never diverge in policy. See SsrfGuard for the connect-time / redirect\n        // / DNS-rebinding rationale.\n        var handler = SsrfGuard.CreateGuardedHandler(\"image\");\n\n        using var client = new HttpClient(handler, disposeHandler: true) { Timeout = TimeSpan.FromSeconds(30) };\n        client.DefaultRequestHeaders.Add(\"User-Agent\", \"OfficeCLI\");\n\n        var response = client.GetAsync(url).GetAwaiter().GetResult();\n        response.EnsureSuccessStatusCode();\n\n        // Enforce the shared size cap whether or not the server sends\n        // Content-Length. ReadBounded lives in SsrfGuard so image and file","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Core/ImageSource.cs#L124-L160","documentation":"Thrown by ImageSource.ResolveDataUri after the base64 payload is successfully decoded but the result is zero bytes. An empty payload is schema-valid and would be silently written as a 0-byte media part, but the picture would render broken in Word/PowerPoint. The guard rejects it up front so the user gets an actionable error instead of a silently corrupted document. This mirrors the extpart carrier's empty-data rejection.","triggerScenarios":"Passing a data URI like 'data:image/png;base64,' (base64 flag present, comma present, but the payload after the comma is empty or decodes to nothing). Also triggered by 'data:image/png;base64,=' or other base64 strings that decode to zero bytes. Convert.FromBase64String succeeds (no exception) but returns an empty array.","commonSituations":"An agent that builds a data URI from a variable that was never populated with image data. A truncation in the data URI where the payload was cut off. A template that left the payload empty as a placeholder. A base64 encoding bug that produced an empty string from valid input.","solutions":["Ensure the base64 payload contains actual encoded image bytes (not empty).","Verify the source image was read correctly before encoding it to base64.","If building the data URI programmatically, add a length check on the decoded bytes before constructing the URI."],"exampleFix":"// before — empty payload\nadd image src='data:image/png;base64,' path='/body'\n\n// after — include actual base64 image data\n// generate base64: base64 logo.png > logo.b64\nadd image src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUg...' path='/body'","handlingStrategy":"validation","validationCode":"// Pre-check that the base64 payload decodes to non-zero bytes\nif (source.StartsWith(\"data:\", StringComparison.OrdinalIgnoreCase))\n{\n    int commaIdx = source.IndexOf(',');\n    string payload = commaIdx >= 0 ? source[(commaIdx + 1)..] : \"\";\n    try\n    {\n        byte[] decoded = Convert.FromBase64String(payload);\n        if (decoded.Length == 0)\n        {\n            Console.Error.WriteLine(\"Data URI payload is empty. Provide actual image data.\");\n            return;\n        }\n    }\n    catch { /* let ImageSource handle the parse error */ }\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    var (stream, contentType) = ImageSource.Resolve(source);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"empty base64 payload\"))\n{\n    // The source image was empty — provide a valid image and retry\n}","preventionTips":["Verify the source image file is non-empty before base64-encoding it.","After encoding, check that the base64 string has meaningful content (not just '=' or empty).","Add a length check on decoded bytes in your data URI builder to catch encoding bugs early."],"tags":["image","data-uri","empty-payload","validation","base64"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}