CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
The owner renamed the product and moved the repository to the self-hosted Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control, and asked for Actions and a first automatic release with the server package and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md). - Chronos everywhere: a byte-exact rename of contents and paths, then a per-language review of the forms the old name's ending had dictated. - Gitea instead of GitHub: the update check, licence, issue and support links read ChronosReleasesUrl; the update check takes the highest stable version, offers a pre-release build its final release and reports a repository it cannot see as a failed check. EULA, security policy and Gitea issue forms live at the repository root. - Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release, nightly image and performance gates in the .NET SDK container with git checkout and the system SQLite. The release cross-compiles the Windows programs, checks their icon, version and DPI manifest, packs installers and zips, publishes the Gitea release and pushes the server image (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows runner (WINDOWS_RUNNER). - New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the docker-exec script, which Gitea's service containers do not allow; the throw-away server runs without durability) and pe-check. - Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR, 3 on the shared runner; the weekly performance gates keep theirs). BREAKING CHANGE: programs, settings, data folders, file types, environment variables and the Docker image use the new name; nothing is migrated from Titano, which never had a release. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
313 lines
16 KiB
C#
313 lines
16 KiB
C#
using System.Runtime.Versioning;
|
|
using System.Security.AccessControl;
|
|
using System.Security.Principal;
|
|
using Chronos.Core.Archiving;
|
|
|
|
namespace Chronos.Server.Hosting;
|
|
|
|
/// <summary>
|
|
/// Keeps the data folder and its secrets private (BRIEF §14.2): it holds the database, the TLS private key, the
|
|
/// temporary admin password and <c>server.json</c>. On Windows the folder sits in <c>%ProgramData%</c>, whose ACL lets
|
|
/// every local user read it and create files in it, so it gets a protected DACL (nothing inherited) that allows only
|
|
/// SYSTEM, Administrators and the account the server runs as; everything inside inherits that. Those accounts must also
|
|
/// own the folder and everything in it, and nothing in it may be a link: an owner can always change the ACL back, and
|
|
/// whoever planted a file chose what it says. Elsewhere the container entrypoint owns <c>/data</c>, and secrets are
|
|
/// written with mode 0600. An archive folder the server creates on Windows gets the same accounts (see
|
|
/// <see cref="PrepareArchive"/>).
|
|
/// </summary>
|
|
public static class DataFolderAccess
|
|
{
|
|
private const UnixFileMode OwnerOnly = UnixFileMode.UserRead | UnixFileMode.UserWrite;
|
|
private const int GenericAll = 0x10000000;
|
|
private const int GenericWrite = 0x40000000;
|
|
|
|
/// <summary>Rights that let an account replace or alter a file or folder (including the generic forms).</summary>
|
|
[SupportedOSPlatform("windows")]
|
|
private const FileSystemRights ChangeRights = FileSystemRights.WriteData | FileSystemRights.AppendData | FileSystemRights.WriteExtendedAttributes
|
|
| FileSystemRights.DeleteSubdirectoriesAndFiles | FileSystemRights.Delete | FileSystemRights.ChangePermissions | FileSystemRights.TakeOwnership
|
|
| (FileSystemRights)GenericWrite | (FileSystemRights)GenericAll;
|
|
|
|
/// <summary>
|
|
/// Creates the folder if needed and, on Windows, refuses a folder that holds a link or an entry owned by anyone
|
|
/// else, then replaces an ACL that grants anyone else access.
|
|
/// </summary>
|
|
/// <exception cref="InvalidOperationException">
|
|
/// Something in the folder is a link or belongs to another account, or the ACL cannot be read or changed (the account
|
|
/// is not an administrator).
|
|
/// </exception>
|
|
public static void Restrict(string folder)
|
|
{
|
|
if (!OperatingSystem.IsWindows())
|
|
{
|
|
Directory.CreateDirectory(folder);
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
RestrictOnWindows(new DirectoryInfo(folder));
|
|
}
|
|
catch (UnauthorizedAccessException ex)
|
|
{
|
|
throw new InvalidOperationException(
|
|
$"Chronos Server cannot check or restrict access to {folder}: {ex.Message} Run it once as an administrator; if someone else changed the folder's access, delete the folder.", ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads a file the server acts on (<c>server.json</c>). On Windows its owner must be SYSTEM, Administrators or the
|
|
/// server's account, checked on the handle that is read, so a file another user planted (or swapped in) is refused
|
|
/// before any of its settings is used.
|
|
/// </summary>
|
|
/// <exception cref="InvalidOperationException">The file belongs to another account.</exception>
|
|
public static string ReadTrustedText(string path)
|
|
{
|
|
if (!OperatingSystem.IsWindows()) return File.ReadAllText(path);
|
|
return ReadTrustedText(path, AllowedAccounts());
|
|
}
|
|
|
|
/// <summary>Writes a secret that only the server's account (and, on Windows, SYSTEM and Administrators) may read.</summary>
|
|
public static void WritePrivateFile(string path, ReadOnlySpan<byte> contents)
|
|
{
|
|
if (OperatingSystem.IsWindows())
|
|
{
|
|
WritePrivateFileOnWindows(new FileInfo(path), contents);
|
|
return;
|
|
}
|
|
using (var stream = new FileStream(path, new FileStreamOptions { Mode = FileMode.Create, Access = FileAccess.Write, UnixCreateMode = OwnerOnly }))
|
|
stream.Write(contents);
|
|
// A file that already existed keeps its old mode otherwise.
|
|
File.SetUnixFileMode(path, OwnerOnly);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates the archive folder when it is missing, and says whether accounts other than SYSTEM, Administrators and the
|
|
/// server's own can change it (always false off Windows, where the container entrypoint checks the archive). On
|
|
/// Windows a folder created here gets a protected ACL: those accounts change it, other local users may only read it;
|
|
/// a <c>.chronos</c> folder (staging, trash) created here is private. Whoever can write where the server moves files
|
|
/// can put a link in place of a folder and send the server's writes elsewhere; an existing archive is only reported,
|
|
/// because its ACL is its owner's choice.
|
|
/// </summary>
|
|
public static bool PrepareArchive(string root)
|
|
{
|
|
if (!OperatingSystem.IsWindows())
|
|
{
|
|
Directory.CreateDirectory(root);
|
|
return false;
|
|
}
|
|
var folder = new DirectoryInfo(root);
|
|
if (!folder.Exists)
|
|
{
|
|
if (folder.Parent is { } parent) parent.Create();
|
|
folder.Create(ArchiveFolderSecurity());
|
|
folder.Refresh();
|
|
}
|
|
// Created before the archive engine would create it with the archive's ACL; an existing one is left as it is.
|
|
var meta = new DirectoryInfo(new ArchivePaths(root).MetaDirectory);
|
|
if (!meta.Exists) meta.Create(PrivateFolderSecurity());
|
|
return OthersCanChange(folder);
|
|
}
|
|
|
|
/// <summary>
|
|
/// True when the ACL is protected, its owner is SYSTEM, Administrators or this process's account, and it names no
|
|
/// other account.
|
|
/// </summary>
|
|
[SupportedOSPlatform("windows")]
|
|
public static bool IsPrivate(FileSystemSecurity security)
|
|
{
|
|
if (!security.AreAccessRulesProtected) return false;
|
|
var allowed = AllowedAccounts();
|
|
if (security.GetOwner(typeof(SecurityIdentifier)) is not SecurityIdentifier owner || !allowed.Contains(owner)) return false;
|
|
foreach (FileSystemAccessRule rule in security.GetAccessRules(includeExplicit: true, includeInherited: true, typeof(SecurityIdentifier)))
|
|
{
|
|
if (rule.AccessControlType == AccessControlType.Allow && !allowed.Contains((SecurityIdentifier)rule.IdentityReference)) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The first problem with <paramref name="folder"/> or anything inside it, as a sentence for the operator, or null:
|
|
/// a link (it would carry what the server reads, writes and secures elsewhere) or an owner outside
|
|
/// <paramref name="owners"/> (who may have written the contents and can always change the ACL back). Links are
|
|
/// reported, never followed.
|
|
/// </summary>
|
|
[SupportedOSPlatform("windows")]
|
|
internal static string? FindUntrusted(DirectoryInfo folder, IReadOnlySet<SecurityIdentifier> owners)
|
|
{
|
|
var everything = new EnumerationOptions { AttributesToSkip = 0, IgnoreInaccessible = false };
|
|
var pending = new Stack<DirectoryInfo>();
|
|
pending.Push(folder);
|
|
while (pending.TryPop(out var directory))
|
|
{
|
|
if (Untrusted(directory, owners) is { } problem) return problem;
|
|
foreach (var entry in directory.EnumerateFileSystemInfos("*", everything))
|
|
{
|
|
if (entry is DirectoryInfo child && !IsLink(child)) pending.Push(child);
|
|
else if (Untrusted(entry, owners) is { } entryProblem) return entryProblem;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <inheritdoc cref="ReadTrustedText(string)"/>
|
|
[SupportedOSPlatform("windows")]
|
|
internal static string ReadTrustedText(string path, IReadOnlySet<SecurityIdentifier> owners)
|
|
{
|
|
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
if (UntrustedOwner(path, stream.GetAccessControl(), owners) is { } problem) throw new InvalidOperationException(problem);
|
|
using var reader = new StreamReader(stream);
|
|
return reader.ReadToEnd();
|
|
}
|
|
|
|
/// <summary>
|
|
/// True when the owner is outside <paramref name="trusted"/> or an allow entry gives change rights to such an
|
|
/// account. Entries that only pass to children count too, since new files would inherit them; CREATOR OWNER and
|
|
/// CREATOR GROUP stand for whoever creates a child, which already takes change rights on the folder.
|
|
/// </summary>
|
|
[SupportedOSPlatform("windows")]
|
|
internal static bool IsChangeableByOthers(FileSystemSecurity security, IReadOnlySet<SecurityIdentifier> trusted, bool checkOwner)
|
|
{
|
|
if (checkOwner && (security.GetOwner(typeof(SecurityIdentifier)) is not SecurityIdentifier owner || !trusted.Contains(owner))) return true;
|
|
foreach (FileSystemAccessRule rule in security.GetAccessRules(includeExplicit: true, includeInherited: true, typeof(SecurityIdentifier)))
|
|
{
|
|
if (rule.AccessControlType != AccessControlType.Allow || (rule.FileSystemRights & ChangeRights) == 0) continue;
|
|
var account = (SecurityIdentifier)rule.IdentityReference;
|
|
if (trusted.Contains(account) || account.IsWellKnown(WellKnownSidType.CreatorOwnerSid) || account.IsWellKnown(WellKnownSidType.CreatorGroupSid)) continue;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>SYSTEM, Administrators and the account this process runs as (SYSTEM again for the Windows service).</summary>
|
|
[SupportedOSPlatform("windows")]
|
|
internal static HashSet<SecurityIdentifier> AllowedAccounts()
|
|
{
|
|
using var identity = WindowsIdentity.GetCurrent();
|
|
var accounts = new HashSet<SecurityIdentifier>
|
|
{
|
|
new(WellKnownSidType.LocalSystemSid, null),
|
|
new(WellKnownSidType.BuiltinAdministratorsSid, null),
|
|
};
|
|
if (identity.User is { } user) accounts.Add(user);
|
|
return accounts;
|
|
}
|
|
|
|
[SupportedOSPlatform("windows")]
|
|
private static void RestrictOnWindows(DirectoryInfo folder)
|
|
{
|
|
if (!folder.Exists)
|
|
{
|
|
if (folder.Parent is { } parent) parent.Create();
|
|
folder.Create(PrivateFolderSecurity());
|
|
folder.Refresh();
|
|
}
|
|
// Before the ACL is touched: a folder another user created in advance, or a file planted in an older version's
|
|
// folder, stays theirs whatever the ACL says, and a link would carry the new ACL elsewhere.
|
|
if (FindUntrusted(folder, AllowedAccounts()) is { } problem) throw new InvalidOperationException(problem);
|
|
// An older version, or Windows' default inheritance from %ProgramData%, left the folder readable by every user.
|
|
// Setting the DACL by name also replaces what the files inside inherited.
|
|
if (!IsPrivate(folder.GetAccessControl())) folder.SetAccessControl(PrivateFolderSecurity());
|
|
}
|
|
|
|
[SupportedOSPlatform("windows")]
|
|
private static string? Untrusted(FileSystemInfo entry, IReadOnlySet<SecurityIdentifier> owners)
|
|
{
|
|
if (IsLink(entry))
|
|
{
|
|
return $"{entry.FullName} is a link to {entry.LinkTarget}, and Chronos Server follows no links in its data folder. "
|
|
+ "Delete the link (CHRONOS_DATA_PATH moves the data folder) and start the server again.";
|
|
}
|
|
FileSystemSecurity security = entry is DirectoryInfo directory
|
|
? directory.GetAccessControl(AccessControlSections.Owner)
|
|
: ((FileInfo)entry).GetAccessControl(AccessControlSections.Owner);
|
|
return UntrustedOwner(entry.FullName, security, owners);
|
|
}
|
|
|
|
[SupportedOSPlatform("windows")]
|
|
private static string? UntrustedOwner(string path, FileSystemSecurity security, IReadOnlySet<SecurityIdentifier> owners)
|
|
{
|
|
var owner = security.GetOwner(typeof(SecurityIdentifier)) as SecurityIdentifier;
|
|
if (owner is not null && owners.Contains(owner)) return null;
|
|
return $"{path} belongs to {(owner is null ? "no account" : AccountName(owner))}, not to SYSTEM, Administrators or the account Chronos Server "
|
|
+ "runs as, so it cannot be trusted. Check it, then delete it or make Administrators its owner, and start the server again.";
|
|
}
|
|
|
|
/// <summary>A symbolic link or junction (other reparse points, such as deduplicated files, are ordinary files).</summary>
|
|
private static bool IsLink(FileSystemInfo entry) => (entry.Attributes & FileAttributes.ReparsePoint) != 0 && entry.LinkTarget is not null;
|
|
|
|
[SupportedOSPlatform("windows")]
|
|
private static string AccountName(SecurityIdentifier account)
|
|
{
|
|
try
|
|
{
|
|
return account.Translate(typeof(NTAccount)).Value;
|
|
}
|
|
catch (IdentityNotMappedException)
|
|
{
|
|
return account.Value;
|
|
}
|
|
}
|
|
|
|
[SupportedOSPlatform("windows")]
|
|
private static bool OthersCanChange(DirectoryInfo folder)
|
|
{
|
|
try
|
|
{
|
|
return IsChangeableByOthers(folder.GetAccessControl(), AllowedAccounts(), checkOwner: true);
|
|
}
|
|
catch (NotSupportedException)
|
|
{
|
|
// A volume without ACLs (FAT, exFAT): everyone can change everything.
|
|
return true;
|
|
}
|
|
catch (Exception ex) when (ex is UnauthorizedAccessException or IOException or InvalidOperationException)
|
|
{
|
|
// The ACL cannot be read (some network shares); the warning would be a guess.
|
|
return false;
|
|
}
|
|
}
|
|
|
|
[SupportedOSPlatform("windows")]
|
|
private static void WritePrivateFileOnWindows(FileInfo file, ReadOnlySpan<byte> contents)
|
|
{
|
|
// A file that already exists is replaced, never rewritten in place: it would keep its owner, who could change its
|
|
// ACL back at any time, and a link would carry the secret elsewhere. The new file is owned by this account.
|
|
file.Delete();
|
|
using var stream = file.Create(FileMode.CreateNew, FileSystemRights.Write, FileShare.None, 4096, FileOptions.None, PrivateFileSecurity());
|
|
stream.Write(contents);
|
|
}
|
|
|
|
/// <summary>A protected DACL that gives the allowed accounts full control, passed on to everything inside.</summary>
|
|
[SupportedOSPlatform("windows")]
|
|
private static DirectorySecurity PrivateFolderSecurity()
|
|
{
|
|
var security = new DirectorySecurity();
|
|
security.SetAccessRuleProtection(isProtected: true, preserveInheritance: false);
|
|
foreach (var account in AllowedAccounts())
|
|
{
|
|
security.AddAccessRule(new FileSystemAccessRule(account, FileSystemRights.FullControl,
|
|
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
|
|
}
|
|
return security;
|
|
}
|
|
|
|
/// <summary>The private DACL, plus read access for every local user (photos and renders, never <c>.chronos</c>).</summary>
|
|
[SupportedOSPlatform("windows")]
|
|
private static DirectorySecurity ArchiveFolderSecurity()
|
|
{
|
|
var security = PrivateFolderSecurity();
|
|
security.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null), FileSystemRights.ReadAndExecute,
|
|
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
|
|
return security;
|
|
}
|
|
|
|
[SupportedOSPlatform("windows")]
|
|
private static FileSecurity PrivateFileSecurity()
|
|
{
|
|
var security = new FileSecurity();
|
|
security.SetAccessRuleProtection(isProtected: true, preserveInheritance: false);
|
|
foreach (var account in AllowedAccounts())
|
|
security.AddAccessRule(new FileSystemAccessRule(account, FileSystemRights.FullControl, AccessControlType.Allow));
|
|
return security;
|
|
}
|
|
}
|