Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asks returns [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1051) #132

Open
Spyder-exe opened this issue Aug 11, 2019 · 8 comments

Comments

@Spyder-exe
Copy link

Spyder-exe commented Aug 11, 2019

Hey so I'm trying to send a GET request to spotify.com, and it constantly returns an SSL Error: wrong version number. It doesn't do this with requests or aiohttp, so I'm guessing asks is mishandling something

example code that triggers the error:

import trio
import asks
async def check(s):

    overview = await s.get("https://www.spotify.com/us/account/overview")
    print(overview.text)


async def run_task():
    s = asks.Session()
    async with trio.open_nursery() as nursery:
        nursery.start_soon(check, s)
    
trio.run(run_task)
@theelous3
Copy link
Owner

Traceback:

Task Crash: Task(id=2, name='m', state='TERMINATED')
Traceback (most recent call last):
  File "/home/theelous3/zoo/python/asks/env/lib/python3.6/site-packages/curio/kernel.py", line 736, in _run_coro
    trap = current._send(current.next_value)
  File "/home/theelous3/zoo/python/asks/env/lib/python3.6/site-packages/curio/task.py", line 167, in _task_runner
    return await coro
  File "<stdin>", line 2, in m
  File "/home/theelous3/zoo/python/asks/asks/asks/base_funcs.py", line 30, in request
    r = await s.request(method, url=uri, **kwargs)
  File "/home/theelous3/zoo/python/asks/asks/asks/sessions.py", line 215, in request
    await self._handle_exception(e, sock)
  File "/home/theelous3/zoo/python/asks/asks/asks/sessions.py", line 257, in _handle_exception
    raise e
  File "/home/theelous3/zoo/python/asks/asks/asks/sessions.py", line 185, in request
    sock, r = await req_obj.make_request()
  File "/home/theelous3/zoo/python/asks/asks/asks/request_object.py", line 216, in make_request
    response_obj = await self._request_io(req, req_body, h11_connection)
  File "/home/theelous3/zoo/python/asks/asks/asks/request_object.py", line 273, in _request_io
    response_obj = await self._redirect(response_obj)
  File "/home/theelous3/zoo/python/asks/asks/asks/request_object.py", line 372, in _redirect
    _, response_obj = await self.make_request()
  File "/home/theelous3/zoo/python/asks/asks/asks/request_object.py", line 216, in make_request
    response_obj = await self._request_io(req, req_body, h11_connection)
  File "/home/theelous3/zoo/python/asks/asks/asks/request_object.py", line 273, in _request_io
    response_obj = await self._redirect(response_obj)
  File "/home/theelous3/zoo/python/asks/asks/asks/request_object.py", line 356, in _redirect
    await self._get_new_sock()
  File "/home/theelous3/zoo/python/asks/asks/asks/request_object.py", line 382, in _get_new_sock
    self.sock = await self.session._grab_connection(self.uri)
  File "/home/theelous3/zoo/python/asks/asks/asks/sessions.py", line 368, in _grab_connection
    sock = await self._make_connection(host_loc)
  File "/home/theelous3/zoo/python/asks/asks/asks/sessions.py", line 342, in _make_connection
    sock, port = await self._connect(host_loc)
  File "/home/theelous3/zoo/python/asks/asks/asks/sessions.py", line 101, in _connect
    (host, int(port))), port
  File "/home/theelous3/zoo/python/asks/asks/asks/sessions.py", line 79, in _open_connection_https
    tls_standard_compatible=False)
  File "/home/theelous3/zoo/python/asks/env/lib/python3.6/site-packages/anyio/__init__.py", line 329, in connect_tcp
    await stream.start_tls()
  File "/home/theelous3/zoo/python/asks/env/lib/python3.6/site-packages/anyio/_networking.py", line 315, in start_tls
    not self._tls_standard_compatible)
  File "/home/theelous3/zoo/python/asks/env/lib/python3.6/site-packages/anyio/_networking.py", line 170, in start_tls
    self._raw_socket.do_handshake()
  File "/usr/lib/python3.6/ssl.py", line 1077, in do_handshake
    self._sslobj.do_handshake()
  File "/usr/lib/python3.6/ssl.py", line 689, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:852)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/theelous3/zoo/python/asks/env/lib/python3.6/site-packages/curio/kernel.py", line 818, in run
    return kernel.run(corofunc, *args)
  File "/home/theelous3/zoo/python/asks/env/lib/python3.6/site-packages/curio/kernel.py", line 179, in run
    raise ret_exc
  File "/home/theelous3/zoo/python/asks/env/lib/python3.6/site-packages/curio/kernel.py", line 736, in _run_coro
    trap = current._send(current.next_value)
  File "/home/theelous3/zoo/python/asks/env/lib/python3.6/site-packages/curio/task.py", line 167, in _task_runner
    return await coro
  File "<stdin>", line 2, in m
  File "/home/theelous3/zoo/python/asks/asks/asks/base_funcs.py", line 30, in request
    r = await s.request(method, url=uri, **kwargs)
  File "/home/theelous3/zoo/python/asks/asks/asks/sessions.py", line 215, in request
    await self._handle_exception(e, sock)
  File "/home/theelous3/zoo/python/asks/asks/asks/sessions.py", line 257, in _handle_exception
    raise e
  File "/home/theelous3/zoo/python/asks/asks/asks/sessions.py", line 185, in request
    sock, r = await req_obj.make_request()
  File "/home/theelous3/zoo/python/asks/asks/asks/request_object.py", line 216, in make_request
    response_obj = await self._request_io(req, req_body, h11_connection)
  File "/home/theelous3/zoo/python/asks/asks/asks/request_object.py", line 273, in _request_io
    response_obj = await self._redirect(response_obj)
  File "/home/theelous3/zoo/python/asks/asks/asks/request_object.py", line 372, in _redirect
    _, response_obj = await self.make_request()
  File "/home/theelous3/zoo/python/asks/asks/asks/request_object.py", line 216, in make_request
    response_obj = await self._request_io(req, req_body, h11_connection)
  File "/home/theelous3/zoo/python/asks/asks/asks/request_object.py", line 273, in _request_io
    response_obj = await self._redirect(response_obj)
  File "/home/theelous3/zoo/python/asks/asks/asks/request_object.py", line 356, in _redirect
    await self._get_new_sock()
  File "/home/theelous3/zoo/python/asks/asks/asks/request_object.py", line 382, in _get_new_sock
    self.sock = await self.session._grab_connection(self.uri)
  File "/home/theelous3/zoo/python/asks/asks/asks/sessions.py", line 368, in _grab_connection
    sock = await self._make_connection(host_loc)
  File "/home/theelous3/zoo/python/asks/asks/asks/sessions.py", line 342, in _make_connection
    sock, port = await self._connect(host_loc)
  File "/home/theelous3/zoo/python/asks/asks/asks/sessions.py", line 101, in _connect
    (host, int(port))), port
  File "/home/theelous3/zoo/python/asks/asks/asks/sessions.py", line 79, in _open_connection_https
    tls_standard_compatible=False)
  File "/home/theelous3/zoo/python/asks/env/lib/python3.6/site-packages/anyio/__init__.py", line 329, in connect_tcp
    await stream.start_tls()
  File "/home/theelous3/zoo/python/asks/env/lib/python3.6/site-packages/anyio/_networking.py", line 315, in start_tls
    not self._tls_standard_compatible)
  File "/home/theelous3/zoo/python/asks/env/lib/python3.6/site-packages/anyio/_networking.py", line 170, in start_tls
    self._raw_socket.do_handshake()
  File "/usr/lib/python3.6/ssl.py", line 1077, in do_handshake
    self._sslobj.do_handshake()
  File "/usr/lib/python3.6/ssl.py", line 689, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:852)

