Files
opengenesislinkserver/tools/ci/OSWebInterfaceSmoke/Program.cs
T

63 lines
2.9 KiB
C#

// 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");
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 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, static resources, CSP, 401, CSRF-origin and fail-closed login");
}
finally { module.Close(); }