@davoodmehraban89 / Aram-1 / commits / 12fa5fa
Fork 0

fix(mapping): harden multi-statement trace integrity and reject forged mapping/account links

davoodmehraban89 committed Oct 31, 2026 at 09:32 UTC 12fa5fad01d38c918c10d89b73a40c760ce7fc1e
1 file changed +86 -42
backend/src/aram_backend/reconciliation_review.py
+86 -42
@@ -131,59 +131,101 @@ def _validated_preview(content: bytes, preview: CoveragePreview) -> None:
131 raise CoverageError("COVERAGE_PROVENANCE_INVALID")
132 if sha256(content).hexdigest() != preview.source_sha256:
133 raise CoverageError("SOURCE_HASH_MISMATCH")
134 - if (preview.account_count < 1
134 + if (type(preview.account_count) is not int or preview.account_count < 1
135 or preview.account_count != len(preview.accounts)
136 or preview.account_count != preview.candidate_mapped_accounts + preview.blocked_accounts
137 or preview.candidate_mapped_accounts != sum(a.included for a in preview.accounts)
138 or preview.blocked_accounts != sum(not a.included for a in preview.accounts)):
139 raise CoverageError("COVERAGE_COUNT_MISMATCH")
140 +
141 accounts = {account.account_code: account for account in preview.accounts}
142 if len(accounts) != preview.account_count:
143 raise CoverageError("DUPLICATE_SOURCE_ACCOUNT")
144
144 - mappings_by_account: dict[str, int] = {}
145 + mappings_by_account: dict[str, list[Any]] = {}
146 + mappings_by_statement: dict[tuple[str, str], list[Any]] = {}
147 mapping_ids: set[str] = set()
146 - mapping_rows: set[tuple[str, int]] = set()
147 - included_statement_keys: set[tuple[str, str]] = set()
148 + mapping_rows: set[int] = set()
149 grouped: dict[tuple[str, str, str, str], list[tuple[str, Decimal]]] = {}
150 +
151 for mapping in preview.mappings:
150 - if mapping.account_code not in accounts:
152 + account = accounts.get(mapping.account_code)
153 + if account is None:
154 raise CoverageError("MAPPING_TRACE_UNKNOWN_ACCOUNT")
152 - mappings_by_account[mapping.account_code] = (
153 - mappings_by_account.get(mapping.account_code, 0) + 1
154 - )
155 - if mapping.mapping_id in mapping_ids or (
156 - mapping.account_code, mapping.mapping_excel_row
157 - ) in mapping_rows:
155 + if (mapping.account_name != account.account_name
156 + or mapping.account_excel_row != account.account_excel_row
157 + or mapping.source_row != account.source_row
158 + or _money(mapping.closing_exposure) != _money(account.closing_exposure)):
159 + raise CoverageError("MAPPING_TRACE_SOURCE_MISMATCH")
160 + if (not isinstance(mapping.mapping_id, str) or not mapping.mapping_id
161 + or mapping.mapping_id != mapping.mapping_id.strip()
162 + or type(mapping.mapping_excel_row) is not int
163 + or mapping.mapping_excel_row < 2
164 + or mapping.mapping_excel_row in mapping_rows):
165 raise CoverageError("DUPLICATE_MAPPING_TRACE")
159 - mapping_ids.add(mapping.mapping_id)
160 - mapping_rows.add((mapping.account_code, mapping.mapping_excel_row))
161 - if mapping.included:
162 - statement_key = (mapping.account_code, mapping.target_statement)
163 - if statement_key in included_statement_keys:
164 - raise CoverageError("DUPLICATE_ACCOUNT_STATEMENT_MAPPING")
165 - included_statement_keys.add(statement_key)
166 - target = _key(
167 - mapping.target_statement, mapping.target_section,
168 - mapping.target_note, mapping.target_subnote,
169 - )
170 - grouped.setdefault(target, []).append(
171 - (mapping.account_code, _money(mapping.closing_exposure))
172 - )
173 - elif accounts[mapping.account_code].included and not any(
174 - other.included and other.account_code == mapping.account_code
175 - for other in preview.mappings
166 + if mapping.mapping_id in mapping_ids:
167 + raise CoverageError("DUPLICATE_MAPPING_TRACE")
168 + if mapping.target_statement not in ALLOWED_STATEMENTS:
169 + raise CoverageError("INVALID_MAPPING_TRACE_STATEMENT")
170 + for value in (
171 + mapping.target_section, mapping.target_note, mapping.target_subnote,
172 ):
177 - raise CoverageError("ACCOUNT_MAPPING_ELIGIBILITY_MISMATCH")
173 + if (not isinstance(value, str) or value != value.strip()
174 + or any(ord(char) < 32 or ord(char) == 127 for char in value)):
175 + raise CoverageError("INVALID_MAPPING_TRACE_TARGET")
176 + if not mapping.target_section or not mapping.target_note:
177 + raise CoverageError("INVALID_MAPPING_TRACE_TARGET")
178 +
179 + mapping_ids.add(mapping.mapping_id)
180 + mapping_rows.add(mapping.mapping_excel_row)
181 + mappings_by_account.setdefault(mapping.account_code, []).append(mapping)
182 + statement_key = (mapping.account_code, mapping.target_statement)
183 + mappings_by_statement.setdefault(statement_key, []).append(mapping)
184 +
185 + for statement_key, items in mappings_by_statement.items():
186 + if len(items) > 1 and any(item.included for item in items):
187 + raise CoverageError("AMBIGUOUS_MAPPING_INCLUDED")
188 +
189 + for mapping in preview.mappings:
190 + if not mapping.included:
191 + continue
192 + target = _key(
193 + mapping.target_statement, mapping.target_section,
194 + mapping.target_note, mapping.target_subnote,
195 + )
196 + grouped.setdefault(target, []).append(
197 + (mapping.account_code, _money(mapping.closing_exposure))
198 + )
199
200 for account in preview.accounts:
180 - if account.mapping_count != mappings_by_account.get(account.account_code, 0):
201 + rows = mappings_by_account.get(account.account_code, [])
202 + if (type(account.mapping_count) is not int
203 + or account.mapping_count < 0
204 + or account.mapping_count != len(rows)):
205 raise CoverageError("MAPPING_TRACE_COUNT_MISMATCH")
182 - if account.included != any(
183 - item.included for item in preview.mappings
184 - if item.account_code == account.account_code
185 - ):
206 + if account.included != any(item.included for item in rows):
207 raise CoverageError("ACCOUNT_MAPPING_ELIGIBILITY_MISMATCH")
208 + if account.mapping_count == 1:
209 + only = rows[0]
210 + if only.included:
211 + expected = (
212 + only.mapping_excel_row, only.mapping_id,
213 + only.target_statement, only.target_section,
214 + only.target_note, only.target_subnote,
215 + )
216 + actual = (
217 + account.mapping_excel_row, account.mapping_id,
218 + account.target_statement, account.target_section,
219 + account.target_note, account.target_subnote,
220 + )
221 + if actual != expected:
222 + raise CoverageError("ACCOUNT_MAPPING_TRACE_MISMATCH")
223 + elif any(value is not None for value in (
224 + account.mapping_excel_row, account.mapping_id,
225 + account.target_statement, account.target_section,
226 + account.target_note, account.target_subnote,
227 + )):
228 + raise CoverageError("ACCOUNT_MAPPING_TRACE_MISMATCH")
229
230 source_values = [_money(account.closing_exposure) for account in preview.accounts]
231 candidate_values = [
@@ -192,21 +234,23 @@ def _validated_preview(content: bytes, preview: CoveragePreview) -> None:
234 blocked_values = [
235 _money(account.closing_exposure) for account in preview.accounts if not account.included
236 ]
195 - if len(preview.notes) != len({
237 + note_keys = [
238 _key(note.statement, note.section, note.note_number, note.subnote_code)
239 for note in preview.notes
198 - }):
240 + ]
241 + if len(note_keys) != len(set(note_keys)):
242 raise CoverageError("DUPLICATE_CANDIDATE_NOTE")
200 - note_by_key = {
201 - _key(note.statement, note.section, note.note_number, note.subnote_code): note
202 - for note in preview.notes
203 - }
243 + note_by_key = dict(zip(note_keys, preview.notes))
244 if set(note_by_key) != set(grouped):
245 raise CoverageError("COVERAGE_NOTE_MAPPING_MISMATCH")
246 for target, note in note_by_key.items():
247 mapped = grouped[target]
208 - if (_money(note.closing_exposure) != _sum_exact([amount for _, amount in mapped])
209 - or note.account_codes != tuple(code for code, _ in mapped)):
248 + codes = tuple(code for code, _ in mapped)
249 + if (len(codes) != len(set(codes))
250 + or _money(note.closing_exposure) != _sum_exact(
251 + [amount for _, amount in mapped]
252 + )
253 + or note.account_codes != codes):
254 raise CoverageError("COVERAGE_NOTE_MAPPING_MISMATCH")
255 if (_sum_exact(source_values) != _money(preview.source_closing_exposure)
256 or _sum_exact(candidate_values) != _money(preview.candidate_closing_exposure)