-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathheader.go
350 lines (301 loc) · 11.2 KB
/
header.go
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package hstspreload
import (
"fmt"
"strconv"
"strings"
"github.com/chromium/hstspreload/chromium/preloadlist"
)
const (
eighteenWeeks = 86400 * 7 * 18
oneYear = 86400 * 365
tenYears = 10 * oneYear
hstsMinimumMaxAge = oneYear
)
// MaxAge holds the max-age of an HSTS header in seconds.
// See https://tools.ietf.org/html/rfc6797#section-6.1.1
type MaxAge struct {
Seconds uint64 `json:"seconds"`
}
// An HSTSHeader stores the semantics of an HSTS header.
// https://tools.ietf.org/html/rfc6797#section-6.1
//
// Note that the `preload` directive is not standardized yet: https://crbug.com/591212
type HSTSHeader struct {
// A MaxAge of `nil` indicates "not present".
MaxAge *MaxAge `json:"max_age,omitempty"`
IncludeSubDomains bool `json:"includeSubDomains"`
Preload bool `json:"preload"`
}
// Iff Issues has no errors, the output integer is the max-age in seconds.
// Note that according to the spec, the max-age value may optionally be quoted:
// https://tools.ietf.org/html/rfc6797#section-6.2
// However, it seems no one does this in practice, and certainly no one has
// asked to be preloaded with a quoted max-age value. So to keep things simple,
// we don't support quoted values.
func parseMaxAge(directive string) (*MaxAge, Issues) {
issues := Issues{}
maxAgeNumericalString := directive[8:]
// TODO: Use more concise validation code to parse a digit string to a signed int.
for i, c := range maxAgeNumericalString {
if i == 0 && c == '0' && len(maxAgeNumericalString) > 1 {
issues = issues.addWarningf(
"header.parse.max_age.leading_zero",
"Unexpected max-age syntax",
"The header's max-age value contains a leading 0: `%s`", directive)
}
if c < '0' || c > '9' {
return nil, issues.addErrorf(
"header.parse.max_age.non_digit_characters",
"Invalid max-age syntax",
"The header's max-age value contains characters that are not digits: `%s`", directive)
}
}
seconds, err := strconv.ParseUint(maxAgeNumericalString, 10, 64)
if err != nil {
return nil, issues.addErrorf(
"header.parse.max_age.parse_int_error",
"Invalid max-age syntax",
"We could not parse the header's max-age value `%s`.", maxAgeNumericalString)
}
return &MaxAge{Seconds: seconds}, issues
}
// ParseHeaderString parses an HSTS header. ParseHeaderString will
// report syntax errors and warnings, but does NOT calculate whether the
// header value is semantically valid. (See PreloadableHeaderString() for
// that.)
//
// To interpret the Issues that are returned, see the list of
// conventions in the documentation for Issues.
func ParseHeaderString(headerString string) (HSTSHeader, Issues) {
hstsHeader := HSTSHeader{}
issues := Issues{}
directives := strings.Split(headerString, ";")
for i, directive := range directives {
// TODO: this trims more than spaces and tabs (LWS). https://crbug.com/596561#c10
directives[i] = strings.TrimSpace(directive)
}
// If strings.Split() is given whitespace, it still returns an (empty) directive.
// So we handle this case separately.
if len(directives) == 1 && directives[0] == "" {
// Return immediately, because all the extra information is redundant.
return hstsHeader, issues.addWarningf(
"header.parse.empty",
"Empty Header",
"The HSTS header is empty.")
}
for _, directive := range directives {
directiveEqualsIgnoringCase := func(s string) bool {
return strings.EqualFold(directive, s)
}
directiveHasPrefixIgnoringCase := func(prefix string) bool {
return strings.HasPrefix(strings.ToLower(directive), strings.ToLower(prefix))
}
switch {
case directiveEqualsIgnoringCase("preload"):
if hstsHeader.Preload {
issues = issues.addUniqueWarningf(
"header.parse.repeated.preload",
"Repeated preload directive",
"Header contains a repeated directive: `preload`")
} else {
hstsHeader.Preload = true
}
case directiveHasPrefixIgnoringCase("preload"):
issues = issues.addUniqueWarningf(
"header.parse.invalid.preload",
"Invalid preload directive",
"Header contains a `preload` directive with extra parts.")
case directiveEqualsIgnoringCase("includeSubDomains"):
if hstsHeader.IncludeSubDomains {
issues = issues.addUniqueWarningf(
"header.parse.repeated.include_sub_domains",
"Repeated includeSubDomains directive",
"Header contains a repeated directive: `includeSubDomains`")
} else {
hstsHeader.IncludeSubDomains = true
}
case directiveHasPrefixIgnoringCase("includeSubDomains"):
issues = issues.addUniqueWarningf(
"header.parse.invalid.include_sub_domains",
"Invalid includeSubDomains directive",
"The header contains an `includeSubDomains` directive with extra directives.")
case directiveHasPrefixIgnoringCase("max-age="):
maxAge, maxAgeIssues := parseMaxAge(directive)
issues = combineIssues(issues, maxAgeIssues)
if len(maxAgeIssues.Errors) > 0 {
continue
}
if hstsHeader.MaxAge == nil {
hstsHeader.MaxAge = maxAge
} else {
issues = issues.addUniqueWarningf(
"header.parse.repeated.max_age",
"Repeated max-age directive",
"The header contains a repeated directive: `max-age`")
}
case directiveHasPrefixIgnoringCase("max-age"):
issues = issues.addUniqueErrorf(
"header.parse.invalid.max_age.no_value",
"Max-age drective without a value",
"The header contains a max-age directive name without an associated value. Please specify the max-age in seconds.")
case directiveEqualsIgnoringCase(""):
issues = issues.addUniqueWarningf(
"header.parse.empty_directive",
"Empty directive or extra semicolon",
"The header includes an empty directive or extra semicolon.")
default:
issues = issues.addWarningf(
"header.parse.unknown_directive",
"Unknown directive",
"The header contains an unknown directive: `%s`", directive)
}
}
return hstsHeader, issues
}
func preloadableHeaderPreload(hstsHeader HSTSHeader) Issues {
issues := Issues{}
if !hstsHeader.Preload {
issues = issues.addErrorf(
"header.preloadable.preload.missing",
"No preload directive",
"The header must contain the `preload` directive.")
}
return issues
}
func preloadableHeaderSubDomains(hstsHeader HSTSHeader) Issues {
issues := Issues{}
if !hstsHeader.IncludeSubDomains {
issues = issues.addErrorf(
"header.preloadable.include_sub_domains.missing",
"No includeSubDomains directive",
"The header must contain the `includeSubDomains` directive.")
}
return issues
}
func preloadableHeaderMaxAge(hstsHeader HSTSHeader, policy preloadlist.PolicyType) Issues {
issues := Issues{}
maxAge := hstsMinimumMaxAge
ageName := "1 year"
if policy == preloadlist.Bulk18Weeks {
maxAge = eighteenWeeks
ageName = "18 weeks"
}
switch {
case hstsHeader.MaxAge == nil:
issues = issues.addErrorf(
"header.preloadable.max_age.missing",
"No max-age directice",
"Header requirement error: Header must contain a valid `max-age` directive.")
case hstsHeader.MaxAge.Seconds < uint64(maxAge):
errorStr := fmt.Sprintf(
"The max-age must be at least %d seconds (≈ %s), but the header currently only has max-age=%d.",
maxAge, ageName, hstsHeader.MaxAge.Seconds,
)
if hstsHeader.MaxAge.Seconds == 0 {
errorStr += " If you are trying to remove this domain from the preload list, please visit https://hstspreload.org/removal/"
issues = issues.addErrorf(
"header.preloadable.max_age.zero",
"Max-age is 0",
errorStr,
)
} else if policy == preloadlist.Bulk18Weeks {
issues = issues.addErrorf(
"header.preloadable.max_age.below_18_weeks",
"Max-age too low",
errorStr,
)
} else {
issues = issues.addErrorf(
"header.preloadable.max_age.below_1_year",
"Max-age too low",
errorStr,
)
}
case hstsHeader.MaxAge.Seconds > tenYears:
issues = issues.addWarningf(
"header.preloadable.max_age.over_10_years",
"Max-age > 10 years",
"FYI: The max-age (%d seconds) is longer than 10 years, which is an unusually long value.",
hstsHeader.MaxAge.Seconds,
)
}
return issues
}
// PreloadableHeader checks whether hstsHeader satisfies all requirements
// for preloading in Chromium.
//
// To interpret the result, see the list of conventions in the
// documentation for Issues.
//
// Most of the time, you'll probably want to use PreloadableHeaderString() instead.
func PreloadableHeader(hstsHeader HSTSHeader) Issues {
return EligibleHeader(hstsHeader, preloadlist.Bulk1Year)
}
func EligibleHeader(hstsHeader HSTSHeader, policy preloadlist.PolicyType) Issues {
issues := Issues{}
issues = combineIssues(issues, preloadableHeaderSubDomains(hstsHeader))
issues = combineIssues(issues, preloadableHeaderPreload(hstsHeader))
issues = combineIssues(issues, preloadableHeaderMaxAge(hstsHeader, policy))
return issues
}
// RemovableHeader checks whether the header satisfies all requirements
// for being removed from the Chromium preload list.
//
// To interpret the result, see the list of conventions in the
// documentation for Issues.
//
// Most of the time, you'll probably want to use RemovableHeaderString() instead.
func RemovableHeader(hstsHeader HSTSHeader) Issues {
issues := Issues{}
if hstsHeader.Preload {
issues = issues.addErrorf(
"header.removable.contains.preload",
"Contains preload directive",
"Header requirement error: For preload list removal, the header must not contain the `preload` directive.")
}
if hstsHeader.MaxAge == nil {
issues = issues.addErrorf(
"header.removable.missing.max_age",
"No max-age directive",
"Header requirement error: Header must contain a valid `max-age` directive.")
}
return issues
}
// PreloadableHeaderString is a convenience function that calls
// ParseHeaderString() and then calls on PreloadableHeader() the parsed
// header. It returns all issues from both calls, combined.
//
// To interpret the result, see the list of conventions in the
// documentation for Issues.
func PreloadableHeaderString(headerString string) Issues {
hstsHeader, issues := ParseHeaderString(headerString)
return combineIssues(issues, PreloadableHeader(hstsHeader))
}
// EligibleHeaderString is a convenience function that calls
// ParseHeaderString() and then calls on EligibleHeader() the parsed
// header. It returns all issues from both calls, combined.
//
// To interpret the result, see the list of conventions in the
// documentation for Issues.
func EligibleHeaderString(headerString string, policy preloadlist.PolicyType) Issues {
hstsHeader, issues := ParseHeaderString(headerString)
return combineIssues(issues, EligibleHeader(hstsHeader, policy))
}
// RemovableHeaderString is a convenience function that calls
// ParseHeaderString() and then calls on RemovableHeader() the parsed
// header. It returns all errors from ParseHeaderString() and all
// issues from RemovableHeader(). Note that *warnings* from
// ParseHeaderString() are ignored, since domains asking to be removed
// will often have minor errors that shouldn't affect removal. It's
// better to have a cleaner verdict in this case.
//
// To interpret the result, see the list of conventions in the
// documentation for Issues.
func RemovableHeaderString(headerString string) Issues {
hstsHeader, issues := ParseHeaderString(headerString)
issues = Issues{
Errors: issues.Errors,
// Ignore parse warnings for removal testing.
}
return combineIssues(issues, RemovableHeader(hstsHeader))
}