Files
codebase-memory-mcp/internal/cbm/zstd_store.h
Martin Vogel a2f09c7ce9 artifact: size import decompression from the zstd frame header
The import path allocated the destination from the plaintext original_size
field in artifact.json and passed that value to the decoder truncated
through int. A large real database already wraps that cast (a full-kernel
index is ~14 GB), and a doctored field could additionally desync the decoder
capacity from the actual buffer. Read the decompressed size from the zstd
frame's own content-size header, require the metadata field to match it, cap
the total, and run the decoder with a size_t capacity equal to the
allocation. cbm_zstd_decompress now takes size_t lengths and returns int64_t.
Adds a regression test that a mismatched original_size is rejected.

Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
2026-07-07 20:41:06 +02:00

26 lines
1.1 KiB
C

#ifndef CBM_ZSTD_STORE_H
#define CBM_ZSTD_STORE_H
#include <stddef.h>
#include <stdint.h>
// Zstd compression at specified level (1=fast .. 22=best).
// Returns compressed size on success, 0 on error.
int cbm_zstd_compress(const char *src, int srcLen, char *dst, int dstCap, int level);
// Zstd decompression. srcLen/dstCap are size_t so a >2 GiB destination capacity
// is never truncated (a large DB artifact, or a crafted one, would otherwise
// wrap through int and desync the real buffer size from the decoder capacity).
// Returns decompressed size on success (>0), 0 on error.
int64_t cbm_zstd_decompress(const char *src, size_t srcLen, char *dst, size_t dstCap);
// Declared decompressed content size of a zstd frame (from its header), or 0
// when unknown/unreadable. Lets the caller size the destination from the frame
// itself instead of trusting a separate, attacker-controllable size field.
size_t cbm_zstd_frame_content_size(const char *src, size_t srcLen);
// Maximum compressed size bound for given input size.
size_t cbm_zstd_compress_bound(int inputSize);
#endif // CBM_ZSTD_STORE_H