Skip to content

Commit

Permalink
Make parsing case-insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Hann authored and Brian Hann committed Feb 6, 2017
1 parent e4cb2ea commit 8b0415e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ let tags - dmarc('v=DMARC1; p=reject; rua=mailto:mailauth-reports@google.com');
// }
```

# Debugging

npm install -g inspect-process
inspect node_modules/ava/profile.js some/test/file.js

# Watching Tests

ava --watch

or

npm test -- --watch

# License

MIT © [SoftVu](https://softvu.com)
18 changes: 10 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ const validators = {
// termPattern: /^fo$/,
description: 'Forensic reporting options. Possible values: "0" to generate reports if all underlying authentication mechanisms fail to produce a DMARC pass result, "1" to generate reports if any mechanisms fail, "d" to generate report if DKIM signature failed to verify, "s" if SPF failed.',
validate(term, value) {
if (!/^([01ds])$/.test(value)) {
if (!/^([01ds])$/i.test(value)) {
throw new Error(`Invalid value for '${term}': '${value}', must be one of: 0, 1, d, s`);
}
}
},
p: {
description: 'Policy to apply to email that fails the DMARC check. Can be "none", "quarantine", or "reject". "none" is used to collect feedback and gain visibility into email streams without impacting existing flows.',
validate(term, value) {
if (!/^(none|quarantine|reject)$/.test(value)) {
if (!/^(none|quarantine|reject)$/i.test(value)) {
throw new Error(`Invalid value for '${term}': '${value}', must be one of: none, quarantine, reject`);
}
}
Expand All @@ -48,7 +48,7 @@ const validators = {
let values = value.split(/,|:/).map(x => x.trim());

for (let val of values) {
if (!/^(afrf|iodef)$/.test(val)) {
if (!/^(afrf|iodef)$/i.test(val)) {
throw new Error(`Invalid value for '${term}': '${value}', must be one or more of these values: afrf, iodef. Multiple values must be separated by a comma or colon`);
}
}
Expand All @@ -68,7 +68,7 @@ const validators = {
let values = value.split(/,/).map(x => x.trim());

for (let val of values) {
let matches = val.match(/^mailto:(.+)$/);
let matches = val.match(/^mailto:(.+)$/i);
if (!matches) {
throw new Error(`Invalid value for '${term}': ${value}, must be a list of DMARC URIs such as 'mailto:some.email@somedomain.com'`);
}
Expand All @@ -85,7 +85,7 @@ const validators = {
let values = value.split(/,/).map(x => x.trim());

for (let val of values) {
let matches = val.match(/^mailto:(.+)$/);
let matches = val.match(/^mailto:(.+)$/i);
if (!matches) {
throw new Error(`Invalid value for '${term}': ${value}, must be a list of DMARC URIs such as 'mailto:some.email@somedomain.com'`);
}
Expand All @@ -99,7 +99,7 @@ const validators = {
sp: {
description: 'Requested Mail Receiver policy for all subdomains. Can be "none", "quarantine", or "reject".',
validate(term, value) {
if (!/^(none|quarantine|reject)$/.test(value)) {
if (!/^(none|quarantine|reject)$/i.test(value)) {
throw new Error(`Invalid value for '${term}': '${value}', must be one of: none, quarantine, reject`);
}
}
Expand All @@ -126,7 +126,7 @@ function parse(policy) {
};

// Make sure `v` is the first tag
if (rules[0][0] !== 'v') {
if (!/^v$/i.test(rules[0][0])) {
retval.messages.push(`First tag in a DMARC policy must be 'v', but found: '${rules[0][0]}'`);
return retval;
}
Expand All @@ -141,7 +141,9 @@ function parse(policy) {
let settings = validators[validatorTerm];

// Term matches validaor
if (term === validatorTerm) {
debugger;
let termRegex = new RegExp(`^${validatorTerm}$`, 'i');
if (termRegex.test(term)) {
found = true;

let tag = {
Expand Down
10 changes: 10 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ test('Must have valid DMARC version', t => {
t.true(ret.messages.some(x => /invalid DMARC version/i.test(x)));
});

test('Lower-case DMARC fails', t => {
let ret = d('v=dmarc1');
t.true(ret.messages.some(x => /invalid DMARC version/i.test(x)));
});

test('Invalid term fails', t => {
let ret = d('v=DMARC1; foo=bar');
// console.log(ret);
Expand All @@ -28,3 +33,8 @@ test('Ignore empty tags and whitespace', t => {
let ret = d('v=DMARC1; p=reject; pct=100; rua=mailto:dmarc_y_rua@yahoo.com; ');
t.falsy(ret.messages);
});

test('Parse without being case-sensitive', t => {
let ret = d('V=DMARC1; P=REJECT; FO=S; PCT=100; RF=AFRF; RI=30; RUF=MAILTO:foo@bar.com; RUA=MAILTO:dmarc_y_rua@yahoo.com; SP=NONE');
t.falsy(ret.messages);
});

0 comments on commit 8b0415e

Please sign in to comment.