| new file mode 100644 |
| index 000000000..f377d949f |
| --- /dev/null |
| +++ b/internal/edge/assetfill_test.go |
| @@ -0,0 +1,212 @@ |
| +package edge |
| + |
| +import ( |
| + "strings" |
| + "testing" |
| + "time" |
| +) |
| + |
| +// Not acceptance: these pin the signature primitives only. The wired fill |
| +// is exercised by the plan's acceptance tests. |
| + |
| +var ( |
| + fillKeyCurrent = []byte("0123456789abcdef0123456789abcdef") |
| + fillKeyNext = []byte("fedcba9876543210fedcba9876543210") |
| + fillNow = time.Unix(1_800_000_000, 0) |
| +) |
| + |
| +func testFillClaims() assetFillClaims { |
| + return assetFillClaims{ |
| + Host: "shop.example.com", |
| + PathQuery: "/assets/app.js?v=2", |
| + Account: "acct-1", |
| + App: "shop", |
| + Gen: "g7", |
| + } |
| +} |
| + |
| +func TestAssetFillRoundTrips(t *testing.T) { |
| + c := testFillClaims() |
| + h := signAssetFill(fillKeyCurrent, c, fillNow) |
| + if h == "" { |
| + t.Fatal("signAssetFill returned empty") |
| + } |
| + ts, sig, ok := strings.Cut(h, ":") |
| + if !ok || ts == "" || sig == "" { |
| + t.Fatalf("header %q is not <unix>:<hex>", h) |
| + } |
| + matched, ok := verifyAssetFill(fillKeyCurrent, nil, c, h, fillNow) |
| + if !ok { |
| + t.Fatal("a fresh assertion did not verify") |
| + } |
| + if string(matched) != string(fillKeyCurrent) { |
| + t.Fatalf("matched key = %q, want the current key", matched) |
| + } |
| + // The host is canonicalized as a hop's is: case, port and a root dot do |
| + // not change what is signed. |
| + d := c |
| + d.Host = "Shop.Example.com.:443" |
| + if _, ok := verifyAssetFill(fillKeyCurrent, nil, d, h, fillNow); !ok { |
| + t.Fatal("a differently spelled host did not canonicalize to the same assertion") |
| + } |
| + // Edges of the skew window still verify. |
| + for _, off := range []time.Duration{-hopSkew, hopSkew} { |
| + if _, ok := verifyAssetFill(fillKeyCurrent, nil, c, h, fillNow.Add(off)); !ok { |
| + t.Fatalf("offset %v inside the skew window was refused", off) |
| + } |
| + } |
| +} |
| + |
| +func TestAssetFillRefusesEachChangedClaim(t *testing.T) { |
| + base := testFillClaims() |
| + h := signAssetFill(fillKeyCurrent, base, fillNow) |
| + cases := []struct { |
| + name string |
| + mut func(c *assetFillClaims) |
| + now time.Time |
| + }{ |
| + {"host", func(c *assetFillClaims) { c.Host = "other.example.com" }, fillNow}, |
| + {"path-query", func(c *assetFillClaims) { c.PathQuery = "/assets/app.js?v=3" }, fillNow}, |
| + {"path", func(c *assetFillClaims) { c.PathQuery = "/assets/other.js?v=2" }, fillNow}, |
| + {"account", func(c *assetFillClaims) { c.Account = "acct-2" }, fillNow}, |
| + {"app", func(c *assetFillClaims) { c.App = "blog" }, fillNow}, |
| + {"gen", func(c *assetFillClaims) { c.Gen = "g8" }, fillNow}, |
| + {"empty gen", func(c *assetFillClaims) { c.Gen = "" }, fillNow}, |
| + {"field shift", func(c *assetFillClaims) { c.Account, c.App = "acct-1\nshop", "" }, fillNow}, |
| + {"timestamp past skew", func(c *assetFillClaims) {}, fillNow.Add(hopSkew + time.Second)}, |
| + {"timestamp before skew", func(c *assetFillClaims) {}, fillNow.Add(-hopSkew - time.Second)}, |
| + } |
| + for _, tc := range cases { |
| + t.Run(tc.name, func(t *testing.T) { |
| + c := base |
| + tc.mut(&c) |
| + if _, ok := verifyAssetFill(fillKeyCurrent, fillKeyNext, c, h, tc.now); ok { |
| + t.Fatal("verified, want refused") |
| + } |
| + }) |
| + } |
| + for _, bad := range []string{"", "nocolon", ":", "1800000000:", ":abcd", "x:abcd", "1800000000:zz", "1800000000:" + strings.Repeat("0", 64)} { |
| + if _, ok := verifyAssetFill(fillKeyCurrent, fillKeyNext, base, bad, fillNow); ok { |
| + t.Fatalf("malformed header %q verified", bad) |
| + } |
| + } |
| + // A different key never verifies, and an empty key is never a key. |
| + if _, ok := verifyAssetFill([]byte("wrong-wrong-wrong-wrong-wrong-xx"), nil, base, h, fillNow); ok { |
| + t.Fatal("a different key verified") |
| + } |
| + if _, ok := verifyAssetFill(nil, nil, base, h, fillNow); ok { |
| + t.Fatal("no keys verified") |
| + } |
| + // Claims that would break the newline framing are not signed at all. |
| + nl := base |
| + nl.App = "shop\nx" |
| + if got := signAssetFill(fillKeyCurrent, nl, fillNow); got != "" { |
| + t.Fatalf("signed claims carrying a newline: %q", got) |
| + } |
| +} |
| + |
| +func TestHopSignatureIsNeverAnAssetFill(t *testing.T) { |
| + c := testFillClaims() |
| + hop := signHop(fillKeyCurrent, c.Host, fillNow) |
| + if _, ok := verifyAssetFill(fillKeyCurrent, nil, c, hop, fillNow); ok { |
| + t.Fatal("a hop signature verified as an asset fill") |
| + } |
| + if _, ok := verifyAssetFill(fillKeyCurrent, fillKeyNext, c, hop, fillNow); ok { |
| + t.Fatal("a hop signature verified as an asset fill under two keys") |
| + } |
| + door := signHopDoor(fillKeyCurrent, c.Host, fillNow) |
| + if _, ok := verifyAssetFill(fillKeyCurrent, nil, c, door, fillNow); ok { |
| + t.Fatal("a door mark verified as an asset fill") |
| + } |
| + // And the converse: an assertion is not a hop. |
| + fill := signAssetFill(fillKeyCurrent, c, fillNow) |
| + if verifyHop(fillKeyCurrent, c.Host, fill, fillNow) { |
| + t.Fatal("an asset fill verified as a hop") |
| + } |
| +} |
| + |
| +func TestAssetFillAcceptsTheNextKey(t *testing.T) { |
| + c := testFillClaims() |
| + // Signed with .next (what signingKey picks the moment it exists). |
| + hn := signAssetFill(signingKey(fillKeyCurrent, fillKeyNext), c, fillNow) |
| + matched, ok := verifyAssetFill(fillKeyCurrent, fillKeyNext, c, hn, fillNow) |
| + if !ok || string(matched) != string(fillKeyNext) { |
| + t.Fatalf("next-signed assertion: ok=%v matched=%q, want next", ok, matched) |
| + } |
| + // Signed with current by a straggler while both are mirrored. |
| + hc := signAssetFill(fillKeyCurrent, c, fillNow) |
| + matched, ok = verifyAssetFill(fillKeyCurrent, fillKeyNext, c, hc, fillNow) |
| + if !ok || string(matched) != string(fillKeyCurrent) { |
| + t.Fatalf("current-signed assertion: ok=%v matched=%q, want current", ok, matched) |
| + } |
| + // With no .next mirrored, a next-signed assertion is refused. |
| + if _, ok := verifyAssetFill(fillKeyCurrent, nil, c, hn, fillNow); ok { |
| + t.Fatal("next-signed assertion verified with no next key") |
| + } |
| +} |
| + |
| +func TestAssetAckBindsAssertionAndExpiry(t *testing.T) { |
| + c := testFillClaims() |
| + a := signAssetFill(fillKeyCurrent, c, fillNow) |
| + const exp = int64(1_800_003_600) |
| + ack := signAssetAck(fillKeyCurrent, a, exp) |
| + if !verifyAssetAck(fillKeyCurrent, a, exp, ack) { |
| + t.Fatal("a fresh acknowledgement did not verify") |
| + } |
| + if verifyAssetAck(fillKeyCurrent, a, exp+1, ack) { |
| + t.Fatal("a changed expiry verified") |
| + } |
| + if verifyAssetAck(fillKeyCurrent, a, 0, ack) { |
| + t.Fatal("a zero expiry verified against a non-zero acknowledgement") |
| + } |
| + other := signAssetFill(fillKeyCurrent, c, fillNow.Add(time.Second)) |
| + if verifyAssetAck(fillKeyCurrent, other, exp, ack) { |
| + t.Fatal("a different assertion verified") |
| + } |
| + // A not-storable acknowledgement (expiry 0) is its own signed value. |
| + zero := signAssetAck(fillKeyCurrent, a, 0) |
| + if !verifyAssetAck(fillKeyCurrent, a, 0, zero) { |
| + t.Fatal("an expiry-0 acknowledgement did not verify") |
| + } |
| + if verifyAssetAck(fillKeyCurrent, a, exp, zero) { |
| + t.Fatal("an expiry-0 acknowledgement verified for a non-zero expiry") |
| + } |
| + if verifyAssetAck(fillKeyNext, a, exp, ack) { |
| + t.Fatal("a different key verified") |
| + } |
| + for _, bad := range []string{"", "zz", ack[:len(ack)-2], ack + "00"} { |
| + if verifyAssetAck(fillKeyCurrent, a, exp, bad) { |
| + t.Fatalf("malformed acknowledgement %q verified", bad) |
| + } |
| + } |
| + if verifyAssetAck(nil, a, exp, signAssetAck(nil, a, exp)) { |
| + t.Fatal("an empty key verified") |
| + } |
| + // Neither a fill assertion's MAC nor a hop's is an acknowledgement. |
| + _, hexSig, _ := strings.Cut(a, ":") |
| + if verifyAssetAck(fillKeyCurrent, a, exp, hexSig) { |
| + t.Fatal("the assertion's own MAC verified as its acknowledgement") |
| + } |
| +} |
| + |
| +func TestAckUsesTheKeyTheAssertionMatched(t *testing.T) { |
| + c := testFillClaims() |
| + // The far edge has not yet seen .next and signs with current only. |
| + assertion := signAssetFill(fillKeyCurrent, c, fillNow) |
| + // The home holds both. signingKey would pick next; the ack must use the |
| + // key the assertion matched instead. |
| + matched, ok := verifyAssetFill(fillKeyCurrent, fillKeyNext, c, assertion, fillNow) |
| + if !ok { |
| + t.Fatal("assertion refused") |
| + } |
| + const exp = int64(1_800_003_600) |
| + ack := signAssetAck(matched, assertion, exp) |
| + if !verifyAssetAck(fillKeyCurrent, assertion, exp, ack) { |
| + t.Fatal("the far edge could not verify an acknowledgement made with the matched key") |
| + } |
| + // The trap this guards: acknowledging with signingKey would not verify. |
| + wrong := signAssetAck(signingKey(fillKeyCurrent, fillKeyNext), assertion, exp) |
| + if verifyAssetAck(fillKeyCurrent, assertion, exp, wrong) { |
| + t.Fatal("an acknowledgement under the other key verified at the far edge") |
| + } |
| +} |