Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions pkgs/atlas-search/crates/atlas-search/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ fn search_with_token(base: &str, request: &SearchRequest, token: &str) -> Result
let mut envelope: Option<Json> = None;
let mut results: Vec<Json> = Vec::new();
let mut cursor: Option<String> = None;
let mut seen_cursors: std::collections::HashSet<String> = std::collections::HashSet::new();
loop {
if Instant::now() >= deadline {
return Err("service job results did not finish paging within 330 seconds".to_string());
Expand Down Expand Up @@ -242,6 +243,15 @@ fn search_with_token(base: &str, request: &SearchRequest, token: &str) -> Result
"service at {base} sent a malformed results page: next_cursor did not advance (repeats the cursor just used)"
));
}
// A cycle with period >= 2 (A -> B -> A) passes the immediate-repeat
// check above on every page: the ranking still never advances, so
// any cursor already followed in this session is equally malformed
// (#5654).
if !seen_cursors.insert(next_cursor.clone()) {
return Err(format!(
"service at {base} sent a malformed results page: next_cursor did not advance (repeats a cursor already seen in this paging session)"
));
}
cursor = Some(next_cursor);
}
let mut out = envelope.ok_or("service returned no results envelope")?;
Expand Down Expand Up @@ -283,6 +293,7 @@ mod tests {

#[derive(Clone, Copy, PartialEq)]
enum StubMode {
CyclingCursor,
RepeatingCursor,
EmptyPage,
HappyPath,
Expand Down Expand Up @@ -342,6 +353,13 @@ mod tests {
} else if first.contains("/results") {
let has_cursor = first.contains("cursor=");
let (results, next): (&str, &str) = match mode {
StubMode::CyclingCursor => {
if first.contains("cursor=cursor-B") {
(r#"[{"id":1}]"#, r#""cursor-A""#)
} else {
(r#"[{"id":1}]"#, r#""cursor-B""#)
}
}
StubMode::RepeatingCursor => (r#"[{"id":1}]"#, r#""same-cursor""#),
StubMode::EmptyPage => (r#"[]"#, r#""some-cursor""#),
StubMode::HappyPath => {
Expand Down Expand Up @@ -392,6 +410,20 @@ mod tests {
);
}

#[test]
fn search_cycling_cursor_returns_err_promptly() {
// #5654: a period-2 cycle (A -> B -> A) passes the immediate-repeat
// check on every page; the seen-cursor set must reject it promptly.
let base = start_stub(StubMode::CyclingCursor);
let started = Instant::now();
let err = search_with_token(&base, &request_k(100_000), "test-token").unwrap_err();
assert!(err.contains("already seen"), "unexpected error: {err}");
assert!(
started.elapsed() < Duration::from_secs(30),
"cycling cursor was not rejected promptly"
);
}

#[test]
fn search_empty_page_with_cursor_returns_err() {
let base = start_stub(StubMode::EmptyPage);
Expand Down
32 changes: 32 additions & 0 deletions pkgs/fleet/crates/fleet-search/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ fn search_with_token(base: &str, request: &SearchRequest, token: &str) -> Result
let mut envelope: Option<Json> = None;
let mut results: Vec<Json> = Vec::new();
let mut cursor: Option<String> = None;
let mut seen_cursors: std::collections::HashSet<String> = std::collections::HashSet::new();
loop {
if Instant::now() >= deadline {
return Err("service job results did not finish paging within 330 seconds".to_string());
Expand Down Expand Up @@ -347,6 +348,15 @@ fn search_with_token(base: &str, request: &SearchRequest, token: &str) -> Result
"service at {base} sent a malformed results page: next_cursor did not advance (repeats the cursor just used)"
));
}
// A cycle with period >= 2 (A -> B -> A) passes the immediate-repeat
// check above on every page: the ranking still never advances, so
// any cursor already followed in this session is equally malformed
// (#5654).
if !seen_cursors.insert(next_cursor.clone()) {
return Err(format!(
"service at {base} sent a malformed results page: next_cursor did not advance (repeats a cursor already seen in this paging session)"
));
}
cursor = Some(next_cursor);
}
let mut out = envelope.ok_or("service returned no results envelope")?;
Expand Down Expand Up @@ -380,6 +390,7 @@ mod tests {

#[derive(Clone, Copy, PartialEq)]
enum StubMode {
CyclingCursor,
RepeatingCursor,
EmptyPage,
HappyPath,
Expand Down Expand Up @@ -441,6 +452,13 @@ mod tests {
} else if first.contains("/results") {
let has_cursor = first.contains("cursor=");
let (results, next): (&str, &str) = match mode {
StubMode::CyclingCursor => {
if first.contains("cursor=cursor-B") {
(r#"[{"id":1}]"#, r#""cursor-A""#)
} else {
(r#"[{"id":1}]"#, r#""cursor-B""#)
}
}
StubMode::RepeatingCursor => (r#"[{"id":1}]"#, r#""same-cursor""#),
StubMode::EmptyPage => (r#"[]"#, r#""some-cursor""#),
StubMode::HappyPath => {
Expand Down Expand Up @@ -491,6 +509,20 @@ mod tests {
);
}

#[test]
fn search_cycling_cursor_returns_err_promptly() {
// #5654: a period-2 cycle (A -> B -> A) passes the immediate-repeat
// check on every page; the seen-cursor set must reject it promptly.
let base = start_stub(StubMode::CyclingCursor);
let started = Instant::now();
let err = search_with_token(&base, &request_k(100_000), "test-token").unwrap_err();
assert!(err.contains("already seen"), "unexpected error: {err}");
assert!(
started.elapsed() < Duration::from_secs(30),
"cycling cursor was not rejected promptly"
);
}

#[test]
fn search_empty_page_with_cursor_returns_err() {
let base = start_stub(StubMode::EmptyPage);
Expand Down