{"record":{"id":"98e0bbda12c26105","repo":"LykosAI/StabilityMatrix","slug":"invalid-webp-header","errorCode":null,"errorMessage":"Invalid WEBP header","messagePattern":"Invalid WEBP header","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Core/Helper/Webp/WebpReader.cs","lineNumber":40,"sourceCode":"    }\n\n    private void ReadHeader()\n    {\n        // RIFF\n        var riff = ReadBytes(4);\n        if (!riff.SequenceEqual([..\"RIFF\"u8]))\n        {\n            throw new InvalidDataException(\"Invalid RIFF header\");\n        }\n\n        // Size: uint32\n        headerFileSize = ReadUInt32();\n\n        // WEBP\n        var webp = ReadBytes(4);\n        if (!webp.SequenceEqual([..\"WEBP\"u8]))\n        {\n            throw new InvalidDataException(\"Invalid WEBP header\");\n        }\n    }\n\n    // Read a single chunk and discard its contents\n    private string ReadVoidChunk()\n    {\n        // FourCC: 4 bytes in ASCII\n        var result = ReadBytes(4);\n\n        // Size: uint32\n        var size = ReadUInt32();\n\n        BaseStream.Seek(size, SeekOrigin.Current);\n\n        return Encoding.ASCII.GetString(result);\n    }\n}\n","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Core/Helper/Webp/WebpReader.cs#L22-L58","documentation":"WebpReader.ReadHeader validates the WEBP signature at bytes 8-11 (after the RIFF marker and size field). If those bytes are not 'WEBP' it throws InvalidDataException: the stream is a RIFF container but not a WEBP file (e.g. a WAV or AVI), or the header is corrupt.","triggerScenarios":"Calling GetIsAnimatedFlag on a non-WEBP RIFF file (WAV/AVI), a file with a corrupted or tampered header, or a stream shorter than 12 bytes so the WEBP read pulls garbage.","commonSituations":"Files renamed to .webp without conversion; truncated downloads where the size field parsed but format bytes are wrong; batch metadata scans over mixed image/audio directories.","solutions":["Check bytes 8-11 equal 'WEBP' (and bytes 0-3 equal 'RIFF') before parsing","Only feed WebpReader streams known to be actual WEBP files","Catch InvalidDataException and classify the file as unsupported","Re-download/reconvert the file if it is genuinely meant to be WEBP"],"exampleFix":"// before\nvar animated = new WebpReader(stream).GetIsAnimatedFlag();\n// after\nvar bytes = new byte[12];\nstream.ReadExactly(bytes);\nstream.Position = 0;\nbool isWebp = bytes.AsSpan(0, 4).SequenceEqual(\"RIFF\"u8)\n           && bytes.AsSpan(8, 4).SequenceEqual(\"WEBP\"u8);\nvar animated = isWebp ? new WebpReader(stream).GetIsAnimatedFlag() : false;","handlingStrategy":"validation","validationCode":"byte[] head = new byte[12];\nusing (var fs = File.OpenRead(path)) { if (fs.Length < 12) return false; fs.ReadExactly(head); }\nbool isWebp = head.AsSpan(0,4).SequenceEqual(\"RIFF\"u8) && head.AsSpan(8,4).SequenceEqual(\"WEBP\"u8);","typeGuard":"static bool IsWebpFile(Stream s) { var b = new byte[12]; long pos = s.Position; if (s.Length - pos < 12) return false; s.ReadExactly(b); s.Position = pos; return b.AsSpan(0,4).SequenceEqual(\"RIFF\"u8) && b.AsSpan(8,4).SequenceEqual(\"WEBP\"u8); }","tryCatchPattern":"try { animated = new WebpReader(stream).GetIsAnimatedFlag(); }\ncatch (InvalidDataException ex) { Logger.Debug(\"Not a WEBP: {Msg}\", ex.Message); animated = false; }","preventionTips":["Check both RIFF and WEBP signatures before reading","Reject files shorter than the 12-byte header","Scan directories with extension+magic validation","Re-download files whose headers look corrupt"],"tags":["csharp","parsing","webp","file-format"],"backgroundTag":"invalid-argument-format","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}