-
-
Notifications
You must be signed in to change notification settings - Fork 4.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
[encode] Encoding bug when handle_errors is invoked #6617
Comments
I wonder if this is the same issue I have recently encountered while testing with v2.9.0 beta (using Do you have DEBUG enabled and if so, do you see something like this in your logs? This was logged when I had the broken browse template:
|
Can you give an example of a file being served and a template that produces the error? Having a actual reproducer would help. I'm not sure how this could be solved. If Caddy already started streaming the response, it's kinda "too late". I think |
Here's an example template that has a bug. Just save as <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Caddy Bug Demo</title>
<meta name="author" content="Blake Miner" />
<meta name="description" content="Blake is a software developer who obsesses about databases" />
</head>
<body>
<main>
<h1>Test</h1>
<p>Here is the bug: {{ poop }}</p>
</main>
<footer>
© Copyright 2023-2024. Blake Miner. All rights reserved.
</footer>
</body>
</html> |
I hear what you're saying here, but somehow
Note that the last line of the curl output "Error" is the output from |
I also think template errors should be emitted on the |
The
I would guess that has to do with one handler flow writing the headers that it expects to be correct, and then an error happening later that causes breakage.
That logger is generally for HTTP requests, not internal errors... why do you have this request? |
Makes sense. I don't know if HTTP provides a mechanism to send a proper response once the stream is broken (i.e. render a 500.html page if an error occurs with a previously streamed response body).... especially when
Makes sense.
Somewhat unrelated to this issue, but my understanding is that template errors propagate through to the |
After thinking about this issue a bit more, I want to provide some clarification:
It is my understanding that Caddy does this. It reads the template from the recorded (i.e. buffered) HTTP response that was buffered by the I think that the true nature of the bug report really boils down to how the encode middleware interacts with the handle_errors middleware. I'm speculating that buffered responses like templates with errors in them reveal the nature of the underlying bug. |
The HTTP response body is corrupted due to an encoding bug, which occurs when combining
encode
andhandle_errors
directives, especially withfile_server
andtemplates
.What We Expect
Caddy will use the HTTP response from
handle_errors
to determine how to encode the response and send appropriate headers (i.e.Content-Encoding
).What Happens
EDIT:
Caddy will start compressing the payload and sendContent-Encoding
headers; however,When an error occurs and the HTTP response is set by the
handle_errors
directive, the response body somehow gets corrupted. I believe this is the case even for buffered HTTP responses.To reproduce
encode
directive is used to enable encoding. It does not matter which encoding format is used.Accept-Encoding
headers to enable the encoding middlewarefile_server
directive serves a file larger than theminimumLength
for the encode directivehandle_errors
directive to be invokedExample:
Consider this Caddyfile configuration:
Suppose file
/var/www/example.com/test.html
is greater than 512 bytes and contains a template error of some kind. In this case, Caddy is supposed to respond with "Error" but instead delivers a corrupted HTTP response. In the above example, the HTTP gzip-encoded response body looks like this (encoded in hexadecimal):Notice the last 5 bytes are the string "Error" but the first bytes appear to be a valid gzip encoding of 0 bytes.
I am happy to write a PR for this if I can get some pointers on where to look.
The text was updated successfully, but these errors were encountered: