Skip to content

Conversation

shivam-kumar-ts
Copy link
Contributor

@shivam-kumar-ts shivam-kumar-ts commented Sep 18, 2025

  • Ensuring the token type is a string before proceeding token validation
  • Added unit tests for token type validation

@shivam-kumar-ts shivam-kumar-ts requested a review from a team as a code owner September 18, 2025 15:54
@mnk-blr
Copy link
Collaborator

mnk-blr commented Sep 18, 2025

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

security/snyk check is complete. No issues have been found. (View Details)

license/snyk check is complete. No issues have been found. (View Details)

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.

Summary of Changes

Hello @shivam-kumar-ts, 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 enhances the robustness of authentication token handling by implementing an early type check for the authToken parameter. It ensures that the token is always a string before any validation logic is applied, including scenarios where full token verification is disabled. This change prevents potential runtime errors caused by unexpected token types, thereby improving the overall stability and reliability of the authentication process.

Highlights

  • Token Type Validation: Introduced a new check in the validateAuthToken function to explicitly ensure that the authToken parameter is a string before proceeding with any further validation steps.
  • Robust Error Handling: If the authToken is not a string, an error message is logged using logger.error, and an Error is thrown, preventing unexpected behavior downstream.
  • Unconditional Type Check: This type validation is performed unconditionally, even when embedConfig.disableTokenVerification is set to true, guaranteeing basic token integrity regardless of other verification settings.
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 adds a validation step to ensure that the authToken is a string before any further processing in validateAuthToken. This is a good defensive check to prevent runtime errors if the getAuthToken callback from the user returns an unexpected type. My review includes suggestions to improve the accuracy of a comment and to enhance code style and readability using modern JavaScript features.

src/authToken.ts Outdated
Comment on lines 62 to 63
// even if token verification is disabled, we will still validate
// the token type to ensure that the token is valid and not expired

Choose a reason for hiding this comment

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

medium

The comment on line 63 is slightly inaccurate. This check validates that the token is a string, but it doesn't ensure the token itself is 'valid and not expired'. The actual token validation (if not disabled) happens later in the function. To avoid confusion, I suggest rephrasing the comment to be more precise about what the following block does.

Suggested change
// even if token verification is disabled, we will still validate
// the token type to ensure that the token is valid and not expired
// even if token verification is disabled, we will still validate
// that the token is a string before proceeding.

src/authToken.ts Outdated
Comment on lines 64 to 68
if(typeof authToken !== 'string') {
const errorMessage = 'Expected getAuthToken to return a string, but received a ' + typeof authToken + '.';
logger.error(errorMessage);
throw new Error(errorMessage);
}

Choose a reason for hiding this comment

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

medium

This block can be improved for better readability and consistency with modern JavaScript practices.

  1. It's a common convention to have a space after the if keyword.
  2. Using a template literal for constructing the errorMessage is more readable than string concatenation.

Additionally, for better maintainability, you could consider moving this error message to src/errors.ts by creating a function that generates this dynamic message, similar to how other errors are handled in the project.

Suggested change
if(typeof authToken !== 'string') {
const errorMessage = 'Expected getAuthToken to return a string, but received a ' + typeof authToken + '.';
logger.error(errorMessage);
throw new Error(errorMessage);
}
if (typeof authToken !== 'string') {
const errorMessage = `Expected getAuthToken to return a string, but received a ${typeof authToken}.`;
logger.error(errorMessage);
throw new Error(errorMessage);
}

Copy link

pkg-pr-new bot commented Sep 18, 2025

Open in StackBlitz

npm i https://pkg.pr.new/thoughtspot/visual-embed-sdk/@thoughtspot/visual-embed-sdk@314

commit: 51d37d4

const cachedAuthToken = getCacheAuthToken();
// even if token verification is disabled, we will still validate
// that the token is a string before proceeding.
if (typeof authToken !== 'string') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please write a unit test for this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

src/errors.ts Outdated
SDK_NOT_INITIALIZED: 'SDK not initialized',
SESSION_INFO_FAILED: 'Failed to get session information',
INVALID_TOKEN_ERROR: 'Received invalid token from getAuthToken callback or authToken endpoint.',
INVALID_TOKEN_TYPE_ERROR: 'Expected getAuthToken to return a string, but received a',
Copy link
Contributor

Choose a reason for hiding this comment

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

we can use template strings here 'Expected getAuthToken to return a string, but received a {invalidType}'

src/authToken.ts Outdated
// even if token verification is disabled, we will still validate
// that the token is a string before proceeding.
if (typeof authToken !== 'string') {
const errorMessage = `${ERROR_MESSAGE.INVALID_TOKEN_TYPE_ERROR} ${typeof authToken}.`;
Copy link
Contributor

Choose a reason for hiding this comment

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

use template strings for this

src/authToken.ts Outdated
authToken: string,
suppressAlert?: boolean,
): Promise<boolean> => {
const cachedAuthToken = getCacheAuthToken();
Copy link
Contributor

Choose a reason for hiding this comment

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

move this line below the type check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link

sonar-prod-ts bot commented Sep 23, 2025

SonarQube Quality Gate

Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@shivam-kumar-ts shivam-kumar-ts merged commit 31d4d15 into main Sep 24, 2025
11 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.

4 participants