mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 08:15:29 +00:00
Port EVM inspection: Reject '+' signs in hex of EVM metadata solc and ipfs values (#1971)
Source: storytold/craft-anything@86c2dd0227f4ee8b2f953dc8b00a9d1426032e11:crates/formats/src/evm.rs
This commit is contained in:
1 parent
eb27a0d8ed
commit
9d718a8741
1 file changed
+22
-2
+22
-2
@@ -349,7 +349,7 @@ fn cbor_item(b: &[u8], pos: &mut usize, depth: u32) -> Option<Value> {
|
||||
Value::from(items.iter().map(|v| v.to_string()).collect::<Vec<_>>().join("."))
|
||||
}
|
||||
("solc", Value::String(s)) if s.starts_with("0x") && s.len() == 8 => {
|
||||
let d: Vec<u8> = (0..3).filter_map(|i| u8::from_str_radix(s.get(2 + 2 * i..4 + 2 * i)?, 16).ok()).collect();
|
||||
let d: Vec<u8> = (0..3).filter_map(|i| hex_byte(s.get(2 + 2 * i..4 + 2 * i)?)).collect();
|
||||
if let [a, bb, c] = d.as_slice() { Value::from(format!("{a}.{bb}.{c}")) } else { v }
|
||||
}
|
||||
("ipfs", Value::String(s)) if s.starts_with("0x1220") => Value::from(base58(&hex_decode(s.get(2..).unwrap_or("")))),
|
||||
@@ -378,8 +378,13 @@ fn bytes_value(s: &[u8]) -> String {
|
||||
out
|
||||
}
|
||||
|
||||
/// One byte from two hex digits; `from_str_radix` alone would also accept a leading `+`.
|
||||
fn hex_byte(pair: &str) -> Option<u8> {
|
||||
if pair.bytes().all(|c| c.is_ascii_hexdigit()) { u8::from_str_radix(pair, 16).ok() } else { None }
|
||||
}
|
||||
|
||||
fn hex_decode(s: &str) -> Vec<u8> {
|
||||
(0..s.len() / 2).filter_map(|i| u8::from_str_radix(s.get(2 * i..2 * i + 2)?, 16).ok()).collect()
|
||||
(0..s.len() / 2).filter_map(|i| hex_byte(s.get(2 * i..2 * i + 2)?)).collect()
|
||||
}
|
||||
|
||||
fn base58(b: &[u8]) -> String {
|
||||
@@ -717,6 +722,21 @@ mod tests {
|
||||
use super::*;
|
||||
use craft_anything_testkit::formats as fx;
|
||||
|
||||
#[test]
|
||||
fn hex_signs_are_not_hex_digits() {
|
||||
let solc = |v: &str| {
|
||||
let mut b = vec![0xa1, 0x64];
|
||||
b.extend(b"solc");
|
||||
b.push(0x60 + v.len() as u8);
|
||||
b.extend(v.as_bytes());
|
||||
cbor_item(&b, &mut 0, 0).unwrap()["solc"].clone()
|
||||
};
|
||||
assert_eq!(solc("0x+1+2+3"), "0x+1+2+3");
|
||||
assert_eq!(solc("0x000008"), "0.0.8");
|
||||
assert!(hex_decode("+f").is_empty());
|
||||
assert_eq!(hex_decode("0f"), vec![15]);
|
||||
}
|
||||
|
||||
/// A plausible trailer length alone must not remove instructions from the analyzed bytecode.
|
||||
#[test]
|
||||
fn incomplete_cbor_must_not_shorten_the_analyzed_contract() {
|
||||
|
||||
Reference in new issue
Block a user