Skip to content
Draft
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
12 changes: 12 additions & 0 deletions tests/unit/oidc/models/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,18 @@ def test_check_repository(self, truth, claim, valid):
check = github.GitHubPublisher.__required_verifiable_claims__["repository"]
assert check(truth, claim, pretend.stub()) == valid

def test_check_event_name_invalid(self):
check = github.GitHubPublisher.__required_verifiable_claims__["event_name"]

with pytest.raises(
errors.InvalidPublisherError,
match=(
"Publishing from a workflow invoked via 'pull_request_target' "
"is not supported."
),
):
check("throwaway", "pull_request_target", pretend.stub())

@pytest.mark.parametrize(
("claim", "ref", "sha", "valid", "expected"),
[
Expand Down
19 changes: 18 additions & 1 deletion warehouse/oidc/models/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@ def _check_sub(
return f"{org}:{repo}".lower() == ground_truth.lower()


def _check_event_name(
ground_truth: str, signed_claim: str, _all_signed_claims: SignedClaims, **_kwargs,
) -> bool:
if signed_claim == "pull_request_target":
raise InvalidPublisherError(
"Publishing from a workflow invoked via 'pull_request_target' is "
"not supported."
)
else:
return True


class GitHubPublisherMixin:
"""
Common functionality for both pending and concrete GitHub OIDC publishers.
Expand All @@ -170,6 +182,7 @@ class GitHubPublisherMixin:
"repository_owner_id": check_claim_binary(str.__eq__),
"job_workflow_ref": _check_job_workflow_ref,
"jti": check_existing_jti,
"event_name": _check_event_name,
}

__required_unverifiable_claims__: set[str] = {"ref", "sha"}
Expand All @@ -186,7 +199,6 @@ class GitHubPublisherMixin:
"run_attempt",
"head_ref",
"base_ref",
"event_name",
"ref_type",
"repository_id",
"workflow",
Expand Down Expand Up @@ -275,6 +287,11 @@ def jti(self) -> str:
"""Placeholder value for JTI."""
return "placeholder"

@property
def event_name(self) -> str:
"""Placeholder value for event_name (not used)"""
return "placeholder"

def publisher_url(self, claims: SignedClaims | None = None) -> str:
base = self.publisher_base_url
sha = claims.get("sha") if claims else None
Expand Down