Files
Febe/tests/Febe.Tests/Collection/HarImportTests.cs
T
Alby96andClaude Opus 5.5 31bbbf8bb7
verifica / controlli (push) Successful in 2m10s
verifica / prove (push) Successful in 26m16s
verifica / installatore (push) Successful in 5m39s
verifica / immagine (push) Successful in 28m39s
Instagram: Febe legge le risposte vere (campioni dal HAR del 21/10)
Febe non estraeva nulla da Instagram per due motivi. Instagram manda i
dati di storie e raccolte (GraphQL) come text/javascript, e la cattura
leggeva solo le risposte dichiarate JSON: le scartava tutte. E i file di
Instagram (lettori, bersagli) erano scheletri, quindi le ricette si
rifiutavano di girare.

Alberto ha mandato un HAR del suo browser e una guida. La guida descrive
richieste costruite a mano, vietate dalle regole 2-4, e un formato che
nel HAR non compare: non si usa. Dal HAR si è letta solo la forma; le
risposte utili sono diventate campioni grezzi fuori dal repository,
ripuliti e verificati con febe-sonda (tests/Febe.Tests/campioni/instagram).

Che cosa cambia:
- la cattura legge le risposte text/javascript chieste dalla pagina (mai
  gli script) e ogni blocco JSON incorporato nel documento della pagina,
  e avvisa quando un corpo atteso non arriverà;
- le risposte che non parlano di contenuti (promozioni, «storia vista»)
  si ignorano invece di valere come formato non riconosciuto;
- lettori.instagram.json è confermato, con sole chiavi presenti nei
  campioni; identificativi delle raccolte nella forma dell'indirizzo;
- bersagli.instagram.json ha le pagine viste nel traffico vero; i
  bersagli restano vuoti (H11): storie, raccolte, Taggati e Reel
  aspettano lo script tools/bersagli/raccogli-bersagli.js;
- le ricette del profilo reggono dati in risposte diverse e prendono la
  paginazione solo dalla risposta che porta i loro post; l'elenco dei
  seguiti scarta i profili suggeriti; «Tu» si riconosce anche dal solo
  identificativo dell'account collegato;
- il visualizzatore delle raccolte conta i contenuti (l'indirizzo non
  cambia), chiede il permesso prima di ogni contenuto e si chiude sempre;
- una ricetta che la configurazione non lascia girare è «Da configurare»:
  non è un guasto, non si sospende, non si pianifica; le sospensioni
  dovute ai file vecchi si azzerano una volta al cambio dei file;
- la barra laterale dice «Collegato» dal cookie di sessione; i «Post
  singolo» per gli indirizzi scaduti non vengono più annullati;
- febe-sonda da-har; la ripulitura tiene latest_reel_media e mette un
  riempitivo neutro sotto le chiavi simili a segreti (gitleaks).

Prove su Windows: 783 superate, 76 saltate (Chromium e Xvfb, nella CI).
Nulla è stato provato contro Instagram. Dettagli e decisioni in
docs/stato.md e docs/decisioni.md (righe del 25 ottobre).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 14:22:16 +01:00

264 lines
16 KiB
C#

