forked from ttacon/uri
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdns.go
More file actions
320 lines (293 loc) · 7.36 KB
/
dns.go
File metadata and controls
320 lines (293 loc) · 7.36 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package uri
import (
"fmt"
"strconv"
"unicode"
"unicode/utf8"
)
// UsesDNSHostValidation reports whether the provided scheme has host validation
// that does not follow RFC3986 (which is quite generic), and assumes a valid
// DNS hostname instead.
//
// This function is declared as a global variable that may be overridden at the package level,
// in case you need specific schemes to validate the host as a DNS name.
//
// See: https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
//
//nolint:gocyclo,gochecknoglobals // essential complexity in scheme-specific DNS validation rules, might be refactored in future; public API explicitly designed to be overridable
var UsesDNSHostValidation = func(scheme string) bool {
switch scheme {
// prioritize early exit on most commonly used schemes
case "https", "http":
return true
case "file":
return false
// less commonly used schemes
case "aaa":
return true
case "aaas":
return true
case "acap":
return true
case "acct":
return true
case "cap":
return true
case "cid":
return true
case "coap", "coaps", "coap+tcp", "coap+ws", "coaps+tcp", "coaps+ws":
return true
case "dav":
return true
case "dict":
return true
case "dns":
return true
case "dntp":
return true
case "finger":
return true
case "ftp":
return true
case "git":
return true
case "gopher":
return true
case "h323":
return true
case "iax":
return true
case "icap":
return true
case "im":
return true
case "imap":
return true
case "ipp", "ipps":
return true
case "irc", "irc6", "ircs":
return true
case "jms":
return true
case "ldap":
return true
case "mailto":
return true
case "mid":
return true
case "msrp", "msrps":
return true
case "nfs":
return true
case "nntp":
return true
case "ntp":
return true
case "postgresql":
return true
case "radius":
return true
case "redis":
return true
case "rmi":
return true
case "rtsp", "rtsps", "rtspu":
return true
case "rsync":
return true
case "sftp":
return true
case "skype":
return true
case "smtp":
return true
case "snmp":
return true
case "soap":
return true
case "ssh":
return true
case "steam":
return true
case "svn":
return true
case "tcp":
return true
case "telnet":
return true
case "udp":
return true
case "vnc":
return true
case "wais":
return true
case "ws":
return true
case "wss":
return true
}
return false
}
func validateDNSHostForScheme(host string) error {
// ref: https://datatracker.ietf.org/doc/html/rfc1035
// <domain> ::= <subdomain> | " "
// <subdomain> ::= <label> | <subdomain> "." <label>
// <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
// <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
// <let-dig-hyp> ::= <let-dig> | "-"
// <let-dig> ::= <letter> | <digit>
// <letter> ::= any one of the 52 alphabetic characters A through Z in
// upper case and a through z in lower case
// <digit> ::= any one of the ten digits 0 through 9
if len(host) > maxDomainLength {
// The size considered is in bytes.
// As a result, different escaping/normalization schemes
// may or may not be valid for the same host.
return errorsJoin(
ErrInvalidDNSName,
fmt.Errorf("hostname is longer than the allowed 255 bytes: %w", ErrURI),
)
}
if len(host) == 0 {
return errorsJoin(
ErrInvalidDNSName,
fmt.Errorf("a DNS name should not contain an empty segment: %w", ErrURI),
)
}
for offset := 0; offset < len(host); {
last, consumed, err := validateHostSegment(host[offset:])
if err != nil {
return err
}
if last != dotSeparator {
break
}
offset += consumed
}
return nil
}
func validateHostSegment(s string) (rune, int, error) {
// NOTE: this validator supports percent-encoded "." separators.
last, offset, err := validateFirstRuneInSegment(s)
if err != nil {
return utf8.RuneError, 0, err
}
var (
once bool
unescapedRune rune
)
for offset < len(s) {
r, size := utf8.DecodeRuneInString(s[offset:])
if r == utf8.RuneError {
return utf8.RuneError, 0, errorsJoin(
ErrInvalidDNSName,
fmt.Errorf("invalid UTF8 rune near: %q: %w", s, ErrURI),
)
}
once = true
offset += size
if r == percentMark {
if offset >= len(s) {
return utf8.RuneError, 0, errorsJoin(
ErrInvalidDNSName,
errorsJoin(ErrInvalidEscaping,
fmt.Errorf("incomplete escape sequence: %w", ErrURI),
))
}
unescapedRune, size, err = unescapePercentEncoding(s[offset:])
if err != nil {
return utf8.RuneError, 0, errorsJoin(
ErrInvalidDNSName,
errorsJoin(ErrInvalidEscaping, err),
)
}
r = unescapedRune
offset += size
}
if r == dotSeparator {
// end of segment, possibly with an escaped "."
if offset >= len(s) {
return utf8.RuneError, 0, errorsJoin(
ErrInvalidDNSName,
fmt.Errorf("a DNS name should not contain an empty segment: %w", ErrURI),
)
}
if !unicode.IsLetter(last) && !unicode.IsDigit(last) {
return utf8.RuneError, 0, errorsJoin(
ErrInvalidDNSName,
fmt.Errorf("a segment in a DNS name must end with a letter or a digit: %q ends with %q: %w", s, last, ErrURI),
)
}
return r, offset, nil
}
if offset > maxSegmentLength {
return utf8.RuneError, 0, errorsJoin(
ErrInvalidDNSName,
fmt.Errorf("a segment in a DNS name should not be longer than 63 bytes: %q: %w", s[:offset], ErrURI),
)
}
if !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '-' {
return utf8.RuneError, 0, errorsJoin(
ErrInvalidDNSName,
fmt.Errorf("a segment in a DNS name must contain only letters, digits or '-': %q contains %q: %w", s, r, ErrURI),
)
}
last = r
}
// last rune in segment
if once && !unicode.IsLetter(last) && !unicode.IsDigit(last) {
return utf8.RuneError, 0, errorsJoin(
ErrInvalidDNSName,
fmt.Errorf("a segment in a DNS name must end with a letter or a digit: %q ends with %q: %w", s, last, ErrURI),
)
}
return last, offset, nil
}
func validateFirstRuneInSegment(s string) (rune, int, error) {
// validate the first rune for a DNS host segment
var offset int
r, size := utf8.DecodeRuneInString(s)
if r == utf8.RuneError {
return utf8.RuneError, 0, errorsJoin(
ErrInvalidDNSName,
fmt.Errorf("invalid UTF8 rune near: %q: %w", s, ErrURI),
)
}
if r == dotSeparator {
return utf8.RuneError, 0, errorsJoin(
ErrInvalidDNSName,
fmt.Errorf("a DNS name should not contain an empty segment: %w", ErrURI),
)
}
offset += size
if r == percentMark {
if offset >= len(s) {
return utf8.RuneError, 0, errorsJoin(
errorsJoin(ErrInvalidEscaping,
fmt.Errorf("incomplete escape sequence: %w", ErrURI),
))
}
unescapedRune, consumed, e := unescapePercentEncoding(s[offset:])
if e != nil {
return utf8.RuneError, 0, errorsJoin(
ErrInvalidDNSName,
errorsJoin(ErrInvalidEscaping, e),
)
}
r = unescapedRune
offset += consumed
}
// If it is a number we fail here to fall back to IP parsing
if _, err := strconv.Atoi(string([]rune{r}) + s[offset:]); err == nil {
return utf8.RuneError, 0, errorsJoin(
ErrInvalidDNSName,
fmt.Errorf("hostname cannot just be a number: %w", ErrURI))
}
if !unicode.IsLetter(r) && (!unicode.IsDigit(r) || offset >= len(s)) {
return utf8.RuneError, 0, errorsJoin(
ErrInvalidDNSName,
fmt.Errorf("a segment in a DNS name must begin with a letter: %q starts with %q: %w", s, r, ErrURI),
)
}
return r, offset, nil
}