Skip to content

fix(abstractions): normalize allowed hosts whitespace and enforce url validation when empty - #2194

Open
Hardik K (hardikkaurani) wants to merge 10 commits into
microsoft:mainfrom
hardikkaurani:fix/allowed-hosts-validation-clean
Open

Hardik K (hardikkaurani) wants to merge 10 commits into
microsoft:mainfrom
hardikkaurani:fix/allowed-hosts-validation-clean

Conversation

@hardikkaurani

Copy link
Copy Markdown
Contributor

Description

This PR strengthens host validation and normalization in AllowedHostsValidator:

  1. Whitespace trimming & host normalization:

    • In validateHosts, host?.trim().toLowerCase() ensures hosts with leading or trailing whitespace (e.g. " http://invalid.com ") are correctly checked against the http:// / https:// prefix restriction and rejected with an Error.
    • In normalizeHosts, hosts are trimmed via host.trim().toLowerCase() and empty entries are filtered out, ensuring entries like " example.com " match target URLs consistently.
  2. URL validation & security hardening:

    • Trims leading/trailing whitespace from the candidate URL before parsing.
    • When allowedHosts is empty (this.allowedHosts.size === 0), URL parsing and userinfo rejection (parsed.username || parsed.password) are now enforced before returning true, preventing host spoofing and malformed URLs from bypassing checks.
  3. Test coverage:

    • Added unit tests for whitespace-padded host prefixes in the constructor and setAllowedHosts.
    • Added unit tests for trimmed host retrieval, URLs with whitespace, and userinfo rejection when allowedHosts is empty.

cc Vincent Biret (@baywet)

@hardikkaurani
Hardik K (hardikkaurani) requested a review from a team as a code owner October 22, 2026 05:44
Copilot AI balanced review requested due to automatic review settings October 22, 2026 05:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 reached their quota limit.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet), opened this PR normalizing allowed hosts whitespace and strengthening URL validation. All tests and CI checks are passing. Whenever you have a chance, could you please review? Thanks!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Host normalization can silently enable unrestricted access, and the browser fallback still accepts unparsed URLs.

2 open issues

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 26, 2026 22:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet),

I've addressed the review comments and updated the PR:

  • Browser Unparsed Fallback Guard: Updated AllowedHostsValidator.isUrlHostValid to return false on the unparsed browser fallback path when allowedHosts is empty, preventing malformed URLs (such as http% or https:user:pass@example.com/path) from bypassing URL parsing and userinfo rejection.
  • Strict Allowlist Normalization: Updated validateHosts to reject non-empty allowlists that normalize to an empty set (e.g., whitespace-only inputs), while preserving unrestricted access for explicitly empty inputs.
  • Regression Tests: Added regression tests for constructor and setAllowedHosts rejection of non-empty inputs normalizing to empty, preservation of explicitly empty sets, and browser fallback behavior for unparsed inputs with empty allowlists.
  • All unit tests across @microsoft/kiota-abstractions (128/128 tests) and Prettier checks pass cleanly.

Whenever convenient, please take another look. Thank you!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new regression test writes to Chrome’s read-only window global, causing the browser test suite to fail before its assertions run.

1 open issue
2 resolved since last review

Review effort: Balanced

… validation when empty

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…d guard browser empty fallback

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
@hardikkaurani
Hardik K (hardikkaurani) force-pushed the fix/allowed-hosts-validation-clean branch from 888cea4 to e56aa96 Compare October 27, 2026 14:29
Copilot AI balanced review requested due to automatic review settings October 27, 2026 14:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet),

I've updated the PR to resolve the review feedback regarding browser test execution:

  • Browser Window Guard: Updated the unparsed URL fallback test in allowedHostsValidator.ts to check whether window is absent (typeof window === "undefined") before configuring the mock on globalThis. During browser test runs (test:browser in Chrome), the test directly evaluates against the native window object instead of mutating globalThis.window, eliminating the TypeError caused by attempting to assign to the read-only window getter.
  • Validation: All unit tests in @microsoft/kiota-abstractions (128/128 passed), build compilation, and Prettier formatting pass cleanly across signed-off commits.

Ready for review!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new validation path rejects supported scheme-less hostnames and valid browser-relative URLs when the allowlist is empty.

2 open issues
1 resolved since last review

Review effort: Balanced

…me-less hosts

Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
Copilot AI balanced review requested due to automatic review settings October 27, 2026 20:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet),

I've pushed an update addressing the review findings on URL validation in AllowedHostsValidator:

  • Browser-Relative URL Resolution: When in a browser environment, path- and query-relative URLs (e.g., /, /?q=1, ?q=1, /api/v1/users, ./...) are now resolved against window.location. This ensures same-origin requests validate as expected under empty and configured allowlists, while keeping bare-host and protocol-relative handling intact.
  • Scheme-Less HTTP Hostnames: Replaced the substring prefix check with explicit HTTP(S) scheme matching (/^https?:/i). Supported scheme-less hostnames beginning with http (such as httpbin.org/path) are now correctly recognized as bare hosts and validated accordingly.
  • Test Coverage: Added regression tests in allowedHostsValidator.ts covering browser-relative resolution and HTTP-prefixed scheme-less hostnames. All 130 unit tests across @microsoft/kiota-abstractions, Prettier formatting, and ESLint checks pass cleanly.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The validator accepts malformed HTTPS URLs that browsers resolve to a disallowed page origin, allowing authentication credentials to be sent to a non-allowlisted destination.

