Coder Social home page Coder Social logo

explanation about access-control-allow-origin HOT 4 OPEN

vitvad avatar vitvad commented on August 17, 2024
explanation

from access-control-allow-origin.

Comments (4)

vitvad avatar vitvad commented on August 17, 2024 1

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 :)

from access-control-allow-origin.

stern0m1 avatar stern0m1 commented on August 17, 2024

from access-control-allow-origin.

yancongwen avatar yancongwen commented on August 17, 2024

thanks

from access-control-allow-origin.

MNF avatar MNF commented on August 17, 2024

Could you please copy the answer to Readme.md ?

from access-control-allow-origin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.