-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmechanisms.js
200 lines (173 loc) · 4.62 KB
/
mechanisms.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
'use strict';
/* eslint no-useless-escape: 0 */
const tld = require('tldjs');
const ipaddr = require('ipaddr.js');
const MechanismError = require('./mechanismerror');
// TODO: need to validate CIDRs and IPs in the patterns
function domainPrefixCheck(name, pattern, term) {
let parts = term.match(pattern);
let value = parts[1];
if (!value) {
return null;
}
if (value === ':' || value === '/') {
throw new MechanismError(`Blank argument for the '${name}' mechanism`, 'error');
}
// Value starts with ':' so it's a domain
if (/^:/.test(value)) {
value = value.replace(/^:/, '');
if (!tld.isValid(value)) {
throw new MechanismError(`Invalid domain for the '${name}' mechanism: '${value}'`, 'error');
}
}
return value;
}
function domainCheckNullable(name, pattern, term) {
return domainCheck(name, pattern, term, true);
}
function domainCheck(name, pattern, term, nullable) {
let value = term.match(pattern)[1];
if (!nullable && !value) {
throw new MechanismError(`Missing mandatory argument for the '${name}' mechanism`, 'error');
}
if (value === ':' || value === '=') {
throw new MechanismError(`Blank argument for the '${name}' mechanism`, 'error');
}
if (/^(:|\=)/.test(value)) {
value = value.replace(/^(:|\=)/, '');
if (!tld.isValid(value)) {
throw new MechanismError(`Invalid domain for the '${name}' mechanism: '${value}'`, 'error');
}
}
return value;
}
module.exports = {
version: {
description: 'The SPF record version',
pattern: /^v=(.+)$/i,
validate(r) {
let version = r.match(this.pattern)[1];
// NOTE: This test can never work since we force match it to spf1 in index.js
// if (version !== 'spf1') {
// throw new MechanismError(`Invalid version '${version}', must be 'spf1'`);
// }
return version;
}
},
all: {
description: 'Always matches. It goes at the end of your record',
pattern: /^all$/i
},
ip4: {
// ip4:<ip4-address>
// ip4:<ip4-network>/<prefix-length>
description: 'Match if IP is in the given range',
pattern: /^ip4:(([\d\.]*)(\/\d+)?)$/i,
validate(r) {
let parts = r.match(this.pattern);
let value = parts[1];
let ip = parts[2];
let cidr = parts[3];
if (!value) {
throw new MechanismError(`Missing or blank mandatory network specification for the 'ip4' mechanism.`, 'error');
}
if (!ipaddr.isValid(ip)) {
throw new MechanismError(`Invalid IP address: '${ip}'`, 'error');
}
if (cidr) {
try {
ipaddr.parseCIDR(value);
}
catch (err) {
throw new MechanismError(`Invalid CIDR format: '${value}'`, 'error');
}
}
return value;
}
},
ip6: {
// ip6:<ip6-address>
// ip6:<ip6-network>/<prefix-length>
description: 'Match if IPv6 is in the given range',
pattern: /^ip6:((.*?)(\/\d+)?)$/i,
validate(r) {
let parts = r.match(this.pattern);
let value = parts[1];
let ip = parts[2];
let cidr = parts[3];
if (!value) {
throw new MechanismError(`Missing or blank mandatory network specification for the 'ip6' mechanism.`, 'error');
}
if (!ipaddr.IPv6.isValid(ip)) {
throw new MechanismError(`Invalid IPv6 address: '${ip}'`, 'error');
}
if (cidr) {
try {
ipaddr.parseCIDR(value);
}
catch (err) {
throw new MechanismError(`Invalid CIDR format: '${value}'`, 'error');
}
}
return value;
}
},
a: {
// a
// a/<prefix-length>
// a:<domain>
// a:<domain>/<prefix-length>
description: 'Match if IP has a DNS \'A\' record in given domain',
pattern: /a((:.*?)?(\/\d*)?)?$/i,
validate(r) {
return domainPrefixCheck('a', this.pattern, r);
}
},
mx: {
// mx
// mx/<prefix-length>
// mx:<domain>
// mx:<domain>/<prefix-length>
description: '',
pattern: /mx((:.*?)?(\/\d*)?)?$/i,
validate(r) {
return domainPrefixCheck('mx', this.pattern, r);
}
},
ptr: {
// ptr
// ptr:<domain>
description: 'Match if IP has a DNS \'PTR\' record within given domain',
pattern: /^ptr(:.*?)?$/i,
validate(r) {
return domainCheckNullable('ptr', this.pattern, r);
}
},
exists: {
pattern: /^exists(:.*?)?$/i,
validate(r) {
return domainCheck('exists', this.pattern, r);
}
},
include: {
description: 'The specified domain is searched for an \'allow\'',
pattern: /^include(:.*?)?$/i,
validate(r) {
return domainCheck('include', this.pattern, r);
}
},
redirect: {
description: 'The SPF record for the value replaces the current record',
pattern: /redirect(\=.*?)?$/i,
validate(r) {
return domainCheck('redirect', this.pattern, r);
}
},
exp: {
description: 'Explanation message to send with rejection',
pattern: /exp(\=.*?)?$/i,
validate(r) {
return domainCheck('exp', this.pattern, r);
}
}
};