[GH-ISSUE #364] ValueError: Invalid header value b'Basic <ConsumerKey>', error when uploading the local cast #845

Closed
opened 2026-03-15 10:42:32 +03:00 by kerem · 7 comments
Owner

Originally created by @mochadwi on GitHub (Jul 24, 2019).
Original GitHub issue: https://github.com/asciinema/asciinema/issues/364

I'm trying to upload the local cast, but below error is thrown instead.

Here's the error.

Traceback (most recent call last):
  File "/usr/local/Cellar/asciinema/2.0.2/libexec/bin/asciinema", line 11, in <module>
    load_entry_point('asciinema==2.0.2', 'console_scripts', 'asciinema')()
  File "/usr/local/Cellar/asciinema/2.0.2/libexec/lib/python3.7/site-packages/asciinema/__main__.py", line 131, in main
    code = command.execute()
  File "/usr/local/Cellar/asciinema/2.0.2/libexec/lib/python3.7/site-packages/asciinema/commands/upload.py", line 14, in execute
    result, warn = self.api.upload_asciicast(self.filename)
  File "/usr/local/Cellar/asciinema/2.0.2/libexec/lib/python3.7/site-packages/asciinema/api.py", line 40, in upload_asciicast
    password=self.install_id
  File "/usr/local/Cellar/asciinema/2.0.2/libexec/lib/python3.7/site-packages/asciinema/urllib_http_adapter.py", line 76, in post
    response = urlopen(request)
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 525, in open
    response = self._open(req, data)
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 543, in _open
    '_open', req)
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1360, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1317, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1244, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1285, in _send_request
    self.putheader(hdr, value)
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1222, in putheader
    raise ValueError('Invalid header value %r' % (values[i],))
ValueError: Invalid header value b'Basic <ConsumerKey>'
Originally created by @mochadwi on GitHub (Jul 24, 2019). Original GitHub issue: https://github.com/asciinema/asciinema/issues/364 I'm trying to upload the local cast, but below error is thrown instead. Here's the error. ``` Traceback (most recent call last): File "/usr/local/Cellar/asciinema/2.0.2/libexec/bin/asciinema", line 11, in <module> load_entry_point('asciinema==2.0.2', 'console_scripts', 'asciinema')() File "/usr/local/Cellar/asciinema/2.0.2/libexec/lib/python3.7/site-packages/asciinema/__main__.py", line 131, in main code = command.execute() File "/usr/local/Cellar/asciinema/2.0.2/libexec/lib/python3.7/site-packages/asciinema/commands/upload.py", line 14, in execute result, warn = self.api.upload_asciicast(self.filename) File "/usr/local/Cellar/asciinema/2.0.2/libexec/lib/python3.7/site-packages/asciinema/api.py", line 40, in upload_asciicast password=self.install_id File "/usr/local/Cellar/asciinema/2.0.2/libexec/lib/python3.7/site-packages/asciinema/urllib_http_adapter.py", line 76, in post response = urlopen(request) File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 222, in urlopen return opener.open(url, data, timeout) File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 525, in open response = self._open(req, data) File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 543, in _open '_open', req) File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 503, in _call_chain result = func(*args) File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1360, in https_open context=self._context, check_hostname=self._check_hostname) File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1317, in do_open encode_chunked=req.has_header('Transfer-encoding')) File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1244, in request self._send_request(method, url, body, headers, encode_chunked) File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1285, in _send_request self.putheader(hdr, value) File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1222, in putheader raise ValueError('Invalid header value %r' % (values[i],)) ValueError: Invalid header value b'Basic <ConsumerKey>' ```
kerem closed this issue 2026-03-15 10:42:38 +03:00
Author
Owner

@ku1ik commented on GitHub (Sep 8, 2019):

Are you still experiencing this? Is it the same when you asciinema rec (rec+upload in one go) vs asciinema rec demo.cast + asciinema upload demo.cast?

