Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[middlewares] globalThis and globalThis.CryptoKey is missing #30475

Closed
panva opened this issue Oct 27, 2021 · 15 comments · Fixed by #31193
Closed

[middlewares] globalThis and globalThis.CryptoKey is missing #30475

panva opened this issue Oct 27, 2021 · 15 comments · Fixed by #31193
Assignees
Labels
bug Issue was opened via the bug report template. Middleware Related to Next.js Middleware

Comments

@panva
Copy link
Contributor

panva commented Oct 27, 2021

What version of Next.js are you using?

12.0.1

What version of Node.js are you using?

v14.18.0

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Both Vercel and next dev

Describe the Bug

Testing a universal JOSE (JWT/JWK/etc) library on Middlewares I've encountered an issue where the CryptoKey constructor is not exposed via globalThis.CryptoKey.

This is the case for all browsers, Deno, and Cloudflare Workers runtimes but not for Middlewares deployed via vercel or running via next dev.

Not having access to this constructor prohibits my particular library from validating user inputs.

In next dev globalThis.CryptoKey is missing, in vercel.com runtime globalThis is missing completely.

Expected Behavior

globalThis, globalThis.CryptoKey, and CryptoKey are exposed both running via next dev or deployed via vercel.com

To Reproduce

Create a middleware like so, both in development as well as vercel.com, observe the failures are there and also, different.

export default async function middleware(req, ev) {
  try {
    if (typeof CryptoKey === 'undefined') {
      throw new Error('CryptoKey is missing')
    }
    if (typeof globalThis === 'undefined') {
      throw new Error('globalThis is missing')
    }
    if (!globalThis.CryptoKey) {
      throw new Error('globalThis.CryptoKey is missing')
    }
    return new Response(JSON.stringify({}), { status: 200 });
  } catch (err) {
    return new Response(JSON.stringify({ error: err.stack }), { status: 400 });
  }
}
@panva panva added the bug Issue was opened via the bug report template. label Oct 27, 2021
@panva panva changed the title [middlewares] globalThis.CryptoKey is missing [middlewares] globalThis and globalThis.CryptoKey is missing Oct 27, 2021
@timneutkens timneutkens added the Middleware Related to Next.js Middleware label Oct 31, 2021
@panva
Copy link
Contributor Author

panva commented Nov 1, 2021

Note that CryptoKey's WebIDL defines CryptoKey as exposed via globals. See https://w3c.github.io/webcrypto/#cryptokey-interface

@javivelasco
Copy link
Member

We're bringing in globalThis to expose the global env, PR coming.

@panva
Copy link
Contributor Author

panva commented Nov 2, 2021

@javivelasco globalThis is just one portion, but a great start nontheless!

@NateRadebaugh
Copy link

Looks like globalThis made it into v12.0.3-canary.3

Does CryptoKey come for free or another PR needed for that?

@panva
Copy link
Contributor Author

panva commented Nov 5, 2021

It does not come for free ;)

@javivelasco
Copy link
Member

We are adding it in the next canary

@panva
Copy link
Contributor Author

panva commented Nov 5, 2021

export async function middleware(req, ev) {
  return new Response(JSON.stringify({
    globalThisPresent: typeof globalThis !== 'undefined',
    CryptoKeyInGlobalThis: typeof globalThis !== 'undefined' && typeof globalThis.CryptoKey !== 'undefined',
    CryptoKey: typeof CryptoKey !== 'undefined'
  }), { status: 200 });
}

State as of 12.0.3-canary.9

next dev

{
  "globalThisPresent": true,
  "CryptoKeyInGlobalThis": false,
  "CryptoKey": false
}

Edge Functions

{
  "globalThisPresent": false,
  "CryptoKeyInGlobalThis": false,
  "CryptoKey": false
}

@panva
Copy link
Contributor Author

panva commented Nov 9, 2021

#31193 (comment)

This is not fixed on Edge Functions still as per my previous comment in this issue.

@Kikobeats
Copy link
Member

Kikobeats commented Nov 9, 2021

@panva right, no problem. A new runtime version should be released paired with the next canary, let me ping you when is done 🙂

@panva
Copy link
Contributor Author

panva commented Nov 9, 2021

Right. But the release of 12.0.3 didnt align globalThis on edge functions so i'm wondering how this works...

@panva
Copy link
Contributor Author

panva commented Nov 10, 2021

Same with the new canary - locally its good now, on vercel.com - not. https://my-next-app-panva.vercel.app

@ijjk
Copy link
Member

ijjk commented Nov 10, 2021

@panva this has now been rolled to stable on Vercel, please re-deploy and give a try!

@NateRadebaugh
Copy link

LGTM from the prior URL:

image

@panva
Copy link
Contributor Author

panva commented Nov 10, 2021

Yup!

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 27, 2022
natew pushed a commit to natew/next.js that referenced this issue Feb 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. Middleware Related to Next.js Middleware
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants