mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	chore: Better randomness (#6755)
## About the changes This change is irrelevant as it doesn't pose a security risk, but there's no reason for us not to use a different type of random generation for the `sessionId` **Note:** the magic number 18 was picked because that's the length of the string we get from `String(Math.random())` Closes https://github.com/Unleash/unleash/security/code-scanning/68 and https://github.com/Unleash/unleash/security/code-scanning/69
This commit is contained in:
		
							parent
							
								
									d5b73694fa
								
							
						
					
					
						commit
						e845459034
					
				@ -1,9 +1,7 @@
 | 
				
			|||||||
// Copy of https://github.com/Unleash/unleash-proxy/blob/main/src/create-context.ts.
 | 
					// Copy of https://github.com/Unleash/unleash-proxy/blob/main/src/create-context.ts.
 | 
				
			||||||
 | 
					import crypto from 'crypto';
 | 
				
			||||||
/* eslint-disable prefer-object-spread */
 | 
					 | 
				
			||||||
import type { Context } from 'unleash-client';
 | 
					import type { Context } from 'unleash-client';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
 | 
					 | 
				
			||||||
export function createContext(value: any): Context {
 | 
					export function createContext(value: any): Context {
 | 
				
			||||||
    const {
 | 
					    const {
 | 
				
			||||||
        appName,
 | 
					        appName,
 | 
				
			||||||
@ -33,9 +31,8 @@ export function createContext(value: any): Context {
 | 
				
			|||||||
    return cleanContext;
 | 
					    return cleanContext;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
 | 
					 | 
				
			||||||
export const enrichContextWithIp = (query: any, ip: string): Context => {
 | 
					export const enrichContextWithIp = (query: any, ip: string): Context => {
 | 
				
			||||||
    query.remoteAddress = query.remoteAddress || ip;
 | 
					    query.remoteAddress = query.remoteAddress || ip;
 | 
				
			||||||
    query.sessionId = query.sessionId || String(Math.random());
 | 
					    query.sessionId = query.sessionId || crypto.randomBytes(18).toString('hex');
 | 
				
			||||||
    return createContext(query);
 | 
					    return createContext(query);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
@ -1,3 +1,4 @@
 | 
				
			|||||||
 | 
					import crypto from 'crypto';
 | 
				
			||||||
import type {
 | 
					import type {
 | 
				
			||||||
    IUnleashConfig,
 | 
					    IUnleashConfig,
 | 
				
			||||||
    IUnleashServices,
 | 
					    IUnleashServices,
 | 
				
			||||||
@ -88,7 +89,8 @@ export class FrontendApiService {
 | 
				
			|||||||
    ): Promise<FrontendApiFeatureSchema[]> {
 | 
					    ): Promise<FrontendApiFeatureSchema[]> {
 | 
				
			||||||
        const client = await this.clientForFrontendApiToken(token);
 | 
					        const client = await this.clientForFrontendApiToken(token);
 | 
				
			||||||
        const definitions = client.getFeatureToggleDefinitions() || [];
 | 
					        const definitions = client.getFeatureToggleDefinitions() || [];
 | 
				
			||||||
        const sessionId = context.sessionId || String(Math.random());
 | 
					        const sessionId =
 | 
				
			||||||
 | 
					            context.sessionId || crypto.randomBytes(18).toString('hex');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const resultDefinitions = definitions
 | 
					        const resultDefinitions = definitions
 | 
				
			||||||
            .filter((feature) =>
 | 
					            .filter((feature) =>
 | 
				
			||||||
@ -115,7 +117,8 @@ export class FrontendApiService {
 | 
				
			|||||||
    ): Promise<FrontendApiFeatureSchema[]> {
 | 
					    ): Promise<FrontendApiFeatureSchema[]> {
 | 
				
			||||||
        const client = await this.newClientForFrontendApiToken(token);
 | 
					        const client = await this.newClientForFrontendApiToken(token);
 | 
				
			||||||
        const definitions = client.getFeatureToggleDefinitions() || [];
 | 
					        const definitions = client.getFeatureToggleDefinitions() || [];
 | 
				
			||||||
        const sessionId = context.sessionId || String(Math.random());
 | 
					        const sessionId =
 | 
				
			||||||
 | 
					            context.sessionId || crypto.randomBytes(18).toString('hex');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const resultDefinitions = definitions
 | 
					        const resultDefinitions = definitions
 | 
				
			||||||
            .filter((feature) => {
 | 
					            .filter((feature) => {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user