Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nirgur committed Sep 30, 2024
1 parent 51a0fdd commit e59227b
Show file tree
Hide file tree
Showing 5 changed files with 16,527 additions and 15,475 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
let sessionId: string;

export const getClientSessionId = (): string => {
if (sessionId) {
return sessionId;
}
const currentDate = new Date();
const utcString = `${currentDate.getUTCFullYear().toString()}-${(
currentDate.getUTCMonth() + 1
)
.toString()
.padStart(2, '0')}-${currentDate
.getUTCDate()
.toString()
.padStart(2, '0')}-${currentDate
.getUTCHours()
.toString()
.padStart(2, '0')}:${currentDate
.getUTCMinutes()
.toString()
.padStart(2, '0')}:${currentDate
.getUTCSeconds()
.toString()
.padStart(2, '0')}:${currentDate.getUTCMilliseconds().toString()}`;
const randomSuffix = Math.floor(1000 + Math.random() * 9000);
sessionId = `${utcString}-${randomSuffix}`;

Check failure

Code scanning / CodeQL

Insecure randomness High

This uses a cryptographically insecure random number generated at
Math.random()
in a security context.
return sessionId;
};
1 change: 1 addition & 0 deletions packages/sdks/core-js-sdk/src/httpClient/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { default as createFetchLogger } from './createFetchLogger';
export { getClientSessionId } from './getClientSessionId';

export function transformSetCookie(setCookieHeader: string) {
// Split the header by semicolons to separate different attributes
Expand Down
30 changes: 1 addition & 29 deletions packages/sdks/core-js-sdk/src/httpClient/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transformSetCookie } from './helpers';
import { getClientSessionId, transformSetCookie } from './helpers';
import createFetchLogger from './helpers/createFetchLogger';
import {
CreateHttpClientConfig,
Expand All @@ -13,34 +13,6 @@ const jsonHeaders = {
'Content-Type': 'application/json',
};

let sessionId: string;
const getClientSessionId = (): string => {
if (sessionId) {
return sessionId;
}
const currentDate = new Date();
const utcString = `${currentDate.getUTCFullYear().toString()}-${(
currentDate.getUTCMonth() + 1
)
.toString()
.padStart(2, '0')}-${currentDate
.getUTCDate()
.toString()
.padStart(2, '0')}-${currentDate
.getUTCHours()
.toString()
.padStart(2, '0')}:${currentDate
.getUTCMinutes()
.toString()
.padStart(2, '0')}:${currentDate
.getUTCSeconds()
.toString()
.padStart(2, '0')}:${currentDate.getUTCMilliseconds().toString()}`;
const randomSuffix = Math.floor(1000 + Math.random() * 9000);
sessionId = `${utcString}-${randomSuffix}`;
return sessionId;
};

/**
* Create a Bearer authorization header with concatenated projectId and token
* @param projectId The project id to use in the header
Expand Down
4 changes: 3 additions & 1 deletion packages/sdks/core-js-sdk/test/httpClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DEFAULT_BASE_API_URL } from '../src/constants';
import createHttpClient from '../src/httpClient';
import { getClientSessionId } from '../src/httpClient/helpers';
import createFetchLogger from '../src/httpClient/helpers/createFetchLogger';
import { ExtendedResponse } from '../src/httpClient/types';

Expand All @@ -11,6 +12,7 @@ const afterRequestHook = jest.fn();
const descopeHeaders = {
'x-descope-sdk-name': 'core-js',
'x-descope-sdk-version': globalThis.BUILD_VERSION,
'x-descope-sdk-session-id': getClientSessionId(),
};

const httpClient = createHttpClient({
Expand Down Expand Up @@ -160,8 +162,8 @@ describe('httpClient', () => {
test2: '123',
test: '123',
Authorization: 'Bearer 456',
...descopeHeaders,
'x-descope-sdk-name': 'lulu',
'x-descope-sdk-version': globalThis.BUILD_VERSION,
}),
method: 'GET',
},
Expand Down
Loading

0 comments on commit e59227b

Please sign in to comment.