Skip to content

Select JWT crypto backend and add deterministic token tests#150

Merged
chipp merged 1 commit intomainfrom
fix-jwt
Feb 17, 2026
Merged

Select JWT crypto backend and add deterministic token tests#150
chipp merged 1 commit intomainfrom
fix-jwt

Conversation

@chipp
Copy link
Owner

@chipp chipp commented Feb 17, 2026

No description provided.

@chipp chipp enabled auto-merge February 17, 2026 13:25
@gemini-code-assist
Copy link

Summary of Changes

Hello @chipp, 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 refactors the JWT token handling to improve testability and explicitly select a cryptographic backend. By introducing functions that allow for injecting secrets and timestamps, the token creation and validation logic can now be tested deterministically. This change also enables the rust_crypto feature of the jsonwebtoken crate, ensuring a pure Rust implementation for cryptographic operations and updating the dependency tree accordingly.

Highlights

  • Cryptographic Backend Selection: Enabled the rust_crypto feature for the jsonwebtoken crate, transitioning to a pure Rust cryptographic implementation for JWT operations.
  • Deterministic Token Testing: Introduced new internal functions for JWT creation and validation that accept an explicit secret and timestamp, allowing for predictable and deterministic testing of token logic.
  • Custom Expiration Validation: Modified the JWT validation process to manually check token expiration, disabling the jsonwebtoken crate's default exp validation to facilitate deterministic testing.
  • Comprehensive Unit Tests: Added a suite of unit tests for JWT tokens, covering various scenarios such as valid tokens, token type mismatches, expiration, and incorrect secrets.
  • Dependency Updates: Updated Cargo.lock to include numerous new cryptographic dependencies required by the rust_crypto feature, reflecting the change in the underlying crypto implementation.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • Cargo.lock
    • Added new dependencies required by the rust_crypto feature, including base16ct, base64ct, const-oid, crypto-bigint, curve25519-dalek, der, ecdsa, ed25519, ed25519-dalek, elliptic-curve, ff, fiat-crypto, group, hkdf, hmac, libm, num-bigint-dig, num-iter, p256, p384, pem-rfc7468, pkcs1, pkcs8, primeorder, rand, rand_chacha, rfc6979, rsa, rustc_version, sec1, spin, spki, and zeroize.
  • bin/alisa/Cargo.toml
    • Configured the jsonwebtoken dependency to explicitly use its rust_crypto feature.
  • bin/alisa/src/web_service/auth/token.rs
    • Refactored is_valid_token and create_token_with_expiration_in into new internal functions (is_valid_token_with_secret, is_valid_token_with_secret_at, create_token_with_expiration_in_with_secret, create_token_with_expiration_in_with_secret_at) to allow for deterministic testing.
    • Modified JWT validation to disable automatic exp validation and perform it manually.
    • Changed the exp field in the Claims struct from usize to u64.
    • Added a current_timestamp helper function.
    • Implemented a new test module with unit tests for token validation and creation under various conditions.
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 refactors the JWT token generation and validation logic to be deterministic and adds comprehensive tests, significantly improving testability. It also switches to the rust_crypto backend for jsonwebtoken, modifies the exp claim type to u64, and explicitly uses the HS512 algorithm. A security audit confirmed that these changes, including manual expiration checks, algorithm security, audience/subject validation, and secret handling, are well-implemented and introduce no new security risks. However, I've left a few comments regarding RFC compliance for token expiration, error handling, and robustness against panics.

};

decoded.is_ok()
decoded.claims.exp >= now_timestamp

Choose a reason for hiding this comment

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

high

According to RFC 7519, the exp (expiration time) claim identifies the expiration time 'on or after which the JWT MUST NOT be accepted for processing'. This means the token is invalid if the current time is greater than or equal to the expiration time. The check should be decoded.claims.exp > now_timestamp to be compliant.

Note that the token_expires_exactly_at_expected_time test will need to be adjusted to reflect this change. For a token expiring at NOW + 30, it should be valid at NOW + 29 but invalid at NOW + 30.

    decoded.claims.exp > now_timestamp

Comment on lines +52 to +55
) {
Ok(decoded) => decoded,
Err(_) => return false,
};

Choose a reason for hiding this comment

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

medium

When token decoding fails, the error is currently ignored. It would be beneficial for debugging to log the error. This can help diagnose issues with malformed tokens, signature mismatches, or other validation problems.

    ) {
        Ok(decoded) => decoded,
        Err(e) => {
            log::debug!("Token decoding failed: {}", e);
            return false;
        }
    };

Comment on lines +93 to +96
let expiration = now_timestamp
.checked_add(expiration.num_seconds())
.expect("valid timestamp");
let expiration = u64::try_from(expiration).expect("non-negative timestamp");

Choose a reason for hiding this comment

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

medium

The use of expect() here can cause the service to panic if an invalid expiration duration is provided (e.g., one that causes a timestamp overflow or underflow resulting in a negative value). While the current usage in the codebase is with constant durations, it's more robust for public functions and their helpers to return a Result instead of panicking. This would allow callers to handle such errors gracefully instead of crashing the thread.

@chipp chipp merged commit 8a46085 into main Feb 17, 2026
6 checks passed
@chipp chipp deleted the fix-jwt branch February 18, 2026 08:33
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