Skip to content

oidc: fix bug when matching GitLab environment claims #18338

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

Merged
merged 1 commit into from
Jul 2, 2025
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
53 changes: 53 additions & 0 deletions tests/unit/oidc/models/test_gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,59 @@ def test_lookup_fails_invalid_ci_config_ref_uri(self, environment):
):
gitlab.GitLabPublisher.lookup_by_claims(pretend.stub(), signed_claims)

@pytest.mark.parametrize("environment", ["SomeEnvironment", "SOME_ENVIRONMENT"])
def test_lookup_succeeds_with_non_lowercase_environment(
self, db_request, environment
):
# Test that we find a matching publisher when the environment claims match
# If we incorrectly normalized the incoming capitalized claim, we wouldn't
# find the matching publisher.
stored_publisher = GitLabPublisherFactory(
id="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
namespace="foo",
project="bar",
workflow_filepath=".gitlab-ci.yml",
environment=environment,
)

signed_claims = {
"project_path": "foo/bar",
"ci_config_ref_uri": ("gitlab.com/foo/bar//.gitlab-ci.yml@refs/heads/main"),
"environment": environment,
}

publisher = gitlab.GitLabPublisher.lookup_by_claims(
db_request.db, signed_claims
)

assert publisher.id == stored_publisher.id
assert publisher.environment == environment

@pytest.mark.parametrize("environment", ["SomeEnvironment", "SOME_ENVIRONMENT"])
def test_lookup_is_case_sensitive_for_environment(self, db_request, environment):
# Test that we don't find a matching publisher when the environment claims don't
# exactly match.
# If we incorrectly normalized the incoming capitalized claim, we would match
# a publisher that has a different environment.
GitLabPublisherFactory(
id="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
namespace="foo",
project="bar",
workflow_filepath=".gitlab-ci.yml",
# stored environment is all lowercase, doesn't match incoming claims
environment=environment.lower(),
)

signed_claims = {
"project_path": "foo/bar",
"ci_config_ref_uri": ("gitlab.com/foo/bar//.gitlab-ci.yml@refs/heads/main"),
"environment": environment,
}

with pytest.raises(errors.InvalidPublisherError) as e:
gitlab.GitLabPublisher.lookup_by_claims(db_request.db, signed_claims)
assert str(e.value) == "Publisher with matching claims was not found"

@pytest.mark.parametrize("environment", ["", "some_environment"])
@pytest.mark.parametrize(
("workflow_filepath_a", "workflow_filepath_b"),
Expand Down
2 changes: 1 addition & 1 deletion warehouse/oidc/models/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def _get_publisher_for_environment(
) -> Self | None:
if environment:
if specific_publisher := first_true(
publishers, pred=lambda p: p.environment == environment.lower()
publishers, pred=lambda p: p.environment == environment
):
return specific_publisher

Expand Down