package calls import ( "crypto/sha256" "encoding/hex" "testing" ) // Review W-4: the subject was bound into the hub session key in the // Task 4 fix round with a reason — "the key then cannot survive the // person it belongs to" — and nothing tested it. Dropping the subject // left the whole suite green. // // A sign-out-and-in test over HTTP does not gate this: re-signing-in // hands the browser a new cookie value, so the key would change with or // without the subject in the hash. What the claim is actually about is // the same cookie value under a different person, which is why this is // a test of the derivation rather than of a request. func TestTheHubSessionKeyBindsTheSubjectTheSaltAndTheCookie(t *testing.T) { const ( salt = "boot-salt" who = "person@example.com" cookie = "a-session-cookie-value" ) base := hubSessionKey(salt, who, cookie) // Two tabs of one browser: the same key, which is what one peer per // room and its takeover need (§4.2). if again := hubSessionKey(salt, who, cookie); again != base { t.Fatal("the same session did not derive the same key") } for _, c := range []struct { what string salt, who, cookie string why string }{ {"subject", salt, "someone.else@example.com", cookie, "a cookie value reused under another person would inherit the room"}, {"salt", "another-boot", who, cookie, "the key would still name a participation from before a restart"}, {"cookie", salt, who, "another-session-cookie", "two browsers of one person would collide, and §4.4 allows four streams per person"}, } { if got := hubSessionKey(c.salt, c.who, c.cookie); got == base { t.Fatalf("changing the %s did not change the key: %s", c.what, c.why) } } // And the key is not a credential: it is neither the cookie value // nor a bare hash of it, so a hub key on its own reaches nothing. if base == cookie { t.Fatal("the key is the session cookie value") } bare := sha256.Sum256([]byte(cookie)) if base == hex.EncodeToString(bare[:]) { t.Fatal("the key is an unsalted hash of the session cookie value") } if len(base) != 64 { t.Fatalf("the key is %d characters, want 64 hex", len(base)) } }