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

Throw error on https:// or http:// prefix if present in allowed host value #127

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Authentication/AllowedHostsValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace Microsoft\Kiota\Abstractions\Authentication;

use InvalidArgumentException;

/**
* Class AllowedHostsValidator
*
Expand Down Expand Up @@ -40,6 +42,9 @@ public function setAllowedHosts(array $hosts): void
{
foreach ($hosts as $host) {
$host = strtolower(trim($host));
if (str_starts_with($host, "https://") || str_starts_with($host, 'http://')) {
throw new InvalidArgumentException("The host $host is not valid as it contains the scheme.");
}
if (!array_key_exists($host, $this->allowedHosts)) {
$this->allowedHosts[$host] = true;
}
Expand Down Expand Up @@ -81,4 +86,4 @@ private function extractHost(string $url): string
throw new \InvalidArgumentException("$url must contain host");
}
}
}
}
10 changes: 10 additions & 0 deletions tests/Authentication/AllowedHostsValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ public function testSetAllowedHostsSetLowercaseTrimmedDeduplicatedHosts(): void
$this->assertEquals($expected, $validator->getAllowedHosts());
}

public function testShouldThrowException(): void
{
$hosts = ["https://abc.com "];
$this->expectException(\InvalidArgumentException::class);
$validator = new AllowedHostsValidator();
$validator->setAllowedHosts($hosts);
$expected = ["abc.com"]; //duplicates should not be added to allowed hosts

}

public function testIsUrlHostValidWithValidHost(): void
{
$this->assertTrue($this->defaultValidator->isUrlHostValid("https://abc.com"));
Expand Down
Loading