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

Validate hosted domain of user #54

Merged
merged 2 commits into from
Mar 19, 2018
Merged

Conversation

pradtke
Copy link
Contributor

@pradtke pradtke commented Mar 12, 2018

If hostedDomain is configured then validate that user is from the hostedDomain.
Per https://developers.google.com/identity/protocols/OpenIDConnect#hd-param 'Be
sure to validate that the returned ID token has an hd claim value that matches
what you expect (e.g. mycolledge.edu). Unlike the request parameter, the ID
token claim is contained within a security token from Google, so the value can
be trusted.'

*/
public function __construct($hostedDomainConfigured, $hostedDomainOfUser)
{
parent::__construct("Hosted domain mismatch '$hostedDomainOfUser' !== '$hostedDomainConfigured'");
Copy link
Member

Choose a reason for hiding this comment

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

This could be used as an XSS attack vector if the site has exception handling output. I think it would be better to only show the $hostedDomainConfigured value, as that will be developer supplied.

* @param string $hostedDomainConfigured
* @param string|null $hostedDomainOfUser
*/
public function __construct($hostedDomainConfigured, $hostedDomainOfUser)
Copy link
Member

Choose a reason for hiding this comment

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

Generally I like to see exceptions maintain the same constructor signature, with a static constructor:

class HostedDomainException extends \Exception
{
    public static function notMatchingDomain($configuredDomain)
    {
        return new static("User is not part of domain $configuredDomain");
    }
}

This preserves the ability to use $code and $previousException when necessary.

if ($this->hostedDomain === '*') {
if (empty($user->getHostedDomain())) {
throw new HostedDomainException($this->hostedDomain, $user->getHostedDomain());
}
Copy link
Member

Choose a reason for hiding this comment

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

This implies a significant change to current functionality, requiring all users to set hostedDomain. I think this check should be skipped unless hostedDomain option has been set, at least until the next major version.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I'm missing your point - this only gets checked if hostedDomain is set to '*' or is not empty, so it doesn't require everyone to configure a hostedDomain.

It does affect everyone with hostedDomain configured though I expect those people don't realize how easily that can be bypassed and already assume the library validates it.

@shadowhand
Copy link
Member

Rebase on master and the Scrutinizer error will be fixed.

pradtke added 2 commits March 19, 2018 09:56
If hostedDomain is configured then validate that user is from the hostedDomain.
Per https://developers.google.com/identity/protocols/OpenIDConnect#hd-param 'Be
sure to validate that the returned ID token has an hd claim value that matches
what you expect (e.g. mycolledge.edu). Unlike the request parameter, the ID
token claim is contained within a security token from Google, so the value can
be trusted.'
- Use static constructor to preserve default constructors
- Don't include user's hd/domain incase it contains XSS'able data
@pradtke
Copy link
Contributor Author

pradtke commented Mar 19, 2018

👍Code has been rebased on master

@shadowhand shadowhand merged commit c0faed2 into thephpleague:master Mar 19, 2018
@shadowhand
Copy link
Member

Released in version 2.2.0.

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

Successfully merging this pull request may close these issues.

2 participants