Skip to content

Commit 88be40d

Browse files
authored
Merge pull request #7073 from topcoder-platform/pm-717
fix(PM-717): use crypto random instead of math random
2 parents bef0398 + 91920df commit 88be40d

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

src/shared/components/Contentful/Article/Article.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import IconFacebook from 'assets/images/icon-facebook.svg';
3434
import IconTwitter from 'assets/images/icon-twitter.svg';
3535
import IconLinkedIn from 'assets/images/icon-linkedIn.svg';
3636
import DiscordIconWhite from 'assets/images/tc-edu/discord-icon-white.svg';
37+
import getSecureRandomIndex from 'utils/secureRandom';
3738

3839
const htmlToText = require('html-to-text');
3940

@@ -45,7 +46,7 @@ const LOCAL_STORAGE_KEY = 'VENBcnRpY2xlVm90ZXM=';
4546
const DEFAULT_BANNER_IMAGE = 'https://images.ctfassets.net/piwi0eufbb2g/7v2hlDsVep7FWufHw0lXpQ/2505e61a880e68fab4e80cd0e8ec1814/0C37CB5E-B253-4804-8935-78E64E67589E.png?w=1200&h=630';
4647
// random ads banner - left sidebar
4748
const RANDOM_BANNERS = ['6G8mjiTC1mzeSQ2YoUG1gB', '1DnDD02xX1liHfSTf5Vsn8', 'HQZ3mN0rR92CbNTkKTHJ5', '1OLoX8ZsvjAnn4TdGbZESD', '77jn01UGoQe2gqA7x0coQD'];
48-
const RANDOM_BANNER = RANDOM_BANNERS[_.random(0, 4)];
49+
const RANDOM_BANNER = RANDOM_BANNERS[getSecureRandomIndex(RANDOM_BANNERS.length)];
4950

5051
class Article extends React.Component {
5152
componentDidMount() {

src/shared/components/Contentful/MemberTalkCloud/MemberTalkCloud.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import PT from 'prop-types';
99
import React from 'react';
1010
import { themr } from 'react-css-super-themr';
1111
import { fixStyle } from 'utils/contentful';
12+
import getSecureRandomIndex from 'utils/secureRandom';
1213
import defaultTheme from './themes/default.scss';
1314

1415
const MAX_MARGIN_TOP = 0;
@@ -17,7 +18,7 @@ const MAX_MARGIN_LEFT = 30;
1718

1819
const getRandomTranslate = () => ({
1920
y: MAX_MARGIN_TOP,
20-
x: _.random(MIN_MARGIN_LEFT, MAX_MARGIN_LEFT, false),
21+
x: getSecureRandomIndex(MIN_MARGIN_LEFT, MAX_MARGIN_LEFT),
2122
});
2223

2324
export class MemberTalkCloud extends React.Component {

src/shared/components/challenge-listing/placeholders/ChallengeCard/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const ChallengeCardPlaceholder = ({ id }) => (
3636
);
3737

3838
ChallengeCardPlaceholder.defaultProps = {
39-
id: Math.random(),
39+
id: 0,
4040
};
4141

4242
ChallengeCardPlaceholder.propTypes = {

src/shared/utils/secureRandom.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const getCryptoLibrary = () => {
2+
if (typeof window !== 'undefined' && window.crypto) {
3+
return window.crypto;
4+
}
5+
/* eslint-disable global-require */
6+
const nodeCrypto = require('crypto');
7+
return nodeCrypto;
8+
};
9+
10+
export default function (min, max) {
11+
const crypto = getCryptoLibrary();
12+
const random = new Uint32Array(1);
13+
if (typeof crypto.getRandomValues === 'function') {
14+
crypto.getRandomValues(random);
15+
} else if (typeof crypto.randomFillSync === 'function') {
16+
crypto.randomFillSync(random);
17+
}
18+
19+
if (!max) {
20+
return random[0] % min;
21+
}
22+
23+
const range = max - min + 1;
24+
return min + (random[0] % range);
25+
}

0 commit comments

Comments
 (0)