package_control icon indicating copy to clipboard operation
package_control copied to clipboard

urllib/wininet downloaders return a truncated body as success (IncompleteRead.partial swallowed)

Open dpc00 opened this issue 2 months ago • 1 comments

The urllib and wininet downloaders return a truncated body as a successful download when the connection closes before Content-Length bytes arrive (curl correctly fails).

Root cause for the urllib downloader: http/debuggable_http_response.py

def read(self, *args):
    try:
        return HTTPResponse.read(self, *args)
    except (IncompleteRead) as e:
        return e.partial

swallows IncompleteRead and hands the partial data back, so urllib_downloader.py sees a normal response (and then cache_result may cache it). The wininet downloader shows the same symptom: its read loop (wininet_downloader.py, while try_again: wininet.InternetReadFile(...)) ignores InternetReadFile's return value and only stops when bytes_read == 0, so both a peer close and a read error/timeout look like a clean EOF. (A stalled response with Content-Length: 20 and 5 bytes sent returned an empty body as success after the 4 s timeout.)

Reproduced live through download_manager.http_get(url, settings) in Sublime Text 4 (Package Control 4.2.8, master 1c52ef1 has the same code), forcing each downloader via downloader_precedence, against a local test server that sends Content-Length: 40, writes 9 bytes and closes the socket:

downloader result
urllib OK, b'{"ok": tr'
wininet OK, b'{"ok": tr'
curl DownloaderException: curl: (18) end of response with 31 bytes missing

Plain Python behaves correctly on the same server (http.client and urllib.request both raise IncompleteRead(9 bytes read, 31 more expected)), so the failure is Package Control's own handling. A gzip body cut in half is detected (EOFError), but an uncompressed one, such as a channel/repository JSON, a .sublime-package, or a library wheel, is not. Depending on the caller this becomes a confusing JSON/zip error far from the cause, or a wrongly cached partial file.

Fix: don't catch IncompleteRead in read (let HTTPException handling in urllib_downloader retry/report it), and in wininet_downloader compare the bytes read with Content-Length when present.

dpc00 avatar Sep 24 '26 16:09 dpc00

This is worse than a one-off bad download because of the HTTP cache: the truncated body is stored and then served indefinitely.

CachingDownloader.cache_result() caches any 200 response that has an ETag/Last-Modified (it doesn't compare the length with Content-Length), and add_conditional_headers() then sends If-None-Match/If-Modified-Since on later requests; a 304 returns the cached copy.

Reproduced live in Sublime Text 4 (Package Control 4.2.8, urllib downloader, real HttpCache) against a local server with ETag: "v1":

  1. First fetch: server sends Content-Length: 40, writes 9 bytes and closes. Result: b'{"ok": tr' (success, see above), and both <md5(url)> (the 9 bytes) and <md5(url)>.info (the ETag) are written to http_cache/.
  2. The server is fixed (it now has the full 40-byte body available; a client without validators gets it; verified with curl).
  3. Second and third fetch via Package Control: the request carries If-None-Match: "v1", the server answers 304, and the client returns the truncated cached body again, b'{"ok": tr'.

So a single interrupted download of a channel/repository JSON (or anything else with an ETag) can leave the user with a permanently broken cached copy until the ETag changes or the cache entry is pruned (http_cache_length, default 7 days). Fixing the read (don't return IncompleteRead.partial, verify Content-Length in wininet) also stops the cache from being poisoned; additionally cache_result could refuse to cache when len(content) doesn't match the response's Content-Length (after decoding it can't be checked, so check before decode).

dpc00 avatar Sep 24 '26 17:09 dpc00