Skip to content

Commit

Permalink
Hardened Condition.to_string()
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsnaps committed Sep 1, 2022
1 parent 7b8291b commit caf423f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
11 changes: 3 additions & 8 deletions limitador-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ fn create_config() -> (Configuration, String) {
Arg::with_name("validate")
.long("validate")
.display_order(7)
.help("Validates the LIMITS_FILE and exits")
.help("Validates the LIMITS_FILE and exits"),
)
.subcommand(
SubCommand::with_name("memory")
Expand Down Expand Up @@ -581,23 +581,18 @@ fn create_config() -> (Configuration, String) {
}
process::exit(0);
}
Err(e) => LimitadorServerError::ConfigFile(format!(
"Couldn't parse: {}",
e
)),
Err(e) => LimitadorServerError::ConfigFile(format!("Couldn't parse: {}", e)),
}
}
Err(e) => LimitadorServerError::ConfigFile(format!(
"Couldn't read file '{}': {}",
limits_file,
e
limits_file, e
)),
};
eprintln!("{}", error);
process::exit(1);
}


let storage = match matches.subcommand() {
Some(("redis", sub)) => StorageConfiguration::Redis(RedisStorageConfiguration {
url: sub.value_of("URL").unwrap().to_owned(),
Expand Down
11 changes: 8 additions & 3 deletions limitador/src/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,14 @@ impl From<Condition> for String {
fn from(condition: Condition) -> Self {
let p = &condition.predicate;
let predicate: String = p.clone().into();
let quotes = if condition.operand.contains('"') {
'\''
} else {
'"'
};
format!(
"{} {} '{}'",
condition.var_name, predicate, condition.operand
"{} {} {}{}{}",
condition.var_name, predicate, quotes, condition.operand, quotes
)
}
}
Expand Down Expand Up @@ -967,6 +972,6 @@ mod tests {
operand: "ok".to_string(),
};
let result = serde_json::to_string(&condition).expect("Should serialize");
assert_eq!(result, r#""foobar == 'ok'""#.to_string());
assert_eq!(result, r#""foobar == \"ok\"""#.to_string());
}
}
2 changes: 1 addition & 1 deletion limitador/src/storage/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ mod tests {
vec!["app_id"],
);
assert_eq!(
"namespace:{example.com},counters_of_limit:{\"namespace\":\"example.com\",\"seconds\":60,\"conditions\":[\"req.method == 'GET'\"],\"variables\":[\"app_id\"]}",
"namespace:{example.com},counters_of_limit:{\"namespace\":\"example.com\",\"seconds\":60,\"conditions\":[\"req.method == \\\"GET\\\"\"],\"variables\":[\"app_id\"]}",
key_for_counters_of_limit(&limit))
}
}

0 comments on commit caf423f

Please sign in to comment.