Skip to content

Conversation

@niklas
Copy link
Contributor

@niklas niklas commented Jul 28, 2025

When doing a HEAD-request with "Accept-Encoding: gzip", some web servers set the content-encoding to gzip, but naturally transfer an empty body.

This makes :zlib.gunzip crash with

** (ErlangError) Erlang error: :data_error
    (erts 15.2.7) :zlib.inflateEnd_nif(#Reference<0.2948451012.2698379265.82762>)
    (erts 15.2.7) :zlib.gunzip/1
    iex:1: (file)

This PR skips decompression of an empty body.

Copilot AI review requested due to automatic review settings July 28, 2025 16:54
Copy link

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.

Pull Request Overview

This PR fixes a crash that occurs when decompressing gzip-encoded responses with empty bodies, which can happen with HEAD requests that include "Accept-Encoding: gzip" headers. The fix adds a guard clause to skip decompression when the response body is empty.

  • Adds early return for empty body decompression to prevent :zlib.gunzip crashes
  • Maintains existing decompression logic for non-empty bodies
Comments suppressed due to low confidence (1)

lib/tesla/middleware/compression.ex:87

  • This new function clause that handles empty body decompression should have test coverage to verify it prevents the crash described in the PR and returns the expected result.
  defp decompress_body(_codecs, "" = body, acc) do

@yordis
Copy link
Member

yordis commented Jul 28, 2025

@niklas thank you so much for the help, I wonder if you could add a unit test to guarantee correctness.

@yordis yordis changed the title Avoid crash then gzip-decompressing empty body fix: Avoid crash then gzip-decompressing empty body Jul 28, 2025
@niklas
Copy link
Contributor Author

niklas commented Jul 30, 2025

Sure. Should I squash them?

@yordis
Copy link
Member

yordis commented Jul 30, 2025

@niklas ideally, yes, I need to figure out that situation to be honest, it helps with releasing automation but I can see that could be annoying.

Maybe I shouldn't check the Commits, and only enforce the title, I am unsure

When doing a HEAD-request with Accept-Encoding: gzip, some web servers set the
`content-encoding` to `gzip`, but naturally transfer an empty body.
@niklas niklas force-pushed the decompress-empty-body branch from 38f42a6 to 71f0751 Compare July 30, 2025 16:55
@yordis yordis merged commit 5bc9b82 into elixir-tesla:master Jul 30, 2025
6 checks passed
@niklas
Copy link
Contributor Author

niklas commented Jul 31, 2025

\o/

tesla-compressed-empty-body

@niklas niklas deleted the decompress-empty-body branch July 31, 2025 09:55
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