Skip to content

Commit fe08f6a

Browse files
committed
fix: handle missing attachments in ARF detection
Prevent TypeError when messageInfo.attachments is undefined or null during email sync by adding fallback to empty array.
1 parent 6333fa3 commit fe08f6a

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/arf-detect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const arfDetect = async messageInfo => {
2727

2828
let returnPath;
2929

30-
for (let attachment of messageInfo.attachments) {
30+
for (let attachment of messageInfo.attachments || []) {
3131
switch (attachment.contentType.toLowerCase()) {
3232
case 'message/feedback-report': {
3333
// found a feedback report

test/complaint-test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,40 @@ function mightBeAComplaint(messageInfo) {
6161
}
6262

6363
test('ARF complaint detection tests', async t => {
64+
await t.test('handles missing attachments gracefully', async () => {
65+
// Test with undefined attachments
66+
const messageInfoUndefined = {
67+
from: { address: 'test@example.com' },
68+
subject: 'Test'
69+
};
70+
const reportUndefined = await arfDetect(messageInfoUndefined);
71+
assert.ok(reportUndefined);
72+
assert.deepStrictEqual(reportUndefined.arf, {});
73+
assert.deepStrictEqual(reportUndefined.headers, {});
74+
75+
// Test with null attachments
76+
const messageInfoNull = {
77+
from: { address: 'test@example.com' },
78+
subject: 'Test',
79+
attachments: null
80+
};
81+
const reportNull = await arfDetect(messageInfoNull);
82+
assert.ok(reportNull);
83+
assert.deepStrictEqual(reportNull.arf, {});
84+
assert.deepStrictEqual(reportNull.headers, {});
85+
86+
// Test with empty attachments array
87+
const messageInfoEmpty = {
88+
from: { address: 'test@example.com' },
89+
subject: 'Test',
90+
attachments: []
91+
};
92+
const reportEmpty = await arfDetect(messageInfoEmpty);
93+
assert.ok(reportEmpty);
94+
assert.deepStrictEqual(reportEmpty.arf, {});
95+
assert.deepStrictEqual(reportEmpty.headers, {});
96+
});
97+
6498
await t.test('Yahoo ARF abuse report', async () => {
6599
const messageInfo = await parseForArfDetect(path('yahoo.eml'));
66100
const report = await arfDetect(messageInfo);

0 commit comments

Comments
 (0)