RE: allowHostHeaderMismatch option only works if the Host Header has an http or https prefix

2022-05-27 Thread Ralph Atallah
Hi Chris,

I suspect that if we were to take the time to set up a proxy, according to 
RFC7230, we would be able to get the absolute-form to reach the Tomcat code and 
in that case, based on reading the AbostractHttp11Processor class, I suspect 
the allowHostHeaderMismatch will kick in and will behave correctly.  So I doubt 
that there is a bug to report at this stage, at least not from my observation.

However, I wonder what all of this means.  Could it be that the Host header 
injection or Host header attack issue can only occur when absolute-form is 
used, i.e. mostly when proxies are set up?  Both you and Mark stated that with 
origin-form there is nothing to compare the Host header to, which makes sense.  
Any thoughts on this assessment?

Thanks,
Ralph

-Original Message-
From: Christopher Schultz  
Sent: Friday, May 27, 2022 4:26 PM
To: users@tomcat.apache.org
Subject: Re: allowHostHeaderMismatch option only works if the Host Header has 
an http or https prefix

WARNING: This email originated from outside of CallMiner. Do not click any 
links or open any attachments unless you recognize the sender and know that the 
content is safe. Please report suspicious emails to: 
reportsuspiciousema...@callminer.com 


Mark,

On 5/27/22 3:13 AM, Mark Thomas wrote:
> On 27/05/2022 02:00, Ralph Atallah wrote:
>> Hi Mark,
>>
>> Thanks again for the prompt response.
>>
>> You wrote below:  "If the original request only has a Host header, 
>> then allowHostHeaderMismatch="false" isn't going to do anything 
>> because there is no mismatch.".  I am not clear on what this means.
>> What should the match be between?  I thought the comparison for the 
>> match was between the URL's hostname, i.e. "example.com" in the 
>> http://example.com/myapp URL, and the Host header value which is 
>> "attacker.com".  If that understanding is incorrect, please point me 
>> in the right direction of what it should be.
>
> The check is that the host in the request URI (if present) is 
> consistent with the Host header. Nothing more, nothing less.
>
> HTTP requests may or may not include the host in the request URI.
>
> The host named in the the headers of an HTTP request is completely 
> independent of the host name used to establish the connection to the 
> web server.
>
>> The AbstractHttp11Processor class does not get to the 
>> allowHostHeaderMismatch detection code because the uriBC (URI
>> ByteChunk) that it reads is expecting an absolute URL 
>> (http://example.com/myapp), but instead, it is getting a relative one 
>> /myapp.  The reason I say the code expects an absolute URL is because 
>> it checks for and "http" string at the beginning.  This makes me 
>> wonder whether there is a setting that controls that URI format, 
>> absolute or relative.
>
> Your understanding of the HTTP protocol is flawed. You may wish to 
> read RFC 7230. Specifically:
>
> https://datatracker.ietf.org/doc/html/rfc7230#section-3.1.1
> and
> https://datatracker.ietf.org/doc/html/rfc7230#section-5.3
>
> Requests with the URI in origin-form do not include a host in the URI.
>
> The purpose of allowHostHeaderMismatch is to ensure that when the 
> request URI is in absolute-form that the request URI is consistent 
> with the Host header.
>
>> Regarding the addition of a filter that you propose, we have an 
>> existing one in our application, but by the time it is reached, the 
>> URL that we see is already http://attacker.com/myapp, i.e. already 
>> "redirected".
>
> There has been no redirect. The URI reported is a combination of the 
> Host header and request URI received.
>
>>  Technically we could check there against a whitelist, but this would 
>> make the solution less out-of-the box, and more needy of user 
>> configuration in our app.  We prefer an out-of-the-box secure solution.
>>
>> Any thoughts on the above?
>
> What you want isn't possible.

Actually, I think what Ralph is requesting is exactly what Tomcat is providing 
in the form of allowHostHeaderMismatch (when set to false).

The only problem is that Ralph is saying it's not working because the URI in 
the request doesn't contain a hostname *at all* (because it's optional). So 
there is nothing to check. Browsers don't bother to send the optional 
protocol/hostname/port/etc and instead send the relative URI -- relative to the 
Host header (no coincidentally).

If you (Ralph) can reproduce this with wc or telnet where you can force the URI 
to be absolute *and* provide a conflicting Host header, then I think you have 
grounds for a bug report.

> If you want requests to be rejected unless the Host header is on a 
> user defined allow list (presumably the set of DNS names defined for 
> the host), then you are going to have to provide a means for the user 
> to provide that configuration.

I don't think it being requested, here. It may be an aim of their application 
to do such things, but that wasn't a part of the initial request 

RE: allowHostHeaderMismatch option only works if the Host Header has an http or https prefix

