瀏覽代碼

test(mail): pin the three shapes that produce a wrong ANSWER, not a crash

All three shipped as silent wrong answers in a sibling implementation of
this protocol, and each is a case where a parser reports success while
handing back something that is not the email - which is worse than
failing, because the caller cannot tell.

  - An absent uid: fetching nothing is not an error in IMAP, so the only
    thing that comes back is the tagged completion. A parser that reports
    success and then reads the buffer anyway returns the protocol line AS
    THE MESSAGE BODY, which parses into a mail with no subject and no
    sender. Our reader manufactures no record, so the caller above is
    what must turn zero messages into not-found.
  - A literal cut short: reporting success with the bytes that arrived
    makes an interrupted transfer indistinguishable from a brief message.
    NeedMore means incomplete, never "succeed with what arrived".
  - A body whose CONTENT contains both ")\r\n" and a line that looks like
    a tagged completion. This one is why the announced byte count is
    trusted and no delimiter is ever searched for: real mail contains
    such text, and a terminator-searching parser truncates exactly those
    messages. Measured byte-intact.

Also links runner_net into runner_host, which the ctx.mail binding needs.
fszontagh 1 月之前
父節點
當前提交
b73620afc8
共有 2 個文件被更改,包括 50 次插入 和 1 次删除
  1. 3 1
      runner/CMakeLists.txt
  2. 47 0
      runner/tests/imap_fetch_test.cpp

+ 3 - 1
runner/CMakeLists.txt

@@ -350,7 +350,9 @@ ${SVAPI_HOST_PRELUDE_TEXT})JS\";
     add_library(runner_host STATIC src/host_context.cpp)
     target_include_directories(runner_host PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/generated)
     # qjs again, PRIVATE on runner_core: host_context.cpp includes quickjs.h.
-    target_link_libraries(runner_host PUBLIC runner_core vectorapi_core PRIVATE qjs)
+    # runner_net carries the mail transport that ctx.mail binds to. PUBLIC so
+    # the host-context tests, which link runner_host, can name its types.
+    target_link_libraries(runner_host PUBLIC runner_core vectorapi_core runner_net PRIVATE qjs)
     target_compile_features(runner_host PUBLIC cxx_std_20)
     set_target_properties(runner_host PROPERTIES POSITION_INDEPENDENT_CODE ON)
 

+ 47 - 0
runner/tests/imap_fetch_test.cpp

@@ -824,3 +824,50 @@ TEST(ImapFetchJson, ProjectsThePartNumbersAPeekWouldNeed) {
     EXPECT_EQ(j.at("parts")[2].at("number"), "3");
     EXPECT_EQ(j.at("parts")[2].at("parts")[0].at("parts")[1].at("number"), "3.2");
 }
+
+// ---------------------------------------------------------------------------
+// The three shapes that produced wrong ANSWERS (not crashes) in a sibling
+// implementation of this protocol. Each is a case where a parser reports
+// success while handing back something that is not the email, which is far
+// worse than failing: the caller cannot tell. Pinned here because our reader
+// is octet-counted and therefore immune to all three, and a future
+// "optimisation" that searched for a terminator would reintroduce every one.
+// ---------------------------------------------------------------------------
+
+TEST(ImapFetchWrongAnswerShapes, AnAbsentUidIsZeroMessagesRatherThanTheProtocolLineAsAnEmail) {
+    // Fetching nothing is not an error in IMAP: the server answers the tagged
+    // completion and sends no FETCH line. A parser that reports success and
+    // then reads the buffer anyway returns "A003 OK Fetch completed" AS THE
+    // MESSAGE BODY, which then parses into a mail with no subject and no
+    // sender - a successful fetch of a message that does not exist. The caller
+    // above must turn this into not-found; what matters here is that no record
+    // is manufactured.
+    const ImapFetchResult r = parseFetchResponse("A003 OK Fetch completed\r\n");
+    EXPECT_EQ(r.status, ImapParse::Ok);
+    EXPECT_TRUE(r.messages.empty());
+}
+
+TEST(ImapFetchWrongAnswerShapes, ALiteralCutShortIsNeedMoreAndYieldsNoPartialBody) {
+    // The announced count is 50 and 10 bytes arrived. Reporting success with
+    // the 10 bytes gives a short but perfectly valid-looking email, so an
+    // interrupted transfer is indistinguishable from a brief message.
+    const ImapFetchResult r = parseFetchResponse("* 1 FETCH (UID 7 BODY[] {50}\r\n0123456789");
+    EXPECT_EQ(r.status, ImapParse::NeedMore);
+    EXPECT_TRUE(r.messages.empty());
+}
+
+TEST(ImapFetchWrongAnswerShapes, ABodyContainingATaggedReplyAndTheTerminatorSurvivesByteIntact) {
+    // This is why the byte count is trusted and no delimiter is searched for.
+    // The body deliberately contains both ")\r\n" and a line that looks like a
+    // tagged completion; real mail contains such text, and a
+    // terminator-searching parser truncates exactly these messages.
+    const std::string body = "A003 OK Fetch completed\r\n)\r\nreal body\r\n";
+    const std::string response = "* 1 FETCH (UID 7 BODY[] {" + std::to_string(body.size()) +
+                                 "}\r\n" + body + ")\r\nA003 OK Fetch completed\r\n";
+    const ImapFetchResult r = parseFetchResponse(response);
+    ASSERT_EQ(r.status, ImapParse::Ok);
+    ASSERT_EQ(r.messages.size(), 1u);
+    const auto it = r.messages[0].bodyParts.find("");
+    ASSERT_NE(it, r.messages[0].bodyParts.end());
+    EXPECT_EQ(it->second, body);
+}