psf / psf/requests

Allow redirects not configurable in sessions

Open
#4,951 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
54.3k
Forks
10.4k
Avg merge
16h 43m
Merged PRs (30d)
3

Description

I have an application I need to reach behind a firewall, I've set up a tunnel to this application via SSH running on my machine which forwards all connections from 127.0.0.1:1234 to 192.168.1.10:80. Whenever I make a request to my application, it redirects me to a login page first.

The issue with this is that all redirect URL's have the 192.168.1.10 hostname, which requests tries to follow and inevitably crashes (as this host does not exist on my network).

I tried to set allow_redirects to False on my session by overriding the request method, but the session source hard-codes the allow_redirects option to True if it isn't explicitly set when calling get, head and options (source)

Expected Result

I expected no redirect to be followed, but rather that that the request ends after the first response. I expected the allow_redirects option to be configurable in sessions like the verify flag is (source)

Actual Result

The 302 was resolved, and attempted to redirect me to a nonexistent host, leading to a connection error exception.

Reproduction Steps

import requests


class Example(requests.Session):
    def __init__(self, allow_redirects=False, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs)
        self.allow_redirects = allow_redirects

    def request(self, *args, **kwargs):
        kwargs.setdefault('allow_redirects', self.allow_redirects)
        return super(Example, self).request(*args, **kwargs)


sess = Example()
sess.get('http://127.0.0.1:1234')  # ConnectionError ... 192.168.1.10 connection refused (redirected)

System Information

$ python -m requests.help
{
  "chardet": {
    "version": "3.0.4"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "2.8"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.6.0"
  },
  "platform": {
    "release": "10",
    "system": "Windows"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.21.0"
  },
  "system_ssl": {
    "version": "100020af"
  },
  "urllib3": {
    "version": "1.24.1"
  },
  "using_pyopenssl": false
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading requests/sessions.py around the get, head, options, and session configuration code referenced in the issue, especially the areas near lines 545 and 625. Verify how session defaults are applied and add configurable redirect behavior so a session can stop at the first response; done means the provided Example reproduction no longer follows the 302.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.