2022-05-27 Thread Ralph Atallah
Hi Mark,

Thank you for your help.  It took some digging to fully understand the nuances 
in your answers below.  Here are some pointers to anyone who experiences the 
same issue in the future and to whom these pointers might be helpful.

1. Although I had previously visited the link to the RFC7230 page 
https://datatracker.ietf.org/doc/html/rfc7230#section-5.3 re-reading more 
closely and with Mark's emphasis on it highlighted the fact that most of the 
time, the request line will be of the origin-form, while the absolute-form will 
be mainly observed when proxies are used.  This was a very important 
explanation of why we never saw the absolute-form reach the 
AbstractHttp11Processor code in our test environment.

2. "The approach requiring the minimal input from the app and where the 
container does most of the work is the one where you define a Host element in 
server.xml with the name and optional aliases for the host names that are 
acceptable and configure the default host (that handles all requests to other 
hosts) to reject all other requests."   This statement was key to the solution:

Our server.xml looked like this:



  
 ...

We simply had to change the defaultHost value to something else than 
"localhost", i.e. a value that will be rejected  (e.g. "defaulthost").   The 
Host's name as well as any Aliases defined within that tag would be the only 
hosts accepted, whether in the URL request or in the Host header request.   The 
rejection would respond with a 404 Not Found error.

Thanks,
Ralph

-Original Message-
From: Mark Thomas  
Sent: Friday, May 27, 2022 3:13 AM
To: users@tomcat.apache.org
Subject: Re: allowHostHeaderMismatch option only works if the Host Header has 
an http or https prefix

WARNING: This email originated from outside of CallMiner. Do not click any 
links or open any attachments unless you recognize the sender and know that the 
content is safe. Please report suspicious emails to: 
reportsuspiciousema...@callminer.com 


On 27/05/2022 02:00, Ralph Atallah wrote:
> Hi Mark,
>
> Thanks again for the prompt response.
>
> You wrote below:  "If the original request only has a Host header, then 
> allowHostHeaderMismatch="false" isn't going to do anything because there is 
> no mismatch.".  I am not clear on what this means.  What should the match be 
> between?  I thought the comparison for the match was between the URL's 
> hostname, i.e. "example.com" in the http://example.com/myapp URL, and the 
> Host header value which is "attacker.com".  If that understanding is 
> incorrect, please point me in the right direction of what it should be.

The check is that the host in the request URI (if present) is consistent with 
the Host header. Nothing more, nothing less.

HTTP requests may or may not include the host in the request URI.

The host named in the the headers of an HTTP request is completely independent 
of the host name used to establish the connection to the web server.

