Skip to content
Open
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ All versions prior to 0.9.0 are untracked.

## [Unreleased]

### Fixed

* Parsing a malformed in-toto statement now includes the underlying validation
error, instead of discarding it. `StatementBuilder.build()` already did this;
`Statement(contents=...)` did not, so a rejected digest algorithm, a missing
field and a bad `_type` were indistinguishable.

## [4.5.0]

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions sigstore/dsse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ def __init__(self, contents: bytes | _Statement) -> None:
self._contents = contents
try:
self._inner = _Statement.model_validate_json(contents)
except ValidationError:
raise Error("malformed in-toto statement")
except ValidationError as e:
raise Error(f"malformed in-toto statement: {e}") from e
else:
self._contents = contents.model_dump_json(by_alias=True).encode()
self._inner = contents
Expand Down
25 changes: 24 additions & 1 deletion test/unit/test_dsse.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import pytest

from sigstore import dsse
from sigstore.dsse import InvalidEnvelope
from sigstore.dsse import Error, InvalidEnvelope


class TestEnvelope:
Expand Down Expand Up @@ -84,3 +84,26 @@ def test_multiple_signatures(self):

with pytest.raises(InvalidEnvelope, match="one signature"):
dsse.Envelope._from_json(raw)


class TestStatement:
def test_malformed_statement_reports_why(self):
# An unsupported digest algorithm is rejected by design, but the caller
# is left guessing: the same message covers a missing field, a bad
# _type, and a rejected digest. StatementBuilder.build() already
# surfaces the underlying validation error; parsing should too.
raw = json.dumps(
{
"_type": "https://in-toto.io/Statement/v1",
"subject": [{"name": "foo", "digest": {"gitCommit": "a" * 40}}],
"predicateType": "https://example.com/predicate/v1",
"predicate": {},
}
)

with pytest.raises(Error, match="malformed in-toto statement") as exc:
dsse.Statement(raw.encode())

# the cause is preserved, and names the offending field
assert exc.value.__cause__ is not None
assert "digest" in str(exc.value)