From c33f98ccbcb4ae8be349733a248d4381f561a9e3 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Mon, 21 Sep 2026 12:55:02 +0200 Subject: [PATCH] fix(security): close the three list endpoints #297 missed (#64) A second pass over every `limit`/`page`/`$regex` in the handlers found three the first commit did not cover: - graph symbol search sent `q` to Mongo as a raw regex and took `limit` as an unclamped `usize` from the query string; - the SBOM list used its own `SbomFilter` paging fields without the deserialize-time clamp, so `?limit=10000000` still worked there; - notifications capped `limit` at 200 but allowed it to be negative and left `page` unbounded, so `(page - 1) * limit as u64` could wrap into an enormous skip. All three now use the shared clamp and `literal_regex`, and every handler computes `skip` through one bounded helper. No raw `$regex` on caller input remains in the agent. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p --- compliance-agent/src/api/handlers/dto.rs | 21 +++++++++++++++++-- compliance-agent/src/api/handlers/graph.rs | 20 +++++++++--------- .../src/api/handlers/notifications.rs | 11 +++++++--- .../src/api/handlers/onboarding.rs | 2 +- compliance-agent/src/api/handlers/sbom.rs | 2 +- 5 files changed, 39 insertions(+), 17 deletions(-) diff --git a/compliance-agent/src/api/handlers/dto.rs b/compliance-agent/src/api/handlers/dto.rs index fc2937e..b6f0ace 100644 --- a/compliance-agent/src/api/handlers/dto.rs +++ b/compliance-agent/src/api/handlers/dto.rs @@ -211,12 +211,21 @@ pub struct SbomFilter { pub has_vulns: Option, #[serde(default)] pub license: Option, - #[serde(default = "default_page")] + #[serde(default = "default_page", deserialize_with = "de_page")] pub page: u64, - #[serde(default = "default_limit")] + #[serde(default = "default_limit", deserialize_with = "de_limit")] pub limit: i64, } +impl SbomFilter { + /// Documents to skip for this page. Bounded by construction. + pub fn skip(&self) -> u64 { + self.page + .saturating_sub(1) + .saturating_mul(self.limit as u64) + } +} + #[derive(Deserialize)] pub struct SbomExportParams { pub repo_id: String, @@ -375,6 +384,14 @@ mod tests { assert_eq!(literal_regex(&long).len(), MAX_SEARCH_LEN); } + #[test] + fn sbom_filter_clamps_the_same_way() { + let f: SbomFilter = serde_json::from_str(r#"{"limit":10000000,"page":0}"#).unwrap(); + assert_eq!(f.limit, MAX_PAGE_SIZE); + assert_eq!(f.page, 1); + assert_eq!(f.skip(), 0); + } + #[test] fn findings_filter_clamps_the_same_way() { let f: FindingsFilter = serde_json::from_str(r#"{"limit":5000,"page":0}"#).unwrap(); diff --git a/compliance-agent/src/api/handlers/graph.rs b/compliance-agent/src/api/handlers/graph.rs index d340dd0..dcf68f7 100644 --- a/compliance-agent/src/api/handlers/graph.rs +++ b/compliance-agent/src/api/handlers/graph.rs @@ -43,11 +43,15 @@ pub struct GraphData { #[derive(Deserialize)] pub struct SearchParams { pub q: String, - #[serde(default = "default_search_limit")] - pub limit: usize, + /// Clamped to `1..=MAX_PAGE_SIZE` while deserializing, like every list. + #[serde( + default = "default_search_limit", + deserialize_with = "super::dto::de_limit" + )] + pub limit: i64, } -fn default_search_limit() -> usize { +fn default_search_limit() -> i64 { 50 } @@ -290,15 +294,11 @@ pub async fn search_symbols( // Simple text search on qualified_name and name fields let filter = doc! { "repo_id": &repo_id, - "name": { "$regex": ¶ms.q, "$options": "i" }, + // Literal substring, not a caller-supplied regex (same fix as #64). + "name": { "$regex": super::dto::literal_regex(¶ms.q), "$options": "i" }, }; - let nodes: Vec = match db - .graph_nodes() - .find(filter) - .limit(params.limit as i64) - .await - { + let nodes: Vec = match db.graph_nodes().find(filter).limit(params.limit).await { Ok(cursor) => collect_cursor_async(cursor).await, Err(e) => { tracing::warn!("Failed to search graph nodes: {e}"); diff --git a/compliance-agent/src/api/handlers/notifications.rs b/compliance-agent/src/api/handlers/notifications.rs index 97d5f43..62011c5 100644 --- a/compliance-agent/src/api/handlers/notifications.rs +++ b/compliance-agent/src/api/handlers/notifications.rs @@ -40,9 +40,14 @@ pub async fn list_notifications( filter.insert("repo_id", repo_id.as_str()); } - let page = params.page.unwrap_or(1).max(1); - let limit = params.limit.unwrap_or(50).min(200); - let skip = (page - 1) * limit as u64; + // Same bounds as every other list: a negative limit or a huge page used to + // wrap `skip` into an enormous offset. + let page = params.page.unwrap_or(1).clamp(1, super::dto::MAX_PAGE); + let limit = params + .limit + .unwrap_or(50) + .clamp(1, super::dto::MAX_PAGE_SIZE); + let skip = (page - 1).saturating_mul(limit as u64); let total = db .cve_notifications() diff --git a/compliance-agent/src/api/handlers/onboarding.rs b/compliance-agent/src/api/handlers/onboarding.rs index 3b12c52..10a35b7 100644 --- a/compliance-agent/src/api/handlers/onboarding.rs +++ b/compliance-agent/src/api/handlers/onboarding.rs @@ -99,7 +99,7 @@ pub async fn list_targets( Query(params): Query, ) -> Result>>, StatusCode> { let db = tenant_db(&agent, &tenant).await?; - let skip = (params.page.saturating_sub(1)) * params.limit as u64; + let skip = params.skip(); let total = db .onboarded_targets() .count_documents(doc! {}) diff --git a/compliance-agent/src/api/handlers/sbom.rs b/compliance-agent/src/api/handlers/sbom.rs index bd90f21..77b09e7 100644 --- a/compliance-agent/src/api/handlers/sbom.rs +++ b/compliance-agent/src/api/handlers/sbom.rs @@ -104,7 +104,7 @@ pub async fn list_sbom( query.insert("license", license); } - let skip = (filter.page.saturating_sub(1)) * filter.limit as u64; + let skip = filter.skip(); let total = db .sbom_entries() .count_documents(query.clone())