feat(import,store): bank files mask, chart files refuse, the audit finds an old line

A bank file's description cell is not typed by a person and often carries
a nine-digit reference, so books.import_bank_csv no longer meets a refusal
for one: a number that reads as a tax id is kept masked to its last four
digits (•••-••-1234) and the answer says how many values and on which
lines (masked_count, masked, masked_note). A chart file is typed by a
person: a cell that looks like a tax id refuses the file, naming its row
and column, never the number. The store's own attachment, unlink, withdraw
and void texts meet the same check. books.audit gains tax_id_in_journal: a
journal line an earlier version took whose text field looks like a tax id
is named by line and field, never the number; the books open and replay
as they are and the journal is never rewritten (BS-entry-4).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-21 15:05:19 -07:00
1 parent 7327d04897
commit 98b1adb754
8 files changed
+337 -11

No files matched your search

+107
View File
@@ -119,6 +119,11 @@ pub const CODES: &[ledger_core::audit::CodeRow] = &[
severity: Severity::Warning,
predicate: "two filed attachments, neither withdrawn, hold the same bytes",
},
ledger_core::audit::CodeRow {
code: "tax_id_in_journal",
severity: Severity::Warning,
predicate: "a journal line's text field holds a number that looks like a tax id (nine digits standing alone, an SSN's 3-2-4 or an EIN's 2-7)",
},
ledger_core::audit::CodeRow {
code: "backup_stale",
severity: Severity::Warning,
@@ -144,6 +149,69 @@ fn found(code: &'static str, subject: Subject, message: String) -> Finding {
}
}
/// The journal fields that hold words someone wrote (never an id, a digest
/// or an amount): what [`Store::integrity_findings`] reads for a tax id.
const TEXT_KEYS: &[&str] = &[
"description",
"memo",
"note",
"notes",
"reason",
"name",
"label",
"title",
"citation",
"decided_by",
"detail",
"detail_md",
"resolution",
"decision_md",
"body_md",
"subject",
"reference",
"customer_ref",
"vendor_ref",
"duplicate_reason",
"no_party_reason",
"payee",
"source",
"reimbursable_to",
"file_name",
"serial",
"tag",
"employee_name",
"display_name",
"address",
"contact",
"kept_at",
"approver",
];
/// Every text field of `v` named by [`TEXT_KEYS`], as (its key, its text),
/// at any depth; a list of texts under such a key counts each.
fn text_fields_in(v: &Value, out: &mut Vec<(String, String)>) {
match v {
Value::Object(m) => {
for (k, x) in m {
if TEXT_KEYS.contains(&k.as_str()) {
match x {
Value::String(s) => out.push((k.clone(), s.clone())),
Value::Array(a) => out.extend(
a.iter()
.filter_map(Value::as_str)
.map(|s| (k.clone(), s.to_string())),
),
_ => {}
}
}
text_fields_in(x, out);
}
}
Value::Array(a) => a.iter().for_each(|x| text_fields_in(x, out)),
_ => {}
}
}
/// What the backups' standing says, one target at a time, for the audit
/// (the server reads the standing; the store does not know the backups).
#[derive(Clone, Debug)]
@@ -436,6 +504,45 @@ impl Store {
format!("attachments {ids} hold the same bytes ({sha})"),
));
}
out.extend(self.tax_id_findings());
out
}
/// The journal lines whose text fields hold what looks like a tax id
/// (BS-partner-2): a line an earlier version took, or one a caller
/// other than the tools wrote. The books open and replay as they are
/// and the journal is never rewritten; the audit says where each one
/// is, by line and field, and never what the number is.
fn tax_id_findings(&self) -> Vec<Finding> {
let rows: Vec<(u64, String, String)> = self
.conn
.prepare("SELECT seq, type, payload FROM events ORDER BY seq")
.and_then(|mut s| {
s.query_map([], |r| {
Ok((r.get::<_, i64>(0)? as u64, r.get(1)?, r.get(2)?))
})?
.collect::<Result<Vec<_>, _>>()
})
.unwrap_or_default();
let mut out = Vec::new();
for (seq, kind, payload) in rows {
let Ok(v) = serde_json::from_str::<Value>(&payload) else {
continue;
};
let mut fields = Vec::new();
text_fields_in(&v, &mut fields);
for (field, text) in fields {
if ledger_core::settings::holds_printed_tin(&text) {
out.push(found(
"tax_id_in_journal",
Subject::Named(format!("journal line {seq}")),
format!(
"journal line {seq} ({kind}) holds, in its `{field}`, a number that looks like a tax id. The journal is never rewritten and the books open as they are: correct what it describes going forward (reverse the entry and enter it again without the number), and keep the tax id only in the vault (books.set_tin or a W-9, books.set_w9)"
),
));
}
}
}
out
}
+13
View File
@@ -4755,6 +4755,10 @@ impl Store {
bytes: &[u8],
description: Option<&str>,
) -> Result<(u64, String, bool), StoreError> {
LedgerError::free_text("the file name", file_name)?;
if let Some(d) = description {
LedgerError::free_text("the description", d)?;
}
self.attach(AttachRequest {
line_type: "receipt.attached",
category: "receipts",
@@ -4795,6 +4799,10 @@ impl Store {
bytes: &[u8],
description: Option<&str>,
) -> Result<(u64, String, bool), StoreError> {
LedgerError::free_text("the file name", file_name)?;
if let Some(d) = description {
LedgerError::free_text("the description", d)?;
}
self.attach(AttachRequest {
line_type: "document.filed",
category,
@@ -5692,6 +5700,7 @@ impl Store {
if reason.is_empty() {
return Err(StoreError::ReasonRequired);
}
LedgerError::free_text("the reason", reason)?;
let (entry_id, withdrawn) = self.attachment_state(attachment_id)?;
if withdrawn {
return Err(StoreError::AttachmentWithdrawn(attachment_id));
@@ -5760,6 +5769,7 @@ impl Store {
if reason.is_empty() {
return Err(StoreError::ReasonRequired);
}
LedgerError::free_text("the reason", reason)?;
self.check_linkable(attachment_id)?;
if let Some(sub) = self.attachment_links(attachment_id)?.first() {
return Err(StoreError::AlreadyLinked(attachment_id, sub.describe()));
@@ -5936,6 +5946,7 @@ impl Store {
if reason.is_empty() {
return Err(StoreError::ReasonRequired);
}
LedgerError::free_text("the reason", reason)?;
self.attachment_state(attachment_id)?;
let linked = self
.conn
@@ -6065,6 +6076,7 @@ impl Store {
if reason.is_empty() {
return Err(StoreError::ReasonRequired);
}
LedgerError::free_text("the reason", reason)?;
if self.statement(attachment_id)?.is_none() {
return Err(StoreError::InvalidStatement(format!(
"attachment {attachment_id} records no statement"
@@ -6215,6 +6227,7 @@ impl Store {
if reason.is_empty() {
return Err(StoreError::ReasonRequired);
}
LedgerError::free_text("the reason", reason)?;
let r = self
.reconciliations(None)?
.into_iter()