From f8b8bff5dbbd4b01c6d97e44ac62ec60583cd5c0 Mon Sep 17 00:00:00 2001 From: Janosch Maier Date: Fri, 26 Mar 2021 16:02:08 +0100 Subject: [PATCH] Add null check on email body to prevent cheerio exception (#2298) --- .../list-unsubscribe/lib/main.tsx | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/app/internal_packages/list-unsubscribe/lib/main.tsx b/app/internal_packages/list-unsubscribe/lib/main.tsx index d1790da5f..949d01b89 100644 --- a/app/internal_packages/list-unsubscribe/lib/main.tsx +++ b/app/internal_packages/list-unsubscribe/lib/main.tsx @@ -66,24 +66,30 @@ interface UnsubscribeAction { } function bestUnsubscribeLink(message): string { - const dom = cheerio.load(message.body); - const links = _getLinks(dom); let result = null; - for (const link of links) { - for (const re of regexps) { - if (re.test(link.href)) { - // If the URL contains e.g. "unsubscribe" we assume that we have correctly - // detected the unsubscribe link. - return link.href; - } - if (re.test(link.innerText)) { - // If the URL does not contain "unsubscribe", but the text around the link contains - // it, it is a possible candidate, we we still check all other links for a better match - result = link.href; + // Only check the body if it has been downloaded already + if (message.body) { + + const dom = cheerio.load(message.body); + const links = _getLinks(dom); + + for (const link of links) { + for (const re of regexps) { + if (re.test(link.href)) { + // If the URL contains e.g. "unsubscribe" we assume that we have correctly + // detected the unsubscribe link. + return link.href; + } + if (re.test(link.innerText)) { + // If the URL does not contain "unsubscribe", but the text around the link contains + // it, it is a possible candidate, we we still check all other links for a better match + result = link.href; + } } } + } return result;