Compare commits
47 Commits
6690343aad
...
b8d87ecabd
| Author | SHA1 | Date | |
|---|---|---|---|
| b8d87ecabd | |||
| 457c143954 | |||
| ccadb14254 | |||
| ed85d5a8ea | |||
| f8b1ee167f | |||
| 66a1273e2c | |||
| e7262a2cae | |||
| 7be0ef3578 | |||
| b277d894dd | |||
| 90ae3b6510 | |||
| a20cc1cf7c | |||
| 80537b548f | |||
| 01ac26e7be | |||
| 4b9ae596ce | |||
| c021e3d4cc | |||
| 0a3bd34ecb | |||
| 59774c0cbb | |||
| c47862a72f | |||
| 3f7608641d | |||
| 6cf85d4f63 | |||
| bd06140c1f | |||
| cc39027590 | |||
| 5822d3e231 | |||
| 4ed58b11c5 | |||
| 5fbfa79c99 | |||
| 94de433926 | |||
| 84d4522ce3 | |||
| 5a2fc9c497 | |||
| f28fad0194 | |||
| 53be95113e | |||
| 1e813e1a75 | |||
| 2d7b767138 | |||
| b39397e742 | |||
| 3cfb635da1 | |||
| cdc2d2d4c7 | |||
| 40fa23b03b | |||
| 3c4608bdb4 | |||
| 94f64c6334 | |||
| 2516fe5f61 | |||
| b0fd59e431 | |||
| 24809b19e2 | |||
| 431929d37c | |||
| abb882bd69 | |||
| 604ce3ebed | |||
| 06bec057c7 | |||
| 9e2acb78ac | |||
| 9f308680bb |
11
.env.example
11
.env.example
@@ -3,8 +3,7 @@
|
|||||||
# DO NOT commit the real token to git!
|
# DO NOT commit the real token to git!
|
||||||
# This file is in .gitignore and should not be committed to git
|
# This file is in .gitignore and should not be committed to git
|
||||||
GITEA_API_TOKEN=your_gitea_api_token_here
|
GITEA_API_TOKEN=your_gitea_api_token_here
|
||||||
# Example: GITEA_API_TOKEN=5cfa8c33e337ebaadb355c0ffa2d053d521ee43b
|
# If a token was ever committed, revoke it and set a new one via the environment.
|
||||||
# (replace with your actual token after revoking the old one)
|
|
||||||
|
|
||||||
# === bal-server ===
|
# === bal-server ===
|
||||||
# !!! WARNING: Never bind to 0.0.0.0 in production. Use 127.0.0.1 and place
|
# !!! WARNING: Never bind to 0.0.0.0 in production. Use 127.0.0.1 and place
|
||||||
@@ -33,8 +32,14 @@ BAL_SERVER_ACTIX_DEFAULT_BURST=100
|
|||||||
BAL_SERVER_ACTIX_WORKERS=4
|
BAL_SERVER_ACTIX_WORKERS=4
|
||||||
BAL_SERVER_ACTIX_MAX_CONNECTIONS=100
|
BAL_SERVER_ACTIX_MAX_CONNECTIONS=100
|
||||||
|
|
||||||
|
# === PostgreSQL (optional; used by pg-server.sh) ===
|
||||||
|
# pg-server.sh requires BAL_PG_PASSWORD to be set; never commit a real password.
|
||||||
|
BAL_PG_PASSWORD=change_me
|
||||||
|
BAL_PG_USER=bal
|
||||||
|
BAL_PG_DB=baldb
|
||||||
|
|
||||||
# === bal-pusher ===
|
# === bal-pusher ===
|
||||||
BAL_PUSHER_DB_FILE=/var/bal/bal.db
|
BAL_SERVER_DB_FILE=/var/bal/bal.db
|
||||||
BAL_PUSHER_BITCOIN_DIR=/home/bal/.bitcoin
|
BAL_PUSHER_BITCOIN_DIR=/home/bal/.bitcoin
|
||||||
BAL_PUSHER_SEND_STATS=false
|
BAL_PUSHER_SEND_STATS=false
|
||||||
BAL_SERVER_URL=http://127.0.0.1:9137
|
BAL_SERVER_URL=http://127.0.0.1:9137
|
||||||
|
|||||||
8
.gitignore
vendored
8
.gitignore
vendored
@@ -1,10 +1,18 @@
|
|||||||
# Secrets and environment files - NEVER commit
|
# Secrets and environment files - NEVER commit
|
||||||
.env
|
.env
|
||||||
.env.*
|
.env.*
|
||||||
|
.env.local
|
||||||
|
*.env.local
|
||||||
|
.env.production
|
||||||
|
.env.secret
|
||||||
!.env.example
|
!.env.example
|
||||||
*.pem
|
*.pem
|
||||||
|
private_key.pem
|
||||||
|
privkey.pem
|
||||||
!public_key.pem
|
!public_key.pem
|
||||||
*.key
|
*.key
|
||||||
|
ec.key
|
||||||
|
chiave_privata.key
|
||||||
|
|
||||||
# Databases
|
# Databases
|
||||||
*.db
|
*.db
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ serde = { version = "1.0.152", features = ["derive"] }
|
|||||||
serde_json = { version = "1.0.116" }
|
serde_json = { version = "1.0.116" }
|
||||||
sqlx = { version = "0.8", features = ["runtime-tokio", "sqlite", "postgres", "chrono"] }
|
sqlx = { version = "0.8", features = ["runtime-tokio", "sqlite", "postgres", "chrono"] }
|
||||||
regex = { version = "1.10.4" }
|
regex = { version = "1.10.4" }
|
||||||
tokio = { version = "1", features = ["rt", "net","macros","rt-multi-thread"] }
|
tokio = { version = "1", features = ["rt", "net","macros","rt-multi-thread","time"] }
|
||||||
url = { version = "2" }
|
url = { version = "2" }
|
||||||
|
|
||||||
# server-only
|
# server-only
|
||||||
|
|||||||
@@ -82,7 +82,6 @@ ENV RUST_LOG=info \
|
|||||||
BAL_SERVER_BIND_ADDRESS=127.0.0.1 \
|
BAL_SERVER_BIND_ADDRESS=127.0.0.1 \
|
||||||
BAL_SERVER_BIND_PORT=9137 \
|
BAL_SERVER_BIND_PORT=9137 \
|
||||||
BAL_SERVER_DB_FILE=/var/bal/bal.db \
|
BAL_SERVER_DB_FILE=/var/bal/bal.db \
|
||||||
BAL_PUSHER_DB_FILE=/var/bal/bal.db \
|
|
||||||
BAL_SERVER_URL=http://127.0.0.1:9137 \
|
BAL_SERVER_URL=http://127.0.0.1:9137 \
|
||||||
BAL_SERVER_PUB_KEY_PATH=/var/bal/public_key.pem \
|
BAL_SERVER_PUB_KEY_PATH=/var/bal/public_key.pem \
|
||||||
SSL_KEY_PATH=/var/bal/private_key.pem
|
SSL_KEY_PATH=/var/bal/private_key.pem
|
||||||
|
|||||||
@@ -90,7 +90,6 @@ ENV RUST_LOG=info \
|
|||||||
BAL_SERVER_BIND_ADDRESS=127.0.0.1 \
|
BAL_SERVER_BIND_ADDRESS=127.0.0.1 \
|
||||||
BAL_SERVER_BIND_PORT=9137 \
|
BAL_SERVER_BIND_PORT=9137 \
|
||||||
BAL_SERVER_DB_FILE=/var/bal/bal.db \
|
BAL_SERVER_DB_FILE=/var/bal/bal.db \
|
||||||
BAL_PUSHER_DB_FILE=/var/bal/bal.db \
|
|
||||||
BAL_SERVER_URL=http://127.0.0.1:9137 \
|
BAL_SERVER_URL=http://127.0.0.1:9137 \
|
||||||
BAL_SERVER_PUB_KEY_PATH=/var/bal/public_key.pem \
|
BAL_SERVER_PUB_KEY_PATH=/var/bal/public_key.pem \
|
||||||
SSL_KEY_PATH=/var/bal/private_key.pem
|
SSL_KEY_PATH=/var/bal/private_key.pem
|
||||||
|
|||||||
@@ -143,7 +143,7 @@ If no network is specified, defaults to `bitcoin`.
|
|||||||
|
|
||||||
| Variable | Description | Default |
|
| Variable | Description | Default |
|
||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
| `BAL_PUSHER_DB_FILE` | Path to the SQLite3 database file. | `bal.db` |
|
| `BAL_SERVER_DB_FILE` | Path to the SQLite3 database file. | `bal.db` |
|
||||||
| `BAL_PUSHER_SEND_STATS` | Send stats to welist server. | `false` |
|
| `BAL_PUSHER_SEND_STATS` | Send stats to welist server. | `false` |
|
||||||
| `BAL_SERVER_URL` | URL of bal-server (for stats reporting). | - |
|
| `BAL_SERVER_URL` | URL of bal-server (for stats reporting). | - |
|
||||||
| `SSL_KEY_PATH` | Ed25519 private key for signing stats reports. | `private_key.pem` |
|
| `SSL_KEY_PATH` | Ed25519 private key for signing stats reports. | `private_key.pem` |
|
||||||
|
|||||||
20
bal-pusher.sh
Normal file → Executable file
20
bal-pusher.sh
Normal file → Executable file
@@ -1,15 +1,29 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||||
|
. "$SCRIPT_DIR/lib.sh"
|
||||||
|
|
||||||
|
WORKING_DIR=$(pwd)
|
||||||
|
|
||||||
export RUST_LOG=trace
|
export RUST_LOG=trace
|
||||||
|
|
||||||
export BAL_PUSHER_DB_FILE="$(pwd)/bal.db"
|
export BAL_SERVER_DB_FILE="$WORKING_DIR/bal.db"
|
||||||
|
export BAL_SERVER_DB_BACKEND="postgresql"
|
||||||
#export BAL_PUSHER_BITCOIN_COOKIE_FILE=/~/.bitcoin/.cookie
|
#export BAL_PUSHER_BITCOIN_COOKIE_FILE=/~/.bitcoin/.cookie
|
||||||
#export BAL_PUSHER_REGTEST_COOKIE_FILE=/~/.bitcoin/regtest/.cookie
|
#export BAL_PUSHER_REGTEST_COOKIE_FILE=/~/.bitcoin/regtest/.cookie
|
||||||
#export BAL_PUSHER_TESTNET_COOKIE_FILE=/~/.bitcoin/testnet3/.cookie
|
#export BAL_PUSHER_TESTNET_COOKIE_FILE=/~/.bitcoin/testnet3/.cookie
|
||||||
#export BAL_PUSHER_SIGNET_COOKIE_FILE=/~/.bitcoin/signet/.cookie
|
#export BAL_PUSHER_SIGNET_COOKIE_FILE=/~/.bitcoin/signet/.cookie
|
||||||
|
|
||||||
export BAL_PUSHER_REGTEST_ZMQ_HASHBLOCK=tcp://127.0.0.1:21332
|
export BAL_PUSHER_REGTEST_ZMQ_HASHBLOCK=tcp://127.0.0.1:21332
|
||||||
export BAL_PUSHER_SEND_STATS=true
|
export BAL_PUSHER_SEND_STATS=true
|
||||||
export WELIST_SERVER_URL=http://localhost:8086
|
export WELIST_SERVER_URL=http://localhost:8086
|
||||||
export WELIST_SKIP_URL_VALIDATION=true
|
export WELIST_SKIP_URL_VALIDATION=true
|
||||||
export BAL_SERVER_URL="http://127.0.0.1:9133"
|
export BAL_SERVER_URL="http://127.0.0.1:9133"
|
||||||
export SSL_KEY_PATH="$(pwd)/private_key.pem"
|
export SSL_KEY_PATH="$WORKING_DIR/private_key.pem"
|
||||||
|
|
||||||
|
if [ "$BAL_SERVER_DB_BACKEND" = "postgresql" ]; then
|
||||||
|
. "$SCRIPT_DIR/pg-server.sh"
|
||||||
|
fi
|
||||||
|
|
||||||
cargo run --bin=bal-pusher regtest --features=pusher
|
cargo run --bin=bal-pusher regtest --features=pusher
|
||||||
|
|||||||
@@ -31,31 +31,7 @@ export BAL_SERVER_REGTEST_FIXED_FEE=1000
|
|||||||
export BAL_SERVER_DB_BACKEND="postgresql"
|
export BAL_SERVER_DB_BACKEND="postgresql"
|
||||||
|
|
||||||
if [ "$BAL_SERVER_DB_BACKEND" = "postgresql" ]; then
|
if [ "$BAL_SERVER_DB_BACKEND" = "postgresql" ]; then
|
||||||
PG_CONTAINER="bal-pg"
|
. "$SCRIPT_DIR/pg-server.sh"
|
||||||
PG_DSN="${BAL_SERVER_PG_DSN:-postgres://bal:balpass@localhost:5432/baldb}"
|
|
||||||
|
|
||||||
docker rm -f "$PG_CONTAINER" 2>/dev/null || true
|
|
||||||
echo_i "Starting PostgreSQL container..."
|
|
||||||
docker run -d --name "$PG_CONTAINER" \
|
|
||||||
-e POSTGRES_USER=bal \
|
|
||||||
-e POSTGRES_PASSWORD=balpass \
|
|
||||||
-e POSTGRES_DB=baldb \
|
|
||||||
-p 5432:5432 postgres:16-alpine
|
|
||||||
|
|
||||||
echo_i "Waiting for PostgreSQL..."
|
|
||||||
until docker exec "$PG_CONTAINER" pg_isready -U bal >/dev/null 2>&1; do
|
|
||||||
sleep 1
|
|
||||||
done
|
|
||||||
echo_i "PostgreSQL ready at $PG_DSN"
|
|
||||||
export BAL_SERVER_PG_DSN="$PG_DSN"
|
|
||||||
|
|
||||||
|
|
||||||
cleanup() {
|
|
||||||
echo_i "Stopping PostgreSQL container..."
|
|
||||||
docker stop "$PG_CONTAINER" >/dev/null 2>&1
|
|
||||||
docker rm "$PG_CONTAINER" >/dev/null 2>&1
|
|
||||||
}
|
|
||||||
trap cleanup EXIT
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
cargo run --bin=bal-server
|
cargo run --bin=bal-server
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ BAL_SERVER_INFO="$info"
|
|||||||
EOF
|
EOF
|
||||||
)
|
)
|
||||||
bal_pusher_conf=$(cat << EOF
|
bal_pusher_conf=$(cat << EOF
|
||||||
BAL_PUSHER_DB_FILE=/home/bal/bal.db
|
BAL_SERVER_DB_FILE=/home/bal/bal.db
|
||||||
BAL_PUSHER_BITCOIN_COOKIE_FILE=/home/bitcoin/.bitcoin/.cookie
|
BAL_PUSHER_BITCOIN_COOKIE_FILE=/home/bitcoin/.bitcoin/.cookie
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
|
|||||||
@@ -201,7 +201,7 @@ This is the async daemon that monitors the blockchain and pushes pending transac
|
|||||||
|
|
||||||
### Configuration
|
### Configuration
|
||||||
All configuration is via environment variables (no config files):
|
All configuration is via environment variables (no config files):
|
||||||
- `BAL_PUSHER_DB_FILE`: Path to SQLite database.
|
- `BAL_SERVER_DB_FILE`: Path to SQLite database.
|
||||||
- `BAL_PUSHER_BITCOIN_DIR`: Bitcoin data directory (for cookie file path).
|
- `BAL_PUSHER_BITCOIN_DIR`: Bitcoin data directory (for cookie file path).
|
||||||
- `BAL_PUSHER_SEND_STATS`: Enable/disable remote stats reporting.
|
- `BAL_PUSHER_SEND_STATS`: Enable/disable remote stats reporting.
|
||||||
- `BAL_SERVER_URL`: URL of the bal-server for internal communication.
|
- `BAL_SERVER_URL`: URL of the bal-server for internal communication.
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
## Database Technology
|
## Database Technology
|
||||||
- **Engine:** `sqlite` (Rust `sqlite` crate, version 0.34.0)
|
- **Engine:** `sqlite` (Rust `sqlite` crate, version 0.34.0)
|
||||||
- **File:** `bal.db` (default, configurable via `BAL_SERVER_DB_FILE` / `BAL_PUSHER_DB_FILE`)
|
- **File:** `bal.db` (default, configurable via `BAL_SERVER_DB_FILE`)
|
||||||
- **Connection Management:** Shared `Arc<Mutex<Connection>>` in `bal-server`, single connection in `bal-pusher`.
|
- **Connection Management:** Shared `Arc<Mutex<Connection>>` in `bal-server`, single connection in `bal-pusher`.
|
||||||
- **WAL Mode:** Enabled via `PRAGMA journal_mode=WAL` with retry logic (up to 5 attempts) for concurrent access safety.
|
- **WAL Mode:** Enabled via `PRAGMA journal_mode=WAL` with retry logic (up to 5 attempts) for concurrent access safety.
|
||||||
- **Busy Timeout:** Set to 5000ms via `PRAGMA busy_timeout=5000`.
|
- **Busy Timeout:** Set to 5000ms via `PRAGMA busy_timeout=5000`.
|
||||||
|
|||||||
@@ -41,25 +41,30 @@ Example: `BAL_SERVER_REGTEST_ADDRESS=tpub...`, `BAL_SERVER_BITCOIN_FIXED_FEE=500
|
|||||||
| `BAL_SERVER_ACTIX_WORKERS` | `4` | Number of actix-web worker threads |
|
| `BAL_SERVER_ACTIX_WORKERS` | `4` | Number of actix-web worker threads |
|
||||||
| `BAL_SERVER_ACTIX_MAX_CONNECTIONS` | `100` | Maximum concurrent connections |
|
| `BAL_SERVER_ACTIX_MAX_CONNECTIONS` | `100` | Maximum concurrent connections |
|
||||||
| `BAL_SERVER_TRUSTED_PROXY` | `127.0.0.1` | Trusted reverse proxy IP for rate-limiting client identification |
|
| `BAL_SERVER_TRUSTED_PROXY` | `127.0.0.1` | Trusted reverse proxy IP for rate-limiting client identification |
|
||||||
| `BAL_SERVER_ACTIX_PUSHTXS_PER_SEC` | `1` | Rate limit: requests per second (applied to all endpoints) |
|
| `BAL_SERVER_ACTIX_PUSHTXS_PER_SEC` | `1` | Rate limit: push txs requests per second |
|
||||||
| `BAL_SERVER_ACTIX_PUSHTXS_BURST` | `3` | Rate limit: burst size (applied to all endpoints) |
|
| `BAL_SERVER_ACTIX_PUSHTXS_BURST` | `3` | Rate limit: push txs burst size |
|
||||||
| `BAL_SERVER_ACTIX_SEARCHTX_PER_SEC` | `5` | Rate limit: searchtx requests per second |
|
| `BAL_SERVER_ACTIX_SEARCHTX_PER_SEC` | `5` | Rate limit: searchtx requests per second |
|
||||||
| `BAL_SERVER_ACTIX_SEARCHTX_BURST` | `10` | Rate limit: searchtx burst size |
|
| `BAL_SERVER_ACTIX_SEARCHTX_BURST` | `10` | Rate limit: searchtx burst size |
|
||||||
| `BAL_SERVER_ACTIX_INFO_PER_SEC` | `20` | Rate limit: info requests per second |
|
| `BAL_SERVER_ACTIX_INFO_PER_SEC` | `20` | Rate limit: info/stats requests per second |
|
||||||
| `BAL_SERVER_ACTIX_INFO_BURST` | `30` | Rate limit: info burst size |
|
| `BAL_SERVER_ACTIX_INFO_BURST` | `30` | Rate limit: info/stats burst size |
|
||||||
| `BAL_SERVER_ACTIX_DEFAULT_PER_SEC` | `50` | Rate limit: default requests per second |
|
| `BAL_SERVER_ACTIX_DEFAULT_PER_SEC` | `50` | Rate limit: default requests per second (`/`, `/.pub_key.pem`, `/version`) |
|
||||||
| `BAL_SERVER_ACTIX_DEFAULT_BURST` | `100` | Rate limit: default burst size |
|
| `BAL_SERVER_ACTIX_DEFAULT_BURST` | `100` | Rate limit: default burst size |
|
||||||
|
|
||||||
|
Each endpoint class has its own independent token bucket, keyed by the real
|
||||||
|
client IP (see `BAL_SERVER_TRUSTED_PROXY`). `BAL_SERVER_ACTIX_TIMEOUT_SECS` is
|
||||||
|
enforced by a request-timeout middleware that returns `408 Request Timeout` if
|
||||||
|
the handler (including request body reading) exceeds the limit.
|
||||||
|
|
||||||
### `bal-pusher` (prefixed `BAL_PUSHER_`)
|
### `bal-pusher` (prefixed `BAL_PUSHER_`)
|
||||||
|
|
||||||
#### Core Settings
|
#### Core Settings
|
||||||
|
|
||||||
| Variable | Default | Description |
|
| Variable | Default | Description |
|
||||||
|----------|---------|-------------|
|
|----------|---------|-------------|
|
||||||
| `BAL_PUSHER_DB_FILE` | `"bal.db"` | Path to the SQLite database file |
|
| `BAL_SERVER_DB_FILE` | `"bal.db"` | Path to the SQLite database file |
|
||||||
| `BAL_PUSHER_BITCOIN_DIR` | `""` | Bitcoin data directory (for cookie file path resolution) |
|
| `BAL_PUSHER_BITCOIN_DIR` | `""` | Bitcoin data directory (for cookie file path resolution) |
|
||||||
| `BAL_PUSHER_SEND_STATS` | `false` | Enable/disable remote stats reporting |
|
| `BAL_PUSHER_SEND_STATS` | `false` | Enable/disable remote stats reporting |
|
||||||
| `BAL_SERVER_URL` | `"http://localhost/"` | URL of the bal-server for internal communication |
|
| `BAL_SERVER_URL` | `"http://127.0.0.1:9133"` | URL of the bal-server for internal communication |
|
||||||
| `SSL_KEY_PATH` | `"privkey.pem"` | Path to Ed25519 private key for signing stats |
|
| `SSL_KEY_PATH` | `"privkey.pem"` | Path to Ed25519 private key for signing stats |
|
||||||
| `BAL_PUSHER_PREFER_IPV6` | `false` | Pin HTTP connection to first IPv6 address (for broken IPv4 routes) |
|
| `BAL_PUSHER_PREFER_IPV6` | `false` | Pin HTTP connection to first IPv6 address (for broken IPv4 routes) |
|
||||||
| `WELIST_SERVER_URL` | `"https://welist.bitcoin-after.life"` | URL to POST signed stats to (validated against SSRF) |
|
| `WELIST_SERVER_URL` | `"https://welist.bitcoin-after.life"` | URL to POST signed stats to (validated against SSRF) |
|
||||||
|
|||||||
@@ -36,7 +36,7 @@
|
|||||||
**Description:** All `unwrap()`/`expect()` calls on critical paths have been replaced with safe error handling.
|
**Description:** All `unwrap()`/`expect()` calls on critical paths have been replaced with safe error handling.
|
||||||
**Mitigation Applied:**
|
**Mitigation Applied:**
|
||||||
- `bal-server`: `from_utf8` returns 400, `sqlite::open` uses `open_db()` with validation, all handlers return proper HTTP status codes.
|
- `bal-server`: `from_utf8` returns 400, `sqlite::open` uses `open_db()` with validation, all handlers return proper HTTP status codes.
|
||||||
- `bal-pusher`: RPC failures log errors + sleep + retry (no panic), ZMQ `recv` uses `set_rcvtimeo(5000)`, `fs::read_to_string` uses `match` + 500, `cfg.lock()` uses `poisoned.into_inner()` recovery.
|
- `bal-pusher`: RPC failures log errors + sleep + retry (no panic), ZMQ `recv` uses `set_rcvtimeo(5000)`, malformed ZMQ frames are handled with `get()`/`first()` and `from_utf8_lossy` (no indexing or UTF-8 `expect`), stats signing returns an error that is logged and skipped rather than `expect`-panicking.
|
||||||
**Regression Tests:** `tests/panic_regression_tests.rs` (2 tests).
|
**Regression Tests:** `tests/panic_regression_tests.rs` (2 tests).
|
||||||
|
|
||||||
### 3. Secret Leakage (FIXED)
|
### 3. Secret Leakage (FIXED)
|
||||||
@@ -52,18 +52,20 @@
|
|||||||
**Description:** All DoS vectors mitigated via actix-web migration.
|
**Description:** All DoS vectors mitigated via actix-web migration.
|
||||||
**Mitigation Applied:**
|
**Mitigation Applied:**
|
||||||
- Body size limit: `PayloadConfig::default().limit(max_body_size)` via `BAL_SERVER_ACTIX_MAX_BODY_SIZE` (default 1 MiB).
|
- Body size limit: `PayloadConfig::default().limit(max_body_size)` via `BAL_SERVER_ACTIX_MAX_BODY_SIZE` (default 1 MiB).
|
||||||
- Rate limiting: `actix-governor` with token-bucket per client IP (`BAL_SERVER_ACTIX_PUSHTXS_PER_SEC`/`BURST`). Uses `RealIpKeyExtractor` to extract real client IP from proxy headers.
|
- Rate limiting: `actix-governor` with a token bucket per client IP, with an **independent bucket for each endpoint class** (push txs, searchtx, info/stats, default) so that polling a cheap endpoint cannot exhaust the push-txs budget. Uses `RealIpKeyExtractor` to extract the real client IP from proxy headers.
|
||||||
- Connection limits: `workers(4)` and `max_connections(100)` via `BAL_SERVER_ACTIX_WORKERS`/`MAX_CONNECTIONS`.
|
- Connection limits: `workers(4)` and `max_connections(100)` via `BAL_SERVER_ACTIX_WORKERS`/`MAX_CONNECTIONS`.
|
||||||
- Body timeout: configurable via `BAL_SERVER_ACTIX_TIMEOUT_SECS`.
|
- Request timeout: `middleware::from_fn` wraps every handler in `tokio::time::timeout`, returning `408 Request Timeout` when `BAL_SERVER_ACTIX_TIMEOUT_SECS` (default 5) elapses, covering slow request bodies.
|
||||||
- ZMQ timeout: `set_rcvtimeo(5000)` prevents infinite blocking.
|
- ZMQ timeout: `set_rcvtimeo(5000)` prevents infinite blocking.
|
||||||
- RPC retry: sleep + retry on connection failure instead of panic.
|
- RPC retry: sleep + retry on connection failure instead of panic.
|
||||||
|
- ZMQ message handling is panic-free: multipart frames are accessed with `get()`/`first()` and topics are decoded with `from_utf8_lossy`, so malformed frames cannot abort the pusher.
|
||||||
|
- Stats-report signing failures are logged and skipped instead of panicking the pusher when the key is unreadable.
|
||||||
|
|
||||||
### 5. SSRF / Network Abuse via `reqwest` (FIXED)
|
### 5. SSRF / Network Abuse via `reqwest` (FIXED)
|
||||||
**Severity:** MEDIUM | **Status:** Fixed
|
**Severity:** MEDIUM | **Status:** Fixed
|
||||||
**Location:** `src/bin/bal-pusher.rs`, `src/validation.rs`
|
**Location:** `src/bin/bal-pusher.rs`, `src/validation.rs`
|
||||||
**Description:** URL validation prevents redirecting requests to internal/private IPs.
|
**Description:** URL validation prevents redirecting requests to internal/private IPs.
|
||||||
**Mitigation Applied:** `is_valid_welist_url()` in `src/validation.rs` blocks localhost, loopback, RFC1918, link-local, multicast, unspecified, and IPv6 unique-local addresses. HTTPS-only scheme enforced.
|
**Mitigation Applied:** `is_valid_welist_url()` in `src/validation.rs` blocks localhost, loopback, RFC1918, link-local, multicast, unspecified, IPv6 unique-local addresses, and **IPv4-mapped IPv6 addresses** (`::ffff:a.b.c.d`) that route to loopback/private/link-local/unspecified IPv4 targets. HTTPS-only scheme enforced. Note: hostnames are not DNS-resolved before validation, so a domain that resolves to a private address is not caught by this check — restrict `WELIST_SERVER_URL` to trusted values.
|
||||||
**Regression Tests:** `tests/ssrf_tests.rs` (integration) + 8 unit tests in `src/validation.rs`.
|
**Regression Tests:** `tests/ssrf_tests.rs` (integration) + 11 unit tests in `src/validation.rs`.
|
||||||
|
|
||||||
### 6. Insecure Database Access (FIXED)
|
### 6. Insecure Database Access (FIXED)
|
||||||
**Severity:** MEDIUM | **Status:** Fixed
|
**Severity:** MEDIUM | **Status:** Fixed
|
||||||
@@ -130,16 +132,17 @@
|
|||||||
6. Use read-only filesystem for the server binary.
|
6. Use read-only filesystem for the server binary.
|
||||||
|
|
||||||
### Application-Level
|
### Application-Level
|
||||||
1. **Rate Limiting:** Implemented via `actix-governor` with token-bucket per client IP. `RealIpKeyExtractor` identifies clients behind reverse proxy using `X-Real-IP` / `X-Forwarded-For` headers. Trusted proxy IP configurable via `BAL_SERVER_TRUSTED_PROXY`.
|
1. **Rate Limiting:** Implemented via `actix-governor` with token-bucket per client IP and a **separate bucket per endpoint class**. `RealIpKeyExtractor` identifies clients behind reverse proxy using `X-Real-IP` / `X-Forwarded-For` headers. Trusted proxy IP configurable via `BAL_SERVER_TRUSTED_PROXY`.
|
||||||
2. **Input Validation:** Network enum check, txid hex validation, body size limits.
|
2. **Input Validation:** Network enum check, txid hex validation, body size limits.
|
||||||
3. **HTTPS:** Via Nginx reverse proxy with Let's Encrypt.
|
3. **Request Timeout:** `tokio::time::timeout` middleware returns `408` when a handler (including body read) exceeds `BAL_SERVER_ACTIX_TIMEOUT_SECS`.
|
||||||
4. **WAL Mode:** Enabled with retry logic for concurrent access.
|
4. **HTTPS:** Via Nginx reverse proxy with Let's Encrypt.
|
||||||
5. **ZMQ Timeout:** 5-second receive timeout prevents infinite blocking.
|
5. **WAL Mode:** Enabled with retry logic for concurrent access.
|
||||||
6. **Transaction Size Limits:** Configurable via rate limiting and body size.
|
6. **ZMQ Timeout:** 5-second receive timeout prevents infinite blocking.
|
||||||
7. **ZMQ Retry:** Reconnect logic with timeout-based detection.
|
7. **Transaction Size Limits:** Configurable via rate limiting and body size.
|
||||||
8. **Fee Limits:** Per-network `fixed_fee` configuration.
|
8. **ZMQ Retry:** Reconnect logic with timeout-based detection.
|
||||||
9. **Network Limits:** Only known networks accepted (bitcoin, testnet, testnet4, signet, regtest).
|
9. **Fee Limits:** Per-network `fixed_fee` configuration.
|
||||||
10. **Locktime Reasonableness:** Locktime compared against blockchain height and median time with threshold-based distinction.
|
10. **Network Limits:** Only known networks accepted (bitcoin, testnet, testnet4, signet, regtest).
|
||||||
|
11. **Locktime Reasonableness:** Locktime compared against blockchain height and median time with threshold-based distinction.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -153,4 +156,4 @@
|
|||||||
| `tests/secret_leakage_tests.rs` | 3 | .gitignore, no tracked secrets, no hardcoded tokens |
|
| `tests/secret_leakage_tests.rs` | 3 | .gitignore, no tracked secrets, no hardcoded tokens |
|
||||||
| `tests/input_validation_tests.rs` | 4 | Address caching, network validation, txid hex validation |
|
| `tests/input_validation_tests.rs` | 4 | Address caching, network validation, txid hex validation |
|
||||||
| `tests/db_path_validation.rs` | 5 | Path traversal, forbidden dirs, symlinks, WAL pragma, valid paths |
|
| `tests/db_path_validation.rs` | 5 | Path traversal, forbidden dirs, symlinks, WAL pragma, valid paths |
|
||||||
| `src/validation.rs` (inline) | 8 | SSRF URL validation unit tests |
|
| `src/validation.rs` (inline) | 11 | SSRF URL validation unit tests |
|
||||||
|
|||||||
34
pg-server.sh
Executable file
34
pg-server.sh
Executable file
@@ -0,0 +1,34 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||||
|
. "$SCRIPT_DIR/lib.sh"
|
||||||
|
|
||||||
|
PG_CONTAINER="bal-pg"
|
||||||
|
PG_USER="${BAL_PG_USER:-bal}"
|
||||||
|
PG_DB="${BAL_PG_DB:-baldb}"
|
||||||
|
# The password MUST come from the environment; never hardcode it.
|
||||||
|
PG_PASSWORD="${BAL_PG_PASSWORD:?BAL_PG_PASSWORD must be set (see .env.example)}"
|
||||||
|
PG_DSN="${BAL_SERVER_PG_DSN:-postgres://${PG_USER}:${PG_PASSWORD}@localhost:5432/${PG_DB}}"
|
||||||
|
|
||||||
|
if docker ps --format '{{.Names}}' | grep -q "^${PG_CONTAINER}$"; then
|
||||||
|
echo_i "PostgreSQL container '$PG_CONTAINER' is already running"
|
||||||
|
elif docker ps -a --format '{{.Names}}' | grep -q "^${PG_CONTAINER}$"; then
|
||||||
|
echo_i "Starting existing PostgreSQL container '$PG_CONTAINER'..."
|
||||||
|
docker start "$PG_CONTAINER" >/dev/null 2>&1
|
||||||
|
else
|
||||||
|
echo_i "Creating PostgreSQL container '$PG_CONTAINER'..."
|
||||||
|
docker run -d --name "$PG_CONTAINER" \
|
||||||
|
-e POSTGRES_USER="$PG_USER" \
|
||||||
|
-e POSTGRES_PASSWORD="$PG_PASSWORD" \
|
||||||
|
-e POSTGRES_DB="$PG_DB" \
|
||||||
|
-p 5432:5432 postgres:16-alpine
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo_i "Waiting for PostgreSQL..."
|
||||||
|
until docker exec "$PG_CONTAINER" pg_isready -U "$PG_USER" >/dev/null 2>&1; do
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
# Do not echo the DSN: it embeds the password.
|
||||||
|
echo_s "PostgreSQL ready (user=$PG_USER, db=$PG_DB, host=localhost:5432)"
|
||||||
|
export BAL_SERVER_PG_DSN="$PG_DSN"
|
||||||
@@ -46,9 +46,9 @@ struct MyConfig {
|
|||||||
impl Default for MyConfig {
|
impl Default for MyConfig {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
MyConfig {
|
MyConfig {
|
||||||
db_backend: env::var("BAL_PUSHER_DB_BACKEND").unwrap_or("sqlite".to_string()),
|
db_backend: env::var("BAL_SERVER_DB_BACKEND").unwrap_or("sqlite".to_string()),
|
||||||
db_file: env::var("BAL_PUSHER_DB_FILE").unwrap_or("bal.db".to_string()),
|
db_file: env::var("BAL_SERVER_DB_FILE").unwrap_or("bal.db".to_string()),
|
||||||
pg_dsn: env::var("BAL_PUSHER_PG_DSN").unwrap_or_default(),
|
pg_dsn: env::var("BAL_SERVER_PG_DSN").unwrap_or_default(),
|
||||||
bitcoin_dir: env::var("BAL_PUSHER_BITCOIN_DIR").unwrap_or("".to_string()),
|
bitcoin_dir: env::var("BAL_PUSHER_BITCOIN_DIR").unwrap_or("".to_string()),
|
||||||
regtest: get_network_params_default(Network::Regtest),
|
regtest: get_network_params_default(Network::Regtest),
|
||||||
testnet: get_network_params_default(Network::Testnet),
|
testnet: get_network_params_default(Network::Testnet),
|
||||||
@@ -59,7 +59,7 @@ impl Default for MyConfig {
|
|||||||
.unwrap_or("false".to_string())
|
.unwrap_or("false".to_string())
|
||||||
.parse::<bool>()
|
.parse::<bool>()
|
||||||
.unwrap_or(false),
|
.unwrap_or(false),
|
||||||
url: env::var("BAL_SERVER_URL").unwrap_or("http://localhost/".to_string()),
|
url: env::var("BAL_SERVER_URL").unwrap_or("http://127.0.0.1:9133".to_string()),
|
||||||
ssl_key_path: env::var("SSL_KEY_PATH").unwrap_or("privkey.pem".to_string()),
|
ssl_key_path: env::var("SSL_KEY_PATH").unwrap_or("privkey.pem".to_string()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -388,7 +388,13 @@ async fn send_stats_report(
|
|||||||
cfg.url, chain, bcinfo.blocks, bcinfo.median_time, bcinfo.best_block_hash
|
cfg.url, chain, bcinfo.blocks, bcinfo.median_time, bcinfo.best_block_hash
|
||||||
);
|
);
|
||||||
trace!("message to be sent: {}", message);
|
trace!("message to be sent: {}", message);
|
||||||
let sign = sign_message(cfg.ssl_key_path.as_str(), message.as_str());
|
let sign = match sign_message(cfg.ssl_key_path.as_str(), message.as_str()) {
|
||||||
|
Ok(signature) => signature,
|
||||||
|
Err(e) => {
|
||||||
|
error!("Cannot sign stats report, skipping: {}", e);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
};
|
||||||
let response = client
|
let response = client
|
||||||
.post(url)
|
.post(url)
|
||||||
.header("User-Agent", format!("bal-pusher/{}", VERSION))
|
.header("User-Agent", format!("bal-pusher/{}", VERSION))
|
||||||
@@ -414,22 +420,22 @@ async fn send_stats_report(
|
|||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
fn sign_message(private_key_path: &str, message: &str) -> String {
|
fn sign_message(private_key_path: &str, message: &str) -> Result<String, Box<dyn StdError>> {
|
||||||
let signing_key =
|
let signing_key = SigningKey::read_pkcs8_pem_file(private_key_path)
|
||||||
SigningKey::read_pkcs8_pem_file(private_key_path).expect("failed to parse private key PEM");
|
.map_err(|e| format!("failed to parse private key PEM at {private_key_path}: {e}"))?;
|
||||||
let signature = signing_key.sign(message.as_bytes());
|
let signature = signing_key.sign(message.as_bytes());
|
||||||
|
|
||||||
general_purpose::STANDARD.encode(signature.to_bytes())
|
Ok(general_purpose::STANDARD.encode(signature.to_bytes()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_env(cfg: &mut MyConfig) {
|
fn parse_env(cfg: &mut MyConfig) {
|
||||||
if let Ok(value) = env::var("BAL_PUSHER_DB_BACKEND") {
|
if let Ok(value) = env::var("BAL_SERVER_DB_BACKEND") {
|
||||||
cfg.db_backend = value;
|
cfg.db_backend = value;
|
||||||
}
|
}
|
||||||
if let Ok(value) = env::var("BAL_PUSHER_DB_FILE") {
|
if let Ok(value) = env::var("BAL_SERVER_DB_FILE") {
|
||||||
cfg.db_file = value;
|
cfg.db_file = value;
|
||||||
}
|
}
|
||||||
if let Ok(value) = env::var("BAL_PUSHER_PG_DSN") {
|
if let Ok(value) = env::var("BAL_SERVER_PG_DSN") {
|
||||||
cfg.pg_dsn = value;
|
cfg.pg_dsn = value;
|
||||||
}
|
}
|
||||||
cfg.regtest = parse_env_netconfig(cfg, "regtest");
|
cfg.regtest = parse_env_netconfig(cfg, "regtest");
|
||||||
@@ -561,12 +567,12 @@ async fn main() -> std::io::Result<()> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
consecutive_timeouts = 0;
|
consecutive_timeouts = 0;
|
||||||
let topic = message[0].clone();
|
let Some(topic) = message.first().cloned() else {
|
||||||
let body = message[1].clone();
|
warn!("ZMQ: received empty multipart message, skipping");
|
||||||
debug!(
|
continue;
|
||||||
"ZMQ:GET TOPIC: {}",
|
};
|
||||||
String::from_utf8(topic.clone()).expect("invalid topic")
|
let body = message.get(1).cloned().unwrap_or_default();
|
||||||
);
|
debug!("ZMQ:GET TOPIC: {}", String::from_utf8_lossy(&topic));
|
||||||
trace!("ZMQ:GET BODY: {}", hex::encode(&body));
|
trace!("ZMQ:GET BODY: {}", hex::encode(&body));
|
||||||
if topic == b"hashblock" {
|
if topic == b"hashblock" {
|
||||||
info!("NEW BLOCK: {}", hex::encode(&body));
|
info!("NEW BLOCK: {}", hex::encode(&body));
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
use actix_governor::{Governor, GovernorConfigBuilder, KeyExtractor, SimpleKeyExtractionError};
|
use actix_governor::{
|
||||||
use actix_web::dev::ServiceRequest;
|
Governor, GovernorConfig, GovernorConfigBuilder, KeyExtractor, SimpleKeyExtractionError,
|
||||||
use actix_web::middleware;
|
};
|
||||||
|
use actix_web::body::MessageBody;
|
||||||
|
use actix_web::dev::{ServiceRequest, ServiceResponse};
|
||||||
|
use actix_web::middleware::{self, Next};
|
||||||
use actix_web::web::Bytes;
|
use actix_web::web::Bytes;
|
||||||
use actix_web::{App, HttpResponse, HttpServer, Responder, web};
|
use actix_web::{App, Error, HttpResponse, HttpServer, Responder, web};
|
||||||
use bitcoin::{Network, Transaction, consensus};
|
use bitcoin::{Network, Transaction, consensus};
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
use hex_conservative::FromHex;
|
use hex_conservative::FromHex;
|
||||||
@@ -13,6 +16,7 @@ use std::env;
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
use std::net::IpAddr;
|
use std::net::IpAddr;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use bal_server::db::{
|
use bal_server::db::{
|
||||||
DatabasePool, InsertInpData, InsertOutData, InsertTxData, check_duplicate_txids,
|
DatabasePool, InsertInpData, InsertOutData, InsertTxData, check_duplicate_txids,
|
||||||
@@ -123,7 +127,6 @@ pub struct StatsResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
#[expect(dead_code)]
|
|
||||||
struct ActixConfig {
|
struct ActixConfig {
|
||||||
max_body_size: usize,
|
max_body_size: usize,
|
||||||
timeout_secs: u64,
|
timeout_secs: u64,
|
||||||
@@ -287,6 +290,44 @@ impl KeyExtractor for RealIpKeyExtractor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Concrete governor configuration type used by all rate-limited routes.
|
||||||
|
///
|
||||||
|
/// `NoOpMiddleware` is the default `actix-governor` middleware; it is only used
|
||||||
|
/// here as a type parameter so a single helper can build every route's config.
|
||||||
|
type GovernorConfigType =
|
||||||
|
GovernorConfig<RealIpKeyExtractor, actix_governor::governor::middleware::NoOpMiddleware>;
|
||||||
|
|
||||||
|
/// Build a rate-limiter configuration keyed by the real client IP.
|
||||||
|
///
|
||||||
|
/// A period of zero is rejected by `actix-governor`, so the values are clamped
|
||||||
|
/// to a minimum of one to avoid a `None` result.
|
||||||
|
fn build_governor_config(per_sec: u64, burst: u32) -> Result<GovernorConfigType, String> {
|
||||||
|
GovernorConfigBuilder::default()
|
||||||
|
.seconds_per_request(per_sec.max(1))
|
||||||
|
.burst_size(burst.max(1))
|
||||||
|
.key_extractor(RealIpKeyExtractor)
|
||||||
|
.finish()
|
||||||
|
.ok_or_else(|| "invalid rate limiter configuration".to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Middleware that aborts a request with `408 Request Timeout` if the inner
|
||||||
|
/// handler (including request body reading) exceeds the configured duration.
|
||||||
|
///
|
||||||
|
/// The duration is read from the `web::Data<u64>` app data registered in `main`.
|
||||||
|
async fn request_timeout<B>(
|
||||||
|
timeout_secs: web::Data<u64>,
|
||||||
|
req: ServiceRequest,
|
||||||
|
next: Next<B>,
|
||||||
|
) -> Result<ServiceResponse<B>, Error>
|
||||||
|
where
|
||||||
|
B: MessageBody,
|
||||||
|
{
|
||||||
|
match tokio::time::timeout(Duration::from_secs(**timeout_secs), next.call(req)).await {
|
||||||
|
Ok(result) => result,
|
||||||
|
Err(_) => Err(actix_web::error::ErrorRequestTimeout("request timeout")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async fn echo_info(
|
async fn echo_info(
|
||||||
path: web::Path<String>,
|
path: web::Path<String>,
|
||||||
data: web::Data<AppState>,
|
data: web::Data<AppState>,
|
||||||
@@ -464,6 +505,7 @@ fn parse_request_transactions(
|
|||||||
known_addresses: &HashSet<String>,
|
known_addresses: &HashSet<String>,
|
||||||
) -> Vec<(ParsedTx, String, u64)> {
|
) -> Vec<(ParsedTx, String, u64)> {
|
||||||
let mut result: Vec<(ParsedTx, String, u64)> = Vec::new();
|
let mut result: Vec<(ParsedTx, String, u64)> = Vec::new();
|
||||||
|
let mut seen_txids: HashSet<String> = HashSet::new();
|
||||||
|
|
||||||
for line in strbody.split('\n') {
|
for line in strbody.split('\n') {
|
||||||
if line.is_empty() {
|
if line.is_empty() {
|
||||||
@@ -489,6 +531,10 @@ fn parse_request_transactions(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let txid = tx.compute_txid().to_string();
|
let txid = tx.compute_txid().to_string();
|
||||||
|
if !seen_txids.insert(txid.clone()) {
|
||||||
|
trace!("duplicate txid {} in request body, skipping", txid);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
let ntxid = tx.compute_ntxid();
|
let ntxid = tx.compute_ntxid();
|
||||||
let wtxid = tx.compute_wtxid();
|
let wtxid = tx.compute_wtxid();
|
||||||
let locktime = tx.lock_time.to_string();
|
let locktime = tx.lock_time.to_string();
|
||||||
@@ -750,7 +796,10 @@ fn parse_env_netconfig(cfg: &mut MyConfig, chain: &str) {
|
|||||||
if let Ok(value) = env::var(format!("BAL_SERVER_{}_ADDRESS", chain.to_uppercase())) {
|
if let Ok(value) = env::var(format!("BAL_SERVER_{}_ADDRESS", chain.to_uppercase())) {
|
||||||
debug!("BAL_SERVER_{}_ADDRESS: {}", chain.to_uppercase(), value);
|
debug!("BAL_SERVER_{}_ADDRESS: {}", chain.to_uppercase(), value);
|
||||||
c.address = value;
|
c.address = value;
|
||||||
if c.address.len() > 5 && &c.address[1..4] == "pub" {
|
let is_xpub = ["xpub", "ypub", "zpub", "tpub", "vpub", "upub"]
|
||||||
|
.iter()
|
||||||
|
.any(|prefix| c.address.starts_with(prefix));
|
||||||
|
if is_xpub {
|
||||||
c.xpub = true;
|
c.xpub = true;
|
||||||
trace!("is_xpub");
|
trace!("is_xpub");
|
||||||
}
|
}
|
||||||
@@ -813,12 +862,26 @@ async fn main() -> std::io::Result<()> {
|
|||||||
let bind_address = data.cfg.bind_address.clone();
|
let bind_address = data.cfg.bind_address.clone();
|
||||||
let bind_port = data.cfg.bind_port;
|
let bind_port = data.cfg.bind_port;
|
||||||
|
|
||||||
let governor_conf = GovernorConfigBuilder::default()
|
// One shared limiter per endpoint class: all workers clone the same
|
||||||
.seconds_per_request(actix_cfg.rate_limit_pushtxs.0)
|
// underlying state, so the rate limit is global per real client IP.
|
||||||
.burst_size(actix_cfg.rate_limit_pushtxs.1)
|
let governor_pushtxs = build_governor_config(
|
||||||
.key_extractor(RealIpKeyExtractor)
|
actix_cfg.rate_limit_pushtxs.0,
|
||||||
.finish()
|
actix_cfg.rate_limit_pushtxs.1,
|
||||||
.unwrap();
|
)
|
||||||
|
.map_err(std::io::Error::other)?;
|
||||||
|
let governor_searchtx = build_governor_config(
|
||||||
|
actix_cfg.rate_limit_searchtx.0,
|
||||||
|
actix_cfg.rate_limit_searchtx.1,
|
||||||
|
)
|
||||||
|
.map_err(std::io::Error::other)?;
|
||||||
|
let governor_info =
|
||||||
|
build_governor_config(actix_cfg.rate_limit_info.0, actix_cfg.rate_limit_info.1)
|
||||||
|
.map_err(std::io::Error::other)?;
|
||||||
|
let governor_default = build_governor_config(
|
||||||
|
actix_cfg.rate_limit_default.0,
|
||||||
|
actix_cfg.rate_limit_default.1,
|
||||||
|
)
|
||||||
|
.map_err(std::io::Error::other)?;
|
||||||
|
|
||||||
println!("Starting server on http://{}:{}", bind_address, bind_port);
|
println!("Starting server on http://{}:{}", bind_address, bind_port);
|
||||||
|
|
||||||
@@ -827,16 +890,45 @@ async fn main() -> std::io::Result<()> {
|
|||||||
.app_data(web::PayloadConfig::default().limit(actix_cfg.max_body_size))
|
.app_data(web::PayloadConfig::default().limit(actix_cfg.max_body_size))
|
||||||
.app_data(data.clone())
|
.app_data(data.clone())
|
||||||
.app_data(web::Data::new(actix_cfg.trusted_proxy))
|
.app_data(web::Data::new(actix_cfg.trusted_proxy))
|
||||||
|
.app_data(web::Data::new(actix_cfg.timeout_secs))
|
||||||
.wrap(middleware::Logger::default())
|
.wrap(middleware::Logger::default())
|
||||||
.wrap(middleware::Compress::default())
|
.wrap(middleware::Compress::default())
|
||||||
.wrap(Governor::new(&governor_conf))
|
.wrap(middleware::from_fn(request_timeout))
|
||||||
.service(web::resource("/").route(web::get().to(echo_home)))
|
.service(
|
||||||
.service(web::resource("/.pub_key.pem").route(web::get().to(echo_pub_key)))
|
web::resource("/")
|
||||||
.service(web::resource("/version").route(web::get().to(echo_version)))
|
.wrap(Governor::new(&governor_default))
|
||||||
.service(web::resource("/{network}/info").route(web::get().to(echo_info)))
|
.route(web::get().to(echo_home)),
|
||||||
.service(web::resource("/{network}/stats").route(web::get().to(echo_stats)))
|
)
|
||||||
.service(web::resource("/{network}/pushtxs").route(web::post().to(echo_push)))
|
.service(
|
||||||
.service(web::resource("/searchtx").route(web::post().to(echo_search)))
|
web::resource("/.pub_key.pem")
|
||||||
|
.wrap(Governor::new(&governor_default))
|
||||||
|
.route(web::get().to(echo_pub_key)),
|
||||||
|
)
|
||||||
|
.service(
|
||||||
|
web::resource("/version")
|
||||||
|
.wrap(Governor::new(&governor_default))
|
||||||
|
.route(web::get().to(echo_version)),
|
||||||
|
)
|
||||||
|
.service(
|
||||||
|
web::resource("/{network}/info")
|
||||||
|
.wrap(Governor::new(&governor_info))
|
||||||
|
.route(web::get().to(echo_info)),
|
||||||
|
)
|
||||||
|
.service(
|
||||||
|
web::resource("/{network}/stats")
|
||||||
|
.wrap(Governor::new(&governor_info))
|
||||||
|
.route(web::get().to(echo_stats)),
|
||||||
|
)
|
||||||
|
.service(
|
||||||
|
web::resource("/{network}/pushtxs")
|
||||||
|
.wrap(Governor::new(&governor_pushtxs))
|
||||||
|
.route(web::post().to(echo_push)),
|
||||||
|
)
|
||||||
|
.service(
|
||||||
|
web::resource("/searchtx")
|
||||||
|
.wrap(Governor::new(&governor_searchtx))
|
||||||
|
.route(web::post().to(echo_search)),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.workers(actix_cfg.workers)
|
.workers(actix_cfg.workers)
|
||||||
.max_connections(actix_cfg.max_connections)
|
.max_connections(actix_cfg.max_connections)
|
||||||
|
|||||||
@@ -38,13 +38,23 @@ fn validate_sqlite_path(dsn: &str) -> Result<(), String> {
|
|||||||
// If absolute, block known sensitive system directories
|
// If absolute, block known sensitive system directories
|
||||||
if p.is_absolute() {
|
if p.is_absolute() {
|
||||||
let forbidden = [
|
let forbidden = [
|
||||||
"/etc", "/proc", "/sys", "/dev", "/usr", "/bin", "/sbin", "/lib", "/opt",
|
Path::new("/etc"),
|
||||||
|
Path::new("/proc"),
|
||||||
|
Path::new("/sys"),
|
||||||
|
Path::new("/dev"),
|
||||||
|
Path::new("/usr"),
|
||||||
|
Path::new("/bin"),
|
||||||
|
Path::new("/sbin"),
|
||||||
|
Path::new("/lib"),
|
||||||
|
Path::new("/opt"),
|
||||||
];
|
];
|
||||||
for prefix in &forbidden {
|
for prefix in &forbidden {
|
||||||
if path_str.starts_with(prefix) {
|
// `Path::starts_with` matches whole path components, so "/etcfoo"
|
||||||
|
// is not treated as being under "/etc".
|
||||||
|
if p.starts_with(prefix) {
|
||||||
return Err(format!(
|
return Err(format!(
|
||||||
"Absolute database path under {} is forbidden",
|
"Absolute database path under {} is forbidden",
|
||||||
prefix
|
prefix.display()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,6 +65,19 @@ pub fn is_valid_welist_url(url_str: &str) -> bool {
|
|||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IPv4-mapped IPv6 addresses (e.g. `::ffff:127.0.0.1`) are
|
||||||
|
// routed to the embedded IPv4 address on dual-stack hosts, so
|
||||||
|
// they must be validated against the IPv4 rules as well.
|
||||||
|
if let Some(v4) = v6.to_ipv4_mapped()
|
||||||
|
&& (v4.is_loopback()
|
||||||
|
|| v4.is_private()
|
||||||
|
|| v4.is_link_local()
|
||||||
|
|| v4.is_unspecified()
|
||||||
|
|| v4.is_multicast())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -154,6 +167,38 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ipv4_mapped_ipv6() {
|
||||||
|
assert!(
|
||||||
|
!is_valid_welist_url("https://[::ffff:127.0.0.1]"),
|
||||||
|
"IPv4-mapped loopback should be blocked"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
!is_valid_welist_url("https://[::ffff:10.0.0.1]"),
|
||||||
|
"IPv4-mapped private address should be blocked"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
!is_valid_welist_url("https://[::ffff:172.16.0.1]"),
|
||||||
|
"IPv4-mapped private address should be blocked"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
!is_valid_welist_url("https://[::ffff:192.168.1.1]"),
|
||||||
|
"IPv4-mapped private address should be blocked"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
!is_valid_welist_url("https://[::ffff:169.254.169.254]"),
|
||||||
|
"IPv4-mapped link-local address should be blocked"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
!is_valid_welist_url("https://[::ffff:0.0.0.0]"),
|
||||||
|
"IPv4-mapped unspecified address should be blocked"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
is_valid_welist_url("https://[::ffff:8.8.8.8]"),
|
||||||
|
"IPv4-mapped public address should be allowed"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_malformed_urls() {
|
fn test_malformed_urls() {
|
||||||
assert!(!is_valid_welist_url("not a url"));
|
assert!(!is_valid_welist_url("not a url"));
|
||||||
|
|||||||
Reference in New Issue
Block a user