Endpoints and Payloads - CORS

Cross-Origin Resource Sharing (CORS)

Two Main Components

  • Security and the Same-Origin Policy
    (This is a policy that says that a web browser allows scripts in one page to access data in another page but only if they have the same origin. This policy protect us from rogue Javascript that will try to alter our data from another website.)
  • Block requests from rogue JavaScript

The same-origin policy is a concept of web security that allows scripts in Webpage 1 to access data from Webpage 2 only if they share the same domain. This means that the errors will be raised in the following cases:

  • Different domains
  • Different subdomains (example.com and api.example.com)
  • Different ports (example.com and example.com:1234)
  • Different protocols (http://example.com and https://example.com)

The CORS is behaving exactly as it should. This policy is there to protect you and your users. For instance, attackers may embed malicious scripts in advertisements. This policy prevents those scripts from successfully making requests to your bank’s website as you access the website hosting the advertisement.

If you’re sending any requests beyond very simple GET or POST requests, then before your actual request is sent, the browser sends a preflight OPTIONS request to the server. If CORS is not enabled, then the browser will not respond properly and the actual request will not be sent.


CORS Headers
How does CORS handle that options preflight request?
Answer: It does so using HTTP response headers. It sets certain headers on the response to basically inform the client that you have passed and you may proceed.

In order for the requests to be processed properly, CORS utilizes headers to specify what the server will allow:

HeaderDescription
Access-Control-Allow-OriginWhat client domains can access its resources. For any domain use *
Access-Control-Allow-CredentialsOnly if using cookies for authentication - in which case its value must be true
Access-Control-Allow-MethodsList of HTTP request types allowed
Access-Control-Allow-HeadersList of HTTP request header values the server will allow, particularly useful if you use any custom headers

Cross-Origin Resource Sharing (CORS)

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值