Bypassing CORS with Electron

Why bypass CORS?

Story time. So recently I was developing an RSS Reader application. Now the application was written purely in React and it was supposed to be deployed as a GitHub.io website where you could add the feeds you were interested in and read articles.

  1. Asking the user to disable Browser based security, which would led to no one using my site (myself included)
  2. Use Electron

A bad but popular way to bypass CORS

Using webSecurity: false as a way to disable the feature in Electron.
Although it bypasses CORS, it also sort of opens a Pandora’s Box which is best avoided.

How to bypass CORS?

It is very simple to bypass CORS

How does it work?

Image source: Victoria Lo and Nick Ramirez

Wait a second. I know how to bypass CORS

What if we made ESPNCricInfo recognize our website?
Like that’s ever gonna happen (for me).

Image source: Nick Ramirez

Bypassing CORS for Headers

  1. Let’s say you wish to use your own headers
  2. But the server has not yet said yes to those headers for CORS purposes
  3. For example, ESPNCricInfo does not allow my testing field “hi”
  4. But let’s say you know their CORS is wrong and wish to send the header anyways
  5. So we need to fix Access-Control-Allow-Headers

They use different cases Mr Bean

Instead of ‘Access-Control-Allow-Origin’, they use ‘access-control-allow-origin’.

Looking at you browsers. Why so accommodating?
  1. Define our key of interest
  2. Lower case it
  3. Look at all lower cased keys till we find ours
  4. Replace it
  5. Terminate loop
  6. If 3 was never true, insert a new key

The new CORS bypass

Do we need to customize Fetch for this?

No no no no

TypeScript Compatibility

Yes indeed this code is indeed compatible in TypeScript

Credits

  1. Jerold Wilson How do you handle CORS in an electron app? — Stack Overflow
    The answer here forms a base but is not correct for our use case which is to bypass CORS for all websites. Further, our code handles the error mentioned by serg06.
  2. peade he for mentioning the need to support bypassing CORS Headers

How did you bypass CORS?

You can tell me either via comments or via LinkedIn in case you don’t have a Medium account