Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/cli/src/cli/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,16 @@ mod tests {
};
assert!(validate(v).is_err());
}

/// Arbitrary bytes are not a valid authoring-meta-v2.
#[test]
fn test_validate_err_for_arbitrary_authoring_meta_v2() {
let mut file = tempfile::NamedTempFile::new().unwrap();
file.write_all(&[0xde, 0xad]).unwrap();
let v = Validate {
meta: KnownMeta::AuthoringMetaV2,
input_path: file.path().to_path_buf(),
};
assert!(validate(v).is_err());
}
}
60 changes: 58 additions & 2 deletions crates/cli/src/meta/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use super::{
KnownMeta,
super::error::Error,
types::{
op::v1::OpMeta, authoring::v1::AuthoringMeta, solidity_abi::v2::SolidityAbiMeta,
interpreter_caller::v1::InterpreterCallerMeta,
op::v1::OpMeta, authoring::v1::AuthoringMeta, authoring::v2::AuthoringMetaV2,
solidity_abi::v2::SolidityAbiMeta, interpreter_caller::v1::InterpreterCallerMeta,
},
};

Expand Down Expand Up @@ -34,6 +34,13 @@ impl KnownMeta {
)?,
}
}
KnownMeta::AuthoringMetaV2 => {
// v2 is abi encoded onchain and this crate has no encoder for
// it, so validation is a decode gate over the input as is
AuthoringMetaV2::abi_decode(data)
.map_err(|e| Error::InvalidInput(e.to_string()))?;
data.to_vec()
}
// rest of meta types are only pure bytes (ut8 strings or binary)
// so no normalization/validation can happen for them at this level
_ => data.to_vec(),
Expand All @@ -43,10 +50,20 @@ impl KnownMeta {

#[cfg(all(test, not(target_family = "wasm")))]
mod tests {
use alloy::sol_types::SolValue;
use crate::error::Error;
use crate::meta::types::authoring::v1::{AuthoringMeta, AuthoringMetaItem};
use crate::meta::types::authoring::v2::AuthoringMetaV2Sol;
use crate::meta::KnownMeta;

fn authoring_meta_v2_abi(word: [u8; 32], description: &str) -> Vec<u8> {
vec![AuthoringMetaV2Sol {
word: word.into(),
description: description.to_string(),
}]
.abi_encode()
}

/// OpV1 normalizes valid metadata to its canonical compact json form.
#[test]
fn test_normalize_op_v1_canonicalizes() {
Expand Down Expand Up @@ -159,6 +176,45 @@ mod tests {
assert_ne!(normalized, json.as_bytes().to_vec());
}

/// AuthoringMetaV2 has a concrete abi encoding, so a decodable payload is
/// valid and passes through byte identically.
#[test]
fn test_normalize_authoring_meta_v2_abi_passthrough() {
let mut word = [0u8; 32];
word[..5].copy_from_slice(b"stack");
let abi = authoring_meta_v2_abi(word, "Copies an existing value from the stack.");
assert_eq!(KnownMeta::AuthoringMetaV2.normalize(&abi).unwrap(), abi);
}

/// AuthoringMetaV2 is not pure bytes: bytes that cannot abi decode as
/// AuthoringMetaV2Sol[] are rejected rather than passed through.
#[test]
fn test_normalize_authoring_meta_v2_rejects_arbitrary_bytes() {
assert!(matches!(
KnownMeta::AuthoringMetaV2.normalize(&[0xde, 0xad]),
Err(Error::InvalidInput(_))
));
assert!(matches!(
KnownMeta::AuthoringMetaV2.normalize(b"[]"),
Err(Error::InvalidInput(_))
));
}

/// The decode gate carries the word utf8 requirement: abi shaped bytes
/// whose word is not utf8 before its first NUL are rejected.
#[test]
fn test_normalize_authoring_meta_v2_rejects_non_utf8_word() {
let mut word = [0u8; 32];
// 0xc3 followed by 0x28 is an invalid utf8 sequence, before any NUL
word[0] = 0xc3;
word[1] = 0x28;
let abi = authoring_meta_v2_abi(word, "bad word bytes");
assert!(matches!(
KnownMeta::AuthoringMetaV2.normalize(&abi),
Err(Error::InvalidInput(_))
));
}

/// Meta types without a structured normal form pass raw bytes through
/// unchanged.
#[test]
Expand Down
Loading