Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for validation and size limiting #14

Open
pzmarzly opened this issue Apr 6, 2019 · 0 comments
Open

Support for validation and size limiting #14

pzmarzly opened this issue Apr 6, 2019 · 0 comments

Comments

@pzmarzly
Copy link

pzmarzly commented Apr 6, 2019

Right now if the raw bytes parse correctly, the next step in most applications is to validate (sanitize) the data. This being an additional step is a risk (programmer may forget about validation) and waste of resources (protocol has to parse all fields before sanitation can run). It may also result in unnecessary allocations - let's say our protocol accepts Vec<_> and someone sends us 8GB of data. protocol will download, parse and allocate 8GB Vec, which will be rejected by the next instruction like if vec.len() > 64 { return; }.

I imagine code could look like this:

#[derive(Protocol)]
pub struct User {
    pub name: String,
    pub pub_key: [u8; 64],
}

impl User {
    fn validate_name(name: String) -> bool { /* some code */ }
    fn validate_pub_key(pub_key: [u8; 64]) -> bool { /* some code */ }
}

User::parse( // or `User::validate_from_raw_bytes`
    &data,
    &protocol::Settings::default()
).unwrap();

Alternatively, protocol could generate trait, so we could have different ways of validating the same struct, and stateful validators:

#[derive(Protocol)]
pub struct User {
    pub name: String,
    pub pub_key: [u8; 64],
}

// protocol generates:
//
// trait UserValidate {
//     fn validate_name(&mut self, name: String) -> bool;
//     fn validate_pub_key(&mut self, pub_key: [u8; 64]) -> bool;
// }

struct UserBasicValidator;
impl UserValidate for UserBasicValidator {
    fn validate_name(&mut self, name: String) -> bool { /* some code */ }
    fn validate_pub_key(&mut self, pub_key: [u8; 64]) -> bool { /* some code */ }
}

User::parse(
    &data,
    &protocol::Settings::default(),
    UserBasicValidator::new(),
).unwrap();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant