mirror of
https://github.com/Corfirean/coa-server-manager.git
synced 2026-11-19 19:25:56 +00:00
feat(phase15): release and security hardening, DPAPI key store, probe & protocol hardening, secret redaction, audit and readiness report
This commit is contained in:
1 parent
b69fa6b867
commit
51e0f136d6
207 files changed
+38235
-7770
No files matched your search
@@ -148,6 +148,9 @@ fn test_phase14_direct_verified_route_connects_and_rewrites_realm_list() {
|
||||
Ok(0) | Err(_) => continue,
|
||||
Ok(_) => {
|
||||
let _ = stream.write_all(&fake_pkt);
|
||||
let _ = stream.flush();
|
||||
let _ = stream.shutdown(std::net::Shutdown::Write);
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -382,6 +385,9 @@ fn test_phase14_security_direct_ingress_strictly_forbids_mysql_and_ra() {
|
||||
Ok(n) => {
|
||||
assert_eq!(&buf[..n], b"TEST_AUTH");
|
||||
let _ = stream.write_all(b"AUTH_OK");
|
||||
let _ = stream.flush();
|
||||
let _ = stream.shutdown(std::net::Shutdown::Write);
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,7 +215,12 @@ pub fn router<K: KeyLookup>(hub: Arc<Hub<K>>) -> Router {
|
||||
.route("/coord/v1/health", get(health::<K>))
|
||||
.route("/coord/v1/host", get(host_ws::<K>))
|
||||
.route("/coord/v1/player", get(player_ws::<K>))
|
||||
.route("/coord/v1/probe", axum::routing::post(probe_post::<K>).get(probe_get::<K>))
|
||||
.route(
|
||||
"/coord/v1/probe",
|
||||
axum::routing::post(probe_post::<K>)
|
||||
.get(probe_get::<K>)
|
||||
.layer(axum::extract::DefaultBodyLimit::max(4096)),
|
||||
)
|
||||
.fallback(|| async { (StatusCode::NOT_FOUND, "not found") })
|
||||
.with_state(hub)
|
||||
}
|
||||
@@ -225,6 +230,22 @@ struct ProbeQuery {
|
||||
ports: String,
|
||||
}
|
||||
|
||||
const FORBIDDEN_PORTS: &[u16] = &[
|
||||
0, 21, 22, 23, 25, 53, 80, 110, 143, 443, 445, 1433, 1521, 3306, 3389,
|
||||
5432, 6379, 8080, 8081, 8082, 9090, 27017,
|
||||
];
|
||||
|
||||
fn is_disallowed_probe_ip(ip: IpAddr) -> bool {
|
||||
match ip {
|
||||
IpAddr::V4(v4) => {
|
||||
v4.is_loopback() || v4.is_private() || v4.is_link_local() || v4.is_broadcast() || v4.is_unspecified()
|
||||
}
|
||||
IpAddr::V6(v6) => {
|
||||
v6.is_loopback() || v6.is_unspecified() || v6.is_multicast()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn probe_post<K: KeyLookup>(
|
||||
State(hub): State<Arc<Hub<K>>>,
|
||||
ConnectInfo(Peer(peer)): ConnectInfo<Peer>,
|
||||
@@ -232,6 +253,12 @@ async fn probe_post<K: KeyLookup>(
|
||||
Json(payload): Json<ProbePayload>,
|
||||
) -> Response {
|
||||
let ip = client_ip(&headers, peer, hub.cfg.trust_proxy);
|
||||
if !hub.ip_rate.take(&ip, 1.0, hub.mono()) {
|
||||
return (StatusCode::TOO_MANY_REQUESTS, "rate limited").into_response();
|
||||
}
|
||||
if hub.cfg.trust_proxy && is_disallowed_probe_ip(ip) {
|
||||
return (StatusCode::FORBIDDEN, "cannot probe private or internal address").into_response();
|
||||
}
|
||||
perform_probe(ip, payload.ports).await
|
||||
}
|
||||
|
||||
@@ -242,6 +269,12 @@ async fn probe_get<K: KeyLookup>(
|
||||
Query(q): Query<ProbeQuery>,
|
||||
) -> Response {
|
||||
let ip = client_ip(&headers, peer, hub.cfg.trust_proxy);
|
||||
if !hub.ip_rate.take(&ip, 1.0, hub.mono()) {
|
||||
return (StatusCode::TOO_MANY_REQUESTS, "rate limited").into_response();
|
||||
}
|
||||
if hub.cfg.trust_proxy && is_disallowed_probe_ip(ip) {
|
||||
return (StatusCode::FORBIDDEN, "cannot probe private or internal address").into_response();
|
||||
}
|
||||
let ports: Vec<u16> = q.ports.split(',').filter_map(|s| s.trim().parse().ok()).collect();
|
||||
perform_probe(ip, ports).await
|
||||
}
|
||||
@@ -251,7 +284,7 @@ async fn perform_probe(ip: IpAddr, ports: Vec<u16>) -> Response {
|
||||
return (StatusCode::BAD_REQUEST, "invalid ports count").into_response();
|
||||
}
|
||||
for &p in &ports {
|
||||
if p == 0 || p == 22 || p == 80 || p == 443 || p == 5432 || p == 8080 || p == 8081 || p == 8082 {
|
||||
if FORBIDDEN_PORTS.contains(&p) {
|
||||
return (StatusCode::FORBIDDEN, "forbidden port").into_response();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -409,3 +409,46 @@ fn probe_endpoint_verifies_reachable_and_unreachable_ports() {
|
||||
assert!(out2.contains(&format!("\"{p2_live}\":true")), "{out2}");
|
||||
assert!(out2.contains("\"all_reachable\":true"), "{out2}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn attack_attempt_to_probe_third_party_or_spoofed_victim_ip_fails() {
|
||||
use std::io::Write;
|
||||
|
||||
// Case A: Attacker connects directly to Coordinator and supplies spoofed X-Forwarded-For header
|
||||
let s = start(quick());
|
||||
let mut t = TcpStream::connect(s.addr).unwrap();
|
||||
let victim_ip = "198.51.100.77";
|
||||
let req = format!(
|
||||
"GET /coord/v1/probe?ports=3724 HTTP/1.1\r\nHost: x\r\nX-Forwarded-For: {victim_ip}\r\n\r\n"
|
||||
);
|
||||
t.write_all(req.as_bytes()).unwrap();
|
||||
let out = read_response(&mut t);
|
||||
assert!(out.starts_with("HTTP/1.1 200"), "{out}");
|
||||
// Coordinator MUST use actual peer IP (127.0.0.1), NEVER the spoofed victim IP
|
||||
assert!(out.contains("\"client_ip\":\"127.0.0.1\""), "Must use peer IP, got: {out}");
|
||||
assert!(!out.contains(victim_ip), "Must never probe victim IP");
|
||||
|
||||
// Case B: Production mode behind proxy (trust_proxy = true): probing private / internal infrastructure is forbidden
|
||||
let mut cfg = quick();
|
||||
cfg.trust_proxy = true;
|
||||
let s_proxy = start(cfg);
|
||||
let mut t2 = TcpStream::connect(s_proxy.addr).unwrap();
|
||||
// Proxy forwards client IP that is private (e.g. attacker attempting internal SSRF)
|
||||
let req2 = "GET /coord/v1/probe?ports=3724 HTTP/1.1\r\nHost: x\r\nX-Forwarded-For: 10.0.0.1\r\n\r\n";
|
||||
t2.write_all(req2.as_bytes()).unwrap();
|
||||
let out2 = read_response(&mut t2);
|
||||
assert!(out2.starts_with("HTTP/1.1 403"), "Internal/private probe must be rejected: {out2}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn attack_attempt_to_probe_forbidden_infrastructure_ports_fails() {
|
||||
use std::io::Write;
|
||||
let s = start(quick());
|
||||
for forbidden_port in [22, 5432, 8080, 8081, 3306, 6379] {
|
||||
let mut t = TcpStream::connect(s.addr).unwrap();
|
||||
let req = format!("GET /coord/v1/probe?ports={forbidden_port} HTTP/1.1\r\nHost: x\r\n\r\n");
|
||||
t.write_all(req.as_bytes()).unwrap();
|
||||
let out = read_response(&mut t);
|
||||
assert!(out.starts_with("HTTP/1.1 403"), "Port {forbidden_port} must be forbidden: {out}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#!/bin/sh
|
||||
# /opt/coa/registry/backup-db.sh
|
||||
# Production logical database backup with retention and atomic completion marker.
|
||||
# Run on the node: sudo sh backup-db.sh
|
||||
set -eu
|
||||
|
||||
BACKUP_DIR="${BACKUP_DIR:-/opt/coa/backups}"
|
||||
RETENTION_DAYS="${RETENTION_DAYS:-14}"
|
||||
TIMESTAMP=$(date -u +"%Y%m%d_%H%M%SZ")
|
||||
TARGET_FILE="${BACKUP_DIR}/coa_registry_${TIMESTAMP}.dump"
|
||||
TMP_FILE="${TARGET_FILE}.tmp"
|
||||
MARKER_FILE="${TARGET_FILE}.complete"
|
||||
|
||||
mkdir -p "$BACKUP_DIR"
|
||||
chmod 700 "$BACKUP_DIR"
|
||||
|
||||
echo "[INFO] Starting database backup for coa_registry to ${TARGET_FILE}..."
|
||||
|
||||
# Execute pg_dump inside postgres container into temp file
|
||||
docker exec -i coa-postgres-postgres-1 sh -c 'PGPASSWORD=$(cat /run/secrets/postgres_password) exec pg_dump -U coa -d coa_registry --format=custom --blobs' > "$TMP_FILE"
|
||||
|
||||
# Verify dump file is non-empty
|
||||
FILE_SIZE=$(wc -c < "$TMP_FILE" || stat -c%s "$TMP_FILE" 2>/dev/null || echo 0)
|
||||
if [ "$FILE_SIZE" -lt 1024 ]; then
|
||||
echo "[ERROR] Backup failed or file too small (${FILE_SIZE} bytes)" >&2
|
||||
rm -f "$TMP_FILE"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Atomic commit
|
||||
mv "$TMP_FILE" "$TARGET_FILE"
|
||||
sha256sum "$TARGET_FILE" > "${TARGET_FILE}.sha256"
|
||||
touch "$MARKER_FILE"
|
||||
|
||||
echo "[INFO] Backup completed successfully: ${TARGET_FILE} (${FILE_SIZE} bytes)"
|
||||
|
||||
# Retention pruning
|
||||
echo "[INFO] Pruning backups older than ${RETENTION_DAYS} days..."
|
||||
find "$BACKUP_DIR" -name "coa_registry_*.dump" -mtime +"$RETENTION_DAYS" -exec rm -f {} {}.sha256 {}.complete \; 2>/dev/null || true
|
||||
echo "[INFO] Backup rotation complete."
|
||||
@@ -0,0 +1,40 @@
|
||||
#!/bin/sh
|
||||
# /opt/coa/registry/restore-db.sh
|
||||
# Production database restore from an existing backup dump file.
|
||||
# Usage: sudo sh restore-db.sh /opt/coa/backups/coa_registry_YYYYMMDD_HHMMSSZ.dump
|
||||
set -eu
|
||||
|
||||
if [ $# -lt 1 ]; then
|
||||
echo "Usage: $0 <path_to_backup_dump>" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
BACKUP_FILE="$1"
|
||||
|
||||
if [ ! -f "$BACKUP_FILE" ]; then
|
||||
echo "[ERROR] Backup file not found: $BACKUP_FILE" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Verify checksum if present
|
||||
if [ -f "${BACKUP_FILE}.sha256" ]; then
|
||||
echo "[INFO] Verifying backup checksum..."
|
||||
sha256sum -c "${BACKUP_FILE}.sha256"
|
||||
fi
|
||||
|
||||
echo "[WARNING] Restoring database coa_registry from $BACKUP_FILE. Existing connections will be terminated."
|
||||
read -p "Type 'RESTORE' to proceed: " CONFIRM
|
||||
if [ "$CONFIRM" != "RESTORE" ]; then
|
||||
echo "[INFO] Restore aborted."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Terminate existing connections to coa_registry
|
||||
docker exec -i coa-postgres-postgres-1 sh -c 'PGPASSWORD=$(cat /run/secrets/postgres_password) exec psql -U coa -d coa -v ON_ERROR_STOP=1 -q' <<SQL
|
||||
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'coa_registry' AND pid <> pg_backend_pid();
|
||||
SQL
|
||||
|
||||
# Restore dump
|
||||
docker exec -i coa-postgres-postgres-1 sh -c 'PGPASSWORD=$(cat /run/secrets/postgres_password) exec pg_restore -U coa -d coa_registry --clean --if-exists --no-owner' < "$BACKUP_FILE"
|
||||
|
||||
echo "[INFO] Database restore completed successfully from $BACKUP_FILE."
|
||||
@@ -238,7 +238,7 @@ impl<K: KeyLookup> Hub<K> {
|
||||
port_map.insert(world_port, (token.clone(), RelayTarget::World));
|
||||
}
|
||||
|
||||
tracing::info!(%realm_id, %player_id, auth_port, world_port, %token, "relay allocation created");
|
||||
tracing::info!(%realm_id, %player_id, auth_port, world_port, "relay allocation created");
|
||||
Ok(alloc)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user