Skip to content

Comments

Potential fix for code scanning alert no. 84: Insecure randomness#279

Merged
Dargon789 merged 2 commits intomasterfrom
alert-autofix-84
Feb 7, 2026
Merged

Potential fix for code scanning alert no. 84: Insecure randomness#279
Dargon789 merged 2 commits intomasterfrom
alert-autofix-84

Conversation

@Dargon789
Copy link
Owner

@Dargon789 Dargon789 commented Feb 7, 2026

Potential fix for https://github.com/Dargon789/sequence.js/security/code-scanning/84

To fix this problem, the generation of identifiers within generateId() (specifically the random component) must use a cryptographically secure random number generator. For browser environments, the correct API is window.crypto.getRandomValues, which returns a cryptographically secure random number. Replace the use of Math.random() with a string derived from secure random bytes, e.g., converting a Uint8Array (or Uint32Array) to a base36 string.

Edit the generateId() implementation in packages/wallet/dapp-client/src/DappTransport.ts so that instead of using Math.random(), it creates a random value by calling window.crypto.getRandomValues, then builds the ID string using this random value. No new imports are needed since the browser window.crypto is already available.


Suggested fixes powered by Copilot Autofix. Review carefully before merging.

Summary by Sourcery

Bug Fixes:

  • Replace Math.random()-based ID component with window.crypto.getRandomValues to ensure IDs use cryptographically secure randomness.

@codesandbox
Copy link

codesandbox bot commented Feb 7, 2026

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

@bolt-new-by-stackblitz
Copy link

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@sourcery-ai
Copy link

sourcery-ai bot commented Feb 7, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR updates the DappTransport.generateId() implementation to replace Math.random()-based ID suffix generation with a cryptographically secure random value sourced from window.crypto.getRandomValues, addressing a code scanning alert for insecure randomness.

Sequence diagram for secure ID generation in DappTransport.generateId

sequenceDiagram
    participant DappTransport
    participant WindowCrypto as window_crypto
    participant Uint32Array as Uint32Array_instance

    DappTransport->>Uint32Array: create Uint32Array(1)
    DappTransport->>WindowCrypto: getRandomValues(Uint32Array_instance)
    WindowCrypto-->>DappTransport: filled Uint32Array_instance
    DappTransport->>DappTransport: randStr = Uint32Array_instance[0].toString(36).padStart(7, '0')
    DappTransport->>DappTransport: id = Date.now().toString(36) + "-" + randStr
    DappTransport-->>DappTransport: return id
Loading

Class diagram for updated DappTransport.generateId implementation

classDiagram
    class DappTransport {
        +generateId() string
    }

    class WindowCrypto {
        +getRandomValues(typedArray Uint32Array) Uint32Array
    }

    class Uint32Array {
        +Uint32Array(length number)
        +get(index number) number
        +set(index number, value number) void
    }

    DappTransport ..> WindowCrypto : uses
    DappTransport ..> Uint32Array : uses
Loading

File-Level Changes

Change Details Files
Use cryptographically secure randomness when generating DappTransport IDs instead of Math.random().
  • Replaced the Math.random()-derived base36 substring with a Uint32Array-based random value generated via window.crypto.getRandomValues.
  • Converted the secure random 32-bit integer to a base36 string and left-padded it to a fixed 7-character length for consistent ID formatting.
  • Preserved the existing Date.now().toString(36) prefix while updating only the random suffix portion of the generated ID string.
packages/wallet/dapp-client/src/DappTransport.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@snyk-io
Copy link

snyk-io bot commented Feb 7, 2026

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@gemini-code-assist
Copy link

Summary of Changes

Hello @Dargon789, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request implements a critical security fix by upgrading the random number generation mechanism used for creating unique identifiers. It transitions from a standard, potentially predictable Math.random() to the cryptographically secure window.crypto.getRandomValues, thereby mitigating a reported vulnerability related to insecure randomness.

Highlights

  • Security Fix: Replaced Math.random() with window.crypto.getRandomValues in the generateId method to ensure cryptographically secure random number generation, addressing a code scanning alert for insecure randomness.
  • ID Generation Improvement: The generateId method now uses Uint32Array and window.crypto.getRandomValues to produce a more robust and secure random component for generated IDs.
Changelog
  • packages/wallet/dapp-client/src/DappTransport.ts
    • Replaced the use of Math.random() with window.crypto.getRandomValues for generating the random part of the ID.
    • Implemented the use of a Uint32Array to obtain cryptographically secure random values.
    • Converted the secure random value from the Uint32Array to a base36 string for inclusion in the generated ID.
Activity
  • The initial changes were suggested and powered by Copilot Autofix, indicating an automated suggestion for the fix.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly addresses the 'Insecure randomness' security alert by replacing Math.random() with the cryptographically secure window.crypto.getRandomValues. The implementation is sound. I've added one suggestion to ensure the generated random string has a fixed length, which makes it more consistent with the previous implementation and avoids potential (though unlikely) collisions.

@Dargon789 Dargon789 marked this pull request as ready for review February 7, 2026 09:25
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Directly accessing window.crypto in generateId() will throw in non-browser or server-side contexts; consider using globalThis.crypto with a graceful fallback (or an environment check) to avoid runtime errors.
  • It might be worth handling the rare case where crypto.getRandomValues is unavailable or fails (e.g. older browsers or restricted environments) so that generateId() still behaves predictably rather than throwing.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Directly accessing `window.crypto` in `generateId()` will throw in non-browser or server-side contexts; consider using `globalThis.crypto` with a graceful fallback (or an environment check) to avoid runtime errors.
- It might be worth handling the rare case where `crypto.getRandomValues` is unavailable or fails (e.g. older browsers or restricted environments) so that `generateId()` still behaves predictably rather than throwing.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@vercel
Copy link

vercel bot commented Feb 7, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
sequence-js-docs Ready Ready Preview, Comment Feb 7, 2026 9:47am
sequence-js-web Ready Ready Preview, Comment Feb 7, 2026 9:47am

@vercel

This comment was marked as resolved.

Dargon789 and others added 2 commits February 7, 2026 09:39
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Signed-off-by: Dargon789 <64915515+Dargon789@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: Dargon789 <64915515+Dargon789@users.noreply.github.com>
@Dargon789 Dargon789 merged commit 5b0e2ed into master Feb 7, 2026
11 of 15 checks passed
@Dargon789 Dargon789 deleted the alert-autofix-84 branch February 7, 2026 10:07
@Dargon789 Dargon789 linked an issue Feb 19, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Sequence diagram for NeuraLegion DAST scan workflow execution

1 participant