Skip to content

Commit 96ce64a

Browse files
committed
pylock: validate attestation-identities kind field
1 parent 394ed84 commit 96ce64a

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/packaging/pylock.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,13 @@ def _from_dict(cls, d: Mapping[str, Any]) -> Self:
541541
"Exactly one of vcs, directory, archive must be set "
542542
"if sdist and wheels are not set"
543543
)
544+
for i, attestation_identity in enumerate(package.attestation_identities or []):
545+
try:
546+
_get_required(attestation_identity, str, "kind")
547+
except Exception as e:
548+
raise PylockValidationError(
549+
e, context=f"attestation-identities[{i}]"
550+
) from e
544551
return package
545552

546553
@property

tests/test_pylock.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,3 +521,58 @@ def test_validate_sequence_of_str() -> None:
521521
assert str(exc_info.value) == (
522522
"Unexpected type str (expected Sequence) in 'dependency-groups'"
523523
)
524+
525+
526+
def test_validate_attestation_identity_missing_kind() -> None:
527+
data = {
528+
"lock-version": "1.0",
529+
"created-by": "pip",
530+
"packages": [
531+
{
532+
"name": "example",
533+
"version": "1.0",
534+
"directory": {
535+
"path": ".",
536+
},
537+
"attestation-identities": [
538+
{
539+
# missing "kind" field
540+
"value": "some-value",
541+
}
542+
],
543+
}
544+
],
545+
}
546+
with pytest.raises(PylockValidationError) as exc_info:
547+
Pylock.from_dict(data)
548+
assert str(exc_info.value) == (
549+
"Missing required value in 'packages[0].attestation-identities[0].kind'"
550+
)
551+
552+
553+
def test_validate_attestation_identity_invalid_kind() -> None:
554+
data = {
555+
"lock-version": "1.0",
556+
"created-by": "pip",
557+
"packages": [
558+
{
559+
"name": "example",
560+
"version": "1.0",
561+
"directory": {
562+
"path": ".",
563+
},
564+
"attestation-identities": [
565+
{
566+
"kind": 123,
567+
"value": "some-value",
568+
}
569+
],
570+
}
571+
],
572+
}
573+
with pytest.raises(PylockValidationError) as exc_info:
574+
Pylock.from_dict(data)
575+
assert str(exc_info.value) == (
576+
"Unexpected type int (expected str) "
577+
"in 'packages[0].attestation-identities[0].kind'"
578+
)

0 commit comments

Comments
 (0)