using System.Text;
using System.Text.Json.Nodes;
using Febe.Sonda;
using Febe.Tests.Support;
namespace Febe.Tests.Collection;
/// <summary>
/// The tools with which Alberto provides evidence from his own browser: the conversion of a HAR into the probe's raw
/// format (<c>febe-sonda da-har</c>) and the targets snippet for the DevTools console. The HAR is synthetic, written by
/// the test: invented generic values, nothing that claims to be Instagram's format.
/// </summary>
[TestClass]
public sealed class HarImportTests
{
private const string FirstBody = """{"dati":{"elementi":[{"numero":"5512345678","nome":"mario.rossi","larghezza":1080}]}}""";
private const string SecondBody = """{"stato":"ok","persone":[{"nome":"lucia.bianchi"}]}""";
private const string EmbeddedBlock = """{"profilo":{"nome":"mario.rossi","privato":false}}""";
private const string SecondEmbeddedBlock = """{"pagina":{"altre":false},"elementi":[]}""";
/// <summary>The request parameter that names a GraphQL operation (the only part of a request body ever read).</summary>
private const string Operation = HarImporter.OperationParameter;
[TestMethod]
public void OnlyDataAnswersOfTheSiteAndEmbeddedDataAreKeptWithoutHeadersCookiesOrQueries()
{
using var temp = new TempFolder("har");
var har = temp.Combine("prova.har");
File.WriteAllText(har, SyntheticHar().ToJsonString());
var raw = temp.Combine("grezzi");
var report = HarImporter.Import(har, raw);
Assert.AreEqual(new HarImportReport(Entries: 12, DataResponses: 4, Documents: 1, DocumentBlocks: 2, OtherHosts: 2, NotData: 2, WithoutBody: 1, NotJson: 1, DocumentsWithoutData: 1), report);
Assert.AreEqual(6, report.Written);
// Flat files named by number and kind: a GraphQL operation name only when it is one, never a path.
CollectionAssert.AreEqual(
new[] { "000001-ProvaElencoQuery.json", "000002-risposta.json", "000003-ProvaPaginaSuccessivaQuery.json", "000004-risposta.json", "000005-documento.json", "000006-documento.json", "LEGGIMI.txt" },
Directory.GetFiles(raw).Select(Path.GetFileName).Order(StringComparer.Ordinal).ToArray());
// Headers, cookies, query strings and request bodies never reach the output.
foreach (var file in Directory.GetFiles(raw))
{
var text = File.ReadAllText(file);
foreach (var secret in new[] { "segreto", "sessionid", "cookie", "igsh", "variables", "pacchetto", "nascosto", "payload", "esterno" })
{
Assert.IsFalse(text.Contains(secret, StringComparison.OrdinalIgnoreCase), $"{Path.GetFileName(file)}: {secret}");
}
}
var first = Sample(raw, "000001-ProvaElencoQuery.json");
var descriptor = first["descrittore"]!;
Assert.AreEqual("https://www.instagram.com/graphql/query", descriptor["indirizzo"]!.GetValue<string>());
Assert.AreEqual("sito", descriptor["host"]!.GetValue<string>());
Assert.AreEqual(200, descriptor["stato"]!.GetValue<int>());
Assert.AreEqual("text/javascript", descriptor["tipo"]!.GetValue<string>(), "Lower case, no parameters.");
Assert.AreEqual("XHR", descriptor["risorsa"]!.GetValue<string>());
Assert.AreEqual("2026-10-21T15:35:04Z", descriptor["quando"]!.GetValue<string>(), "UTC, to the second.");
Assert.IsTrue(JsonNode.DeepEquals(JsonNode.Parse(FirstBody), first["corpo"]), "The base64 body, decoded and parsed.");
CollectionAssert.AreEquivalent(new[] { "descrittore", "corpo" }, first.AsObject().Select(p => p.Key).ToArray());
var second = Sample(raw, "000002-risposta.json");
Assert.AreEqual("https://www.instagram.com/api/v1/prova/mario.rossi/info/", second["descrittore"]!["indirizzo"]!.GetValue<string>());
Assert.AreEqual("Fetch", second["descrittore"]!["risorsa"]!.GetValue<string>());
Assert.AreEqual("application/json", second["descrittore"]!["tipo"]!.GetValue<string>());
Assert.IsTrue(JsonNode.DeepEquals(JsonNode.Parse(SecondBody), second["corpo"]));
// A page document: one sample per valid JSON block it embeds, in order and with the document's descriptor, as
// Febe's capture hands them to the readers; nothing of its scripts.
var page = Sample(raw, "000005-documento.json");
Assert.AreEqual("Document", page["descrittore"]!["risorsa"]!.GetValue<string>());
Assert.AreEqual("text/html", page["descrittore"]!["tipo"]!.GetValue<string>());
Assert.AreEqual("https://www.instagram.com/mario.rossi/", page["descrittore"]!["indirizzo"]!.GetValue<string>());
Assert.IsTrue(JsonNode.DeepEquals(JsonNode.Parse(EmbeddedBlock), page["corpo"]));
var nextBlock = Sample(raw, "000006-documento.json");
Assert.IsTrue(JsonNode.DeepEquals(page["descrittore"], nextBlock["descrittore"]));
Assert.IsTrue(JsonNode.DeepEquals(JsonNode.Parse(SecondEmbeddedBlock), nextBlock["corpo"]));
// The folder is a raw folder like the probe's: «ripulisci» and «verifica» work on it unchanged.
var clean = temp.Combine("ripuliti");
var cleaned = new SampleSanitizer(key: new byte[32]).SanitizeFolder(raw, clean);
Assert.AreEqual(6, cleaned.Files);
var verify = SampleVerifier.Verify(raw, clean);
Assert.IsTrue(verify.Passed, string.Join("; ", verify.Leaks.Select(l => $"{l.CleanFile} {l.Length} {l.Kind}")));
Assert.IsFalse(File.ReadAllText(Path.Combine(clean, "000001-ProvaElencoQuery.json")).Contains("mario", StringComparison.Ordinal));
}
[TestMethod]
public void TheConversionRefusesRepositoriesUsedFoldersAndFilesThatAreNotAHar()
{
using var temp = new TempFolder("har");
var har = temp.Combine("prova.har");
File.WriteAllText(har, SyntheticHar().ToJsonString());
// Raw output inside a repository: refused before anything is written.
Directory.CreateDirectory(temp.Combine("progetto", ".git"));
var inside = temp.Combine("progetto", "grezzi");
StringAssert.Contains(Assert.ThrowsExactly<InvalidOperationException>(() => HarImporter.Import(har, inside)).Message, "fuori dal repository");
Assert.IsFalse(Directory.Exists(inside));
// The HAR itself holds cookies: it is not read from inside a repository either.
var harInside = temp.Combine("progetto", "prova.har");
File.Copy(har, harInside);
Assert.ThrowsExactly<InvalidOperationException>(() => HarImporter.Import(harInside, temp.Combine("altrove")));
// A folder that is already in use: refused, nothing is mixed or overwritten.
var used = Directory.CreateDirectory(temp.Combine("usata")).FullName;
File.WriteAllText(Path.Combine(used, "000001-risposta.json"), "{}");
StringAssert.Contains(Assert.ThrowsExactly<InvalidOperationException>(() => HarImporter.Import(har, used)).Message, "non è vuota");
Assert.AreEqual("{}", File.ReadAllText(Path.Combine(used, "000001-risposta.json")));
Assert.ThrowsExactly<FileNotFoundException>(() => HarImporter.Import(temp.Combine("manca.har"), temp.Combine("nuova1")));
File.WriteAllText(temp.Combine("non-har.har"), """{"altro":[]}""");
Assert.ThrowsExactly<InvalidDataException>(() => HarImporter.Import(temp.Combine("non-har.har"), temp.Combine("nuova2")));
File.WriteAllText(temp.Combine("rotto.har"), "{\"log\":");
Assert.ThrowsExactly<InvalidDataException>(() => HarImporter.Import(temp.Combine("rotto.har"), temp.Combine("nuova3")));
File.WriteAllText(temp.Combine("elenco.har"), "[1,2]");
Assert.ThrowsExactly<InvalidDataException>(() => HarImporter.Import(temp.Combine("elenco.har"), temp.Combine("nuova4")));
Assert.IsFalse(Directory.Exists(temp.Combine("nuova4")), "Nothing is created for a file that is not a HAR.");
}
[TestMethod]
public void OnlyNamesOfTheSiteCodeNameAFile()
{
Assert.AreEqual("PolarisStoriesV3HighlightsPageQuery", HarImporter.OperationName("PolarisStoriesV3HighlightsPageQuery"));
Assert.AreEqual("usePolarisRegisterInRecentSearchesMutation", HarImporter.OperationName(" usePolarisRegisterInRecentSearchesMutation "));
Assert.AreEqual("ProvaAggiornamentiSubscription", HarImporter.OperationName("ProvaAggiornamentiSubscription"));
foreach (var value in new[] { null, "", "Query", "mario.rossi", "mario_rossi", "Pagina di mario Query", "Profilo12345Query", "/mario.rossi/Query", "ProvaElenco" })
{
Assert.IsNull(HarImporter.OperationName(value), value ?? "null");
}
}
[TestMethod]
public void TheTargetsSnippetOnlyListensAndReads()
{
var path = Febe.Tests.Desktop.Support.Repository.PathOf("tools", "bersagli", "raccogli-bersagli.js");
var script = File.ReadAllText(path);
// The same deny-list as Febe's own page scripts: no gesture, no DOM change, no request, no cookie or storage.
Febe.Browser.Cdp.ReadOnlyScriptGuard.EnsureReadOnly(script);
foreach (var word in new[] { "preventDefault", "stopPropagation", "stopImmediatePropagation", "postMessage", "document.cookie", "cookieStore", "Storage", "indexedDB", "fetch(", "XMLHttpRequest", "sendBeacon", "WebSocket", "new Image", "innerHTML", "outerHTML", "classList", ".style", "setAttribute", "removeAttribute", "eval(", "Function(", "open(" })
{
Assert.IsFalse(script.Contains(word, StringComparison.Ordinal), word);
}
// One capturing click listener, and the function that copies the records.
foreach (var part in new[] { "addEventListener('click', onClick, { capture: true, passive: true })", "globalThis.febeBersagli = ", "copy(text)" })
{
Assert.IsTrue(script.Contains(part, StringComparison.Ordinal), part);
}
Assert.AreEqual(1, script.Split("addEventListener").Length - 1, "A single listener.");
Assert.IsTrue(File.Exists(Path.Combine(Path.GetDirectoryName(path)!, "LEGGIMI.md")));
}
private static JsonNode Sample(string folder, string name) => JsonNode.Parse(File.ReadAllText(Path.Combine(folder, name)))!;
private static string Base64(string text) => Convert.ToBase64String(Encoding.UTF8.GetBytes(text));
private static JsonObject SyntheticHar()
{
static JsonArray Headers(params (string Name, string Value)[] headers) =>
[.. headers.Select(h => (JsonNode)new JsonObject { ["name"] = h.Name, ["value"] = h.Value })];
static JsonObject Entry(string resourceType, string method, string url, int status, JsonObject content, JsonObject? postData = null, string started = "2026-10-21T17:35:04.512+02:00")
{
var request = new JsonObject
{
["method"] = method,
["url"] = url,
["httpVersion"] = "h3",
["headers"] = Headers(("Cookie", "sessionid=segreto-cookie; ds_user_id=segreto-cookie"), ("X-Prova", "segreto-intestazione")),
["queryString"] = new JsonArray(new JsonObject { ["name"] = "variables", ["value"] = "segreto-indirizzo" }),
["cookies"] = new JsonArray(new JsonObject { ["name"] = "sessionid", ["value"] = "segreto-cookie" }),
["headersSize"] = -1,
["bodySize"] = 0,
};
if (postData is not null)
{
request["postData"] = postData;
}
return new JsonObject
{
["_resourceType"] = resourceType,
["startedDateTime"] = started,
["time"] = 123.4,
["request"] = request,
["response"] = new JsonObject
{
["status"] = status,
["statusText"] = "",
["httpVersion"] = "h3",
["headers"] = Headers(("Set-Cookie", "prova=segreto-risposta"), ("Content-Type", "text/plain; segreto-intestazione")),
["cookies"] = new JsonArray(new JsonObject { ["name"] = "prova", ["value"] = "segreto-risposta" }),
["content"] = content,
["redirectURL"] = "",
},
};
}
static JsonObject Content(string mime, string? text, bool base64 = false)
{
var content = new JsonObject { ["size"] = text?.Length ?? 0, ["mimeType"] = mime };
if (text is not null)
{
content["text"] = base64 ? Base64(text) : text;
if (base64)
{
content["encoding"] = "base64";
}
}
return content;
}
static JsonObject Form(string text, params (string Name, string Value)[] parameters)
{
var form = new JsonObject { ["mimeType"] = "application/x-www-form-urlencoded", ["text"] = text };
if (parameters.Length > 0)
{
form["params"] = Headers(parameters);
}
return form;
}
var page = $$"""
<!DOCTYPE html><html><head>
<script>var nascosto = "segreto-script";</script>
<script type="application/json" data-prova="1">{{EmbeddedBlock}}</script>
<script type="application/json">{ "rotto": </script>
</head><body><p>segreto-testo-della-pagina</p><script type="application/json">{{SecondEmbeddedBlock}}</script></body></html>
""";
return new JsonObject
{
["log"] = new JsonObject
{
["version"] = "1.2",
["creator"] = new JsonObject { ["name"] = "WebInspector", ["version"] = "537.36" },
["pages"] = new JsonArray(),
["entries"] = new JsonArray(
Entry("xhr", "POST", "https://www.instagram.com/graphql/query?variables=segreto-indirizzo", 200, Content("text/javascript; charset=utf-8", FirstBody, base64: true),
Form($"av=segreto-corpo&{Operation}=ProvaElencoQuery&variables=segreto-corpo", ("av", "segreto-corpo"), (Operation, "ProvaElencoQuery"), ("variables", "segreto-corpo"))),
Entry("fetch", "GET", "https://www.instagram.com/api/v1/prova/mario.rossi/info/?segreto=1", 200, Content("application/json", SecondBody)),
Entry("xhr", "POST", "https://www.instagram.com/graphql/query", 200, Content("application/json", """{"pagina":{"cursore":"QVFIUjBz","altre":true}}"""),
Form($"{Operation}=ProvaPaginaSuccessivaQuery&x=segreto-corpo")),
Entry("xhr", "POST", "https://www.instagram.com/api/graphql", 200, Content("text/javascript", """{"dati":{"vuoto":null}}"""),
Form($"{Operation}=mario.rossi", (Operation, "mario.rossi"))),
Entry("script", "GET", "https://www.instagram.com/static/pacchetto.js", 200, Content("text/javascript", "console.log('segreto-script')")),
Entry("image", "GET", "https://scontent.cdninstagram.com/v/foto.jpg?firma=segreto-indirizzo", 200, Content("image/jpeg", "segreto-immagine", base64: true)),
Entry("document", "GET", "https://www.instagram.com/mario.rossi/?igsh=segreto-indirizzo", 200, Content("text/html; charset=utf-8", page)),
Entry("xhr", "POST", "https://www.instagram.com/ajax/prova/", 200, Content("text/javascript", """for (;;);{"payload":{"segreto":1}}""")),
Entry("document", "GET", "https://www.instagram.com/altro/", 200, Content("text/html", "<html><body>segreto-senza-dati</body></html>")),
Entry("xhr", "GET", "https://www.instagram.com/api/v1/vuota/", 200, Content("application/json", null)),
Entry("fetch", "GET", "https://esterno.example/api/?segreto=1", 200, Content("application/json", """{"segreto":1}""")),
Entry("fetch", "GET", "https://scontent.cdninstagram.com/v/dati.json", 200, Content("application/json", """{"segreto":1}"""))),
},
};
}
}