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

Security Improvemet #774

Closed
miguelmir123 opened this issue Feb 29, 2024 · 3 comments
Closed

Security Improvemet #774

miguelmir123 opened this issue Feb 29, 2024 · 3 comments

Comments

@miguelmir123
Copy link

Issue Summary

There is an easy way to improve the security when using the jwt token, in the jwt class the token is not being verify which is common a bad practice in working with this kind of tokens.

Code Snippet

the problems appears here in the jwt class:
@classmethod

def from_jwt(cls, jwt, key=""):

    """

    Decode a JWT string into a Jwt object

    :param str jwt: JWT string

    :param Optional[str] key: key used to verify JWT signature, if not provided then validation

                              is skipped.

    :raises JwtDecodeError if decoding JWT fails for any reason.

    :return: A DecodedJwt object containing the jwt information.

    """

    verify = True if key else False

    try:

        headers = jwt_lib.get_unverified_header(jwt)

is in the last line where the token signature is not being verified, The verification can be easily added by using this function to decode the token:

import jwt

jwt.decode(token, key, algorithms="HS256")

I hope it helps improve the code,
pd: I am not doing the commit about because I propose this as a part of College Project and I do not have enough time to test it...

@miniluz
Copy link

miniluz commented Mar 6, 2024

Hello Miguel,

This is not an issue. The verification here is not needed: this is just checking the headers to check that the operation is valid to discard before decoding if it isn't. If the header is valid, it is always decoded and thus verified before processing. The logic is clear:

headers = jwt_lib.get_unverified_header(jwt)

alg = headers.get("alg")
if alg != cls.ALGORITHM:
    raise ValueError(
        f"Incorrect decoding algorithm {alg}, "
        f"expecting {cls.ALGORITHM}."
    )

payload = jwt_lib.decode(
    ...
)

@tiwarishubham635
Copy link
Contributor

Is this still an issue?

@tiwarishubham635
Copy link
Contributor

Closing as no response was received in last 30 days

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

No branches or pull requests

3 participants