@@ -17,6 +17,11 @@ exports.version = function () {
1717} ;
1818
1919
20+ exports . limits = {
21+ maxMatchLength : 4096 // Limit the length of uris and headers to avoid a DoS attack on string matching
22+ } ;
23+
24+
2025// Extract host and port from request
2126
2227// $1 $2
@@ -31,6 +36,10 @@ exports.parseHost = function (req, hostHeaderName) {
3136 return null ;
3237 }
3338
39+ if ( hostHeader . length > exports . limits . maxMatchLength ) {
40+ return null ;
41+ }
42+
3443 const hostParts = hostHeader . match ( internals . hostHeaderRegex ) ;
3544 if ( ! hostParts ) {
3645 return null ;
@@ -100,6 +109,10 @@ exports.nowSecs = function (localtimeOffsetMsec) {
100109} ;
101110
102111
112+ internals . authHeaderRegex = / ^ ( \w + ) (?: \s + ( .* ) ) ? $ / ; // Header: scheme[ something]
113+ internals . attributeRegex = / ^ [ \w \! # \$ % & ' \( \) \* \+ , \- \. \/ \: ; < \= > \? @ \[ \] \^ ` \{ \| \} ~ ] + $ / ; // !#$%&'()*+,-./:;<=>?@[]^_`{|}~ and space, a-z, A-Z, 0-9
114+
115+
103116// Parse Hawk HTTP Authorization header
104117
105118exports . parseAuthorizationHeader = function ( header , keys ) {
@@ -110,7 +123,11 @@ exports.parseAuthorizationHeader = function (header, keys) {
110123 return Boom . unauthorized ( null , 'Hawk' ) ;
111124 }
112125
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 ) ;
114131 if ( ! headerParts ) {
115132 return Boom . badRequest ( 'Invalid header syntax' ) ;
116133 }
@@ -136,9 +153,9 @@ exports.parseAuthorizationHeader = function (header, keys) {
136153 return ;
137154 }
138155
139- // Allowed attribute value characters: !#$%&'()*+,-./:;<=>?@[]^_`{|}~ and space, a-z, A-Z, 0-9
156+ // Allowed attribute value characters
140157
141- if ( $2 . match ( / ^ [ \w \! # \$ % & ' \( \) \* \+ , \- \. \/ \: ; < \= > \? @ \[ \] \^ ` \{ \| \} ~ ] + $ / ) === null ) {
158+ if ( $2 . match ( internals . attributeRegex ) === null ) {
142159 errorMessage = 'Bad attribute value: ' + $1 ;
143160 return ;
144161 }
0 commit comments