bankstatementparser 0.0.20__tar.gz → 0.0.22__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/PKG-INFO +4 -5
  2. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/README.md +3 -3
  3. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/__init__.py +1 -1
  4. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/base_parser.py +52 -6
  5. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/cli.py +3 -0
  6. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/export/ledger.py +50 -8
  7. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/export/parquet.py +9 -6
  8. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/hybrid/ollama_direct.py +33 -0
  9. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/input_validator.py +72 -71
  10. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/pyproject.toml +1 -1
  11. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/LICENSE +0 -0
  12. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/LICENSE-APACHE +0 -0
  13. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/LICENSE-MIT +0 -0
  14. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/_amounts.py +0 -0
  15. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/_llm_common.py +0 -0
  16. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/additional_parsers.py +0 -0
  17. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/analytics.py +0 -0
  18. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/api.py +0 -0
  19. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/bank_statement_parsers.py +0 -0
  20. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/camt_parser.py +0 -0
  21. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/enrichment/__init__.py +0 -0
  22. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/enrichment/account_mapper.py +0 -0
  23. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/enrichment/categorizer.py +0 -0
  24. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/exceptions.py +0 -0
  25. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/export/__init__.py +0 -0
  26. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/field86_parser.py +0 -0
  27. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/forensics.py +0 -0
  28. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/hybrid/__init__.py +0 -0
  29. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/hybrid/evaluation.py +0 -0
  30. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/hybrid/llm_extractor.py +0 -0
  31. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/hybrid/orchestrator.py +0 -0
  32. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/hybrid/pdf_text.py +0 -0
  33. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/hybrid/prompts.py +0 -0
  34. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/hybrid/scanner.py +0 -0
  35. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/hybrid/verification.py +0 -0
  36. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/hybrid/vision.py +0 -0
  37. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/pain001_parser.py +0 -0
  38. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/parallel.py +0 -0
  39. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/plugins.py +0 -0
  40. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/privacy.py +0 -0
  41. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/py.typed +0 -0
  42. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/reconciliation.py +0 -0
  43. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/record_types.py +0 -0
  44. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/transaction_deduplicator.py +0 -0
  45. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/transaction_models.py +0 -0
  46. {bankstatementparser-0.0.20 → bankstatementparser-0.0.22}/bankstatementparser/zip_security.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: bankstatementparser
3
- Version: 0.0.20
3
+ Version: 0.0.22
4
4
  Summary: BankStatementParser is your essential tool for easy bank statement management. Designed with finance and treasury experts in mind, it offers a simple way to handle CAMT (ISO 20022) formats and more. Get quick, accurate insights from your financial data and spend less time on processing. It's the smart, hassle-free way to stay on top of your transactions.
5
5
  License: Apache-2.0 OR MIT
6
6
  License-File: LICENSE
@@ -16,7 +16,6 @@ Classifier: Programming Language :: Python :: 3.11
16
16
  Classifier: Programming Language :: Python :: 3.12
17
17
  Classifier: Programming Language :: Python :: 3.13
18
18
  Classifier: Programming Language :: Python :: 3.14
19
- Classifier: Programming Language :: Python :: 3.15
20
19
  Provides-Extra: api
21
20
  Provides-Extra: enrichment
22
21
  Provides-Extra: excel
@@ -64,7 +63,7 @@ Description-Content-Type: text/markdown
64
63
 
65
64
  <p align="center">
66
65
  <a href="https://github.com/sebastienrousseau/bankstatementparser/actions"><img src="https://img.shields.io/github/actions/workflow/status/sebastienrousseau/bankstatementparser/quality-gates.yml?style=for-the-badge&logo=github" alt="Build" /></a>
67
- <a href="https://pypi.org/project/bankstatementparser/"><img src="https://img.shields.io/pypi/pyversions/bankstatementparser.svg?style=for-the-badge&v=0.0.20" alt="PyPI" /></a>
66
+ <a href="https://pypi.org/project/bankstatementparser/"><img src="https://img.shields.io/pypi/pyversions/bankstatementparser.svg?style=for-the-badge&v=0.0.22" alt="PyPI" /></a>
68
67
  <a href="https://pypi.org/project/bankstatementparser/"><img src="https://img.shields.io/pypi/dm/bankstatementparser.svg?style=for-the-badge" alt="PyPI Downloads" /></a>
