-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: parse header to get resulting objects
- Loading branch information
1 parent
b31b042
commit 7fc3ec0
Showing
3 changed files
with
80 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,8 @@ if (!semver.satisfies(version, '>= 3.5')) | |
`Python v3.5+ is required, you currently have v${version} installed` | ||
); | ||
|
||
const KEYS = ['spf', 'dkim', 'arc', 'dmarc']; | ||
|
||
function authenticateMessage(message, ...args) { | ||
const command = `python3 ${scripts.authenticateMessage} ${args.join(' ')}`; | ||
debug(command); | ||
|
@@ -57,7 +59,53 @@ function authenticateMessage(message, ...args) { | |
child.stdin.end(); | ||
child.on('close', () => { | ||
if (stderr.length > 0) return reject(new Error(stderr.join(''))); | ||
resolve(stdout.join('')); | ||
// Authentication-Results: mx1.forwardemail.net; spf=fail reason="SPF fail - not authorized" smtp.helo=jacks-macbook-pro.local [email protected]; dkim=fail; arc=none; dmarc=fail (Used From Domain Record) header.from=forwardemail.net policy.dmarc=reject | ||
const result = { header: stdout.join('').trim() }; | ||
for (const key of KEYS) { | ||
result[key] = {}; | ||
} | ||
|
||
result.dmarc.policy = 'none'; | ||
|
||
const terms = result.header | ||
.split(/;/) | ||
.map((t) => t.trim()) | ||
.filter((t) => t !== ''); | ||
|
||
for (const term of terms) { | ||
const split = term.split('='); | ||
if (term.startsWith('spf=')) { | ||
result.spf.result = split[1].split(' ')[0]; | ||
const index = term.indexOf('reason='); | ||
if (index !== -1) | ||
result.spf.reason = term | ||
.slice(index + 'reason='.length) | ||
.split('"')[1]; | ||
} else if (term.startsWith('dkim=')) { | ||
result.dkim.result = split[1].split(' ')[0]; | ||
const index = term.indexOf('('); | ||
if (index !== -1) | ||
result.dkim.reason = term.slice(index + '('.length).split(')')[0]; | ||
} else if (term.startsWith('arc=')) { | ||
result.arc.result = split[1].split(' ')[0]; | ||
// TODO: right now this does not return a comment due to this issue: | ||
// <https://github.com/ValiMail/authentication-headers/issues/12 | ||
} else if (term.startsWith('dmarc=')) { | ||
result.dmarc.result = split[1].split(' ')[0]; | ||
// reason | ||
const index = term.indexOf('('); | ||
if (index !== -1) | ||
result.dmarc.reason = term.slice(index + '('.length).split(')')[0]; | ||
// policy | ||
const policyIndex = term.indexOf('policy.dmarc='); | ||
if (policyIndex !== -1) | ||
result.dmarc.policy = term | ||
.slice(policyIndex + 'policy.dmarc='.length) | ||
.split(' ')[0]; | ||
} | ||
} | ||
|
||
resolve(result); | ||
}); | ||
}); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters