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

fix: handle URL-safe base64 decoding for JWT #38991

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

baku2san
Copy link

@baku2san baku2san commented Dec 27, 2024

Description

This pull request addresses an issue with JWT decoding where URL-safe base64 decoding was not being used, leading to UTF-8 decoding errors. The changes ensure that the base64 encoded string is properly padded and decoded using URL-safe base64 decoding.

RFC4648

Changes:

  • Replaced base64.decodebytes with base64.urlsafe_b64decode.
  • Added logic to calculate and append necessary padding to the base64 string.

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

- Updated the JWT decoding logic to use URL-safe base64 decoding.
- Added padding to the base64 encoded string to ensure proper decoding.
- This fixes the issue where UTF-8 decoding errors occurred due to missing padding in the base64 string.

Changes:
- Replaced `base64.decodebytes` with `base64.urlsafe_b64decode`.
- Added logic to calculate and append necessary padding to the base64 string.
@baku2san baku2san requested review from pvaneck, xiangyan99 and a team as code owners December 27, 2024 14:36
@github-actions github-actions bot added Azure.Identity Community Contribution Community members are working on the issue customer-reported Issues that are reported by GitHub users external to the Azure organization. labels Dec 27, 2024
Copy link

Thank you for your contribution @baku2san! We will review the pull request and get back to you soon.

@azure-sdk
Copy link
Collaborator

API change check

API changes are not detected in this pull request.

Copy link
Member

@pvaneck pvaneck left a comment

Choose a reason for hiding this comment

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

Thanks for the PR and improving the JWT payload decoding! Left a suggestion inline.

Do you mind also adding this change to the async decorator as well?

@baku2san
Copy link
Author

baku2san commented Jan 9, 2025

@microsoft-github-policy-service agree

Copy link
Member

@pvaneck pvaneck left a comment

Choose a reason for hiding this comment

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

Thanks for the updates. Seems like the black reformatting might be causing some conflicts. To be sure that we use the same black versioning/configuration that the CI uses, try running it via tox (from inside the sdk/identity/azure-identity directory):

pip install tox
tox run -e black -c ../../../eng/tox/tox.ini --root . -- azure

baku2san and others added 3 commits January 11, 2025 22:12
…s.py

Co-authored-by: Paul Van Eck <paulvaneck@microsoft.com>
…ators.py

Co-authored-by: Paul Van Eck <paulvaneck@microsoft.com>
@baku2san
Copy link
Author

baku2san commented Jan 11, 2025

Thanks for the guidance on using tox for Black formatting. I've followed your instructions and reformatted the code accordingly.

I have verified and confirmed that the expression padding_needed = -len(base64_meta_data) % 4 is correct for determining the necessary padding in Base64 encoding. The results clearly show that this method accurately calculates the required padding, ensuring the data length is a multiple of 4.

Thank you for your assistance in clarifying this!

len(base64_meta_data) padding_needed = -(len(base64_meta_data) % 4) padding_needed = -len(base64_meta_data) % 4  base
0 0 0 4
1 -1 3 3
2 -2 2 2
3 -3 1 1
4 0 0 4
import pandas as pd

lengths = range(11)  # This will create a range from 0 to 10
results = []
for length in lengths:
    expr1 = -(length % 4)
    expr2 = -length % 4
    expr3 = 4 - (length % 4)
    results.append((length, expr1, expr2, expr3))

df = pd.DataFrame(results, columns=["len(base64_meta_data)", "padding_needed = -(len(base64_meta_data) % 4)", "padding_needed = -len(base64_meta_data) % 4", "base"])
print(df)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Azure.Identity Community Contribution Community members are working on the issue customer-reported Issues that are reported by GitHub users external to the Azure organization.
Projects
Status: Untriaged
Development

Successfully merging this pull request may close these issues.

3 participants