69
68
  <a href="https://codecov.io/github/sebastienrousseau/bankstatementparser?branch=main"><img src="https://img.shields.io/codecov/c/github/sebastienrousseau/bankstatementparser?style=for-the-badge" alt="Codecov" /></a>
70
69
  <a href="LICENSE"><img src="https://img.shields.io/github/license/sebastienrousseau/bankstatementparser?style=for-the-badge" alt="License" /></a>
@@ -390,7 +389,7 @@ for the full surface.
390
389
  |---|---|
391
390
  | **PII redaction** | Names, IBANs, and addresses masked by default — opt in with `--show-pii` |
392
391
  | **Secure ZIP** | `iter_secure_xml_entries()` rejects zip bombs, encrypted entries, and suspicious compression ratios |
393
- | **Tested** | 1007 tests, coverage gated at 100% in CI, property-based fuzzing with Hypothesis |
392
+ | **Tested** | 1014 tests, coverage gated at 100% in CI, property-based fuzzing with Hypothesis |
394
393
 
395
394
  ---
396
395
 
@@ -758,7 +757,7 @@ bankstatementparser/api.py REST API microservice (FastAPI)
758
757
  docs/compliance/ ISO 13485 validation, risk register, traceability matrix
759
758
  examples/ 14 deterministic + 9 hybrid runnable example scripts
760
759
  scripts/ SBOM generation, checksums, signature verification
761
- tests/ 1007 tests (unit, integration, property-based, security, hybrid mocks)
760
+ tests/ 1014 tests (unit, integration, property-based, security, hybrid mocks)
762
761
  ```
763
762
 
764
763
  ---
@@ -19,7 +19,7 @@
19
19
 
20
20
  <p align="center">
21
21
  <a href="https://github.com/sebastienrousseau/bankstatementparser/actions"><img src="https://img.shields.io/github/actions/workflow/status/sebastienrousseau/bankstatementparser/quality-gates.yml?style=for-the-badge&logo=github" alt="Build" /></a>
22
- <a href="https://pypi.org/project/bankstatementparser/"><img src="https://img.shields.io/pypi/pyversions/bankstatementparser.svg?style=for-the-badge&v=0.0.20" alt="PyPI" /></a>
22
+ <a href="https://pypi.org/project/bankstatementparser/"><img src="https://img.shields.io/pypi/pyversions/bankstatementparser.svg?style=for-the-badge&v=0.0.22" alt="PyPI" /></a>
23
23
  <a href="https://pypi.org/project/bankstatementparser/"><img src="https://img.shields.io/pypi/dm/bankstatementparser.svg?style=for-the-badge" alt="PyPI Downloads" /></a>
24
24
  <a href="https://codecov.io/github/sebastienrousseau/bankstatementparser?branch=main"><img src="https://img.shields.io/codecov/c/github/sebastienrousseau/bankstatementparser?style=for-the-badge" alt="Codecov" /></a>
25
25
  <a href="LICENSE"><img src="https://img.shields.io/github/license/sebastienrousseau/bankstatementparser?style=for-the-badge" alt="License" /></a>
@@ -345,7 +345,7 @@ for the full surface.
345
345
  |---|---|
346
346
  | **PII redaction** | Names, IBANs, and addresses masked by default — opt in with `--show-pii` |
347
347
  | **Secure ZIP** | `iter_secure_xml_entries()` rejects zip bombs, encrypted entries, and suspicious compression ratios |
348
- | **Tested** | 1007 tests, coverage gated at 100% in CI, property-based fuzzing with Hypothesis |
348
+ | **Tested** | 1014 tests, coverage gated at 100% in CI, property-based fuzzing with Hypothesis |
349
349
 
350
350
  ---
351
351
 
@@ -713,7 +713,7 @@ bankstatementparser/api.py REST API microservice (FastAPI)
713
713
  docs/compliance/ ISO 13485 validation, risk register, traceability matrix
714
714
  examples/ 14 deterministic + 9 hybrid runnable example scripts
715
715
  scripts/ SBOM generation, checksums, signature verification
716
- tests/ 1007 tests (unit, integration, property-based, security, hybrid mocks)
716
+ tests/ 1014 tests (unit, integration, property-based, security, hybrid mocks)
717
717
  ```
718
718
 
719
719
  ---
