Skip to content

Commit

Permalink
reject Hash<N> as public user function input
Browse files Browse the repository at this point in the history
  • Loading branch information
jayz22 committed Apr 17, 2024
1 parent 1c0e4b7 commit c5550c6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
24 changes: 23 additions & 1 deletion soroban-sdk-macros/src/derive_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,29 @@ pub fn derive_fn(
.skip(if env_input.is_some() { 1 } else { 0 })
.enumerate()
.map(|(i, a)| match a {
FnArg::Typed(_) => {
FnArg::Typed(pat_ty) => {
if ident != "__check_auth" {
let mut ty = &*pat_ty.ty;
if let Type::Reference(TypeReference { elem, .. }) = ty {
ty = elem;
}
if let Type::Path(TypePath {
path: syn::Path { segments, .. },
..
}) = ty
{
if segments.last().map_or(false, |s| s.ident == "Hash" && !s.arguments.is_none()) {
errors.push(Error::new(a.span(), "`Hash<T>` cannot be used as argument to a public user function,
since there is no guarantee the received input is from a secure hash function.
If you still intend to use a hash with such a guarantee, please use `ByteN`"));
} else {
()
}
} else {
()
}
}

let ident = format_ident!("arg_{}", i);
let arg = FnArg::Typed(PatType {
attrs: vec![],
Expand Down
9 changes: 9 additions & 0 deletions soroban-sdk-macros/src/derive_spec_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ pub fn derive_fn_spec(
));
StringM::<MAX>::default()
});

if let ScSpecTypeDef::Hash(_) = type_ {
if ident != "__check_auth" {
errors.push(Error::new(a.span(), "`Hash<T>` cannot be used as argument to a public user function,
since there is no guarantee the received input is from a secure hash function.
If you still intend to use a hash with such a guarantee, please use `ByteN`"));
}
}

ScSpecFunctionInputV0 {
doc: "".try_into().unwrap(),
name,
Expand Down

0 comments on commit c5550c6

Please sign in to comment.