Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 28, 2025

When a gzipped response decompresses successfully but contains non-UTF-8 bytes, the middleware was returning decompressed binary data with no content-encoding header. This violates HTTP semantics—the client requested gzip via Accept-Encoding but received uncompressed binary.

Changes

  • Store original body before decompression: Added original_body to preserve the compressed payload
  • Return original on decode failure: When UTF-8 decoding fails, return original_body with original content_encoding instead of decompressed bytes with empty encoding
  • Simplify exception handling: Removed unused decompressed flag, use contextlib.suppress for cleaner code

Example

Before:

# Client requests gzipped response
# Middleware decompresses successfully but decode fails
# Returns: decompressed binary + content-encoding: ""
# Result: Client receives unexpected format

After:

# Client requests gzipped response  
# Middleware decompresses successfully but decode fails
# Returns: original compressed body + content-encoding: "gzip"
# Result: Client receives expected format

The middleware now respects the client's Accept-Encoding negotiation when toolbar injection isn't possible.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

JacobCoffee and others added 10 commits December 27, 2025 21:20
The debug toolbar middleware was failing to inject the toolbar HTML into
gzip-compressed responses. When Litestar's compression middleware runs
before the debug toolbar, the response body is gzip bytes, which cannot
be decoded as UTF-8. The middleware silently returned the original body
without injection.

This fix:
- Detects gzip content-encoding header
- Decompresses the response body before injection
- Injects the toolbar HTML into the decompressed HTML
- Returns uncompressed response (strips content-encoding header)

This ensures the toolbar is visible when compression is enabled, which
is the default in many production-like configurations.

Fixes toolbar not appearing on admin pages when compression is enabled.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: JacobCoffee <45884264+JacobCoffee@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI self-assigned this Dec 28, 2025
Base automatically changed from fix/gzip-compression-injection to main December 28, 2025 04:11
@JacobCoffee JacobCoffee marked this pull request as ready for review December 28, 2025 04:16
Copilot AI review requested due to automatic review settings December 28, 2025 04:16
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot wasn't able to review any files in this pull request.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…failure

Co-authored-by: JacobCoffee <45884264+JacobCoffee@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 28, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • astral.sh
    • Triggering command: /usr/bin/curl curl -LsSf REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title [WIP] WIP address feedback on gzip-compressed responses Fix gzip response handling to preserve HTTP semantics on decode failure Dec 28, 2025
Copilot AI requested a review from JacobCoffee December 28, 2025 04:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants