-
Notifications
You must be signed in to change notification settings - Fork 32
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
ReferenceError: window is not defined when importing CryptoES with the newer 2.0.0 version #34
Comments
Commonly speaking his should not happen, because a js env should at least has one of globalThis/global/window and has crypto in it. But in case of special cases like yours, I wrapped this assignment in try/catch(newly published in v2.0.1). This will rollback it to insecure Math.random. |
What do you mean by "special case"? Is there something I can do to qualify not to resort back to math.random? |
In a standard node or browser env, There should be a But if your env doesn't has any of these variable (like in raw ReactNative), secure random won't assign successfully, Math.random will be used. So run a |
in chrome70, globalThis is missing, but window.crypto.getRandomValues is still existent if you use you should use const crypto =
(typeof globalThis != 'undefined' ? globalThis : void 0)?.crypto ||
(typeof global != 'undefined' ? global : void 0)?.crypto ||
(typeof window != 'undefined' ? window : void 0)?.crypto ||
(typeof self != 'undefined' ? self : void 0)?.crypto ||
(typeof frames != 'undefined' ? frames : void 0)?.[0]?.crypto; |
@lisonge @nickolasdeluca |
Just tried I'm curious, why didn't this happen with Just trying to understand what happened to maybe try to find an answer, because I feel that since Math.Random is not cryptographically safe, It's not safe for me to keep using it... |
Because in CryptoES 1.2.7, there is only Math.random, the secure random is added in 2.0.0 recently (after I thought globalThis is popular enough) Secure random is added to CryptoJS 4.0.0 in 2020, before that it's also only with Math.random. Now in it if you don't have global crypto it will error without rolling back. The logic of assignment is same in same in CryptoJS and CryptoES, so it shouldn't that you can get a global crypto variable in CryptoJS but ont in CryptoES. If it really happens, please debug with break points to find out where is the problem and share with us. |
I just debugged the // Native crypto import via require (NodeJS)
if (!crypto && typeof require === 'function') {
try {
crypto = require('crypto');
} catch (err) {}
} I have the const crypto = require("crypto-js"); And my test route: fastify.get(
"/test",
{
schema: {
tags: ["test"],
response: {
200: {
type: "object",
properties: {
message: { type: "string" },
},
},
},
},
},
async (request, reply) => {
let aes = crypto.AES.encrypt("Test", configuration.serverCryptoKey);
reply.send({
message: aes,
});
}
); Was this what you were looking for? |
globalThis is added to nodejs in v19.
We didn't adopt It's also simple to solve your problem, that is to use a polyfill of globalThis with crypto before importing CryptoES, I suggest this one: https://github.com/zloirock/core-js#ecmascript-globalthis |
I don't like the idea to depend on yet another package to polyfill the global env variable. |
Whenever I import
CryptoES
and rebuild my API it throws this error:Using
crypto-es
version2.0.0
.Project is a backend API using
Fastify
, pure JS, no TypeScript involved.Just tested with previous
1.2.7
version and this error isn't thrown.Can provide more details if needed, just let me know what you need.
The text was updated successfully, but these errors were encountered: