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

GHAS. Fix Insecure randomness #19398

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
Draft

Conversation

giancorderoortiz
Copy link
Contributor

Fixes https://github.com/SAP/spartacus/security/code-scanning/49

To fix the problem, we need to replace the use of Math.random() with a cryptographically secure random number generator. In a browser environment, we can use crypto.getRandomValues to generate secure random values. This method provides a cryptographically secure way to generate random numbers.

We will:

  1. Import the necessary crypto module.
  2. Replace the Math.random() call with crypto.getRandomValues to generate a secure random value.
  3. Convert the generated random value to a string format similar to the original implementation.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@giancorderoortiz giancorderoortiz changed the title Fix code scanning alert no. 49: Insecure randomness GHAS. Fix Insecure randomness Oct 15, 2024
@@ -85,7 +85,9 @@ export class MultiCartService implements MultiCartFacade {
* Simple random temp cart id generator
*/
protected generateTempCartId(): string {
const pseudoUuid = Math.random().toString(36).substring(2, 11);
const array = new Uint32Array(1);
window.crypto.getRandomValues(array);
Copy link
Contributor

Choose a reason for hiding this comment

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

I would be careful with window as it could potentially break the SSR rendering process.

Copy link
Contributor

Choose a reason for hiding this comment

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

The function is triggered when a new cart is created, which is unlikely to occur during the rendering process. However, it is recommended to check if the window object exists before using it.

Copy link
Contributor

Choose a reason for hiding this comment

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

as per @Platonn comment I've switched window with globalThis.

Copy link
Contributor

@kpawelczak kpawelczak Oct 23, 2024

Choose a reason for hiding this comment

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

Reverted globalThis.crypto as it didn't work in SSR.

@kpawelczak kpawelczak marked this pull request as ready for review October 24, 2024 07:16
@kpawelczak kpawelczak requested a review from a team as a code owner October 24, 2024 07:16
Copy link

cypress bot commented Oct 24, 2024

spartacus    Run #45564

Run Properties:  status check passed Passed #45564  •  git commit 86d9c79fc2 ℹ️: Merge 195b30c58d5339dba840f8c0b068d7362584cea1 into 052291b89bfc5e1626128092e510...
Project spartacus
Branch Review CXSPA-8659_randomness
Run status status check passed Passed #45564
Run duration 11m 53s
Commit git commit 86d9c79fc2 ℹ️: Merge 195b30c58d5339dba840f8c0b068d7362584cea1 into 052291b89bfc5e1626128092e510...
Committer Giancarlo Cordero Ortiz
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 3
Tests that did not run due to a developer annotating a test with .skip  Pending 2
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 125
⚠️ You've recorded test results over your free plan limit.
Upgrade your plan to view test results.
View all changes introduced in this branch ↗︎

@github-actions github-actions bot marked this pull request as draft October 30, 2024 11:08
@kpawelczak kpawelczak marked this pull request as ready for review October 30, 2024 13:43
@github-actions github-actions bot marked this pull request as draft November 5, 2024 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants