-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request from GHSA-55m3-44xf-hg4h
GoogleOAuthenticator.hosted_domain: check against hd, not email's domain
- Loading branch information
Showing
2 changed files
with
121 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,22 +5,23 @@ | |
from unittest import mock | ||
|
||
from pytest import fixture, mark, raises | ||
from tornado.web import HTTPError | ||
from traitlets.config import Config | ||
|
||
from ..google import GoogleOAuthenticator | ||
from .mocks import setup_oauth_mock | ||
|
||
|
||
def user_model(email, username="user1"): | ||
def user_model(email, username="user1", hd=None): | ||
"""Return a user model""" | ||
return { | ||
model = { | ||
'sub': hashlib.md5(email.encode()).hexdigest(), | ||
'email': email, | ||
'custom': username, | ||
'hd': email.split('@')[1], | ||
'verified_email': True, | ||
} | ||
if hd: | ||
model['hd'] = hd | ||
return model | ||
|
||
|
||
@fixture | ||
|
@@ -187,37 +188,49 @@ async def test_google( | |
assert auth_model == None | ||
|
||
|
||
async def test_hosted_domain_single_entry(google_client): | ||
@mark.parametrize( | ||
"test_variation_id,user_email,user_hd,expect_username,expect_allowed,expect_admin", | ||
[ | ||
("01", "[email protected]", "ok-hd.org", "user1", True, True), | ||
("02", "[email protected]", "ok-hd.org", "user2", True, None), | ||
("03", "[email protected]", "ok-hd.org", None, False, None), | ||
("04", "[email protected]", "", None, False, None), | ||
("05", "[email protected]", "", None, False, None), | ||
# Test variation 06 below isn't believed to be possible, but since we | ||
# aren't sure this test clarifies what we expect to happen. | ||
("06", "[email protected]", "ok-hd.org", "[email protected]", True, None), | ||
], | ||
) | ||
async def test_hosted_domain_single_entry( | ||
google_client, | ||
test_variation_id, | ||
user_email, | ||
user_hd, | ||
expect_username, | ||
expect_allowed, | ||
expect_admin, | ||
): | ||
""" | ||
Tests that sign in is restricted to the listed domain and that the username | ||
represents the part before the `@domain.com` as expected when hosted_domain | ||
contains a single entry. | ||
""" | ||
c = Config() | ||
c.GoogleOAuthenticator.hosted_domain = ["In-Hosted-Domain.com"] | ||
c.GoogleOAuthenticator.hosted_domain = ["ok-hd.org"] | ||
c.GoogleOAuthenticator.admin_users = {"user1"} | ||
c.GoogleOAuthenticator.allowed_users = {"user2"} | ||
c.GoogleOAuthenticator.allowed_users = {"user2", "blocked", "[email protected]"} | ||
c.GoogleOAuthenticator.blocked_users = {"blocked"} | ||
authenticator = GoogleOAuthenticator(config=c) | ||
|
||
handled_user_model = user_model("[email protected]") | ||
handled_user_model = user_model(user_email, hd=user_hd) | ||
handler = google_client.handler_for_user(handled_user_model) | ||
auth_model = await authenticator.get_authenticated_user(handler, None) | ||
assert auth_model | ||
assert auth_model["name"] == "user1" | ||
assert auth_model["admin"] == True | ||
|
||
handled_user_model = user_model("[email protected]") | ||
handler = google_client.handler_for_user(handled_user_model) | ||
auth_model = await authenticator.get_authenticated_user(handler, None) | ||
assert auth_model | ||
assert auth_model["name"] == "user2" | ||
assert auth_model["admin"] == None | ||
|
||
handled_user_model = user_model("[email protected]") | ||
handler = google_client.handler_for_user(handled_user_model) | ||
with raises(HTTPError) as exc: | ||
await authenticator.get_authenticated_user(handler, None) | ||
assert exc.value.status_code == 403 | ||
if expect_allowed: | ||
assert auth_model | ||
assert auth_model["name"] == expect_username | ||
assert auth_model["admin"] == expect_admin | ||
else: | ||
assert auth_model == None | ||
|
||
|
||
@mark.parametrize( | ||
|
@@ -235,37 +248,49 @@ async def test_check_allowed_no_auth_state(google_client, name, allowed): | |
assert await authenticator.check_allowed(name, None) | ||
|
||
|
||
async def test_hosted_domain_multiple_entries(google_client): | ||
@mark.parametrize( | ||
"test_variation_id,user_email,user_hd,expect_username,expect_allowed", | ||
[ | ||
("01", "[email protected]", "ok-hd1.org", "[email protected]", True), | ||
("02", "[email protected]", "ok-hd2.org", "[email protected]", True), | ||
("03", "[email protected]", "ok-hd1.org", None, False), | ||
("04", "[email protected]", "", None, False), | ||
("05", "[email protected]", "", None, False), | ||
# Test variation 06 below isn't believed to be possible, but since we | ||
# aren't sure this test clarifies what we expect to happen. | ||
("06", "[email protected]", "ok-hd1.org", "[email protected]", True), | ||
], | ||
) | ||
async def test_hosted_domain_multiple_entries( | ||
google_client, | ||
test_variation_id, | ||
user_email, | ||
user_hd, | ||
expect_username, | ||
expect_allowed, | ||
): | ||
""" | ||
Tests that sign in is restricted to the listed domains and that the username | ||
represents the full email as expected when hosted_domain contains multiple | ||
entries. | ||
""" | ||
c = Config() | ||
c.GoogleOAuthenticator.hosted_domain = [ | ||
"In-Hosted-Domain1.com", | ||
"In-Hosted-Domain2.com", | ||
"ok-hd1.org", | ||
"ok-hd2.ORG", | ||
] | ||
c.GoogleOAuthenticator.blocked_users = ["[email protected]"] | ||
c.GoogleOAuthenticator.allow_all = True | ||
authenticator = GoogleOAuthenticator(config=c) | ||
|
||
handled_user_model = user_model("[email protected]") | ||
handled_user_model = user_model(user_email, hd=user_hd) | ||
handler = google_client.handler_for_user(handled_user_model) | ||
auth_model = await authenticator.get_authenticated_user(handler, None) | ||
assert auth_model | ||
assert auth_model["name"] == "[email protected]" | ||
|
||
handled_user_model = user_model("[email protected]") | ||
handler = google_client.handler_for_user(handled_user_model) | ||
auth_model = await authenticator.get_authenticated_user(handler, None) | ||
assert auth_model | ||
assert auth_model["name"] == "[email protected]" | ||
|
||
handled_user_model = user_model("[email protected]") | ||
handler = google_client.handler_for_user(handled_user_model) | ||
with raises(HTTPError) as exc: | ||
await authenticator.get_authenticated_user(handler, None) | ||
assert exc.value.status_code == 403 | ||
if expect_allowed: | ||
assert auth_model | ||
assert auth_model["name"] == expect_username | ||
else: | ||
assert auth_model == None | ||
|
||
|
||
@mark.parametrize( | ||
|