Skip to content

Self-hosted AI workspace.

Python

fix: reply-all Cc builder crashes on a non-string To or Cc field (#1700)

Afonso Coutinhoc1b674c709d9
diff --git a/static/js/emailLibrary/replyRecipients.js b/static/js/emailLibrary/replyRecipients.js
index 6a5bf220e..9235c355b 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 000000000..7eaa68e89
--- /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())
+
+
[email protected](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": "[email protected], [email protected]"}, "[email protected]")
+
+
[email protected](not _HAS_NODE, reason="node binary not on PATH")
+def test_build_reply_all_cc_still_excludes_self():
+ out = _cc({"to": "[email protected], [email protected]", "cc": ""}, "[email protected]")
+ assert out == "[email protected]"