@davoodmehraban89 / Aram / commits / 2bcf2b3
Fork 0

fix(backend): harden fiscal-year isolation and add CI

Prevent raw foreign-key IDs from bypassing cross-year and locked-year protections. Add regression coverage and a GitHub Actions workflow for tests, migration round-trips, and compilation. Refs #5

davoodmehraban89 committed Aug 20, 2026 at 22:21 UTC 2bcf2b3cbab6d3df1d9340397cec43e363aec8f7
3 files changed +129 -8
.github/workflows/backend-ci.yml new
+42
@@ -0,0 +1,42 @@
1 +name: Backend CI
2 +
3 +on:
4 + pull_request:
5 + paths:
6 + - "backend/**"
7 + - ".github/workflows/backend-ci.yml"
8 + push:
9 + branches:
10 + - source-data-cleanup
11 + paths:
12 + - "backend/**"
13 + - ".github/workflows/backend-ci.yml"
14 +
15 +permissions:
16 + contents: read
17 +
18 +jobs:
19 + test:
20 + runs-on: ubuntu-latest
21 + defaults:
22 + run:
23 + working-directory: backend
24 + steps:
25 + - uses: actions/checkout@v4
26 + - uses: actions/setup-python@v5
27 + with:
28 + python-version: "3.12"
29 + cache: pip
30 + cache-dependency-path: backend/pyproject.toml
31 + - name: Install
32 + run: python -m pip install -e '.[test]'
33 + - name: Test
34 + run: pytest
35 + - name: Validate migration round-trip
36 + env:
37 + ARAM_DATABASE_URL: sqlite:///./ci-migration.db
38 + run: |
39 + alembic upgrade head
40 + alembic downgrade base
41 + - name: Compile
42 + run: python -m compileall -q src tests migrations
backend/src/aram_backend/models.py
+38 -8
@@ -181,15 +181,27 @@ class StatementLine(TimestampMixin, Base):
181 target_note: Mapped[NoteDefinition | None] = relationship()
182
183
184 -def _year_id_for(obj: object) -> int | None:
184 +def _year_id_for(obj: object, session: Session) -> int | None:
185 if isinstance(obj, FiscalYear):
186 return obj.id
187 if isinstance(obj, YearOwnedMixin):
188 return obj.year_id
189 if isinstance(obj, NoteRow):
190 - return obj.note.year_id if obj.note is not None else None
190 + if obj.note is not None:
191 + return obj.note.year_id
192 + if obj.note_id is not None:
193 + return session.scalar(
194 + select(NoteDefinition.year_id).where(NoteDefinition.id == obj.note_id)
195 + )
196 if isinstance(obj, StatementLine):
192 - return obj.statement.year_id if obj.statement is not None else None
197 + if obj.statement is not None:
198 + return obj.statement.year_id
199 + if obj.statement_id is not None:
200 + return session.scalar(
201 + select(FinancialStatement.year_id).where(
202 + FinancialStatement.id == obj.statement_id
203 + )
204 + )
205 return None
206
207
@@ -212,7 +224,7 @@ def enforce_audit_invariants(session: Session, _flush_context, _instances) -> No
224 for obj in protected:
225 if isinstance(obj, FiscalYear):
226 continue
215 - year_id = _year_id_for(obj)
227 + year_id = _year_id_for(obj, session)
228 if year_id is None:
229 continue
230 status = session.scalar(select(FiscalYear.status).where(FiscalYear.id == year_id))
@@ -221,9 +233,27 @@ def enforce_audit_invariants(session: Session, _flush_context, _instances) -> No
233 f"Fiscal year {year_id} is {status.value.lower()} and cannot be mutated"
234 )
235
224 - if isinstance(obj, MappingRule) and obj.target_note is not None:
225 - if obj.target_note.year_id != obj.year_id:
236 + if isinstance(obj, MappingRule):
237 + target_year_id = (
238 + obj.target_note.year_id
239 + if obj.target_note is not None
240 + else session.scalar(
241 + select(NoteDefinition.year_id).where(
242 + NoteDefinition.id == obj.target_note_id
243 + )
244 + )
245 + )
246 + if target_year_id != obj.year_id:
247 raise ValueError("Mapping rule and target note must belong to the same fiscal year")
227 - if isinstance(obj, StatementLine) and obj.target_note is not None:
228 - if obj.target_note.year_id != obj.statement.year_id:
248 + if isinstance(obj, StatementLine) and obj.target_note_id is not None:
249 + target_year_id = (
250 + obj.target_note.year_id
251 + if obj.target_note is not None
252 + else session.scalar(
253 + select(NoteDefinition.year_id).where(
254 + NoteDefinition.id == obj.target_note_id
255 + )
256 + )
257 + )
258 + if target_year_id != year_id:
259 raise ValueError("Statement line and target note must belong to the same fiscal year")
backend/tests/test_models.py
+49
@@ -68,6 +68,37 @@ def test_fiscal_year_isolation_for_mapping(session):
68 session.commit()
69
70
71 +def test_fiscal_year_isolation_cannot_be_bypassed_with_raw_ids(session):
72 + first = make_year(session, 1403)
73 + second = make_year(session, 1404)
74 + note = NoteDefinition(year_id=first.id, note_number="10", title="Assets")
75 + session.add(note)
76 + session.commit()
77 + session.add(MappingRule(
78 + year_id=second.id, rule_key="raw-id-rule", account_code="1000",
79 + target_note_id=note.id, mapping_type="O"
80 + ))
81 + with pytest.raises(ValueError, match="same fiscal year"):
82 + session.commit()
83 +
84 +
85 +def test_statement_line_isolation_cannot_be_bypassed_with_raw_ids(session):
86 + first = make_year(session, 1403)
87 + second = make_year(session, 1404)
88 + note = NoteDefinition(year_id=first.id, note_number="10", title="Assets")
89 + statement = FinancialStatement(
90 + year_id=second.id, statement_type=StatementType.BALANCE_SHEET
91 + )
92 + session.add_all([note, statement])
93 + session.commit()
94 + session.add(StatementLine(
95 + statement_id=statement.id, line_order=0, line_label="Assets",
96 + target_note_id=note.id
97 + ))
98 + with pytest.raises(ValueError, match="same fiscal year"):
99 + session.commit()
100 +
101 +
102 def test_locked_year_blocks_mutation(session):
103 year = make_year(session)
104 year.status = FiscalYearStatus.APPROVED
@@ -79,6 +110,24 @@ def test_locked_year_blocks_mutation(session):
110 session.commit()
111
112
113 +def test_locked_year_protection_cannot_be_bypassed_with_raw_parent_id(session):
114 + year = make_year(session)
115 + statement = FinancialStatement(
116 + year_id=year.id, statement_type=StatementType.BALANCE_SHEET
117 + )
118 + session.add(statement)
119 + session.commit()
120 + year.status = FiscalYearStatus.APPROVED
121 + session.commit()
122 + year.status = FiscalYearStatus.LOCKED
123 + session.commit()
124 + session.add(StatementLine(
125 + statement_id=statement.id, line_order=0, line_label="Blocked"
126 + ))
127 + with pytest.raises(ValueError, match="locked"):
128 + session.commit()
129 +
130 +
131 def test_approved_year_blocks_silent_overwrite(session):
132 year = make_year(session)
133 account = AccountVersion(