-
Notifications
You must be signed in to change notification settings - Fork 395
poc: Create passing JS context for PSP #20740
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
base: develop
Are you sure you want to change the base?
Conversation
🚨 PR Title Validation Failed 🚨Your pull request title does not follow the required format. Please update it to match the expected pattern: Expected format: Allowed Types
Example of a valid PR title✅ ❌ Merge is blocked until the PR title is corrected. |
Function(script[0].innerText)(); | ||
|
||
const originalScript = script[0].innerText; | ||
const sessionId = this.generateSessionId(); |
Check failure
Code scanning / CodeQL
Insecure randomness High
Math.random()
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 19 days ago
To fix the problem, we should replace the usage of Math.random()
in generateSessionId()
with a call to a cryptographically secure random number generator. In Node.js and modern browsers, the crypto
module provides such functionality. To maintain compatibility with the rest of the code and stay within the shown region, we should import Node's built-in crypto
module and use crypto.randomBytes()
to generate a random string value for the session ID. The rest of the function can remain unchanged.
In file integration-libs/opf/base/root/services/opf-resource-loader.service.ts
, we need to:
- Add an import for Node's
crypto
module. - Replace line 246 so that instead of using
Math.random().toString(36).substring(2)
, we use a securely generated random string, e.g., fromcrypto.randomBytes(16).toString('hex')
.
-
Copy modified line R11 -
Copy modified line R247
@@ -8,6 +8,7 @@ | ||
import { Injectable, PLATFORM_ID, inject } from '@angular/core'; | ||
import { Config, ScriptLoader } from '@spartacus/core'; | ||
|
||
import * as crypto from 'crypto'; | ||
import { | ||
OpfDynamicScriptResource, | ||
OpfDynamicScriptResourceType, | ||
@@ -243,7 +244,7 @@ | ||
*/ | ||
private generateSessionId(): string { | ||
const timestamp = Date.now(); | ||
const random = Math.random().toString(36).substring(2); | ||
const random = crypto.randomBytes(16).toString('hex'); | ||
return `opf-session-${timestamp}-${random}`; | ||
} | ||
|
Closes: CXSPA-10980