feat: allow HTTP operation on private LAN addresses

This commit is contained in:
2026-11-16 06:48:36 +01:00
parent 4337f1d801
commit 870844dbe3
9 changed files with 83 additions and 31 deletions

View File

@ -1,6 +1,6 @@
//! Zweck: Persistenter Dienstclient und ausschließlich lokale Tokenprüfung.
//! Wichtig: Bestätigung erst nach fsync; Geheimnisse werden niemals protokolliert.
//! Version: 1
//! Version: 2
use axum::{
Json, Router,
extract::State,
@ -50,14 +50,14 @@ impl Konfiguration {
}
pub fn pruefen(&self) -> Ergebnis<()> {
let url = reqwest::Url::parse(&self.server).map_err(|_| "Serveradresse ungültig")?;
let lokal = matches!(url.host_str(), Some("127.0.0.1" | "localhost" | "[::1]"));
let lokal = url.host_str().is_some_and(keyforge_protokoll::lan_host);
if (url.scheme() != "https" && !(url.scheme() == "http" && lokal))
|| !url.username().is_empty()
|| url.password().is_some()
|| url.query().is_some()
|| url.fragment().is_some()
{
return Err("Server benötigt HTTPS; HTTP nur auf localhost erlaubt".into());
return Err("Server benötigt HTTPS; HTTP nur zu privaten LAN-IP-Adressen oder localhost erlaubt".into());
}
if self.pruefen.adresse.ip() != Ipv4Addr::LOCALHOST
|| !keyforge_protokoll::dienst_id_gueltig(&self.dienst_id)

View File

@ -1,6 +1,6 @@
//! Zweck: Gemeinsame Token- und API-Typen für Server und Client.
//! Wichtige Information: Geheimnisse werden nur erzeugt und gehasht, nie protokolliert.
//! Version: 1
//! Version: 2
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
use serde::{Deserialize, Serialize};
@ -102,6 +102,19 @@ pub struct Problem {
pub korrelation_id: String,
}
/// Erlaubt HTTP zu lokalen und privaten IP-Adressen im freigegebenen LAN-Betrieb.
#[must_use]
pub fn lan_host(host: &str) -> bool {
if host == "localhost" {
return true;
}
match host.trim_matches(['[', ']']).parse::<std::net::IpAddr>() {
Ok(std::net::IpAddr::V4(ip)) => ip.is_loopback() || ip.is_private(),
Ok(std::net::IpAddr::V6(ip)) => ip.is_loopback() || ip.is_unique_local(),
Err(_) => false,
}
}
#[cfg(test)]
mod selbsttest {
use super::*;

View File

@ -57,3 +57,25 @@ fn pruefcode_ist_stabil_und_gruppiert() {
assert_ne!(pruefcode_aus_hash(&sha256_hex("abd")).unwrap(), code);
assert!(pruefcode_aus_hash("kein hash").is_err());
}
#[test]
fn http_ist_fuer_lan_und_loopback_erlaubt() {
for host in [
"192.168.1.4",
"10.0.0.4",
"172.16.0.1",
"127.0.0.1",
"localhost",
"[fd00::1]",
] {
assert!(keyforge_protokoll::lan_host(host));
}
for host in [
"8.8.8.8",
"172.32.0.1",
"example.org",
"[2001:4860:4860::8888]",
] {
assert!(!keyforge_protokoll::lan_host(host));
}
}

View File

@ -1,6 +1,6 @@
//! Zweck: Startet Dienst und Verwaltung oder richtet ein Administratorkonto ein.
//! Wichtige Information: Konto-Geheimnisse kommen ausschließlich von der Standardeingabe.
//! Version: 2
//! Version: 3
use keyforge_kern::Speicher;
use keyforge_server::{Zustand, router};
use serde::Deserialize;
@ -88,8 +88,8 @@ async fn ausfuehren() -> Result<(), &'static str> {
.adresse
.parse()
.map_err(|_| "Serveradresse muss eine IP-Adresse mit Port sein")?;
if !bind.ip().is_loopback() {
return Err("Ohne TLS darf KeyForge nur auf einer Loopback-Adresse lauschen");
if !bind.ip().is_unspecified() && !keyforge_protokoll::lan_host(&bind.ip().to_string()) {
return Err("HTTP-Bindung nur an private LAN-, Loopback- oder Wildcard-Adressen erlaubt");
}
datenbank_schuetzen(&konfig.datenbank)?;
let anmeldung = &konfig.datenbank;