cypress icon indicating copy to clipboard operation
cypress copied to clipboard

Subtle Crypto not available in Firefox

Open j-berman opened this issue 4 years ago • 8 comments

Current behavior

Subtle Crypto (window.crypto.subtle) is undefined in Firefox 84.

First noticed my CI tests in Firefox were failing, then reproduced locally by updating my local Firefox (and Cypress) to the latest.

Desired behavior

Subtle crypto to be available in Firefox Cypress tests.

Test code to reproduce

it('Subtle crypto unavailable', function () {
    cy.visit('./cypress/integration/index.html').then(function (win) {
        expect(win.crypto).to.have.property('subtle')
    })
})

Versions

Failing CI Versions

Firefox 84 Linux Ubuntu - 18.04 Cypress - 4.12.1

Last known working CI Versions

Firefox 83 Linux Ubuntu - 18.04 Cypress - 4.12.1

Failing Local Versions

Firefox 84 Mac OS X - 10.14.6 Cypress - 6.2.1

Last known working local version

Firefox 78 Mac OS X - 10.14.6 Cypress - 6.2.1

Note

Subtle crypto is supposed to be available at localhost, as it's considered a secure context.

From MDN's docs on secure contexts:

Locally-delivered resources such as those with http://127.0.0.1 URLs, http://localhost and http://*.localhost URLs (e.g. http://dev.whatever.localhost/), and file:// URLs are also considered to have been delivered securely.

But it also has this note, which seems to be the culprit for the breaking change in Firefox 84, but I'm not really sure why:

Note: Firefox 84 and later support http://localhost and http://*.localhost URLs as trustworthy origins (earlier versions did not, because localhost was not guaranteed to map to a local/loopback address).

I have access to subtle crypto in Firefox 84 when serving my site from a webpack dev server at localhost, just not within Cypress for some reason.

I figure it has something to do with Cypress' browser environment being incompatible with Firefox 84:

Cypress launches the browser in a way that’s different from a regular browser environment. But it launches in a way that we believe makes testing more reliable and accessible.

j-berman avatar Jan 16 '21 12:01 j-berman

Throwing this out there too: if it's possible to run Cypress CI tests on different browser versions, that would be a nice workaround in the meantime. Haven't been able to figure that out, doesn't seem possible using the Cypress service

j-berman avatar Jan 18 '21 02:01 j-berman

@j-berman We have docker images where some different browser versions are provided, but we don't have a 'matrix' of browser versions to run against. https://github.com/cypress-io/cypress-docker-images

We proxy a lot of requests through Cypress, so yeah, I imagine there's something we're not taking into account in this situation.

Reproducible example

index.html

<html>
  <body>
    <h1>Hello world</h1>
    <script>
      console.log(window.crypto)
    </script>
  </body>
</html>

spec.js

it('Subtle crypto unavailable', () => {
  cy.visit('index.html').then((win) => {
    // Fails in Firefox 84
    expect(win.crypto).to.have.property('subtle')
  })
})

Firefox 81

Screen Shot 2021-01-20 at 3 58 26 PM

Firefox 84

Screen Shot 2021-01-20 at 4 00 34 PM

jennifer-shehane avatar Jan 20 '21 09:01 jennifer-shehane

I can find crypto.subtle on 127.0.0.1 in Firefox v84, but not on localhost.

samal-rasmussen avatar Feb 04 '21 23:02 samal-rasmussen

Same issue here on macOS with firefox 88 and a local server running on localhost:3000

cupcakearmy avatar May 09 '21 20:05 cupcakearmy

The similar issue

127.0.0.1:8080 --> ok 127.0.0.1:8081 --> ok localhost:8080 --> ok http://domain -----> ok https://domain ---> ok

But

192.168.0.xxx:8080 ---> undefined

fintechee avatar Mar 27 '22 18:03 fintechee

I'm also having this issue with Firefox 100, Ubuntu 20.04, Cypress 10.0.2 on localhost.

Both Electron and Chrome work normally. I can access subtle on Firefox from localhost outside of Cypress.

I tried to polyfill using @peculiar/webcrypto but wasn't able to get it working.

ShuPink avatar Jun 08 '22 04:06 ShuPink

I'm also seeing this in our CI as of late.

  • Firefox 101
  • Cypress 9.6.0
  • Xubuntu 21.10

con-cso avatar Jul 08 '22 11:07 con-cso

I just had the same problem, crypto.subtle not being available on localhost.

I found that the about:config setting network.proxy.allow_hijacking_localhost was set to true, but default is false. Resetting it to false made it work on localhost!

sisou avatar Nov 25 '22 11:11 sisou

I just had the same problem, crypto.subtle not being available on localhost.

I found that the about:config setting network.proxy.allow_hijacking_localhost was set to true, but default is false. Resetting it to false made it work on localhost!

I got same error on FF only, this fixed.

metawise avatar Jan 04 '23 05:01 metawise

I am facing this issue while visiting a RainbowKit v1.5 website at 192.168.1.XXX.

Interestingly, my issue is in Brave Browser, but Firefox works okay.

