Teknologi

CORS: Why Browsers Block Requests and How to Fix it.

45
×

CORS: Why Browsers Block Requests and How to Fix it.

Sebarkan artikel ini

Imagine this: you run a coffee shop called BrewCafe, and you need some ingredients from a supplier called CoffeeBeansInc. You send a request for beans, but they don’t respond. Why? Because the supplier doesn’t recognize you and won’t share their beans without permission.

This is the basic idea behind CORS (Cross-Origin Resource Sharing). Let’s break it down step by step!

What is CORS?

CORS is a security rule built into browsers. It ensures that one website cannot access data from another website without permission.

The browser will block a request if:

  1. The origin of the request (e.g., brewcafe.com) is different from the origin of the server receiving the request (e.g., api.coffeebeansinc.com).
  2. The server hasn’t granted permission to share the data.

The browser acts as a gatekeeper, making sure that only authorized requests can go through.

Example Problem

  1. BrewCafe is hosted at https://brewcafe.com.
  2. They try to fetch ingredient data from their supplier’s API at https://api.coffeebeansinc.com.
  3. The browser notices that the origins (the domain names) are different.

As a result, the browser blocks the request with an error like this:

Access to fetch at ‘https://api.coffeebeansinc.com’ from origin ‘https://brewcafe.com’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present.

This means the server at CoffeeBeansInc didn’t provide the necessary permission for BrewCafe to access the data.

Why is CORS Important?

CORS is a safety feature. Without it, malicious websites could abuse a browser’s ability to make requests.

For example, imagine you’re logged into your online banking account (banking.com). Without CORS, a harmful website could potentially send requests to banking.com using your credentials, accessing sensitive information or transferring money without your permission.

CORS helps protect users from this kind of threat by enforcing a rule called the Same-Origin Policy. This rule says:

  • Only websites from the “same house” (same domain, protocol, and port) can exchange data.

How Does CORS Work?

For BrewCafe to successfully access data from CoffeeBeansInc, the server at CoffeeBeansInc needs to say, “Yes, it’s okay for you to fetch this data.”

The server grants permission by sending a special note called a CORS header.

How to Fix a CORS Error

When you encounter a CORS issue, it means the server isn’t sending the necessary permissions. Here’s how to fix it:

1. Add the Right Headers on the Server

The server needs to send headers to grant permission:

Allow any origin:

Access-Control-Allow-Origin: *

This allows requests from any website.

Allow only specific origins:

Access-Control-Allow-Origin: https://brewcafe.com

This is more secure, as it only allows a specific website to access the data.

2. Handle Preflight Requests

When making more complex requests (such as POST, PUT, or custom headers), the browser sends a preflight request before the actual request. This is done using the OPTIONS method. It’s like asking the server, “Hey, are you okay with this request?”

For example, if BrewCafe tries to send a POST request with custom headers, the browser first sends an OPTIONS request to CoffeeBeansInc to check if the server allows such requests.

The server needs to respond with the following headers to indicate it’s okay to proceed:

Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: Content-Type, Authorization

This tells the browser that the server is okay with the methods (GET, POST, OPTIONS) and headers (Content-Type, Authorization) the frontend intends to use.

3. Use a Proxy for Local Development

During local development, if the backend server isn’t yet set up to handle CORS, you can use a proxy. This acts as a middleman to forward requests from your frontend to the backend, making it appear as though they are from the same origin.

CORS Can’t Be Bypassed

While you might be tempted to use browser extensions or modify browser settings to bypass CORS during development, these are not solutions for production environments.

The proper way to fix CORS issues is to configure the server correctly to handle the requests.

Conclusion

CORS is a mechanism that protects your data while making sure that only authorized websites can interact with your server.

If you encounter a CORS error:

  1. Don’t panic — this is not a bug in your browser.
  2. Fix it by adding the correct headers on the server.
  3. Understand that CORS is an important security measure for protecting users online.

With the right setup, communication between BrewCafe and CoffeeBeansInc will run smoothly, and you can get the ingredients you need without any issues! ☕️

Additional Notes

Be cautious when using Access-Control-Allow-Origin: * in production environments. Allowing all origins to access your server can be risky, as it opens the door to any website making requests to your server, potentially leading to security vulnerabilities. It’s generally safer to specify trusted domains that are allowed to make requests.

Baca Juga :  Tony Blair Bertemu Menkomdigi: Bahas AI, E-SIM, dan mMasa Depan Talenta Digital RI