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

fix(core): generateRandomString uses Math.random() #14132

Open
wants to merge 1 commit into
base: feat/server-auth/main
Choose a base branch
from

Conversation

HuiSF
Copy link
Member

@HuiSF HuiSF commented Jan 10, 2025

Description of changes

Update the existing generateRandomString() implementation to use crypto.getRandomValues().

Issue #, if available

Description of how you validated changes

  • unit tests
  • manual testing with a sample app integrating Hosted UI.

Checklist

  • PR description included
  • yarn test passes
  • Unit Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)

Checklist for repo maintainers

  • Verify E2E tests for existing workflows are working as expected or add E2E tests for newly added workflows
  • New source file paths included in this PR have been added to CODEOWNERS, if appropriate

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@HuiSF HuiSF requested review from sktimalsina, cshfang, pranavosu and a team as code owners January 10, 2025 21:41
@HuiSF HuiSF changed the title feat(adapter-nextjs): add runtimeOptions.cookies to createServerRunner ( fix(core): generateRandomString uses Math.random() Jan 10, 2025
Copy link
Member

@ashwinkumar6 ashwinkumar6 left a comment

Choose a reason for hiding this comment

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

changes lgtm !!
(will need to double check polyfill in RN)

Comment on lines +28 to +30
const length = 10;
const result = generateRandomString(length);
expect(result).toHaveLength(length);
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I think it's probably ok to just be concise here

Suggested change
const length = 10;
const result = generateRandomString(length);
expect(result).toHaveLength(length);
expect(generateRandomString(length)).toHaveLength(10);

expect(crypto.getRandomValues).toHaveBeenCalledWith(expect.any(Uint8Array));
});

it('generates different strings on subsequent calls', () => {
Copy link
Member

Choose a reason for hiding this comment

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

Is this a meaningful assertion given the mocked getRandomValues?

}
const array = new Uint8Array(bufferView.buffer);
for (let i = 0; i < array.byteLength; i++) {
array[i] = Math.floor(Math.random() * 256);
Copy link
Member

Choose a reason for hiding this comment

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

Maybe instead of trying to replicate randomness, we should trust that getRandomValues would indeed give us random values and we mock here with something static that we can assert our actual logic (what we end up doing with the random values) is correct

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