fix(client): reject runtime-invalid negotiated dscp
This commit is contained in:
3 files changed
+29
-2
No files matched your search
@@ -52,3 +52,19 @@ fn strict_negotiation_rejects_negative_returned_length() {
|
||||
Err(ClientError::NegotiationRejected { reason }) if reason == "length must be non-negative"
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn loose_negotiation_rejects_runtime_invalid_returned_dscp() {
|
||||
let config = ClientConfig::default();
|
||||
let requested = params_from_config(&config).unwrap();
|
||||
|
||||
for dscp in [-1, 64] {
|
||||
let mut returned = requested.clone();
|
||||
returned.dscp = dscp;
|
||||
assert!(matches!(
|
||||
validate_negotiated_params(&requested, &returned, NegotiationPolicy::Loose),
|
||||
Err(ClientError::NegotiationRejected { reason })
|
||||
if reason == "dscp must be in range 0..=63"
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ use std::time::Instant;
|
||||
use irtt_proto::{Params, PROTOCOL_VERSION};
|
||||
|
||||
use crate::{
|
||||
config::NegotiationPolicy,
|
||||
config::{NegotiationPolicy, MAX_DSCP_CODEPOINT},
|
||||
error::ClientError,
|
||||
probe::{CompletedSet, PendingMap, TimedOutMap},
|
||||
};
|
||||
@@ -63,6 +63,7 @@ pub(crate) fn validate_negotiated_params(
|
||||
reason: "interval must be positive".to_owned(),
|
||||
});
|
||||
}
|
||||
validate_dscp_restriction(returned.dscp)?;
|
||||
|
||||
if policy == NegotiationPolicy::Strict && returned != requested {
|
||||
return Err(ClientError::NegotiationRejected {
|
||||
@@ -94,6 +95,16 @@ fn validate_duration_restriction(requested: i64, returned: i64) -> Result<(), Cl
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_dscp_restriction(returned: i64) -> Result<(), ClientError> {
|
||||
if !(0..=i64::from(MAX_DSCP_CODEPOINT)).contains(&returned) {
|
||||
return Err(ClientError::NegotiationRejected {
|
||||
reason: format!("dscp must be in range 0..={MAX_DSCP_CODEPOINT}"),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -272,7 +272,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn params_round_trip_dscp_codepoints_without_shifting() {
|
||||
for dscp in [0, 46, 63] {
|
||||
for dscp in [0, 46, 63, 64, 184, -1] {
|
||||
let params = Params {
|
||||
protocol_version: 1,
|
||||
dscp,
|
||||
|
||||
Reference in new issue
Block a user