Skip to content

[Bug]: Electron How to avoid CORS-RFC1918 (private network)? #33133

Closed

Description

@tanch007

Preflight Checklist

Electron Version

16.0.5

What operating system are you using?

Windows

Operating System Version

windows 10 1909

What arch are you using?

x64

Last Known Working Electron version

No response

Expected Behavior

connect the websocket

Actual Behavior

Electron v16.0.5

My BrowserWindow uses loadURL, and the web page loaded calls api from another host. I cannot modify the web. The web works fine in normal browser like chrome, though it requires a little cross origin settings for chrome to work.

How can I make electron to avoid the cors policy ?

Here are the methods I've tried. All together, not one by one.

webSecurity: false
app.commandLine.appendSwitch('disable-features', 'OutOfBlinkCors')
app.commandLine.appendSwitch('disable-site-isolation-trials')
onBeforeSendHeaders, requestHeaders, add "Origin", "Access-Control-Request-Method", "Access-Control-Request-Private-Network"
onHeadersReceived, responseHeaders, add "Access-Control-Allow-Origin", "Access-Control-Allow-Private-Network"
Here is the error in the console of electron.

Access to XMLHttpRequest at 'http://xxxxxx.com' from origin 'http://yyyyyyy.com' has been blocked by CORS policy: The request client is not a secure context and the resource is in more-private address space private.
Once I adding the code below, it shows another error

app.commandLine.appendSwitch('disable-features', 'BlockInsecurePrivateNetworkRequests')
The error changes to

Access to XMLHttpRequest at 'http://xxxxxx.com' from origin 'http://yyyyyyy.com' has been blocked by CORS policy: Request had no target IP address space, yet the resource is in address space private.

Testcase Gist URL

No response

Additional Information

No response

Activity

  1. dheerajkysetti commented on Mar 26, 2022

    @dheerajkysetti

    @tanchaopeng Were you able to resolve this issue, which does not involve changing the way a website is served?

    I am facing a the same issue and appear to have exhausted options, I have currently rolled back to previous version of electron, which runs chromium version older than 98.

  2. sheng-di commented on Jun 13, 2022

    @sheng-di

    +1

  3. tanch007 commented on Aug 8, 2022

    @tanch007
    Author

    @dheerajkysetti you can add code

    app.commandLine.appendSwitch('disable-features','BlockInsecurePrivateNetworkRequests')

  4. dheerajkysetti commented on Nov 25, 2022

    @dheerajkysetti

    @dheerajkysetti you can add code

    app.commandLine.appendSwitch('disable-features','BlockInsecurePrivateNetworkRequests')

    Hi @tanchaopeng on electron version 21.3.0( amc and linux), this issues still exists and the disable features switch didnt work.

    The same works on electron version 19.0.13.

    is this bug being tracked anywhere?
    @electron-triage

  5. MounicaChanda0809 commented on Nov 29, 2022

    @MounicaChanda0809

    Hi still seeing this issue. I am on electron 21.1.1. Tried all appendSwitch sugesstions

      app.commandLine.appendSwitch('disable-features', 'OutOfBlinkCors')
      app.commandLine.appendSwitch('disable-site-isolation-trials') 
      app.commandLine.appendSwitch('disable-features','BlockInsecurePrivateNetworkRequests')
    
  6. flavinlouise commented on Dec 17, 2022

    @flavinlouise

    have same issue in 21.2.0

  7. AZforyo commented on Jan 4, 2023

    @AZforyo

    @tanchaopeng Have you solved the problem? please tell me how to do!!!!
    求教!

  8. AZforyo commented on Jan 4, 2023

    @AZforyo

    Hi still seeing this issue. I am on electron 21.1.1. Tried all appendSwitch sugesstions

      app.commandLine.appendSwitch('disable-features', 'OutOfBlinkCors')
      app.commandLine.appendSwitch('disable-site-isolation-trials') 
      app.commandLine.appendSwitch('disable-features','BlockInsecurePrivateNetworkRequests')
    

    Have you solved the problem? please tell me how to do!!!!

  9. cnhuye commented on Mar 12, 2023

    @cnhuye

    I have same problem on electron 20.1.3, this DONT work for me :

    app.commandLine.appendSwitch('disable-features','BlockInsecurePrivateNetworkRequests')
    

    Following this answer, #37435 (comment),
    change the electron to 19.0.13, it worked.

  10. raymon610 commented on Aug 28, 2023

    @raymon610

    I have same problem on electron 20.1.3, this DONT work for me :

    app.commandLine.appendSwitch('disable-features','BlockInsecurePrivateNetworkRequests')
    

    Following this answer, #37435 (comment), change the electron to 19.0.13, it worked.

    app.commandLine.appendSwitch('disable-web-security');
    on electron 25.0.1 it work

  11. dtychshenko commented on Oct 17, 2023

    @dtychshenko

    Beware that disable-web-security flag will disable all CORS checks among a few other things besides CORS-RFC1918 Private Network Access (PNA) checks. For us, disabling all of web security was too lax and we wanted to disable only PNA temporarily.

    Also note that the way the related PNA flags behave can vary greatly between different versions of Chromium, which is why disabling BlockInsecurePrivateNetworkRequests flag alone works for some, but not others. In our case, I had to dig through the Chromium source code of the version used by the Electron version that we use to figure out how to disable PNA checks.

    So in our case using Electron 22.3.15 which uses Chrome 108.0.5359.215, the following will disable PNA/CORS-RFC1918:

    app.commandLine.appendSwitch('disable-features', 'BlockInsecurePrivateNetworkRequests,PrivateNetworkAccessSendPreflights');

    There is also an option to override the specific IP addresses to a particular space; however, the IP addresses specified have to be exact, i.e. you can't override ranges or subnets:

    app.commandLine.appendSwitch('ip-address-space-overrides', '158.11.22.33:443=local,10.45.56.67:443=public,127.0.0.1:8080=private');

    In this example, when PNA check is done, a resource with IP address 158.11.22.33 on port 443 will be considered to be in local space, rather than public. Similarly, 10.45.56.67 is generally a private address, but we can override it to be considered public.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions