Skip to content

Commit c09fdea

Browse files
committed
oidc: fix bug when matching GitLab environment claims
Signed-off-by: Facundo Tuesca <[email protected]>
1 parent 3c2df2d commit c09fdea

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

tests/unit/oidc/models/test_gitlab.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,59 @@ def test_lookup_fails_invalid_ci_config_ref_uri(self, environment):
7171
):
7272
gitlab.GitLabPublisher.lookup_by_claims(pretend.stub(), signed_claims)
7373

74+
@pytest.mark.parametrize("environment", ["SomeEnvironment", "SOME_ENVIRONMENT"])
75+
def test_lookup_succeeds_with_non_lowercase_environment(
76+
self, db_request, environment
77+
):
78+
# Test that we find a matching publisher when the environment claims match
79+
# If we incorrectly normalized the incoming capitalized claim, we wouldn't
80+
# find the matching publisher.
81+
stored_publisher = GitLabPublisherFactory(
82+
id="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
83+
namespace="foo",
84+
project="bar",
85+
workflow_filepath=".gitlab-ci.yml",
86+
environment=environment,
87+
)
88+
89+
signed_claims = {
90+
"project_path": "foo/bar",
91+
"ci_config_ref_uri": ("gitlab.com/foo/bar//.gitlab-ci.yml@refs/heads/main"),
92+
"environment": environment,
93+
}
94+
95+
publisher = gitlab.GitLabPublisher.lookup_by_claims(
96+
db_request.db, signed_claims
97+
)
98+
99+
assert publisher.id == stored_publisher.id
100+
assert publisher.environment == environment
101+
102+
@pytest.mark.parametrize("environment", ["SomeEnvironment", "SOME_ENVIRONMENT"])
103+
def test_lookup_is_case_sensitive_for_environment(self, db_request, environment):
104+
# Test that we don't find a matching publisher when the environment claims don't
105+
# exactly match.
106+
# If we incorrectly normalized the incoming capitalized claim, we would match
107+
# a publisher that has a different environment.
108+
GitLabPublisherFactory(
109+
id="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
110+
namespace="foo",
111+
project="bar",
112+
workflow_filepath=".gitlab-ci.yml",
113+
# stored environment is all lowercase, doesn't match incoming claims
114+
environment=environment.lower(),
115+
)
116+
117+
signed_claims = {
118+
"project_path": "foo/bar",
119+
"ci_config_ref_uri": ("gitlab.com/foo/bar//.gitlab-ci.yml@refs/heads/main"),
120+
"environment": environment,
121+
}
122+
123+
with pytest.raises(errors.InvalidPublisherError) as e:
124+
gitlab.GitLabPublisher.lookup_by_claims(db_request.db, signed_claims)
125+
assert str(e.value) == "Publisher with matching claims was not found"
126+
74127
@pytest.mark.parametrize("environment", ["", "some_environment"])
75128
@pytest.mark.parametrize(
76129
("workflow_filepath_a", "workflow_filepath_b"),

warehouse/oidc/models/gitlab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def _get_publisher_for_environment(
188188
) -> Self | None:
189189
if environment:
190190
if specific_publisher := first_true(
191-
publishers, pred=lambda p: p.environment == environment.lower()
191+
publishers, pred=lambda p: p.environment == environment
192192
):
193193
return specific_publisher
194194

0 commit comments

Comments
 (0)