Skip to content
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

Issue #7250 - Correct HostHeaderCustomizer logic and add new RejectMissingAuthorityCustomizer #7292

Closed

Conversation

joakime
Copy link
Contributor

@joakime joakime commented Dec 15, 2021

This is a minimal version of the draft PR #7251

  • Fixes HostHeaderCustomizer to only do Host
    header on non-HTTP/1.1
  • Introduces RejectMissingAuthorityCustomizer
  • Adds modules + xml for both

Signed-off-by: Joakim Erdfelt [email protected]

+ Fixes HostHeaderCustomizer to only do Host
  header on non-HTTP/1.1
+ Introduces RejectMissingAuthorityCustomizer
+ Adds modules + xml for both

Signed-off-by: Joakim Erdfelt <[email protected]>
@joakime joakime self-assigned this Dec 15, 2021
@joakime joakime requested a review from gregw December 15, 2021 21:58
@joakime joakime added the Bug For general bugs on Jetty side label Dec 15, 2021
@joakime joakime changed the title Issue #7250 - New RejectMissingAuthorityCustomizer Issue #7250 - Correct HostHeaderCustomizer logic and add new RejectMissingAuthorityCustomizer Dec 16, 2021
@joakime joakime added Specification For all industry Specifications (IETF / Servlet / etc) Sponsored This issue affects a user with a commercial support agreement labels Dec 16, 2021
Copy link
Contributor

@gregw gregw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a couple of optimizations

Comment on lines +69 to +83
String hostHeaderValue = request.getHeader("Host");

// No Host Header
if (request.getHttpVersion().getVersion() != HttpVersion.HTTP_1_1.getVersion() &&
hostHeaderValue == null)
{
if (request.getHttpURI().isAbsolute())
{
request.getHttpFields().put(HttpHeader.HOST, request.getHttpURI().getAuthority());
}
else
{
request.getHttpFields().put(HttpHeader.HOST, hostValue);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since getHeader is a linear lookup, it is best to avoid doing it if possible, and then to use efficient non string lookup:

Suggested change
String hostHeaderValue = request.getHeader("Host");
// No Host Header
if (request.getHttpVersion().getVersion() != HttpVersion.HTTP_1_1.getVersion() &&
hostHeaderValue == null)
{
if (request.getHttpURI().isAbsolute())
{
request.getHttpFields().put(HttpHeader.HOST, request.getHttpURI().getAuthority());
}
else
{
request.getHttpFields().put(HttpHeader.HOST, hostValue);
}
}
// If not 1.1 and no Host Header
if (request.getHttpVersion().getVersion() != HttpVersion.HTTP_1_1.getVersion() &&
!request.getHttpFields().contains(HttpHeader.HOST))
{
if (request.getHttpURI().isAbsolute())
{
request.getHttpFields().put(HttpHeader.HOST, request.getHttpURI().getAuthority());
}
else
{
request.getHttpFields().put(HttpHeader.HOST, hostValue);
}
}

Comment on lines +65 to +80
if (host != null)
{
if (!(host instanceof HostPortHttpField) && StringUtil.isNotBlank(host.getValue()))
{
return true;
}

if (host instanceof HostPortHttpField)
{
HostPortHttpField authority = (HostPortHttpField)host;
metadata.getURI().setAuthority(authority.getHost(), authority.getPort());
if (StringUtil.isNotBlank(authority.getHost()))
return true;
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this way reduces the number of tests in the hot path:

Suggested change
if (host != null)
{
if (!(host instanceof HostPortHttpField) && StringUtil.isNotBlank(host.getValue()))
{
return true;
}
if (host instanceof HostPortHttpField)
{
HostPortHttpField authority = (HostPortHttpField)host;
metadata.getURI().setAuthority(authority.getHost(), authority.getPort());
if (StringUtil.isNotBlank(authority.getHost()))
return true;
}
}
}
if (host != null)
if (host instanceof HostPortHttpField)
{
HostPortHttpField authority = (HostPortHttpField)host;
metadata.getURI().setAuthority(authority.getHost(), authority.getPort());
if (StringUtil.isNotBlank(authority.getHost()))
return true;
}
else if (host != null && StringUtil.isNotBlank(host.getValue()))
{
return true;
}
}

@joakime joakime marked this pull request as draft January 13, 2022 14:03
@joakime
Copy link
Contributor Author

joakime commented Mar 1, 2022

@gregw would this be good for jetty-12.0.x-hackathon branch instead?

@joakime
Copy link
Contributor Author

joakime commented Jun 28, 2022

@gregw bump do you want to see this for jetty-12.0.x ?

@gregw
Copy link
Contributor

gregw commented Jun 28, 2022

Note keen on any changes to behaviour in jetty-9. Does the sponsor still require this?

@joakime
Copy link
Contributor Author

joakime commented Jun 28, 2022

Nope.
The other recent changes for overriding the authority at the connector level were sufficient.

@joakime joakime closed this Jun 28, 2022
@joakime joakime deleted the jetty-9.4.x-reject-no-authority-customizer branch June 29, 2022 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug For general bugs on Jetty side Specification For all industry Specifications (IETF / Servlet / etc) Sponsored This issue affects a user with a commercial support agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HostHeaderCustomizer doesn't address empty Host header value with no authority set
2 participants