Skip to content
Draft
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
37 changes: 36 additions & 1 deletion packages/rs-platform-wallet-ffi/src/asset_lock/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ use std::ffi::CString;
use std::os::raw::c_char;
use std::time::Duration;

const MAX_ASSET_LOCK_PROOF_SIZE_BYTES: usize = 16 * 1024 * 1024;

fn validate_asset_lock_proof_size(proof_len: usize) -> Result<(), PlatformWalletFFIResult> {
if proof_len > MAX_ASSET_LOCK_PROOF_SIZE_BYTES {
return Err(PlatformWalletFFIResult::err(
PlatformWalletFFIResultCode::ErrorInvalidParameter,
format!(
"asset lock proof length {proof_len} exceeds the {MAX_ASSET_LOCK_PROOF_SIZE_BYTES}-byte limit"
),
));
}
Ok(())
}

/// Build an `OutPoint` from a 32-byte raw txid pointer and a vout.
///
/// **FFI invariant:** the `txid` parameter is typed `*const [u8; 32]`,
Expand Down Expand Up @@ -264,10 +278,13 @@ pub unsafe extern "C" fn asset_lock_manager_recover(

// Parse optional proof
let proof = if !proof_bytes.is_null() && proof_len > 0 {
// A proof contains one Core transaction and, at most, one InstantLock;
// 16 MiB is generous while matching the persisted-blob ceiling.
unwrap_result_or_return!(validate_asset_lock_proof_size(proof_len));
let data = std::slice::from_raw_parts(proof_bytes, proof_len);
let (p, _) = unwrap_result_or_return!(dpp::bincode::decode_from_slice(
data,
dpp::bincode::config::standard()
dpp::bincode::config::standard().with_limit::<MAX_ASSET_LOCK_PROOF_SIZE_BYTES>()
));
Some(p)
} else {
Expand All @@ -288,3 +305,21 @@ pub unsafe extern "C" fn asset_lock_manager_recover(
unwrap_option_or_return!(option);
PlatformWalletFFIResult::ok()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn should_reject_oversized_asset_lock_proof_before_decode() {
assert!(validate_asset_lock_proof_size(MAX_ASSET_LOCK_PROOF_SIZE_BYTES).is_ok());

let result = validate_asset_lock_proof_size(MAX_ASSET_LOCK_PROOF_SIZE_BYTES + 1)
.expect_err("oversized proof must fail");

assert_eq!(
result.code,
PlatformWalletFFIResultCode::ErrorInvalidParameter
);
}
}
Loading