Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

Mask free-text credentials (password=…, token: …, "api_key":"…") in redacted text

Lin (kwant bot)517d40073280
diff --git a/src/profiles/classify.rs b/src/profiles/classify.rs
index 0980fc6d..b22d9a9e 100644
--- a/src/profiles/classify.rs
+++ b/src/profiles/classify.rs
@@ -205,6 +205,73 @@ fn relative_reset(lower: &str) -> Option<Duration> {
found.then_some(total)
}
+/// Key names (matched case-insensitively, as the whole name or its suffix, so `client_secret` and
+/// `x-api-key` count) whose value is a credential when written as `name=value` or `name: value`.
+const CREDENTIAL_KEYS: [&str; 8] = [
+ "password", "passwd", "pwd", "secret", "token", "api_key", "apikey", "api-key",
+];
+
+/// Masks the value in free-text credentials such as `password=hunter2`, `pwd: hunter2` or
+/// `"api_key":"…"`, keeping the key: `password=[redacted]`. Only a key followed by `=` or `:`
+/// counts, so prose like "the password field is empty" is left alone.
+pub fn mask_credentials(text: &str) -> String {
+ let is_name = |b: u8| b.is_ascii_alphanumeric() || b == b'_' || b == b'-';
+ let bytes = text.as_bytes();
+ let mut out = String::with_capacity(text.len());
+ let mut copied = 0;
+ let mut i = 0;
+ while i < bytes.len() {
+ if !is_name(bytes[i]) || (i > 0 && is_name(bytes[i - 1])) {
+ i += 1;
+ continue;
+ }
+ let start = i;
+ while i < bytes.len() && is_name(bytes[i]) {
+ i += 1;
+ }
+ let name = text[start..i].to_ascii_lowercase();
+ if !CREDENTIAL_KEYS.iter().any(|k| name.ends_with(k)) {
+ continue;
+ }
+ // name, an optional closing quote, then `=` or `:`
+ let mut j = i;
+ if j < bytes.len() && (bytes[j] == b'"' || bytes[j] == b'\'') {
+ j += 1;
+ }
+ while j < bytes.len() && bytes[j] == b' ' {
+ j += 1;
+ }
+ if j >= bytes.len() || (bytes[j] != b'=' && bytes[j] != b':') {
+ continue;
+ }
+ j += 1;
+ while j < bytes.len() && bytes[j] == b' ' {
+ j += 1;
+ }
+ // a quoted value up to its closing quote, otherwise up to whitespace or punctuation
+ let (from, to) = match bytes.get(j) {
+ Some(&q) if q == b'"' || q == b'\'' => {
+ let end = text[j + 1..].find(q as char).map_or(bytes.len(), |e| j + 1 + e);
+ (j + 1, end)
+ }
+ _ => {
+ let end = text[j..]
+ .find(|c: char| c.is_whitespace() || ",;&)]}\"'".contains(c))
+ .map_or(bytes.len(), |e| j + e);
+ (j, end)
+ }
+ };
+ if to > from {
+ out.push_str(&text[copied..from]);
+ out.push_str("[redacted]");
+ copied = to;
+ i = to;
+ }
+ }
+ out.push_str(&text[copied..]);
+ out
+}
+
/// Shortens provider error text for a chat notice: one line, at most `max` chars, with anything
/// that looks like a key or token masked.
pub fn redact_detail(text: &str, max: usize) -> String {
@@ -213,6 +280,7 @@ pub fn redact_detail(text: &str, max: usize) -> String {
.map(str::trim)
.find(|l| !l.is_empty())
.unwrap_or_default();
+ let line = mask_credentials(line);
let masked: Vec<String> = line
.split(' ')
.map(|w| {
@@ -375,4 +443,41 @@ mod tests {
assert!(out.contains("[redacted]"));
assert_eq!(redact_detail(&"x".repeat(300), 10).chars().count(), 11);
}
+
+ #[test]
+ fn free_text_credentials_keep_the_key_and_lose_the_value() {
+ for (text, want) in [
+ ("login failed password=hunter2 for bob", "login failed password=[redacted] for bob"),
+ ("password: hunter2", "password: [redacted]"),
+ ("PASSWORD=Hunter2", "PASSWORD=[redacted]"),
+ ("pwd=hunter2", "pwd=[redacted]"),
+ ("passwd=hunter2&user=bob", "passwd=[redacted]&user=bob"),
+ ("secret=s3cr3t", "secret=[redacted]"),
+ ("client_secret: abc", "client_secret: [redacted]"),
+ ("token=abc.def, then", "token=[redacted], then"),
+ ("api_key=abc123", "api_key=[redacted]"),
+ ("X-Api-Key: abc123", "X-Api-Key: [redacted]"),
+ (
+ r#"{"user":"bob","password":"hunter 2","api_key": "abc"}"#,
+ r#"{"user":"bob","password":"[redacted]","api_key": "[redacted]"}"#,
+ ),
+ ("password='hunter2'", "password='[redacted]'"),
+ ] {
+ assert_eq!(redact_detail(text, 500), want, "{text}");
+ }
+ }
+
+ #[test]
+ fn prose_about_credentials_is_left_alone() {
+ for text in [
+ "the password field is empty",
+ "Password incorrect, please try again",
+ "your token expired",
+ "max_tokens=4096 and tokens: 12",
+ "password:",
+ "keyboard=us",
+ ] {
+ assert_eq!(redact_detail(text, 500), text);
+ }
+ }
}
diff --git a/src/server/away.rs b/src/server/away.rs
index 39c8d1c8..1552b72c 100644
--- a/src/server/away.rs
+++ b/src/server/away.rs
@@ -7,7 +7,7 @@
use super::AppState;
use crate::db::away::{AwayRow, LAST_SEEN_KEY};
-use crate::profiles::classify::redact_detail;
+use crate::profiles::classify::{mask_credentials, redact_detail};
use axum::{
extract::{Query, State},
http::StatusCode,
@@ -47,13 +47,15 @@ fn looks_secret(word: &str) -> bool {
}
/// One line for a card: the first non-empty line without markdown markers, at most
-/// [`SUMMARY_CHARS`], with any word that has a key-like part (`"sk-…"`, `key=…`) masked.
+/// [`SUMMARY_CHARS`], with free-text credentials (`password=…`) and any word that has a key-like
+/// part (`"sk-…"`) masked.
pub fn summarize(text: &str) -> String {
let line = text
.lines()
.map(|l| l.trim().trim_start_matches(['#', '>', '*', '-', ' ']).trim())
.find(|l| !l.is_empty())
.unwrap_or_default();
+ let line = mask_credentials(line);
let masked: Vec<&str> = line
.split_whitespace()
.map(|w| {
diff --git a/tests/away_test.rs b/tests/away_test.rs
index 560d37d2..7fc9700d 100644
--- a/tests/away_test.rs
+++ b/tests/away_test.rs
@@ -179,7 +179,7 @@ async fn away_lists_every_kind_filters_caps_and_marks_seen() {
turn_id: "turn-gone".into(),
kwant_id: lead.id.clone(),
tool_name: format!("mcp__mail__{tool}"),
- parameters: json!({"server": "mail", "tool": tool, "arguments": {"to": "[email protected]", "api_key": "hidden-value"}}),
+ parameters: json!({"server": "mail", "tool": tool, "arguments": {"to": "[email protected]", "api_key": "hidden-value", "command": "mysql -u bob password=hunter2"}}),
};
let live = state.db.save_approval(&approval("send")).await.unwrap();
let _rx = state
@@ -231,7 +231,7 @@ async fn away_lists_every_kind_filters_caps_and_marks_seen() {
let v = get(&app, "/api/away").await;
let body = v.to_string();
- assert!(!body.contains(SECRET) && !body.contains("hidden-value"), "secret leaked: {body}");
+ assert!(!body.contains(SECRET) && !body.contains("hidden-value") && !body.contains("hunter2"), "secret leaked: {body}");
// Waiting ignores since; the stale ticket and the answered question aren't there
assert_eq!(ids(&v["waiting"]), vec![live.id.clone(), q.id.clone()]);
@@ -247,6 +247,8 @@ async fn away_lists_every_kind_filters_caps_and_marks_seen() {
assert_eq!(ai["status"], "pending");
assert_eq!(ai["title"], "Use send (mail)");
assert!(ai["summary"].as_str().unwrap().contains("to: [email protected]"));
+ // A free-text password in an ordinary argument keeps its key and loses its value
+ assert!(ai["summary"].as_str().unwrap().contains("command: mysql -u bob password=[redacted]"), "{}", ai["summary"]);
// No live turn to follow: it opens the agent's direct chat
assert_eq!(ai["link"]["direct_kwant_id"], lead.id.as_str());
assert!(!ids(&v["waiting"]).contains(&stale.id));