dmarcator 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/ChangeLog.md +55 -0
- data/LICENSE.txt +21 -0
- data/README.md +137 -0
- data/lib/dmarcator/exceptions.rb +6 -0
- data/lib/dmarcator/parser.rb +221 -0
- data/lib/dmarcator/query.rb +63 -0
- data/lib/dmarcator/record.rb +393 -0
- data/lib/dmarcator/uri.rb +114 -0
- data/lib/dmarcator/version.rb +4 -0
- data/lib/dmarcator.rb +5 -0
- data/spec/data/alexa.csv +500 -0
- data/spec/dmarcator_spec.rb +62 -0
- data/spec/parser_spec.rb +413 -0
- data/spec/record_spec.rb +229 -0
- data/spec/spec_helper.rb +11 -0
- data/spec/uri_spec.rb +141 -0
- metadata +88 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: e13144ffc39b0f47db1c26f65201c05db51af4701cf51ffd2398dcab8011a791
|
|
4
|
+
data.tar.gz: 7560c54eb55c97de2a841e22f5c1ff4469741b23a2bd3f593934eb24b21c3582
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: f7966f69b4b9741fad6a84c6f502631137f32891955d81a3f94e3b83fe01810fca1a8a94544dbb4f81f6adbc361b6d6d89374377d435e46f8df44a82e247a37a
|
|
7
|
+
data.tar.gz: 016b57445827a1c297870e079a148be4c0ce786dabc3f0cb2574a09beb00c8d91742a117b0d03e8095c3b4f4aeeb46a22a88431c3c4b781df948021a77f1bab4
|
data/ChangeLog.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
### 0.1.0 / 2026-10-13
|
|
2
|
+
|
|
3
|
+
* Forked from the Trail of Bits `dmarc` gem and published as `dmarcator`.
|
|
4
|
+
* Renamed the `DMARC` module to `DMARCator` (`require "dmarcator"`).
|
|
5
|
+
* Hardened parsing and DNS querying; see the README for details.
|
|
6
|
+
|
|
7
|
+
### 0.6.0 / 2024-01-24
|
|
8
|
+
|
|
9
|
+
Entries below this point refer to the original `dmarc` gem.
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
* {DMARC::Record.parse} now allows trailing whitespace, per DMARC spec.
|
|
13
|
+
|
|
14
|
+
### 0.5.0 / 2016-06-16
|
|
15
|
+
|
|
16
|
+
* Added {DMARC::Uri} to represent `mailto:dmarc@example.com!10m` URIs.
|
|
17
|
+
|
|
18
|
+
### 0.4.0 / 2016-06-10
|
|
19
|
+
|
|
20
|
+
* Added {DMARC::Record#to_h}.
|
|
21
|
+
* Added {DMARC::Record#adkim?}.
|
|
22
|
+
* Added {DMARC::Record#aspf?}.
|
|
23
|
+
* Added {DMARC::Record#fo?}.
|
|
24
|
+
* Added {DMARC::Record#p?}.
|
|
25
|
+
* Added {DMARC::Record#pct?}.
|
|
26
|
+
* Added {DMARC::Record#rf?}.
|
|
27
|
+
* Added {DMARC::Record#ri?}.
|
|
28
|
+
* Added {DMARC::Record#rua?}.
|
|
29
|
+
* Added {DMARC::Record#ruf?}.
|
|
30
|
+
* Added {DMARC::Record#sp?}.
|
|
31
|
+
|
|
32
|
+
### 0.3.0 / 2015-07-01
|
|
33
|
+
|
|
34
|
+
* Added {DMARC::Record.query}.
|
|
35
|
+
* Added {DMARC::Record#to_s}.
|
|
36
|
+
* Added {DMARC::InvalidRecord}.
|
|
37
|
+
* Deprecate {DMARC::Record.from_txt}.
|
|
38
|
+
* {DMARC::Record#v} now returns `:DMARC1`.
|
|
39
|
+
* {DMARC::Record#p} and {DMARC::Record#sp} now return Symbols
|
|
40
|
+
* {DMARC::Record#rua} and {DMARC::Record#ruf} will always return Arrays.
|
|
41
|
+
* Fixed a bug in {DMARC::Parser} with respect to order of tags.
|
|
42
|
+
* {DMARC::Parser::Transform} now coerces URIs into URI objects.
|
|
43
|
+
|
|
44
|
+
### 0.2.0 / 2014-10-20
|
|
45
|
+
|
|
46
|
+
* Added `DMARC::Error`.
|
|
47
|
+
* Added {DMARC::InvalidRecord}.
|
|
48
|
+
* Add support for parsing `fo` tokens.
|
|
49
|
+
* Ignore unknown tags instead of raising a parser exception.
|
|
50
|
+
* Ignore tags with invalid values instead of raising a parser exception.
|
|
51
|
+
|
|
52
|
+
### 0.1.0 / 2014-04-25
|
|
53
|
+
|
|
54
|
+
* Initial release.
|
|
55
|
+
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014-2016 Trail of Bits
|
|
4
|
+
Copyright (c) 2026 Noah Berman
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
7
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
8
|
+
the Software without restriction, including without limitation the rights to
|
|
9
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
10
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
11
|
+
subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
18
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
19
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
20
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
21
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# DMARCator
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/rb/dmarcator)
|
|
4
|
+
[](https://rubydoc.info/gems/dmarcator)
|
|
5
|
+
|
|
6
|
+
DMARCator is a Ruby library for parsing and querying [DMARC] records.
|
|
7
|
+
|
|
8
|
+
> **DMARCator is a fork of the [`dmarc`][dmarc-gem] gem, originally created by
|
|
9
|
+
> Davis Gallinghouse at [Trail of Bits].** It keeps the original MIT license and
|
|
10
|
+
> credits its authors, but ships under a new name with additional hardening and
|
|
11
|
+
> a stricter parser. See [Acknowledgments](#acknowledgments) below.
|
|
12
|
+
|
|
13
|
+
[DMARC] policies are published as DNS TXT records at `_dmarc.<domain>`. This
|
|
14
|
+
library parses those records into a structured `DMARCator::Record` and can query
|
|
15
|
+
them directly through Ruby's `Resolv` DNS client.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
Add this line to your application's Gemfile:
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
gem "dmarcator"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
And then execute:
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
bundle install
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Or install it yourself:
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
gem install dmarcator
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
The gem is loaded via:
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
require "dmarcator"
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Example
|
|
44
|
+
|
|
45
|
+
Parse a DMARC record:
|
|
46
|
+
|
|
47
|
+
```ruby
|
|
48
|
+
require "dmarcator"
|
|
49
|
+
|
|
50
|
+
record = DMARCator::Record.parse(
|
|
51
|
+
"v=DMARC1; p=reject; rua=mailto:d@rua.agari.com; ruf=mailto:d@ruf.agari.com; fo=1"
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
record.v
|
|
55
|
+
# => :DMARC1
|
|
56
|
+
|
|
57
|
+
record.p
|
|
58
|
+
# => :reject
|
|
59
|
+
|
|
60
|
+
record.adkim
|
|
61
|
+
# => :r
|
|
62
|
+
|
|
63
|
+
record.aspf
|
|
64
|
+
# => :r
|
|
65
|
+
|
|
66
|
+
record.fo
|
|
67
|
+
# => ["0"]
|
|
68
|
+
|
|
69
|
+
record.pct
|
|
70
|
+
# => 100
|
|
71
|
+
|
|
72
|
+
record.rf
|
|
73
|
+
# => :afrf
|
|
74
|
+
|
|
75
|
+
record.ri
|
|
76
|
+
# => 86400
|
|
77
|
+
|
|
78
|
+
record.rua
|
|
79
|
+
# => [#<DMARCator::Uri ... @uri=#<URI::MailTo mailto:d@rua.agari.com>, @size=nil, @unit=nil>]
|
|
80
|
+
|
|
81
|
+
record.ruf
|
|
82
|
+
# => [#<DMARCator::Uri ... @uri=#<URI::MailTo mailto:d@ruf.agari.com>, @size=nil, @unit=nil>]
|
|
83
|
+
|
|
84
|
+
record.sp
|
|
85
|
+
# => :reject
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Query the DMARC record for a domain:
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
record = DMARCator::Record.query("twitter.com")
|
|
92
|
+
# => #<DMARCator::Record ... @p=:reject, @rua=[...], ...>
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Requirements
|
|
96
|
+
|
|
97
|
+
* [Ruby] >= 3.0.0
|
|
98
|
+
* [parslet] >= 1.0.0, < 3.0.0
|
|
99
|
+
|
|
100
|
+
## Testing
|
|
101
|
+
|
|
102
|
+
To run the RSpec tests:
|
|
103
|
+
|
|
104
|
+
```sh
|
|
105
|
+
rake spec
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
To test the parser against the bundled Alexa Top 500 fixture:
|
|
109
|
+
|
|
110
|
+
```sh
|
|
111
|
+
rake spec:gauntlet
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## License
|
|
115
|
+
|
|
116
|
+
This project is released under the MIT License. See the [LICENSE.txt] file for
|
|
117
|
+
details.
|
|
118
|
+
|
|
119
|
+
## Acknowledgments
|
|
120
|
+
|
|
121
|
+
DMARCator is a fork of [`dmarc`][dmarc-gem], a DMARC record parser created by
|
|
122
|
+
Davis Gallinghouse at [Trail of Bits] and released under the MIT License.
|
|
123
|
+
|
|
124
|
+
This fork renames the `DMARC` module to `DMARCator` (loaded via
|
|
125
|
+
`require "dmarcator"`) while keeping the original API shape, and adds:
|
|
126
|
+
|
|
127
|
+
* strict `mailto:`-only handling for `rua`/`ruf` report URIs,
|
|
128
|
+
* validation for required and ranged tags (`p`, `pct`, `ri`) and duplicate tags,
|
|
129
|
+
* a DNS query path that selects the `v=DMARC1` TXT record per RFC 7489,
|
|
130
|
+
* hardened packaging and test fixtures.
|
|
131
|
+
|
|
132
|
+
[DMARC]: https://www.rfc-editor.org/rfc/rfc7489
|
|
133
|
+
[dmarc-gem]: https://rubygems.org/gems/dmarc
|
|
134
|
+
[Trail of Bits]: https://www.trailofbits.com
|
|
135
|
+
[Ruby]: https://www.ruby-lang.org
|
|
136
|
+
[parslet]: https://kschiess.github.io/parslet/
|
|
137
|
+
[LICENSE.txt]: LICENSE.txt
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
require 'dmarcator/exceptions'
|
|
2
|
+
require 'dmarcator/uri'
|
|
3
|
+
|
|
4
|
+
require 'parslet'
|
|
5
|
+
require 'uri'
|
|
6
|
+
|
|
7
|
+
module DMARCator
|
|
8
|
+
#
|
|
9
|
+
# DMARC parser.
|
|
10
|
+
#
|
|
11
|
+
# @see https://tools.ietf.org/html/rfc7489#section-6.4
|
|
12
|
+
#
|
|
13
|
+
class Parser < Parslet::Parser
|
|
14
|
+
|
|
15
|
+
root :dmarc_record
|
|
16
|
+
|
|
17
|
+
rule(:dmarc_record) do
|
|
18
|
+
dmarc_version.repeat(1,1) >>
|
|
19
|
+
(dmarc_sep >> dmarc_tag).repeat >>
|
|
20
|
+
dmarc_sep.maybe >>
|
|
21
|
+
wsp?.maybe
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
rule(:dmarc_sep) { wsp? >> str(';') >> wsp? }
|
|
25
|
+
|
|
26
|
+
rule(:dmarc_version) do
|
|
27
|
+
str('v') >> wsp? >>
|
|
28
|
+
str('=') >> wsp? >>
|
|
29
|
+
str('DMARC1').as(:v)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
rule(:dmarc_tag) do
|
|
33
|
+
dmarc_request |
|
|
34
|
+
dmarc_srequest |
|
|
35
|
+
dmarc_auri |
|
|
36
|
+
dmarc_furi |
|
|
37
|
+
dmarc_adkim |
|
|
38
|
+
dmarc_aspf |
|
|
39
|
+
dmarc_ainterval |
|
|
40
|
+
dmarc_fo |
|
|
41
|
+
dmarc_rfmt |
|
|
42
|
+
dmarc_percent |
|
|
43
|
+
unknown_tag
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def self.tag_rule(name,tag,&block)
|
|
47
|
+
rule(:"dmarc_#{name}") do
|
|
48
|
+
str(tag) >> wsp? >> str('=') >> wsp? >>
|
|
49
|
+
(instance_eval(&block).as(tag.to_sym) | unknown_value)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
tag_rule(:request,'p') do
|
|
54
|
+
str('none') | str('quarantine') | str('reject')
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
tag_rule(:srequest,'sp') do
|
|
58
|
+
str('none') | str('quarantine') | str('reject')
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
tag_rule(:auri, 'rua') do
|
|
62
|
+
dmarc_uri >> (wsp? >> str(',') >> wsp? >> dmarc_uri).repeat
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
tag_rule(:ainterval,'ri') { digit.repeat(1, 10) >> digit.absent? }
|
|
66
|
+
|
|
67
|
+
tag_rule(:furi,'ruf') do
|
|
68
|
+
dmarc_uri >> (wsp? >> str(',') >> wsp? >> dmarc_uri).repeat
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
tag_rule(:fo,'fo') do
|
|
72
|
+
fo_opt >> (wsp? >> str(':') >> wsp? >> fo_opt).repeat
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
rule(:fo_opt) { match['01ds'].as(:fo_opt) }
|
|
76
|
+
|
|
77
|
+
tag_rule(:rfmt,'rf') { str('afrf') | str('iodef') }
|
|
78
|
+
|
|
79
|
+
tag_rule(:percent,'pct') { digit.repeat(1,3) >> digit.absent? }
|
|
80
|
+
|
|
81
|
+
tag_rule(:adkim, 'adkim') { match['rs'] }
|
|
82
|
+
tag_rule(:aspf, 'aspf') { match['rs'] }
|
|
83
|
+
|
|
84
|
+
rule(:unknown_tag) { match["^; \t"].repeat(1) }
|
|
85
|
+
rule(:unknown_value) { match["^; \t"].repeat(1) }
|
|
86
|
+
|
|
87
|
+
rule(:dmarc_uri) do
|
|
88
|
+
mailto_uri.as(:uri) >> (
|
|
89
|
+
str('!') >> digit.repeat(1, 10).as(:size) >> digit.absent? >> (
|
|
90
|
+
match['kmgt']
|
|
91
|
+
).as(:unit).maybe
|
|
92
|
+
).maybe >> str('!').absent?
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
rule(:mailto_uri) do
|
|
96
|
+
str('mailto:') >> mailto_address
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
rule(:mailto_address) do
|
|
100
|
+
mailto_local >> str('@') >> mailto_domain
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
rule(:mailto_local) { mailto_char.repeat(1) }
|
|
104
|
+
rule(:mailto_domain) { mailto_char.repeat(1) }
|
|
105
|
+
rule(:mailto_char) { match["^;,! \t@"] }
|
|
106
|
+
|
|
107
|
+
rule(:digit) { match('[0-9]') }
|
|
108
|
+
rule(:wsp) { str(' ') | str("\t") }
|
|
109
|
+
rule(:wsp?) { wsp.repeat }
|
|
110
|
+
|
|
111
|
+
class Transform < Parslet::Transform
|
|
112
|
+
|
|
113
|
+
rule(fo_opt: simple(:fo_opt)) { fo_opt }
|
|
114
|
+
rule(fo: simple(:fo_opt)) { {fo: [fo_opt]} }
|
|
115
|
+
|
|
116
|
+
rule(v: simple(:version)) { {v: version.to_sym} }
|
|
117
|
+
rule(p: simple(:p)) { {p: p.to_sym } }
|
|
118
|
+
rule(sp: simple(:sp)) { {sp: sp.to_sym} }
|
|
119
|
+
rule(rf: simple(:rf)) { {rf: rf.to_sym} }
|
|
120
|
+
rule(adkim: simple(:adkim)) { {adkim: adkim.to_sym} }
|
|
121
|
+
rule(aspf: simple(:aspf)) { {aspf: aspf.to_sym} }
|
|
122
|
+
|
|
123
|
+
rule(pct: simple(:pct)) { {pct: pct.to_i} }
|
|
124
|
+
rule(ri: simple(:ri)) { {ri: ri.to_i} }
|
|
125
|
+
|
|
126
|
+
rule(uri: simple(:uri), size: simple(:size), unit: simple(:unit)) do
|
|
127
|
+
Uri.new(
|
|
128
|
+
Transform.parse_uri(uri),
|
|
129
|
+
size.to_i,
|
|
130
|
+
unit.to_sym
|
|
131
|
+
)
|
|
132
|
+
end
|
|
133
|
+
rule(uri: simple(:uri), size: simple(:size)) do
|
|
134
|
+
Uri.new(
|
|
135
|
+
Transform.parse_uri(uri),
|
|
136
|
+
size.to_i
|
|
137
|
+
)
|
|
138
|
+
end
|
|
139
|
+
rule(uri: simple(:uri)) { Uri.new(Transform.parse_uri(uri)) }
|
|
140
|
+
rule(rua: subtree(:uris)) { {rua: Array(uris)} }
|
|
141
|
+
rule(ruf: subtree(:uris)) { {ruf: Array(uris)} }
|
|
142
|
+
|
|
143
|
+
MAX_ERROR_MESSAGE_LENGTH = 300
|
|
144
|
+
|
|
145
|
+
def self.parse_uri(value)
|
|
146
|
+
uri = value.to_s
|
|
147
|
+
|
|
148
|
+
begin
|
|
149
|
+
parsed = URI.parse(uri)
|
|
150
|
+
rescue URI::Error => error
|
|
151
|
+
raise InvalidRecord, truncate(error.message)
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
unless parsed.is_a?(URI::MailTo)
|
|
155
|
+
raise InvalidRecord, "invalid DMARC report URI (expected mailto): #{truncate(uri)}"
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
parsed
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def self.truncate(message)
|
|
162
|
+
message = message.to_s
|
|
163
|
+
|
|
164
|
+
if message.length > MAX_ERROR_MESSAGE_LENGTH
|
|
165
|
+
message[0, MAX_ERROR_MESSAGE_LENGTH] + "..."
|
|
166
|
+
else
|
|
167
|
+
message
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
#
|
|
174
|
+
# Parses a DMARC record.
|
|
175
|
+
#
|
|
176
|
+
# @param [String] record
|
|
177
|
+
# The raw DMARC record to parse.
|
|
178
|
+
#
|
|
179
|
+
# @return [Hash{Symbol => Object}]
|
|
180
|
+
# The Hash of tags within the record.
|
|
181
|
+
#
|
|
182
|
+
def self.parse(record)
|
|
183
|
+
tags = Transform.new.apply(new.parse(record))
|
|
184
|
+
hash = {}
|
|
185
|
+
|
|
186
|
+
tags.each do |tag|
|
|
187
|
+
tag.each_key do |key|
|
|
188
|
+
if hash.key?(key)
|
|
189
|
+
raise InvalidRecord, "duplicate DMARC tag: #{key}"
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
hash.merge!(tag)
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
validate!(hash)
|
|
197
|
+
|
|
198
|
+
return hash
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
#
|
|
202
|
+
# Validates the parsed tags.
|
|
203
|
+
#
|
|
204
|
+
# @param [Hash{Symbol => Object}] tags
|
|
205
|
+
# The parsed tags.
|
|
206
|
+
#
|
|
207
|
+
# @raise [InvalidRecord]
|
|
208
|
+
# One of the tags has an invalid value.
|
|
209
|
+
#
|
|
210
|
+
def self.validate!(tags)
|
|
211
|
+
if tags[:pct] && !(tags[:pct].is_a?(Integer) && tags[:pct].between?(0, 100))
|
|
212
|
+
raise InvalidRecord, "pct must be an integer between 0 and 100"
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
if tags[:ri] && !(tags[:ri].is_a?(Integer) && tags[:ri].between?(0, 2**32 - 1))
|
|
216
|
+
raise InvalidRecord, "ri must be an integer between 0 and 4294967295"
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
end
|
|
221
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
require 'resolv'
|
|
2
|
+
|
|
3
|
+
module DMARCator
|
|
4
|
+
#
|
|
5
|
+
# Validates a domain before using it in a DNS query.
|
|
6
|
+
#
|
|
7
|
+
# @param [String] domain
|
|
8
|
+
# The domain to validate.
|
|
9
|
+
#
|
|
10
|
+
# @return [String]
|
|
11
|
+
# The validated domain.
|
|
12
|
+
#
|
|
13
|
+
# @raise [ArgumentError]
|
|
14
|
+
# The domain is not a syntactically valid DNS name.
|
|
15
|
+
#
|
|
16
|
+
def self.validate_domain(domain)
|
|
17
|
+
unless domain.is_a?(String) && !domain.empty? &&
|
|
18
|
+
domain.length <= 253 &&
|
|
19
|
+
!domain.start_with?('.') &&
|
|
20
|
+
!domain.include?('..') &&
|
|
21
|
+
!domain.match?(/[^A-Za-z0-9._-]/)
|
|
22
|
+
raise ArgumentError, "invalid domain: #{domain.inspect}"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
domain
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
#
|
|
29
|
+
# Queries a domain for the DMARC record.
|
|
30
|
+
#
|
|
31
|
+
# @param [String] domain
|
|
32
|
+
# The domain to query DMARC for.
|
|
33
|
+
#
|
|
34
|
+
# @param [Resolv::DNS] resolver
|
|
35
|
+
# The resolver to use.
|
|
36
|
+
#
|
|
37
|
+
# @return [String, nil]
|
|
38
|
+
# The domain's DMARC record or `nil` if none exists.
|
|
39
|
+
#
|
|
40
|
+
# @api semipublic
|
|
41
|
+
#
|
|
42
|
+
# @since 0.3.0
|
|
43
|
+
#
|
|
44
|
+
def self.query(domain,resolver=Resolv::DNS.new)
|
|
45
|
+
validate_domain(domain)
|
|
46
|
+
|
|
47
|
+
host = "_dmarc.#{domain}"
|
|
48
|
+
|
|
49
|
+
begin
|
|
50
|
+
records = resolver.getresources(host, Resolv::DNS::Resource::IN::TXT)
|
|
51
|
+
rescue Resolv::ResolvError
|
|
52
|
+
return nil
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Per RFC 7489 §6.6.3, use the TXT record beginning with "v=DMARC1".
|
|
56
|
+
# If multiple DMARC records exist, treat the domain as having none.
|
|
57
|
+
dmarc_records = records.map { |record| record.strings.join }
|
|
58
|
+
.select { |text| text.start_with?('v=DMARC1') }
|
|
59
|
+
|
|
60
|
+
return dmarc_records.first if dmarc_records.one?
|
|
61
|
+
nil
|
|
62
|
+
end
|
|
63
|
+
end
|