fix(store): text composed from stored text is masked, never refused; the audit reads each field as its class
An asset an earlier version took may hold a number that reads as a tax id in its description. Depreciation, the final depreciation and the disposal compose their entry descriptions from it and went through the checked constructor, so such an asset could no longer be depreciated or disposed of. The composed text now holds the number masked to its last four. A template's default description and an issued invoice's file name, also composed from stored text, are masked the same way. A write-off's file name and description are checked before its entry is journaled, as the rest of its evidence is. The read-back of the projection reads an old tag or serial number as history (replay), so verify never refuses books that hold one. The audit's tax_id_in_journal gives each journal key a class: a bare nine digits in a tag, serial, reference, file name, code or label is a number, in free words a finding. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
dd5f1befd6
commit
049cf2a6b5
3 files changed
+400
-55
No files matched your search
@@ -149,57 +149,72 @@ fn found(code: &'static str, subject: Subject, message: String) -> Finding {
|
||||
}
|
||||
}
|
||||
|
||||
/// How the audit reads one journal field for a tax id.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
enum TextClass {
|
||||
/// Free words: nine digits standing alone, an SSN's 3-2-4 or an EIN's
|
||||
/// 2-7 are found.
|
||||
Free,
|
||||
/// A reference (a tag, a serial, an invoice number, a file name, an
|
||||
/// account code): only the grouped shapes are found, a bare nine digits
|
||||
/// being a legitimate number there (WB-partner-15).
|
||||
Reference,
|
||||
}
|
||||
|
||||
/// 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",
|
||||
/// or an amount) and how each is read: what [`Store::integrity_findings`]
|
||||
/// reads for a tax id.
|
||||
const TEXT_KEYS: &[(&str, TextClass)] = &[
|
||||
("description", TextClass::Free),
|
||||
("memo", TextClass::Free),
|
||||
("note", TextClass::Free),
|
||||
("notes", TextClass::Free),
|
||||
("reason", TextClass::Free),
|
||||
("name", TextClass::Free),
|
||||
("label", TextClass::Reference),
|
||||
("title", TextClass::Free),
|
||||
("citation", TextClass::Free),
|
||||
("decided_by", TextClass::Free),
|
||||
("detail", TextClass::Free),
|
||||
("detail_md", TextClass::Free),
|
||||
("resolution", TextClass::Free),
|
||||
("decision_md", TextClass::Free),
|
||||
("body_md", TextClass::Free),
|
||||
("subject", TextClass::Free),
|
||||
("reference", TextClass::Reference),
|
||||
("customer_ref", TextClass::Reference),
|
||||
("vendor_ref", TextClass::Reference),
|
||||
("duplicate_reason", TextClass::Free),
|
||||
("no_party_reason", TextClass::Free),
|
||||
("payee", TextClass::Free),
|
||||
("source", TextClass::Free),
|
||||
("reimbursable_to", TextClass::Free),
|
||||
("file_name", TextClass::Reference),
|
||||
("serial", TextClass::Reference),
|
||||
("tag", TextClass::Reference),
|
||||
("code", TextClass::Reference),
|
||||
("employee_name", TextClass::Free),
|
||||
("display_name", TextClass::Free),
|
||||
("address", TextClass::Free),
|
||||
("contact", TextClass::Free),
|
||||
("kept_at", TextClass::Reference),
|
||||
("approver", TextClass::Free),
|
||||
];
|
||||
|
||||
/// 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)>) {
|
||||
/// Every text field of `v` named by [`TEXT_KEYS`], as (its key, how it is
|
||||
/// read, 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, TextClass, String)>) {
|
||||
match v {
|
||||
Value::Object(m) => {
|
||||
for (k, x) in m {
|
||||
if TEXT_KEYS.contains(&k.as_str()) {
|
||||
if let Some((_, class)) = TEXT_KEYS.iter().find(|(key, _)| *key == k.as_str()) {
|
||||
match x {
|
||||
Value::String(s) => out.push((k.clone(), s.clone())),
|
||||
Value::String(s) => out.push((k.clone(), *class, s.clone())),
|
||||
Value::Array(a) => out.extend(
|
||||
a.iter()
|
||||
.filter_map(Value::as_str)
|
||||
.map(|s| (k.clone(), s.to_string())),
|
||||
.map(|s| (k.clone(), *class, s.to_string())),
|
||||
),
|
||||
_ => {}
|
||||
}
|
||||
@@ -545,8 +560,14 @@ impl Store {
|
||||
};
|
||||
let mut fields = Vec::new();
|
||||
text_fields_in(&v, &mut fields);
|
||||
for (field, text) in fields {
|
||||
if ledger_core::settings::holds_printed_tin(&text) {
|
||||
for (field, class, text) in fields {
|
||||
let looks = match class {
|
||||
TextClass::Free => ledger_core::settings::holds_printed_tin(&text),
|
||||
TextClass::Reference => {
|
||||
ledger_core::settings::holds_printed_tin_in_reference(&text)
|
||||
}
|
||||
};
|
||||
if looks {
|
||||
out.push(found(
|
||||
"tax_id_in_journal",
|
||||
Subject::Named(format!("journal line {seq}")),
|
||||
|
||||
@@ -1350,16 +1350,19 @@ impl Store {
|
||||
) -> Result<EntryId, StoreError> {
|
||||
let (default_description, lines) =
|
||||
self.ledger.template_lines(template_id, amount, slots)?;
|
||||
let description = description
|
||||
.map(str::to_string)
|
||||
.or(default_description)
|
||||
.unwrap_or_else(|| {
|
||||
// The caller's words are checked as they come; a description the
|
||||
// template or its convention's title gives is stored text, masked
|
||||
// where an older version let it hold a tax id's shape.
|
||||
let description = description.map(str::to_string).unwrap_or_else(|| {
|
||||
let stored = default_description.unwrap_or_else(|| {
|
||||
self.ledger
|
||||
.conventions()
|
||||
.convention(template_id)
|
||||
.map(|c| c.title.clone())
|
||||
.unwrap_or_default()
|
||||
});
|
||||
ledger_core::settings::mask_printed_tins(&stored).into_owned()
|
||||
});
|
||||
self.rehearse(|l| {
|
||||
let e = l.try_create_entry(date, &description)?;
|
||||
for (account, side, amount) in &lines {
|
||||
@@ -2459,7 +2462,13 @@ impl Store {
|
||||
});
|
||||
}
|
||||
let amount = due.amount;
|
||||
let description = format!("Depreciation — {} — {period}", asset.description);
|
||||
// Composed from the asset's own words, which the books may hold
|
||||
// from before free text was read for a tax id's shape: a number
|
||||
// that reads as one is masked, never refused (WB-partner-15).
|
||||
let description = format!(
|
||||
"Depreciation — {} — {period}",
|
||||
ledger_core::settings::mask_printed_tins(&asset.description)
|
||||
);
|
||||
let money = Money::new(amount, self.base_currency);
|
||||
// Every refusal before the first journaled line: the entry on a
|
||||
// copy (and that it is the period's run, no more and no less:
|
||||
@@ -2885,7 +2894,8 @@ impl Store {
|
||||
(
|
||||
format!(
|
||||
"Depreciation — {} — {} (final)",
|
||||
asset.description, preview.period
|
||||
ledger_core::settings::mask_printed_tins(&asset.description),
|
||||
preview.period
|
||||
),
|
||||
vec![
|
||||
(
|
||||
@@ -2902,7 +2912,11 @@ impl Store {
|
||||
)
|
||||
});
|
||||
let disposal = (
|
||||
format!("Disposal — {} — {}", asset.description, date),
|
||||
format!(
|
||||
"Disposal — {} — {}",
|
||||
ledger_core::settings::mask_printed_tins(&asset.description),
|
||||
date
|
||||
),
|
||||
Self::disposal_lines(
|
||||
&asset,
|
||||
&preview,
|
||||
@@ -3586,8 +3600,14 @@ impl Store {
|
||||
entry_id,
|
||||
"invoice entry diverged from journal"
|
||||
);
|
||||
// The copy's name is composed from the invoice's number (a prefix
|
||||
// the books' settings give, then digits): a number that reads as a
|
||||
// tax id's printed shape is masked, never refused (WB-partner-15).
|
||||
let file_name = match &fields.number {
|
||||
Some(n) => format!("{n}.pdf"),
|
||||
Some(n) => format!(
|
||||
"{}.pdf",
|
||||
ledger_core::settings::mask_printed_tins_in_reference(n)
|
||||
),
|
||||
None => format!("Invoice {live}.pdf"),
|
||||
};
|
||||
// The entry's and the invoice's lines apply first: they were
|
||||
@@ -4173,6 +4193,20 @@ impl Store {
|
||||
));
|
||||
}
|
||||
}
|
||||
// The file's words, as `Self::attach_receipt` reads them, before
|
||||
// the write-off's entry is journaled (a refusal after it would
|
||||
// leave the entry without its evidence).
|
||||
if let WriteOffEvidence::File {
|
||||
file_name,
|
||||
description,
|
||||
..
|
||||
} = evidence
|
||||
{
|
||||
LedgerError::reference_text("the file name", file_name)?;
|
||||
if let Some(d) = description {
|
||||
LedgerError::free_text("the description", d)?;
|
||||
}
|
||||
}
|
||||
if let WriteOffEvidence::File { bytes, .. } = evidence
|
||||
&& let Some(existing) = self.live_attachment_with_sha(&sha256_hex(bytes))?
|
||||
{
|
||||
@@ -4755,7 +4789,7 @@ impl Store {
|
||||
bytes: &[u8],
|
||||
description: Option<&str>,
|
||||
) -> Result<(u64, String, bool), StoreError> {
|
||||
LedgerError::free_text("the file name", file_name)?;
|
||||
LedgerError::reference_text("the file name", file_name)?;
|
||||
if let Some(d) = description {
|
||||
LedgerError::free_text("the description", d)?;
|
||||
}
|
||||
@@ -4799,7 +4833,7 @@ impl Store {
|
||||
bytes: &[u8],
|
||||
description: Option<&str>,
|
||||
) -> Result<(u64, String, bool), StoreError> {
|
||||
LedgerError::free_text("the file name", file_name)?;
|
||||
LedgerError::reference_text("the file name", file_name)?;
|
||||
if let Some(d) = description {
|
||||
LedgerError::free_text("the description", d)?;
|
||||
}
|
||||
@@ -6827,9 +6861,11 @@ impl Store {
|
||||
.conn
|
||||
.prepare("SELECT asset_id, tag FROM asset_tags_log ORDER BY seq")?;
|
||||
let tags = tstmt.query_map([], |r| Ok((r.get::<_, i64>(0)?, r.get::<_, String>(1)?)))?;
|
||||
// History is read, not asked for: a tag or a serial number the books
|
||||
// hold reads back whatever shape it has (BS-entry-4; WB-partner-15).
|
||||
for t in tags {
|
||||
let (id, tag) = t?;
|
||||
ledger.set_asset_tag(id as u64, &tag)?;
|
||||
ledger_core::parties::replaying(|| ledger.set_asset_tag(id as u64, &tag))?;
|
||||
}
|
||||
let mut sstmt = self
|
||||
.conn
|
||||
@@ -6837,7 +6873,7 @@ impl Store {
|
||||
let serials = sstmt.query_map([], |r| Ok((r.get::<_, i64>(0)?, r.get::<_, String>(1)?)))?;
|
||||
for row in serials {
|
||||
let (id, serial) = row?;
|
||||
ledger.set_asset_serial(id as u64, Some(&serial))?;
|
||||
ledger_core::parties::replaying(|| ledger.set_asset_serial(id as u64, Some(&serial)))?;
|
||||
}
|
||||
// Basis records: a pre-P3 one (no value at contribution) as its
|
||||
// event replays, a newer one with its facts.
|
||||
|
||||
Reference in new issue
Block a user