|
@@ -0,0 +1,120 @@
|
|
|
|
|
+#include <gtest/gtest.h>
|
|
|
|
|
+
|
|
|
|
|
+#include <string>
|
|
|
|
|
+
|
|
|
|
|
+#include "functions/header_fields.hpp"
|
|
|
|
|
+#include "functions/mime_words.hpp"
|
|
|
|
|
+
|
|
|
|
|
+using svapi::functions::headerValue;
|
|
|
|
|
+using svapi::functions::unfoldHeaders;
|
|
|
|
|
+
|
|
|
|
|
+namespace {
|
|
|
|
|
+/// The decoded value of one header, which is what a consumer actually wants.
|
|
|
|
|
+std::string decoded(const std::string& raw, const char* name) {
|
|
|
|
|
+ return svapi::functions::MimeWords::decode(headerValue(unfoldHeaders(raw), name));
|
|
|
|
|
+}
|
|
|
|
|
+} // namespace
|
|
|
|
|
+
|
|
|
|
|
+TEST(HeaderFields, TheShapeMEASUREDAgainstARealProvider) {
|
|
|
|
|
+ // Exactly what came back through the deployed runner: `Subject:` with
|
|
|
|
|
+ // NOTHING on its own line, and the value split across two continuation
|
|
|
|
|
+ // lines as two separate encoded words.
|
|
|
|
|
+ const std::string raw =
|
|
|
|
|
+ "Return-Path: <nbot@fsociety.hu>\r\n"
|
|
|
|
|
+ "Subject:\r\n"
|
|
|
|
|
+ " =?UTF-8?B?c3ZhcGkgcm91bmR0cmlwIC0gw6FydsOtenTFsXLFkQ==?=\r\n"
|
|
|
|
|
+ " =?UTF-8?B?IHTDvGvDtnJmw7pyw7Nnw6lw?=\r\n"
|
|
|
|
|
+ "To: <nbot@fsociety.hu>\r\n"
|
|
|
|
|
+ "\r\n"
|
|
|
|
|
+ "body text that must not be parsed as a header\r\n";
|
|
|
|
|
+ // BOTH encoded words, in order, decoded and concatenated.
|
|
|
|
|
+ EXPECT_EQ(decoded(raw, "Subject"), "svapi roundtrip - árvíztűrő tükörfúrógép");
|
|
|
|
|
+ // And the body did not leak into the fields.
|
|
|
|
|
+ EXPECT_EQ(headerValue(unfoldHeaders(raw), "body text that must not be parsed as a header"),
|
|
|
|
|
+ "");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(HeaderFields, TheTwoNAIVEExtractIONSAreWrongAndThisIsWhy) {
|
|
|
|
|
+ // The regexes a consumer reaches for first. Kept as a test so the reason
|
|
|
|
|
+ // this code exists cannot be forgotten: one fails loudly, the other LIES.
|
|
|
|
|
+ const std::string raw = "Subject:\r\n =?UTF-8?Q?first?=\r\n =?UTF-8?Q?_second?=\r\n\r\n";
|
|
|
|
|
+ const std::string full = decoded(raw, "Subject");
|
|
|
|
|
+ EXPECT_EQ(full, "first second");
|
|
|
|
|
+ // A `.*` after "Subject:" cannot cross the CRLF - it would see nothing.
|
|
|
|
|
+ // A per-line match would capture only "first" - a silently truncated value.
|
|
|
|
|
+ EXPECT_NE(full, "");
|
|
|
|
|
+ EXPECT_NE(full, "first");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(HeaderFields, AnUnfoldedHeaderIsLeftAlone) {
|
|
|
|
|
+ const std::string raw = "Subject: plain and short\r\nFrom: a@b.com\r\n\r\n";
|
|
|
|
|
+ EXPECT_EQ(headerValue(unfoldHeaders(raw), "Subject"), "plain and short");
|
|
|
|
|
+ EXPECT_EQ(headerValue(unfoldHeaders(raw), "From"), "a@b.com");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(HeaderFields, LookupIsCaseInsensitiveBecauseServersVary) {
|
|
|
|
|
+ const std::string raw = "SUBJECT: shouty\r\nx-Thing: v\r\n\r\n";
|
|
|
|
|
+ EXPECT_EQ(headerValue(unfoldHeaders(raw), "Subject"), "shouty");
|
|
|
|
|
+ EXPECT_EQ(headerValue(unfoldHeaders(raw), "subject"), "shouty");
|
|
|
|
|
+ EXPECT_EQ(headerValue(unfoldHeaders(raw), "X-THING"), "v");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(HeaderFields, TheFIRSTOccurrenceWinsSoAnInjectedHeaderCannotOverride) {
|
|
|
|
|
+ // Mail with two Subject lines is malformed. Taking the first is the
|
|
|
|
|
+ // conventional reading AND the safe one: a header appended further down
|
|
|
|
|
+ // must not beat the real one.
|
|
|
|
|
+ const std::string raw = "Subject: real\r\nFrom: a@b.com\r\nSubject: injected\r\n\r\n";
|
|
|
|
|
+ EXPECT_EQ(headerValue(unfoldHeaders(raw), "Subject"), "real");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(HeaderFields, TabContinuationsCountToo) {
|
|
|
|
|
+ // RFC 5322 allows a tab as the continuation prefix, and real servers use
|
|
|
|
|
+ // it - the Received line measured on prod folds with tabs.
|
|
|
|
|
+ const std::string raw = "Received: from x\r\n\tby y\r\n\tfor <z@example.com>\r\n\r\n";
|
|
|
|
|
+ EXPECT_EQ(headerValue(unfoldHeaders(raw), "Received"), "from x by y for <z@example.com>");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(HeaderFields, BareLfIsAcceptedAsWellAsCrlf) {
|
|
|
|
|
+ // A fetched header is whatever the server sent. Insisting on CRLF would
|
|
|
|
|
+ // make a lenient server's mail unreadable.
|
|
|
|
|
+ const std::string raw = "Subject:\n one\n two\n\n";
|
|
|
|
|
+ EXPECT_EQ(headerValue(unfoldHeaders(raw), "Subject"), "one two");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(HeaderFields, AMalformedLineIsSKIPPEDRatherThanFatal) {
|
|
|
|
|
+ // A header block is attacker-influenced. One junk line must not make a
|
|
|
|
|
+ // mailbox unreadable, so it is dropped and the rest still parses.
|
|
|
|
|
+ const std::string raw = "Subject: fine\r\nthis-line-has-no-colon\r\nFrom: a@b.com\r\n\r\n";
|
|
|
|
|
+ const auto fields = unfoldHeaders(raw);
|
|
|
|
|
+ EXPECT_EQ(headerValue(fields, "Subject"), "fine");
|
|
|
|
|
+ EXPECT_EQ(headerValue(fields, "From"), "a@b.com");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(HeaderFields, AContinuationWithNoFieldBeforeItIsDropped) {
|
|
|
|
|
+ // Not attributable to anything, so guessing would be worse than dropping.
|
|
|
|
|
+ EXPECT_NO_THROW({ (void)unfoldHeaders(" orphan continuation\r\nSubject: ok\r\n\r\n"); });
|
|
|
|
|
+ EXPECT_EQ(headerValue(unfoldHeaders(" orphan\r\nSubject: ok\r\n\r\n"), "Subject"), "ok");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(HeaderFields, AnAbsentHeaderIsEmptyNotAnError) {
|
|
|
|
|
+ EXPECT_EQ(headerValue(unfoldHeaders("Subject: x\r\n\r\n"), "Date"), "");
|
|
|
|
|
+ EXPECT_EQ(headerValue(unfoldHeaders(""), "Subject"), "");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(HeaderFields, TheBLANKLineEndsTheBlockSoABodyIsNeverParsed) {
|
|
|
|
|
+ // A body can contain anything, including lines that look like headers.
|
|
|
|
|
+ const std::string raw = "Subject: real\r\n\r\nSubject: this is body text\r\n";
|
|
|
|
|
+ const auto fields = unfoldHeaders(raw);
|
|
|
|
|
+ EXPECT_EQ(fields.size(), 1u);
|
|
|
|
|
+ EXPECT_EQ(headerValue(fields, "Subject"), "real");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+TEST(HeaderFields, AnAddressHeaderDecodesAndItsAddressSurvives) {
|
|
|
|
|
+ const std::string raw =
|
|
|
|
|
+ "From: =?UTF-8?Q?K=C5=91v=C3=A1g=C3=B3_=C3=96rs?= <ors.kovago@example.com>\r\n\r\n";
|
|
|
|
|
+ const std::string from = decoded(raw, "From");
|
|
|
|
|
+ EXPECT_EQ(from, "Kővágó Örs <ors.kovago@example.com>");
|
|
|
|
|
+ // And the envelope form strips the display name, which is the other half
|
|
|
|
|
+ // of what a consumer needs.
|
|
|
|
|
+ EXPECT_EQ(svapi::functions::MimeWords::envelopeAddress(from), "ors.kovago@example.com");
|
|
|
|
|
+}
|