mod shape;
#[cfg(test)]
mod tests;
use std::{borrow::Cow, ops::Range, sync::Arc};
use aho_corasick::{AhoCorasick, MatchKind};
use aither_core::{
LanguageModel,
llm::{
Event, GenerateError, LLMRequest, LLMRequestWithTools, Message, RetryPolicy, model::Profile,
},
};
use arc_swap::ArcSwap;
use askama::Template;
use futures_core::Stream;
use futures_lite::StreamExt;
use schemars::JsonSchema;
use serde::de::DeserializeOwned;
use serde_json::Value;
pub use shape::SecretShape;
use shape::SecretShapes;
#[derive(Clone)]
pub struct SecretGuard {
shared: Arc<Shared>,
}
struct Shared {
shapes: SecretShapes,
registry: ArcSwap<Registry>,
}
#[derive(Default)]
struct Registry {
names: Vec<Arc<str>>,
values: Vec<Arc<str>>,
matcher: Option<AhoCorasick>,
}
impl Registry {
fn with(&self, name: &str, value: &str) -> Self {
let mut names = self.names.clone();
let mut values = self.values.clone();
if let Some(index) = names.iter().position(|known| &**known == name) {
values[index] = value.into();
} else {
names.push(name.into());
values.push(value.into());
}
let matcher = AhoCorasick::builder()
.match_kind(MatchKind::LeftmostLongest)
.build(values.iter().map(|value| value.as_bytes()))
.expect("a handful of registered secrets fits the automaton");
Self {
names,
values,
matcher: Some(matcher),
}
}
}
impl std::fmt::Debug for SecretGuard {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SecretGuard")
.field("registered", &self.shared.registry.load().names)
.finish_non_exhaustive()
}
}
impl Default for SecretGuard {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SecretKind {
Registered {
name: Arc<str>,
},
Shaped(SecretShape),
}
impl std::fmt::Display for SecretKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Registered { name } => write!(f, "secret {name}"),
Self::Shaped(shape) => shape.fmt(f),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SecretFinding {
pub kind: SecretKind,
pub range: Range<usize>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SecretRefusal {
pub findings: Vec<SecretFinding>,
}
#[derive(Template)]
#[template(path = "secret_placeholder.txt", escape = "none")]
struct PlaceholderTemplate<'a> {
name: &'a str,
}
const SHAPE_PLACEHOLDER: &str = include_str!("texts/secret_shape_placeholder.txt");
#[derive(Default)]
struct Tally {
registered: Vec<Arc<str>>,
shaped: Vec<SecretShape>,
}
impl Tally {
fn record(&mut self, kind: &SecretKind) {
match kind {
SecretKind::Registered { name } => self.registered.push(Arc::clone(name)),
SecretKind::Shaped(shape) => self.shaped.push(*shape),
}
}
fn log(self, subject: &str) {
if self.registered.is_empty() && self.shaped.is_empty() {
return;
}
let registered: Vec<&str> = self.registered.iter().map(|name| &**name).collect();
tracing::info!(
registered = ?registered,
shaped = ?self.shaped,
"replaced secrets in {subject}",
);
}
}
impl SecretGuard {
#[must_use]
pub fn new() -> Self {
Self {
shared: Arc::new(Shared {
shapes: SecretShapes::new(),
registry: ArcSwap::from_pointee(Registry::default()),
}),
}
}
pub fn register(&self, name: impl AsRef<str>, value: impl AsRef<str>) {
let (name, value) = (name.as_ref(), value.as_ref());
assert!(!value.is_empty(), "secret {name} is registered empty");
self.shared
.registry
.rcu(|registry| Arc::new(registry.with(name, value)));
}
pub fn guard<M>(&self, model: M) -> Guarded<M> {
Guarded {
model,
guard: self.clone(),
}
}
#[must_use]
pub fn find(&self, text: &str) -> Vec<SecretFinding> {
let registry = self.shared.registry.load();
let mut findings: Vec<SecretFinding> = registry
.matcher
.as_ref()
.map(|matcher| {
matcher
.find_iter(text)
.map(|hit| SecretFinding {
kind: SecretKind::Registered {
name: Arc::clone(®istry.names[hit.pattern().as_usize()]),
},
range: hit.range(),
})
.collect()
})
.unwrap_or_default();
let mut taken: Vec<Range<usize>> = findings
.iter()
.map(|finding| finding.range.clone())
.collect();
let mut shaped = Vec::new();
self.shared.shapes.find(text, |shape, range| {
if !taken.iter().any(|kept| overlaps(kept, &range)) {
taken.push(range.clone());
shaped.push(SecretFinding {
kind: SecretKind::Shaped(shape),
range,
});
}
});
findings.extend(shaped);
findings.sort_by_key(|finding| finding.range.start);
findings
}
pub fn check(&self, text: &str) -> Result<(), SecretRefusal> {
let findings = self.find(text);
if findings.is_empty() {
Ok(())
} else {
Err(SecretRefusal { findings })
}
}
#[must_use]
pub fn redact<'a>(&self, text: &'a str) -> Cow<'a, str> {
let mut tally = Tally::default();
let redacted = self.redact_text(text, &mut tally);
tally.log("a text");
redacted
}
fn redact_request(&self, request: &mut LLMRequest) {
let mut tally = Tally::default();
for message in request.messages_mut() {
match message {
Message::User { content, .. }
| Message::System { content }
| Message::Tool { content, .. } => self.redact_in_place(content, &mut tally),
Message::Assistant {
content,
tool_calls,
reasoning: _,
} => {
self.redact_in_place(content, &mut tally);
for call in tool_calls
.iter_mut()
.filter(|call| call.reasoning_state.is_none())
{
self.redact_value(&mut call.arguments, &mut tally);
}
}
Message::Configuration { .. } => {}
}
}
tally.log("a model request");
}
fn redact_value(&self, value: &mut Value, tally: &mut Tally) {
match value {
Value::String(text) => self.redact_in_place(text, tally),
Value::Array(items) => {
for item in items {
self.redact_value(item, tally);
}
}
Value::Object(fields) => {
for field in fields.values_mut() {
self.redact_value(field, tally);
}
}
Value::Null | Value::Bool(_) | Value::Number(_) => {}
}
}
fn redact_in_place(&self, text: &mut String, tally: &mut Tally) {
if let Cow::Owned(redacted) = self.redact_text(text, tally) {
*text = redacted;
}
}
fn redact_text<'a>(&self, text: &'a str, tally: &mut Tally) -> Cow<'a, str> {
let findings = self.find(text);
if findings.is_empty() {
return Cow::Borrowed(text);
}
let mut redacted = String::with_capacity(text.len());
let mut copied = 0;
for finding in findings {
redacted.push_str(&text[copied..finding.range.start]);
match &finding.kind {
SecretKind::Registered { name } => PlaceholderTemplate { name }
.render_into(&mut redacted)
.expect("the secret placeholder renders"),
SecretKind::Shaped(_) => redacted.push_str(SHAPE_PLACEHOLDER),
}
copied = finding.range.end;
tally.record(&finding.kind);
}
redacted.push_str(&text[copied..]);
Cow::Owned(redacted)
}
}
const fn overlaps(a: &Range<usize>, b: &Range<usize>) -> bool {
a.start < b.end && b.start < a.end
}
#[derive(Debug, Clone)]
pub struct Guarded<M> {
model: M,
guard: SecretGuard,
}
impl<M> Guarded<M> {
pub const fn inner(&self) -> &M {
&self.model
}
pub const fn guard(&self) -> &SecretGuard {
&self.guard
}
}
impl<M: LanguageModel> LanguageModel for Guarded<M> {
type Error = M::Error;
fn respond(
&self,
mut request: LLMRequest,
) -> impl Stream<Item = Result<Event, Self::Error>> + Send {
self.guard.redact_request(&mut request);
self.model.respond(request)
}
fn respond_with_tools(
&self,
mut request: LLMRequestWithTools<'_>,
) -> impl Stream<Item = Result<Event, Self::Error>> + Send {
self.guard.redact_request(request.request_mut());
self.model.respond_with_tools(request)
}
fn generate<T: JsonSchema + DeserializeOwned + 'static>(
&self,
mut request: LLMRequest,
) -> impl Future<Output = Result<T, GenerateError<Self::Error>>> + Send {
self.guard.redact_request(&mut request);
self.model.generate(request)
}
fn complete(&self, prefix: &str) -> impl Stream<Item = Result<Event, Self::Error>> + Send {
let prefix = self.guard.redact(prefix);
async_stream::stream! {
let mut events = std::pin::pin!(self.model.complete(&prefix));
while let Some(event) = events.next().await {
yield event;
}
}
}
fn summarize(&self, text: &str) -> impl Stream<Item = Result<Event, Self::Error>> + Send {
let text = self.guard.redact(text);
async_stream::stream! {
let mut events = std::pin::pin!(self.model.summarize(&text));
while let Some(event) = events.next().await {
yield event;
}
}
}
fn categorize<T: JsonSchema + DeserializeOwned + 'static>(
&self,
text: &str,
) -> impl Future<Output = Result<T, GenerateError<Self::Error>>> + Send {
let text = self.guard.redact(text);
async move { self.model.categorize(&text).await }
}
fn profile(&self) -> impl Future<Output = Result<Profile, Self::Error>> + Send {
self.model.profile()
}
fn retry_policy(&self) -> RetryPolicy {
self.model.retry_policy()
}
}