Skip to content

Commit

Permalink
Store matched mechanisms in result
Browse files Browse the repository at this point in the history
  • Loading branch information
jm42 committed Dec 25, 2018
1 parent 6cc820c commit 512080b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -5,6 +5,7 @@ All notable changes grouped by version.
## [0.2.0] 2018-12-25

- `BC-BREAK` Now DNS queries will be performed at evalution time. Previous checks that returned `TempFail` might now return another value on an early match. Old method can be re-activated with `{ prefetch: true }` options.
- Now `SPFResult` contains the last `mechanism` matched (useful for Received-SPF header field "mechanism") and a list of all `matched` mechanisms in case that at least one "include" mechanism was processed.

## [0.1.0] 2018-12-24

Expand Down
38 changes: 29 additions & 9 deletions index.js
Expand Up @@ -37,6 +37,13 @@ class SPFResult {

/** Description text. */
this.message = message;

/** Last matched mechanism or "default" is none. Used in Received-SPF
* header field. */
this.mechanism = "default";

/** List of all matched mechanisms (order from last to first). */
this.matched = [];
}
}

Expand Down Expand Up @@ -264,12 +271,27 @@ class SPF {
// TODO implement exp

for (let i = 0; i < mechanisms.length; i++) {
if (!this.options.prefetch && mechanisms[i].resolve) {
_.assign(mechanisms[i], await mechanisms[i].resolve());
const mechanism = mechanisms[i];

if (!this.options.prefetch && mechanism.resolve) {
_.assign(mechanism, await mechanism.resolve());
}

if (mechanism.type === 'include') {
mechanism.evaluated = await this.evaluate(mechanism.includes, addr);
}

if (await this.match(mechanisms[i], addr)) {
return new SPFResult(mechanisms[i].prefixdesc);
if (this.match(mechanism, addr)) {
const result = new SPFResult(mechanism.prefixdesc);

result.mechanism = mechanism.type;
result.matched = [mechanism.type];

if (mechanism.type === 'include') {
result.matched = _.merge(result.matched, mechanism.evaluated.matched);
}

return result;
}
}

Expand All @@ -278,7 +300,7 @@ class SPF {
return new SPFResult(results.Neutral);
}

async match(mechanism, addr) {
match(mechanism, addr) {
switch (mechanism.type) {
case 'version':
if (mechanism.value !== 'spf' + this.options.version) {
Expand All @@ -302,13 +324,11 @@ class SPF {
return addr.match(mechanism.address);

case 'include':
const result = await this.evaluate(mechanism.includes, addr);

if (result.result === results.None) {
if (mechanism.evaluated.result === results.None) {
throw new SPFResult(results.PermError, 'Validation for "include:' + mechanism.value + '" missed');
}

return result.result === results.Pass;
return mechanism.evaluated.result === results.Pass;

// TODO implement ptr
// TODO implement exists
Expand Down

0 comments on commit 512080b

Please sign in to comment.