Skip to content

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

Rust

fix: harden http and web security

AkitaOnRailsf3699161d764
diff --git a/Cargo.lock b/Cargo.lock
index 134dbc7b..bc3fc048 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -181,6 +181,7 @@ dependencies = [
"tempfile",
"thiserror 2.0.18",
"tokio",
+ "tokio-util",
"tower",
"tracing",
]
@@ -219,6 +220,7 @@ dependencies = [
"pulldown-cmark",
"serde",
"serde_json",
+ "sha2",
"syntect",
"tempfile",
"thiserror 2.0.18",
diff --git a/Cargo.toml b/Cargo.toml
index 6c89b708..ba866d43 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -52,7 +52,7 @@ serde_yaml = "0.9"
# Async runtime.
tokio = { version = "1", features = ["full"] }
-tokio-util = { version = "0.7", features = ["rt"] }
+tokio-util = { version = "0.7", features = ["rt", "io"] }
# Logging / tracing.
tracing = "0.1"
diff --git a/README.md b/README.md
index 77543359..2d289802 100644
--- a/README.md
+++ b/README.md
@@ -271,8 +271,9 @@ See [`docs/deploy.md`](docs/deploy.md) for the full homelab pattern
### DNS-rebinding guard (`AI_MEMORY_ALLOWED_HOSTS`)
Any non-loopback bind also needs `AI_MEMORY_ALLOWED_HOSTS` set to the
-host/IP the clients will use, otherwise the rmcp server rejects
-external `Host` headers with 403:
+host/IP the clients will use, otherwise the HTTP server rejects
+external `Host` headers with 403 before they reach `/mcp`, `/hook`,
+`/handoff`, `/admin/*`, or `/web/*`:
```bash
-e AI_MEMORY_ALLOWED_HOSTS="<server-ip>,localhost,127.0.0.1"
diff --git a/crates/ai-memory-cli/src/cli.rs b/crates/ai-memory-cli/src/cli.rs
index ee7bd1be..e8963226 100644
--- a/crates/ai-memory-cli/src/cli.rs
+++ b/crates/ai-memory-cli/src/cli.rs
@@ -284,6 +284,12 @@ pub struct StatusArgs {
pub struct SearchArgs {
/// FTS5 query string (e.g. `"karpathy wiki"` or `quick OR slow`).
pub query: String,
+ /// Workspace name. Defaults to `default`.
+ #[arg(long, default_value = "default")]
+ pub workspace: String,
+ /// Project name. When omitted, auto-derived from the current project.
+ #[arg(long)]
+ pub project: Option<String>,
/// Maximum number of hits to return.
#[arg(short = 'n', long, default_value_t = 10)]
pub limit: usize,
diff --git a/crates/ai-memory-cli/src/commands/search.rs b/crates/ai-memory-cli/src/commands/search.rs
index 2e54bd4d..dded818e 100644
--- a/crates/ai-memory-cli/src/commands/search.rs
+++ b/crates/ai-memory-cli/src/commands/search.rs
@@ -25,11 +25,17 @@ struct Hit {
/// Returns an error if the server is unreachable or returns non-2xx.
pub async fn run(_config: &Config, args: SearchArgs) -> Result<()> {
let ep = ServerEndpoint::from_env();
+ let project = super::resolve_project_name(args.project.as_deref())?;
let limit_str = args.limit.to_string();
let hits: Vec<Hit> = get_json(
&ep,
"/admin/search",
- &[("q", args.query.as_str()), ("limit", limit_str.as_str())],
+ &[
+ ("q", args.query.as_str()),
+ ("workspace", args.workspace.as_str()),
+ ("project", project.as_str()),
+ ("limit", limit_str.as_str()),
+ ],
)
.await?;
diff --git a/crates/ai-memory-cli/src/commands/serve.rs b/crates/ai-memory-cli/src/commands/serve.rs
index b7aef6e9..7d318565 100644
--- a/crates/ai-memory-cli/src/commands/serve.rs
+++ b/crates/ai-memory-cli/src/commands/serve.rs
@@ -11,7 +11,11 @@ use ai_memory_store::Store;
use ai_memory_web;
use ai_memory_wiki::{WatcherHandle, Wiki};
use anyhow::{Context, Result};
-use axum::extract::DefaultBodyLimit;
+use axum::body::Body;
+use axum::extract::{DefaultBodyLimit, State};
+use axum::http::{Request, StatusCode, header};
+use axum::middleware::Next;
+use axum::response::{IntoResponse, Response};
use rmcp::ServiceExt;
use rmcp::transport::stdio;
use rmcp::transport::streamable_http_server::{
@@ -232,6 +236,10 @@ pub async fn run(config: &Config, args: ServeArgs) -> Result<()> {
auth_state,
require_bearer,
))
+ .layer(axum::middleware::from_fn_with_state(
+ Arc::new(config.allowed_hosts.clone()),
+ require_allowed_host,
+ ))
.layer(DefaultBodyLimit::max(MAX_BODY_BYTES));
let listener = tokio::net::TcpListener::bind(&bind)
.await
@@ -265,3 +273,65 @@ pub async fn run(config: &Config, args: ServeArgs) -> Result<()> {
}
Ok(())
}
+
+async fn require_allowed_host(
+ State(allowed_hosts): State<Arc<Vec<String>>>,
+ req: Request<Body>,
+ next: Next,
+) -> Response {
+ let Some(host) = req
+ .headers()
+ .get(header::HOST)
+ .and_then(|h| h.to_str().ok())
+ else {
+ return (StatusCode::BAD_REQUEST, "missing Host header\n").into_response();
+ };
+ if host_allowed(host, &allowed_hosts) {
+ return next.run(req).await;
+ }
+ tracing::warn!(host, allowed = ?allowed_hosts, "rejected request with disallowed Host header");
+ (StatusCode::FORBIDDEN, "forbidden host\n").into_response()
+}
+
+fn host_allowed(host: &str, allowed_hosts: &[String]) -> bool {
+ allowed_hosts.iter().any(|allowed| {
+ host.eq_ignore_ascii_case(allowed) || host_without_port(host).eq_ignore_ascii_case(allowed)
+ })
+}
+
+fn host_without_port(host: &str) -> &str {
+ if let Some(rest) = host.strip_prefix('[')
+ && let Some((inside, _)) = rest.split_once(']')
+ {
+ return inside;
+ }
+ match host.rsplit_once(':') {
+ Some((name, port)) if !name.contains(':') && port.chars().all(|c| c.is_ascii_digit()) => {
+ name
+ }
+ _ => host,
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn host_allowed_accepts_host_with_port() {
+ let allowed = vec!["127.0.0.1".to_string(), "localhost".to_string()];
+ assert!(host_allowed("127.0.0.1:49374", &allowed));
+ assert!(host_allowed("localhost", &allowed));
+ }
+
+ #[test]
+ fn host_allowed_rejects_unknown_host() {
+ let allowed = vec!["127.0.0.1".to_string()];
+ assert!(!host_allowed("evil.example:49374", &allowed));
+ }
+
+ #[test]
+ fn host_without_port_handles_ipv6_loopback() {
+ assert_eq!(host_without_port("[::1]:49374"), "::1");
+ }
+}
diff --git a/crates/ai-memory-cli/templates/config.default.toml b/crates/ai-memory-cli/templates/config.default.toml
index 852e8056..b53f7cfa 100644
--- a/crates/ai-memory-cli/templates/config.default.toml
+++ b/crates/ai-memory-cli/templates/config.default.toml
@@ -9,6 +9,12 @@
# HTTP bind address used by `ai-memory serve`.
bind = "127.0.0.1:49374"
+# Host-header allowlist for the HTTP server (DNS-rebinding defence).
+# Keep loopback defaults for local-only use. When binding to a LAN
+# address, include the host/IP clients will use, e.g.
+# `AI_MEMORY_ALLOWED_HOSTS=homelab,192.168.0.90,localhost,127.0.0.1`.
+allowed_hosts = ["localhost", "127.0.0.1", "::1"]
+
# Default tracing filter (overridable by RUST_LOG).
log_level = "info"
diff --git a/crates/ai-memory-core/src/ids.rs b/crates/ai-memory-core/src/ids.rs
index eddaba1c..350970e7 100644
--- a/crates/ai-memory-core/src/ids.rs
+++ b/crates/ai-memory-core/src/ids.rs
@@ -7,6 +7,7 @@
//! `docs/issues-basic-memory.md`).
use std::fmt;
+use std::path::Component;
use std::str::FromStr;
use serde::{Deserialize, Serialize};
@@ -93,8 +94,8 @@ id_newtype!(pub HandoffId, "Identifier for a cross-agent handoff record.");
pub struct PagePath(String);
impl PagePath {
- /// Construct from a raw string. Rejects empty, leading-slash, and
- /// dot-segment paths.
+ /// Construct from a raw string. Rejects empty, absolute, Windows-prefix,
+ /// backslash-separated, and dot-segment paths.
///
/// # Errors
/// Returns [`MemoryError::InvalidPagePath`] when the input is empty or
@@ -109,6 +110,26 @@ impl PagePath {
"leading slash: {raw}"
)));
}
+ if raw.contains('\\') {
+ return Err(MemoryError::InvalidPagePath(format!(
+ "backslash separator in {raw}"
+ )));
+ }
+ for component in std::path::Path::new(&raw).components() {
+ match component {
+ Component::Normal(_) => {}
+ Component::CurDir | Component::ParentDir => {
+ return Err(MemoryError::InvalidPagePath(format!(
+ "dot segment in {raw}"
+ )));
+ }
+ Component::Prefix(_) | Component::RootDir => {
+ return Err(MemoryError::InvalidPagePath(format!(
+ "absolute path in {raw}"
+ )));
+ }
+ }
+ }
for segment in raw.split('/') {
if segment.is_empty() || segment == "." || segment == ".." {
return Err(MemoryError::InvalidPagePath(format!(
@@ -190,6 +211,12 @@ mod tests {
assert!(PagePath::new("a/../b").is_err());
}
+ #[test]
+ fn page_path_rejects_backslashes_and_windows_prefixes() {
+ assert!(PagePath::new(r"notes\secret.md").is_err());
+ assert!(PagePath::new(r"C:\Users\me\secret.md").is_err());
+ }
+
#[test]
fn ids_are_unique() {
let a = WorkspaceId::new();
diff --git a/crates/ai-memory-mcp/Cargo.toml b/crates/ai-memory-mcp/Cargo.toml
index 38d6a38a..933a5e88 100644
--- a/crates/ai-memory-mcp/Cargo.toml
+++ b/crates/ai-memory-mcp/Cargo.toml
@@ -22,6 +22,7 @@ tempfile.workspace = true
serde.workspace = true
serde_json.workspace = true
tokio.workspace = true
+tokio-util.workspace = true
tracing.workspace = true
rmcp.workspace = true
schemars.workspace = true
diff --git a/crates/ai-memory-mcp/src/admin.rs b/crates/ai-memory-mcp/src/admin.rs
index 19f00a9a..eb397695 100644
--- a/crates/ai-memory-mcp/src/admin.rs
+++ b/crates/ai-memory-mcp/src/admin.rs
@@ -41,6 +41,7 @@ use axum::routing::{get, post};
use flate2::Compression;
use flate2::write::GzEncoder;
use serde::{Deserialize, Serialize};
+use tokio_util::io::ReaderStream;
use tracing::{info, warn};
/// Shared state for the admin router.
@@ -137,18 +138,18 @@ pub fn admin_router(state: AdminState) -> Router {
///
/// Snapshots the live SQLite DB via the online backup API, then
/// tar-gzips `wiki/`, the snapshot, and `config.toml` (if present)
-/// into an in-memory buffer. The response body IS the tarball bytes —
-/// `Content-Type: application/gzip`, no JSON wrapper.
+/// into a tempfile. The response streams the file instead of buffering
+/// the full archive in memory.
async fn handle_backup(State(state): State<Arc<AdminState>>) -> Response {
- match build_backup_tarball(&state).await {
- Ok(bytes) => Response::builder()
+ match build_backup_tarball_file(&state).await {
+ Ok(file) => Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "application/gzip")
.header(
header::CONTENT_DISPOSITION,
"attachment; filename=\"backup.tar.gz\"",
)
- .body(Body::from(bytes))
+ .body(Body::from_stream(ReaderStream::new(file)))
.unwrap_or_else(|_| {
Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
@@ -173,7 +174,7 @@ async fn handle_backup(State(state): State<Arc<AdminState>>) -> Response {
}
}
-async fn build_backup_tarball(state: &AdminState) -> anyhow::Result<Vec<u8>> {
+async fn build_backup_tarball_file(state: &AdminState) -> anyhow::Result<tokio::fs::File> {
let staging = tempfile::tempdir()?;
let snapshot_path = staging.path().join("memory.sqlite");
info!(snapshot = %snapshot_path.display(), "snapshotting SQLite for backup");
@@ -183,9 +184,10 @@ async fn build_backup_tarball(state: &AdminState) -> anyhow::Result<Vec<u8>> {
.await
.map_err(|e| anyhow::anyhow!("sqlite snapshot: {e}"))?;
- let mut buf = Vec::new();
+ let mut tar_file = tempfile::NamedTempFile::new()?;
+ let tar_path = tar_file.path().to_path_buf();
{
- let encoder = GzEncoder::new(&mut buf, Compression::default());
+ let encoder = GzEncoder::new(tar_file.as_file_mut(), Compression::default());
let mut tar = tar::Builder::new(encoder);
tar.mode(tar::HeaderMode::Deterministic);
@@ -207,7 +209,11 @@ async fn build_backup_tarball(state: &AdminState) -> anyhow::Result<Vec<u8>> {
let encoder = tar.into_inner()?;
encoder.finish()?;
}
- Ok(buf)
+ tar_file.as_file_mut().sync_data()?;
+ let file = tokio::fs::File::open(&tar_path).await?;
+ drop(tar_file);
+ let _ = std::fs::remove_file(&tar_path);
+ Ok(file)
}
// ---------------------------------------------------------------------
@@ -262,6 +268,12 @@ async fn handle_status(State(state): State<Arc<AdminState>>) -> impl IntoRespons
struct SearchQuery {
/// FTS5 query expression.
q: String,
+ /// Workspace name to search within. When omitted with `project`, admin search is global.
+ #[serde(default)]
+ workspace: Option<String>,
+ /// Project name to search within. When omitted with `workspace`, admin search is global.
+ #[serde(default)]
+ project: Option<String>,
/// Max number of hits to return. Capped at 100 server-side.
#[serde(default = "default_search_limit")]
limit: usize,
@@ -276,7 +288,20 @@ async fn handle_search(
Query(query): Query<SearchQuery>,
) -> impl IntoResponse {
let limit = query.limit.clamp(1, 100);
- match state.reader.search_pages(query.q, limit).await {
+ let search_result = match (query.workspace.as_deref(), query.project.as_deref()) {
+ (Some(workspace), Some(project)) => match resolve_ws_proj(&state, workspace, project).await
+ {
+ Ok((ws, proj)) => {
+ state
+ .reader
+ .search_pages_for_project(ws, proj, query.q, limit)
+ .await
+ }
+ Err(e) => return e,
+ },
+ _ => state.reader.search_pages(query.q, limit).await,
+ };
+ match search_result {
Ok(hits) => (
StatusCode::OK,
Json(serde_json::to_value(&hits).unwrap_or_else(|_| serde_json::json!([]))),
diff --git a/crates/ai-memory-mcp/src/server.rs b/crates/ai-memory-mcp/src/server.rs
index 79175704..e8b702ec 100644
--- a/crates/ai-memory-mcp/src/server.rs
+++ b/crates/ai-memory-mcp/src/server.rs
@@ -335,11 +335,20 @@ impl AiMemoryServer {
error = %e,
"embedder failed; degrading memory_query to BM25-only"
);
- self.reader.search_pages(args.query, limit).await
+ self.reader
+ .search_pages_for_project(
+ self.workspace_id,
+ self.project_id,
+ args.query,
+ limit,
+ )
+ .await
}
}
} else {
- self.reader.search_pages(args.query, limit).await
+ self.reader
+ .search_pages_for_project(self.workspace_id, self.project_id, args.query, limit)
+ .await
};
let hits = hits.map_err(|e| McpError::internal_error(e.to_string(), None))?;
self.spawn_access_bump(hits.iter().map(|h| h.id).collect());
@@ -361,7 +370,7 @@ impl AiMemoryServer {
let limit = args.limit.unwrap_or(self.default_limit).clamp(1, 100);
let hits = self
.reader
- .recent_pages(limit)
+ .recent_pages_for_project(self.workspace_id, self.project_id, limit)
.await
.map_err(|e| McpError::internal_error(e.to_string(), None))?;
self.spawn_access_bump(hits.iter().map(|h| h.id).collect());
@@ -549,7 +558,7 @@ impl AiMemoryServer {
async fn memory_status(&self) -> Result<CallToolResult, McpError> {
let counts = self
.reader
- .status_counts()
+ .status_counts_for_project(self.workspace_id, self.project_id)
.await
.map_err(|e| McpError::internal_error(e.to_string(), None))?;
let response = StatusResponse { counts };
@@ -572,7 +581,7 @@ impl AiMemoryServer {
let limit = args.recent_pages_limit.unwrap_or(10);
let snapshot = self
.reader
- .briefing(limit)
+ .briefing_for_project(self.workspace_id, self.project_id, limit)
.await
.map_err(|e| McpError::internal_error(e.to_string(), None))?;
ok_json(&snapshot)
@@ -599,7 +608,7 @@ impl AiMemoryServer {
let limit = args.recent_pages_limit.unwrap_or(10);
let snapshot = self
.reader
- .briefing(limit)
+ .briefing_for_project(self.workspace_id, self.project_id, limit)
.await
.map_err(|e| McpError::internal_error(e.to_string(), None))?;
diff --git a/crates/ai-memory-store/src/reader.rs b/crates/ai-memory-store/src/reader.rs
index bf77060b..1d3b7ab1 100644
--- a/crates/ai-memory-store/src/reader.rs
+++ b/crates/ai-memory-store/src/reader.rs
@@ -265,6 +265,65 @@ impl ReaderPool {
.await
}
+ /// Run a full-text search scoped to one project.
+ ///
+ /// # Errors
+ /// Propagates any SQL or pool error.
+ pub async fn search_pages_for_project(
+ &self,
+ workspace_id: WorkspaceId,
+ project_id: ProjectId,
+ query: String,
+ limit: usize,
+ ) -> StoreResult<Vec<PageHit>> {
+ self.with_conn(move |conn| {
+ let mut stmt = conn.prepare(
+ "SELECT pages.id, pages.path, pages.title, \
+ snippet(pages_fts, 1, '<mark>', '</mark>', '…', 24) AS snip, \
+ pages_fts.rank \
+ FROM pages_fts \
+ JOIN pages ON pages.rowid = pages_fts.rowid \
+ WHERE pages_fts MATCH ?1 \
+ AND pages.workspace_id = ?2 \
+ AND pages.project_id = ?3 \
+ AND pages.is_latest = 1 \
+ ORDER BY pages_fts.rank \
+ LIMIT ?4",
+ )?;
+ #[allow(clippy::cast_possible_wrap)]
+ let rows = stmt.query_map(
+ params![
+ query,
+ workspace_id.as_bytes(),
+ project_id.as_bytes(),
+ limit as i64
+ ],
+ |row| {
+ let id_bytes: Vec<u8> = row.get(0)?;
+ let path: String = row.get(1)?;
+ let title: String = row.get(2)?;
+ let snippet: String = row.get(3)?;
+ let rank: f64 = row.get(4)?;
+ Ok((id_bytes, path, title, snippet, rank))
+ },
+ )?;
+
+ let mut hits = Vec::new();
+ for row in rows {
+ let (id_bytes, path, title, snippet, rank) = row?;
+ hits.push(PageHit {
+ id: PageId::from_slice(&id_bytes)?,
+ path: PagePath::new(path)?,
+ title,
+ snippet,
+ rank,
+ });
+ }
+ Ok(hits)
+ })
+ .await
+ }
+
/// Return the N most-recently-updated `is_latest = 1` pages.
///
/// # Errors
@@ -305,6 +364,54 @@ impl ReaderPool {
.await
}
+ /// Return the N most-recently-updated pages scoped to one project.
+ ///
+ /// # Errors
+ /// Propagates any SQL or pool error.
+ pub async fn recent_pages_for_project(
+ &self,
+ workspace_id: WorkspaceId,
+ project_id: ProjectId,
+ limit: usize,
+ ) -> StoreResult<Vec<PageHit>> {
+ self.with_conn(move |conn| {
+ let mut stmt = conn.prepare(
+ "SELECT id, path, title, \
+ substr(body, 1, 240) AS snip, \
+ CAST(updated_at AS REAL) AS rank \
+ FROM pages \
+ WHERE workspace_id = ?1 AND project_id = ?2 AND is_latest = 1 \
+ ORDER BY updated_at DESC \
+ LIMIT ?3",
+ )?;
+ #[allow(clippy::cast_possible_wrap)]
+ let rows = stmt.query_map(
+ params![workspace_id.as_bytes(), project_id.as_bytes(), limit as i64],
+ |row| {
+ let id_bytes: Vec<u8> = row.get(0)?;
+ let path: String = row.get(1)?;
+ let title: String = row.get(2)?;
+ let snippet: String = row.get(3)?;
+ let rank: f64 = row.get(4)?;
+ Ok((id_bytes, path, title, snippet, rank))
+ },
+ )?;
+ let mut hits = Vec::new();
+ for row in rows {
+ let (id_bytes, path, title, snippet, rank) = row?;
+ hits.push(PageHit {
+ id: PageId::from_slice(&id_bytes)?,
+ path: PagePath::new(path)?,
+ title,
+ snippet,
+ rank,
+ });
+ }
+ Ok(hits)
+ })
+ .await
+ }
+
/// Return all observations for the given session, ordered by
/// `created_at` ascending.
///
@@ -522,7 +629,9 @@ impl ReaderPool {
limit: usize,
) -> StoreResult<Vec<PageHit>> {
// Fetch FTS5 hits first.
- let fts_hits = self.search_pages(query, limit * 2).await?;
+ let fts_hits = self
+ .search_pages_for_project(workspace_id, project_id, query, limit * 2)
+ .await?;
let Some(qv) = query_vec else {
// No query vector → pure FTS5.
let mut out = fts_hits;
@@ -764,6 +873,141 @@ impl ReaderPool {
.await
}
+ /// Assemble a project-scoped [`BriefingSnapshot`].
+ ///
+ /// # Errors
+ /// Propagates any SQL or pool error.
+ #[allow(clippy::too_many_lines)]
+ pub async fn briefing_for_project(
+ &self,
+ workspace_id: WorkspaceId,
+ project_id: ProjectId,
+ recent_pages_limit: usize,
+ ) -> StoreResult<BriefingSnapshot> {
+ let recent_limit = recent_pages_limit.clamp(1, 100) as i64;
+ self.with_conn(move |conn| {
+ let now_us = jiff::Timestamp::now().as_microsecond();
+ let day_us: i64 = 86_400 * 1_000_000;
+ let cutoff_7d = now_us - 7 * day_us;
+ let cutoff_30d = now_us - 30 * day_us;
+
+ let counts = StatusCounts {
+ pages_latest: count_project(
+ conn,
+ "SELECT COUNT(*) FROM pages WHERE workspace_id = ?1 AND project_id = ?2 AND is_latest = 1",
+ workspace_id,
+ project_id,
+ )?,
+ pages_all: count_project(
+ conn,
+ "SELECT COUNT(*) FROM pages WHERE workspace_id = ?1 AND project_id = ?2",
+ workspace_id,
+ project_id,
+ )?,
+ sessions: count_project(
+ conn,
+ "SELECT COUNT(*) FROM sessions WHERE workspace_id = ?1 AND project_id = ?2",
+ workspace_id,
+ project_id,
+ )?,
+ observations: count_project(
+ conn,
+ "SELECT COUNT(*) FROM observations WHERE workspace_id = ?1 AND project_id = ?2",
+ workspace_id,
+ project_id,
+ )?,
+ };
+
+ let activity_7d = window_activity_project(conn, 7, cutoff_7d, workspace_id, project_id)?;
+ let activity_30d = window_activity_project(conn, 30, cutoff_30d, workspace_id, project_id)?;
+
+ let last_observation_at: Option<i64> = conn
+ .query_row(
+ "SELECT MAX(created_at) FROM observations WHERE workspace_id = ?1 AND project_id = ?2",
+ params![workspace_id.as_bytes(), project_id.as_bytes()],
+ |row| row.get::<_, Option<i64>>(0),
+ )
+ .optional()?
+ .flatten();
+ let last_observation_at = last_observation_at
+ .and_then(|us| jiff::Timestamp::from_microsecond(us).ok())
+ .map(|ts| ts.to_string());
+
+ let pending_handoff_count = count_project(
+ conn,
+ "SELECT COUNT(*) FROM handoffs WHERE workspace_id = ?1 AND project_id = ?2 AND state = 'open'",
+ workspace_id,
+ project_id,
+ )?;
+
+ let mut rules_stmt = conn.prepare(
+ "SELECT path, title, \
+ COALESCE(json_extract(frontmatter_json, '$.kind'), 'fact') AS kind, \
+ updated_at \
+ FROM pages \
+ WHERE workspace_id = ?1 AND project_id = ?2 AND is_latest = 1 AND path LIKE '_rules/%' \
+ ORDER BY updated_at DESC",
+ )?;
+ let rules: Vec<BriefingPage> = rules_stmt
+ .query_map(params![workspace_id.as_bytes(), project_id.as_bytes()], briefing_page_from_row)?
+ .collect::<Result<Vec<_>, _>>()?
+ .into_iter()
+ .collect::<Result<Vec<_>, _>>()?;
+
+ let mut recent_stmt = conn.prepare(
+ "SELECT path, title, \
+ COALESCE(json_extract(frontmatter_json, '$.kind'), 'fact') AS kind, \
+ updated_at \
+ FROM pages \
+ WHERE workspace_id = ?1 AND project_id = ?2 AND is_latest = 1 \
+ ORDER BY updated_at DESC \
+ LIMIT ?3",
+ )?;
+ let recent_pages: Vec<BriefingPage> = recent_stmt
+ .query_map(params![workspace_id.as_bytes(), project_id.as_bytes(), recent_limit], briefing_page_from_row)?
+ .collect::<Result<Vec<_>, _>>()?
+ .into_iter()
+ .collect::<Result<Vec<_>, _>>()?;
+
+ Ok(BriefingSnapshot {
+ counts,
+ activity_7d,
+ activity_30d,
+ last_observation_at,
+ pending_handoff_count,
+ rules,
+ recent_pages,
+ })
+ })
+ .await
+ }
+
+ /// Look up a page's workspace and project names by page id.
+ ///
+ /// # Errors
+ /// Propagates any SQL or pool error.
+ pub async fn page_meta_by_id(&self, page_id: PageId) -> StoreResult<Option<PageMeta>> {
+ self.with_conn(move |conn| {
+ let row_opt = conn
+ .query_row(
+ "SELECT w.name, p.name, w.id, p.id, pg.path, pg.title, \
+ COALESCE(json_extract(pg.frontmatter_json, '$.kind'), 'fact'), \
+ pg.tier, pg.pinned, pg.created_at, pg.updated_at, \
+ sp.path AS supersedes_path \
+ FROM pages pg \
+ JOIN projects p ON p.id = pg.project_id \
+ JOIN workspaces w ON w.id = pg.workspace_id \
+ LEFT JOIN pages sp ON sp.id = pg.supersedes \
+ WHERE pg.id = ?1 AND pg.is_latest = 1",
+ params![page_id.as_bytes()],
+ page_meta_from_row,
+ )
+ .optional()?;
+ row_opt.transpose()
+ })
+ .await
+ }
+
/// Look up a page's workspace and project names by its path (across all
/// workspaces and projects). Returns the first `is_latest = 1` match.
///
@@ -1143,6 +1387,92 @@ impl ReaderPool {
})
.await
}
+
+ /// Return aggregate counts scoped to one project.
+ ///
+ /// # Errors
+ /// Propagates any SQL or pool error.
+ pub async fn status_counts_for_project(
+ &self,
+ workspace_id: WorkspaceId,
+ project_id: ProjectId,
+ ) -> StoreResult<StatusCounts> {
+ self.with_conn(move |conn| {
+ let pages_latest = count_project(
+ conn,
+ "SELECT COUNT(*) FROM pages WHERE workspace_id = ?1 AND project_id = ?2 AND is_latest = 1",
+ workspace_id,
+ project_id,
+ )?;
+ let pages_all = count_project(
+ conn,
+ "SELECT COUNT(*) FROM pages WHERE workspace_id = ?1 AND project_id = ?2",
+ workspace_id,
+ project_id,
+ )?;
+ let sessions = count_project(
+ conn,
+ "SELECT COUNT(*) FROM sessions WHERE workspace_id = ?1 AND project_id = ?2",
+ workspace_id,
+ project_id,
+ )?;
+ let observations = count_project(
+ conn,
+ "SELECT COUNT(*) FROM observations WHERE workspace_id = ?1 AND project_id = ?2",
+ workspace_id,
+ project_id,
+ )?;
+ Ok(StatusCounts {
+ pages_latest,
+ pages_all,
+ sessions,
+ observations,
+ })
+ })
+ .await
+ }
+}
+
+fn page_meta_from_row(row: &rusqlite::Row<'_>) -> rusqlite::Result<StoreResult<PageMeta>> {
+ let workspace_name: String = row.get(0)?;
+ let project_name: String = row.get(1)?;
+ let ws_id_bytes: Vec<u8> = row.get(2)?;
+ let proj_id_bytes: Vec<u8> = row.get(3)?;
+ let path: String = row.get(4)?;
+ let title: String = row.get(5)?;
+ let kind: String = row.get(6)?;
+ let tier: String = row.get(7)?;
+ let pinned: i64 = row.get(8)?;
+ let created_us: i64 = row.get(9)?;
+ let updated_us: i64 = row.get(10)?;
+ let supersedes: Option<String> = row.get(11)?;
+
+ let workspace_id = WorkspaceId::from_slice(&ws_id_bytes)
+ .map_err(|_| rusqlite::Error::IntegralValueOutOfRange(2, 0))?;
+ let project_id = ProjectId::from_slice(&proj_id_bytes)
+ .map_err(|_| rusqlite::Error::IntegralValueOutOfRange(3, 0))?;
+
+ let created_at = jiff::Timestamp::from_microsecond(created_us)
+ .map(|ts| ts.to_string())
+ .unwrap_or_default();
+ let updated_at = jiff::Timestamp::from_microsecond(updated_us)
+ .map(|ts| ts.to_string())
+ .unwrap_or_default();
+
+ Ok(Ok(PageMeta {
+ workspace_name,
+ project_name,
+ workspace_id,
+ project_id,
+ path,
+ title,
+ kind,
+ tier,
+ pinned: pinned != 0,
+ created_at,
+ updated_at,
+ supersedes,
+ }))
}
fn row_to_observation(row: &rusqlite::Row<'_>) -> rusqlite::Result<StoreResult<Observation>> {
@@ -1426,6 +1756,22 @@ fn count(conn: &Connection, sql: &str) -> StoreResult<u64> {
Ok(u64::try_from(n.unwrap_or(0)).unwrap_or(0))
}
+fn count_project(
+ conn: &Connection,
+ sql: &str,
+ workspace_id: WorkspaceId,
+ project_id: ProjectId,
+) -> StoreResult<u64> {
+ let n: Option<i64> = conn
+ .query_row(
+ sql,
+ params![workspace_id.as_bytes(), project_id.as_bytes()],
+ |row| row.get(0),
+ )
+ .optional()?;
+ Ok(u64::try_from(n.unwrap_or(0)).unwrap_or(0))
+}
+
/// Count rows in a time-bounded window. Used by [`ReaderPool::briefing`]
/// to compute "last 7 days" / "last 30 days" activity slices.
fn window_activity(conn: &Connection, days: u32, cutoff_us: i64) -> StoreResult<ActivityWindow> {
@@ -1447,6 +1793,37 @@ fn window_activity(conn: &Connection, days: u32, cutoff_us: i64) -> StoreResult<
})
}
+fn window_activity_project(
+ conn: &Connection,
+ days: u32,
+ cutoff_us: i64,
+ workspace_id: WorkspaceId,
+ project_id: ProjectId,
+) -> StoreResult<ActivityWindow> {
+ let count_since = |sql: &str| -> StoreResult<u64> {
+ let n: Option<i64> = conn
+ .query_row(
+ sql,
+ params![workspace_id.as_bytes(), project_id.as_bytes(), cutoff_us],
+ |row| row.get(0),
+ )
+ .optional()?;
+ Ok(u64::try_from(n.unwrap_or(0)).unwrap_or(0))
+ };
+ Ok(ActivityWindow {
+ days,
+ sessions: count_since(
+ "SELECT COUNT(*) FROM sessions WHERE workspace_id = ?1 AND project_id = ?2 AND started_at > ?3",
+ )?,
+ observations: count_since(
+ "SELECT COUNT(*) FROM observations WHERE workspace_id = ?1 AND project_id = ?2 AND created_at > ?3",
+ )?,
+ pages_updated: count_since(
+ "SELECT COUNT(*) FROM pages WHERE workspace_id = ?1 AND project_id = ?2 AND is_latest = 1 AND updated_at > ?3",
+ )?,
+ })
+}
+
/// Materialise one row from the briefing's recent-pages / rules queries
/// into a [`BriefingPage`]. The row shape is `(path, title, kind,
/// updated_at_us)` — all queries above MUST select those columns in
diff --git a/crates/ai-memory-web/Cargo.toml b/crates/ai-memory-web/Cargo.toml
index 58a444f9..0b18da53 100644
--- a/crates/ai-memory-web/Cargo.toml
+++ b/crates/ai-memory-web/Cargo.toml
@@ -30,6 +30,7 @@ syntect = { version = "5", default-features = false, features = ["default-fancy"
[build-dependencies]
anyhow.workspace = true
+sha2.workspace = true
[dev-dependencies]
tempfile.workspace = true
diff --git a/crates/ai-memory-web/build.rs b/crates/ai-memory-web/build.rs
index ea8d1476..cc1ec7f2 100644
--- a/crates/ai-memory-web/build.rs
+++ b/crates/ai-memory-web/build.rs
@@ -15,7 +15,11 @@ use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::SystemTime;
+use sha2::{Digest, Sha256};
+
const TAILWIND_VERSION: &str = "3.4.17";
+const TAILWIND_LINUX_X64_SHA256: &str =
+ "7d24f7fa191d2193b78cd5f5a42a6093e14409521908529f42d80b11fde1f1d4";
fn main() {
// Re-run triggers.
@@ -103,9 +107,16 @@ fn mtime(p: &Path) -> std::io::Result<SystemTime> {
/// Platform-specific download URL for the Tailwind CLI binary.
fn tailwind_url() -> String {
+ let slug = tailwind_slug();
+ format!(
+ "https://github.com/tailwindlabs/tailwindcss/releases/download/v{TAILWIND_VERSION}/{slug}"
+ )
+}
+
+fn tailwind_slug() -> &'static str {
let os = std::env::consts::OS;
let arch = std::env::consts::ARCH;
- let slug = match (os, arch) {
+ match (os, arch) {
("linux", "x86_64") => "tailwindcss-linux-x64",
("linux", "aarch64") => "tailwindcss-linux-arm64",
("macos", "x86_64") => "tailwindcss-macos-x64",
@@ -114,10 +125,34 @@ fn tailwind_url() -> String {
_ => panic!(
"Unsupported platform {os}/{arch} — set TAILWIND_SKIP=1 and provide static/tailwind.css manually"
),
- };
- format!(
- "https://github.com/tailwindlabs/tailwindcss/releases/download/v{TAILWIND_VERSION}/{slug}"
- )
+ }
+}
+
+fn expected_tailwind_sha256() -> &'static str {
+ match tailwind_slug() {
+ "tailwindcss-linux-x64" => TAILWIND_LINUX_X64_SHA256,
+ other => panic!(
+ "No pinned Tailwind SHA-256 for {other}. Set TAILWIND_SKIP=1 and provide static/tailwind.css manually."
+ ),
+ }
+}
+
+fn verify_tailwind_checksum(path: &Path) {
+ let expected = expected_tailwind_sha256();
+ let bytes = std::fs::read(path).unwrap_or_else(|e| {
+ panic!(
+ "failed reading downloaded Tailwind binary {}: {e}",
+ path.display()
+ )
+ });
+ let got = format!("{:x}", Sha256::digest(&bytes));
+ if got != expected {
+ let _ = std::fs::remove_file(path);
+ panic!(
+ "Tailwind CSS CLI checksum mismatch for {}: expected {expected}, got {got}",
+ path.display()
+ );
+ }
}
/// Download the Tailwind CLI and return the path to the executable.
@@ -138,7 +173,7 @@ fn download_tailwind(out_dir: &Path) -> PathBuf {
let dest = cache_dir.join(&bin_name);
if dest.exists() {
- // Already cached.
+ verify_tailwind_checksum(&dest);
return dest;
}
@@ -156,6 +191,8 @@ fn download_tailwind(out_dir: &Path) -> PathBuf {
);
}
+ verify_tailwind_checksum(&dest);
+
// Make executable on Unix.
#[cfg(unix)]
{
diff --git a/crates/ai-memory-web/src/markdown.rs b/crates/ai-memory-web/src/markdown.rs
index 0530ab7f..c3554ed2 100644
--- a/crates/ai-memory-web/src/markdown.rs
+++ b/crates/ai-memory-web/src/markdown.rs
@@ -2,13 +2,13 @@
//!
//! Stub at scaffold time; full implementation in the next step.
-use pulldown_cmark::{Options, Parser, html};
+use pulldown_cmark::{CowStr, Event, Options, Parser, Tag, html};
/// Render a markdown body to HTML using GFM-ish defaults.
///
-/// v1: trust the wiki source (the wiki is on-disk markdown the
-/// project owner writes/consolidates; not user-uploaded content from
-/// arbitrary callers). Syntax highlighting is deferred.
+/// Raw HTML is escaped and unsafe link/image schemes are neutralised.
+/// Wiki content can be derived from prompts, hooks, or LLM output, so
+/// the browser surface must treat it as untrusted.
#[must_use]
pub fn render(body: &str) -> String {
let mut opts = Options::empty();
@@ -17,12 +17,81 @@ pub fn render(body: &str) -> String {
opts.insert(Options::ENABLE_STRIKETHROUGH);
opts.insert(Options::ENABLE_TASKLISTS);
opts.insert(Options::ENABLE_SMART_PUNCTUATION);
- let parser = Parser::new_ext(body, opts);
+ let parser = Parser::new_ext(body, opts).map(sanitize_event);
let mut out = String::with_capacity(body.len() + body.len() / 4);
html::push_html(&mut out, parser);
out
}
+fn sanitize_event(event: Event<'_>) -> Event<'_> {
+ match event {
+ Event::Html(s) | Event::InlineHtml(s) => Event::Text(s),
+ Event::Start(Tag::Link {
+ link_type,
+ dest_url,
+ title,
+ id,
+ }) => Event::Start(Tag::Link {
+ link_type,
+ dest_url: safe_url(dest_url),
+ title,
+ id,
+ }),
+ Event::Start(Tag::Image {
+ link_type,
+ dest_url,
+ title,
+ id,
+ }) => Event::Start(Tag::Image {
+ link_type,
+ dest_url: safe_url(dest_url),
+ title,
+ id,
+ }),
+ other => other,
+ }
+}
+
+fn safe_url(url: CowStr<'_>) -> CowStr<'_> {
+ let trimmed = url.trim_start();
+ let lower = trimmed.to_ascii_lowercase();
+ if lower.starts_with("http://")
+ || lower.starts_with("https://")
+ || lower.starts_with("mailto:")
+ || lower.starts_with('/')
+ || lower.starts_with('#')
+ || !lower.contains(':')
+ {
+ url
+ } else {
+ CowStr::Boxed("#".into())
+ }
+}
+
+/// Escape text for insertion into an HTML template while preserving the
+/// fixed `<mark>` tags emitted by SQLite FTS snippets.
+#[must_use]
+pub fn escape_snippet(snippet: &str) -> String {
+ escape_html(snippet)
+ .replace("&lt;mark&gt;", "<mark>")
+ .replace("&lt;/mark&gt;", "</mark>")
+}
+
+fn escape_html(input: &str) -> String {
+ let mut out = String::with_capacity(input.len());
+ for c in input.chars() {
+ match c {
+ '&' => out.push_str("&amp;"),
+ '<' => out.push_str("&lt;"),
+ '>' => out.push_str("&gt;"),
+ '"' => out.push_str("&quot;"),
+ '\'' => out.push_str("&#39;"),
+ _ => out.push(c),
+ }
+ }
+ out
+}
+
/// Drop the leading H1 from a markdown body if present. Static-site
/// convention: the first H1 IS the page title, and the page template
/// already renders the title in its header — leaving it in the body
@@ -69,6 +138,27 @@ mod tests {
assert!(html.contains("<td>1</td>"));
}
+ #[test]
+ fn escapes_raw_html() {
+ let html = render("<script>alert(1)</script>");
+ assert!(html.contains("&lt;script&gt;alert(1)&lt;/script&gt;"));
+ assert!(!html.contains("<script>"));
+ }
+
+ #[test]
+ fn neutralises_javascript_links() {
+ let html = render("[x](javascript:alert(1))");
+ assert!(html.contains("href=\"#\""));
+ assert!(!html.contains("javascript:"));
+ }
+
+ #[test]
+ fn escapes_search_snippet_but_keeps_marks() {
+ let out = escape_snippet("<script>x</script> <mark>hit</mark>");
+ assert!(out.contains("&lt;script&gt;x&lt;/script&gt;"));
+ assert!(out.contains("<mark>hit</mark>"));
+ }
+
#[test]
fn strip_atx_h1_drops_first_heading() {
let out = strip_leading_h1("# Title\n\nbody text\n");
diff --git a/crates/ai-memory-web/src/routes/search.rs b/crates/ai-memory-web/src/routes/search.rs
index f2878143..2132ec2a 100644
--- a/crates/ai-memory-web/src/routes/search.rs
+++ b/crates/ai-memory-web/src/routes/search.rs
@@ -8,6 +8,7 @@ use axum::http::StatusCode;
use axum::response::Html;
use serde::Deserialize;
+use crate::markdown;
use crate::state::WebState;
use crate::templates::{SearchHit, SearchView};
@@ -46,13 +47,13 @@ pub(crate) async fn handler(
// Get workspace + project by looking up full meta.
// We try across all project/workspace combos by using the raw
// search hit's path and calling a lightweight query.
- if let Ok(Some(m)) = state.reader.page_meta_by_path(h.path.as_str()).await {
+ if let Ok(Some(m)) = state.reader.page_meta_by_id(h.id).await {
results.push(SearchHit {
workspace: m.workspace_name,
project: m.project_name,
path: h.path.as_str().to_owned(),
title: h.title,
- snippet: h.snippet,
+ snippet: markdown::escape_snippet(&h.snippet),
});
} else {
// Fallback: no workspace/project known; skip for now.