From a992e89766b371500f55387c71a4aa380d288d30 Mon Sep 17 00:00:00 2001 From: Carl Niklas Rydberg Date: Sun, 21 Dec 2025 23:18:12 +0100 Subject: [PATCH] =?UTF-8?q?Made=20StoreConfig=20enforcement=20pluggable=20?= =?UTF-8?q?at=20the=20amduat=5Fasl=5Fstore=5Ft=20boundary=20by=20adding=20?= =?UTF-8?q?a=20validate=5Fconfig=20hook=20to=20store=20ops.=20validate=5Fc?= =?UTF-8?q?onfig=20is=20now=20optional,=20get=20rejects=20refs=20whose=20h?= =?UTF-8?q?ash=5Fid=20doesn=E2=80=99t=20match=20the=20store=20config,=20an?= =?UTF-8?q?d=20put=20verifies=20the=20returned=20Reference=20matches=20the?= =?UTF-8?q?=20configured=20hash=5Fid.=20This=20uses=20the=20StoreConfig=20?= =?UTF-8?q?at=20the=20boundary=20without=20forcing=20every=20store=20to=20?= =?UTF-8?q?implement=20the=20hook.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/amduat/asl/store.h | 4 ++ src/adapters/asl_store_fs/asl_store_fs.c | 24 ++++++++++++ src/near_core/asl/store.c | 48 ++++++++++++------------ 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/include/amduat/asl/store.h b/include/amduat/asl/store.h index ed371d5..1becd3e 100644 --- a/include/amduat/asl/store.h +++ b/include/amduat/asl/store.h @@ -11,6 +11,7 @@ extern "C" { #endif +/* Zero-initialize before setting fields to keep future-compatibility. */ typedef struct { amduat_asl_encoding_profile_id_t encoding_profile_id; amduat_hash_id_t hash_id; @@ -31,6 +32,9 @@ typedef struct { amduat_asl_store_error_t (*get)(void *ctx, amduat_reference_t ref, amduat_artifact_t *out_artifact); + amduat_asl_store_error_t (*validate_config)( + void *ctx, + amduat_asl_store_config_t config); } amduat_asl_store_ops_t; typedef struct { diff --git a/src/adapters/asl_store_fs/asl_store_fs.c b/src/adapters/asl_store_fs/asl_store_fs.c index 2fa13bb..8f2dae2 100644 --- a/src/adapters/asl_store_fs/asl_store_fs.c +++ b/src/adapters/asl_store_fs/asl_store_fs.c @@ -489,6 +489,28 @@ static amduat_asl_store_error_t amduat_asl_store_fs_put_impl( return AMDUAT_ASL_STORE_OK; } +static amduat_asl_store_error_t amduat_asl_store_fs_validate_config( + void *ctx, + amduat_asl_store_config_t config) { + const amduat_hash_asl1_desc_t *hash_desc; + + (void)ctx; + + if (config.encoding_profile_id != AMDUAT_ENC_ASL1_CORE_V1) { + return AMDUAT_ASL_STORE_ERR_UNSUPPORTED; + } + hash_desc = amduat_hash_asl1_desc_lookup(config.hash_id); + if (hash_desc == NULL || hash_desc->digest_len == 0 || + hash_desc->impl.digest == NULL) { + return AMDUAT_ASL_STORE_ERR_UNSUPPORTED; + } + if (hash_desc->digest_len < AMDUAT_ASL_STORE_FS_MIN_DIGEST_BYTES) { + return AMDUAT_ASL_STORE_ERR_UNSUPPORTED; + } + + return AMDUAT_ASL_STORE_OK; +} + static amduat_asl_store_error_t amduat_asl_store_fs_get_impl( void *ctx, amduat_reference_t ref, @@ -671,7 +693,9 @@ bool amduat_asl_store_fs_init(amduat_asl_store_fs_t *fs, amduat_asl_store_ops_t amduat_asl_store_fs_ops(void) { amduat_asl_store_ops_t ops; + memset(&ops, 0, sizeof(ops)); ops.put = amduat_asl_store_fs_put_impl; ops.get = amduat_asl_store_fs_get_impl; + ops.validate_config = amduat_asl_store_fs_validate_config; return ops; } diff --git a/src/near_core/asl/store.c b/src/near_core/asl/store.c index b452074..847988a 100644 --- a/src/near_core/asl/store.c +++ b/src/near_core/asl/store.c @@ -1,23 +1,11 @@ #include "amduat/asl/store.h" -#include "amduat/enc/asl1_core.h" -#include "amduat/hash/asl1.h" -static bool amduat_asl_store_config_supported( - const amduat_asl_store_config_t *config) { - const amduat_hash_asl1_desc_t *hash_desc; - - if (config == NULL) { - return false; +static amduat_asl_store_error_t amduat_asl_store_validate_config( + amduat_asl_store_t *store) { + if (store == NULL || store->ops.validate_config == NULL) { + return AMDUAT_ASL_STORE_OK; } - if (amduat_enc_asl1_core_desc_lookup(config->encoding_profile_id) == NULL) { - return false; - } - hash_desc = amduat_hash_asl1_desc_lookup(config->hash_id); - if (hash_desc == NULL || hash_desc->digest_len == 0 || - hash_desc->impl.digest == NULL) { - return false; - } - return true; + return store->ops.validate_config(store->ctx, store->config); } void amduat_asl_store_init(amduat_asl_store_t *store, @@ -35,26 +23,40 @@ void amduat_asl_store_init(amduat_asl_store_t *store, amduat_asl_store_error_t amduat_asl_store_put(amduat_asl_store_t *store, amduat_artifact_t artifact, amduat_reference_t *out_ref) { + amduat_asl_store_error_t cfg_err; + amduat_asl_store_error_t store_err; + if (store == NULL || store->ops.put == NULL) { return AMDUAT_ASL_STORE_ERR_UNSUPPORTED; } - if (!amduat_asl_store_config_supported(&store->config)) { - return AMDUAT_ASL_STORE_ERR_UNSUPPORTED; + cfg_err = amduat_asl_store_validate_config(store); + if (cfg_err != AMDUAT_ASL_STORE_OK) { + return cfg_err; } - return store->ops.put(store->ctx, artifact, out_ref); + store_err = store->ops.put(store->ctx, artifact, out_ref); + if (store_err != AMDUAT_ASL_STORE_OK) { + return store_err; + } + if (out_ref != NULL && out_ref->hash_id != store->config.hash_id) { + return AMDUAT_ASL_STORE_ERR_INTEGRITY; + } + return AMDUAT_ASL_STORE_OK; } amduat_asl_store_error_t amduat_asl_store_get(amduat_asl_store_t *store, amduat_reference_t ref, amduat_artifact_t *out_artifact) { + amduat_asl_store_error_t cfg_err; + if (store == NULL || store->ops.get == NULL) { return AMDUAT_ASL_STORE_ERR_UNSUPPORTED; } - if (!amduat_asl_store_config_supported(&store->config)) { - return AMDUAT_ASL_STORE_ERR_UNSUPPORTED; - } if (ref.hash_id != store->config.hash_id) { return AMDUAT_ASL_STORE_ERR_UNSUPPORTED; } + cfg_err = amduat_asl_store_validate_config(store); + if (cfg_err != AMDUAT_ASL_STORE_OK) { + return cfg_err; + } return store->ops.get(store->ctx, ref, out_artifact); }