> The AbstractHttp11Processor class does not get to the allowHostHeaderMismatch 
> detection code because the uriBC (URI ByteChunk) that it reads is expecting 
> an absolute URL (http://example.com/myapp), but instead, it is getting a 
> relative one /myapp.  The reason I say the code expects an absolute URL is 
> because it checks for and "http" string at the beginning.  This makes me 
> wonder whether there is a setting that controls that URI format, absolute or 
> relative.

Your understanding of the HTTP protocol is flawed. You may wish to read RFC 
7230. Specifically:

https://datatracker.ietf.org/doc/html/rfc7230#section-3.1.1
and
https://datatracker.ietf.org/doc/html/rfc7230#section-5.3

Requests with the URI in origin-form do not include a host in the URI.

The purpose of allowHostHeaderMismatch is to ensure that when the request URI 
is in absolute-form that the request URI is consistent with the Host header.

> Regarding the addition of a filter that you propose, we have an existing one 
> in our application, but by the time it is reached, the URL that we see is 
> already http://attacker.com/myapp, i.e. already "redirected".

There has been no redirect. The URI reported is a combination of the Host 
header and request URI received.

>  Technically we could check there against a whitelist, but this would make 
> the solution less out-of-the box, and more needy of user configuration in our 
> app.  We prefer an out-of-the-box secure solution.
>
> Any thoughts on the above?

What you want isn't possible. If you want requests to be rejected unless the 
Host header is on a user defined allow list (presumably the set of DNS names 
defined for the host), then you are going to have to provide a means for the 
user to provide that configuration.

The approach requiring the minimal input from the app and where the container 
does most of the work is the one where you define a Host element in server.xml 
with the name and optional aliases for 

Re: allowHostHeaderMismatch option only works if the Host Header has an http or https prefix

2022-05-27 Thread Christopher Schultz

Mark,

On 5/27/22 3:13 AM, Mark Thomas wrote:

On 27/05/2022 02:00, Ralph Atallah wrote:

Hi Mark,

Thanks again for the prompt response.

You wrote below:  "If the original request only has a Host header, 
then allowHostHeaderMismatch="false" isn't going to do anything 
because there is no mismatch.".  I am not clear on what this means.  
What should the match be between?  I thought the comparison for the 
match was between the URL's hostname, i.e. "example.com" in the 
http://example.com/myapp URL, and the Host header value which is 
"attacker.com".  If that understanding is incorrect, please point me 
in the right direction of what it should be.


The check is that the host in the request URI (if present) is consistent 
with the Host header. Nothing more, nothing less.


HTTP requests may or may not include the host in the request URI.

The host named in the the headers of an HTTP request is completely 
independent of the host name used to establish the connection to the web 
server.


The AbstractHttp11Processor class does not get to the 
allowHostHeaderMismatch detection code because the uriBC (URI 
ByteChunk) that it reads is expecting an absolute URL 
(http://example.com/myapp), but instead, it is getting a relative one 
/myapp.  The reason I say the code expects an absolute URL is because 
it checks for and "http" string at the beginning.  This makes me 
wonder whether there is a setting that controls that URI format, 
absolute or relative.


Your understanding of the HTTP protocol is flawed. You may wish to read 
RFC 7230. Specifically:


https://datatracker.ietf.org/doc/html/rfc7230#section-3.1.1
and
https://datatracker.ietf.org/doc/html/rfc7230#section-5.3

Requests with the URI in origin-form do not include a host in the URI.

The purpose of allowHostHeaderMismatch is to ensure that when the 
request URI is in absolute-form that the request URI is consistent with 
the Host header.


Regarding the addition of a filter that you propose, we have an 
existing one in our application, but by the time it is reached, the 
URL that we see is already http://attacker.com/myapp, i.e. already 
"redirected".


There has been no redirect. The URI reported is a combination of the 
Host header and request URI received.


 Technically we could check there against a whitelist, but this would 
make the solution less out-of-the box, and more needy of user 
configuration in our app.  We prefer an out-of-the-box secure solution.


Any thoughts on the above?


What you want isn't possible.


Actually, I think what Ralph is requesting is exactly what Tomcat is 
providing in the form of allowHostHeaderMismatch (when set to false).


The only problem is that Ralph is saying it's not working because the 
URI in the request doesn't contain a hostname *at all* (because it's 
optional). So there is nothing to check. Browsers don't bother to send 
the optional protocol/hostname/port/etc and instead send the relative 
URI -- relative to the Host header (no coincidentally).


If you (Ralph) can reproduce this with wc or telnet where you can force 
the URI to be absolute *and* provide a conflicting Host header, then I 
think you have grounds for a bug report.



If you want requests to be rejected unless the Host header is on a
user defined allow list (presumably the set of DNS names defined for
the host), then you are going to have to provide a means for the user
to provide that configuration.


I don't think it being requested, here. It may be an aim of their 
application to do such things, but that wasn't a part of the initial 
request ("prevent host header injection" -- which is very strangely worded).


-chris

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Re: What causes "client errors" with mod_jk

2022-05-27 Thread Rainer Jung

Hi Chris,

Am 27.05.2022 um 01:49 schrieb Christopher Schultz:

On 5/26/22 17:25, Rainer Jung wrote:

Am 26.05.2022 um 21:49 schrieb Christopher Schultz:


Would you prefer to use mod_unique_id + unique-id-logging in mod_jk over 
just adding more request-level information to the mod_jk.log? I'm kind 
of okay either way, but for my current purposes it seems more convenient 
to have all relevant information in a single place (the mod_jk.log file).


I understand, but more useful info might pile up (URI, client IP), so I 
prefer having a working correlation ID to the other major sources of 
info, Apache access log and Apache error log. Also: the current idea of 
mod_jk when a request starts is not exactly when it started in Apache, 
but when the JK service method started. So we would also need to add the 
web server start time.


For now I used the request id and switched a lot (!) of log calls to 
using the new jk_request_log instead of jk_log. So you get the id in the 
next release for many of the lines mod_jk is able to log in a request 
context. Some very low level log lines will not have access to the 
request id, but on ase of error they should always be accompanied by a 
higher level logging of the error including the request id.


We'll see how it goes. Feel free to try it, I committed everything to 
the git repos.


I'm okay adding either or both of these "features" to the JK portion of 
the code. If we are considering "enhancing" this kind of logging in the 
JK portion, I would recommend that we add request_start_time to the 
jk_ws_service; I don't see a good way to determine the nature of the 
host web server from within the JK code and it's better-done from the 
outside-in rather than the inside-out.


Unrelated: I believe the segfaults I'm seeing have to do with me simply 
updating the .so file on the disk before restarting the httpd process. 
As soon as I copy the .so file over the existing module binary, I start 
getting a string of segfaults in the log file. When I don't try to 
"hot-update" the module binary, I don't see any of that happening. (I 
also don't see any possible segfaults in my code at this point, eitaher.) 
I have httpd set up to dump cores but I think my file permissions are 
wrong so I'm not actually getting anything in there (yet).


Best regards,

Rainer

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Re: allowHostHeaderMismatch option only works if the Host Header has an http or https prefix

2022-05-27 Thread Mark Thomas

On 27/05/2022 02:00, Ralph Atallah wrote:

Hi Mark,

Thanks again for the prompt response.

You wrote below:  "If the original request only has a Host header, then allowHostHeaderMismatch="false" 
isn't going to do anything because there is no mismatch.".  I am not clear on what this means.  What should the 
match be between?  I thought the comparison for the match was between the URL's hostname, i.e. "example.com" 
in the http://example.com/myapp URL, and the Host header value which is "attacker.com".  If that 
understanding is incorrect, please point me in the right direction of what it should be.


The check is that the host in the request URI (if present) is consistent 
with the Host header. Nothing more, nothing less.


HTTP requests may or may not include the host in the request URI.

The host named in the the headers of an HTTP request is completely 
independent of the host name used to establish the connection to the web 
server.



The AbstractHttp11Processor class does not get to the allowHostHeaderMismatch detection 
code because the uriBC (URI ByteChunk) that it reads is expecting an absolute URL 
(http://example.com/myapp), but instead, it is getting a relative one /myapp.  The reason 
I say the code expects an absolute URL is because it checks for and "http" 
string at the beginning.  This makes me wonder whether there is a setting that controls 
that URI format, absolute or relative.


Your understanding of the HTTP protocol is flawed. You may wish to read 
RFC 7230. Specifically:


https://datatracker.ietf.org/doc/html/rfc7230#section-3.1.1
and
https://datatracker.ietf.org/doc/html/rfc7230#section-5.3

Requests with the URI in origin-form do not include a host in the URI.

The purpose of allowHostHeaderMismatch is to ensure that when the 
request URI is in absolute-form that the request URI is consistent with 
the Host header.



Regarding the addition of a filter that you propose, we have an existing one in our 
application, but by the time it is reached, the URL that we see is already 
http://attacker.com/myapp, i.e. already "redirected".


There has been no redirect. The URI reported is a combination of the 
Host header and request URI received.



 Technically we could check there against a whitelist, but this would make the 
solution less out-of-the box, and more needy of user configuration in our app.  
We prefer an out-of-the-box secure solution.

Any thoughts on the above?


What you want isn't possible. If you want requests to be rejected unless 
the Host header is on a user defined allow list (presumably the set of 
DNS names defined for the host), then you are going to have to provide a 
means for the user to provide that configuration.


The approach requiring the minimal input from the app and where the 
container does most of the work is the one where you define a Host 
element in server.xml with the name and optional aliases for the host 
names that are acceptable and configure the default host (that handles 
all requests to other hosts) to reject all other requests.


Mark



Thanks,
Ralph

-Original Message-
From: Mark Thomas 
Sent: Thursday, May 26, 2022 12:21 PM
To: users@tomcat.apache.org
Subject: Re: allowHostHeaderMismatch option only works if the Host Header has 
an http or https prefix

WARNING: This email originated from outside of CallMiner. Do not click any links or 
open any attachments unless you recognize the sender and know that the content is 
safe. Please report suspicious emails to: reportsuspiciousema...@callminer.com 


On 26/05/2022 14:29, Ralph Atallah wrote:

Hi Mark,

What we are trying to do is to prevent Host header attacks by ensuring that the host name 
in the http request URL always matches the "Host" header in the request.  If it 
does not, we are supposed refuse the request and respond with 400 Bad Request as per 
OWASP recommendations.   Here are some examples:

Normal request
 GET http://example.com/myapp
 Host: example.com
 Expected response:  200 OK

Request with a host header attack
 GET http://example.com/myapp
 Host: attacker.com
 Expected response:  400 Bad Request

The AbstracktHttp11Processor.java class seems to be doing exactly that in the 
code snippet below:

 if (allowHostHeaderMismatch) {
   // The requirements of RFC 2616 are being
   // applied. If the host header and the request
   // line do not agree, the request line takes
   // precedence
   hostValueMB = headers.setValue("host");
   hostValueMB.setBytes(uriB, uriBCStart + pos, slashPos - pos);
   } else {
// The requirements of RFC 7230 are being
// applied. If the host header and the request
// line do not agree, trigger a 400 response.
badRequest("http11processor.request.inconsistentHosts");
   }

However, this portion of the code is never reached for the reason mentioned in 
the