Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(web): ignore an unparseable query string on page URLs

Peterson Salmed5e4a99df018
diff --git a/crates/ai-memory-web/src/routes/page.rs b/crates/ai-memory-web/src/routes/page.rs
index db790ddc..f26281d9 100644
--- a/crates/ai-memory-web/src/routes/page.rs
+++ b/crates/ai-memory-web/src/routes/page.rs
@@ -5,6 +5,7 @@ use std::sync::Arc;
use ai_memory_core::PagePath;
use ai_memory_store::PageSummary;
use askama::Template;
+use axum::extract::rejection::QueryRejection;
use axum::extract::{Path, Query, State};
use axum::http::StatusCode;
use axum::response::{Html, IntoResponse, Response};
@@ -20,7 +21,7 @@ const NAMESPACE_PAGE_SIZE: usize = 25;
/// Namespace listing options. Strings, so a bad value falls back to a
/// default instead of becoming axum's 400.
-#[derive(serde::Deserialize)]
+#[derive(Default, serde::Deserialize)]
pub(crate) struct NamespaceQuery {
sort: Option<String>,
page: Option<String>,
@@ -31,8 +32,11 @@ pub(crate) async fn handler(
State(state): State<Arc<WebState>>,
viewer: Option<axum::Extension<ai_memory_core::AuthorizedViewer>>,
Path((workspace, project, path)): Path<(String, String, String)>,
- Query(query): Query<NamespaceQuery>,
+ query: Result<Query<NamespaceQuery>, QueryRejection>,
) -> Response {
+ // A query string that does not parse at all (a repeated key) is ignored
+ // too: every page URL takes this extractor, not only namespaces.
+ let query = query.map(|Query(q)| q).unwrap_or_default();
if let Err(refusal) = super::authorize_read(&state, viewer, &workspace, &project).await {
return refusal_response(&refusal);
}
diff --git a/crates/ai-memory-web/tests/suite/routes.rs b/crates/ai-memory-web/tests/suite/routes.rs
index 7f409fe8..a63206a3 100644
--- a/crates/ai-memory-web/tests/suite/routes.rs
+++ b/crates/ai-memory-web/tests/suite/routes.rs
@@ -4841,6 +4841,11 @@ async fn namespace_pages_split_at_25_and_clamp_bad_page_values() {
assert_eq!(status, StatusCode::OK, "page={junk}");
assert!(body.contains("Showing 1&ndash;25"), "page={junk}");
}
+ // A repeated key does not parse into the options at all; it falls back
+ // too instead of becoming axum's 400.
+ let (status, body) = get_body(&app, &format!("{base}?sort=a&sort=b&page=2")).await;
+ assert_eq!(status, StatusCode::OK, "{body}");
+ assert!(body.contains("Showing 1&ndash;25"), "{body}");
// Exactly one page of results: no pager, no range.
let (_tmp, app) = seed_notes(25).await;
@@ -4849,6 +4854,30 @@ async fn namespace_pages_split_at_25_and_clamp_bad_page_values() {
assert!(!exact.contains("Showing") && !exact.contains("aria-label=\"Pagination\""));
}
+/// Every page URL takes the namespace options, so an unparseable query
+/// string must not turn an ordinary page into a 400.
+#[tokio::test]
+async fn page_url_ignores_an_unparseable_query_string() {
+ let (_tmp, store, wiki) = setup().await;
+ let ws = store
+ .writer
+ .get_or_create_workspace("default")
+ .await
+ .unwrap();
+ let proj = store
+ .writer
+ .get_or_create_project(ws, "scratch", None)
+ .await
+ .unwrap();
+ wiki.write_page(wiki_req(ws, proj, "notes/bar.md", "# Bar\n\nbody"))
+ .await
+ .unwrap();
+ let app = router(store.reader.clone(), wiki.clone());
+ let (status, body) = get_body(&app, "/w/default/scratch/p/notes/bar.md?sort=a&sort=b").await;
+ assert_eq!(status, StatusCode::OK, "{body}");
+ assert!(body.contains("Bar"), "{body}");
+}
+
#[tokio::test]
async fn namespace_title_sort_breaks_ties_by_recency() {
let (_tmp, store, wiki) = setup().await;