use crate::channel::{
ApprovalPrompt, Attachment, Channel, ChannelCapabilities, ChannelError, ChannelEvent,
CommandInvocation, CommandSpec, Conversation, MessageRef, OutboundFile, OutboundMessage,
RunProgress,
};
pub fn redact_text(text: &str) -> String {
aidaemon_tools::sanitize::redact_for_owner(text)
}
pub fn redact_text_in(scope: &aidaemon_core::RedactionScope, text: &str) -> String {
aidaemon_tools::sanitize::redact_for_owner_in(scope, text)
}
pub fn redact_body(body: &serde_json::Value) -> serde_json::Value {
redact_body_in(&aidaemon_core::RedactionScope::process(), body)
}
pub fn redact_body_in(
scope: &aidaemon_core::RedactionScope,
body: &serde_json::Value,
) -> serde_json::Value {
let mut body = body.clone();
aidaemon_tools::sanitize::redact_json_for_owner_in(scope, &mut body);
body
}
pub struct RedactingChannel {
inner: std::sync::Arc<dyn Channel>,
scope: aidaemon_core::RedactionScope,
}
impl RedactingChannel {
pub fn new(channel: std::sync::Arc<dyn Channel>, scope: aidaemon_core::RedactionScope) -> Self {
Self {
inner: channel,
scope,
}
}
pub fn inner(&self) -> &std::sync::Arc<dyn Channel> {
&self.inner
}
fn text(&self, text: &str) -> String {
redact_text_in(&self.scope, text)
}
fn progress(&self, progress: &RunProgress) -> RunProgress {
let mut progress = progress.clone();
progress.streamed = self.text(&progress.streamed);
progress
}
}
#[async_trait::async_trait]
impl Channel for RedactingChannel {
fn name(&self) -> &str {
self.inner.name()
}
fn capabilities(&self) -> ChannelCapabilities {
self.inner.capabilities()
}
async fn run(
&self,
events: tokio::sync::mpsc::Sender<ChannelEvent>,
shutdown: tokio_util::sync::CancellationToken,
) -> Result<(), ChannelError> {
self.inner.run(events, shutdown).await
}
async fn send(
&self,
conversation: &Conversation,
message: &OutboundMessage,
) -> Result<MessageRef, ChannelError> {
let mut message = message.clone();
message.text = self.text(&message.text);
self.inner.send(conversation, &message).await
}
async fn edit(&self, message: &MessageRef, text: &str) -> Result<(), ChannelError> {
self.inner.edit(message, &self.text(text)).await
}
async fn ask(&self, prompt: &ApprovalPrompt) -> Result<MessageRef, ChannelError> {
let mut prompt = prompt.clone();
prompt.text = self.text(&prompt.text);
for choice in &mut prompt.choices {
choice.label = self.text(&choice.label);
}
prompt.details = prompt.details.take().and_then(|details| {
let value = serde_json::to_value(&details).ok()?;
serde_json::from_value(redact_body_in(&self.scope, &value)).ok()
});
self.inner.ask(&prompt).await
}
async fn remove_buttons(&self, message: &MessageRef) -> Result<(), ChannelError> {
self.inner.remove_buttons(message).await
}
async fn send_status(
&self,
conversation: &Conversation,
progress: &RunProgress,
) -> Result<MessageRef, ChannelError> {
self.inner
.send_status(conversation, &self.progress(progress))
.await
}
async fn update_status(
&self,
message: &MessageRef,
progress: &RunProgress,
) -> Result<(), ChannelError> {
self.inner
.update_status(message, &self.progress(progress))
.await
}
fn status_text(&self, progress: &RunProgress) -> String {
self.text(&self.inner.status_text(progress))
}
async fn typing(&self, conversation: &Conversation) -> Result<(), ChannelError> {
self.inner.typing(conversation).await
}
async fn delete(&self, message: &MessageRef) -> Result<(), ChannelError> {
self.inner.delete(message).await
}
async fn send_file(
&self,
conversation: &Conversation,
file: &OutboundFile,
) -> Result<MessageRef, ChannelError> {
let mut file = file.clone();
file.name = self.text(&file.name);
file.caption = file.caption.as_deref().map(|caption| self.text(caption));
self.inner.send_file(conversation, &file).await
}
async fn download(&self, attachment: &Attachment) -> Result<Vec<u8>, ChannelError> {
self.inner.download(attachment).await
}
async fn download_limited(
&self,
attachment: &Attachment,
max_bytes: u64,
) -> Result<Vec<u8>, ChannelError> {
self.inner.download_limited(attachment, max_bytes).await
}
async fn register_commands(&self, commands: &[CommandSpec]) -> Result<(), ChannelError> {
self.inner.register_commands(commands).await
}
fn strips_latex(&self) -> bool {
self.inner.strips_latex()
}
async fn display_name(&self, user_id: &str) -> Result<Option<String>, ChannelError> {
self.inner.display_name(user_id).await
}
async fn answer_command(
&self,
command: &CommandInvocation,
text: &str,
) -> Result<(), ChannelError> {
self.inner.answer_command(command, &self.text(text)).await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_password_line_never_reaches_a_chat() {
let body = serde_json::json!({
"chat_id": 42,
"text": "Running a command · [success] User name: admin User password: Wire-Synthetic-91",
"reply_markup": {"inline_keyboard": [[{"text": "OK", "callback_data": "a:1"}]]},
});
let sent = redact_body(&body);
let text = sent["text"].as_str().unwrap();
assert!(!text.contains("Wire-Synthetic-91"), "{text}");
assert!(text.contains("User password: [REDACTED:"), "{text}");
assert_eq!(sent["chat_id"], 42);
assert_eq!(sent["reply_markup"], body["reply_markup"]);
let later = redact_text("Your admin login is admin / Wire-Synthetic-91.");
assert!(!later.contains("Wire-Synthetic-91"), "{later}");
}
#[derive(Default)]
struct Raw {
shown: std::sync::Mutex<Vec<String>>,
}
impl Raw {
fn note(&self, text: impl Into<String>) -> MessageRef {
self.shown.lock().unwrap().push(text.into());
MessageRef {
conversation_id: "c".into(),
message_id: "m".into(),
}
}
}
#[async_trait::async_trait]
impl Channel for Raw {
fn name(&self) -> &str {
"raw"
}
fn capabilities(&self) -> ChannelCapabilities {
ChannelCapabilities::default()
}
async fn run(
&self,
_events: tokio::sync::mpsc::Sender<ChannelEvent>,
_shutdown: tokio_util::sync::CancellationToken,
) -> Result<(), ChannelError> {
Ok(())
}
async fn send(
&self,
_conversation: &Conversation,
message: &OutboundMessage,
) -> Result<MessageRef, ChannelError> {
Ok(self.note(&message.text))
}
async fn edit(&self, _message: &MessageRef, text: &str) -> Result<(), ChannelError> {
self.note(text);
Ok(())
}
async fn ask(&self, prompt: &ApprovalPrompt) -> Result<MessageRef, ChannelError> {
self.note(serde_json::to_string(prompt).unwrap());
Ok(self.note(&prompt.text))
}
async fn send_file(
&self,
_conversation: &Conversation,
file: &OutboundFile,
) -> Result<MessageRef, ChannelError> {
self.note(&file.name);
Ok(self.note(file.caption.clone().unwrap_or_default()))
}
}
#[tokio::test]
async fn a_wrapped_adapter_is_handed_nothing_unredacted() {
const SECRET: &str = "tok_wrapped_9a8b7c6d";
let scope = aidaemon_core::RedactionScope::isolated();
scope.register(SECRET).unwrap();
let raw = std::sync::Arc::new(Raw::default());
let channel = RedactingChannel::new(raw.clone(), scope);
let conversation = Conversation {
id: "c".into(),
kind: crate::ConversationKind::Direct,
};
let line = format!("token {SECRET}; User password: Wrapped-Pass-77");
let shown = channel
.send(&conversation, &OutboundMessage::text(&line))
.await
.unwrap();
channel.edit(&shown, &line).await.unwrap();
let mut progress = RunProgress::working();
progress.streamed = line.clone();
channel.send_status(&conversation, &progress).await.unwrap();
channel.update_status(&shown, &progress).await.unwrap();
let prompt = ApprovalPrompt::new(
"p1",
conversation.clone(),
&line,
vec![crate::ApprovalChoice {
id: "approve".into(),
label: line.clone(),
}],
);
channel.ask(&prompt).await.unwrap();
channel
.send_file(
&conversation,
&OutboundFile {
name: format!("{SECRET}.txt"),
bytes: Vec::new(),
mime_type: None,
caption: Some(line.clone()),
},
)
.await
.unwrap();
let command = CommandInvocation {
conversation: conversation.clone(),
sender: crate::Sender {
id: "u".into(),
display_name: None,
is_bot: false,
},
name: "token".into(),
args: String::new(),
handle: "h".into(),
};
channel.answer_command(&command, &line).await.unwrap();
let shown = raw.shown.lock().unwrap().clone();
assert!(shown.len() >= 9, "{shown:?}");
for text in &shown {
assert!(!text.contains(SECRET), "{text}");
assert!(!text.contains("Wrapped-Pass-77"), "{text}");
}
assert_eq!(channel.name(), "raw");
}
}