Skip to content

Commit

Permalink
Added validation for Limit.max_value to be > 0
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsnaps committed Sep 6, 2022
1 parent 73e2bd4 commit 3167bfc
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions limitador-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,22 @@ impl Limiter {
Ok(f) => {
let parsed_limits: Result<Vec<Limit>, _> = serde_yaml::from_reader(f);
match parsed_limits {
Ok(limits) => {
match &self {
Self::Blocking(limiter) => limiter.configure_with(limits)?,
Self::Async(limiter) => limiter.configure_with(limits).await?,
}
if limitador::limit::check_deprecated_syntax_usages_and_reset() {
error!("You are using deprecated syntax for your conditions! See the migration guide https://kudrant.io/migration/limitador/constraints")
Ok(limits) => match first_zero_or_negative(&limits) {
None => {
match &self {
Self::Blocking(limiter) => limiter.configure_with(limits)?,
Self::Async(limiter) => limiter.configure_with(limits).await?,
}
if limitador::limit::check_deprecated_syntax_usages_and_reset() {
error!("You are using deprecated syntax for your conditions! See the migration guide https://kudrant.io/migration/limitador/constraints")
}
Ok(())
}
Ok(())
}
Some(index) => Err(LimitadorServerError::ConfigFile(format!(
".[{}]: invalid value for `max_value`: positive integer expected",
index
))),
},
Err(e) => Err(LimitadorServerError::ConfigFile(format!(
"Couldn't parse: {}",
e
Expand All @@ -230,6 +236,15 @@ impl Limiter {
}
}

fn first_zero_or_negative(limits: &[Limit]) -> Option<usize> {
for (index, limit) in limits.iter().enumerate() {
if limit.max_value() < 1 {
return Some(index);
}
}
None
}

#[actix_rt::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = {
Expand Down

0 comments on commit 3167bfc

Please sign in to comment.