Skip to content

fix(filer): HTTP(S) uploads read outputs in text mode, so binary files fail and some text is altered #215

Closed
elixir-cloud-aai/tesk-core
#61

Description

@anuragxxd

Problem

HTTPTransput.upload_file reads the output file in text mode and uploads the decoded string (source/tesk-core/src/tesk_core/filer.py:42-45):

with open(self.path, 'r') as file:
    file_contents = file.read()
req = requests.put(self.url, data=file_contents)

Decoding the file, and then encoding the string again for the request (http.client encodes a str body as Latin-1), rejects or changes anything that isn't plain ASCII with LF line endings. The deployed filer image docker.io/elixircloud/tesk-core-filer:v0.10.2, the one charts/tesk/values.yaml uses, has the same code.

Reproduction

I ran HTTPTransput(path, url, Type.File).upload_file() inside elixircloud/tesk-core-filer:v0.10.2 (Python 3.7.10, requests 2.31.0) against a local server that saves each PUT body:

File upload_file() Bytes received
ASCII text returns 0 identical
gzip raises UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1 nothing
UTF-8 café returns 0 é arrives as Latin-1 (c3 a9 became e9)
UTF-8 x ≥ 1 raises UnicodeEncodeError: 'latin-1' codec can't encode character '≥' nothing
CRLF text returns 0 CRLF arrives as LF

So binary outputs (BAM, gzip, images) can't be uploaded over HTTP(S). Two kinds of text output are uploaded altered while the upload reports success.

Proposed fix

Open the file in binary mode and pass the file object, so it's sent byte for byte and streamed instead of read into memory:

with open(self.path, 'rb') as file:
    data = file if os.fstat(file.fileno()).st_size > 0 else b''
    req = requests.put(self.url, data=data)

requests sets Content-Length from the file size. The b'' case matters: for an empty file object requests falls back to Transfer-Encoding: chunked, which S3-style presigned upload URLs reject. In the same image, this version uploaded all five files above plus an empty file byte for byte, each with a Content-Length and no chunked encoding. Passing the file object alone sent the empty file chunked.

Related: download_file holds the whole response in memory (req.content); stream=True with iter_content would avoid that.

#179 plans a filer redesign. This is a bug in the code deployed today, so it seems worth fixing in place first. I'm happy to send a PR.

Context: we use TES HTTP(S) outputs with presigned or redirecting upload URLs, so TES nodes need no credentials for the client's storage. calypr/funnel#1471 covers the same capability in Funnel.

Activity

  1. anuragxxd commented on Oct 23, 2026

    @anuragxxd
    MemberAuthor

    @uniqueg what do you think about this?

  2. uniqueg commented on Oct 23, 2026

    @uniqueg
    Member

    @uniqueg what do you think about this?

    Go for it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions