0

Similar to a question asked here: Http Redirection code 3XX in python requests. I do also not receive redirection when I'm trying to post a form with python's requests.

To bypass same origin policy, my goal is it to proxy (redirect) an internal site with my flask application through the following code:

method_requests_mapping = {
'GET': requests.get,
'HEAD': requests.head,
'POST': requests.post,
'PUT': requests.put,
'DELETE': requests.delete,
'PATCH': requests.patch,
'OPTIONS': requests.options,
}


@bp.route('/<path:url>', methods=method_requests_mapping.keys())
def proxy(url):
    url='https://intern.something.com/'+url
    username=session['username']
    password=session['password']





    requests_function = method_requests_mapping[flask.request.method]
    request = requests_function(url, stream=True,    params=flask.request.args,auth=(username, password),allow_redirects=False)

    response = flask.Response(flask.stream_with_context(request.iter_content()),
                          content_type=request.headers['content-type'],

                          status=request.status_code, )
    response.headers['Access-Control-Allow-Origin'] = '*'
    print(request.history)
    print(request.cookies)
    print(request.status_code)
    return response

If I am trying to use the site without my flask proxy network analysis shows me this:

Request:

Host: intern.something.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:64.0)  Gecko/20100101 Firefox/64.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate, br
Referer: https://intern.something.com/contract_config_edit.php4?Contract_ID=1463234
Content-Type: application/x-www-form-urlencoded
Content-Length: 4024
Authorization: Basic YWhvZWhuZTpLYXR6ZTc0MzYh
Connection: keep-alive
Cookie: PHPSESSID=kr9am6tpid67ikct3up67f03h0
Upgrade-Insecure-Requests: 1

Answer:

HTTP/1.1 302 Found
Date: Wed, 02 Jan 2019 07:50:31 GMT
Server: Apache/2.2.3 (Red Hat)
X-Powered-By: PHP/5.1.6
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-  check=0
Pragma: no-cache
Location: https://intern.something.com /contract_show.php4?Contract_ID=1463234
Content-Length: 0
Connection: close
Content-Type: text/html

But if I do it with the proxy it seems not to work correctly:

Request:

Host: 10.146.177.18:7000
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:64.0)     Gecko/20100101 Firefox/64.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://10.146.177.18:7000/backoffice/contract /contract_config_edit.php4?Contract_ID=1463234
Content-Type: application/x-www-form-urlencoded
Content-Length: 4024
Authorization: Basic RWluaG9ybjpGZXVlcnphbmdlbmJvaGxlNTU0ISE/
Connection: keep-alive
Cookie:  _pk_id.7.1c19=5f552d1eb2170bab.1546180080.2.1546185355.1546184002.; session=.eJwtj1FKxTAQRddivt9Hkk5mJm8LLqJMJjdUxFbaPgTFvVvRz3PhwD1fYR47jiXcz_2BW5hfergHjTrIMlHxOrgSWh- NxNU0e67iEch5SpqaQaRxSz4oo1dzcRLNXcQ5Ugd4yMhVS8m9oVMt3pJpacw2UUEtrUfXaNQ7C DJaEw234Mc-5nN7xXr9YWdTBpJAY-KRMBVCKYYqrPEyJFav-fLe7Tg- tv234tnOTwhN_HTtjwP7X1z6p9XecKEtG5YV4fsHxkJOZg.Dw34rg.p2bNxLLF26aIXxth9VN7 BHA5x4U
Upgrade-Insecure-Requests: 1

Answer:

HTTP/1.0 200 OK
Content-Type: text/html
Access-Control-Allow-Origin: *
Vary: Cookie
Connection: close
Server: Werkzeug/0.14.1 Python/3.5.2
Date: Wed, 02 Jan 2019 08:15:38 GMT

Maybe it could be a problem with the cookies though it seems in the console it sends the correct cookie:

10.146.177.49 - - [02/Jan/2019 09:15:38] "POST /backoffice/contract/contract_config_edit.php4?Contract_ID=1463234 HTTP/1.1" 200 -

<RequestsCookieJar[<Cookie PHPSESSID=saqjj7n6m61aee19k3pe6moaf4 for    intern.something.com/>]>

Does anyone know what the problem is here?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
vato2
  • 63
  • 6
  • redirects from POSTs are awkward, have you read things like: https://softwareengineering.stackexchange.com/a/99899 ? – Sam Mason Jan 02 '19 at 11:26
  • Hi Sam, the problem is , that I didn't intend to do it like that, thats just what the site does I want to proxy. After submit on config_edit it redirects to config_show. But with https not http if you meant this? – vato2 Jan 02 '19 at 11:46
  • 1
    oh, so the redirect is happening because the POST completed "successfully"? I presume you need to forward more headers onto your target server, probably without changing them as much as you're doing. your `Authorization` and `Cookie` headers in particular look to be changing a lot, or you're not posting from the same thing. why not just forward them on directly rather doing e.g. `auth=(username, password)`? – Sam Mason Jan 02 '19 at 11:58
  • yes, after submission, do you have any idea how to forward the headers, did try it with session but this seems also not to work, thank you – vato2 Jan 02 '19 at 16:51
  • how hard did you search? this is the top result for "requests forward flask headers" https://stackoverflow.com/a/36601467/1358308 which looks pretty close – Sam Mason Jan 02 '19 at 16:54
  • Thank you , yes I already tried this one , doesent work with redirects and gives sometimes strange errors though to root paths etc... I will look further to find a solution, or maybe nginx could be a solution never tried ... – vato2 Jan 02 '19 at 17:00

0 Answers0