Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
artifact: size import decompression from the zstd frame header
Martin Vogela2f09c7ce972
diff --git a/internal/cbm/zstd_store.c b/internal/cbm/zstd_store.cindex 732f64daa..509537419 100644--- a/internal/cbm/zstd_store.c+++ b/internal/cbm/zstd_store.c@@ -5,6 +5,7 @@#include "zstd_store.h"#include <stddef.h>+#include <stdint.h>int cbm_zstd_compress(const char *src, int srcLen, char *dst, int dstCap, int level) {size_t rc = ZSTD_compress(dst, (size_t)dstCap, src, (size_t)srcLen, level);@@ -14,12 +15,20 @@ int cbm_zstd_compress(const char *src, int srcLen, char *dst, int dstCap, int lereturn (int)rc;}-int cbm_zstd_decompress(const char *src, int srcLen, char *dst, int dstCap) {- size_t rc = ZSTD_decompress(dst, (size_t)dstCap, src, (size_t)srcLen);+int64_t cbm_zstd_decompress(const char *src, size_t srcLen, char *dst, size_t dstCap) {+ size_t rc = ZSTD_decompress(dst, dstCap, src, srcLen);if (ZSTD_isError(rc)) {return 0;}- return (int)rc;+ return (int64_t)rc;+}++size_t cbm_zstd_frame_content_size(const char *src, size_t srcLen) {+ unsigned long long n = ZSTD_getFrameContentSize(src, srcLen);+ if (n == ZSTD_CONTENTSIZE_UNKNOWN || n == ZSTD_CONTENTSIZE_ERROR) {+ return 0;+ }+ return (size_t)n;}size_t cbm_zstd_compress_bound(int inputSize) {diff --git a/internal/cbm/zstd_store.h b/internal/cbm/zstd_store.hindex b854f736e..ef427df04 100644--- a/internal/cbm/zstd_store.h+++ b/internal/cbm/zstd_store.h@@ -2,14 +2,22 @@#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.-// Returns decompressed size on success, 0 on error.-int cbm_zstd_decompress(const char *src, int srcLen, char *dst, int dstCap);+// 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);diff --git a/src/pipeline/artifact.c b/src/pipeline/artifact.cindex c7a8f451a..9e27e944f 100644--- a/src/pipeline/artifact.c+++ b/src/pipeline/artifact.c@@ -15,6 +15,13 @@ enum {};#define ART_BYTES_PER_MB ((size_t)1024 * 1024)+/* Generous ceiling on an imported artifact's decompressed size. Real indexes+ * (a full Linux-kernel DB is ~14 GB) fit comfortably; a frame that declares+ * more than this is rejected before any allocation so a crafted content size+ * can neither trigger a runaway allocation nor be used to desync the decoder+ * capacity from the destination buffer. */+#define ART_MAX_DECOMPRESSED_BYTES ((size_t)64 * 1024 * ART_BYTES_PER_MB)+#include "pipeline/artifact.h"#include "store/store.h"#include "foundation/platform.h"@@ -614,16 +621,29 @@ int cbm_artifact_import(const char *repo_path, const char *cache_db_path) {}/* Decompress */- char *decompressed = malloc(original_size);+ /* Size the destination from the zstd frame's own content-size header, not+ * from the separately-stored (attacker-controllable) original_size field.+ * The allocation and the decoder capacity are then the SAME size_t value,+ * so a crafted size can never make the capacity exceed the real buffer+ * (the int-truncation that used to do exactly that is gone with the size_t+ * signature). Require the metadata field to agree, and cap the total. */+ size_t frame_size = cbm_zstd_frame_content_size(compressed, clen);+ if (frame_size == 0 || frame_size > ART_MAX_DECOMPRESSED_BYTES || frame_size != original_size) {+ free(compressed);+ cbm_log_error("artifact.import", "err", "bad_decompressed_size");+ return CBM_NOT_FOUND;+ }++ char *decompressed = malloc(frame_size);if (!decompressed) {free(compressed);return CBM_NOT_FOUND;}- int dlen = cbm_zstd_decompress(compressed, (int)clen, decompressed, (int)original_size);+ int64_t dlen = cbm_zstd_decompress(compressed, clen, decompressed, frame_size);free(compressed);- if (dlen <= 0) {+ if (dlen <= 0 || (size_t)dlen != frame_size) {free(decompressed);cbm_log_error("artifact.import", "err", "zstd_decompress");return CBM_NOT_FOUND;diff --git a/tests/test_artifact.c b/tests/test_artifact.cindex f87be801b..0e39bb17f 100644--- a/tests/test_artifact.c+++ b/tests/test_artifact.c@@ -94,6 +94,74 @@ static const char *capture_logs_end(void) {/* ── Tests ───────────────────────────────────────────────────────── */+/* Rewrite the "original_size" number in an artifact.json in place, adding+ * `delta` to it. Returns false if the field / a digit run isn't found. */+static bool bump_artifact_original_size(const char *meta_path, long delta) {+ FILE *fp = fopen(meta_path, "rb");+ if (!fp) {+ return false;+ }+ char buf[4096];+ size_t n = fread(buf, 1, sizeof(buf) - 1, fp);+ fclose(fp);+ buf[n] = '\0';+ char *key = strstr(buf, "\"original_size\"");+ if (!key) {+ return false;+ }+ char *colon = strchr(key, ':');+ if (!colon) {+ return false;+ }+ char *ds = colon + 1;+ while (*ds == ' ' || *ds == '\t') {+ ds++;+ }+ char *de = ds;+ while (*de >= '0' && *de <= '9') {+ de++;+ }+ if (de == ds) {+ return false;+ }+ long val = strtol(ds, NULL, 10) + delta;+ char out[4096];+ int pre = (int)(ds - buf);+ snprintf(out, sizeof(out), "%.*s%ld%s", pre, buf, val, de);+ fp = fopen(meta_path, "wb");+ if (!fp) {+ return false;+ }+ fwrite(out, 1, strlen(out), fp);+ fclose(fp);+ return true;+}++/* The decompressed size is driven by the zstd frame's own content-size header,+ * not the separately-stored original_size field (which travels in plaintext+ * artifact.json and is trivially editable). A mismatch between the two must be+ * rejected — this is the check that keeps the destination allocation and the+ * decoder capacity pinned to the same verified size, so a doctored size can+ * never make the decoder write past the buffer. */+TEST(artifact_import_rejects_size_mismatch) {+ setup_artifact_test();+ create_test_db(g_db);+ ASSERT_EQ(cbm_artifact_export(g_db, g_repo, "test-proj", CBM_ARTIFACT_FAST), 0);++ char meta[1024];+ snprintf(meta, sizeof(meta), "%s/.codebase-memory/artifact.json", g_repo);+ ASSERT_TRUE(+ bump_artifact_original_size(meta, 4096)); /* claim 4 KiB more than the frame holds */++ char import_db[1024];+ snprintf(import_db, sizeof(import_db), "%s/imported.db", g_tmpdir);+ int rc = cbm_artifact_import(g_repo, import_db);+ ASSERT_NEQ(rc, 0); /* must reject the mismatch, not import on the doctored size */++ cleanup_dir(g_tmpdir);+ PASS();+}+TEST(artifact_export_fast_roundtrip) {setup_artifact_test();create_test_db(g_db);@@ -374,5 +442,6 @@ SUITE(artifact) {RUN_TEST(artifact_gitattributes_created);RUN_TEST(artifact_export_rename_failure_logs_specific_error);RUN_TEST(pipeline_persistence_export_failure_returns_error);+ RUN_TEST(artifact_import_rejects_size_mismatch);RUN_TEST(artifact_null_safety);}