Skip to content

fix(abstractions): validate allowed hosts and access token provider in BaseBearerTokenAuthenticationProvider - #867

Merged
Vincent Biret (baywet) merged 2 commits into
microsoft:mainfrom
hardikkaurani:fix/uri-normalization-and-bearer-auth-validation
Nov 9, 2026
Merged

Vincent Biret (baywet) merged 2 commits into
microsoft:mainfrom
hardikkaurani:fix/uri-normalization-and-bearer-auth-validation

Conversation

@hardikkaurani

@hardikkaurani Hardik K (hardikkaurani) commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Overview

In accordance with Kiota's architectural specification across languages (.NET, Go, Java, TypeScript), BaseBearerTokenAuthenticationProvider.authenticate_request must verify whether the target URI's host is approved by the access token provider's AllowedHostsValidator.

As requested by maintainer Vincent Biret (@baywet) in #867 (comment), this PR has been scoped exclusively to the BaseBearerTokenAuthenticationProvider changes. The parameter collection sanitization and ISO duration validation changes have been moved to a separate PR: #883.

Problems Addressed

  1. Unchecked Request Host in authenticate_request:

    • authenticate_request was retrieving tokens and populating the Authorization header regardless of whether request.url belonged to an approved host.
    • Updated authenticate_request to validate is_url_host_valid(request.url) before acquiring tokens or modifying headers, preventing credentials from leaking to unapproved hosts.
  2. Null access_token_provider in __init__:

    • Added a defensive check in BaseBearerTokenAuthenticationProvider.__init__ to raise ValueError("Access token provider cannot be null") if access_token_provider is null.

Changes

  • packages/abstractions/kiota_abstractions/authentication/base_bearer_token_authentication_provider.py:
    • Validate access_token_provider in __init__.
    • Return early when request.url host is not in allowed hosts before attempting token retrieval.
  • packages/abstractions/tests/authentication/test_base_bearer_token_authentication.py:
    • Add tests for provider null validation, unallowed host early return, and header preservation.

Verification

  • pytest: 216 passed.
  • mypy kiota_abstractions: clean (no issues in 48 source files).

@hardikkaurani
Hardik K (hardikkaurani) requested a review from a team as a code owner November 6, 2026 23:24
Copilot AI balanced review requested due to automatic review settings November 6, 2026 23:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet),

While testing URL template expansion and authentication flows against edge-case payloads, I noticed a few discrepancies in kiota_abstractions:

  1. Parameter collection sanitization: Passing collections containing None elements (e.g. [None, 'active'] or all-None lists) caused StdUriTemplate.expand() to raise an unhandled ValueError: Illegal class passed as substitution: None. Additionally, enum collection normalization evaluated all(isinstance(x, Enum) for x in value), so any list with a leading None bypassed enum string conversion and produced unnormalized values. We now filter None entries and inspect surviving elements so normalization remains order-independent.
  2. ISO 8601 duration validation: In date_utils.py, component checks evaluated values numerically rather than testing match group presence. This caused empty duration indicators like "P" and "PT", as well as weeks combined with zero-valued time units ("P1WT0S"), to be parsed as timedelta(0) instead of being rejected as malformed. We now verify group presence so only valid ISO durations succeed.
  3. Allowed hosts validation in bearer auth: BaseBearerTokenAuthenticationProvider was missing the AllowedHostsValidator check before acquiring tokens, differing from the other language SDKs and risking token leakage to unapproved endpoints. We've added the host validation check prior to token acquisition and header modification.

All 229 unit tests pass cleanly alongside static typing and formatting checks. When you have a moment, please take a look. Thanks!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues were identified in the URI parameter handling, duration parsing, or bearer authentication changes.

No open issues 🎉

Review effort: Balanced

@github-project-automation github-project-automation Bot moved this to In Progress 🚧 in Kiota Nov 9, 2026
…n BaseBearerTokenAuthenticationProvider

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…rovider allowed hosts and provider validation

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
@hardikkaurani
Hardik K (hardikkaurani) force-pushed the fix/uri-normalization-and-bearer-auth-validation branch from f520525 to e3675d9 Compare November 9, 2026 20:15
Copilot AI balanced review requested due to automatic review settings November 9, 2026 20:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@hardikkaurani Hardik K (hardikkaurani) changed the title fix(abstractions): improve parameter collection sanitization, ISO duration validation, and bearer auth allowed hosts fix(abstractions): validate allowed hosts and access token provider in BaseBearerTokenAuthenticationProvider Nov 9, 2026
@sonarqubecloud

sonarqubecloud Bot commented Nov 9, 2026

Copy link
Copy Markdown

@baywet
Vincent Biret (baywet) requested a balanced review from Copilot November 9, 2026 20:16
@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Vincent Biret (@baywet) Re-scoped this PR exclusively to the BaseBearerTokenAuthenticationProvider changes (allowed hosts validation in authenticate_request and null checks in __init__).

As requested, I've moved the parameter collection sanitization and ISO duration component validation changes over to a separate PR: #883.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues were identified for the token-provider validation and allowed-host checks.

No open issues 🎉

Review effort: Balanced

@baywet
Vincent Biret (baywet) added this pull request to the merge queue Nov 9, 2026
Merged via the queue into microsoft:main with commit b97fdd0 Nov 9, 2026
69 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress 🚧 to Done ✔️ in Kiota Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done ✔️

Development

Successfully merging this pull request may close these issues.

3 participants