Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

fix: guard SimpleFIN holding currency against non-ISO tickers (#450)

brandonjones242d4c53c56675
diff --git a/backend/app/providers/base.py b/backend/app/providers/base.py
index 8872acdd..cb465a08 100644
--- a/backend/app/providers/base.py
+++ b/backend/app/providers/base.py
@@ -136,6 +136,11 @@ class HoldingData:
purchase_price: Optional[Decimal] = None
purchase_date: Optional[date] = None
isin: Optional[str] = None
+ # Exchange/asset symbol (e.g. "AAPL", "DOGE") for the dedicated
+ # `assets.ticker` column. Kept separate from `currency`: connectors that
+ # report a crypto position put the ticker in both, and only one of them
+ # belongs in a 3-char ISO currency field.
+ ticker: Optional[str] = None
maturity_date: Optional[date] = None
is_withdrawn: bool = False # provider signaled the position was sold/transferred
metadata: Optional[dict] = None
diff --git a/backend/app/providers/simplefin.py b/backend/app/providers/simplefin.py
index 02ef2157..c274e443 100644
--- a/backend/app/providers/simplefin.py
+++ b/backend/app/providers/simplefin.py
@@ -121,6 +121,37 @@ def _to_decimal(value: Any) -> Optional[Decimal]:
return None
+def _iso_currency(value: Any, fallback: Optional[str]) -> Optional[str]:
+ """Normalize a connector-supplied currency, guarding against non-ISO values.
+
+ SimpleFIN's ``currency`` fields are populated by the bank connector, and
+ for crypto/brokerage positions some connectors put the asset's ticker
+ there instead (e.g. ``"DOGE"``) rather than an ISO 4217 code. Every
+ column these land in — ``accounts.currency``, ``transactions.currency``,
+ ``assets.currency`` — is ``VARCHAR(3)``, so anything longer overflows the
+ write and takes down the whole connection's sync (issue #448).
+
+ This is a shape check, not a validity check: a 3-letter ticker like
+ ``BTC`` still passes. It exists to keep an oversized value from ever
+ reaching the DB, so use it at every site that forwards a raw
+ provider currency.
+ """
+ if isinstance(value, str) and len(value) == 3 and value.isalpha():
+ return value.upper()
+ return fallback
+
+
+def _ticker(value: Any) -> Optional[str]:
+ """Normalize a holding's symbol for the ``assets.ticker`` column.
+
+ Truncated to the column's 32 chars so an unexpectedly long symbol can't
+ reproduce the overflow this module already guards against for currency.
+ """
+ if not isinstance(value, str):
+ return None
+ return value.strip().upper()[:32] or None
+
+
def _surface_errors(errlist: list[dict], context: str) -> None:
"""Raise a typed exception for auth errors; log everything else.
@@ -338,7 +369,7 @@ class SimpleFinProvider(BankProvider):
accounts: list[AccountData] = []
for raw in payload.get("accounts") or []:
balance = _to_decimal(raw.get("balance")) or Decimal("0")
- currency = raw.get("currency") or "USD"
+ currency = _iso_currency(raw.get("currency"), "USD")
account_id = str(raw.get("id") or "")
if not account_id:
continue
@@ -430,7 +461,10 @@ class SimpleFinProvider(BankProvider):
amount=amount,
date=txn_date,
type=txn_type,
- currency=raw.get("currency"),
+ # None (not a fallback) when the connector sends something that
+ # isn't an ISO code: the sync layer already resolves a null
+ # transaction currency to the account's, then the user's.
+ currency=_iso_currency(raw.get("currency"), None),
status=status,
payee=payee,
raw_data=raw,
@@ -453,7 +487,8 @@ class SimpleFinProvider(BankProvider):
HoldingData(
external_id=holding_id,
name=raw.get("description") or raw.get("symbol") or holding_id,
- currency=raw.get("currency") or acc_currency,
+ currency=_iso_currency(raw.get("currency"), acc_currency),
+ ticker=_ticker(raw.get("symbol")),
current_value=market_value,
quantity=_to_decimal(raw.get("shares")),
unit_price=_to_decimal(raw.get("market_value")) / _to_decimal(
diff --git a/backend/app/services/connection_service.py b/backend/app/services/connection_service.py
index c41ddea5..db6feac1 100644
--- a/backend/app/services/connection_service.py
+++ b/backend/app/services/connection_service.py
@@ -237,6 +237,7 @@ async def _upsert_asset_from_holding(
purchase_price=holding.purchase_price,
purchase_date=holding.purchase_date,
isin=holding.isin,
+ ticker=holding.ticker,
maturity_date=holding.maturity_date,
external_metadata=holding.metadata,
valuation_method="manual",
@@ -269,6 +270,8 @@ async def _upsert_asset_from_holding(
asset.purchase_date = holding.purchase_date
if holding.isin:
asset.isin = holding.isin
+ if holding.ticker:
+ asset.ticker = holding.ticker
if holding.maturity_date:
asset.maturity_date = holding.maturity_date
return asset
diff --git a/backend/tests/test_providers_simplefin.py b/backend/tests/test_providers_simplefin.py
index 04b57d51..2e8916bd 100644
--- a/backend/tests/test_providers_simplefin.py
+++ b/backend/tests/test_providers_simplefin.py
@@ -406,6 +406,103 @@ async def test_get_holdings_parses_investment_data():
assert h.current_value == Decimal("105884.80")
assert h.quantity == Decimal("550.0")
assert (h.metadata or {}).get("symbol") == "AAPL"
+ # Also promoted to the dedicated column, not just the metadata blob.
+ assert h.ticker == "AAPL"
+
+
+async def test_get_holdings_crypto_ticker_currency_falls_back_to_account_currency():
+ """A connector-supplied ticker like ``DOGE`` isn't an ISO currency code.
+
+ ``HoldingData.currency`` maps to a ``VARCHAR(3)`` DB column — writing a
+ 4-letter ticker there overflows and used to crash the entire sync for
+ the account (see issue #448). It should fall back to the account's
+ currency instead.
+ """
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(
+ 200,
+ json={
+ "accounts": [
+ {
+ "id": "acc-1",
+ "currency": "USD",
+ "holdings": [
+ {
+ "id": "h-crypto",
+ "description": "Dogecoin",
+ "symbol": "DOGE",
+ "currency": "DOGE",
+ "market_value": "42.00",
+ "shares": "100",
+ },
+ ],
+ }
+ ]
+ },
+ )
+
+ creds = {"access_url": "https://u:[email protected]/simplefin"}
+ with _patched_client(handler):
+ holdings = await SimpleFinProvider().get_holdings(creds)
+ assert len(holdings) == 1
+ assert holdings[0].currency == "USD"
+ # The ticker itself belongs in the dedicated 32-char column, not in
+ # `currency` — that's the pairing issue #448 asks for.
+ assert holdings[0].ticker == "DOGE"
+
+
+async def test_get_accounts_non_iso_currency_falls_back_to_usd():
+ """The same connector quirk on an *account* would overflow accounts.currency.
+
+ Accounts are upserted before holdings during a sync, so an unguarded
+ account currency crashes the connection before the holdings guard is
+ ever reached.
+ """
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(
+ 200,
+ json={
+ "accounts": [
+ {
+ "id": "acc-1",
+ "name": "Crypto Wallet",
+ "currency": "DOGE",
+ "balance": "10.00",
+ "transactions": [],
+ }
+ ]
+ },
+ )
+
+ creds = {"access_url": "https://u:[email protected]/simplefin"}
+ with _patched_client(handler):
+ accounts = await SimpleFinProvider().get_accounts(creds)
+ assert accounts[0].currency == "USD"
+
+
+def test_build_transaction_non_iso_currency_is_none():
+ """A bogus transaction currency resolves to None, not a bad 3-char write.
+
+ The sync layer reads `txn_data.currency or acc_data.currency or
+ user_currency`, so None correctly defers to the account's currency.
+ """
+ raw = {
+ "id": "t1",
+ "amount": "-1.00",
+ "posted": 1672531200,
+ "currency": "DOGE",
+ "description": "buy",
+ }
+ txn = SimpleFinProvider._build_transaction(raw, "description")
+ assert txn is not None
+ assert txn.currency is None
+ # A real ISO code still passes through, normalized.
+ ok = SimpleFinProvider._build_transaction({**raw, "currency": "eur"}, "description")
+ assert ok.currency == "EUR"
# ----- misc -------------------------------------------------------------------