diff --git a/pkgs/atlas-search/crates/atlas-search/src/service.rs b/pkgs/atlas-search/crates/atlas-search/src/service.rs index cd71aab5cc..dd5e55ff36 100644 --- a/pkgs/atlas-search/crates/atlas-search/src/service.rs +++ b/pkgs/atlas-search/crates/atlas-search/src/service.rs @@ -191,6 +191,7 @@ fn search_with_token(base: &str, request: &SearchRequest, token: &str) -> Result let mut envelope: Option = None; let mut results: Vec = Vec::new(); let mut cursor: Option = None; + let mut seen_cursors: std::collections::HashSet = std::collections::HashSet::new(); loop { if Instant::now() >= deadline { return Err("service job results did not finish paging within 330 seconds".to_string()); @@ -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")?; @@ -283,6 +293,7 @@ mod tests { #[derive(Clone, Copy, PartialEq)] enum StubMode { + CyclingCursor, RepeatingCursor, EmptyPage, HappyPath, @@ -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 => { @@ -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); diff --git a/pkgs/fleet/crates/fleet-search/src/client.rs b/pkgs/fleet/crates/fleet-search/src/client.rs index 1c0eb7dd15..8a6e21696f 100644 --- a/pkgs/fleet/crates/fleet-search/src/client.rs +++ b/pkgs/fleet/crates/fleet-search/src/client.rs @@ -296,6 +296,7 @@ fn search_with_token(base: &str, request: &SearchRequest, token: &str) -> Result let mut envelope: Option = None; let mut results: Vec = Vec::new(); let mut cursor: Option = None; + let mut seen_cursors: std::collections::HashSet = std::collections::HashSet::new(); loop { if Instant::now() >= deadline { return Err("service job results did not finish paging within 330 seconds".to_string()); @@ -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")?; @@ -380,6 +390,7 @@ mod tests { #[derive(Clone, Copy, PartialEq)] enum StubMode { + CyclingCursor, RepeatingCursor, EmptyPage, HappyPath, @@ -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 => { @@ -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);