<!-- gh-comment-id:529188222 --> @ku1ik commented on GitHub (Sep 8, 2019): Are you still experiencing this? Is it the same when you `asciinema rec` (rec+upload in one go) vs `asciinema rec demo.cast` + `asciinema upload demo.cast`?
Author
Owner

@Kikobeats commented on GitHub (Jun 25, 2020):

same issue here, one year later

<!-- gh-comment-id:649755232 --> @Kikobeats commented on GitHub (Jun 25, 2020): same issue here, one year later
Author
Owner

@vladimirkuvanovv commented on GitHub (Jun 28, 2020):

I have the same issue too

<!-- gh-comment-id:650770290 --> @vladimirkuvanovv commented on GitHub (Jun 28, 2020): I have the same issue too
Author
Owner

@gingermusketeer commented on GitHub (Sep 19, 2021):

I was able to get around this issue by changing this line to :

encoded_auth = base64.b64encode(bytes(auth, "ascii"))
<!-- gh-comment-id:922450719 --> @gingermusketeer commented on GitHub (Sep 19, 2021): I was able to get around this issue by changing [this line](https://github.com/asciinema/asciinema/blob/develop/asciinema/urllib_http_adapter.py#L70) to : ```python encoded_auth = base64.b64encode(bytes(auth, "ascii")) ```
Author
Owner

@sriumcp commented on GitHub (Oct 1, 2021):

The solution from @gingermusketeer looks right. HTTP headers are indeed supposed to be ascii encoded. Any chance of this fix making it into a release soon?

I love asciinema and was eagerly looking forward to use this until I was bitten by this bug!

<!-- gh-comment-id:932418968 --> @sriumcp commented on GitHub (Oct 1, 2021): The solution from @gingermusketeer looks right. HTTP headers are indeed supposed to be `ascii` encoded. Any chance of this fix making it into a release soon? I love asciinema and was eagerly looking forward to use this until I was bitten by this bug!
Author
Owner

@ku1ik commented on GitHub (Oct 2, 2021):

Thanks for digging into this. However it's not about utf-8 vs ascii. Base64 encoding always produces ascii output, regardles of the input.

I figured out it's about the length of the input. Given the password here is the locally generated UUID v4, which is fixed-length, it must be the length of the username, which is taken from $USER env var.

I tested it and in fact username longer than 20 chars causes ValueError: Invalid header value b'Basic ...' error.

Turns out base64.encodebytes return bytes containing the base64-encoded data, with newlines (b'\n') inserted after every 76 bytes of output (see https://docs.python.org/3/library/base64.html#base64.encodebytes). That extra newline inside the header value is the problem.

base64.b64encode doesn't do line breaks, that's why it works. I tested it with utf-8 and it does the job:

encoded_auth = base64.b64encode(bytes(auth, "utf-8"))

I'll release a patch shortly.

<!-- gh-comment-id:932752954 --> @ku1ik commented on GitHub (Oct 2, 2021): Thanks for digging into this. However it's not about utf-8 vs ascii. Base64 encoding always produces ascii output, regardles of the input. I figured out it's about the length of the input. Given the password here is the locally generated UUID v4, which is fixed-length, it must be the length of the username, which is taken from `$USER` env var. I tested it and in fact username longer than 20 chars causes `ValueError: Invalid header value b'Basic ...'` error. Turns out `base64.encodebytes` return bytes containing the base64-encoded data, with newlines (b'\n') inserted after every 76 bytes of output (see https://docs.python.org/3/library/base64.html#base64.encodebytes). That extra newline inside the header value is the problem. `base64.b64encode` doesn't do line breaks, that's why it works. I tested it with utf-8 and it does the job: ```python3 encoded_auth = base64.b64encode(bytes(auth, "utf-8")) ``` I'll release a patch shortly.
Author
Owner

@gingermusketeer commented on GitHub (Oct 2, 2021):

Thanks a lot @sickill !

<!-- gh-comment-id:932755590 --> @gingermusketeer commented on GitHub (Oct 2, 2021): Thanks a lot @sickill !
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/asciinema#845
No description provided.