I do not get this issue on localhost when I am on the machine that is running the web server.

stoooops avatar Jan 16 '23 08:01 stoooops

Hi @all I'm facing the same problem on firefox for localhost. When I run it manually, the network.proxy.allow_hijacking_localhost is set to false. Only in Cypress, it is true. For me it worked as I changed the baseUrl key in cypres config from localhost:4200 to 127.0.0.1:4200

JanZellmer avatar Jan 27 '23 08:01 JanZellmer

I just had the same problem, crypto.subtle not being available on localhost.

I found that the about:config setting network.proxy.allow_hijacking_localhost was set to true, but default is false. Resetting it to false made it work on localhost!

hello how to set

NingXinHui avatar Apr 01 '23 09:04 NingXinHui

hello how to set

Type about:config in the location (address) bar and press the "Enter" key to open the about:config page, just like you open a website by typing the URL in the location bar.

Then search for the setting name in the config search bar.

sisou avatar Apr 01 '23 12:04 sisou

Looks like that network.proxy.allow_hijacking_localhost needs to be true though for Cypress to work correctly, doesn't seem to be the solution. If I turn it to false via browser launch options I don't see any cypress related things anymore in the browser window.

rolfspuler avatar Jun 21 '23 14:06 rolfspuler

i got issue with window.crypto.subtle error was window.crypto.subtle is undefined error was breaking login with msal (msal-angular). when i set "network.proxy.testing_localhost_is_secure_when_hijacked" to true, it started to work, so this part of code solved my issue:

setupNodeEvents(on) {
      on('before:browser:launch', (browser, launchOptions) => {
        if (browser.family === 'firefox') {
          // launchOptions.preferences is a map of preference names to values
          // login is not working in firefox when testing_localhost_is_secure_when_hijacked is false
          launchOptions.preferences['network.proxy.testing_localhost_is_secure_when_hijacked'] = true;
        }

        return launchOptions;
      });
    }

browser: firefox 114.0.2 application served to 127.0.0.1 (not possible to use localhost) baseUrl: http://localhost:<port>/

caesar1987sk avatar Jun 23 '23 11:06 caesar1987sk

It looks like that should not be necessary @caesar1987sk - we already do this by default: https://github.com/cypress-io/cypress/blob/f05f6236b9ae86f78cc0c40e492ec04e52438d56/packages/server/lib/browsers/firefox.ts#L442

I suspect serving vias 127.0.0.1 is what is fixing this for you? If you do that, but remove the

launchOptions.preferences['network.proxy.testing_localhost_is_secure_when_hijacked'] = true;

Is it causing errors?

lmiller1990 avatar Jun 30 '23 02:06 lmiller1990

I tried this out. ~~The workaround would be to use 127.0.0.1 for now, until we find out why localhost isn't working.~~ It looks like using this comment's recommendation it works. I wonder if https://github.com/cypress-io/cypress/blob/f05f6236b9ae86f78cc0c40e492ec04e52438d56/packages/server/lib/browsers/firefox.ts#L442 is getting overridden at some point. I'll take a look.

Not working:

it("runs", () => {
  cy.visit("http://localhost:8000");
  cy.window().then((win) => expect(win.crypto).to.have.property("subtle"));
 });

Working:

it("runs", () => {
  cy.visit("http://127.0.0.1:8000");
  cy.window().then((win) => expect(win.crypto).to.have.property("subtle"));
 });

If I add

setupNodeEvents(on) {
      on('before:browser:launch', (browser, launchOptions) => {
        if (browser.family === 'firefox') {
          // launchOptions.preferences is a map of preference names to values
          // login is not working in firefox when testing_localhost_is_secure_when_hijacked is false
          launchOptions.preferences['network.proxy.testing_localhost_is_secure_when_hijacked'] = true;
        }

        return launchOptions;
      });
    }

in cypress.config.js, it works with localhost. I'll dig into this.

lmiller1990 avatar Jun 30 '23 02:06 lmiller1990

it("runs", () => {
  cy.visit("http://127.0.0.1:8000");
  cy.window().then((win) => expect(win.crypto).to.have.property("subtle"));
 });

If I add

setupNodeEvents(on) {
      on('before:browser:launch', (browser, launchOptions) => {
        if (browser.family === 'firefox') {
          // launchOptions.preferences is a map of preference names to values
          // login is not working in firefox when testing_localhost_is_secure_when_hijacked is false
          launchOptions.preferences['network.proxy.testing_localhost_is_secure_when_hijacked'] = true;
        }

        return launchOptions;
      });
    }

in cypress.config.js, it works with localhost. I'll dig into this.

Adding this to my cypress config works for me. Thanks 👍🏼

cybermelons avatar Aug 18 '23 23:08 cybermelons

I did not end up figuring this out. Anyway, above work around works great.

lmiller1990 avatar Aug 20 '23 22:08 lmiller1990

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

cypress-app-bot avatar May 27 '24 01:05 cypress-app-bot

This issue has been closed due to inactivity.

cypress-app-bot avatar Jun 10 '24 01:06 cypress-app-bot