{"record":{"id":"329bf39c06d88ade","repo":"can1357/oh-my-pi","slug":"invalid-xz-stream-backward-index-size-is-invalid","errorCode":null,"errorMessage":"Invalid XZ stream: backward index size is invalid","messagePattern":"Invalid XZ stream: backward index size is invalid","errorType":"exception","errorClass":"ArchiveError","httpStatus":null,"severity":"error","filePath":"packages/utils/src/ar/codecs/xz.ts","lineNumber":130,"sourceCode":"\t\twhile (end >= 4 && bytes[end - 1] === 0 && bytes[end - 2] === 0 && bytes[end - 3] === 0 && bytes[end - 4] === 0) {\n\t\t\tend -= 4;\n\t\t\tpadding += 4;\n\t\t}\n\t\tif (end === 0) throw new ArchiveError(\"Invalid XZ stream: padding without a stream\");\n\t\tif (end < 24) throw new ArchiveError(\"Invalid XZ stream: truncated stream framing\");\n\t\tconst footerStart = end - 12;\n\t\tif (bytes[footerStart + 10] !== 0x59 || bytes[footerStart + 11] !== 0x5a)\n\t\t\tthrow new ArchiveError(\"Invalid XZ stream: footer magic mismatch\");\n\t\tif (crc32(bytes.subarray(footerStart + 4, footerStart + 10)) !== read32LE(bytes, footerStart))\n\t\t\tthrow new ArchiveError(\"Invalid XZ stream: footer CRC32 mismatch\");\n\t\tconst flag0 = bytes[footerStart + 8]!;\n\t\tconst flag1 = bytes[footerStart + 9]!;\n\t\tif (flag0 !== 0 || (flag1 & 0xf0) !== 0) throw new ArchiveError(\"Unsupported XZ stream flags\");\n\t\tconst checkId = flag1 & 0x0f;\n\t\tcheckSize(checkId);\n\t\tconst indexSize = (read32LE(bytes, footerStart + 4) + 1) * 4;\n\t\tif (!Number.isSafeInteger(indexSize) || indexSize > footerStart)\n\t\t\tthrow new ArchiveError(\"Invalid XZ stream: backward index size is invalid\");\n\t\tconst indexStart = footerStart - indexSize;\n\t\tconst records = parseIndex(bytes, indexStart, indexSize);\n\t\tlet blocksSize = 0;\n\t\tfor (const record of records) {\n\t\t\tblocksSize += Math.ceil(record.unpaddedSize / 4) * 4;\n\t\t\tif (!Number.isSafeInteger(blocksSize)) throw new ArchiveError(\"XZ stream uses sizes too large to read safely\");\n\t\t}\n\t\tconst start = indexStart - blocksSize - 12;\n\t\tif (start < 0 || start + 12 > bytes.byteLength || !equalBytes(bytes.subarray(start, start + 6), XZ_MAGIC)) {\n\t\t\tthrow new ArchiveError(\"Invalid XZ stream: header position or magic is invalid\");\n\t\t}\n\t\tif (bytes[start + 6] !== flag0 || bytes[start + 7] !== flag1)\n\t\t\tthrow new ArchiveError(\"Invalid XZ stream: header and footer flags differ\");\n\t\tif (crc32(bytes.subarray(start + 6, start + 8)) !== read32LE(bytes, start + 8))\n\t\t\tthrow new ArchiveError(\"Invalid XZ stream: header CRC32 mismatch\");\n\t\tstreams.unshift({ start, indexStart, footerStart, checkId, records });\n\t\tend = start;\n\t\tvoid padding;","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/utils/src/ar/codecs/xz.ts#L112-L148","documentation":"The footer's backward-size field encodes the size of the stream index as (stored+1)*4 bytes. discoverStreams() validates this is a safe integer and that the index actually fits before the footer; a stored size that overflows or points before the stream start means the footer is corrupt or the buffer is not a real XZ stream.","triggerScenarios":"A backward-size field (footerStart+4..7) corrupted to an implausible value, a misaligned buffer slice so garbage is read as the size, or a deliberately malformed file whose declared index is larger than the preceding bytes.","commonSituations":"Corrupted downloads, fuzzed/malformed archives, or hand-assembled XZ streams with an incorrect index size encoding.","solutions":["Verify the file integrity (xz -t or checksum) and re-acquire it if corrupt","Re-compress the data to regenerate a consistent footer/index","If slicing buffers, confirm the footer offset is exactly end-12 for the stream","Reject untrusted archives up front if you know they come from an unreliable source"],"exampleFix":"// before\nconst slice = whole.subarray(whole.byteLength - 10); // shifts footer fields\nawait xzDecode(slice); // bogus backward size\n// after\nconst slice = whole; // parse full streams, library locates the footer itself\nawait xzDecode(slice);","handlingStrategy":"validation","validationCode":"if (bytes.byteLength % 4 !== 0 || bytes.byteLength < 24) throw new Error(\"not plausibly an xz buffer\");","typeGuard":"null","tryCatchPattern":"try {\n  await xzDecode(bytes);\n} catch (err) {\n  if (err instanceof ArchiveError && err.message.includes(\"backward index size\")) {\n    throw new Error(\"XZ index/footer inconsistent — archive is corrupt or malformed\");\n  }\n  throw err;\n}","preventionTips":["Only decode archives from trusted pipelines","Pass full buffers so the library computes offsets itself","Re-compress instead of patching compressed bytes"],"tags":["xz","archive","corrupt-input","validation"],"backgroundTag":"invalid-archive-format","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}