Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions FINANCIAL_LEDGER.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ The resolver receives the credential supplied with the request and must validate

## Local HTTP API

`banking_api.py` provides a threaded HTTP server for the application service. It binds to `127.0.0.1` by default:
`banking_api.py` provides a threaded HTTP server for the application service. It binds only to an IPv4 loopback address (default `127.0.0.1`); non-loopback host addresses are rejected:

```python
from banking_api import create_server
Expand All @@ -125,4 +125,4 @@ Every supported route requires an `Authorization: Bearer <credential>` header. T
| GET | `/accounts/{account_id}/statement?limit=100` | Read an owner's account statement |
| POST | `/transfers` | Transfer integer minor units between internal accounts |

JSON request bodies are limited to 64 KiB. The API does not expose arbitrary journal posting, deposits, withdrawals, card authorization, or external payment settlement. It has no built-in TLS termination, rate limiting, or production credential-validation provider. Keep it on loopback until the actual JaMail credential validator and an authenticated TLS reverse proxy are configured. Credential-source outages return HTTP 503; invalid credentials return HTTP 401.
JSON request bodies are limited to 64 KiB. The API does not expose arbitrary journal posting, deposits, withdrawals, card authorization, or external payment settlement. It has no built-in TLS termination, rate limiting, or production credential-validation provider. Keep it on loopback; remote access requires a separately designed authenticated TLS gateway. Credential-source outages return HTTP 503; invalid credentials return HTTP 401.
13 changes: 12 additions & 1 deletion banking_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""
from __future__ import annotations

import ipaddress
import json
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from urllib.parse import parse_qs, unquote, urlsplit
Expand Down Expand Up @@ -185,9 +186,19 @@ def log_message(self, format, *args):


def create_server(service: BankingService, host="127.0.0.1", port=8000):
"""Create a threaded API server; remote exposure requires a TLS proxy."""
"""Create a threaded API server bound only to an IPv4 loopback address."""
if not isinstance(service, BankingService):
raise TypeError("service must be a BankingService")
if not isinstance(host, str):
raise TypeError("host must be an IPv4 loopback address")
try:
address = ipaddress.ip_address(host)
except ValueError as exc:
raise ValueError("banking API host must be an IPv4 loopback address") from exc
if not isinstance(address, ipaddress.IPv4Address) or not address.is_loopback:
raise ValueError("banking API host must be an IPv4 loopback address")
if not isinstance(port, int) or isinstance(port, bool) or not 0 <= port <= 65535:
raise ValueError("port must be an integer from 0 through 65535")
handler = type("ConfiguredBankingAPIHandler", (BankingAPIHandler,), {
"service": service,
})
Expand Down
18 changes: 16 additions & 2 deletions tests/test_banking_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def setUp(self):
(self.owner_a, 5000), (self.clearing, -5000),
])
account_owners = {"token-a": "owner-a", "token-b": "owner-b"}
service = BankingService(self.ledger, account_owners.get)
self.server = create_server(service, host="127.0.0.1", port=0)
self.service = BankingService(self.ledger, account_owners.get)
self.server = create_server(self.service, host="127.0.0.1", port=0)
self.thread = threading.Thread(target=self.server.serve_forever, daemon=True)
self.thread.start()
self.base_url = "http://127.0.0.1:" + str(self.server.server_address[1])
Expand Down Expand Up @@ -163,6 +163,20 @@ def unavailable(credential):
outage_server.server_close()
thread.join(timeout=2)

def test_server_refuses_non_loopback_bind_addresses(self):
for host in ("0.0.0.0", "192.0.2.10", "::1", "localhost"):
with self.subTest(host=host), self.assertRaisesRegex(
ValueError, "IPv4 loopback"
):
create_server(self.service, host=host, port=0)

def test_server_rejects_invalid_port_values(self):
for port in (-1, 65536, True, "8000"):
with self.subTest(port=port), self.assertRaisesRegex(
ValueError, "port must be an integer"
):
create_server(self.service, host="127.0.0.1", port=port)

def test_invalid_credential_and_unsupported_method_are_rejected(self):
status, body = self.request("GET", "/accounts", "invalid-token")
self.assertEqual(status, 401)
Expand Down
Loading