- Windows data folder: the folder and everything in it must belong to SYSTEM, Administrators or the server's account and hold no link, checked before the ACL is rewritten; otherwise the server stops and names the entry. server.json is read only after its owner is checked on the open handle. Secrets are deleted and created anew with the private ACL, so a planted file's owner loses its rights. - Windows archive: a folder the server creates is changeable only by those accounts (users may read it) and a .titano folder it creates is private; an existing archive others can change is logged as a warning at start. - Proxy mode: only loopback is trusted by default, since LAN clients reach the HTTP port from private addresses; a cookie sign-in that did not come through a trusted proxy over HTTPS answers proxy_untrusted, naming TITANO_TRUSTED_PROXIES. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
133 lines
5.7 KiB
C#
133 lines
5.7 KiB
C#
using System.Net;
|
|
using System.Text.RegularExpressions;
|
|
using Titano.Data;
|
|
using Titano.Server.Configuration;
|
|
using Titano.Server.Hosting;
|
|
using Titano.Testing;
|
|
|
|
namespace Titano.Server.Tests.Hosting;
|
|
|
|
[Category("server")]
|
|
public sealed class StartupTests(TestContext context)
|
|
{
|
|
[Test]
|
|
public void TheServerRefusesToStartWithoutTheArchiveModule()
|
|
{
|
|
var root = context.CreateTempDirectory("bare");
|
|
var options = new ServerOptions
|
|
{
|
|
DataPath = Path.Combine(root, "data"),
|
|
ArchivePath = Path.Combine(root, "archive"),
|
|
HttpPort = 0,
|
|
HttpsPort = 0,
|
|
Database = DatabaseKind.Sqlite,
|
|
Discovery = false,
|
|
};
|
|
var error = Assert.Throws<InvalidOperationException>(() => ServerHost.Build(options, []));
|
|
Assert.Contains("IArchiveIngest", error.Message);
|
|
Assert.Contains("IFrameAssetLocator", error.Message);
|
|
}
|
|
|
|
[Test]
|
|
public void TitanoDbAcceptsAConnectionString()
|
|
{
|
|
var root = context.CreateTempDirectory("env");
|
|
var options = ServerOptions.Load(new Dictionary<string, string?>
|
|
{
|
|
["TITANO_DATA_PATH"] = root,
|
|
["TITANO_DB"] = "Host=db.lan;Port=6543;Database=archive;Username=titano;Password=secret",
|
|
});
|
|
Assert.Equal(DatabaseKind.Postgres, options.Database);
|
|
var database = options.ToDatabaseOptions();
|
|
Assert.Equal(Dialect.Postgres, database.Dialect);
|
|
Assert.Equal(6543, Titano.Data.Postgres.PostgresSettings.Parse(database.ConnectionString).Port);
|
|
}
|
|
|
|
[Test]
|
|
public void PartsBuildAQuotedPostgresConnectionString()
|
|
{
|
|
var options = new ServerOptions
|
|
{
|
|
Database = DatabaseKind.Postgres, // the Windows default is SQLite
|
|
DbHost = "postgres",
|
|
DbName = "titano",
|
|
DbUser = "titano",
|
|
DbPassword = "it's;secret",
|
|
DbSslMode = "disable",
|
|
};
|
|
var settings = Titano.Data.Postgres.PostgresSettings.Parse(options.ToDatabaseOptions().ConnectionString);
|
|
Assert.Equal(("postgres", "titano", "it's;secret"), (settings.Host, settings.Database, settings.Password));
|
|
Assert.Equal(Titano.Data.Postgres.SslMode.Disable, settings.SslMode);
|
|
}
|
|
|
|
[Test]
|
|
public void EnvironmentVariablesLockTheirSettings()
|
|
{
|
|
var root = context.CreateTempDirectory("locked");
|
|
var options = ServerOptions.Load(new Dictionary<string, string?>
|
|
{
|
|
["TITANO_DATA_PATH"] = root,
|
|
["TITANO_DB"] = "postgres",
|
|
["TITANO_HTTP_PORT"] = "9090",
|
|
["TITANO_DISCOVERY"] = "false",
|
|
});
|
|
Assert.SequenceEqual(["Database", "HttpPort", "Discovery"], options.Locked);
|
|
Assert.Throws<InvalidOperationException>(() => ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_WORKERS"] = "many" }));
|
|
}
|
|
|
|
[Test]
|
|
public void TrustedProxiesAreAddressesOrNetworks()
|
|
{
|
|
var root = context.CreateTempDirectory("proxies");
|
|
// Loopback only: machines of the home network reach the HTTP port directly from private addresses.
|
|
var defaults = ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres" }).TrustedProxyNetworks();
|
|
foreach (var address in new[] { "127.0.0.1", "127.0.0.53", "::1" })
|
|
Assert.True(defaults.Any(n => n.Contains(IPAddress.Parse(address))), address);
|
|
foreach (var address in new[] { "10.20.30.40", "172.18.0.3", "192.168.1.2", "fd00::7", "203.0.113.5" })
|
|
Assert.False(defaults.Any(n => n.Contains(IPAddress.Parse(address))), address);
|
|
|
|
var options = ServerOptions.Load(new Dictionary<string, string?>
|
|
{
|
|
["TITANO_DATA_PATH"] = root,
|
|
["TITANO_DB"] = "postgres",
|
|
["TITANO_TRUSTED_PROXIES"] = "10.1.2.3, 2001:db8::/32;172.20.0.5/16",
|
|
});
|
|
Assert.Contains("TrustedProxies", options.Locked);
|
|
Assert.SequenceEqual(["10.1.2.3/32", "2001:db8::/32", "172.20.0.0/16"], options.TrustedProxyNetworks().Select(n => n.ToString()));
|
|
foreach (var invalid in new[] { "proxy.lan", "10.0.0.0/33", "fe80::1%2" })
|
|
{
|
|
var error = Assert.Throws<InvalidOperationException>(() => ServerOptions.Load(new Dictionary<string, string?>
|
|
{
|
|
["TITANO_DATA_PATH"] = root,
|
|
["TITANO_DB"] = "postgres",
|
|
["TITANO_TRUSTED_PROXIES"] = "127.0.0.1," + invalid,
|
|
}));
|
|
Assert.Contains(invalid, error.Message);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void PublicPortsComeFromTheEnvironment()
|
|
{
|
|
var root = context.CreateTempDirectory("public");
|
|
var options = ServerOptions.Load(new Dictionary<string, string?>
|
|
{
|
|
["TITANO_DATA_PATH"] = root,
|
|
["TITANO_DB"] = "postgres",
|
|
["TITANO_PUBLIC_HTTP_PORT"] = "18080",
|
|
["TITANO_PUBLIC_HTTPS_PORT"] = "18443",
|
|
});
|
|
Assert.Equal((18080, 18443), (options.PublicHttpPort, options.PublicHttpsPort));
|
|
Assert.Null(ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres" }).PublicHttpsPort);
|
|
foreach (var invalid in new[] { "0", "70000", "https" })
|
|
Assert.Throws<InvalidOperationException>(() => ServerOptions.Load(new Dictionary<string, string?> { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_PUBLIC_HTTPS_PORT"] = invalid }));
|
|
}
|
|
|
|
[Test]
|
|
public void HelpListsEveryVariable()
|
|
{
|
|
foreach (var variable in ServerOptions.EnvironmentVariables.Concat(["PUID", "PGID", "TZ"]))
|
|
Assert.True(Regex.IsMatch(ServerCommandLine.Usage, $@"\b{variable}\b"), variable);
|
|
}
|
|
}
|