From 667b739af4eb2fdc259f0cbaa562862da7070d57 Mon Sep 17 00:00:00 2001 From: Afonso Coutinho Date: Wed, 3 Jun 2026 05:37:22 +0100 Subject: [PATCH] fix: reply-all Cc builder crashes on a non-string To or Cc field (#1700) --- static/js/emailLibrary/replyRecipients.js | 2 +- tests/test_reply_all_cc_nonstring_js.py | 40 +++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tests/test_reply_all_cc_nonstring_js.py diff --git a/static/js/emailLibrary/replyRecipients.js b/static/js/emailLibrary/replyRecipients.js index 6a5bf22..9235c35 100644 --- a/static/js/emailLibrary/replyRecipients.js +++ b/static/js/emailLibrary/replyRecipients.js @@ -20,7 +20,7 @@ export function extractEmail(addr) { export function buildReplyAllCc(data, mine) { const list = Array.isArray(mine) ? mine : [mine]; const me = new Set(list.map((a) => (a || '').toLowerCase()).filter(Boolean)); - const split = (s) => (s || '').split(',').map((x) => x.trim()).filter(Boolean); + const split = (s) => (typeof s === 'string' ? s : '').split(',').map((x) => x.trim()).filter(Boolean); return [...split(data && data.to), ...split(data && data.cc)] .filter((addr) => !me.has(extractEmail(addr))) .join(', '); diff --git a/tests/test_reply_all_cc_nonstring_js.py b/tests/test_reply_all_cc_nonstring_js.py new file mode 100644 index 0000000..7eaa68e --- /dev/null +++ b/tests/test_reply_all_cc_nonstring_js.py @@ -0,0 +1,40 @@ +"""Pin buildReplyAllCc (static/js/emailLibrary/replyRecipients.js) against a +non-string To/Cc. Driven through `node --input-type=module`; skips without node. +""" +import json +import shutil +import subprocess +from pathlib import Path + +import pytest + +_REPO = Path(__file__).resolve().parent.parent +_HELPER = _REPO / "static" / "js" / "emailLibrary" / "replyRecipients.js" +_HAS_NODE = shutil.which("node") is not None + + +def _cc(data, mine): + js = f""" + import {{ buildReplyAllCc }} from '{_HELPER.as_posix()}'; + console.log(JSON.stringify(buildReplyAllCc({json.dumps(data)}, {json.dumps(mine)}))); + """ + proc = subprocess.run( + ["node", "--input-type=module"], + input=js, capture_output=True, text=True, cwd=str(_REPO), timeout=30, + ) + assert proc.returncode == 0, proc.stderr + return json.loads(proc.stdout.strip()) + + +@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH") +def test_build_reply_all_cc_tolerates_non_string_fields(): + # data.to / data.cc come from a JSON message blob and are not always + # strings; the old (s || "").split crashed on a non-string To. + out = _cc({"to": 123, "cc": "a@x.com, b@x.com"}, "me@x.com") + assert out == "a@x.com, b@x.com" + + +@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH") +def test_build_reply_all_cc_still_excludes_self(): + out = _cc({"to": "me@x.com, a@x.com", "cc": ""}, "me@x.com") + assert out == "a@x.com"