Add null check on email body to prevent cheerio exception (#2298)

This commit is contained in:
Janosch Maier 2021-03-26 16:02:08 +01:00 committed by GitHub
parent 187ba07206
commit f8b8bff5db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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;