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

Fail-fast validation #1

Open
jprochazk opened this issue Mar 26, 2023 · 0 comments
Open

Fail-fast validation #1

jprochazk opened this issue Mar 26, 2023 · 0 comments
Labels
enhancement New feature or request
Milestone

Comments

@jprochazk
Copy link
Owner

jprochazk commented Mar 26, 2023

To protect against primitive DoS attacks, it should be possible to halt further validation on the first error. Cheap checks (e.g. length, range) should be performed first, and expensive checks (pattern, custom) last. There should be a priority system so that custom checks can be put first if the user know that they are cheap.

priority wraps a a rule and changes its priority level:

#[derive(Validate)]
struct Test {
    #[garde(
        length(min=10, max=100),
        priority(
          custom(cheap_check),
          100
        ),
    )]
    value: String,
}

fn cheap_check(_: &str, value: &str, _: &()) -> Result<(), Error> {
  // ... something cheaper than the `length` check
  Ok(())
}

Rules should have a constant priority (e.g. range is cheap because it's O(1), contains is not because it is worst case O(N*M)). It should also be possible to designate some rules as having complexity that depends on their input, e.g. prefix priority will be based on the length of the value, and regex priority will be based on the complexity of the expression.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant