Files
Febe/tests/Febe.Tests.Windows/DpapiSessionProtectorTests.cs
Alby96andClaude Opus 5.5 c87768766c
verifica / controlli (push) Successful in 22s
verifica / prove (push) Successful in 16m46s
verifica / installatore (push) Successful in 4m43s
verifica / immagine (push) Successful in 21m2s
Sessione salvata cifrata, copiata a «Ho finito» (#36, S4)
Il client del sito ha bisogno della sessione del browser di Febe: ora la copia
c'è, cifrata e dietro un interruttore, senza cambiare nulla di predefinito.

- Opzione «raccolta» (pagine predefinita, richieste): solo con «richieste» e
  almeno un'operazione configurata si costruiscono sessione salvata, client del
  sito e copia. Con Instagram nulla cambia: le dieci operazioni restano non
  configurate (mancano __spin_*, __req, __s, __dyn, __csr e altri).
- Cookie con dominio, percorso, solo-host, Secure, HttpOnly e scadenza, mandati
  solo dove il browser li manderebbe (RFC 6265).
- Sezione «page» del catalogo: dove la pagina tiene ogni valore; nuovi campi rev,
  hs, hsi, dpr, ccg, comet_req, entry_nonce. Il caricatore rifiuta
  un'operazione configurata con un valore di sessione senza fonte nella pagina.
- Dati/sessione/sessione.json: DPAPI su Windows, AES-256-GCM in Docker (chiave
  da FEBE_CHIAVE_SESSIONE o chiave.bin), scrittura atomica, file illeggibile =
  nessuna sessione. «Scollega» la cancella; chiave.bin resta.
- Copia a «Ho finito» con Network.getCookies e una sola Runtime.evaluate in sola
  lettura, solo da una pagina del sito e finché dura il permesso di guida.
- FEBE_PASSWORD e FEBE_CHIAVE_SESSIONE non passano a Chromium, Xvfb, ffmpeg.
- Sito di prova: csrftoken, cookie limitato a /api/, registro di moduli suo.
- Revisione su tre lenti: 11 punti confermati, tutti corretti.

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

66 lines
2.8 KiB
C#

using System.Text;
using Febe.Core.Security;
using Febe.Desktop.Security;
namespace Febe.Tests.Windows;
/// <summary>
/// The saved session on Windows (issue #36, S4, spec §13.2): DPAPI for the current user, from the desktop framework of
/// .NET with no package. No WebView2 needed. A blob that does not open is <see cref="SessionUnreadableException"/>,
/// whose text holds nothing of the session.
/// </summary>
[TestClass]
public sealed class DpapiSessionProtectorTests
{
private const string Secret = "valore-di-prova-della-sessione";
private static readonly byte[] Plain = Encoding.UTF8.GetBytes("{\"cookies\":[{\"name\":\"sessione_prova\",\"value\":\"" + Secret + "\"}]}");
[TestMethod]
public void ARoundTripForTheCurrentUserGivesTheSameBytes()
{
var protector = new DpapiSessionProtector();
Assert.AreEqual("dpapi", protector.Scheme);
var blob = protector.Protect(Plain);
Assert.IsTrue(SessionBlob.HasHeader(blob, SessionBlob.Dpapi), "the shared header with the DPAPI scheme");
CollectionAssert.AreEqual(Plain, protector.Unprotect(blob));
CollectionAssert.AreEqual(Plain, new DpapiSessionProtector().Unprotect(blob), "another instance of the same user");
CollectionAssert.AreNotEqual(blob, protector.Protect(Plain), "DPAPI salts every blob");
}
[TestMethod]
public void TheBlobHoldsNoValueInClear()
{
var blob = new DpapiSessionProtector().Protect(Plain);
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, blob.AsSpan().IndexOf(form), "a form of the value is in the blob");
}
}
[TestMethod]
public void ATamperedBlobOrAnotherSchemeIsUnreadableAndTheMessageHasNoValue()
{
var protector = new DpapiSessionProtector();
var blob = protector.Protect(Plain);
var changedByte = blob.ToArray();
changedByte[^5] ^= 0x40;
var changedHeader = blob.ToArray();
changedHeader[4] = 9;
var aesHeader = blob.ToArray();
aesHeader[5] = SessionBlob.AesGcm;
var headerOnly = SessionBlob.Header(SessionBlob.Dpapi);
foreach (var wrong in new[] { changedByte, changedHeader, aesHeader, headerOnly, [], Encoding.UTF8.GetBytes(Secret) })
{
var error = Assert.ThrowsExactly<SessionUnreadableException>(() => protector.Unprotect(wrong));
Assert.AreEqual(SessionUnreadableException.Text, error.Message);
Assert.IsNull(error.InnerException, "nothing of DPAPI's own text is passed on");
Assert.DoesNotContain(Secret, error.ToString());
}
Assert.DoesNotContain(Secret, protector.ToString());
}
}