Skip to content
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
21 changes: 21 additions & 0 deletions tests/unit/oidc/models/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,27 @@ def test_github_publisher_verifies(self, monkeypatch, environment, missing_claim
optional_verifiable_claims
)

@pytest.mark.parametrize(
("truth", "claim", "valid"),
[
# invalid: claim should never be empty or missing
("", None, False),
("foo", None, False),
("", "", False),
("foo", "", False),
# valid: exact and case-insensitive matches
("foo", "foo", True),
("Foo", "foo", True),
("Foo", "Foo", True),
("foo", "Foo", True),
("FOO", "foo", True),
("foo", "FOO", True),
],
)
def test_check_repository(self, truth, claim, valid):
check = github.GitHubPublisher.__required_verifiable_claims__["repository"]
assert check(truth, claim, pretend.stub()) == valid

@pytest.mark.parametrize(
("claim", "ref", "sha", "valid", "expected"),
[
Expand Down
11 changes: 10 additions & 1 deletion warehouse/oidc/models/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
)


def _check_repository(ground_truth, signed_claim, all_signed_claims):
# Defensive: GitHub should never give us an empty repository claim.
if not signed_claim:
return False

# GitHub repository names are case-insensitive.
return signed_claim.lower() == ground_truth.lower()


def _check_job_workflow_ref(ground_truth, signed_claim, all_signed_claims):
# We expect a string formatted as follows:
# OWNER/REPO/.github/workflows/WORKFLOW.yml@REF
Expand Down Expand Up @@ -112,7 +121,7 @@ class GitHubPublisherMixin:

__required_verifiable_claims__: dict[str, CheckClaimCallable[Any]] = {
"sub": _check_sub,
"repository": check_claim_binary(str.__eq__),
"repository": _check_repository,
"repository_owner": check_claim_binary(str.__eq__),
"repository_owner_id": check_claim_binary(str.__eq__),
"job_workflow_ref": _check_job_workflow_ref,
Expand Down