cypress: Subtle Crypto not available in Firefox

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.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 6
  • Comments: 19 (4 by maintainers)

Commits related to this issue

Most upvoted comments

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.

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’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.

@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

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>/

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

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