fix: validate SVM optional parameter presence - #1449
Conversation
Signed-off-by: Reinis Martinsons <reinis@umaproject.org>
9fd883e to
9b62bf2
Compare
Signed-off-by: Reinis Martinsons <reinis@umaproject.org>
Signed-off-by: Reinis Martinsons <reinis@umaproject.org>
# Conflicts: # package.json
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9b619aa238
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| constraint = has_valid_params_presence( | ||
| &[relay_data.is_some(), repayment_chain_id.is_some(), repayment_address.is_some()], | ||
| instruction_params.is_some()) @ SvmError::InconsistentOptionalParameters, |
There was a problem hiding this comment.
Move fill optional-parameter check before unwrapping
When fill_relay is called with relay_data set to None and the optional instruction_params account omitted, this constraint is never reached: Anchor validates the earlier mint and recipient_token_account constraints first, and both unwrap instruction_params.as_ref().unwrap() while it is None. That inconsistent-parameter case still aborts with a panic/program failure instead of returning InconsistentOptionalParameters, so the new validation should be attached to an earlier account such as state before any constraint reads the optional parameters.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Thanks for flagging this. I checked the actual generated Rust at 9b619aa238bccc89226bc4904b1dcec77a942c77 using Anchor CLI 0.31.1:
anchor expand -p svm_spokeThe generated FillRelay::try_accounts does not validate the ordinary account constraints in struct declaration order. Its relevant processing order is:
- Deserialize the instruction arguments and parse/extract all account wrappers. This does not evaluate the
mintor recipient constraint expressions. - Process the
fill_statusinit_if_neededblock. - Run
has_valid_params_presence(...); on therelay_data = Noneplus omittedinstruction_paramscase, this returnsInconsistentOptionalParameters. - Only after that, run
is_relay_hash_valid(...), which contains the first relevant unwrap. - Later, run the
mintaddress constraint and then the recipient ATA constraint, which contain the other unwraps.
This ordering follows directly from the Anchor 0.31.1 generator:
try_accountsemits field deserialization before the constraints block, and the generated function placesdeser_fieldsbeforeconstraints(lines 117-135).- More importantly,
generate_constraintsemits everyinit_fieldbefore allaccess_checksfor non-init fields.fill_statusis the init field here, whilemintandrecipient_token_accountare non-init fields. - Within each field, Anchor linearizes
init, seeds, mutability, and raw/custom constraints before address/token/mint constraints.
The expansion therefore confirms that the presence check is reached before any of these optional-parameter unwraps. Moving it to state is not necessary for this case, and the current placement is safe against the panic described here.
🤖 Reinis Codex coding agent
Summary
Adds consistency validation for SVM instructions that can load optional parameters either from instruction data or from the
instruction_paramsaccount.The validation requires each optional argument group to be consistently present or absent, and requires that presence to be opposite the
instruction_paramsaccount presence. This prevents callers from supplying conflicting values through both input paths.This fix has been reviewed by auditors and has already been deployed onchain.