|
17 | 17 | import io.netty.buffer.ByteBuf; |
18 | 18 | import io.netty.channel.ChannelHandlerContext; |
19 | 19 | import io.netty.handler.codec.http.HttpHeaderNames; |
| 20 | +import io.netty.handler.codec.http.HttpMethod; |
20 | 21 | import io.netty.handler.codec.http.HttpStatusClass; |
21 | 22 | import io.netty.handler.codec.http.HttpUtil; |
22 | 23 | import io.netty.handler.codec.http2.Http2Connection.Endpoint; |
| 24 | +import io.netty.handler.codec.http2.Http2Headers.PseudoHeaderName; |
23 | 25 | import io.netty.util.internal.logging.InternalLogger; |
24 | 26 | import io.netty.util.internal.logging.InternalLoggerFactory; |
25 | 27 |
|
@@ -63,6 +65,7 @@ public class DefaultHttp2ConnectionDecoder implements Http2ConnectionDecoder { |
63 | 65 | private final boolean autoAckPing; |
64 | 66 | private final Http2Connection.PropertyKey contentLengthKey; |
65 | 67 | private final boolean validateHeaders; |
| 68 | + private final boolean validateRequiredPseudoHeaders; |
66 | 69 |
|
67 | 70 | public DefaultHttp2ConnectionDecoder(Http2Connection connection, |
68 | 71 | Http2ConnectionEncoder encoder, |
@@ -129,7 +132,39 @@ public DefaultHttp2ConnectionDecoder(Http2Connection connection, |
129 | 132 | boolean autoAckSettings, |
130 | 133 | boolean autoAckPing, |
131 | 134 | boolean validateHeaders) { |
| 135 | + this(connection, encoder, frameReader, requestVerifier, autoAckSettings, autoAckPing, validateHeaders, false); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Create a new instance. |
| 140 | + * @param connection The {@link Http2Connection} associated with this decoder. |
| 141 | + * @param encoder The {@link Http2ConnectionEncoder} associated with this decoder. |
| 142 | + * @param frameReader Responsible for reading/parsing the raw frames. As opposed to this object which applies |
| 143 | + * h2 semantics on top of the frames. |
| 144 | + * @param requestVerifier Determines if push promised streams are valid. |
| 145 | + * @param autoAckSettings {@code false} to disable automatically applying and sending settings acknowledge frame. |
| 146 | + * The {@code Http2ConnectionEncoder} is expected to be an instance of |
| 147 | + * {@link Http2SettingsReceivedConsumer} and will apply the earliest received but not yet |
| 148 | + * ACKed SETTINGS when writing the SETTINGS ACKs. {@code true} to enable automatically |
| 149 | + * applying and sending settings acknowledge frame. |
| 150 | + * @param autoAckPing {@code false} to disable automatically sending ping acknowledge frame. {@code true} to enable |
| 151 | + * automatically sending ping ack frame. |
| 152 | + * @param validateHeaders {@code true} to validate headers according to |
| 153 | + * <a href="https://tools.ietf.org/html/rfc7540#section-8.1.2.6">RFC 7540, 8.1.2.6</a>. |
| 154 | + * @param validateRequiredPseudoHeaders {@code true} to reject request/response HEADERS that omit a mandatory |
| 155 | + * pseudo-header field, according to |
| 156 | + * <a href="https://www.rfc-editor.org/rfc/rfc9113.html#section-8.3">RFC 9113, 8.3</a>. |
| 157 | + */ |
| 158 | + public DefaultHttp2ConnectionDecoder(Http2Connection connection, |
| 159 | + Http2ConnectionEncoder encoder, |
| 160 | + Http2FrameReader frameReader, |
| 161 | + Http2PromisedRequestVerifier requestVerifier, |
| 162 | + boolean autoAckSettings, |
| 163 | + boolean autoAckPing, |
| 164 | + boolean validateHeaders, |
| 165 | + boolean validateRequiredPseudoHeaders) { |
132 | 166 | this.validateHeaders = validateHeaders; |
| 167 | + this.validateRequiredPseudoHeaders = validateRequiredPseudoHeaders; |
133 | 168 | this.autoAckPing = autoAckPing; |
134 | 169 | if (autoAckSettings) { |
135 | 170 | settingsReceivedConsumer = null; |
@@ -244,6 +279,48 @@ private void verifyContentLength(Http2Stream stream, int data, boolean isEnd) th |
244 | 279 | } |
245 | 280 | } |
246 | 281 |
|
| 282 | + /** |
| 283 | + * Validates that an initial request or response HEADERS frame carries the mandatory pseudo-header fields, |
| 284 | + * as required by <a href="https://www.rfc-editor.org/rfc/rfc9113.html#section-8.3">RFC 9113, 8.3</a>. |
| 285 | + * Trailers and informational (1xx) responses are handled by the caller and do not reach this method. |
| 286 | + */ |
| 287 | + private static void validateRequiredPseudoHeaders(boolean server, int streamId, Http2Headers headers) |
| 288 | + throws Http2Exception { |
| 289 | + if (server) { |
| 290 | + // Request pseudo-header fields (RFC 9113, 8.3.1). |
| 291 | + CharSequence method = headers.method(); |
| 292 | + if (method == null) { |
| 293 | + throw streamError(streamId, PROTOCOL_ERROR, |
| 294 | + "Request is missing mandatory :method pseudo-header field."); |
| 295 | + } |
| 296 | + // CONNECT (RFC 9113, 8.5) omits :scheme/:path and carries :authority; extended CONNECT (RFC 8441), |
| 297 | + // identified by :protocol, follows the regular :scheme/:path rules. |
| 298 | + if (HttpMethod.CONNECT.asciiName().contentEquals(method) && |
| 299 | + !headers.contains(PseudoHeaderName.PROTOCOL.value())) { |
| 300 | + if (headers.authority() == null) { |
| 301 | + throw streamError(streamId, PROTOCOL_ERROR, |
| 302 | + "CONNECT request is missing mandatory :authority pseudo-header field."); |
| 303 | + } |
| 304 | + } else { |
| 305 | + if (headers.scheme() == null) { |
| 306 | + throw streamError(streamId, PROTOCOL_ERROR, |
| 307 | + "Request is missing mandatory :scheme pseudo-header field."); |
| 308 | + } |
| 309 | + CharSequence path = headers.path(); |
| 310 | + if (path == null || path.length() == 0) { |
| 311 | + throw streamError(streamId, PROTOCOL_ERROR, |
| 312 | + "Request is missing mandatory :path pseudo-header field."); |
| 313 | + } |
| 314 | + } |
| 315 | + } else { |
| 316 | + // Response pseudo-header fields (RFC 9113, 8.3.2). |
| 317 | + if (headers.status() == null) { |
| 318 | + throw streamError(streamId, PROTOCOL_ERROR, |
| 319 | + "Response is missing mandatory :status pseudo-header field."); |
| 320 | + } |
| 321 | + } |
| 322 | + } |
| 323 | + |
247 | 324 | /** |
248 | 325 | * Handles all inbound frames from the network. |
249 | 326 | */ |
@@ -388,6 +465,10 @@ public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers |
388 | 465 | } |
389 | 466 |
|
390 | 467 | if (!isTrailers) { |
| 468 | + if (validateRequiredPseudoHeaders && !isInformational) { |
| 469 | + // Reject initial request/response HEADERS that omit a mandatory pseudo-header (RFC 9113, 8.3). |
| 470 | + validateRequiredPseudoHeaders(connection.isServer(), stream.id(), headers); |
| 471 | + } |
391 | 472 | // extract the content-length header |
392 | 473 | List<? extends CharSequence> contentLength = headers.getAll(HttpHeaderNames.CONTENT_LENGTH); |
393 | 474 | if (contentLength != null && !contentLength.isEmpty()) { |
|