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

fix(amazon-cognito-identity-js): set crypto for Node #7136

Merged
merged 3 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const crypto = require('crypto');

describe('cryptoSecureRandomInt test', () => {
let windowSpy;

beforeEach(() => {
jest.resetModules();
windowSpy = jest.spyOn(window, 'window', 'get');
});

afterEach(() => {
windowSpy.mockRestore();
});

test('crypto is set for window (browser)', () => {
windowSpy.mockImplementation(() => ({
crypto: {
getRandomValues: () => [12345],
},
}));

const cryptoSecureRandomInt = require('../src/utils/cryptoSecureRandomInt')
.default;
expect(window.crypto).toBeTruthy();
expect(cryptoSecureRandomInt()).toBe(12345);
});

test('crypto is set for window (IE 11)', () => {
windowSpy.mockImplementation(() => ({
crypto: undefined,
msCrypto: {
getRandomValues: () => [67890],
},
}));

const cryptoSecureRandomInt = require('../src/utils/cryptoSecureRandomInt')
.default;
expect(window.msCrypto).toBeTruthy();
expect(cryptoSecureRandomInt()).toBe(67890);
});

test('crypto is set for Node', () => {
windowSpy.mockImplementation(() => ({
crypto: null,
}));

const randomBytesMock = jest
.spyOn(crypto, 'randomBytes')
.mockImplementationOnce(() => ({
readInt32LE: jest.fn().mockReturnValueOnce(54321),
}));

const cryptoSecureRandomInt = require('../src/utils/cryptoSecureRandomInt')
.default;
expect(cryptoSecureRandomInt()).toBe(54321);

randomBytesMock.mockRestore();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ if (!crypto && typeof global !== 'undefined' && global.crypto) {
crypto = global.crypto;
}

// Native crypto import via require (NodeJS)
if (!crypto && typeof require === 'function') {
try {
crypto = require('crypto');
} catch (err) {}
}

Comment on lines +18 to +24
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is based on how crypto-js handles this case: https://github.com/brix/crypto-js/blob/develop/src/core.js#L25-L30

/*
* Cryptographically secure pseudorandom number generator
* As Math.random() is cryptographically not safe to use
Expand Down
3 changes: 3 additions & 0 deletions packages/amazon-cognito-identity-js/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ var config = {
devtoolModuleFilenameTemplate: require('../aws-amplify/webpack-utils')
.devtoolModuleFilenameTemplate,
},
externals: {
crypto: 'crypto',
},
Comment on lines +39 to +41
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed for the build:umd to successfully build due to require('crypto'). However, I'm not even sure if the UMD build is even used anymore since webpack outputs the build to dist and we have "main": "lib/index.js" in package.json. With that said, removing the UMD build is out of scope for this PR.

plugins: [
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.BannerPlugin({ banner, raw: true }),
Expand Down