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

explanation #47

Open
stern0m1 opened this issue Nov 2, 2017 · 4 comments
Open

explanation #47

stern0m1 opened this issue Nov 2, 2017 · 4 comments

Comments

@stern0m1
Copy link

stern0m1 commented Nov 2, 2017

If someone could explain me how this works I would appreciate it.
Thanks

@vitvad
Copy link
Owner

vitvad commented Nov 2, 2017

Read this first https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

Then small addition from me:

We have [CLIENT] <---> [BROWSER] <---> [SERVER]

Example: We want to request data from CLIENT (http://mysupersite.com) with AJAX on SERVER (http://thirdparty-api.com/hack-the-bank/)

How it was and how it is now:

  1. XMLHttpRequest v1 - we can't do this with ajax
  2. XMLHttpRequest v2 - we can do this if server has header (Access-Control-Allow-Origin: http://mysupersite.com ) and client will send header (Origin: http://mysupersite.com)
    2.1) When user send ajax request browser always add automatically Origin: header with client host

Browser and server perform checks if client can request data:

  • server could check that request come with header Origin and if this header contain allowed site could respond with data
  • browser check that server respond has header Access-Control-Allow-Origin: http://mysupersite.com
    and allow client to send process request.

to make CORS request client actually do 2 requests:

  1. Preflighted request https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Preflighted_requests
    which ask server if I can request data from it at all, and what METHOD server support.
    see also https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS#Preflighted_requests_in_CORS

NOTE: if Preflighted request respond with:
Access-Control-Allow-Origin: http://mysupersite.com
then browser allow to send real request from that address to get data from (SERVER)

  1. real request will have header Origin:

So I just intercept requests and response to modify them in extension.

For example we want to get data from server http://server.com on client http://cheater.com, but server allow requests only from http://goodboy.com. When user will send ajax request from cheater.com browser will add automatically Origin: http://cheater.com

In extension I intercept:

  • preflight request and respond with Access-Control-Allow-Origin: http://cheater.com to fool browser
  • real request and rewrite Origin: http://cheater.com to Origin: http://goodboy.com to fool server

PS. server could have additional checks - in this case you will not get response from server

something like this... if I remember it correctly :)

@stern0m1
Copy link
Author

stern0m1 commented Nov 3, 2017 via email

@yancongwen
Copy link

thanks

@MNF
Copy link

MNF commented Jan 31, 2019

Could you please copy the answer to Readme.md ?

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