@theelous3
Copy link
Owner

Looks like an anyio issue.

Testing in browser, that url redirects you to the accounts.spotify.com domain, off domain of spotify.com

If you are not already logged in, you cannot access the domain you're aiming for. When requests and aiohttp return ok, they are not returning the actual accounts page, they are returning the subdomained login page here:

'https://accounts.spotify.com/login/?_locale=en-US&continue=https%3A//www.spotify.com/us/account/overview/'

We can open an issue on anyio to see what we can do about not panicing on ssl stuff when we are 30x moved from one domain to a subdomain.

@Spyder-exe
Copy link
Author

Spyder-exe commented Aug 12, 2019

Sure thing, I'll open up a issue when I find the time, thanks

@agronholm
Copy link
Contributor

agronholm commented Aug 12, 2019

Continuing from the other side: this is an asks bug. The HTTP server returns a redirect in the form of //accounts.spotify.com/login/?_locale=en-US&continue=https%3A//www.spotify.com/us/account/overview/ which confuses asks into making an HTTPS request to port 80.

@Spyder-exe
Copy link
Author

Spyder-exe commented Aug 12, 2019

As per anyio's creator: agronholm/anyio#67

asks calls anyio.connect_tcp() using port 80 but with autostart_tls=True. This is definitely a bug in asks, not anyio.

Maybe this is related to my previous issue about asks mishandling redirections? #131

@Spyder-exe Spyder-exe reopened this Aug 12, 2019
@agronholm
Copy link
Contributor

If I'm interpreting the RFC correctly, any missing components in the redirect URI (scheme, host, port) should be replaced with values from the current URI. As I understand, asks does not do this.

@theelous3
Copy link
Owner

@agronholm nice catch.
@Spyder-exe also nice catch.

Going to crack this open now. Time for a nice spring clean around redirects.

>>> curio.run(m, "https://www.spotify.com/us/account/overview")
HTTPS CALL TO: ('www.spotify.com', 443)
HTTPS CALL TO: ('www.spotify.com', 443)
HTTPS CALL TO: ('accounts.spotify.com', 80)

@freis
Copy link

freis commented Dec 12, 2019

Hey @theelous3 did you manage to follow up with this? I'm getting the same issue when trying to request an IP from the port 80 that then redirects to HTTPs and has one of those mismatch certs. I use the ssl_context in all my sessions but on this case I think that the context is not passed on the redirect.

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

No branches or pull requests

4 participants