Files
Alby96andClaude Opus 5.5 2ffb766a95
verifica / controlli (push) Successful in 21s
verifica / prove (push) Successful in 25m28s
verifica / installatore (push) Successful in 12m42s
verifica / immagine (push) Successful in 25m29s
Strada B: la pagina chiede i dati; febe-sonda richieste per i campioni nuovi
La prova dal vivo del 12/11 è fallita perché le ricette passive dipendono da
bersagli che si scoprono solo su Instagram. Alberto ha scelto la strada B
(issue #34): dalla pagina già aperta Febe chiede al sito gli stessi dati che
chiede la sua applicazione web, solo verso gli indirizzi di
richieste.<sito>.json, in lettura e con il permesso del Governatore.

- Specifica: eccezione della regola 2, riga e regole delle richieste in §6.1,
  correzione dell'idea di §1, regola 5, §6.3 e §7.4 da riscrivere con #36,
  §14.5, domanda 11 sulle storie; regola 4 di CLAUDE.md; due righe in
  decisioni.md.
- febe-sonda richieste: descrive la forma delle richieste XHR/Fetch della
  pagina (percorso, nomi e tipi di parametri, intestazioni, campi, struttura
  delle variabili GraphQL) senza valori personali. Un nome che nel HAR compare
  come valore diventa «(nome tolto)». 27 prove su un HAR sintetico; corretti
  i 17 difetti confermati da una revisione avversaria.
- OperationName accetta il suffisso «…Query_connection».
- prove-dal-vivo.md, parte A ter: il giro da fare per il HAR nuovo (#35).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 16:56:26 +01:00

265 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"));
Assert.AreEqual("ProvaGrigliaQuery_connection", HarImporter.OperationName("ProvaGrigliaQuery_connection"), "The following page of a connection.");
foreach (var value in new[] { null, "", "Query", "mario.rossi", "mario_rossi", "Pagina di mario Query", "Profilo12345Query", "/mario.rossi/Query", "ProvaElenco", "ProvaQuery_mario.rossi", "ProvaQuery_Mario" })
{
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}"""))),
},
};
}
}