@@ -109,7 +109,7 @@ private function assertHeaderName($name): void
109109 digit => 0-9
110110 others => !#$%&\'*+-.^_`|~
111111 */
112- if (\preg_match ('/^[a-zA-Z0-9!#$%& \'*+-.^_`|~]+$/ ' , $ name ) !== 1 ) {
112+ if (\preg_match ('/^[a-zA-Z0-9!#$%& \'*+-.^_`|~]+$/D ' , $ name ) !== 1 ) {
113113 throw new InvalidArgumentException (\sprintf (
114114 '"%s" is not valid header name, it must be an RFC 7230 compatible string. ' ,
115115 $ name
@@ -151,10 +151,21 @@ private function assertHeaderValue($value): void
151151 /**
152152 * Validate header value
153153 *
154+ * headers values should be ISO-8859-1 encoded by default
155+ *
156+ * field-value = *( field-content / obs-fold )
157+ * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
158+ * field-vchar = VCHAR / obs-text
159+ * VCHAR = %x21-7E
160+ * obs-text = %x80-FF
161+ * obs-fold = CRLF 1*( SP / HTAB )
162+
154163 * @param mixed $value Header value to test
155164 *
156165 * @return void
157166 *
167+ * @see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2
168+ *
158169 * @throws InvalidArgumentException
159170 *
160171 * @psalm-assert string $value
@@ -165,20 +176,21 @@ private function assertHeaderValueLine($value): void
165176 return ;
166177 }
167178 $ this ->assertString ($ value , 'Header value ' , true );
168- /*
169- https://www.rfc-editor.org/rfc/rfc7230.txt (page.25)
170-
171- field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
172- field-vchar = VCHAR / obs-text
173- obs-text = %x80-FF (character range outside ASCII.)
174- NOT ALLOWED
175- SP = space
176- HTAB = horizontal tab
177- VCHAR = any visible [USASCII] character. (x21-x7e)
178- */
179- if (\preg_match ('/^[ \t\x21-\x7e]+$/ ' , $ value ) !== 1 ) {
179+ $ value = \trim ((string ) $ value , " \t" );
180+ // The regular expression intentionally does not support the obs-fold production, because as
181+ // per RFC 7230#3.2.4:
182+ //
183+ // A sender MUST NOT generate a message that includes
184+ // line folding (i.e., that has any field-value that contains a match to
185+ // the obs-fold rule) unless the message is intended for packaging
186+ // within the message/http media type.
187+ //
188+ // Clients must not send a request with line folding and a server sending folded headers is
189+ // likely very rare. Line folding is a fairly obscure feature of HTTP/1.1 and thus not accepting
190+ // folding is not likely to break any legitimate use case.
191+ if (\preg_match ('/^[\x20\x09\x21-\x7E\x80-\xFF]*$/D ' , $ value ) !== 1 ) {
180192 throw new InvalidArgumentException (\sprintf (
181- '"%s" is not valid header value, it must contains visible ASCII characters only . ' ,
193+ '"%s" is not valid header value. ' ,
182194 $ value
183195 ));
184196 }
0 commit comments