|
| 1 | +require "./spec_helper" |
| 2 | + |
| 3 | +require "spec" |
| 4 | +require "../src/dkim" |
| 5 | + |
| 6 | +describe Dkim::VerifyMail do |
| 7 | + describe "round-trip sign then verify" do |
| 8 | + it "passes with relaxed/relaxed" do |
| 9 | + signed, key = sign_for_test(header_canonicalization: "relaxed", body_canonicalization: "relaxed") |
| 10 | + Dkim::VerifyMail.new(signed).verify(public_key: key).should eq Dkim::VerifyStatus::Pass |
| 11 | + end |
| 12 | + |
| 13 | + it "passes with simple/simple" do |
| 14 | + signed, key = sign_for_test(header_canonicalization: "simple", body_canonicalization: "simple") |
| 15 | + Dkim::VerifyMail.new(signed).verify(public_key: key).should eq Dkim::VerifyStatus::Pass |
| 16 | + end |
| 17 | + |
| 18 | + it "passes with relaxed/simple" do |
| 19 | + signed, key = sign_for_test(header_canonicalization: "relaxed", body_canonicalization: "simple") |
| 20 | + Dkim::VerifyMail.new(signed).verify(public_key: key).should eq Dkim::VerifyStatus::Pass |
| 21 | + end |
| 22 | + |
| 23 | + it "passes with simple/relaxed" do |
| 24 | + signed, key = sign_for_test(header_canonicalization: "simple", body_canonicalization: "relaxed") |
| 25 | + Dkim::VerifyMail.new(signed).verify(public_key: key).should eq Dkim::VerifyStatus::Pass |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + describe "body hash fail" do |
| 30 | + it "returns BodyHashFail when body is modified" do |
| 31 | + signed, key = sign_for_test |
| 32 | + modified = signed.sub("Are you hungry yet?", "Are you hungry now?") |
| 33 | + Dkim::VerifyMail.new(modified).verify(public_key: key).should eq Dkim::VerifyStatus::BodyHashFail |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + describe "signature fail" do |
| 38 | + it "returns Fail when a signed header is modified" do |
| 39 | + signed, key = sign_for_test |
| 40 | + modified = signed.sub("Subject: Is dinner ready?", "Subject: Is lunch ready?") |
| 41 | + result = Dkim::VerifyMail.new(modified).verify(public_key: key) |
| 42 | + # Body hash still matches (body unchanged), but signature verification fails |
| 43 | + result.should eq Dkim::VerifyStatus::Fail |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + describe "empty body" do |
| 48 | + it "passes with empty body" do |
| 49 | + empty_body_mail = "From: test@example.com\r\nTo: other@example.com\r\nSubject: empty\r\n\r\n" |
| 50 | + signed, key = sign_for_test(message: empty_body_mail) |
| 51 | + Dkim::VerifyMail.new(signed).verify(public_key: key).should eq Dkim::VerifyStatus::Pass |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + describe "l= body length tag" do |
| 56 | + it "passes when content is appended beyond l= boundary" do |
| 57 | + signed, key = sign_for_test(body_length: 10) |
| 58 | + appended = signed.rstrip + "\r\nAppended extra content\r\n" |
| 59 | + Dkim::VerifyMail.new(appended).verify(public_key: key).should eq Dkim::VerifyStatus::Pass |
| 60 | + end |
| 61 | + |
| 62 | + it "fails when body within l= is modified" do |
| 63 | + signed, key = sign_for_test(body_length: 10) |
| 64 | + # Modify early bytes of the body (within l= boundary) |
| 65 | + modified = signed.sub("Hi.", "XX.") |
| 66 | + Dkim::VerifyMail.new(modified).verify(public_key: key).should eq Dkim::VerifyStatus::BodyHashFail |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + describe "key revocation" do |
| 71 | + it "returns KeyRevoked when public key is empty" do |
| 72 | + signed, _ = sign_for_test |
| 73 | + Dkim::VerifyMail.new(signed).verify(public_key: "").should eq Dkim::VerifyStatus::KeyRevoked |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + describe "v= validation" do |
| 78 | + it "returns InvalidSig when v= is missing" do |
| 79 | + signed, key = sign_for_test |
| 80 | + # Remove v=1 from the DKIM-Signature header |
| 81 | + modified = signed.sub("v=1;", "") |
| 82 | + Dkim::VerifyMail.new(modified).verify(public_key: key).should eq Dkim::VerifyStatus::InvalidSig |
| 83 | + end |
| 84 | + |
| 85 | + it "returns InvalidSig when v= has wrong value" do |
| 86 | + signed, key = sign_for_test |
| 87 | + modified = signed.sub("v=1;", "v=2;") |
| 88 | + Dkim::VerifyMail.new(modified).verify(public_key: key).should eq Dkim::VerifyStatus::InvalidSig |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + describe "c= defaults" do |
| 93 | + it "defaults body canonicalization to simple when c= has no slash" do |
| 94 | + signed, key = sign_for_test(header_canonicalization: "relaxed", body_canonicalization: "simple") |
| 95 | + # Removing "/simple" changes a signed header, so signature fails — |
| 96 | + # but body hash still passes (Fail not BodyHashFail), proving the default works |
| 97 | + modified = signed.sub("c=relaxed/simple", "c=relaxed") |
| 98 | + Dkim::VerifyMail.new(modified).verify(public_key: key).should eq Dkim::VerifyStatus::Fail |
| 99 | + end |
| 100 | + |
| 101 | + it "defaults to simple/simple when c= tag is absent" do |
| 102 | + signed, key = sign_for_test(header_canonicalization: "simple", body_canonicalization: "simple") |
| 103 | + Dkim::VerifyMail.new(signed).verify(public_key: key).should eq Dkim::VerifyStatus::Pass |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + describe "x= expiration" do |
| 108 | + it "returns Expired when signature has expired" do |
| 109 | + signed, key = sign_for_test(expire: Time.unix(TIME + 1)) |
| 110 | + Dkim::VerifyMail.new(signed).verify(public_key: key).should eq Dkim::VerifyStatus::Expired |
| 111 | + end |
| 112 | + |
| 113 | + it "passes when signature has not expired" do |
| 114 | + signed, key = sign_for_test(expire: Time.utc + 1.hours) |
| 115 | + Dkim::VerifyMail.new(signed).verify(public_key: key).should eq Dkim::VerifyStatus::Pass |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + describe "no signature" do |
| 120 | + it "returns NoSignature when message has no DKIM-Signature" do |
| 121 | + Dkim::VerifyMail.new(MAIL).verify(public_key: PUBLIC_KEY_B64).should eq Dkim::VerifyStatus::NoSignature |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + describe "multiple signatures" do |
| 126 | + it "returns Pass if any signature passes" do |
| 127 | + signed, key = sign_for_test |
| 128 | + # Prepend a second (invalid) DKIM-Signature header |
| 129 | + bad_sig = "DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bad.com; s=bad; h=from; bh=bad; b=bad\r\n" |
| 130 | + multi = bad_sig + signed |
| 131 | + Dkim::VerifyMail.new(multi).verify(public_key: key).should eq Dkim::VerifyStatus::Pass |
| 132 | + end |
| 133 | + |
| 134 | + it "verify_all returns status for each signature" do |
| 135 | + signed, key = sign_for_test |
| 136 | + bad_sig = "DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bad.com; s=bad; h=from; bh=bad; b=bad\r\n" |
| 137 | + multi = bad_sig + signed |
| 138 | + results = Dkim::VerifyMail.new(multi).verify_all(public_key: key) |
| 139 | + results.size.should eq 2 |
| 140 | + results.should contain Dkim::VerifyStatus::Pass |
| 141 | + end |
| 142 | + end |
| 143 | +end |
0 commit comments