// SPDX-License-Identifier: MPL-2.0 using System.Net; using System.Net.Http; using System.Net.Sockets; using System.Text; using Nini.Config; using OpenGenesisLINK.Addons.OSWebInterface; using var probe = new TcpListener(IPAddress.Loopback, 0); probe.Start(); int port = ((IPEndPoint)probe.LocalEndpoint).Port; probe.Stop(); var config = new IniConfigSource(); IConfig entry = config.Configs.Add("OSWebInterface"); entry.Set("Enabled", "true"); entry.Set("Port", port.ToString()); entry.Set("DataDirectory", "os-webinterface-smoke-test"); entry.Set("PublicOrigin", "https://admin.example.test"); var module = new OSWebInterfaceModule(); module.Initialise(config); try { using var client = new HttpClient(new HttpClientHandler { UseCookies = false }) { BaseAddress = new Uri($"http://127.0.0.1:{port}/"), Timeout = TimeSpan.FromSeconds(8) }; var home = await client.GetAsync("/"); if (home.StatusCode != HttpStatusCode.OK) throw new Exception("Index document status " + home.StatusCode); string html = await home.Content.ReadAsStringAsync(); if (!html.Contains("Simulator Control")) throw new Exception("Index body missing"); if (home.Headers.Contains("Access-Control-Allow-Origin")) throw new Exception("Unexpected CORS header"); if (!home.Headers.Contains("Content-Security-Policy")) throw new Exception("CSP missing"); foreach (string path in new[] { "/style.css", "/app.js" }) { using var staticFile = await client.GetAsync(path); if (staticFile.StatusCode != HttpStatusCode.OK) throw new Exception("Static resource missing: " + path); } using var deniedUpdate = await client.GetAsync("/api/updates/live-status"); if (deniedUpdate.StatusCode != HttpStatusCode.Unauthorized) throw new Exception("Unauthenticated live-updater status not denied"); using var foreignHostRequest = new HttpRequestMessage(HttpMethod.Get, "/"); foreignHostRequest.Headers.Host = "admin.example.test"; using var foreignHostResponse = await client.SendAsync(foreignHostRequest); if (foreignHostResponse.StatusCode != HttpStatusCode.NotFound) throw new Exception("Listener unexpectedly accepted foreign Host header"); using var proxyLogin = new HttpRequestMessage(HttpMethod.Post, "/api/login"); proxyLogin.Headers.Add("Origin", "https://admin.example.test"); proxyLogin.Headers.Add("X-Forwarded-Host", "admin.example.test"); proxyLogin.Headers.Add("X-Forwarded-Proto", "https"); proxyLogin.Content = new StringContent("{\"username\":\"Test User\",\"password\":\"123\"}", Encoding.UTF8, "application/json"); using var trusted = await client.SendAsync(proxyLogin); if (trusted.StatusCode != HttpStatusCode.ServiceUnavailable) throw new Exception("Trusted reverse proxy login did not reach authentication layer"); using var fakeProxyLogin = new HttpRequestMessage(HttpMethod.Post, "/api/login"); fakeProxyLogin.Headers.Add("Origin", "https://attacker.example"); fakeProxyLogin.Headers.Add("X-Forwarded-Host", "admin.example.test"); fakeProxyLogin.Headers.Add("X-Forwarded-Proto", "https"); fakeProxyLogin.Content = new StringContent("{\"username\":\"Test User\",\"password\":\"123\"}", Encoding.UTF8, "application/json"); using var fakeProxy = await client.SendAsync(fakeProxyLogin); if (fakeProxy.StatusCode != HttpStatusCode.Forbidden) throw new Exception("Forged reverse-proxy Origin was not rejected"); using var denied = await client.GetAsync("/api/overview"); if (denied.StatusCode != HttpStatusCode.Unauthorized) throw new Exception("Unauthenticated overview not denied"); using var forged = new HttpRequestMessage(HttpMethod.Post, "/api/login"); forged.Headers.Add("Origin", "https://attacker.example"); forged.Content = new StringContent("{\"username\":\"Test User\",\"password\":\"123\"}", Encoding.UTF8, "application/json"); using var rejected = await client.SendAsync(forged); if (rejected.StatusCode != HttpStatusCode.Forbidden) throw new Exception("Cross-origin login not denied"); using var noBackends = await client.PostAsync("/api/login", new StringContent("{\"username\":\"Test User\",\"password\":\"123\"}", Encoding.UTF8, "application/json")); if (noBackends.StatusCode != HttpStatusCode.ServiceUnavailable) throw new Exception("Missing authentication service did not fail closed"); Console.WriteLine("OSWebInterface HTTP smoke PASS: UI, CSP, 401, CSRF, fail-closed login and trusted HTTPS reverse proxy"); } finally { module.Close(); }