Skip to content

Commit e559023

Browse files
authored
fix(uri): enforce max length in PathAndQuery (#856)
1 parent 2178e17 commit e559023

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

src/uri/path.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{cmp, fmt, hash, str};
44

55
use bytes::Bytes;
66

7-
use super::{ErrorKind, InvalidUri};
7+
use super::{ErrorKind, InvalidUri, MAX_LEN};
88
use crate::byte_str::ByteStr;
99

1010
/// Represents the path component of a URI
@@ -475,6 +475,10 @@ const fn scan_path_and_query(bytes: &[u8]) -> Result<Scanned, ErrorKind> {
475475
return Err(ErrorKind::Empty);
476476
}
477477

478+
if bytes.len() > MAX_LEN {
479+
return Err(ErrorKind::TooLong);
480+
}
481+
478482
if bytes.len() == 1 && bytes[0] == b'*' {
479483
return Ok(Scanned {
480484
query,
@@ -676,6 +680,21 @@ mod tests {
676680
PathAndQuery::try_from(&[b'/', b'a', b'?', 0x7F][..]).expect_err("reject DEL");
677681
}
678682

683+
#[test]
684+
fn rejects_too_long_path_and_query() {
685+
let path = format!("/{}?query", "a".repeat(MAX_LEN));
686+
let err = PathAndQuery::try_from(path).expect_err("reject overly long path and query");
687+
assert_eq!(err.0, ErrorKind::TooLong);
688+
}
689+
690+
#[test]
691+
fn accepts_max_length_path_and_query() {
692+
let path = format!("/{}?", "a".repeat(MAX_LEN - 2));
693+
let path_and_query = PathAndQuery::try_from(path).expect("accept maximum length");
694+
assert_eq!(path_and_query.as_str().len(), MAX_LEN);
695+
assert_eq!(path_and_query.query(), Some(""));
696+
}
697+
679698
#[test]
680699
fn json_is_fine() {
681700
assert_eq!(

0 commit comments

Comments
 (0)