using System.Collections.Concurrent; using System.Security.Cryptography; using System.Text; using Febe.Core.Collection.Requests; using Febe.Core.Library; using Febe.Core.Security; using Febe.Tests.Support; namespace Febe.Tests.Collection; /// /// The saved session on disk (issue #36, S4, spec §6.0, §13.2): Dati/sessione/sessione.json encrypted, owner-only, /// replaced at once; read back by a new start as a new capture; renewals serialised and kept in memory when the disk /// refuses them; «Scollega» deletes it for good; a file that cannot be read is no session, never a crash; no value in /// clear on disk, in a text or in a log line. /// [TestClass] public sealed class FileSiteSessionTests { private static readonly DateTimeOffset Now = new(2026, 11, 17, 10, 0, 0, TimeSpan.Zero); private static readonly Uri Graph = new("https://www.instagram.com/api/graphql"); private static readonly string[] Secrets = [ "segreto-sessione-1", "segreto-csrf-2", "segreto-percorso-3", "segreto-lsd-4", "segreto-dtsg-5", "segreto-rev-6", "segreto-nonce-7", "segreto-docid-8", "segreto-agente-9", "segreto-claim-10", ]; [TestMethod] public async Task WhatIsWrittenIsReadBackByANewInstance() { using var folder = new TempFolder("sessione"); var layout = new DataFolderLayout(folder.Path); var protector = Protector(); var store = new FileSiteSession(layout.SessionFile, protector); Assert.AreEqual(SavedSessionState.None, store.State); Assert.IsNull(store.Read()); var values = Values(); Assert.IsTrue(await store.ReplaceAsync(values, CancellationToken.None)); Assert.AreSame(values, store.Read()); Assert.AreEqual(SavedSessionState.Present, store.State); Assert.IsFalse(store.Unsaved); Assert.AreEqual(Path.Combine(folder.Path, "sessione", "sessione.json"), store.FilePath, "Dati/sessione/sessione.json."); var restored = new FileSiteSession(layout.SessionFile, protector).Read(); Assert.IsNotNull(restored); Assert.AreEqual(SavedSessionState.Present, new FileSiteSession(layout.SessionFile, protector).State); CollectionAssert.AreEqual( values.Cookies.Select(Describe).ToArray(), restored.Cookies.Select(Describe).ToArray(), "Name, value, domain, host-only, path, secure, HTTP-only and expiry, in order."); Assert.AreEqual(values.UserAgent, restored.UserAgent); CollectionAssert.AreEquivalent(values.Fields.ToArray(), restored.Fields.ToArray()); CollectionAssert.AreEquivalent(values.DocIds.ToArray(), restored.DocIds.ToArray()); Assert.AreEqual(values.CopiedAt, restored.CopiedAt); Assert.AreEqual("segreto-percorso-3", restored.CookieFor("ds_user_id", Graph, Now), "The path cookie still goes to /api/."); Assert.IsNull(restored.CookieFor("ds_user_id", new Uri("https://www.instagram.com/marta/"), Now)); } [TestMethod] public async Task AValueReadBackIsANewCaptureSoALateRenewalOfTheOldProcessTouchesNothing() { using var folder = new TempFolder("sessione"); var file = Path.Combine(folder.Path, "sessione", "sessione.json"); var protector = Protector(); var before = new FileSiteSession(file, protector); var values = Values(); await before.ReplaceAsync(values, CancellationToken.None); var after = new FileSiteSession(file, protector); var restored = after.Read()!; Assert.AreNotEqual(values.Capture, restored.Capture, "A new start is a new capture."); Assert.AreNotEqual(restored.Capture, new SiteSessionValues([], "agente").Capture, "And later captures never reuse its id."); await after.UpdateAsync(new SiteSessionUpdate([Cookie("sessionid", "vecchia-risposta")], capture: values.Capture), CancellationToken.None); Assert.AreEqual("segreto-sessione-1", after.Read()!.Cookie("sessionid")); Assert.AreEqual("segreto-sessione-1", new FileSiteSession(file, protector).Read()!.Cookie("sessionid"), "Nor the file."); } [TestMethod] public async Task TheFileHoldsNoValueInClear() { using var folder = new TempFolder("sessione"); var file = Path.Combine(folder.Path, "sessione.json"); var store = new FileSiteSession(file, Protector()); await store.ReplaceAsync(Values(), CancellationToken.None); var bytes = File.ReadAllBytes(file); Assert.AreEqual("FEBS", Encoding.ASCII.GetString(bytes, 0, 4)); foreach (var secret in Secrets.Append("sessionid").Append("instagram")) { foreach (var form in new[] { Encoding.UTF8.GetBytes(secret), Encoding.Unicode.GetBytes(secret), Encoding.ASCII.GetBytes(Convert.ToBase64String(Encoding.UTF8.GetBytes(secret))) }) { Assert.AreEqual(-1, bytes.AsSpan().IndexOf(form), $"«{secret}» is on disk in clear."); } } } [TestMethod] public async Task AFailedWriteKeepsTheRenewedCookiesInMemoryAndIsTriedAgainLater() { using var folder = new TempFolder("sessione"); var file = Path.Combine(folder.Path, "sessione.json"); var protector = new FlakyProtector(Protector()); var log = new ConcurrentQueue(); var store = new FileSiteSession(file, protector, log.Enqueue); await store.ReplaceAsync(Values(), CancellationToken.None); var capture = store.Read()!.Capture; // The site rotated csrftoken: the disk refuses, the memory keeps the new value (the old one no longer works). protector.Fail = true; await store.UpdateAsync(new SiteSessionUpdate([Csrf("segreto-rinnovo-11")], capture: capture), CancellationToken.None); await store.UpdateAsync(new SiteSessionUpdate([Csrf("segreto-rinnovo-12")], capture: capture), CancellationToken.None); Assert.AreEqual("segreto-rinnovo-12", store.Read()!.Cookie("csrftoken")); Assert.IsTrue(store.Unsaved); Assert.AreEqual("segreto-csrf-2", new FileSiteSession(file, Protector(protector)).Read()!.Cookie("csrftoken"), "The file is the old one, whole."); Assert.AreEqual(1, log.Count(l => l.Contains("non scritta", StringComparison.Ordinal)), "One line for a run of failures:\n" + string.Join("\n", log)); Assert.IsFalse(Directory.GetFiles(folder.Path).Any(f => f.EndsWith(OwnerOnlyFiles.PartSuffix, StringComparison.Ordinal))); // A failed copy at «Ho finito» still replaces the session in memory, and says it is not on disk. Assert.IsFalse(await store.ReplaceAsync(Values(csrf: "segreto-nuovo-accesso-13"), CancellationToken.None)); Assert.AreEqual("segreto-nuovo-accesso-13", store.Read()!.Cookie("csrftoken")); capture = store.Read()!.Capture; protector.Fail = false; await store.UpdateAsync(new SiteSessionUpdate([Cookie("mid", "segreto-mid-14")], capture: capture), CancellationToken.None); Assert.IsFalse(store.Unsaved); var reread = new FileSiteSession(file, Protector(protector)).Read()!; Assert.AreEqual("segreto-nuovo-accesso-13", reread.Cookie("csrftoken"), "The next write carries everything kept in memory."); Assert.AreEqual("segreto-mid-14", reread.Cookie("mid")); Assert.IsTrue(log.Any(l => l.Contains("di nuovo", StringComparison.Ordinal))); AssertNoSecret(log); } [TestMethod] public async Task AWriteTheDiskRefusesAfterEncryptionLeavesNoPartFileKeepsTheMemoryAndTheNextWriteSucceeds() { using var folder = new TempFolder("sessione"); var file = Path.Combine(folder.Path, "sessione.json"); var protector = Protector(); var log = new ConcurrentQueue(); var store = new FileSiteSession(file, protector, log.Enqueue); // Something the rename cannot replace stands where the session goes: the part file is written, the move fails. Directory.CreateDirectory(file); Assert.IsFalse(await store.ReplaceAsync(Values(), CancellationToken.None)); Assert.IsTrue(store.Unsaved); Assert.AreEqual("segreto-csrf-2", store.Read()!.Cookie("csrftoken"), "Kept in memory."); Assert.IsEmpty(Directory.GetFiles(folder.Path, "*" + OwnerOnlyFiles.PartSuffix), "A failed write leaves no part file."); Assert.AreEqual(1, log.Count(l => l.Contains("non scritta", StringComparison.Ordinal)), string.Join("\n", log)); // The obstacle goes: the next write carries what memory kept. Directory.Delete(file); var capture = store.Read()!.Capture; await store.UpdateAsync(new SiteSessionUpdate([Cookie("mid", "segreto-mid-14")], capture: capture), CancellationToken.None); Assert.IsFalse(store.Unsaved); var reread = new FileSiteSession(file, protector).Read()!; Assert.AreEqual("segreto-csrf-2", reread.Cookie("csrftoken")); Assert.AreEqual("segreto-mid-14", reread.Cookie("mid")); if (OperatingSystem.IsWindows()) { // On Windows a file held open without sharing cannot be replaced: the old file stays whole. var before = File.ReadAllBytes(file); using (new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.None)) { await store.UpdateAsync(new SiteSessionUpdate([Cookie("mid", "segreto-mid-15")], capture: capture), CancellationToken.None); } Assert.IsTrue(store.Unsaved); CollectionAssert.AreEqual(before, File.ReadAllBytes(file), "The old file is intact."); Assert.IsEmpty(Directory.GetFiles(folder.Path, "*" + OwnerOnlyFiles.PartSuffix)); } AssertNoSecret(log); } [TestMethod] public async Task ConcurrentUpdatesAreSerializedAndNoneIsLost() { using var folder = new TempFolder("sessione"); var file = Path.Combine(folder.Path, "sessione.json"); var protector = Protector(); var store = new FileSiteSession(file, protector); await store.ReplaceAsync(Values(), CancellationToken.None); var capture = store.Read()!.Capture; await Task.WhenAll(Enumerable.Range(0, 40).Select(i => Task.Run(() => store.UpdateAsync(new SiteSessionUpdate([Cookie("c" + i.ToString(System.Globalization.CultureInfo.InvariantCulture), "v")], capture: capture), CancellationToken.None)))); Assert.AreEqual(Values().Cookies.Count + 40, store.Read()!.Cookies.Count); Assert.AreEqual(Values().Cookies.Count + 40, new FileSiteSession(file, protector).Read()!.Cookies.Count, "The last write holds every update."); } [TestMethod] public async Task DeleteRemovesTheFileAndItsLeftoversAndNoLateUpdateBringsItBack() { using var folder = new TempFolder("sessione"); var file = Path.Combine(folder.Path, "sessione.json"); var protector = new FlakyProtector(Protector()); var log = new List(); var store = new FileSiteSession(file, protector, log.Add); await store.ReplaceAsync(Values(), CancellationToken.None); var capture = store.Read()!.Capture; File.WriteAllText(file + ".1234abcd" + OwnerOnlyFiles.PartSuffix, "resto"); // An update waits behind a write that is in progress; «Scollega» comes; whatever the order they get the gate // in, no session is left. using var hold = new ManualResetEventSlim(false); protector.Hold = hold; var replacing = Task.Run(() => store.ReplaceAsync(store.Read()!, CancellationToken.None)); Assert.IsTrue(protector.Entered.Wait(TimeSpan.FromSeconds(30))); var updating = store.UpdateAsync(new SiteSessionUpdate([Cookie("tardivo", "x")], capture: capture), CancellationToken.None); var deleting = store.DeleteAsync(CancellationToken.None); hold.Set(); await Task.WhenAll(replacing, updating, deleting); await store.UpdateAsync(new SiteSessionUpdate([Cookie("tardivo", "y")], capture: capture), CancellationToken.None); Assert.IsNull(store.Read()); Assert.AreEqual(SavedSessionState.None, store.State); Assert.IsFalse(File.Exists(file)); Assert.AreEqual(0, Directory.GetFiles(folder.Path).Length, "No part file left either."); Assert.IsNull(new FileSiteSession(file, protector).Read()); Assert.IsTrue(log.Any(l => l.Contains("cancellata", StringComparison.Ordinal))); await store.DeleteAsync(CancellationToken.None); } [TestMethod] public async Task AnUnreadableFileIsNoSessionAndIsLeftAsItIs() { using var folder = new TempFolder("sessione"); var file = Path.Combine(folder.Path, "sessione.json"); var protector = Protector(); await new FileSiteSession(file, protector).ReplaceAsync(Values(), CancellationToken.None); var written = File.ReadAllBytes(file); // Another key (a new FEBE_CHIAVE_SESSIONE, another user for DPAPI), a damaged file, another format. var cases = new (string File, ISessionProtector Protector, byte[] Bytes)[] { (file, Protector(), written), (Path.Combine(folder.Path, "rovinato.json"), protector, [.. written.Take(written.Length - 3)]), (Path.Combine(folder.Path, "testo.json"), protector, Encoding.UTF8.GetBytes("""{"format":1,"cookies":[]}""")), (Path.Combine(folder.Path, "formato.json"), protector, protector.Protect(Encoding.UTF8.GetBytes("""{"format":9,"segreto":"segreto-formato-15"}"""))), (Path.Combine(folder.Path, "cookie.json"), protector, protector.Protect(Encoding.UTF8.GetBytes("""{"format":1,"userAgent":"a","cookies":[{"name":"a=b","value":"segreto-cookie-16","domain":"x","hostOnly":true,"path":"/","secure":false,"httpOnly":false}],"fields":{},"docIds":{}}"""))), }; foreach (var (path, reader, bytes) in cases) { if (path != file) { File.WriteAllBytes(path, bytes); } var log = new List(); var store = new FileSiteSession(path, reader, log.Add); Assert.IsNull(store.Read(), path); Assert.AreEqual(SavedSessionState.Unreadable, store.State, path); CollectionAssert.AreEqual(bytes, File.ReadAllBytes(path), "Left as it is."); Assert.AreEqual(1, log.Count); StringAssert.Contains(log[0], "illeggibile"); Assert.IsFalse(log[0].Contains("segreto", StringComparison.Ordinal) || log[0].Contains(path, StringComparison.Ordinal), log[0]); Assert.AreEqual("Sessione salvata illeggibile.", store.ToString()); // A renewal finds no session; a new login replaces the file. await store.UpdateAsync(new SiteSessionUpdate([Cookie("x", "y")]), CancellationToken.None); Assert.IsNull(store.Read()); Assert.IsTrue(await store.ReplaceAsync(Values(), CancellationToken.None)); Assert.AreEqual(SavedSessionState.Present, new FileSiteSession(path, reader).State); } } [TestMethod] public async Task TheFolderAndTheFileAreOwnerOnlyOnUnix() { using var folder = new TempFolder("sessione"); var layout = new DataFolderLayout(folder.Path); await new FileSiteSession(layout.SessionFile, Protector()).ReplaceAsync(Values(), CancellationToken.None); Assert.IsTrue(File.Exists(layout.SessionFile)); Assert.AreEqual(layout.Session, Path.GetDirectoryName(layout.SessionFile)); Assert.AreEqual(layout.Session, Path.GetDirectoryName(layout.SessionKeyFile)); if (!OperatingSystem.IsWindows()) { Assert.AreEqual(OwnerOnlyFiles.OwnerFile, File.GetUnixFileMode(layout.SessionFile)); Assert.AreEqual(OwnerOnlyFiles.OwnerFolder, File.GetUnixFileMode(layout.Session)); } // The daily copies and the diagnostic report live elsewhere and never look in Dati/sessione. Assert.IsFalse(layout.Session.StartsWith(layout.Backups, StringComparison.Ordinal)); Assert.IsFalse(layout.Session.StartsWith(layout.Diagnostics, StringComparison.Ordinal)); } [TestMethod] public async Task TheTextAndTheLogLinesShowCountsOnly() { using var folder = new TempFolder("sessione"); var file = Path.Combine(folder.Path, "sessione.json"); var log = new List(); var protector = Protector(); var store = new FileSiteSession(file, protector, log.Add); Assert.AreEqual("Nessuna sessione salvata.", store.ToString()); await store.ReplaceAsync(Values(), CancellationToken.None); await store.UpdateAsync(new SiteSessionUpdate([Csrf("segreto-rinnovo-17")], claim: "segreto-claim-18"), CancellationToken.None); _ = new FileSiteSession(file, protector, log.Add); await store.DeleteAsync(CancellationToken.None); var texts = log.Append(store.ToString()).Append(protector.ToString()).ToList(); AssertNoSecret(texts); foreach (var text in texts) { Assert.IsFalse(text.Contains("csrftoken", StringComparison.Ordinal) || text.Contains(folder.Path, StringComparison.Ordinal), text); } Assert.IsTrue(log.Any(l => l.Contains("Sessione salvata scritta: 4 cookie", StringComparison.Ordinal)), string.Join("\n", log)); Assert.IsTrue(log.Any(l => l.Contains("Sessione salvata letta", StringComparison.Ordinal))); } /// A session with a cookie of each kind, every page value and a doc_id; values from . private static SiteSessionValues Values(string csrf = "segreto-csrf-2") => new( [ new SiteCookie("sessionid", "segreto-sessione-1", ".instagram.com", "/", hostOnly: false, secure: true, httpOnly: true, expires: Now.AddDays(365)), Csrf(csrf), SiteCookie.ForHost("ds_user_id", "segreto-percorso-3", "www.instagram.com", "/api/", secure: true), SiteCookie.ForHost("rur", "\"CLN\\054segreto\"", "www.instagram.com"), ], "Mozilla/5.0 segreto-agente-9", new Dictionary { ["lsd"] = "segreto-lsd-4", ["fb_dtsg"] = "segreto-dtsg-5", ["rev"] = "segreto-rev-6", ["entry_nonce"] = "segreto-nonce-7", ["claim"] = "segreto-claim-10", ["dpr"] = "2", }, new Dictionary { ["PolarisProfilePostsQuery"] = "segreto-docid-8" }, Now); /// The csrftoken of Instagram as the browser keeps it (Domain=.instagram.com, secure): a renewal has the same key. private static SiteCookie Csrf(string value) => new("csrftoken", value, ".instagram.com", "/", hostOnly: false, secure: true, expires: Now.AddDays(30)); private static SiteCookie Cookie(string name, string value) => SiteCookie.ForHost(name, value, "www.instagram.com"); private static string Describe(SiteCookie c) => $"{c.Name}|{c.Value}|{c.Domain}|{c.HostOnly}|{c.Path}|{c.Secure}|{c.HttpOnly}|{c.Expires:O}"; private static readonly ConcurrentDictionary Keys = new(); /// A protector with a key made now (never a key in the repository). private static AesGcmSessionProtector Protector() { var key = RandomNumberGenerator.GetBytes(SessionKey.Length); var protector = new AesGcmSessionProtector(key); Keys[protector] = key; return protector; } /// Another protector with the same key as 's. private static AesGcmSessionProtector Protector(FlakyProtector flaky) => new(Keys[flaky.Inner]); private static void AssertNoSecret(IEnumerable texts) { foreach (var text in texts) { Assert.IsFalse(text.Contains("segreto", StringComparison.Ordinal), text); } } /// A protector that can refuse to write (as a full or read-only disk would) or hold a write halfway. private sealed class FlakyProtector(AesGcmSessionProtector inner) : ISessionProtector { public AesGcmSessionProtector Inner => inner; public bool Fail { get; set; } public ManualResetEventSlim? Hold { get; set; } public ManualResetEventSlim Entered { get; } = new(false); public string Scheme => inner.Scheme; public byte[] Protect(ReadOnlySpan plain) { if (Hold is { } hold) { Hold = null; Entered.Set(); hold.Wait(TimeSpan.FromSeconds(30)); } return Fail ? throw new IOException("Disco pieno.") : inner.Protect(plain); } public byte[] Unprotect(ReadOnlySpan blob) => inner.Unprotect(blob); } }