@@ -17,6 +17,11 @@ exports.version = function () {
17
17
} ;
18
18
19
19
20
+ exports . limits = {
21
+ maxMatchLength : 4096 // Limit the length of uris and headers to avoid a DoS attack on string matching
22
+ } ;
23
+
24
+
20
25
// Extract host and port from request
21
26
22
27
// $1 $2
@@ -31,6 +36,10 @@ exports.parseHost = function (req, hostHeaderName) {
31
36
return null ;
32
37
}
33
38
39
+ if ( hostHeader . length > exports . limits . maxMatchLength ) {
40
+ return null ;
41
+ }
42
+
34
43
const hostParts = hostHeader . match ( internals . hostHeaderRegex ) ;
35
44
if ( ! hostParts ) {
36
45
return null ;
@@ -100,6 +109,10 @@ exports.nowSecs = function (localtimeOffsetMsec) {
100
109
} ;
101
110
102
111
112
+ internals . authHeaderRegex = / ^ ( \w + ) (?: \s + ( .* ) ) ? $ / ; // Header: scheme[ something]
113
+ internals . attributeRegex = / ^ [ \w \! # \$ % & ' \( \) \* \+ , \- \. \/ \: ; < \= > \? @ \[ \] \^ ` \{ \| \} ~ ] + $ / ; // !#$%&'()*+,-./:;<=>?@[]^_`{|}~ and space, a-z, A-Z, 0-9
114
+
115
+
103
116
// Parse Hawk HTTP Authorization header
104
117
105
118
exports . parseAuthorizationHeader = function ( header , keys ) {
@@ -110,7 +123,11 @@ exports.parseAuthorizationHeader = function (header, keys) {
110
123
return Boom . unauthorized ( null , 'Hawk' ) ;
111
124
}
112
125
113
- const headerParts = header . match ( / ^ ( \w + ) (?: \s + ( .* ) ) ? $ / ) ; // Header: scheme[ something]
126
+ if ( header . length > exports . limits . maxMatchLength ) {
127
+ return Boom . badRequest ( 'Header length too long' ) ;
128
+ }
129
+
130
+ const headerParts = header . match ( internals . authHeaderRegex ) ;
114
131
if ( ! headerParts ) {
115
132
return Boom . badRequest ( 'Invalid header syntax' ) ;
116
133
}
@@ -136,9 +153,9 @@ exports.parseAuthorizationHeader = function (header, keys) {
136
153
return ;
137
154
}
138
155
139
- // Allowed attribute value characters: !#$%&'()*+,-./:;<=>?@[]^_`{|}~ and space, a-z, A-Z, 0-9
156
+ // Allowed attribute value characters
140
157
141
- if ( $2 . match ( / ^ [ \w \! # \$ % & ' \( \) \* \+ , \- \. \/ \: ; < \= > \? @ \[ \] \^ ` \{ \| \} ~ ] + $ / ) === null ) {
158
+ if ( $2 . match ( internals . attributeRegex ) === null ) {
142
159
errorMessage = 'Bad attribute value: ' + $1 ;
143
160
return ;
144
161
}
0 commit comments