1 open issue
2 resolved since last review

Review effort: Balanced

parsed = new URL(trimmedUrl, browserBase);
} else {
const hasScheme = /^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//i.test(trimmedUrl);
const candidate = hasScheme ? trimmedUrl : trimmedUrl.startsWith("//") ? `${defaultProtocol}${trimmedUrl}` : `${defaultProtocol}//${trimmedUrl}`;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P1] Scheme-less URLs can authenticate a different browser host

On https://other.example/app/, an allowlist containing httpbin.org now accepts httpbin.org/path, because validation prepends https://. Browser fetch instead treats the unchanged string as relative, sending it to other.example. Both isUrlHostValid and validateProtocol pass, so an API key or bearer token can reach a host outside the allowlist. This input was rejected before this change.

Please resolve all browser-relative references against browserBase, including bare paths, or ensure the validated absolute URL is also the URL sent. A regression test should cover an allowlisted httpbin.org with a different browser origin.

@hardikkaurani Hardik K (hardikkaurani) Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in commit 35441f7.

  • WHATWG Browser Base Resolution: In browser environments, all relative references, including bare paths such as httpbin.org/path, are now resolved against browserBase using new URL(trimmedUrl, browserBase), matching browser fetch destination semantics.
  • Node.js Compatibility: When browserBase is undefined, supported scheme-less host behavior (https://) is preserved, while unresolvable relative paths without a base are rejected.
  • Regression Coverage: Added tests verifying browser-base resolution and allowlist enforcement, alongside coverage for absolute URLs, protocol-relative URLs, path-relative references, and credential attachment consistency through ApiKeyAuthenticationProvider.

All 38 validator tests and 142 abstractions tests pass cleanly.

Copilot AI balanced review requested due to automatic review settings November 17, 2026 06:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The validator rejects valid percent-containing paths and queries, causing credentials to be omitted, while browser-incompatible assertions and global overrides break Chrome tests.

3 open issues
2 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 17, 2026 13:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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),

I've pushed an update addressing the latest review feedback and browser CI test failures:

  • Authority-Scoped Percent Validation: Scoped the malformed percent-encoding check (/%(?![0-9a-fA-F]{2})/) specifically to the authority / bare-host candidate prior to URL parsing. This permits valid literal percent signs in URL paths and query parameters (such as /items/100%?q=50%) to be parsed as expected by the WHATWG URL parser, while continuing to reject malformed bare inputs like http%. Added regression test coverage for path and query percent characters.
  • Browser DOM Fixture Isolation: Refactored the browser test cases in allowedHostsValidator.ts to use a helper (withBrowserBaseUrl) that injects a temporary <base> element into document.head in browser environments and safely cleans it up in finally. This resolves the TypeError from attempting to mutate read-only document and window getters during Chrome test runs.
  • Environment-Aware Bare-Host Tests: Made bare-host assertions environment-aware via utils.inNodeEnv(). In Node without a base URL, scheme-less strings evaluate as bare hosts, whereas in browser environments they resolve against the test document's origin. Guarded the Node-specific Test F with it.runIf(utils.inNodeEnv()).
  • Removed ESM Spy: Removed the unsupported ESM namespace spy (vi.spyOn(utils, "inNodeEnv")) from Test H, keeping module exports intact.
  • Verification: All unit tests in @microsoft/kiota-abstractions (142/142 passed), TypeScript compilation (npm run build:esm), ESLint (npm run lint:eslint:loud), and Prettier checks pass cleanly.

Whenever convenient, please take another look and trigger the workflow run. Thanks!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human assessment is needed because URL-validation edge cases can incorrectly omit authentication from valid same-origin requests.

No open issues 🎉

3 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 17, 2026 13:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The browser test throws before its header assertion on HTTP because the temporary HTTPS base changes document.baseURI, while validateProtocol checks window.location.protocol.

1 open issue

Review effort: Balanced

Comment on lines +531 to +532
await otherProvider.authenticateRequest(reqRelative);
expect(reqRelative.headers.tryGetValue("X-OTHER-KEY")).to.deep.equal(["other-api-key"]);

@hardikkaurani Hardik K (hardikkaurani) Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch. Updated the test expectation to be protocol-aware based on window.location.protocol.

When running in an HTTP-served browser test environment, it now asserts that authenticateRequest rejects with the expected protocol error (Authentication scheme can only be used with https requests) and confirms credentials are not attached. When running under HTTPS, it validates successful authentication and header population. The relative-URL destination hostname checks (other.example) remain asserted across both cases.

Copilot AI balanced review requested due to automatic review settings November 20, 2026 17:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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),

I have updated the test in packages/abstractions/test/common/authentication/allowedHostsValidator.ts to address the browser test runner failure:

  • Protocol-Aware Browser Test Assertion: In Test H, the browser-relative authentication check now inspects window.location.protocol. When executed under an HTTP-served browser runner, it asserts rejection with the expected protocol error (Authentication scheme can only be used with https requests) and confirms credentials are not attached, while validating successful authentication and header population under HTTPS.
  • Verification: Verified TypeScript compilation (npm run build:esm), ESLint (npm run lint:eslint:loud), Prettier, and all Node test suites across @microsoft/kiota-abstractions (142/142 tests passing).

Whenever convenient, could you please approve the workflow run and review? Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants