Skip to content

Commit

Permalink
fix(dkim): ensure DKIM-Signature headers are appropriately wrapped
Browse files Browse the repository at this point in the history
closes #339
  • Loading branch information
adamcooke committed Aug 1, 2021
1 parent 4880eee commit 476129c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
16 changes: 9 additions & 7 deletions lib/postal/dkim_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def initialize(domain, message)
end

def dkim_header
"DKIM-Signature: v=1;" + dkim_properties + signature
"DKIM-Signature: v=1; " + dkim_properties.join("\r\n\t") + signature.scan(/.{1,72}/).join("\r\n\t")
end

private
Expand Down Expand Up @@ -96,16 +96,18 @@ def body_hash
end

def dkim_properties
String.new.tap do |header|
header << " a=rsa-sha256; c=relaxed/relaxed;"
header << " d=#{@domain_name}; s=#{@dkim_identifier}; t=#{Time.now.utc.to_i};"
header << " bh=#{body_hash}; h=#{header_names.join(':')};"
header << " b="
Array.new.tap do |header|
header << "a=rsa-sha256; c=relaxed/relaxed;"
header << "d=#{@domain_name};"
header << "s=#{@dkim_identifier}; t=#{Time.now.utc.to_i};"
header << "bh=#{body_hash};"
header << "h=#{header_names.join(':')};"
header << "b="
end
end

def dkim_header_for_signing
"dkim-signature:v=1;" + dkim_properties
"dkim-signature:v=1; #{dkim_properties.join(' ')}"
end

def signable_header_string
Expand Down
11 changes: 6 additions & 5 deletions spec/lib/postal/dkim_header_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
allow(domain).to receive(:dkim_key).and_return(OpenSSL::PKey::RSA.new(frontmatter['private_key']))
allow(domain).to receive(:dkim_identifier).and_return(frontmatter['dkim_identifier'])

expectation = "DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; " \
"d=#{frontmatter['domain']}; s=#{frontmatter['dkim_identifier']}; t=#{mocked_time.to_i}; " \
"bh=#{frontmatter['bh']}; "\
"h=#{frontmatter['headers']}; " \
"b=#{frontmatter['b']}"
expectation = "DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r\n" \
"\td=#{frontmatter['domain']};\r\n" \
"\ts=#{frontmatter['dkim_identifier']}; t=#{mocked_time.to_i};\r\n" \
"\tbh=#{frontmatter['bh']};\r\n"\
"\th=#{frontmatter['headers']};\r\n" \
"\tb=#{frontmatter['b'].scan(/.{1,72}/).join("\r\n\t")}"

header = described_class.new(domain, email)

Expand Down

0 comments on commit 476129c

Please sign in to comment.