using System.Text.Json; using System.Text.Json.Nodes; using Febe.Core.Collection; using Febe.Sonda; using Febe.Tests.Support; namespace Febe.Tests.Collection; /// /// Cleaning of probe samples (spec §14.5) on synthetic raw samples written by the test: invented, generic shapes /// with made-up people. Nothing real, nothing that claims to be Instagram's format. /// [TestClass] public sealed class SanitizerTests { private const string RawSample = """ { "descrittore": { "indirizzo": "https://www.instagram.com/api/qualcosa/mario.rossi", "host": "sito", "stato": 200, "tipo": "application/json" }, "corpo": { "utente": { "id": "5512345678", "nome_utente": "mario.rossi", "nome": "Mario Rossi", "privato": false, "seguiti_count": 312 }, "elementi": [ { "id": 3187654321098765, "taken_at": 1791878400, "didascalia": "Cena con Lucia al lago di Garda!! 🍝", "width": 1080, "height": 1350, "duration": 12.5, "media_type": "image", "url": "https://scontent-mxp1-1.cdninstagram.com/v/t51/123_456_n.jpg?stp=abc&oh=00_secret&oe=6712ABCD", "pagina": "https://www.instagram.com/p/CxYz123/", "visto": true, "vuoto": null } ], "per_id": { "17841400123456": { "nome_utente": "lucia.bianchi" } }, "cursore": "QVFIUjBzNmFQZ0pNb2xKd2E3ZQ==", "ripetuto": "mario.rossi" } } """; [TestMethod] public void StructureStaysAndEveryPersonalValueIsReplaced() { using var temp = new TempFolder("ripulitura"); var raw = Directory.CreateDirectory(temp.Combine("grezzi")).FullName; File.WriteAllText(Path.Combine(raw, "000001.json"), RawSample); File.WriteAllText(Path.Combine(raw, "interazioni.jsonl"), """ {"evento":"fuoco","percorso":"/mario.rossi/","elemento":{"role":"link","ariaLabel":"Storie di mario.rossi","href":{"host":"www.instagram.com","path":"/stories/mario.rossi/"}}} {"evento":"sopra","percorso":"/","elemento":{"role":"button","ariaLabel":"Chiudi"}} """); Directory.CreateDirectory(Path.Combine(raw, SampleSanitizer.BrowserProfileFolder)); File.WriteAllText(Path.Combine(raw, SampleSanitizer.BrowserProfileFolder, "Preferences.json"), """{"segreto":"cookie di sessione"}"""); var clean = temp.Combine("ripuliti"); var report = new SampleSanitizer(key: new byte[32]).SanitizeFolder(raw, clean); Assert.AreEqual(2, report.Files, "The browser profile is never part of the samples."); Assert.IsFalse(Directory.Exists(Path.Combine(clean, SampleSanitizer.BrowserProfileFolder))); var cleaned = JsonNode.Parse(File.ReadAllText(Path.Combine(clean, "000001.json")))!; var body = cleaned["corpo"]!; var item = body["elementi"]![0]!; // Structure and ordinary keys stay. CollectionAssert.AreEquivalent( new[] { "id", "taken_at", "didascalia", "width", "height", "duration", "media_type", "url", "pagina", "visto", "vuoto" }, item.AsObject().Select(p => p.Key).ToArray()); // Allowlisted fields stay: dimensions, durations, counts, type codes, dates; booleans and null always. Assert.AreEqual(1080, item["width"]!.GetValue()); Assert.AreEqual(12.5, item["duration"]!.GetValue()); Assert.AreEqual(1791878400, item["taken_at"]!.GetValue()); Assert.AreEqual("image", item["media_type"]!.GetValue()); Assert.AreEqual(312, body["utente"]!["seguiti_count"]!.GetValue()); Assert.IsTrue(item["visto"]!.GetValue()); Assert.IsNull(item["vuoto"]); // Strings and long numbers: fake values of the same shape. var name = body["utente"]!["nome_utente"]!.GetValue(); Assert.AreNotEqual("mario.rossi", name); Assert.AreEqual(11, name.Length); Assert.AreEqual('.', name[5], "Punctuation keeps the shape."); Assert.AreEqual(name, body["ripetuto"]!.GetValue(), "Equal values get equal fakes: relations survive."); var id = item["id"]!.GetValue(); Assert.AreNotEqual(3187654321098765, id); Assert.AreEqual(16, id.ToString(System.Globalization.CultureInfo.InvariantCulture).Length); Assert.AreEqual(10, body["utente"]!["id"]!.GetValue().Length); var caption = item["didascalia"]!.GetValue(); Assert.AreEqual("Cena con Lucia al lago di Garda!! 🍝".Length, caption.Length); Assert.IsFalse(caption.Contains("Lucia", StringComparison.Ordinal)); // Addresses: only site or CDN, and the file extension. StringAssert.StartsWith(item["url"]!.GetValue(), "https://cdn.esempio/"); StringAssert.EndsWith(item["url"]!.GetValue(), ".jpg"); StringAssert.StartsWith(item["pagina"]!.GetValue(), "https://sito.esempio/"); StringAssert.StartsWith(cleaned["descrittore"]!["indirizzo"]!.GetValue(), "https://sito.esempio/"); // Keys that look like identifiers are replaced too. var key = body["per_id"]!.AsObject().Single().Key; Assert.AreNotEqual("17841400123456", key); Assert.AreEqual(14, key.Length); var verify = SampleVerifier.Verify(raw, clean); Assert.IsTrue(verify.Passed, string.Join("; ", verify.Leaks.Select(l => $"{l.CleanFile} {l.Length} {l.Kind}"))); Assert.IsTrue(verify.CheckedTokens > 10); } [TestMethod] public void TheVerificationFailsWhenARawValueSurvivesAndDoesNotPrintIt() { using var temp = new TempFolder("ripulitura"); var raw = Directory.CreateDirectory(temp.Combine("grezzi")).FullName; File.WriteAllText(Path.Combine(raw, "000001.json"), RawSample); var clean = temp.Combine("ripuliti"); new SampleSanitizer().SanitizeFolder(raw, clean); // Someone «fixes» a cleaned file by hand and puts a real name back. var path = Path.Combine(clean, "000001.json"); var node = JsonNode.Parse(File.ReadAllText(path))!; node["corpo"]!["elementi"]![0]!["didascalia"] = "nota: lucia.bianchi"; File.WriteAllText(path, node.ToJsonString()); var report = SampleVerifier.Verify(raw, clean); Assert.IsFalse(report.Passed); Assert.IsTrue(report.Leaks.All(l => l.CleanFile == "000001.json")); Assert.IsTrue(report.Leaks.Any(l => l.Kind == "lettere")); foreach (var leak in report.Leaks) { Assert.IsFalse(leak.ToString().Contains("lucia", StringComparison.OrdinalIgnoreCase), "Where, never what."); } // A raw copy is the worst case. var copy = temp.Combine("copia"); Directory.CreateDirectory(copy); File.Copy(Path.Combine(raw, "000001.json"), Path.Combine(copy, "000001.json")); Assert.IsTrue(SampleVerifier.Verify(raw, copy).Leaks.Count > 5); // A missing cleaned file fails too. File.Delete(path); Assert.IsFalse(SampleVerifier.Verify(raw, clean).Passed); } [TestMethod] public void TheCleanFolderCannotBeInsideTheRawOne() { using var temp = new TempFolder("ripulitura"); var raw = Directory.CreateDirectory(temp.Combine("grezzi")).FullName; File.WriteAllText(Path.Combine(raw, "000001.json"), "{}"); Assert.ThrowsExactly(() => new SampleSanitizer().SanitizeFolder(raw, Path.Combine(raw, "ripuliti"))); } [TestMethod] public void RawSamplesAreRefusedInsideAGitRepository() { using var temp = new TempFolder("sonda"); Directory.CreateDirectory(temp.Combine("progetto", ".git")); var inside = temp.Combine("progetto", "campioni", "grezzi"); Assert.AreEqual(temp.Combine("progetto"), RepositoryGuard.FindRepository(inside)); var refused = Assert.ThrowsExactly(() => RepositoryGuard.EnsureOutside(inside)); StringAssert.Contains(refused.Message, "fuori dal repository"); RepositoryGuard.EnsureOutside(temp.Combine("altrove")); // A worktree has a «.git» file instead of a folder: refused too. Directory.CreateDirectory(temp.Combine("albero")); File.WriteAllText(temp.Combine("albero", ".git"), "gitdir: /altrove/.git/worktrees/albero\n"); Assert.IsNotNull(RepositoryGuard.FindRepository(temp.Combine("albero", "campioni"))); } [TestMethod] public void TheAllowlistIsShortAndExplicit() { var allow = SanitizerAllowlist.Default; Assert.AreEqual(AllowedField.Dimension, allow.FieldOf("original_width")); Assert.AreEqual(AllowedField.Date, allow.FieldOf("expiring_at")); Assert.AreEqual(AllowedField.Count, allow.FieldOf("comment_count")); Assert.AreEqual(AllowedField.None, allow.FieldOf("username")); Assert.AreEqual(AllowedField.None, allow.FieldOf("caption")); Assert.IsTrue(allow.KeepsString("media_type", "video")); Assert.IsFalse(allow.KeepsString("media_type", "una frase lunga con 12345 cifre"), "A code must look like a code."); Assert.IsTrue(allow.KeepsString("pubblicato", "2026-10-13T08:00:00Z")); Assert.IsFalse(allow.KeepsString("pubblicato", "ieri sera")); Assert.IsFalse(allow.KeepsString("width", "1080"), "Dimensions keep numbers only."); } [TestMethod] public void AddressesOutsideSiteAndCdnAreMarkedAsOther() { var sanitizer = new SampleSanitizer(hosts: new HostClassifier(["127.0.0.1"], ["127.0.0.2"])); var node = sanitizer.Sanitize(JsonNode.Parse("""{"a":"http://127.0.0.2:8482/m/f1/960x1200.jpg?scad=1&firma=x","b":"http://127.0.0.1:8481/profilo/marta.prova/","c":"https://esterno.example/x.webp"}"""))!; StringAssert.StartsWith(node["a"]!.GetValue(), "https://cdn.esempio/"); StringAssert.EndsWith(node["a"]!.GetValue(), ".jpg"); Assert.IsFalse(node["b"]!.GetValue().Contains("prova", StringComparison.Ordinal), "A user name after a dot is not an extension."); StringAssert.StartsWith(node["c"]!.GetValue(), "https://altro.esempio/"); StringAssert.EndsWith(node["c"]!.GetValue(), ".webp"); } [TestMethod] public void TheProbeReadsOnlyThroughAReadOnlyScript() => Febe.Browser.Cdp.ReadOnlyScriptGuard.EnsureReadOnly(ProbeRecorder.InteractionScript); [TestMethod] public void TheCleanedSamplesInTheRepositoryAreCleanedSamples() { // Every file of tests/campioni/prova comes out of «ripulisci»: fake addresses, no address of the test site. foreach (var file in Directory.EnumerateFiles(Path.Combine(AppContext.BaseDirectory, "campioni", "prova"), "*.json")) { var text = File.ReadAllText(file); Assert.IsFalse(text.Contains("127.0.0.", StringComparison.Ordinal), file); Assert.IsFalse(text.Contains("prova\"", StringComparison.Ordinal), file); using var document = JsonDocument.Parse(text); Assert.IsTrue(document.RootElement.TryGetProperty("corpo", out _)); } } }