@@ -91,7 +91,7 @@ from .zip_security import (
91
91
  #: so the conformance gate can compare it statically against
92
92
  #: pyproject.toml -- that comparison is the thing that catches drift, and
93
93
  #: a runtime lookup would make the two trivially equal and check nothing.
94
- __version__ = "0.0.20"
94
+ __version__ = "0.0.22"
95
95
 
96
96
  __all__ = [
97
97
  "AnalyticsReport",
@@ -28,12 +28,43 @@ from typing import TYPE_CHECKING, Union, cast
28
28
  import pandas as pd
29
29
 
30
30
  from .exceptions import ExportError
31
+ from .input_validator import InputValidator
31
32
  from .privacy import redact_record
32
33
  from .record_types import SummaryRecord
33
34
 
34
35
  if TYPE_CHECKING:
35
36
  import polars as pl
36
37
 
38
+ FORMULA_PREFIXES = ("=", "@", "\t", "\r")
39
+
40
+
41
+ def _is_safe_numeric_string(text: str) -> bool:
42
+ """Return True if text parses as a signed or unsigned number."""
43
+ try:
44
+ float(text)
45
+ return True
46
+ except ValueError:
47
+ return False
48
+
49
+
50
+ def sanitize_csv_cell(value: object) -> object:
51
+ """Neutralize spreadsheet formula triggers (CWE-1236)."""
52
+ if not isinstance(value, str):
53
+ return value
54
+ if value.startswith(FORMULA_PREFIXES):
55
+ return f"'{value}"
56
+ if value.startswith(("+", "-")) and not _is_safe_numeric_string(value):
57
+ return f"'{value}"
58
+ return value
59
+
60
+
61
+ def sanitize_dataframe_for_csv(df: pd.DataFrame) -> pd.DataFrame:
62
+ """Return a DataFrame copy with formula triggers neutralized for CSV export."""
63
+ sanitized = df.copy()
64
+ for col in sanitized.columns:
65
+ sanitized[col] = sanitized[col].map(sanitize_csv_cell)
66
+ return sanitized
67
+
37
68
 
38
69
  def _single_summary(summaries: list[SummaryRecord]) -> SummaryRecord:
39
70
  """Return one scope, rejecting implicit selection or cross-scope totals."""
@@ -111,9 +142,16 @@ class BankStatementParser(ABC):
111
142
  redact_pii: Mask identities, narratives and provenance fields.
112
143
 
113
144
  Raises:
114
- IOError: If file cannot be written.
145
+ ExportError: If file cannot be written or path is invalid.
115
146
  """
116
- temp_path = Path(f"{output_path}.tmp")
147
+ try:
148
+ validated_path = InputValidator().validate_output_file_path(
149
+ str(output_path)
150
+ )
151
+ except Exception as exc:
152
+ raise ExportError(f"Failed to export CSV: {exc}") from exc
153
+
154
+ temp_path = Path(f"{validated_path}.tmp")
117
155
  try:
118
156
  df = self.parse()
119
157
  if redact_pii:
@@ -121,10 +159,11 @@ class BankStatementParser(ABC):
121
159
  [redact_record(row) for row in df.to_dict("records")],
122
160
  columns=df.columns,
123
161
  )
162
+ df = sanitize_dataframe_for_csv(df)
124
163
  df.to_csv(temp_path, index=False)
125
164
 
126
165
  # Atomic rename to prevent corruption
127
- temp_path.replace(output_path)
166
+ temp_path.replace(validated_path)
128
167
  except Exception as exc:
129
168
  # Clean up temp file if it exists
130
169
  if temp_path.exists():
@@ -141,9 +180,16 @@ class BankStatementParser(ABC):
141
180
  redact_pii: Mask records and summaries using the common policy.
142
181
 
143
182
  Raises:
144
- IOError: If file cannot be written.
183
+ ExportError: If file cannot be written or path is invalid.
145
184
  """
146
- temp_path = Path(f"{output_path}.tmp")
185
+ try:
186
+ validated_path = InputValidator().validate_output_file_path(
187
+ str(output_path)
188
+ )
189
+ except Exception as exc:
190
+ raise ExportError(f"Failed to export JSON: {exc}") from exc
191
+
192
+ temp_path = Path(f"{validated_path}.tmp")
147
193
  try:
148
194
  df = self.parse()
149
195
 
@@ -166,7 +212,7 @@ class BankStatementParser(ABC):
166
212
  json.dump(data, f, indent=2, default=str)
167
213
 
168
214
  # Atomic rename to prevent corruption
169
- temp_path.replace(output_path)
215
+ temp_path.replace(validated_path)
170
216
  except Exception as exc:
171
217
  # Clean up temp file if it exists
172
218
  if temp_path.exists():
@@ -36,6 +36,7 @@ from bankstatementparser.input_validator import (
36
36
  ValidationError,
37
37
  )
38
38
 
39
+ from .base_parser import sanitize_dataframe_for_csv
39
40
  from .privacy import redact_record
40
41
  from .record_types import PaymentRecord, TransactionRecord
41
42
 
@@ -329,6 +330,7 @@ class BankStatementCLI:
329
330
  safe_output_path = str(output_path.parent / safe_name)
330
331
  if not show_pii:
331
332
  data_df = self._redact_dataframe(data_df)
333
+ data_df = sanitize_dataframe_for_csv(data_df)
332
334
  data_df.to_csv(safe_output_path, index=False)
333
335
  print(f"Parsed data saved to {safe_output_path}")
334
336
  else:
@@ -425,6 +427,7 @@ class BankStatementCLI:
425
427
  if output_path:
426
428
  safe_name = self.validator.get_safe_filename(output_path.name)
427
429
  safe_output_path = str(output_path.parent / safe_name)
430
+ df = sanitize_dataframe_for_csv(df)
428
431
  df.to_csv(safe_output_path, index=False)
429
432
  print(
430
433
  f"Ingested {len(rows)} transactions "
@@ -35,10 +35,32 @@ No external dependencies — both functions produce plain strings.
35
35
 
36
36
  from __future__ import annotations
37
37
 
38
+ import re
38
39
  from collections.abc import Iterable
39
40
 
40
41
  from ..transaction_models import Transaction
41
42
 
43
+ _LEDGER_ACCOUNT_DISALLOWED = re.compile(r"[\r\n\t;\"#]+")
44
+ _LEDGER_CURRENCY_RE = re.compile(r"^[A-Za-z0-9_]{1,10}$")
45
+
46
+
47
+ def _sanitize_account(
48
+ account: str, fallback: str = "Expenses:Uncategorized"
49
+ ) -> str:
50
+ """Sanitize account name to prevent plaintext accounting syntax injection."""
51
+ cleaned = _LEDGER_ACCOUNT_DISALLOWED.sub(" ", account).strip()
52
+ return cleaned or fallback
53
+
54
+
55
+ def _sanitize_currency(currency: str, fallback: str = "EUR") -> str:
56
+ """Sanitize currency symbol to prevent newline injection in postings."""
57
+ cleaned = (
58
+ currency.strip().replace("\r", "").replace("\n", "").replace("\t", "")
59
+ )
60
+ if _LEDGER_CURRENCY_RE.match(cleaned):
61
+ return cleaned
62
+ return fallback
63
+
42
64
 
43
65
  def to_hledger(
44
66
  transactions: Iterable[Transaction],
@@ -74,8 +96,15 @@ def to_hledger(
74
96
  ``.ledger`` or ``.journal`` file.
75
97
  """
76
98
  lines: list[str] = []
77
- if redact_pii:
78
- account = "Assets:Redacted"
99
+ account = (
100
+ "Assets:Redacted"
101
+ if redact_pii
102
+ else _sanitize_account(account, "Assets:Bank:Checking")
103
+ )
104
+ contra_account = _sanitize_account(
105
+ contra_account, "Expenses:Uncategorized"
106
+ )
107
+ clean_default_currency = _sanitize_currency(default_currency, "EUR")
79
108
  for tx in transactions:
80
109
  date = (
81
110
  tx.booking_date.isoformat()
@@ -87,7 +116,9 @@ def to_hledger(
87
116
  if redact_pii
88
117
  else _escape_description(tx.description or "Unknown")
89
118
  )
90
- currency = tx.currency or default_currency
119
+ currency = _sanitize_currency(
120
+ tx.currency or clean_default_currency, clean_default_currency
121
+ )
91
122
  amount = format(tx.amount.normalize(), "f")
92
123
 
93
124
  contra = (
@@ -133,8 +164,15 @@ def to_beancount(
133
164
  A string containing the full journal.
134
165
  """
135
166
  lines: list[str] = []
136
- if redact_pii:
137
- account = "Assets:Redacted"
167
+ account = (
168
+ "Assets:Redacted"
169
+ if redact_pii
170
+ else _sanitize_account(account, "Assets:Bank:Checking")
171
+ )
172
+ contra_account = _sanitize_account(
173
+ contra_account, "Expenses:Uncategorized"
174
+ )
175
+ clean_default_currency = _sanitize_currency(default_currency, "EUR")
138
176
  for tx in transactions:
139
177
  date = (
140
178
  tx.booking_date.isoformat()
@@ -151,7 +189,9 @@ def to_beancount(
151
189
  if redact_pii
152
190
  else _escape_beancount_string(tx.description or "Unknown")
153
191
  )
154
- currency = tx.currency or default_currency
192
+ currency = _sanitize_currency(
193
+ tx.currency or clean_default_currency, clean_default_currency
194
+ )
155
195
  amount = format(tx.amount.normalize(), "f")
156
196
  neg_amount = format((-tx.amount).normalize(), "f")
157
197
 
@@ -174,8 +214,10 @@ def _resolve_contra(tx: Transaction, default: str) -> str:
174
214
  category = tx.category
175
215
  if category:
176
216
  safe = category.replace(" ", ":").replace("/", ":")
177
- return f"Expenses:{safe}"
178
- return default
217
+ safe = _LEDGER_ACCOUNT_DISALLOWED.sub("", safe).strip()
218
+ if safe:
219
+ return f"Expenses:{safe}"
220
+ return _sanitize_account(default)
179
221
 
180
222
 
181
223
  def _escape_description(value: str) -> str:
@@ -15,6 +15,7 @@ from typing import Any
15
15
 
16
16
  import pandas as pd
17
17
 
18
+ from ..input_validator import InputValidator
18
19
  from ..privacy import redact_record
19
20
 
20
21
 
@@ -36,6 +37,11 @@ def export_parquet(
36
37
  Returns:
37
38
  Parquet file contents as bytes.
38
39
  """
40
+ validated_destination = (
41
+ InputValidator().validate_output_file_path(str(output_path))
42
+ if output_path is not None
43
+ else None
44
+ )
39
45
  if isinstance(transactions, pd.DataFrame):
40
46
  df = transactions.copy()
41
47
  else:
@@ -78,10 +84,8 @@ def export_parquet(
78
84
  ) from exc
79
85
  parquet_bytes = buf.getvalue()
80
86
 
81
- if output_path is not None:
82
- p = Path(output_path)
83
- p.parent.mkdir(parents=True, exist_ok=True)
84
- p.write_bytes(parquet_bytes)
87
+ if validated_destination is not None:
88
+ validated_destination.write_bytes(parquet_bytes)
85
89
 
86
90
  return parquet_bytes
87
91
 
@@ -121,8 +125,7 @@ def export_parquet_stream(
121
125
  if len(names) != len(schema.names):
122
126
  raise ValueError("Parquet schema field names must be unique")
123
127
  required = [field.name for field in schema if not field.nullable]
124
- destination = Path(output_path)
125
- destination.parent.mkdir(parents=True, exist_ok=True)
128
+ destination = InputValidator().validate_output_file_path(str(output_path))
126
129
  with tempfile.NamedTemporaryFile(
127
130
  dir=destination.parent,
128
131
  prefix=".bsp_",
@@ -46,7 +46,9 @@ from __future__ import annotations
46
46
 
47
47
  import base64
48
48
  import os
49
+ from ipaddress import ip_address
49
50
  from typing import Any
51
+ from urllib.parse import urlparse
50
52
 
51
53
  from .._llm_common import (
52
54
  DEFAULT_API_BASE,
@@ -56,11 +58,41 @@ from .._llm_common import (
56
58
  OLLAMA_PREFIX_RE as _PROVIDER_PREFIX_RE,
57
59
  )
58
60
 
61
+ _BLOCKED_METADATA_HOSTS = {
62
+ "169.254.169.254",
63
+ "metadata.google.internal",
64
+ "instance-data",
65
+ }
66
+
59
67
 
60
68
  class OllamaDirectError(RuntimeError):
61
69
  """Raised when the direct Ollama call fails or returns garbage."""
62
70
 
63
71
 
72
+ def _validate_api_base(api_base: str) -> None:
73
+ """Validate api_base URL to mitigate SSRF vectors."""
74
+ parsed = urlparse(api_base)
75
+ if parsed.scheme not in ("http", "https"):
76
+ raise OllamaDirectError(
77
+ f"Invalid api_base scheme {parsed.scheme!r}; must be http or https"
78
+ )
79
+ host = (parsed.hostname or "").lower()
80
+ if not host:
81
+ raise OllamaDirectError("api_base must include a valid hostname")
82
+ if host in _BLOCKED_METADATA_HOSTS:
83
+ raise OllamaDirectError(
84
+ f"Access to cloud metadata endpoint {host!r} is blocked"
85
+ )
86
+ try:
87
+ ip = ip_address(host)
88
+ if ip.is_link_local:
89
+ raise OllamaDirectError(
90
+ f"Access to link-local address {host!r} is blocked"
91
+ )
92
+ except ValueError:
93
+ pass
94
+
95
+
64
96
  def ollama_direct_completion(**kwargs: Any) -> dict[str, Any]:
65
97
  """Translate a LiteLLM-style call into a direct Ollama ``/api/chat`` call.
66
98
 
@@ -112,6 +144,7 @@ def ollama_direct_completion(**kwargs: Any) -> dict[str, Any]:
112
144
  or os.environ.get(ENV_API_BASE)
113
145
  or DEFAULT_API_BASE
114
146
  )
147
+ _validate_api_base(api_base)
115
148
  timeout = float(kwargs.get("timeout", 300.0))
116
149
  temperature = float(kwargs.get("temperature", 0.0))
117
150
 
@@ -74,6 +74,8 @@ class InputValidator:
74
74
  ".XLS",
75
75
  ".json",
76
76
  ".JSON",
77
+ ".parquet",
78
+ ".PARQUET",
77
79
  }
78
80
 
79
81
  # Dangerous path patterns to block
@@ -441,6 +443,58 @@ class InputValidator:
441
443
  f"XML content is too large ({size_mb:.1f}MB). Maximum allowed: {max_mb:.1f}MB"
442
444
  )
443
445
 
446
+ def _read_file_header(self, path: Path) -> bytes:
447
+ """Read the initial bytes of a file for format detection."""
448
+ try:
449
+ with open(path, "rb") as f:
450
+ return f.read(1024)
451
+ except (OSError, UnicodeDecodeError) as e:
452
+ raise ValidationError(
453
+ f"Cannot read file for format validation: {e}"
454
+ ) from e
455
+
456
+ def _validate_pdf_format(self, path: Path) -> None:
457
+ """Validate that a PDF input begins with the PDF magic signature."""
458
+ header = self._read_file_header(path)
459
+ if not header.startswith(b"%PDF"):
460
+ raise ValidationError(f"File is not a valid PDF document: {path}")
461
+
462
+ def _check_binary_signatures(self, header: bytes, path: Path) -> None:
463
+ """Check for known binary signatures that indicate invalid text formats."""
464
+ binary_signatures = [
465
+ b"\x89PNG",
466
+ b"GIF8",
467
+ b"\xff\xd8\xff",
468
+ b"PK",
469
+ b"\x7fELF",
470
+ b"MZ",
471
+ b"\x00\x00\x01\x00",
472
+ b"%PDF",
473
+ ]
474
+ for sig in binary_signatures:
475
+ if header[: len(sig)] == sig:
476
+ raise ValidationError(
477
+ f"File appears to contain binary data, expected XML: {path}"
478
+ )
479
+
480
+ def _check_xml_indicators(self, header: bytes, path: Path) -> None:
481
+ """Check for XML declaration or common root namespace indicators."""
482
+ header_str = header.decode("utf-8", errors="ignore").lower()
483
+ xml_indicators = [
484
+ "<?xml",
485
+ "<document",
486
+ "xmlns",
487
+ "camt.053",
488
+ "pain.001",
489
+ "iso:std:iso:20022",
490
+ ]
491
+ if not any(ind in header_str for ind in xml_indicators):
492
+ if any(c < 32 and c not in (9, 10, 13) for c in header[:100]):
493
+ raise ValidationError(
494
+ f"File appears to contain binary data, expected XML: {path}"
495
+ )
496
+ logger.warning(f"File may not be a valid XML document: {path}")
497
+
444
498
  def _validate_input_format(self, path: Path) -> None:
445
499
  """Validate input file format by checking file content.
446
500
 
@@ -450,84 +504,31 @@ class InputValidator:
450
504
  Raises:
451
505
  ValidationError: If format validation fails.
452
506
  """
453
- try:
454
- # Check MIME type
455
- mime_type, _ = mimetypes.guess_type(str(path))
456
- if mime_type and not any(
457
- xml_type in mime_type for xml_type in ["xml", "text"]
458
- ):
459
- logger.warning(
460
- f"Unexpected MIME type '{mime_type}' for file: {path}"
461
- )
462
-
463
- # Read first few bytes to check for XML declaration
464
- with open(path, "rb") as f:
465
- header = f.read(1024) # Read first 1KB
466
-
467
- # Check for known binary file signatures (magic bytes)
468
- binary_signatures = [
469
- b"\x89PNG", # PNG
470
- b"GIF8", # GIF
471
- b"\xff\xd8\xff", # JPEG
472
- b"PK", # ZIP/XLSX/DOCX
473
- b"\x7fELF", # ELF executable
474
- b"MZ", # Windows executable
475
- b"\x00\x00\x01\x00", # ICO
476
- b"%PDF", # PDF
477
- ]
478
- for sig in binary_signatures:
479
- if header[: len(sig)] == sig:
480
- raise ValidationError(
481
- f"File appears to contain binary data, expected XML: {path}"
482
- )
483
-
484
- # Validate UTF-8 encoding
485
- try:
486
- header.decode("utf-8")
487
- except UnicodeDecodeError as exc:
488
- raise ValidationError(
489
- f"File encoding is not valid UTF-8: {path}"
490
- ) from exc
491
-
492
- if path.suffix.lower() != ".xml":
493
- return
494
-
495
- # Check for XML declaration or root elements
496
- header_str = header.decode("utf-8", errors="ignore").lower()
497
-
498
- # Look for XML indicators
499
- xml_indicators = [
500
- "<?xml",
501
- "<document",
502
- "xmlns",
503
- "camt.053",
504
- "pain.001",
505
- "iso:std:iso:20022",
506
- ]
507
-
508
- has_xml_indicator = any(
509
- indicator in header_str for indicator in xml_indicators
507
+ mime_type, _ = mimetypes.guess_type(str(path))
508
+ if mime_type and not any(
509
+ xml_type in mime_type for xml_type in ["xml", "text"]
510
+ ):
511
+ logger.warning(
512
+ f"Unexpected MIME type '{mime_type}' for file: {path}"
510
513
  )
511
514
 
512
- if not has_xml_indicator:
513
- # Check if it's binary data (control chars other than whitespace)
514
- if any(c < 32 and c not in (9, 10, 13) for c in header[:100]):
515
- raise ValidationError(
516
- f"File appears to contain binary data, expected XML: {path}"
517
- )
518
- else:
519
- logger.warning(
520
- f"File may not be a valid XML document: {path}"
521
- )
515
+ suffix = path.suffix.lower()
516
+ if suffix == ".pdf":
517
+ self._validate_pdf_format(path)
518
+ return
519
+
520
+ header = self._read_file_header(path)
521
+ self._check_binary_signatures(header, path)
522
522
 
523
+ try:
524
+ header.decode("utf-8")
523
525
  except UnicodeDecodeError as exc:
524
526
  raise ValidationError(
525
527
  f"File encoding is not valid UTF-8: {path}"
526
528
  ) from exc
527
- except OSError as e:
528
- raise ValidationError(
529
- f"Cannot read file for format validation: {e}"
530
- ) from e
529
+
530
+ if suffix == ".xml":
531
+ self._check_xml_indicators(header, path)
531
532
 
532
533
  def _validate_xml_bytes_format(
533
534
  self, xml_bytes: bytes, source_name: str
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "bankstatementparser"
3
- version = "0.0.20"
3
+ version = "0.0.22"
4
4
  description = "BankStatementParser is your essential tool for easy bank statement management. Designed with finance and treasury experts in mind, it offers a simple way to handle CAMT (ISO 20022) formats and more. Get quick, accurate insights from your financial data and spend less time on processing. It's the smart, hassle-free way to stay on top of your transactions."
5
5
  authors = ["Sebastien Rousseau <sebastian.rousseau@gmail.com>"]
6
6
  license = "Apache-2.0 OR MIT"