diff --git a/.env.example b/.env.example index a2a52e4..8c69eec 100644 --- a/.env.example +++ b/.env.example @@ -6,6 +6,9 @@ GHOSTNODE_INTELLIGENCE_DB=./data/staking_intelligence.sqlite3 GHOSTNODE_DASHBOARD_MODE=localhost GHOSTNODE_DASHBOARD_LAN_ADDRESS= GHOSTNODE_DASHBOARD_PORT=44678 -# Required only for localhost_lan; keep the token outside the repository. +# LAN auth defaults to token_required. trusted_lan is an explicit, risky, +# unauthenticated read-only mode; administrative endpoints still need a token. +GHOSTNODE_DASHBOARD_LAN_AUTH_MODE=token_required +# Required for token_required and administrative LAN operations; keep the token outside the repository. GHOSTNODE_DASHBOARD_TOKEN_FILE= GHOSTNODE_NETWORK_CONFIG=~/.config/ghostnode-tools/network.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 1add7be..16f84f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,19 @@ # Changelog +## 0.3.1 + +- Added an explicit `trusted_lan` mode for direct, unauthenticated read-only + dashboard access on a configured private LAN address. +- Preserved `token_required` as the default for new LAN configurations. +- Kept network settings and other administrative endpoints token-protected in + `trusted_lan` mode. +- Added focused regression coverage for LAN modes, persistence, rollback + compatibility, and wildcard rejection. + +`trusted_lan` is intentionally opt-in: every reachable LAN client can read +the dashboard, and plain HTTP does not provide confidentiality. Use it only on +a trusted network; prefer `token_required` or an SSH tunnel otherwise. + ## 0.3.0 - Version-neutral README with a latest-release link. diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 83c8f03..f6f6bd8 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -11,12 +11,16 @@ | `GHOSTNODE_DASHBOARD_MODE` | `localhost` | `localhost` or `localhost_lan` | | `GHOSTNODE_DASHBOARD_LAN_ADDRESS` | unset | One assigned private LAN IPv4 address; never a wildcard | | `GHOSTNODE_DASHBOARD_PORT` | `44678` | Unprivileged dashboard TCP port | +| `GHOSTNODE_DASHBOARD_LAN_AUTH_MODE` | `token_required` | `token_required` or explicitly reviewed `trusted_lan` | | `GHOSTNODE_DASHBOARD_TOKEN_FILE` | unset | Private file containing the LAN bearer token | | `GHOSTNODE_NETWORK_CONFIG` | `~/.config/ghostnode-tools/network.json` | Private atomically written non-secret settings | The dashboard always includes `127.0.0.1`. In `localhost_lan` mode it adds -exactly the configured assigned private LAN IPv4 address. LAN mode requires a -bearer token from a private file; there is no default password. Wildcard, +exactly the configured assigned private LAN IPv4 address. LAN mode defaults to +`token_required`, requiring a bearer token from a private file; there is no +default password. Explicit `trusted_lan` permits unauthenticated read-only +access for reachable LAN clients while administrative endpoints remain +token-protected. Wildcard, multicast, public, loopback-as-LAN, and IPv6 binds are rejected. Network settings are validated before listeners start and web proposals never apply changes. Never put RPC passwords, cookies, bearer tokens, seeds, or wallet keys in diff --git a/INSTALL.md b/INSTALL.md index f23cdff..1e161da 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -60,11 +60,15 @@ does not load or unlock wallets. The dashboard is read-only and defaults to loopback. To enable the optional dual-address mode, configure `GHOSTNODE_DASHBOARD_MODE=localhost_lan`, an -assigned private `GHOSTNODE_DASHBOARD_LAN_ADDRESS`, and a restrictive -`GHOSTNODE_DASHBOARD_TOKEN_FILE` in a private environment file. The token file +assigned private `GHOSTNODE_DASHBOARD_LAN_ADDRESS`, and +`GHOSTNODE_DASHBOARD_LAN_AUTH_MODE=token_required` (the safe default) with a +restrictive `GHOSTNODE_DASHBOARD_TOKEN_FILE` in a private environment file. The token file must already exist; GhostNode Tools never generates or logs a default password. Wildcard, public, multicast, loopback-as-LAN, and IPv6 addresses are rejected. -Plain HTTP on a LAN does not provide confidentiality. For remote viewing, +An explicitly reviewed `trusted_lan` mode allows unauthenticated read-only +access to every reachable LAN client; administrative endpoints still require +the private token. A specific LAN address is not authentication. Plain HTTP on +a LAN does not provide confidentiality. For remote viewing, localhost mode with an SSH tunnel remains preferred: ```bash diff --git a/README.md b/README.md index b6d34ff..6e37671 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,8 @@ unlocks wallets, or changes node configuration. No Ghost Core connection is required for M1. -v0.3.0 adds configurable, validated dashboard network access. The portable +v0.3.1 adds an explicitly reviewed trusted-LAN access option on top of the +configurable, validated dashboard network access introduced in v0.3.0. The portable defaults are `ghost-cli`, `~/.ghost`, and wallet `ghostmain`; they can be overridden with command options or environment variables. @@ -69,8 +70,11 @@ python -m pip install --no-build-isolation --no-deps -e . The dashboard is read-only and defaults to `127.0.0.1:44678`. It can optionally bind exactly one explicitly assigned private LAN IPv4 address in addition to localhost. Wildcard, multicast, public, loopback-as-LAN, and IPv6 addresses are -rejected. LAN mode requires a private bearer token file; there is no default -password and plain HTTP does not provide confidentiality. Keep LAN access on a +rejected. LAN mode defaults to `token_required`, using a private bearer token +file; there is no default password and plain HTTP does not provide +confidentiality. An explicitly selected `trusted_lan` mode permits +unauthenticated read-only access to every client that can reach the configured +private address. Keep LAN access on a trusted network or use HTTPS/SSH tunneling as appropriate. The dashboard groups diagnostics into Blockchain, Network, System, Service, @@ -138,8 +142,11 @@ sudo systemctl disable ghostnode-dashboard.service The unit runs as `etcmc`, uses the project `.venv`, and reads dashboard network settings from its private environment file. Localhost is the default. To use -LAN mode, configure a private token file and an explicitly assigned LAN IPv4 -address; never use `0.0.0.0` or expose the dashboard publicly. +For authenticated LAN mode, configure a private token file and an explicitly +assigned LAN IPv4 address; never use `0.0.0.0` or expose the dashboard publicly. +The explicit `trusted_lan` mode is an operator decision and does not protect +read-only data from reachable LAN clients; administrative endpoints remain +token-protected. The intelligence collector unit is separate and is not enabled by default. It requires a separate operator review, backup, and migration plan. diff --git a/SECURITY.md b/SECURITY.md index 668b9ac..f536b0b 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -7,9 +7,12 @@ files, and mainnet reports outside the repository. The dashboard defaults to `127.0.0.1` and localhost mode has no authentication because it is loopback-only. Optional LAN mode binds exactly one assigned -private IPv4 address and requires a bearer token from a private file for every -request. It rejects wildcard, multicast, public, loopback-as-LAN, and IPv6 -addresses. There is no default password. Plain HTTP on a LAN does not provide +private IPv4 address. New LAN configurations use `token_required`, which +requires a bearer token from a private file for every request. An operator may +explicitly select `trusted_lan` for unauthenticated read-only access, but +administrative endpoints remain token-protected. It rejects wildcard, +multicast, public, loopback-as-LAN, and IPv6 addresses. There is no default +password. Plain HTTP on a LAN does not provide confidentiality; use an appropriately secured HTTPS layer or SSH tunnel when needed. This document is not a guarantee of security or legal compliance. diff --git a/docs/GHOST_CORE_SETUP.md b/docs/GHOST_CORE_SETUP.md index 5e02325..fc36a2b 100644 --- a/docs/GHOST_CORE_SETUP.md +++ b/docs/GHOST_CORE_SETUP.md @@ -115,6 +115,9 @@ encryption passphrase is not guaranteed recoverable. Keep the dashboard on `127.0.0.1` and use an SSH tunnel for authorized remote viewing by default. If `localhost_lan` is explicitly configured, use exactly -one assigned private LAN IPv4 address and a private bearer token; plain HTTP -does not provide confidentiality. Never expose Ghost Core RPC directly to the +one assigned private LAN IPv4 address. The safe default is +`GHOSTNODE_DASHBOARD_LAN_AUTH_MODE=token_required`; the explicit +`trusted_lan` mode allows unauthenticated read-only access to reachable LAN +clients while administrative endpoints remain token-protected. Plain HTTP does +not provide confidentiality. Never expose Ghost Core RPC directly to the public Internet. diff --git a/docs/V0_3_NETWORK_ACCESS.md b/docs/V0_3_NETWORK_ACCESS.md index cbbe6e5..c970303 100644 --- a/docs/V0_3_NETWORK_ACCESS.md +++ b/docs/V0_3_NETWORK_ACCESS.md @@ -12,11 +12,15 @@ that is not currently assigned to a local interface. ## LAN security -LAN mode requires a bearer token supplied through a private token file. There -is no default password and GhostNode Tools never generates, prints, or commits -the token. Plain HTTP does not provide confidentiality or integrity against a -hostile LAN; use an appropriately secured HTTPS layer or an SSH tunnel when -that matters. +LAN mode defaults to `token_required`, using a bearer token supplied through a +private token file. There is no default password and GhostNode Tools never +generates, prints, or commits the token. An explicitly selected `trusted_lan` +mode allows direct unauthenticated read-only dashboard/API access to every +client able to reach the configured LAN address; state-changing and +configuration endpoints remain token-protected. A specific LAN bind is not an +authentication boundary. Plain HTTP does not provide confidentiality or +integrity against a hostile LAN; use an appropriately secured HTTPS layer or +an SSH tunnel when that matters. Example private environment settings: @@ -24,6 +28,7 @@ Example private environment settings: GHOSTNODE_DASHBOARD_MODE=localhost_lan GHOSTNODE_DASHBOARD_LAN_ADDRESS=192.168.1.100 GHOSTNODE_DASHBOARD_PORT=44678 +GHOSTNODE_DASHBOARD_LAN_AUTH_MODE=token_required GHOSTNODE_DASHBOARD_TOKEN_FILE=/private/path/ghostnode-dashboard.token ``` @@ -31,12 +36,16 @@ The token file should be owned by the service account and mode `0600`. ## First-run review -`ghostnode init --json --network-mode localhost_lan --lan-address 192.168.1.100` +`ghostnode init --json --network-mode localhost_lan --lan-address 192.168.1.100 --lan-auth-mode token_required` prints a proposed configuration. It does not apply it. Applying settings requires the explicit pair `--apply-network --confirm-network` and writes only the private non-secret configuration atomically. The dashboard's network settings endpoint is read-only; its POST endpoint validates proposals but does not write files or restart services. +For an explicitly reviewed trusted-LAN deployment, use +`--lan-auth-mode trusted_lan`. This does not protect read-only dashboard data +from other reachable LAN clients; keep the mode limited to a trusted network. + Always preserve the previous working configuration, test both listeners, and keep the existing SSH tunnel or proxy until the replacement is verified. diff --git a/ghostnode/__init__.py b/ghostnode/__init__.py index 9ec1433..0f5894a 100644 --- a/ghostnode/__init__.py +++ b/ghostnode/__init__.py @@ -1,3 +1,3 @@ """GhostNode Tools.""" -__version__ = "0.3.0" +__version__ = "0.3.1" diff --git a/ghostnode/commands/init.py b/ghostnode/commands/init.py index 7c07d49..12d024c 100644 --- a/ghostnode/commands/init.py +++ b/ghostnode/commands/init.py @@ -21,6 +21,8 @@ @click.option("--network", "expected_network", default="main", show_default=True) @click.option("--network-mode", type=click.Choice(["localhost", "localhost_lan"]), default="localhost", show_default=True) +@click.option("--lan-auth-mode", type=click.Choice(["token_required", "trusted_lan"]), default="token_required", + show_default=True, help="Explicit LAN authentication mode.") @click.option("--lan-address", default=None, help="Assigned private LAN IPv4 address for localhost_lan mode.") @click.option("--dashboard-port", default=44678, show_default=True, type=int) @click.option("--network-config", default=None, help="Private network configuration path.") @@ -28,16 +30,17 @@ @click.option("--confirm-network", is_flag=True, help="Confirm the explicit network configuration review.") def init(as_json: bool, cli_path: str, datadir: str, wallet_name: str, wallet_dir: str | None, rpc_host: str | None, rpc_port: int | None, - expected_network: str, network_mode: str, lan_address: str | None, + expected_network: str, network_mode: str, lan_auth_mode: str, lan_address: str | None, dashboard_port: int, network_config: str | None, apply_network: bool, confirm_network: bool) -> None: """Run the safe, read-only first-run readiness workflow.""" try: - network = validate_network_settings(NetworkSettings(network_mode, lan_address, dashboard_port)) + network = validate_network_settings(NetworkSettings(network_mode, lan_address, dashboard_port, lan_auth_mode)) except ValueError as exc: raise click.ClickException(str(exc)) from exc network_plan = {**network.as_dict(), "restart_required": True, "applied": False, - "security": "token required for localhost_lan"} + "security": ("token required for localhost_lan" if network.lan_auth_mode == "token_required" + else "trusted LAN: every reachable client can read the dashboard")} if apply_network: if not confirm_network: raise click.ClickException("--apply-network requires --confirm-network after reviewing the plan") diff --git a/ghostnode/commands/web.py b/ghostnode/commands/web.py index f12ed04..47a636e 100644 --- a/ghostnode/commands/web.py +++ b/ghostnode/commands/web.py @@ -21,14 +21,16 @@ def validate_dashboard_host(host: str) -> str: @click.option("--host", default="127.0.0.1", show_default=True, help="Bind address; localhost is the safe default.") @click.option("--port", default=44678, show_default=True, type=int) @click.option("--network-mode", type=click.Choice(["localhost", "localhost_lan"]), default=None, - help="Use localhost only or authenticated localhost plus one LAN IPv4 listener.") + help="Use localhost only or localhost plus one LAN IPv4 listener.") +@click.option("--lan-auth-mode", type=click.Choice(["token_required", "trusted_lan"]), default=None, + help="LAN authentication mode; token_required is the safe default.") @click.option("--lan-address", default=None, help="Explicit assigned private LAN IPv4 address.") @click.option("--token-file", default=None, help="Private file containing the LAN bearer token.") @click.option("--cli-path", default=Config.cli_path, show_default=True) @click.option("--datadir", default=Config.datadir, show_default=True) @click.option("--wallet", "wallet_name", default=Config.wallet_name, show_default=True) @click.option("--service", "service_name", default=Config.service, show_default=True) -def web(host: str, port: int, network_mode: str | None, lan_address: str | None, +def web(host: str, port: int, network_mode: str | None, lan_auth_mode: str | None, lan_address: str | None, token_file: str | None, cli_path: str, datadir: str, wallet_name: str, service_name: str) -> None: """Run the read-only dashboard with an explicit localhost or authenticated LAN mode.""" @@ -40,8 +42,10 @@ def web(host: str, port: int, network_mode: str | None, lan_address: str | None, active_config = Config(cli_path=cli_path, datadir=datadir, wallet_name=wallet_name, service=service_name, dashboard_mode=network_mode or Config.dashboard_mode, dashboard_lan_address=lan_address or Config.dashboard_lan_address, - dashboard_port=port, dashboard_token_file=token_file or Config.dashboard_token_file) - settings = NetworkSettings(active_config.dashboard_mode, active_config.dashboard_lan_address, port) + dashboard_port=port, dashboard_token_file=token_file or Config.dashboard_token_file, + dashboard_lan_auth_mode=lan_auth_mode or Config.dashboard_lan_auth_mode) + settings = NetworkSettings(active_config.dashboard_mode, active_config.dashboard_lan_address, port, + active_config.dashboard_lan_auth_mode) if network_mode is None and lan_address is None and port == 44678 \ and active_config.dashboard_mode == "localhost" \ and active_config.dashboard_lan_address is None: @@ -52,8 +56,8 @@ def web(host: str, port: int, network_mode: str | None, lan_address: str | None, if not ports_available(settings): raise click.ClickException("one or more dashboard listeners are unavailable; no configuration was changed") token = read_dashboard_token(active_config.dashboard_token_file) - if settings.mode == LAN_MODE and not token: - raise click.ClickException("LAN mode requires a private bearer token file; no listener was started") + if settings.mode == LAN_MODE and settings.lan_auth_mode == "token_required" and not token: + raise click.ClickException("token_required LAN mode requires a private bearer token file; no listener was started") app = create_app(active_config, network_settings=settings, dashboard_token=token) if settings.mode == "localhost": uvicorn.run(app, host=LOCALHOST, port=settings.port) diff --git a/ghostnode/config.py b/ghostnode/config.py index 378bde1..23d89e5 100644 --- a/ghostnode/config.py +++ b/ghostnode/config.py @@ -18,4 +18,5 @@ class Config: dashboard_port: int = (int(os.environ["GHOSTNODE_DASHBOARD_PORT"]) if os.environ.get("GHOSTNODE_DASHBOARD_PORT") else 44678) dashboard_token_file: str | None = os.environ.get("GHOSTNODE_DASHBOARD_TOKEN_FILE") or None + dashboard_lan_auth_mode: str = os.environ.get("GHOSTNODE_DASHBOARD_LAN_AUTH_MODE", "token_required") dashboard_network_config: str | None = os.environ.get("GHOSTNODE_NETWORK_CONFIG") or None diff --git a/ghostnode/network.py b/ghostnode/network.py index e7b8781..e10c80b 100644 --- a/ghostnode/network.py +++ b/ghostnode/network.py @@ -18,6 +18,8 @@ LOCALHOST = "127.0.0.1" LOCALHOST_MODE = "localhost" LAN_MODE = "localhost_lan" +TOKEN_REQUIRED = "token_required" +TRUSTED_LAN = "trusted_lan" DEFAULT_PORT = 44678 @@ -26,6 +28,7 @@ class NetworkSettings: mode: str = LOCALHOST_MODE lan_address: str | None = None port: int = DEFAULT_PORT + lan_auth_mode: str = TOKEN_REQUIRED @property def bind_addresses(self) -> tuple[str, ...]: @@ -33,6 +36,7 @@ def bind_addresses(self) -> tuple[str, ...]: def as_dict(self) -> dict[str, object]: return {"mode": self.mode, "lan_address": self.lan_address, "port": self.port, + "lan_auth_mode": self.lan_auth_mode, "localhost_address": LOCALHOST, "bind_addresses": list(self.bind_addresses)} @@ -68,7 +72,9 @@ def validate_network_settings(settings: NetworkSettings, if settings.mode == LOCALHOST_MODE: if settings.lan_address not in (None, ""): raise ValueError("localhost mode cannot configure a LAN address") - return NetworkSettings(LOCALHOST_MODE, None, settings.port) + return NetworkSettings(LOCALHOST_MODE, None, settings.port, TOKEN_REQUIRED) + if settings.lan_auth_mode not in {TOKEN_REQUIRED, TRUSTED_LAN}: + raise ValueError("unsupported LAN authentication mode") try: address = ipaddress.ip_address(settings.lan_address or "") except ValueError as exc: @@ -79,7 +85,7 @@ def validate_network_settings(settings: NetworkSettings, assigned = set(available if available is not None else available_lan_ipv4()) if str(address) not in assigned: raise ValueError("LAN address is not assigned to a local interface") - return NetworkSettings(LAN_MODE, str(address), settings.port) + return NetworkSettings(LAN_MODE, str(address), settings.port, settings.lan_auth_mode) def load_network_settings(path: str | Path | None = None) -> NetworkSettings: @@ -90,7 +96,8 @@ def load_network_settings(path: str | Path | None = None) -> NetworkSettings: data = json.loads(target.read_text(encoding="utf-8")) return validate_network_settings(NetworkSettings(data.get("mode", LOCALHOST_MODE), data.get("lan_address"), - int(data.get("port", DEFAULT_PORT)))) + int(data.get("port", DEFAULT_PORT)), + data.get("lan_auth_mode", data.get("auth_mode", TOKEN_REQUIRED)))) except (OSError, ValueError, TypeError, json.JSONDecodeError): return NetworkSettings() diff --git a/ghostnode/web.py b/ghostnode/web.py index f418ca4..77a890d 100644 --- a/ghostnode/web.py +++ b/ghostnode/web.py @@ -16,7 +16,7 @@ from ghostnode.intelligence.views import overview as intelligence_overview from ghostnode.intelligence.views import performance as intelligence_performance from ghostnode.intelligence.views import rewards as intelligence_rewards -from ghostnode.network import (LAN_MODE, LOCALHOST, NetworkSettings, load_network_settings, +from ghostnode.network import (LAN_MODE, LOCALHOST, TOKEN_REQUIRED, NetworkSettings, load_network_settings, read_dashboard_token, token_is_valid, validate_network_settings) BASE_DIR = Path(__file__).resolve().parent @@ -111,7 +111,7 @@ def create_app(config: Config | None = None, clock: Callable[[], datetime] | Non active_config.dashboard_port) settings = validate_network_settings(settings) expected_token = dashboard_token if dashboard_token is not None else read_dashboard_token(active_config.dashboard_token_file) - if settings.mode == LAN_MODE and not expected_token: + if settings.mode == LAN_MODE and settings.lan_auth_mode == TOKEN_REQUIRED and not expected_token: raise ValueError("LAN dashboard mode requires GHOSTNODE_DASHBOARD_TOKEN_FILE or GHOSTNODE_DASHBOARD_TOKEN") templates = Jinja2Templates(directory=str(BASE_DIR / "templates")) app = FastAPI(title="GhostNode Tools", docs_url=None, redoc_url=None) @@ -129,11 +129,14 @@ def local_or_lan_origin_allowed(request: Request) -> bool: @app.middleware("http") async def dashboard_access(request: Request, call_next): - if settings.mode == LAN_MODE and listener_host(request) != LOCALHOST: + is_lan_request = settings.mode == LAN_MODE and listener_host(request) != LOCALHOST + if is_lan_request and (settings.lan_auth_mode == TOKEN_REQUIRED or request.method in {"POST", "PUT", "PATCH", "DELETE"}): authorization = request.headers.get("authorization", "") provided = authorization[7:].strip() if authorization.lower().startswith("bearer ") else None if not token_is_valid(provided, expected_token): - return JSONResponse({"detail": "LAN dashboard authentication required"}, status_code=401, + detail = ("LAN dashboard authentication required" if settings.lan_auth_mode == TOKEN_REQUIRED + else "administrative LAN authentication required") + return JSONResponse({"detail": detail}, status_code=401, headers={"WWW-Authenticate": "Bearer"}) if request.method in {"POST", "PUT", "PATCH", "DELETE"} and not local_or_lan_origin_allowed(request): return JSONResponse({"detail": "request origin is not allowed"}, status_code=403) @@ -178,7 +181,8 @@ async def api_intelligence_collector(): async def api_network_settings(): return {"status": "OK", "data": { **settings.as_dict(), - "security_status": "AUTHENTICATED_LAN" if settings.mode == LAN_MODE else "LOCALHOST_ONLY", + "security_status": (("AUTHENTICATED_LAN" if settings.lan_auth_mode == TOKEN_REQUIRED else "TRUSTED_LAN") + if settings.mode == LAN_MODE else "LOCALHOST_ONLY"), "token_configured": bool(expected_token) if settings.mode == LAN_MODE else False, "restart_required_for_apply": True, "read_only": True, @@ -192,7 +196,8 @@ async def api_network_proposal(request: Request): if not isinstance(payload, dict): raise ValueError("request body must be a JSON object") proposed = NetworkSettings(payload.get("mode", settings.mode), payload.get("lan_address"), - int(payload.get("port", settings.port))) + int(payload.get("port", settings.port)), + payload.get("lan_auth_mode", settings.lan_auth_mode)) checked = validate_network_settings(proposed) except (ValueError, TypeError, KeyError, JSONDecodeError) as exc: return JSONResponse({"status": "ERROR", "message": str(exc)}, status_code=400) diff --git a/pyproject.toml b/pyproject.toml index 1c93dff..200c2e5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "ghostnode-tools" -version = "0.3.0" +version = "0.3.1" description = "Read-only Ghost Core monitoring and local staking intelligence tools" license = { file = "LICENSE" } authors = [{ name = "eflipper13" }] diff --git a/tests/test_network.py b/tests/test_network.py index afcd7ef..39ea307 100644 --- a/tests/test_network.py +++ b/tests/test_network.py @@ -8,7 +8,7 @@ from ghostnode.collectors import network from ghostnode.commands import network as network_command from ghostnode.config import Config -from ghostnode.network import (LAN_MODE, LOCALHOST_MODE, NetworkSettings, +from ghostnode.network import (LAN_MODE, LOCALHOST_MODE, TRUSTED_LAN, NetworkSettings, ports_available, save_network_settings, token_is_valid, validate_network_settings) from ghostnode.web import create_app @@ -103,7 +103,7 @@ def test_network_config_persistence_is_atomic_and_non_secret(tmp_path): saved = save_network_settings(NetworkSettings(LOCALHOST_MODE, None, 44679), path) assert saved == path assert path.stat().st_mode & 0o777 == 0o600 - assert "token" not in path.read_text() + assert "test-token" not in path.read_text() save_network_settings(NetworkSettings(LOCALHOST_MODE, None, 44680), path) assert (tmp_path / "network.json.previous").exists() assert '44679' in (tmp_path / "network.json.previous").read_text() @@ -128,6 +128,31 @@ async def test_lan_dashboard_requires_token_and_allows_authorized_read(monkeypat assert allowed.json()["data"]["security_status"] == "AUTHENTICATED_LAN" +@pytest.mark.anyio +async def test_trusted_lan_allows_read_only_access_but_protects_admin(monkeypatch): + monkeypatch.setattr("ghostnode.network.available_lan_ipv4", lambda: ("192.168.1.100",)) + app = create_app(network_settings=NetworkSettings(LAN_MODE, "192.168.1.100", 44678, TRUSTED_LAN), + dashboard_token="admin-token") + transport = httpx.ASGITransport(app=app) + async with httpx.AsyncClient(transport=transport, base_url="http://testserver") as client: + settings = await client.get("/api/network/settings") + proposal_denied = await client.post("/api/network/proposals", json={"mode": "localhost"}) + proposal_allowed = await client.post("/api/network/proposals", + headers={"Authorization": "Bearer admin-token", + "Origin": "http://192.168.1.100:44678"}, + json={"mode": "localhost"}) + assert settings.status_code == 200 + assert settings.json()["data"]["security_status"] == "TRUSTED_LAN" + assert proposal_denied.status_code == 401 + assert proposal_allowed.status_code == 200 + + +def test_invalid_lan_auth_mode_is_rejected(): + with pytest.raises(ValueError, match="authentication mode"): + validate_network_settings(NetworkSettings(LAN_MODE, "192.168.1.100", 44678, "unsafe"), + available=("192.168.1.100",)) + + @pytest.mark.anyio async def test_network_proposal_does_not_write_or_apply(monkeypatch, tmp_path): monkeypatch.setattr("ghostnode.network.available_lan_ipv4", lambda: ("192.168.1.100",))