andromedaprotocol/andromeda-core

[Primitive]: Restriction on Data Size

Closed this issue · 2 comments

Description

Data provided to the primitive currently does not have a restriction for any type. We should add type based validation so that invalid data cannot be stored.

Implementation

The suggested way to implement this feature is to add a method to the Primitive type with a conditional handler for each type:

impl Primitive {
  pub fn validate(&self) -> ContractError<()> {
    match self {
    // ...
    }
  }
}

Checks should include things such as strings being non-empty.

Some types may not require validation.

Acceptance Criteria

  • Validation method for primitive type
  • Unit-testing for validation method

What should we add? Thinking of limiting the length of Binary and String, but unsure to what length.

pub fn validate(&self, api: &dyn Api) -> Result<(), ContractError> {
        match self {
            Primitive::Uint128(number) => {
                ensure!(
                    !number.to_string().is_empty(),
                    ContractError::EmptyString {}
                );
            }
            Primitive::Decimal(_) => {}
            Primitive::Coin(coin) => {
                ensure!(!coin.amount.is_zero(), ContractError::InvalidZeroAmount {});
                ensure!(!coin.denom.is_empty(), ContractError::InvalidDenom {});
            }
            Primitive::Addr(address) => {
                api.addr_validate(address.as_str())?;
            }
            Primitive::String(string) => {
                ensure!(!string.is_empty(), ContractError::EmptyString {});
            }
            Primitive::Bool(_) => {}
            Primitive::Binary(binary) => {
                ensure!(!binary.is_empty(), ContractError::EmptyString {});
            }
        }
        Ok(())
    }

I think we can allow zero amount coins but the rest looks solid.