-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refuse adding invalid HTTP 2.0 headers #277
Open
arkanovicz
wants to merge
5
commits into
apache:main
Choose a base branch
from
arkanovicz:filter-invalid-http2-headers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4f3bf8b
Filter invalid HTTP 2.0 headers from response
arkanovicz 8fb2704
Code cleaning
arkanovicz e417594
Always throw an IllegalArgumentException for invalid HTTP/2.0 headers
arkanovicz 7c8d0eb
Take into account michael-o requested changes
arkanovicz 45371de
Fix comment
arkanovicz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
|
||
import jakarta.servlet.WriteListener; | ||
|
||
import org.apache.coyote.http2.Http2OutputBuffer; | ||
import org.apache.juli.logging.Log; | ||
import org.apache.juli.logging.LogFactory; | ||
import org.apache.tomcat.util.buf.B2CConverter; | ||
|
@@ -61,6 +62,11 @@ public final class Response { | |
*/ | ||
private static final Locale DEFAULT_LOCALE = Locale.getDefault(); | ||
|
||
/** | ||
* Helper to log the invalid HTTP/2.0 header error only once per instance | ||
*/ | ||
private static AtomicBoolean invalidHeaderWarningEmitted = new AtomicBoolean(false); | ||
|
||
|
||
// ----------------------------------------------------- Instance Variables | ||
|
||
|
@@ -435,6 +441,37 @@ private boolean checkSpecialHeader( String name, String value) { | |
return false; | ||
} | ||
} | ||
if (outputBuffer instanceof Http2OutputBuffer && name.equalsIgnoreCase("Connection") ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
/* | ||
Connection headers are invalid in HTTP/2.0, and some clients (like Safari or curl) | ||
are very touchy about it. Most probably, an application component has added the | ||
typical HTTP/1.x "Connection: keep-alive" header, but despide the component's | ||
good intention, the header is faulty in HTTP/2.0 and *should* always be filtered. | ||
|
||
The current implementation emits a warning in the logs only once per instance. | ||
We could want to add an HTTP/2.0 option to rather always send/log an exception. | ||
|
||
@see https://tools.ietf.org/html/rfc7540#section-8.1.2.2 | ||
*/ | ||
|
||
if (log.isWarnEnabled()) | ||
{ | ||
/* log a warning only once per instance *with the stacktrace* */ | ||
if (!invalidHeaderWarningEmitted.getAndSet(true)) | ||
{ | ||
try | ||
{ | ||
throw new IllegalArgumentException(sm.getString("response.ignoringInvalidHeader", "Connection", value)); | ||
} | ||
catch (IllegalArgumentException iae) | ||
{ | ||
log.warn(iae); | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.coyote.http2; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServlet; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import org.apache.catalina.Context; | ||
import org.apache.catalina.startup.Tomcat; | ||
|
||
public class TestInvalidHeader extends Http2TestBase { | ||
|
||
/* | ||
* @see org.apache.coyote.Response#checkSpecialHeaders() | ||
*/ | ||
protected static class FaultyServlet extends SimpleServlet | ||
{ | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
public static final int CONTENT_LENGTH = 8192; | ||
|
||
@Override | ||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) | ||
throws ServletException, IOException | ||
{ | ||
// Add faulty header | ||
resp.addHeader("Connection", "keep-alive"); | ||
super.doGet(req, resp); | ||
} | ||
} | ||
|
||
|
||
@Test | ||
public void testInvalidHeader() throws Exception { | ||
|
||
enableHttp2(); | ||
|
||
Tomcat tomcat = getTomcatInstance(); | ||
|
||
Context ctxt = tomcat.addContext("", null); | ||
Tomcat.addServlet(ctxt, "simple", new FaultyServlet()); | ||
ctxt.addServletMappingDecoded("/simple", "simple"); | ||
|
||
tomcat.start(); | ||
|
||
openClientConnection(); | ||
doHttpUpgrade(); | ||
sendClientPreface(); | ||
validateHttp2InitialResponse(); | ||
|
||
byte[] frameHeader = new byte[9]; | ||
ByteBuffer headersPayload = ByteBuffer.allocate(128); | ||
buildGetRequest(frameHeader, headersPayload, null, 3, "/simple"); | ||
|
||
writeFrame(frameHeader, headersPayload); | ||
|
||
readSimpleGetResponse(); | ||
|
||
Assert.assertEquals(getSimpleResponseTrace(3), output.getTrace()); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
per instance
of what ? Because you usestatic
here, so it is on class loader level.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@martin-g It's a langage abuse. Per instance of running tomcat, sorry. Anyhow, @rmaucher suggestion to just throw the exception is simpler, so the AtomicBoolean is deprecated.