@@ -161,6 +161,129 @@ public void execute() throws Throwable { |
161 | 161 | }); |
162 | 162 | } |
163 | 163 |
|
| 164 | + @Test |
| 165 | + public void rejectNonAsciiHeaderNameAsciiString() { |
| 166 | + // U+1F631 ("😱") encoded as F0 9F 98 B1 — bytes outside the token grammar that previously |
| 167 | + // slipped past the upper-case-only validator. See issue #11975. |
| 168 | + final byte[] buf = {(byte) 0xF0, (byte) 0x9F, (byte) 0x98, (byte) 0xB1}; |
| 169 | + final Http2Headers headers = new DefaultHttp2Headers(); |
| 170 | + assertThrows(Http2Exception.class, new Executable() { |
| 171 | + @Override |
| 172 | + public void execute() throws Throwable { |
| 173 | + headers.add(new AsciiString(buf), of("test")); |
| 174 | + } |
| 175 | + }); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void rejectNonAsciiHeaderNameCharSequence() { |
| 180 | + final Http2Headers headers = new DefaultHttp2Headers(); |
| 181 | + assertThrows(Http2Exception.class, new Executable() { |
| 182 | + @Override |
| 183 | + public void execute() throws Throwable { |
| 184 | + // Non-ASCII char via the CharSequence path. |
| 185 | + headers.add("naÿme", "test"); |
| 186 | + } |
| 187 | + }); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void rejectHighBitHeaderNameCharSequence() { |
| 192 | + final Http2Headers headers = new DefaultHttp2Headers(); |
| 193 | + assertThrows(Http2Exception.class, new Executable() { |
| 194 | + @Override |
| 195 | + public void execute() throws Throwable { |
| 196 | + // U+0100 — above 0xFF, must be rejected by the CharSequence path. |
| 197 | + headers.add("naĀme", "test"); |
| 198 | + } |
| 199 | + }); |
| 200 | + } |
| 201 | + |
| 202 | + @ParameterizedTest(name = "{displayName} [{index}] byte=0x{0}") |
| 203 | + @MethodSource("nonTokenBytesInHeaderName") |
| 204 | + void rejectNonTokenCharactersInHeaderName(int illegalByte) { |
| 205 | + final String name = "n" + (char) illegalByte + "ame"; |
| 206 | + final Http2Headers headers = new DefaultHttp2Headers(); |
| 207 | + assertThrows(Http2Exception.class, new Executable() { |
| 208 | + @Override |
| 209 | + public void execute() throws Throwable { |
| 210 | + headers.add(name, "test"); |
| 211 | + } |
| 212 | + }); |
| 213 | + } |
| 214 | + |
| 215 | + static Stream<Arguments> nonTokenBytesInHeaderName() { |
| 216 | + return Stream.of( |
| 217 | + // Control characters: every byte in the C0 range that the previous validator silently let through. |
| 218 | + Arguments.of(0x00), // NUL |
| 219 | + Arguments.of(0x01), // SOH |
| 220 | + Arguments.of(0x07), // BEL |
| 221 | + Arguments.of(0x08), // BS |
| 222 | + Arguments.of(0x09), // HTAB |
| 223 | + Arguments.of(0x0A), // LF |
| 224 | + Arguments.of(0x0B), // VT |
| 225 | + Arguments.of(0x0C), // FF |
| 226 | + Arguments.of(0x0D), // CR |
| 227 | + Arguments.of(0x1F), // US |
| 228 | + Arguments.of(0x7F), // DEL |
| 229 | + // Whitespace and RFC 7230 separators that are not valid token characters. |
| 230 | + Arguments.of(0x20), // SP |
| 231 | + Arguments.of((int) ','), |
| 232 | + Arguments.of((int) ';'), |
| 233 | + Arguments.of((int) ':'), |
| 234 | + Arguments.of((int) '/'), |
| 235 | + Arguments.of((int) '='), |
| 236 | + Arguments.of((int) '?'), |
| 237 | + Arguments.of((int) '@'), |
| 238 | + Arguments.of((int) '('), |
| 239 | + Arguments.of((int) ')'), |
| 240 | + Arguments.of((int) '['), |
| 241 | + Arguments.of((int) ']'), |
| 242 | + Arguments.of((int) '{'), |
| 243 | + Arguments.of((int) '}'), |
| 244 | + Arguments.of((int) '<'), |
| 245 | + Arguments.of((int) '>'), |
| 246 | + Arguments.of((int) '\\'), |
| 247 | + Arguments.of((int) '"') |
| 248 | + ); |
| 249 | + } |
| 250 | + |
| 251 | + @Test |
| 252 | + public void uppercaseHeaderNameStillRejected() { |
| 253 | + // Regression: pre-existing rejection of upper-case header names must continue to fire. |
| 254 | + final Http2Headers headers = new DefaultHttp2Headers(); |
| 255 | + assertThrows(Http2Exception.class, new Executable() { |
| 256 | + @Override |
| 257 | + public void execute() throws Throwable { |
| 258 | + headers.add(of("X-Custom"), of("test")); |
| 259 | + } |
| 260 | + }); |
| 261 | + } |
| 262 | + |
| 263 | + @Test |
| 264 | + public void acceptValidLowercaseTokenHeaderName() { |
| 265 | + // Regression: valid RFC 7230 token (lower-case ALPHA, DIGIT, "!#$%&'*+-.^_`|~") must be accepted. |
| 266 | + Http2Headers headers = new DefaultHttp2Headers(); |
| 267 | + headers.add(of("x-custom-header"), of("v")); |
| 268 | + headers.add(of("2name"), of("v")); |
| 269 | + headers.add(of("a!#$%&'*+-.^_`|~b"), of("v")); |
| 270 | + assertTrue(headers.contains("x-custom-header")); |
| 271 | + assertTrue(headers.contains("2name")); |
| 272 | + assertTrue(headers.contains("a!#$%&'*+-.^_`|~b")); |
| 273 | + } |
| 274 | + |
| 275 | + @Test |
| 276 | + public void acceptPseudoHeaderName() { |
| 277 | + // Regression: leading-colon pseudo-header names must remain accepted even though ":" is not a token char. |
| 278 | + Http2Headers headers = new DefaultHttp2Headers(); |
| 279 | + headers.method(of("GET")); |
| 280 | + headers.path(of("/")); |
| 281 | + headers.scheme(of("https")); |
| 282 | + headers.authority(of("example.com")); |
| 283 | + assertEquals(of("GET"), headers.method()); |
| 284 | + assertEquals(of("/"), headers.path()); |
| 285 | + } |
| 286 | + |
164 | 287 | @Test |
165 | 288 | public void testClearResetsPseudoHeaderDivision() { |
166 | 289 | DefaultHttp2Headers http2Headers = new DefaultHttp2Headers(); |
|