-
Notifications
You must be signed in to change notification settings - Fork 387
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
base: develop
Are you sure you want to change the base?
Conversation
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@@ -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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
spartacus Run #45564
Run Properties:
|
Project |
spartacus
|
Branch Review |
CXSPA-8659_randomness
|
Run status |
Passed #45564
|
Run duration | 11m 53s |
Commit |
86d9c79fc2 ℹ️: Merge 195b30c58d5339dba840f8c0b068d7362584cea1 into 052291b89bfc5e1626128092e510...
|
Committer | Giancarlo Cordero Ortiz |
View all properties for this run ↗︎ |
Test results | |
---|---|
Failures |
0
|
Flaky |
3
|
Pending |
2
|
Skipped |
0
|
Passing |
125
|
Upgrade your plan to view test results. | |
View all changes introduced in this branch ↗︎ |
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 usecrypto.getRandomValues
to generate secure random values. This method provides a cryptographically secure way to generate random numbers.We will:
crypto
module.Math.random()
call withcrypto.getRandomValues
to generate a secure random value.Suggested fixes powered by Copilot Autofix. Review carefully before merging.