Skip to content

🔒 Fix insecure random number generator for Request ID#106

Merged
DaTiC0 merged 4 commits intomainfrom
fix-insecure-random-requestid-13092052650724018031
Mar 12, 2026
Merged

🔒 Fix insecure random number generator for Request ID#106
DaTiC0 merged 4 commits intomainfrom
fix-insecure-random-requestid-13092052650724018031

Conversation

@DaTiC0
Copy link
Owner

@DaTiC0 DaTiC0 commented Mar 12, 2026

🎯 What: The vulnerability fixed
The report_state function in action_devices.py was generating a Request ID using random.randint. The random module uses a pseudo-random number generator (PRNG) that is not cryptographically secure, meaning generated values can potentially be predicted.

⚠️ Risk: The potential impact if left unfixed
If an attacker can predict the generated Request ID, they could potentially spoof or intercept requests. This could lead to unauthorized state reports or interference with the normal operation of connected devices, undermining the integrity of the system's communication.

🛡️ Solution: How the fix addresses the vulnerability
Replaced the use of the random module with the Python standard library secrets module, which is specifically designed for generating cryptographically strong random numbers suitable for security tokens and IDs. The generation logic now uses 10**19 + secrets.randbelow(10**20 - 10**19 + 1) to maintain the exact same range and format of the Request ID securely.


PR created automatically by Jules for task 13092052650724018031 started by @DaTiC0

Summary by Sourcery

Bug Fixes:

  • Replace insecure use of the random module with the secrets module when generating Request IDs for report_state to prevent predictable values.

In `action_devices.py`, replaced the insecure `import random` and
`random.randint` with the cryptographically strong `import secrets` and
`secrets.randbelow` when generating the Request ID for state reports.
This ensures the predictability of request IDs is eliminated, preventing
potential spoofing or replay vulnerabilities related to ID generation.

Co-authored-by: DaTiC0 <13198638+DaTiC0@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Mar 12, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Replaces the insecure use of Python's non-cryptographic random.randint for generating Request IDs in report_state with a cryptographically secure generator from the secrets module, while preserving the original numeric range and format.

File-Level Changes

Change Details Files
Harden Request ID generation by switching from non-cryptographic to cryptographically secure randomness while preserving ID range.
  • Remove import of the random module inside report_state in favor of secrets
  • Replace random.randint(10**19, 10**20) with 10**19 + secrets.randbelow(10**20 - 10**19 + 1) to preserve the inclusive numeric range for Request IDs
  • Keep downstream usage of requestId unchanged (still stringified and written to report_state_file)
action_devices.py

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

@gemini-code-assist
Copy link

Summary of Changes

Hello, 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 addresses a critical security vulnerability by upgrading the random number generation mechanism used for Request IDs. By switching to a cryptographically secure method, the system prevents potential prediction of IDs by attackers, thereby safeguarding the integrity and authorization of device communication and state reporting.

Highlights

  • Security Fix: Insecure Random Number Generator: The report_state function in action_devices.py previously used random.randint to generate Request IDs, which is not cryptographically secure. This has been replaced with secrets.randbelow from Python's secrets module to ensure cryptographically strong random number generation for Request IDs.
Changelog
  • action_devices.py
    • Replaced random.randint with secrets.randbelow for generating cryptographically secure Request IDs.
Activity
  • No human activity has been recorded on this pull request yet.
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
Contributor

@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 reviewed your changes and they look great!


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.

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 a security vulnerability by replacing the insecure random module with the cryptographically secure secrets module for Request ID generation. The fix is effective. I have provided a couple of minor suggestions to improve code style and readability in the implementation.

return "ReportState not available"
import random
n = random.randint(10**19, 10**20)
import secrets

Choose a reason for hiding this comment

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

medium

In line with PEP 8 style guidelines, imports should be located at the top of the file. This improves readability and makes dependencies clear at a glance. Please move import secrets to the top of action_devices.py.

Copy link
Owner Author

Choose a reason for hiding this comment

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

medium

In line with PEP 8 style guidelines, imports should be located at the top of the file. This improves readability and makes dependencies clear at a glance. Please move import secrets to the top of action_devices.py.

@jules

Copy link
Contributor

Choose a reason for hiding this comment

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

Done. Moved the import secrets statement to the top of action_devices.py in line with PEP 8.

import random
n = random.randint(10**19, 10**20)
import secrets
n = 10**19 + secrets.randbelow(10**20 - 10**19 + 1)

Choose a reason for hiding this comment

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

medium

For better readability, the expression 10**20 - 10**19 can be simplified. This makes the code easier to understand at a glance.

n = 10**19 + secrets.randbelow(9 * 10**19 + 1)

Copy link
Owner Author

Choose a reason for hiding this comment

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

medium

For better readability, the expression 10**20 - 10**19 can be simplified. This makes the code easier to understand at a glance.

n = 10**19 + secrets.randbelow(9 * 10**19 + 1)

@Julse

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point. I've updated the code to simplify the expression to 9 * 10**19 + 1 for better readability. Tests are still passing.

In `action_devices.py`, replaced the insecure `import random` and
`random.randint` with the cryptographically strong `import secrets` and
`secrets.randbelow` when generating the Request ID for state reports.
This ensures the predictability of request IDs is eliminated, preventing
potential spoofing or replay vulnerabilities related to ID generation.

Co-authored-by: DaTiC0 <13198638+DaTiC0@users.noreply.github.com>
Repository owner deleted a comment from google-labs-jules bot Mar 12, 2026
google-labs-jules bot and others added 2 commits March 12, 2026 19:49
Update the random number generation logic based on PR feedback.
Changed `10**20 - 10**19 + 1` to the simplified equivalent
`9 * 10**19 + 1` to make the code easier to understand at a glance.

Co-authored-by: DaTiC0 <13198638+DaTiC0@users.noreply.github.com>
Moved `import secrets` to the top of `action_devices.py` to comply with
PEP 8 style guidelines, improving readability and making dependencies
clear at a glance.

Co-authored-by: DaTiC0 <13198638+DaTiC0@users.noreply.github.com>
Copy link
Owner Author

@DaTiC0 DaTiC0 left a comment

Choose a reason for hiding this comment

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

If everything is ok merge it @jules

@DaTiC0 DaTiC0 merged commit 9a18f5f into main Mar 12, 2026
7 checks passed
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.

1 participant