|
|
@@ -1777,6 +1777,29 @@ protected:
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+/// A server that announces a literal LARGER than the body it sends, and then
|
|
|
+/// supplies enough further bytes that the count is satisfied anyway.
|
|
|
+///
|
|
|
+/// This is the shape that corrupts silently. MEASURED against libcurl 8.22.0:
|
|
|
+/// curl counts the response's own framing toward the announced 50, reports
|
|
|
+/// CURLE_OK, and hands up a 50-byte literal whose last ten bytes are
|
|
|
+/// `)<CRLF>A003 OK`. Note this is NOT the truncating server above - that one
|
|
|
+/// closes the connection and curl notices. Here the transfer completes
|
|
|
+/// cleanly, which is exactly why nothing below the parser can catch it.
|
|
|
+class UnderAnnouncedLiteralImapServer : public svapi::testing::FakeImapServer {
|
|
|
+protected:
|
|
|
+ std::string respond(const std::string& line, bool& closeAfter) override {
|
|
|
+ if (upperOf(line).find("UID FETCH") != std::string::npos) {
|
|
|
+ // 40 bytes of body, announced as 50. The ten bytes curl will eat
|
|
|
+ // out of the framing are `)<CRLF>A003 OK`, and a well-formed close
|
|
|
+ // and completion line follow so the transfer still finishes.
|
|
|
+ return "* 1 FETCH (UID 7 BODY[HEADER] {50}\r\n" + std::string(40, 'B') +
|
|
|
+ ")\r\nA003 OK)\r\nA003 OK Fetch completed\r\n";
|
|
|
+ }
|
|
|
+ return svapi::testing::FakeImapServer::respond(line, closeAfter);
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
} // namespace
|
|
|
|
|
|
TEST(HostContextMail, AUidThatVanishedBetweenTheSearchAndTheFetchIsSkippedNotFabricated) {
|
|
|
@@ -1824,3 +1847,56 @@ TEST(HostContextMail, ATruncatedLiteralIsAnErrorRatherThanAShortBody) {
|
|
|
<< out.result.value.dump();
|
|
|
server.stop();
|
|
|
}
|
|
|
+
|
|
|
+TEST(HostContextMail, AnUnderAnnouncedLiteralIsRefusedRatherThanCarryingProtocolTextAsBody) {
|
|
|
+ // The defect: curl reports SUCCESS for this response, the direct parse
|
|
|
+ // returns NeedMore (so the framing guard fires, correctly), and the framed
|
|
|
+ // re-parse then returns Ok with a body carrying ten bytes of IMAP framing
|
|
|
+ // welded to its end. Without the content check that is a 200 with a
|
|
|
+ // corrupted message.
|
|
|
+ UnderAnnouncedLiteralImapServer server;
|
|
|
+ ASSERT_TRUE(server.start());
|
|
|
+
|
|
|
+ auto out = runScript(mailTrigger(),
|
|
|
+ withPort(catching("ctx.mail.read({ host: '127.0.0.1', port: %PORT%,"
|
|
|
+ " tls: false, user: 'u', password: 'p' })"),
|
|
|
+ server.port()));
|
|
|
+ ASSERT_TRUE(out.result.ok) << out.result.error;
|
|
|
+ ASSERT_EQ(out.result.value.at("threw"), true) << out.result.value.dump();
|
|
|
+ // Not retryable and not a timeout: a server that miscounts its own literal
|
|
|
+ // will miscount it again.
|
|
|
+ EXPECT_EQ(out.result.value.at("code"), "mail_protocol_error") << out.result.value.dump();
|
|
|
+ // The actual corruption, asserted directly: no framing may appear anywhere
|
|
|
+ // in what reached the script.
|
|
|
+ const std::string dumped = out.result.value.dump();
|
|
|
+ EXPECT_EQ(dumped.find("A003 OK"), std::string::npos) << dumped;
|
|
|
+ EXPECT_EQ(dumped.find("BBBB"), std::string::npos) << dumped;
|
|
|
+ server.stop();
|
|
|
+}
|
|
|
+
|
|
|
+TEST(HostContextMail, AnHonestBodyEndingInAParenAndCrlfIsStillDelivered) {
|
|
|
+ // The falsifying half. The guard keys on the framing SHAPE, so a body that
|
|
|
+ // legitimately ends with `)` and a CRLF - which real mail does, constantly -
|
|
|
+ // must still come through byte-intact. A guard that refused this would pass
|
|
|
+ // the test above while breaking ordinary mailboxes.
|
|
|
+ svapi::testing::FakeImapServer server;
|
|
|
+ // A body that both MENTIONS a tagged completion line and ends with `)`
|
|
|
+ // CRLF - the two features the guard keys on, arranged as real mail
|
|
|
+ // arranges them. Our own documentation looks like this.
|
|
|
+ server.messageBody =
|
|
|
+ "From: a@example.com\r\nSubject: imap notes\r\n\r\n"
|
|
|
+ "the server answered A003 OK Fetch completed (as it should)\r\n";
|
|
|
+ ASSERT_TRUE(server.start());
|
|
|
+
|
|
|
+ auto out = runScript(mailTrigger(),
|
|
|
+ withPort(catching("ctx.mail.read({ host: '127.0.0.1', port: %PORT%,"
|
|
|
+ " tls: false, user: 'u', password: 'p' })"),
|
|
|
+ server.port()));
|
|
|
+ ASSERT_TRUE(out.result.ok) << out.result.error;
|
|
|
+ ASSERT_EQ(out.result.value.at("threw"), false) << out.result.value.dump();
|
|
|
+ const nlohmann::json& value = out.result.value.at("value");
|
|
|
+ ASSERT_EQ(value.at("messages").size(), 1u) << value.dump();
|
|
|
+ // Intact, not merely present: the guard must not have trimmed the tail.
|
|
|
+ EXPECT_NE(value.dump().find("as it should"), std::string::npos) << value.dump();
|
|
|
+ server.stop();
|
|
|
+}
|