use crate::capture::ScreenView;
use anyhow::{Context, Result};
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RedactionRuleConfig {
pub name: String,
#[serde(default)]
pub pattern: String,
#[serde(default = "default_replacement")]
pub replacement: String,
#[serde(default = "default_true")]
pub enabled: bool,
#[serde(default)]
pub color: Option<String>,
#[serde(default)]
pub min_entropy: Option<f64>,
#[serde(default)]
pub keep_prefix: Option<usize>,
#[serde(default)]
pub keep_suffix: Option<usize>,
}
impl RedactionRuleConfig {
pub fn new(name: &str, pattern: &str, replacement: &str) -> Self {
Self {
name: name.to_string(),
pattern: pattern.to_string(),
replacement: replacement.to_string(),
enabled: true,
color: None,
min_entropy: None,
keep_prefix: None,
keep_suffix: None,
}
}
}
fn default_replacement() -> String {
"[REDACTED]".to_string()
}
fn default_true() -> bool {
true
}
pub const DEFAULT_BLOCK_COLOR: [u8; 3] = [212, 25, 25];
pub const DEFAULT_LABEL_COLOR: [u8; 3] = [0, 0, 0];
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct RedactionConfig {
pub enabled: bool,
pub auto: bool,
#[serde(default)]
pub color: Option<String>,
#[serde(default)]
pub label_color: Option<String>,
#[serde(default)]
pub rules_path: Option<String>,
pub rules: Vec<RedactionRuleConfig>,
}
impl Default for RedactionConfig {
fn default() -> Self {
Self {
enabled: true,
auto: false,
color: None,
label_color: None,
rules_path: None,
rules: Vec::new(),
}
}
}
pub fn resolve_should_redact(cfg: &RedactionConfig, redact: bool, no_redact: bool) -> bool {
if no_redact || !cfg.enabled {
return false;
}
if redact {
return true;
}
cfg.auto
}
pub fn explicit_request_is_blocked(cfg: &RedactionConfig, redact: bool, no_redact: bool) -> bool {
redact && !no_redact && !cfg.enabled
}
pub const REDACTION_DISABLED_MSG: &str = "redaction was requested but is disabled in config ([redaction] enabled = false); \
set enabled = true to use --redact / redact: true";
#[derive(Debug)]
struct CompiledRule {
name: String,
label: Option<String>,
regex: Regex,
has_redact_group: bool,
skip: Option<fn(&str) -> bool>,
min_entropy: Option<f64>,
block_color: [u8; 3],
label_color: [u8; 3],
keep_prefix: usize,
keep_suffix: usize,
}
impl CompiledRule {
fn match_spans(&self, line: &str) -> Vec<(usize, usize)> {
if self.has_redact_group {
self.regex
.captures_iter(line)
.filter_map(|caps| caps.name("redact").map(|m| (m.start(), m.end())))
.collect()
} else {
self.regex
.find_iter(line)
.map(|m| (m.start(), m.end()))
.collect()
}
}
}
#[derive(Debug)]
pub struct RedactionEngine {
rules: Vec<CompiledRule>,
}
impl RedactionEngine {
pub fn from_config(cfg: &RedactionConfig) -> Result<Self> {
Self::from_config_with_labels(cfg, true)
}
pub fn from_config_with_labels(cfg: &RedactionConfig, show_labels: bool) -> Result<Self> {
let mut order: Vec<String> = Vec::new();
let mut merged: HashMap<String, RedactionRuleConfig> = HashMap::new();
for rule in builtin_rules() {
order.push(rule.name.clone());
merged.insert(rule.name.clone(), rule);
}
for user in &cfg.rules {
if let Some(existing) = merged.get_mut(&user.name) {
if !user.pattern.is_empty() {
existing.pattern = user.pattern.clone();
}
existing.replacement = user.replacement.clone();
existing.enabled = user.enabled;
existing.min_entropy = user.min_entropy;
if user.color.is_some() {
existing.color = user.color.clone();
}
if user.keep_prefix.is_some() {
existing.keep_prefix = user.keep_prefix;
}
if user.keep_suffix.is_some() {
existing.keep_suffix = user.keep_suffix;
}
} else {
order.push(user.name.clone());
merged.insert(user.name.clone(), user.clone());
}
}
let default_block = cfg
.color
.as_deref()
.and_then(parse_hex_rgb)
.unwrap_or(DEFAULT_BLOCK_COLOR);
let default_label = cfg
.label_color
.as_deref()
.and_then(parse_hex_rgb)
.unwrap_or(DEFAULT_LABEL_COLOR);
let ordered: Vec<RedactionRuleConfig> = order
.into_iter()
.map(|name| merged[&name].clone())
.collect();
Self::compile(&ordered, default_block, default_label, show_labels)
}
pub fn from_rules(rules: &[RedactionRuleConfig]) -> Result<Self> {
Self::from_rules_with_labels(rules, true)
}
pub fn from_rules_with_labels(
rules: &[RedactionRuleConfig],
show_labels: bool,
) -> Result<Self> {
Self::compile(rules, DEFAULT_BLOCK_COLOR, DEFAULT_LABEL_COLOR, show_labels)
}
fn compile(
rules: &[RedactionRuleConfig],
default_block: [u8; 3],
default_label: [u8; 3],
show_labels: bool,
) -> Result<Self> {
let mut compiled = Vec::new();
for rc in rules {
if !rc.enabled || rc.pattern.is_empty() {
continue;
}
let regex = Regex::new(&rc.pattern)
.with_context(|| format!("invalid regex for redaction rule '{}'", rc.name))?;
let block_color = rc
.color
.as_deref()
.and_then(parse_hex_rgb)
.unwrap_or(default_block);
let label = if show_labels {
label_for(&rc.name, &rc.replacement)
} else {
None
};
compiled.push(CompiledRule {
name: rc.name.clone(),
label,
has_redact_group: regex.capture_names().flatten().any(|n| n == "redact"),
regex,
skip: builtin_skip(&rc.name),
min_entropy: rc.min_entropy,
block_color,
label_color: default_label,
keep_prefix: rc.keep_prefix.unwrap_or(0),
keep_suffix: rc.keep_suffix.unwrap_or(0),
});
}
Ok(Self { rules: compiled })
}
pub fn rule_count(&self) -> usize {
self.rules.len()
}
pub fn rule_names(&self) -> Vec<&str> {
self.rules.iter().map(|r| r.name.as_str()).collect()
}
pub fn validate_rule_names(&self, requested: &[String]) -> Result<()> {
let unknown: Vec<&str> = requested
.iter()
.filter(|name| !self.rules.iter().any(|r| &r.name == *name))
.map(|s| s.as_str())
.collect();
if !unknown.is_empty() {
let available = self.rule_names().into_iter().collect::<Vec<_>>().join(", ");
anyhow::bail!(
"unknown redaction rule(s): {}. Available rules: {}",
unknown.join(", "),
available
);
}
Ok(())
}
pub fn redact_screen<S: ScreenView + ?Sized>(
&self,
screen: &S,
only: Option<&[String]>,
) -> RedactionMap {
let mut map = RedactionMap::default();
self.redact_screen_into(screen, only, &mut map);
map
}
pub fn redact_screen_into<S: ScreenView + ?Sized>(
&self,
screen: &S,
only: Option<&[String]>,
map: &mut RedactionMap,
) {
let mut counts: HashMap<String, usize> = HashMap::new();
for (line, positions) in logical_lines(screen) {
for rule in &self.rules {
if let Some(names) = only
&& !names.iter().any(|n| n == &rule.name)
{
continue;
}
for (start, end) in rule.match_spans(&line) {
let matched = &line[start..end];
if let Some(skip) = rule.skip
&& skip(matched)
{
continue;
}
if let Some(threshold) = rule.min_entropy
&& shannon_entropy(matched) < threshold
{
continue;
}
let mut match_cells: Vec<(u16, u16)> = Vec::new();
for &pos in &positions[start..end] {
if match_cells.last() != Some(&pos) {
match_cells.push(pos);
}
}
if match_cells.is_empty() {
continue;
}
let start_idx = rule.keep_prefix.min(match_cells.len());
let end_idx = match_cells
.len()
.saturating_sub(rule.keep_suffix)
.max(start_idx);
let redact_cells = &match_cells[start_idx..end_idx];
if redact_cells.is_empty() {
continue;
}
map.apply_run(
redact_cells,
rule.label.as_deref(),
rule.block_color,
rule.label_color,
);
*counts.entry(rule.name.clone()).or_insert(0) += 1;
}
}
}
let mut combined: HashMap<String, usize> = map.counts.iter().cloned().collect();
for (name, count) in counts {
*combined.entry(name).or_insert(0) += count;
}
let mut audit: Vec<(String, usize)> = combined.into_iter().collect();
audit.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
map.counts = audit;
}
}
fn logical_lines<S: ScreenView + ?Sized>(screen: &S) -> Vec<(String, Vec<(u16, u16)>)> {
let (rows, cols) = screen.size();
let mut lines = Vec::new();
let mut row = 0u16;
while row < rows {
let mut line = String::new();
let mut positions: Vec<(u16, u16)> = Vec::new();
let mut last = row;
loop {
append_row(screen, last, cols, &mut line, &mut positions);
if last + 1 < rows && screen.row_wrapped(last) {
last += 1;
} else {
break;
}
}
lines.push((line, positions));
row = last + 1;
}
lines
}
fn append_row<S: ScreenView + ?Sized>(
screen: &S,
row: u16,
cols: u16,
line: &mut String,
positions: &mut Vec<(u16, u16)>,
) {
for col in 0..cols {
let contents = screen
.cell(row, col)
.map(|c| c.contents())
.unwrap_or_default();
if contents.is_empty() {
line.push(' ');
positions.push((row, col));
} else {
for ch in contents.chars() {
for _ in 0..ch.len_utf8() {
positions.push((row, col));
}
line.push(ch);
}
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct RedactedCell {
pub label_char: Option<char>,
pub block_color: [u8; 3],
pub label_color: [u8; 3],
}
#[derive(Debug, Default)]
pub struct RedactionMap {
cells: HashMap<(u16, u16), RedactedCell>,
pub counts: Vec<(String, usize)>,
}
impl RedactionMap {
fn apply_run(
&mut self,
cells: &[(u16, u16)],
label: Option<&str>,
block_color: [u8; 3],
label_color: [u8; 3],
) {
let tag_chars: Vec<char> = match label {
Some(l) if !l.is_empty() => format!("[{}]", l).chars().collect(),
_ => Vec::new(),
};
let use_label = !tag_chars.is_empty() && cells.len() >= tag_chars.len();
for (i, &(row, col)) in cells.iter().enumerate() {
let label_char = if use_label && i < tag_chars.len() {
Some(tag_chars[i])
} else {
None
};
self.cells.entry((row, col)).or_insert(RedactedCell {
label_char,
block_color,
label_color,
});
}
}
pub fn add_manual(&mut self, row: u16, col_start: u16, col_end: u16, label: Option<&str>) {
self.add_manual_with_color(
row,
col_start,
col_end,
label,
DEFAULT_BLOCK_COLOR,
DEFAULT_LABEL_COLOR,
);
}
pub fn add_manual_with_color(
&mut self,
row: u16,
col_start: u16,
col_end: u16,
label: Option<&str>,
block_color: [u8; 3],
label_color: [u8; 3],
) {
if col_end <= col_start {
return;
}
let cells: Vec<(u16, u16)> = (col_start..col_end).map(|col| (row, col)).collect();
self.apply_run(&cells, label, block_color, label_color);
let name = match label {
Some(l) if !l.is_empty() => format!("manual:{}", l),
_ => "manual".to_string(),
};
if let Some(entry) = self.counts.iter_mut().find(|(n, _)| n == &name) {
entry.1 += 1;
} else {
self.counts.push((name, 1));
}
}
pub fn get(&self, row: u16, col: u16) -> Option<&RedactedCell> {
self.cells.get(&(row, col))
}
pub fn is_empty(&self) -> bool {
self.cells.is_empty()
}
pub fn cell_count(&self) -> usize {
self.cells.len()
}
pub fn audit_summary(&self) -> String {
self.counts
.iter()
.map(|(name, count)| format!("{}x {}", count, name))
.collect::<Vec<_>>()
.join(", ")
}
pub fn redacted_plain_text<S: ScreenView + ?Sized>(&self, screen: &S) -> String {
let (rows, cols) = screen.size();
let mut out_rows: Vec<String> = Vec::with_capacity(rows as usize);
for row in 0..rows {
let mut line = String::new();
for col in 0..cols {
if self.cells.contains_key(&(row, col)) {
line.push('\u{2588}'); } else {
let contents = screen
.cell(row, col)
.map(|c| c.contents())
.unwrap_or_default();
if contents.is_empty() {
line.push(' ');
} else {
line.push_str(contents);
}
}
}
out_rows.push(line.trim_end().to_string());
}
out_rows.join("\n").trim_end().to_string()
}
}
#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum ManualRedactionSpec {
Pattern {
pattern: String,
#[serde(default, alias = "label")]
replacement: Option<String>,
#[serde(default)]
keep_prefix: Option<usize>,
#[serde(default)]
keep_suffix: Option<usize>,
#[serde(default)]
color: Option<String>,
},
Coordinate {
row: u16,
col_start: u16,
col_end: u16,
#[serde(default)]
label: Option<String>,
#[serde(default)]
color: Option<String>,
},
}
const PATTERN_FIELDS: &[&str] = &[
"pattern",
"replacement",
"label",
"keep_prefix",
"keep_suffix",
"color",
];
const PATTERN_REQUIRED: &[&str] = &["pattern"];
const COORDINATE_FIELDS: &[&str] = &["row", "col_start", "col_end", "label", "color"];
const COORDINATE_REQUIRED: &[&str] = &["row", "col_start", "col_end"];
const PATTERN_DESCRIPTION: &str = "Redact every match of a regex pattern. \
`replacement` (also accepted as `label`) is drawn as the on-image \
`[LABEL]` tag; omitting it draws a plain block with no text overlay.";
const COORDINATE_DESCRIPTION: &str = "Redact an explicit cell range on a single row (0-based). Coordinates are \
validated against the screenshot before anything is drawn: a row at or past \
its last row, a column at or past its last column, or a `col_start` that is \
not before `col_end` is rejected with an error naming the screenshot's real \
dimensions.";
fn field_schema(name: &str, is_pattern: bool) -> serde_json::Value {
match name {
"pattern" => serde_json::json!({
"type": "string",
"description": "Regex pattern to match against the terminal text.",
}),
"replacement" => serde_json::json!({
"type": ["string", "null"],
"default": null,
"description": "Replacement marker used to derive the on-image `[LABEL]` tag. \
May also be given as `label`. Omitted (or empty) draws a plain \
block with no text overlay.",
}),
"label" if is_pattern => serde_json::json!({
"type": ["string", "null"],
"default": null,
"description": "Alias for `replacement`: the on-image `[LABEL]` tag for this \
pattern. Pass one or the other, not both.",
}),
"label" => serde_json::json!({
"type": ["string", "null"],
"default": null,
"description": "Short label drawn over the redaction block. Defaults to \
`REDACTED`.",
}),
"keep_prefix" => serde_json::json!({
"type": ["integer", "null"],
"format": "uint",
"minimum": 0,
"default": null,
"description": "Number of leading matched characters to leave unmasked. When \
set, only the characters after the prefix are blocked out (e.g. \
show `AKIA****` for an AWS key). Defaults to 0 (mask everything).",
}),
"keep_suffix" => serde_json::json!({
"type": ["integer", "null"],
"format": "uint",
"minimum": 0,
"default": null,
"description": "Number of trailing matched characters to leave unmasked. When \
set, only the characters before the suffix are blocked out. \
Defaults to 0 (mask everything).",
}),
"color" => serde_json::json!({
"type": ["string", "null"],
"default": null,
"description": "Block color for this redaction as `#RRGGBB`. Defaults to the \
built-in red.",
}),
"row" => serde_json::json!({
"type": "integer",
"format": "uint16",
"minimum": 0,
"maximum": 65535,
"description": "Row index (0-based), counted in the screenshot as rendered: row \
0 is the topmost line the PNG shows, which for a `head_lines` / \
`tail_lines` capture is the first line of that selection. Must \
be less than the number of rows the screenshot shows.",
}),
"col_start" => serde_json::json!({
"type": "integer",
"format": "uint16",
"minimum": 0,
"maximum": 65535,
"description": "First column to redact (0-based, inclusive). Must be less than \
both `col_end` and the screenshot's width.",
}),
"col_end" => serde_json::json!({
"type": "integer",
"format": "uint16",
"minimum": 0,
"maximum": 65535,
"description": "One past the last column to redact (exclusive). Must be greater \
than `col_start` and no greater than the screenshot's width.",
}),
other => unreachable!("no schema for redaction field {:?}", other),
}
}
fn variant_schema(
description: &str,
fields: &[&str],
required: &[&str],
is_pattern: bool,
) -> serde_json::Value {
let properties: serde_json::Map<String, serde_json::Value> = fields
.iter()
.map(|field| ((*field).to_string(), field_schema(field, is_pattern)))
.collect();
serde_json::json!({
"type": "object",
"description": description,
"properties": properties,
"required": required,
"additionalProperties": false,
})
}
pub fn manual_redaction_schema() -> serde_json::Value {
serde_json::json!({
"description": "A single manual redaction: either a regex pattern (redacts every \
match) or an explicit cell range. The CLI's repeatable \
`--redaction '<JSON>'` option takes exactly the same specification.",
"oneOf": [
variant_schema(PATTERN_DESCRIPTION, PATTERN_FIELDS, PATTERN_REQUIRED, true),
variant_schema(
COORDINATE_DESCRIPTION,
COORDINATE_FIELDS,
COORDINATE_REQUIRED,
false,
),
],
})
}
impl schemars::JsonSchema for ManualRedactionSpec {
fn schema_name() -> std::borrow::Cow<'static, str> {
"ManualRedactionSpec".into()
}
fn schema_id() -> std::borrow::Cow<'static, str> {
concat!(module_path!(), "::ManualRedactionSpec").into()
}
fn json_schema(_generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
schemars::Schema::try_from(manual_redaction_schema())
.expect("the manual redaction schema is a JSON object")
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct PatternFields {
pattern: String,
#[serde(default, alias = "label")]
replacement: Option<String>,
#[serde(default)]
keep_prefix: Option<usize>,
#[serde(default)]
keep_suffix: Option<usize>,
#[serde(default)]
color: Option<String>,
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct CoordinateFields {
row: u16,
col_start: u16,
col_end: u16,
#[serde(default)]
label: Option<String>,
#[serde(default)]
color: Option<String>,
}
impl ManualRedactionSpec {
pub fn from_json(input: &str) -> Result<Self> {
let value: serde_json::Value = serde_json::from_str(input).map_err(|e| {
anyhow::anyhow!(
"invalid redaction JSON: {}. Expected an object such as \
'{{\"pattern\":\"[a-f0-9]{{32}}\",\"replacement\":\"HASH\",\"keep_prefix\":4}}' \
or '{{\"row\":3,\"col_start\":12,\"col_end\":44,\"label\":\"SECRET\"}}', got: {}",
e,
input
)
})?;
Self::from_value(value).with_context(|| format!("in redaction: {}", input))
}
pub fn from_value(value: serde_json::Value) -> Result<Self> {
let object = value.as_object().ok_or_else(|| {
anyhow::anyhow!(
"a redaction must be a JSON object with either a \"pattern\" or \
\"row\"/\"col_start\"/\"col_end\""
)
})?;
let is_pattern = object.contains_key("pattern");
let allowed = if is_pattern {
PATTERN_FIELDS
} else if COORDINATE_REQUIRED
.iter()
.any(|field| object.contains_key(*field))
{
for field in COORDINATE_REQUIRED {
if !object.contains_key(*field) {
anyhow::bail!(
"a coordinate redaction needs \"row\", \"col_start\" and \"col_end\"; \
\"{}\" is missing",
field
);
}
}
COORDINATE_FIELDS
} else {
anyhow::bail!(
"a redaction needs either a \"pattern\" or \"row\"/\"col_start\"/\"col_end\""
);
};
let unknown: Vec<&str> = object
.keys()
.map(String::as_str)
.filter(|key| !allowed.contains(key))
.collect();
if !unknown.is_empty() {
anyhow::bail!(
"unknown field(s) {} in redaction; this redaction accepts: {}",
unknown.join(", "),
allowed.join(", ")
);
}
if is_pattern {
let fields: PatternFields =
serde_json::from_value(value).context("invalid pattern redaction")?;
Ok(Self::Pattern {
pattern: fields.pattern,
replacement: fields.replacement,
keep_prefix: fields.keep_prefix,
keep_suffix: fields.keep_suffix,
color: fields.color,
})
} else {
let fields: CoordinateFields =
serde_json::from_value(value).context("invalid coordinate redaction")?;
Ok(Self::Coordinate {
row: fields.row,
col_start: fields.col_start,
col_end: fields.col_end,
label: fields.label,
color: fields.color,
})
}
}
pub fn parse_all<S: AsRef<str>>(inputs: &[S]) -> Result<Vec<Self>> {
inputs
.iter()
.enumerate()
.map(|(i, input)| {
Self::from_json(input.as_ref()).with_context(|| format!("--redaction #{}", i + 1))
})
.collect()
}
}
impl<'de> Deserialize<'de> for ManualRedactionSpec {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = serde_json::Value::deserialize(deserializer)?;
Self::from_value(value).map_err(serde::de::Error::custom)
}
}
#[derive(Debug, Clone)]
pub struct CoordinateRedaction {
pub row: u16,
pub col_start: u16,
pub col_end: u16,
pub label: Option<String>,
pub block_color: [u8; 3],
}
#[derive(Debug)]
pub struct ManualRedactions {
engine: Option<RedactionEngine>,
coordinates: Vec<CoordinateRedaction>,
show_labels: bool,
}
impl ManualRedactions {
pub fn new(specs: &[ManualRedactionSpec], show_labels: bool) -> Result<Self> {
let mut pattern_rules: Vec<RedactionRuleConfig> = Vec::new();
let mut coordinates: Vec<CoordinateRedaction> = Vec::new();
for (i, spec) in specs.iter().enumerate() {
match spec {
ManualRedactionSpec::Pattern {
pattern,
replacement,
keep_prefix,
keep_suffix,
color,
} => {
if pattern.is_empty() {
anyhow::bail!("redaction #{}: \"pattern\" must not be empty", i + 1);
}
Regex::new(pattern).with_context(|| {
format!("redaction #{}: invalid regex {:?}", i + 1, pattern)
})?;
let replacement = replacement.clone().unwrap_or_default();
let mut rule =
RedactionRuleConfig::new(&format!("custom_{}", i), pattern, &replacement);
rule.keep_prefix = *keep_prefix;
rule.keep_suffix = *keep_suffix;
if let Some(color) = color {
parse_color(color, i)?;
rule.color = Some(color.clone());
}
pattern_rules.push(rule);
}
ManualRedactionSpec::Coordinate {
row,
col_start,
col_end,
label,
color,
} => {
let block_color = match color {
Some(color) => parse_color(color, i)?,
None => DEFAULT_BLOCK_COLOR,
};
coordinates.push(CoordinateRedaction {
row: *row,
col_start: *col_start,
col_end: *col_end,
label: label.clone(),
block_color,
});
}
}
}
let engine = if pattern_rules.is_empty() {
None
} else {
Some(RedactionEngine::from_rules_with_labels(
&pattern_rules,
show_labels,
)?)
};
Ok(Self {
engine,
coordinates,
show_labels,
})
}
pub fn is_empty(&self) -> bool {
self.engine.is_none() && self.coordinates.is_empty()
}
pub fn coordinates(&self) -> &[CoordinateRedaction] {
&self.coordinates
}
pub fn validate_bounds(&self, rendered_rows: u16, rendered_cols: u16) -> Result<()> {
for coordinate in &self.coordinates {
let CoordinateRedaction {
row,
col_start,
col_end,
..
} = *coordinate;
let invalid = |detail: String| -> Result<()> {
anyhow::bail!(
"invalid redaction coordinate (row {}, col_start {}, col_end {}): {}. \
The screenshot renders {} row(s) x {} column(s); rows are 0..{} and \
columns are 0..{} (col_end is exclusive). Cells outside that are \
retained by the capture but never drawn, so redacting them would \
change nothing.",
row,
col_start,
col_end,
detail,
rendered_rows,
rendered_cols,
rendered_rows,
rendered_cols
)
};
if row >= rendered_rows {
return invalid(format!("row {} is past the last rendered row", row));
}
if col_start >= col_end {
return invalid(format!(
"col_start {} is not before col_end {}, so the range is empty",
col_start, col_end
));
}
if col_start >= rendered_cols {
return invalid(format!(
"col_start {} is past the last rendered column",
col_start
));
}
if col_end > rendered_cols {
return invalid(format!(
"col_end {} is past the last rendered column boundary",
col_end
));
}
}
Ok(())
}
pub fn apply_to<S: ScreenView + ?Sized>(&self, screen: &S, map: &mut RedactionMap) {
if let Some(engine) = &self.engine {
engine.redact_screen_into(screen, None, map);
}
for coordinate in &self.coordinates {
let label = if self.show_labels {
coordinate.label.as_deref().or(Some("REDACTED"))
} else {
None
};
map.add_manual_with_color(
coordinate.row,
coordinate.col_start,
coordinate.col_end,
label,
coordinate.block_color,
DEFAULT_LABEL_COLOR,
);
}
}
}
fn parse_color(color: &str, index: usize) -> Result<[u8; 3]> {
parse_hex_rgb(color).ok_or_else(|| {
anyhow::anyhow!(
"redaction #{}: invalid color {:?}; expected a hex color such as \"#d41919\"",
index + 1,
color
)
})
}
fn label_for(name: &str, replacement: &str) -> Option<String> {
if replacement.is_empty() {
return None;
}
let builtin = match name {
"ipv4" | "ipv6" => Some("IP"),
"mac" => Some("MAC"),
"aws_key" | "aws_secret" | "api_key" => Some("KEY"),
"private_key" => Some("KEY"),
"jwt" => Some("JWT"),
"email" => Some("EMAIL"),
"hostname" => Some("HOST"),
_ => None,
};
if let Some(l) = builtin {
return Some(l.to_string());
}
let cleaned: String = replacement
.trim_matches(|c| c == '[' || c == ']')
.replace("REDACTED", "")
.trim_matches(|c: char| c == '-' || c == '_' || c.is_whitespace())
.to_uppercase();
let base = if cleaned.is_empty() {
name.to_uppercase()
} else {
cleaned
};
Some(base.chars().take(6).collect())
}
fn builtin_skip(name: &str) -> Option<fn(&str) -> bool> {
match name {
"ipv4" => Some(is_ignorable_ipv4),
"ipv6" => Some(is_ignorable_ipv6),
_ => None,
}
}
fn is_ignorable_ipv4(s: &str) -> bool {
let octets: Vec<&str> = s.split('.').collect();
if octets.len() != 4 {
return false;
}
match s {
"0.0.0.0" | "255.255.255.255" => return true,
_ => {}
}
octets[0] == "127"
}
fn is_ignorable_ipv6(s: &str) -> bool {
let addr: std::net::Ipv6Addr = match s.parse() {
Ok(a) => a,
Err(_) => return true,
};
if addr.is_loopback() || addr.is_unspecified() {
return true;
}
let hex_digits = s.chars().filter(|c| c.is_ascii_hexdigit()).count();
let groups: Vec<&str> = s.split(':').filter(|g| !g.is_empty()).collect();
let longest = groups.iter().map(|g| g.len()).max().unwrap_or(0);
hex_digits < 4 || (longest < 3 && groups.len() < 4)
}
fn builtin_rules() -> Vec<RedactionRuleConfig> {
let r = |name: &str, pattern: &str, replacement: &str| {
RedactionRuleConfig::new(name, pattern, replacement)
};
vec![
r(
"ipv4",
r"(?:^|[^0-9A-Za-z.])(?P<redact>(?:(?:25[0-5]|2[0-4]\d|1?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|1?\d\d?))(?:$|[^0-9A-Za-z.\-]|-(?:[^A-Za-z]|$))",
"[REDACTED-IP]",
),
r(
"ipv6",
r"(?i)(?:^|[^0-9A-Za-z:.])(?P<redact>::ffff:(?:\d{1,3}\.){3}\d{1,3}|(?:[0-9a-f]{1,4}:){2,7}[0-9a-f]{1,4}|(?:[0-9a-f]{1,4}:){1,7}:(?:[0-9a-f]{1,4}(?::[0-9a-f]{1,4}){0,6})?|::(?:[0-9a-f]{1,4}(?::[0-9a-f]{1,4}){0,7})?)(?:$|[^0-9A-Za-z:])",
"[REDACTED-IPv6]",
),
r(
"mac",
r"\b(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}\b",
"[REDACTED-MAC]",
),
r(
"aws_key",
r"\b(?:AKIA|ASIA|AGPA|AIDA|AROA|ANPA)[0-9A-Z]{16}\b",
"[REDACTED-KEY]",
),
r(
"aws_secret",
r#"(?i)(?:aws_secret_access_key|aws_secret)\s*[:=]\s*['"]?(?P<redact>[A-Za-z0-9/+=]{40})['"]?"#,
"[REDACTED-KEY]",
),
r(
"private_key",
r"-----(?:BEGIN|END)[A-Z0-9 ]*PRIVATE KEY-----",
"[REDACTED-KEY]",
),
r(
"jwt",
r"\beyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\b",
"[REDACTED-JWT]",
),
r(
"email",
r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b",
"[REDACTED-EMAIL]",
),
r(
"hostname",
r"\b(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+(?:internal|local|corp|lan|intranet)\b",
"[REDACTED-HOST]",
),
r(
"api_key",
r#"(?i)(?:api[_-]?key|apikey|access[_-]?token|secret[_-]?key|auth[_-]?token|bearer)\s*[:=]\s*['"]?(?P<redact>[A-Za-z0-9_\-\.]{16,})"#,
"[REDACTED-KEY]",
),
r(
"github_token",
r"\b(?:gh[opsu]_[A-Za-z0-9]{36}|github_pat_[A-Za-z0-9_]{82})\b",
"[REDACTED-TOKEN]",
),
r(
"slack_token",
r"\bxox[baprs]-[0-9]{10,13}-[0-9]{10,13}-[A-Za-z0-9]{24,34}\b",
"[REDACTED-TOKEN]",
),
r(
"private_key_pem",
r"-----BEGIN (?:RSA |EC |DSA |OPENSSH )?PRIVATE KEY-----",
"[REDACTED-KEY]",
),
r(
"gcp_service_account",
r#"(?i)"type"\s*:\s*"service_account""#,
"[REDACTED-KEY]",
),
r(
"azure_client_secret",
r"\b[A-Za-z0-9_~.]{3}[0-9]Q~[A-Za-z0-9_~.\-]{31,34}\b",
"[REDACTED-SECRET]",
),
r(
"generic_api_key",
r#"(?i)(?:api[_-]?key|apikey)\s*[:=]\s*['"]?(?P<redact>[A-Za-z0-9_\-]{20,})"#,
"[REDACTED-KEY]",
),
r(
"bearer_token",
r#"(?i)(?:bearer|authorization)\s*[:=]\s*['"]?(?P<redact>[A-Za-z0-9_\-.]{20,})"#,
"[REDACTED-TOKEN]",
),
r(
"connection_string",
r#"(?i)(?:connection.?string|conn.?str)\s*[:=]\s*['"]?(?P<redact>[^'";\s]{20,})"#,
"[REDACTED-SECRET]",
),
r(
"discord_token",
r"\b[MN][A-Za-z0-9]{23,}\.[A-Za-z0-9_-]{6}\.[A-Za-z0-9_-]{27,}",
"[REDACTED-TOKEN]",
),
r(
"hashicorp_vault_token",
r"\bhvs\.[A-Za-z0-9]{24,}",
"[REDACTED-TOKEN]",
),
]
}
pub fn parse_hex_rgb(hex: &str) -> Option<[u8; 3]> {
let hex = hex.trim().trim_start_matches('#');
if hex.len() != 6 || !hex.bytes().all(|b| b.is_ascii_hexdigit()) {
return None;
}
let r = u8::from_str_radix(&hex[0..2], 16).ok()?;
let g = u8::from_str_radix(&hex[2..4], 16).ok()?;
let b = u8::from_str_radix(&hex[4..6], 16).ok()?;
Some([r, g, b])
}
fn shannon_entropy(s: &str) -> f64 {
if s.is_empty() {
return 0.0;
}
let mut counts: HashMap<char, usize> = HashMap::new();
let mut total = 0usize;
for ch in s.chars() {
*counts.entry(ch).or_insert(0) += 1;
total += 1;
}
let total = total as f64;
-counts
.values()
.map(|&c| {
let p = c as f64 / total;
p * p.log2()
})
.sum::<f64>()
}
#[derive(Debug, Clone, Deserialize)]
struct YamlRule {
#[serde(default)]
name: Option<String>,
#[serde(default)]
id: Option<String>,
#[serde(default)]
pattern: Option<String>,
#[serde(default)]
regex: Option<String>,
#[serde(default)]
min_entropy: Option<f64>,
#[serde(default)]
confidence: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
struct YamlRulesFile {
#[serde(default)]
rules: Vec<YamlRule>,
}
#[derive(Debug, Clone, Deserialize)]
struct TomlRulesFile {
#[serde(default)]
rules: Vec<RedactionRuleConfig>,
}
impl YamlRule {
fn into_rule(self) -> Option<RedactionRuleConfig> {
let name = self
.name
.clone()
.or_else(|| self.id.clone())
.filter(|n| !n.is_empty())?;
let pattern = self.pattern.or(self.regex).filter(|p| !p.is_empty())?;
let enabled = !matches!(
self.confidence
.as_deref()
.map(str::to_ascii_lowercase)
.as_deref(),
Some("false") | Some("disabled") | Some("off")
);
Some(RedactionRuleConfig {
name,
pattern,
replacement: default_replacement(),
enabled,
color: None,
min_entropy: self.min_entropy,
keep_prefix: None,
keep_suffix: None,
})
}
}
pub fn load_rules_from_dir(dir: &std::path::Path) -> Vec<RedactionRuleConfig> {
let mut out = Vec::new();
let entries = match std::fs::read_dir(dir) {
Ok(e) => e,
Err(e) => {
tracing::warn!("Failed to read rules dir {:?}: {}", dir, e);
return out;
}
};
let mut paths: Vec<std::path::PathBuf> =
entries.filter_map(|e| e.ok().map(|e| e.path())).collect();
paths.sort();
for path in paths {
let ext = path
.extension()
.and_then(|e| e.to_str())
.map(str::to_ascii_lowercase);
let contents = match std::fs::read_to_string(&path) {
Ok(c) => c,
Err(e) => {
tracing::warn!("Failed to read rules file {:?}: {}", path, e);
continue;
}
};
match ext.as_deref() {
Some("toml") => match toml::from_str::<TomlRulesFile>(&contents) {
Ok(f) => out.extend(f.rules),
Err(e) => tracing::warn!("Failed to parse TOML rules {:?}: {}", path, e),
},
Some("yaml") | Some("yml") => {
match serde_yaml_ng::from_str::<YamlRulesFile>(&contents) {
Ok(f) => out.extend(f.rules.into_iter().filter_map(YamlRule::into_rule)),
Err(e) => tracing::warn!("Failed to parse YAML rules {:?}: {}", path, e),
}
}
_ => {}
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
use crate::capture::{CapturedScreen, DEFAULT_MAX_SCROLLBACK_LINES};
use std::collections::BTreeSet;
fn engine() -> RedactionEngine {
RedactionEngine::from_config(&RedactionConfig::default()).unwrap()
}
fn screen_of(text: &str, cols: u16, rows: u16) -> CapturedScreen {
CapturedScreen::parse(text.as_bytes(), rows, cols, DEFAULT_MAX_SCROLLBACK_LINES)
}
fn redact(text: &str) -> RedactionMap {
let parser = screen_of(text, 120, 10);
engine().redact_screen(&parser, None)
}
fn count_of(map: &RedactionMap, name: &str) -> usize {
map.counts
.iter()
.find(|(n, _)| n == name)
.map(|(_, c)| *c)
.unwrap_or(0)
}
#[test]
fn ipv4_matches_real_addresses() {
let map = redact("host is 192.168.1.42 online");
assert_eq!(count_of(&map, "ipv4"), 1);
}
#[test]
fn ipv4_ignores_loopback_and_unspecified() {
assert_eq!(count_of(&redact("localhost 127.0.0.1"), "ipv4"), 0);
assert_eq!(count_of(&redact("bind 0.0.0.0:8080"), "ipv4"), 0);
assert_eq!(count_of(&redact("bcast 255.255.255.255"), "ipv4"), 0);
}
#[test]
fn ipv4_rejects_out_of_range_octets() {
assert_eq!(count_of(&redact("version 999.999.1.1 here"), "ipv4"), 0);
}
#[test]
fn ipv6_matches() {
let map = redact("addr 2001:0db8:85a3:0000:0000:8a2e:0370:7334 up");
assert!(count_of(&map, "ipv6") >= 1);
}
#[test]
fn mac_matches_both_separators() {
assert_eq!(count_of(&redact("mac 00:1a:2b:3c:4d:5e"), "mac"), 1);
assert_eq!(count_of(&redact("mac 00-1A-2B-3C-4D-5E"), "mac"), 1);
}
#[test]
fn aws_key_matches() {
let map = redact("key AKIAIOSFODNN7EXAMPLE end");
assert_eq!(count_of(&map, "aws_key"), 1);
}
#[test]
fn aws_secret_matches_in_context() {
let input = "AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY";
let parser = screen_of(input, 120, 3);
let map = engine().redact_screen(&parser, None);
assert_eq!(count_of(&map, "aws_secret"), 1);
let text = map.redacted_plain_text(&parser);
assert!(text.contains("AWS_SECRET_ACCESS_KEY="));
assert!(!text.contains("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"));
}
#[test]
fn contextual_rules_preserve_key_names() {
let input = concat!(
"API_KEY=abcdefghijklmnopqrstuvwx\n",
"Authorization=abcdefghijklmnopqrstuvwxyz012345\n",
"CONNECTION_STRING=postgresql://user:secret@db.example/app"
);
let parser = screen_of(input, 120, 5);
let map = engine().redact_screen(&parser, None);
let text = map.redacted_plain_text(&parser);
assert!(text.contains("API_KEY="));
assert!(text.contains("Authorization="));
assert!(text.contains("CONNECTION_STRING="));
assert!(!text.contains("abcdefghijklmnopqrstuvwx"));
assert!(!text.contains("abcdefghijklmnopqrstuvwxyz012345"));
assert!(!text.contains("postgresql://user:secret@db.example/app"));
}
#[test]
fn jwt_matches() {
let jwt = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.dozjgNryP4J3jVmNHl0w5N";
let map = redact(&format!("token {}", jwt));
assert_eq!(count_of(&map, "jwt"), 1);
}
#[test]
fn private_key_marker_matches() {
let map = redact("-----BEGIN RSA PRIVATE KEY-----");
assert_eq!(count_of(&map, "private_key"), 1);
}
#[test]
fn email_matches() {
let map = redact("contact admin@example.com now");
assert_eq!(count_of(&map, "email"), 1);
}
#[test]
fn hostname_matches_internal_domains() {
assert_eq!(count_of(&redact("ssh db01.internal"), "hostname"), 1);
assert_eq!(count_of(&redact("ping fileserver.corp"), "hostname"), 1);
}
#[test]
fn api_key_matches() {
let map = redact("api_key=abcdef0123456789ABCDEF");
assert_eq!(count_of(&map, "api_key"), 1);
}
#[test]
fn plain_prose_is_not_redacted() {
let map = redact("the quick brown fox jumps over the lazy dog");
assert!(map.is_empty(), "unexpected redactions: {:?}", map.counts);
}
#[test]
fn ordinary_developer_output_is_not_redacted() {
let corpus = [
"use std::fs::read;",
"let path = std::path::PathBuf::from(\"/tmp/x\");",
"std::cout << x << std::endl;",
"Error::new(ErrorKind::Other, \"boom\")",
"impl fmt::Display for Config {",
"Foo::bar::baz(&mut self)",
"termshot v1.2.3.4-beta released",
"running 66 tests in 0.41s",
"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
"-rw-rw-r-- 1 adam adam 56917 Aug 25 13:07 Cargo.lock",
"commit 25eabf1a9c4f1e0b7d5c3a2f8e6d4b0c9a7f5e31",
"aGVsbG8gd29ybGQgdGhpcyBpcyBhIGxvbmcgYmFzZTY0IHN0cmluZyBmb3IgdGVzdGluZw==",
"12:34:56 INFO server started",
"Compiling termshot v0.1.0 (/home/adam/Desktop/termshot)",
"rotate the api key regularly and store it somewhere safe",
];
for line in corpus {
let map = redact(line);
assert!(
map.is_empty(),
"false positive on {:?}: {:?}",
line,
map.counts
);
}
}
#[test]
fn ipv6_addresses_are_still_redacted() {
for addr in [
"inet6 fe80::1cb2:ff:fe00:1/64",
"connecting to [2001:db8::1]:443",
"addr 2001:0db8:85a3:0000:0000:8a2e:0370:7334 up",
"peer fd00:1234:5678::abcd",
] {
let map = redact(addr);
assert!(count_of(&map, "ipv6") >= 1, "missed IPv6 in {:?}", addr);
}
}
#[test]
fn ipv6_ignores_loopback_and_unspecified() {
assert_eq!(count_of(&redact("listening on ::1 port 80"), "ipv6"), 0);
assert_eq!(count_of(&redact("bind :: port 80"), "ipv6"), 0);
}
#[test]
fn wrapped_secret_is_redacted_across_rows() {
let parser = screen_of("token: AKIAIOSFODNN7EXAMPLE done", 24, 6);
let map = engine().redact_screen(&parser, None);
assert_eq!(count_of(&map, "aws_key"), 1, "wrapped key missed");
let rows: Vec<u16> = (0..6)
.filter(|&row| (0..24).any(|col| map.get(row, col).is_some()))
.collect();
assert!(
rows.len() >= 2,
"expected redaction on both wrapped rows, got {:?}",
rows
);
assert!(!map.redacted_plain_text(&parser).contains("AKIA"));
}
#[test]
fn hard_newlines_are_not_joined_into_a_match() {
let rule = RedactionRuleConfig::new("hash", "[a-f0-9]{32}", "HASH");
let engine = RedactionEngine::from_rules(&[rule]).unwrap();
let parser = screen_of("8846f7eaee8fb117ad06\r\nbdd830b7586c\r\n", 60, 6);
let map = engine.redact_screen(&parser, None);
assert!(
map.is_empty(),
"hard-separated lines were joined into a match: {}",
map.audit_summary()
);
}
#[test]
fn soft_wrapped_halves_are_joined_into_a_match() {
let rule = RedactionRuleConfig::new("hash", "[a-f0-9]{32}", "HASH");
let engine = RedactionEngine::from_rules(&[rule]).unwrap();
let parser = screen_of("8846f7eaee8fb117ad06bdd830b7586c\r\n", 20, 6);
let map = engine.redact_screen(&parser, None);
assert_eq!(count_of(&map, "hash"), 1, "wrapped hash missed");
assert!(map.get(0, 19).is_some(), "first row not masked");
assert!(map.get(1, 0).is_some(), "second row not masked");
}
#[test]
fn only_the_redact_group_is_masked() {
let parser = screen_of("ip 10.0.0.9!", 120, 3);
let map = engine().redact_screen(&parser, None);
assert!(map.get(0, 2).is_none(), "context space was masked");
assert!(map.get(0, 3).is_some(), "address start not masked");
assert!(map.get(0, 10).is_some(), "address end not masked");
assert!(map.get(0, 11).is_none(), "trailing context was masked");
}
#[test]
fn parse_hex_rgb_rejects_non_ascii() {
assert_eq!(parse_hex_rgb("#abc\u{20ac}"), None);
assert_eq!(parse_hex_rgb("#d41919"), Some([212, 25, 25]));
}
#[test]
fn map_marks_correct_columns() {
let parser = screen_of("ip 10.0.0.9", 120, 3);
let map = engine().redact_screen(&parser, None);
assert!(map.get(0, 2).is_none()); for col in 3..11 {
assert!(
map.get(0, col).is_some(),
"column {} should be redacted",
col
);
}
}
#[test]
fn keep_prefix_and_suffix_leave_edges_unmasked() {
let mut rule = RedactionRuleConfig::new("hash", "[a-f0-9]{32}", "HASH");
rule.keep_prefix = Some(4);
let engine = RedactionEngine::from_rules(&[rule]).unwrap();
let hash = "8846f7eaee8fb117ad06bdd830b7586c";
let parser = screen_of(&format!("h: {}", hash), 120, 3);
let map = engine.redact_screen(&parser, None);
for col in 3..7 {
assert!(map.get(0, col).is_none(), "prefix col {} redacted", col);
}
for col in 7..35 {
assert!(map.get(0, col).is_some(), "col {} not redacted", col);
}
}
#[test]
fn keep_prefix_covering_whole_match_redacts_nothing() {
let mut rule = RedactionRuleConfig::new("word", "[a-z]{4}", "W");
rule.keep_prefix = Some(4);
let engine = RedactionEngine::from_rules(&[rule]).unwrap();
let parser = screen_of("abcd", 120, 3);
let map = engine.redact_screen(&parser, None);
assert!(map.is_empty(), "nothing should be redacted");
}
#[test]
fn keep_suffix_leaves_trailing_chars_unmasked() {
let mut rule = RedactionRuleConfig::new("digits", "[0-9]{6}", "N");
rule.keep_suffix = Some(2);
let engine = RedactionEngine::from_rules(&[rule]).unwrap();
let parser = screen_of("123456", 120, 3);
let map = engine.redact_screen(&parser, None);
for col in 0..4 {
assert!(map.get(0, col).is_some(), "col {} not redacted", col);
}
for col in 4..6 {
assert!(map.get(0, col).is_none(), "suffix col {} redacted", col);
}
}
#[test]
fn label_is_placed_over_block() {
let parser = screen_of("ip 10.11.12.13 up", 120, 3);
let map = engine().redact_screen(&parser, None);
let tag: String = (3..7)
.filter_map(|c| map.get(0, c).and_then(|rc| rc.label_char))
.collect();
assert_eq!(tag, "[IP]");
}
#[test]
fn only_filter_limits_rules() {
let parser = screen_of("192.168.0.1 admin@example.com", 120, 3);
let only = vec!["ipv4".to_string()];
let map = engine().redact_screen(&parser, Some(&only));
assert_eq!(count_of(&map, "ipv4"), 1);
assert_eq!(count_of(&map, "email"), 0);
}
#[test]
fn validate_rule_names_rejects_unknown() {
let engine = engine();
assert!(engine.validate_rule_names(&["ipv4".to_string()]).is_ok());
let err = engine
.validate_rule_names(&["ipv4".to_string(), "not_a_rule".to_string()])
.unwrap_err()
.to_string();
assert!(err.contains("not_a_rule"), "error was: {}", err);
}
#[test]
fn redacted_plain_text_hides_value() {
let parser = screen_of("secret 203.0.113.7 host", 120, 3);
let map = engine().redact_screen(&parser, None);
let text = map.redacted_plain_text(&parser);
assert!(!text.contains("203.0.113.7"), "text still leaks: {}", text);
assert!(text.contains('\u{2588}'));
}
#[test]
fn resolve_should_redact_logic() {
let mut cfg = RedactionConfig {
enabled: false,
auto: false,
..Default::default()
};
assert!(!resolve_should_redact(&cfg, false, false));
assert!(!resolve_should_redact(&cfg, true, false));
assert!(explicit_request_is_blocked(&cfg, true, false));
assert!(!explicit_request_is_blocked(&cfg, false, false));
assert!(!resolve_should_redact(&cfg, true, true));
assert!(!explicit_request_is_blocked(&cfg, true, true));
cfg.enabled = true;
cfg.auto = true;
assert!(resolve_should_redact(&cfg, false, false));
assert!(!resolve_should_redact(&cfg, false, true)); assert!(!explicit_request_is_blocked(&cfg, true, false));
let default = RedactionConfig::default();
assert!(default.enabled);
assert!(!default.auto);
assert!(!resolve_should_redact(&default, false, false));
assert!(resolve_should_redact(&default, true, false));
}
#[test]
fn all_builtin_patterns_are_valid_regexes() {
for rule in builtin_rules() {
assert!(
Regex::new(&rule.pattern).is_ok(),
"invalid regex for builtin '{}': {}",
rule.name,
rule.pattern
);
}
let engine = RedactionEngine::from_config(&RedactionConfig::default()).unwrap();
let names = engine.rule_names();
for expected in [
"github_token",
"slack_token",
"private_key_pem",
"gcp_service_account",
"azure_client_secret",
"generic_api_key",
"bearer_token",
"connection_string",
"discord_token",
"hashicorp_vault_token",
] {
assert!(
names.contains(&expected),
"missing builtin rule '{}'",
expected
);
}
}
#[test]
fn new_builtin_rules_match_sample_secrets() {
let engine = RedactionEngine::from_config(&RedactionConfig::default()).unwrap();
let samples = [
"ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
"xoxb-1234567890-1234567890-abcdefghijKLMNOPqrstuvwx",
"-----BEGIN OPENSSH PRIVATE KEY-----",
"token = hvs.ABCDEFGHIJKLMNOPQRSTUVWX01",
];
for s in samples {
let parser = screen_of(s, 120, 3);
let map = engine.redact_screen(&parser, None);
assert!(!map.is_empty(), "expected a redaction for sample: {}", s);
}
}
#[test]
fn user_rule_can_disable_builtin() {
let cfg = RedactionConfig {
enabled: true,
auto: false,
rules: vec![RedactionRuleConfig {
name: "email".to_string(),
pattern: String::new(),
replacement: default_replacement(),
enabled: false,
color: None,
min_entropy: None,
keep_prefix: None,
keep_suffix: None,
}],
..Default::default()
};
let engine = RedactionEngine::from_config(&cfg).unwrap();
let parser = screen_of("admin@example.com", 120, 3);
let map = engine.redact_screen(&parser, None);
assert_eq!(count_of(&map, "email"), 0);
}
#[test]
fn overriding_builtin_keeps_partial_redaction_fields() {
let cfg = RedactionConfig {
enabled: true,
auto: false,
rules: vec![RedactionRuleConfig {
name: "aws_key".to_string(),
pattern: String::new(),
replacement: default_replacement(),
enabled: true,
color: None,
min_entropy: None,
keep_prefix: Some(4),
keep_suffix: None,
}],
..Default::default()
};
let engine = RedactionEngine::from_config(&cfg).unwrap();
let parser = screen_of("AKIAIOSFODNN7EXAMPLE", 120, 3);
let map = engine.redact_screen(&parser, None);
for col in 0..4 {
assert!(map.get(0, col).is_none(), "prefix col {} redacted", col);
}
for col in 4..20 {
assert!(map.get(0, col).is_some(), "col {} not redacted", col);
}
}
#[test]
fn custom_user_rule_is_applied() {
let cfg = RedactionConfig {
enabled: true,
auto: false,
rules: vec![RedactionRuleConfig {
name: "ticket".to_string(),
pattern: r"TICKET-\d+".to_string(),
replacement: "[REDACTED-TICKET]".to_string(),
enabled: true,
color: None,
min_entropy: None,
keep_prefix: None,
keep_suffix: None,
}],
..Default::default()
};
let engine = RedactionEngine::from_config(&cfg).unwrap();
let parser = screen_of("see TICKET-1234", 120, 3);
let map = engine.redact_screen(&parser, None);
assert_eq!(count_of(&map, "ticket"), 1);
}
#[test]
fn per_rule_color_is_applied() {
let mut rule = RedactionRuleConfig::new("ip", r"\b10\.0\.0\.\d+\b", "[REDACTED-IP]");
rule.color = Some("#ff6600".to_string());
let cfg = RedactionConfig {
rules: vec![rule],
..Default::default()
};
let engine = RedactionEngine::from_config(&cfg).unwrap();
let parser = screen_of("ip 10.0.0.5 up", 120, 3);
let map = engine.redact_screen(&parser, Some(&["ip".to_string()]));
let cell = map.get(0, 3).expect("redacted");
assert_eq!(cell.block_color, [255, 102, 0]);
}
#[test]
fn engine_wide_color_overrides_default() {
let cfg = RedactionConfig {
color: Some("#00ff00".to_string()),
label_color: Some("#111111".to_string()),
enabled: true,
..Default::default()
};
let engine = RedactionEngine::from_config(&cfg).unwrap();
let parser = screen_of("ip 192.168.1.9 up", 120, 3);
let map = engine.redact_screen(&parser, None);
let cell = map.get(0, 3).expect("redacted");
assert_eq!(cell.block_color, [0, 255, 0]);
assert_eq!(cell.label_color, [17, 17, 17]);
}
#[test]
fn min_entropy_filters_low_randomness_matches() {
let mut rule = RedactionRuleConfig::new("token", r"\btok_[A-Za-z0-9]+\b", "[REDACTED]");
rule.min_entropy = Some(3.0);
let cfg = RedactionConfig {
rules: vec![rule],
..Default::default()
};
let engine = RedactionEngine::from_config(&cfg).unwrap();
let low = screen_of("tok_aaaaaaaa", 120, 3);
assert_eq!(count_of(&engine.redact_screen(&low, None), "token"), 0);
let high = screen_of("tok_a8Xk2Qm9Zp", 120, 3);
assert_eq!(count_of(&engine.redact_screen(&high, None), "token"), 1);
}
#[test]
fn manual_coordinate_redaction() {
let parser = screen_of("secret data here", 120, 3);
let mut map = RedactionMap::default();
map.add_manual(0, 0, 6, Some("SECRET"));
for col in 0..6 {
assert!(map.get(0, col).is_some(), "column {} redacted", col);
}
assert!(map.get(0, 6).is_none());
let text = map.redacted_plain_text(&parser);
assert!(!text.contains("secret"));
}
#[test]
fn shannon_entropy_bounds() {
assert_eq!(shannon_entropy(""), 0.0);
assert_eq!(shannon_entropy("aaaa"), 0.0);
assert!((shannon_entropy("abab") - 1.0).abs() < 1e-9);
}
#[test]
fn yaml_rules_load() {
let dir = std::path::Path::new("target/yaml-test-rules");
std::fs::create_dir_all(dir).unwrap();
let yaml = r#"
rules:
- name: slack_token
id: kf.slack
pattern: 'xoxb-[0-9A-Za-z-]+'
min_entropy: 3.0
confidence: high
- id: disabled_rule
pattern: 'DISABLED-\d+'
confidence: disabled
- name: no_pattern
"#;
std::fs::write(dir.join("kf.yaml"), yaml).unwrap();
let rules = load_rules_from_dir(dir);
assert_eq!(rules.len(), 2);
let slack = rules.iter().find(|r| r.name == "slack_token").unwrap();
assert_eq!(slack.min_entropy, Some(3.0));
assert!(slack.enabled);
let disabled = rules.iter().find(|r| r.name == "disabled_rule").unwrap();
assert!(!disabled.enabled);
std::fs::remove_dir_all(dir).ok();
}
#[test]
fn yaml_rules_keep_generic_syntax_and_rejection_semantics() {
let dir = std::path::Path::new("target/yaml-ng-test-rules");
std::fs::remove_dir_all(dir).ok();
std::fs::create_dir_all(dir).unwrap();
std::fs::write(
dir.join("generic.yaml"),
r#"
default_entropy: &entropy 3.5
rules:
- name: anchored_rule
pattern: 'ANCHOR-[0-9a-f]{8}'
confidence: high
min_entropy: *entropy
- {id: flow_rule, regex: 'FLOW-\d+'}
- name: block_rule
pattern: >-
BLOCK-[A-Z]+
description: |
A key the rule type does not model; it must be ignored, not rejected.
tags: [one, two]
"#,
)
.unwrap();
std::fs::write(
dir.join("broken.yaml"),
"rules:\n - name: x\n pattern: [\n",
)
.unwrap();
std::fs::write(
dir.join("short.yml"),
"rules:\n - name: yml_rule\n pattern: 'YML-\\d+'\n",
)
.unwrap();
std::fs::write(
dir.join("ignored.json"),
r#"{"rules":[{"name":"json_rule","pattern":"J-1"}]}"#,
)
.unwrap();
let rules = load_rules_from_dir(dir);
let names: BTreeSet<&str> = rules.iter().map(|r| r.name.as_str()).collect();
assert_eq!(
names,
BTreeSet::from(["anchored_rule", "flow_rule", "block_rule", "yml_rule"]),
"malformed YAML is skipped, .yml loads, non-YAML extensions are ignored"
);
let anchored = rules.iter().find(|r| r.name == "anchored_rule").unwrap();
assert_eq!(
anchored.min_entropy,
Some(3.5),
"an aliased anchor value still populates the rule"
);
assert!(anchored.enabled);
let block = rules.iter().find(|r| r.name == "block_rule").unwrap();
assert_eq!(
block.pattern, "BLOCK-[A-Z]+",
"a folded block scalar keeps its exact pattern text"
);
std::fs::remove_dir_all(dir).ok();
}
#[test]
fn toml_rules_dir_loads() {
let dir = std::path::Path::new("target/toml-test-rules");
std::fs::create_dir_all(dir).unwrap();
let toml_src = r#"
[[rules]]
name = "ticket"
pattern = 'TICKET-\d+'
replacement = "[REDACTED-TICKET]"
"#;
std::fs::write(dir.join("extra.toml"), toml_src).unwrap();
let rules = load_rules_from_dir(dir);
assert_eq!(rules.len(), 1);
assert_eq!(rules[0].name, "ticket");
std::fs::remove_dir_all(dir).ok();
}
#[test]
fn pattern_specs_decode_every_documented_field() {
let spec = ManualRedactionSpec::from_json(
r##"{"pattern":"[a-f0-9]{32}","replacement":"HASH","keep_prefix":4,"keep_suffix":2,"color":"#00ff00"}"##,
)
.expect("valid pattern spec");
match spec {
ManualRedactionSpec::Pattern {
pattern,
replacement,
keep_prefix,
keep_suffix,
color,
} => {
assert_eq!(pattern, "[a-f0-9]{32}");
assert_eq!(replacement.as_deref(), Some("HASH"));
assert_eq!(keep_prefix, Some(4));
assert_eq!(keep_suffix, Some(2));
assert_eq!(color.as_deref(), Some("#00ff00"));
}
other => panic!("expected a pattern spec, got {:?}", other),
}
match ManualRedactionSpec::from_json(r#"{"pattern":"x","label":"TAG"}"#).unwrap() {
ManualRedactionSpec::Pattern { replacement, .. } => {
assert_eq!(replacement.as_deref(), Some("TAG"))
}
other => panic!("expected a pattern spec, got {:?}", other),
}
}
#[test]
fn coordinate_specs_decode_their_range() {
match ManualRedactionSpec::from_json(
r##"{"row":3,"col_start":12,"col_end":44,"label":"SECRET","color":"#d41919"}"##,
)
.unwrap()
{
ManualRedactionSpec::Coordinate {
row,
col_start,
col_end,
label,
color,
} => {
assert_eq!((row, col_start, col_end), (3, 12, 44));
assert_eq!(label.as_deref(), Some("SECRET"));
assert_eq!(color.as_deref(), Some("#d41919"));
}
other => panic!("expected a coordinate spec, got {:?}", other),
}
}
#[test]
fn the_cli_and_serde_paths_share_one_decoder() {
for bad in [
r#"{"pattern":"x","replacment":"y"}"#,
r#"{"row":0,"col_start":0}"#,
r#"{"label":"x"}"#,
r#"[1,2,3]"#,
] {
let cli = ManualRedactionSpec::from_json(bad).expect_err("the CLI must refuse it");
let serde = serde_json::from_str::<ManualRedactionSpec>(bad)
.expect_err("the MCP path must refuse it too");
let cli = cli.to_string();
assert!(
serde.to_string().contains(&cli) || cli.contains("in redaction"),
"the two paths disagree about {bad}: {cli} vs {serde}"
);
}
let good = r#"{"pattern":"x","keep_prefix":1}"#;
assert!(serde_json::from_str::<ManualRedactionSpec>(good).is_ok());
assert!(ManualRedactionSpec::from_json(good).is_ok());
}
#[test]
fn manual_redactions_reject_invalid_input_up_front() {
let bad_regex = ManualRedactionSpec::from_json(r#"{"pattern":"([a-"}"#).unwrap();
let err = ManualRedactions::new(&[bad_regex], true).unwrap_err();
assert!(err.to_string().contains("invalid regex"), "{err}");
let bad_color =
ManualRedactionSpec::from_json(r#"{"pattern":"x","color":"nope"}"#).unwrap();
let err = ManualRedactions::new(&[bad_color], true).unwrap_err();
assert!(err.to_string().contains("invalid color"), "{err}");
let bad_coord_color = ManualRedactionSpec::from_json(
r#"{"row":0,"col_start":0,"col_end":1,"color":"12345"}"#,
)
.unwrap();
let err = ManualRedactions::new(&[bad_coord_color], true).unwrap_err();
assert!(err.to_string().contains("invalid color"), "{err}");
}
#[test]
fn manual_coordinates_are_bounds_checked() {
let specs =
vec![ManualRedactionSpec::from_json(r#"{"row":0,"col_start":0,"col_end":4}"#).unwrap()];
let manual = ManualRedactions::new(&specs, true).unwrap();
assert!(manual.validate_bounds(4, 8).is_ok());
let err = manual.validate_bounds(4, 3).unwrap_err().to_string();
assert!(
err.contains("col_end 4 is past the last rendered column boundary"),
"{err}"
);
assert!(err.contains("renders 4 row(s) x 3 column(s)"), "{err}");
let err = ManualRedactions::new(&specs, true)
.unwrap()
.validate_bounds(0, 8)
.unwrap_err()
.to_string();
assert!(err.contains("row 0 is past the last rendered row"), "{err}");
}
#[test]
fn manual_redactions_apply_patterns_and_coordinates() {
let specs = vec![
ManualRedactionSpec::from_json(
r#"{"pattern":"AKIA[0-9A-Z]{16}","replacement":"KEY","keep_prefix":4}"#,
)
.unwrap(),
ManualRedactionSpec::from_json(
r##"{"row":0,"col_start":0,"col_end":4,"label":"SECRET","color":"#00ff00"}"##,
)
.unwrap(),
];
let manual = ManualRedactions::new(&specs, true).unwrap();
assert!(!manual.is_empty());
assert_eq!(manual.coordinates().len(), 1);
let mut parser = vt100::Parser::new(4, 40, 0);
parser.process(
b"user AKIAIOSFODNN7EXAMPLE
",
);
let mut map = RedactionMap::default();
manual.apply_to(parser.screen(), &mut map);
let text = map.redacted_plain_text(parser.screen());
assert!(text.contains("AKIA\u{2588}"), "{text}");
assert!(!text.contains("AKIAIOSFODNN7EXAMPLE"), "{text}");
assert_eq!(map.get(0, 0).unwrap().block_color, [0, 255, 0]);
assert_eq!(map.get(0, 9).unwrap().block_color, DEFAULT_BLOCK_COLOR);
assert_eq!(
map.audit_summary(),
"1x custom_0, 1x manual:SECRET",
"the audit must name the specs the way the MCP tool does"
);
}
#[test]
fn manual_redactions_can_suppress_labels() {
let specs =
vec![ManualRedactionSpec::from_json(r#"{"row":0,"col_start":0,"col_end":4}"#).unwrap()];
let manual = ManualRedactions::new(&specs, false).unwrap();
let mut parser = vt100::Parser::new(2, 20, 0);
parser.process(b"secret\r\n");
let mut map = RedactionMap::default();
manual.apply_to(parser.screen(), &mut map);
for col in 0..4 {
assert!(map.get(0, col).unwrap().label_char.is_none());
}
}
fn schema_variants() -> (serde_json::Value, serde_json::Value) {
let schema = manual_redaction_schema();
let variants = schema["oneOf"]
.as_array()
.expect("the schema discriminates with oneOf, as the parser does")
.clone();
assert_eq!(variants.len(), 2, "expected two variants: {schema}");
(variants[0].clone(), variants[1].clone())
}
#[test]
fn the_schema_publishes_exactly_what_the_parser_accepts() {
let (pattern, coordinate) = schema_variants();
for (variant, fields, required) in [
(&pattern, PATTERN_FIELDS, PATTERN_REQUIRED),
(&coordinate, COORDINATE_FIELDS, COORDINATE_REQUIRED),
] {
assert_eq!(variant["type"], "object", "{variant}");
assert_eq!(
variant.get("additionalProperties"),
Some(&serde_json::Value::Bool(false)),
"a variant that allows unknown fields promises what the parser refuses: \
{variant}"
);
let published: BTreeSet<&str> = variant["properties"]
.as_object()
.expect("properties is an object")
.keys()
.map(String::as_str)
.collect();
assert_eq!(
published,
fields.iter().copied().collect::<BTreeSet<&str>>(),
"the published fields must be the accepted fields: {variant}"
);
let published_required: Vec<&str> = variant["required"]
.as_array()
.expect("required is an array")
.iter()
.map(|value| value.as_str().expect("a field name"))
.collect();
assert_eq!(published_required, required, "{variant}");
for (name, field) in variant["properties"].as_object().unwrap() {
assert!(
field.get("type").is_some(),
"field {name} has no type: {field}"
);
assert!(
field
.get("description")
.and_then(serde_json::Value::as_str)
.is_some_and(|d| !d.is_empty()),
"field {name} has no description: {field}"
);
}
}
assert!(pattern["properties"].get("pattern").is_some());
assert!(pattern["properties"].get("label").is_some());
assert!(coordinate["properties"].get("pattern").is_none());
}
#[test]
fn the_json_schema_impl_publishes_the_same_document() {
let mut generator = schemars::SchemaGenerator::default();
let schema =
<ManualRedactionSpec as schemars::JsonSchema>::json_schema(&mut generator).to_value();
assert_eq!(schema, manual_redaction_schema());
assert_eq!(
<ManualRedactionSpec as schemars::JsonSchema>::schema_name(),
"ManualRedactionSpec"
);
}
}