passmcp-sdk 0.0.11__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- passmcp_sdk/__init__.py +155 -0
- passmcp_sdk/attestation.py +433 -0
- passmcp_sdk/client.py +271 -0
- passmcp_sdk/http_transport.py +391 -0
- passmcp_sdk/http_wire.py +170 -0
- passmcp_sdk/oauth.py +298 -0
- passmcp_sdk/origin.py +53 -0
- passmcp_sdk/py.typed +2 -0
- passmcp_sdk/schema.py +145 -0
- passmcp_sdk/server.py +470 -0
- passmcp_sdk/stdio.py +164 -0
- passmcp_sdk/types.py +289 -0
- passmcp_sdk-0.0.11.dist-info/METADATA +230 -0
- passmcp_sdk-0.0.11.dist-info/RECORD +17 -0
- passmcp_sdk-0.0.11.dist-info/WHEEL +5 -0
- passmcp_sdk-0.0.11.dist-info/licenses/LICENSE +73 -0
- passmcp_sdk-0.0.11.dist-info/top_level.txt +1 -0
passmcp_sdk/__init__.py
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Sebastien Rousseau <sebastienrousseau@users.noreply.github.com>
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
"""Apache-2.0 Python MCP client/server SDK and offline passmcp attestation verifier."""
|
|
5
|
+
|
|
6
|
+
from passmcp_sdk.attestation import (
|
|
7
|
+
PREDICATE_TYPE,
|
|
8
|
+
STATEMENT_TYPE,
|
|
9
|
+
SUBJECT_KIND_DESCRIPTOR,
|
|
10
|
+
AttestationError,
|
|
11
|
+
Basis,
|
|
12
|
+
Counts,
|
|
13
|
+
Evaluation,
|
|
14
|
+
Instrument,
|
|
15
|
+
NoSuchCheckError,
|
|
16
|
+
Score,
|
|
17
|
+
ServerIdentity,
|
|
18
|
+
Statement,
|
|
19
|
+
Subject,
|
|
20
|
+
Target,
|
|
21
|
+
Verdict,
|
|
22
|
+
parse_statement,
|
|
23
|
+
subject_for,
|
|
24
|
+
target_descriptor,
|
|
25
|
+
)
|
|
26
|
+
from passmcp_sdk.client import McpClient, Policy, PolicyViolationError, http_transport
|
|
27
|
+
from passmcp_sdk.http_transport import (
|
|
28
|
+
HttpClientTransport,
|
|
29
|
+
HttpRequest,
|
|
30
|
+
HttpResponse,
|
|
31
|
+
StreamableHttpHandler,
|
|
32
|
+
)
|
|
33
|
+
from passmcp_sdk.oauth import (
|
|
34
|
+
AuthorizationServerMetadata,
|
|
35
|
+
BearerChallenge,
|
|
36
|
+
OAuthError,
|
|
37
|
+
ProtectedResourceMetadata,
|
|
38
|
+
authorization_server_metadata_url,
|
|
39
|
+
build_bearer_challenge,
|
|
40
|
+
compute_s256_challenge,
|
|
41
|
+
generate_pkce_verifier,
|
|
42
|
+
parse_bearer_challenge,
|
|
43
|
+
protected_resource_metadata_url,
|
|
44
|
+
protected_resource_metadata_urls,
|
|
45
|
+
validate_authorization_server_metadata,
|
|
46
|
+
validate_protected_resource_metadata,
|
|
47
|
+
verify_s256,
|
|
48
|
+
)
|
|
49
|
+
from passmcp_sdk.schema import CacheHints, validate_object_schema
|
|
50
|
+
from passmcp_sdk.server import McpServer, OriginGuard
|
|
51
|
+
from passmcp_sdk.stdio import StdioTransport, serve_stdio
|
|
52
|
+
from passmcp_sdk.types import (
|
|
53
|
+
HEADER_MISMATCH,
|
|
54
|
+
INTERNAL_ERROR,
|
|
55
|
+
INVALID_PARAMS,
|
|
56
|
+
INVALID_REQUEST,
|
|
57
|
+
LEGACY_RESOURCE_NOT_FOUND,
|
|
58
|
+
META_CLIENT_CAPABILITIES,
|
|
59
|
+
META_PROTOCOL_VERSION,
|
|
60
|
+
META_SERVER_INFO,
|
|
61
|
+
METHOD_NOT_FOUND,
|
|
62
|
+
PARSE_ERROR,
|
|
63
|
+
REVISION_2025_06_18,
|
|
64
|
+
REVISION_2025_11_25,
|
|
65
|
+
REVISION_2026_07_28,
|
|
66
|
+
SUPPORTED_REVISIONS,
|
|
67
|
+
UNSUPPORTED_PROTOCOL_VERSION,
|
|
68
|
+
CallToolResult,
|
|
69
|
+
JsonRpcError,
|
|
70
|
+
Prompt,
|
|
71
|
+
PromptArgument,
|
|
72
|
+
PromptMessage,
|
|
73
|
+
Resource,
|
|
74
|
+
ResourceContent,
|
|
75
|
+
ResourceTemplate,
|
|
76
|
+
Tool,
|
|
77
|
+
ToolAnnotations,
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
__version__ = "0.0.11"
|
|
81
|
+
|
|
82
|
+
__all__ = [
|
|
83
|
+
"AttestationError",
|
|
84
|
+
"AuthorizationServerMetadata",
|
|
85
|
+
"Basis",
|
|
86
|
+
"BearerChallenge",
|
|
87
|
+
"CacheHints",
|
|
88
|
+
"CallToolResult",
|
|
89
|
+
"Counts",
|
|
90
|
+
"Evaluation",
|
|
91
|
+
"HEADER_MISMATCH",
|
|
92
|
+
"UNSUPPORTED_PROTOCOL_VERSION",
|
|
93
|
+
"HttpClientTransport",
|
|
94
|
+
"HttpRequest",
|
|
95
|
+
"HttpResponse",
|
|
96
|
+
"INTERNAL_ERROR",
|
|
97
|
+
"INVALID_PARAMS",
|
|
98
|
+
"INVALID_REQUEST",
|
|
99
|
+
"Instrument",
|
|
100
|
+
"JsonRpcError",
|
|
101
|
+
"LEGACY_RESOURCE_NOT_FOUND",
|
|
102
|
+
"META_CLIENT_CAPABILITIES",
|
|
103
|
+
"META_PROTOCOL_VERSION",
|
|
104
|
+
"META_SERVER_INFO",
|
|
105
|
+
"METHOD_NOT_FOUND",
|
|
106
|
+
"McpClient",
|
|
107
|
+
"McpServer",
|
|
108
|
+
"NoSuchCheckError",
|
|
109
|
+
"OAuthError",
|
|
110
|
+
"OriginGuard",
|
|
111
|
+
"PARSE_ERROR",
|
|
112
|
+
"PREDICATE_TYPE",
|
|
113
|
+
"Policy",
|
|
114
|
+
"PolicyViolationError",
|
|
115
|
+
"Prompt",
|
|
116
|
+
"PromptArgument",
|
|
117
|
+
"PromptMessage",
|
|
118
|
+
"ProtectedResourceMetadata",
|
|
119
|
+
"REVISION_2025_06_18",
|
|
120
|
+
"REVISION_2025_11_25",
|
|
121
|
+
"REVISION_2026_07_28",
|
|
122
|
+
"Resource",
|
|
123
|
+
"ResourceContent",
|
|
124
|
+
"ResourceTemplate",
|
|
125
|
+
"STATEMENT_TYPE",
|
|
126
|
+
"SUBJECT_KIND_DESCRIPTOR",
|
|
127
|
+
"SUPPORTED_REVISIONS",
|
|
128
|
+
"Score",
|
|
129
|
+
"ServerIdentity",
|
|
130
|
+
"Statement",
|
|
131
|
+
"StdioTransport",
|
|
132
|
+
"StreamableHttpHandler",
|
|
133
|
+
"Subject",
|
|
134
|
+
"Target",
|
|
135
|
+
"Tool",
|
|
136
|
+
"ToolAnnotations",
|
|
137
|
+
"Verdict",
|
|
138
|
+
"__version__",
|
|
139
|
+
"authorization_server_metadata_url",
|
|
140
|
+
"build_bearer_challenge",
|
|
141
|
+
"compute_s256_challenge",
|
|
142
|
+
"generate_pkce_verifier",
|
|
143
|
+
"http_transport",
|
|
144
|
+
"parse_bearer_challenge",
|
|
145
|
+
"parse_statement",
|
|
146
|
+
"protected_resource_metadata_url",
|
|
147
|
+
"protected_resource_metadata_urls",
|
|
148
|
+
"serve_stdio",
|
|
149
|
+
"subject_for",
|
|
150
|
+
"target_descriptor",
|
|
151
|
+
"validate_authorization_server_metadata",
|
|
152
|
+
"validate_object_schema",
|
|
153
|
+
"validate_protected_resource_metadata",
|
|
154
|
+
"verify_s256",
|
|
155
|
+
]
|
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Sebastien Rousseau <sebastienrousseau@users.noreply.github.com>
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
"""Offline verifier for in-toto passmcp MCP evaluation attestations."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import hashlib
|
|
9
|
+
import json
|
|
10
|
+
from collections.abc import Mapping
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
from datetime import datetime
|
|
13
|
+
from typing import Any
|
|
14
|
+
|
|
15
|
+
STATEMENT_TYPE = "https://in-toto.io/Statement/v1"
|
|
16
|
+
PREDICATE_TYPE = "https://satellion.com/attestation/mcp-evaluation/v1"
|
|
17
|
+
SUBJECT_KIND_DESCRIPTOR = "mcp-target-descriptor"
|
|
18
|
+
VALID_TRANSPORTS = frozenset({"http", "stdio"})
|
|
19
|
+
VALID_STATUSES = frozenset({"pass", "warn", "fail", "skip", "info"})
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class AttestationError(ValueError):
|
|
23
|
+
"""Raised when an attestation statement fails structural or digest validation."""
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class NoSuchCheckError(LookupError):
|
|
27
|
+
"""Raised when a statement carries no verdict for the requested check ID."""
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclass(frozen=True)
|
|
31
|
+
class Subject:
|
|
32
|
+
"""in-toto statement subject entry."""
|
|
33
|
+
|
|
34
|
+
name: str
|
|
35
|
+
digest: Mapping[str, str]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@dataclass(frozen=True)
|
|
39
|
+
class ServerIdentity:
|
|
40
|
+
"""Optional server identity recorded during evaluation."""
|
|
41
|
+
|
|
42
|
+
name: str = ""
|
|
43
|
+
version: str = ""
|
|
44
|
+
protocol_version: str = ""
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclass(frozen=True)
|
|
48
|
+
class Target:
|
|
49
|
+
"""Evaluated MCP server target descriptor."""
|
|
50
|
+
|
|
51
|
+
transport: str
|
|
52
|
+
endpoint: str
|
|
53
|
+
server: ServerIdentity | None = None
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass(frozen=True)
|
|
57
|
+
class Basis:
|
|
58
|
+
"""Evaluation specification, rubric, and check inventory basis."""
|
|
59
|
+
|
|
60
|
+
spec_revision: str = ""
|
|
61
|
+
rubric: str = ""
|
|
62
|
+
check_inventory: str = ""
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@dataclass(frozen=True)
|
|
66
|
+
class Instrument:
|
|
67
|
+
"""Producer tool metadata."""
|
|
68
|
+
|
|
69
|
+
name: str
|
|
70
|
+
version: str
|
|
71
|
+
report_schema_version: int = 0
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@dataclass(frozen=True)
|
|
75
|
+
class Verdict:
|
|
76
|
+
"""Single check verdict in an evaluation statement."""
|
|
77
|
+
|
|
78
|
+
id: str
|
|
79
|
+
phase: str
|
|
80
|
+
status: str
|
|
81
|
+
severity: str = ""
|
|
82
|
+
evidence: tuple[str, ...] = ()
|
|
83
|
+
doc: str = ""
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
@dataclass(frozen=True)
|
|
87
|
+
class Counts:
|
|
88
|
+
"""Tallied verdict counts by status."""
|
|
89
|
+
|
|
90
|
+
passed: int = 0
|
|
91
|
+
warn: int = 0
|
|
92
|
+
fail: int = 0
|
|
93
|
+
skip: int = 0
|
|
94
|
+
info: int = 0
|
|
95
|
+
|
|
96
|
+
def to_status_map(self) -> dict[str, int]:
|
|
97
|
+
return {
|
|
98
|
+
"pass": self.passed,
|
|
99
|
+
"warn": self.warn,
|
|
100
|
+
"fail": self.fail,
|
|
101
|
+
"skip": self.skip,
|
|
102
|
+
"info": self.info,
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
@dataclass(frozen=True)
|
|
107
|
+
class Score:
|
|
108
|
+
"""Rubric-backed numeric score and letter grade."""
|
|
109
|
+
|
|
110
|
+
total: float
|
|
111
|
+
grade: str
|
|
112
|
+
categories_assessed: int = 0
|
|
113
|
+
categories_total: int = 0
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
@dataclass(frozen=True)
|
|
117
|
+
class Evaluation:
|
|
118
|
+
"""Decoded mcp-evaluation/v1 predicate."""
|
|
119
|
+
|
|
120
|
+
subject_kind: str
|
|
121
|
+
target: Target
|
|
122
|
+
judged_against: Basis
|
|
123
|
+
instrument: Instrument
|
|
124
|
+
ran_at: str
|
|
125
|
+
took: str
|
|
126
|
+
verdicts: tuple[Verdict, ...]
|
|
127
|
+
counts: Counts
|
|
128
|
+
score: Score | None = None
|
|
129
|
+
blocked: str = ""
|
|
130
|
+
trace_id: str = ""
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
@dataclass(frozen=True)
|
|
134
|
+
class Statement:
|
|
135
|
+
"""Verified in-toto Statement/v1 carrying an mcp-evaluation/v1 predicate."""
|
|
136
|
+
|
|
137
|
+
type: str
|
|
138
|
+
subject: tuple[Subject, ...]
|
|
139
|
+
predicate_type: str
|
|
140
|
+
predicate: Evaluation
|
|
141
|
+
|
|
142
|
+
@property
|
|
143
|
+
def score_total(self) -> float | None:
|
|
144
|
+
return self.predicate.score.total if self.predicate.score is not None else None
|
|
145
|
+
|
|
146
|
+
@property
|
|
147
|
+
def score_grade(self) -> str | None:
|
|
148
|
+
return self.predicate.score.grade if self.predicate.score is not None else None
|
|
149
|
+
|
|
150
|
+
def covers(self, transport: str, endpoint: str) -> bool:
|
|
151
|
+
"""Report whether the subject digest covers the given transport and endpoint."""
|
|
152
|
+
if len(self.subject) != 1:
|
|
153
|
+
return False
|
|
154
|
+
descriptor = target_descriptor(transport, endpoint)
|
|
155
|
+
found, mismatch = _check_digest_set(self.subject[0].digest, descriptor)
|
|
156
|
+
return found and mismatch == ""
|
|
157
|
+
|
|
158
|
+
def verdict_for(self, check_id: str) -> Verdict:
|
|
159
|
+
"""Return the first verdict recorded for check_id or raise NoSuchCheckError."""
|
|
160
|
+
for verdict in self.predicate.verdicts:
|
|
161
|
+
if verdict.id == check_id:
|
|
162
|
+
return verdict
|
|
163
|
+
raise NoSuchCheckError(
|
|
164
|
+
f"attestation: the statement carries no verdict for that check: {check_id}"
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def target_descriptor(transport: str, endpoint: str) -> bytes:
|
|
169
|
+
"""Compute the canonical UTF-8 target descriptor bytes."""
|
|
170
|
+
return f"{transport}\n{endpoint.strip()}".encode()
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def subject_for(transport: str, endpoint: str) -> Subject:
|
|
174
|
+
"""Construct a canonical SHA-256 subject entry for (transport, endpoint)."""
|
|
175
|
+
digest = hashlib.sha256(target_descriptor(transport, endpoint)).hexdigest()
|
|
176
|
+
return Subject(name=endpoint, digest={"sha256": digest})
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def _check_digest_set(digest_map: Mapping[str, str], descriptor: bytes) -> tuple[bool, str]:
|
|
180
|
+
found = False
|
|
181
|
+
mismatch = ""
|
|
182
|
+
known = (
|
|
183
|
+
("sha256", hashlib.sha256(descriptor).hexdigest()),
|
|
184
|
+
("sha512", hashlib.sha512(descriptor).hexdigest()),
|
|
185
|
+
)
|
|
186
|
+
for alg, expected in known:
|
|
187
|
+
if alg not in digest_map:
|
|
188
|
+
continue
|
|
189
|
+
found = True
|
|
190
|
+
if digest_map[alg] != expected and not mismatch:
|
|
191
|
+
mismatch = alg
|
|
192
|
+
return found, mismatch
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def _require_mapping(value: Any, label: str) -> Mapping[str, Any]:
|
|
196
|
+
if not isinstance(value, Mapping):
|
|
197
|
+
raise AttestationError(f"attestation: {label} must be an object")
|
|
198
|
+
return value
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def _str_field(obj: Mapping[str, Any], key: str) -> str:
|
|
202
|
+
val = obj.get(key, "")
|
|
203
|
+
if val is None:
|
|
204
|
+
return ""
|
|
205
|
+
if not isinstance(val, str):
|
|
206
|
+
raise AttestationError(f"attestation: {key} must be a string")
|
|
207
|
+
return val
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def _int_field(obj: Mapping[str, Any], key: str) -> int:
|
|
211
|
+
val = obj.get(key, 0)
|
|
212
|
+
if isinstance(val, bool) or not isinstance(val, int):
|
|
213
|
+
raise AttestationError(f"attestation: {key} must be an integer")
|
|
214
|
+
return val
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def _is_valid_timestamp(ran_at: str) -> bool:
|
|
218
|
+
trimmed = ran_at.strip()
|
|
219
|
+
if not trimmed:
|
|
220
|
+
return False
|
|
221
|
+
try:
|
|
222
|
+
parsed = datetime.fromisoformat(trimmed)
|
|
223
|
+
except ValueError:
|
|
224
|
+
return False
|
|
225
|
+
return parsed.year > 1
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def _parse_subjects(raw_subjects: Any) -> tuple[Subject, ...]:
|
|
229
|
+
if raw_subjects is None:
|
|
230
|
+
return ()
|
|
231
|
+
if not isinstance(raw_subjects, list):
|
|
232
|
+
raise AttestationError("attestation: subject must be an array")
|
|
233
|
+
subjects: list[Subject] = []
|
|
234
|
+
for item in raw_subjects:
|
|
235
|
+
sub = _require_mapping(item, "subject entry")
|
|
236
|
+
name = _str_field(sub, "name")
|
|
237
|
+
raw_digest = sub.get("digest")
|
|
238
|
+
if raw_digest is None:
|
|
239
|
+
digest_map: dict[str, str] = {}
|
|
240
|
+
else:
|
|
241
|
+
mapped = _require_mapping(raw_digest, "subject.digest")
|
|
242
|
+
digest_map = {}
|
|
243
|
+
for k, v in mapped.items():
|
|
244
|
+
if not isinstance(k, str) or not isinstance(v, str):
|
|
245
|
+
raise AttestationError("attestation: subject.digest entries must be strings")
|
|
246
|
+
digest_map[k] = v
|
|
247
|
+
subjects.append(Subject(name=name, digest=digest_map))
|
|
248
|
+
return tuple(subjects)
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def _parse_target(raw_target: Any) -> Target:
|
|
252
|
+
obj = _require_mapping(raw_target if raw_target is not None else {}, "predicate.target")
|
|
253
|
+
raw_srv = obj.get("server")
|
|
254
|
+
server: ServerIdentity | None = None
|
|
255
|
+
if raw_srv is not None:
|
|
256
|
+
srv_map = _require_mapping(raw_srv, "predicate.target.server")
|
|
257
|
+
server = ServerIdentity(
|
|
258
|
+
name=_str_field(srv_map, "name"),
|
|
259
|
+
version=_str_field(srv_map, "version"),
|
|
260
|
+
protocol_version=_str_field(srv_map, "protocolVersion"),
|
|
261
|
+
)
|
|
262
|
+
return Target(
|
|
263
|
+
transport=_str_field(obj, "transport"),
|
|
264
|
+
endpoint=_str_field(obj, "endpoint"),
|
|
265
|
+
server=server,
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def _parse_verdicts(raw_verdicts: Any) -> tuple[Verdict, ...]:
|
|
270
|
+
if raw_verdicts is None:
|
|
271
|
+
return ()
|
|
272
|
+
if not isinstance(raw_verdicts, list):
|
|
273
|
+
raise AttestationError("attestation: predicate.verdicts must be an array")
|
|
274
|
+
out: list[Verdict] = []
|
|
275
|
+
for entry in raw_verdicts:
|
|
276
|
+
item = _require_mapping(entry, "verdict")
|
|
277
|
+
raw_ev = item.get("evidence")
|
|
278
|
+
evidence: tuple[str, ...] = ()
|
|
279
|
+
if raw_ev is not None:
|
|
280
|
+
if not isinstance(raw_ev, list) or not all(isinstance(x, str) for x in raw_ev):
|
|
281
|
+
raise AttestationError("attestation: verdict.evidence must be a string array")
|
|
282
|
+
evidence = tuple(raw_ev)
|
|
283
|
+
out.append(
|
|
284
|
+
Verdict(
|
|
285
|
+
id=_str_field(item, "id"),
|
|
286
|
+
phase=_str_field(item, "phase"),
|
|
287
|
+
status=_str_field(item, "status"),
|
|
288
|
+
severity=_str_field(item, "severity"),
|
|
289
|
+
evidence=evidence,
|
|
290
|
+
doc=_str_field(item, "doc"),
|
|
291
|
+
)
|
|
292
|
+
)
|
|
293
|
+
return tuple(out)
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def _parse_score(raw_score: Any) -> Score | None:
|
|
297
|
+
if raw_score is None:
|
|
298
|
+
return None
|
|
299
|
+
obj = _require_mapping(raw_score, "predicate.score")
|
|
300
|
+
total = obj.get("total", 0)
|
|
301
|
+
if isinstance(total, bool) or not isinstance(total, (int, float)):
|
|
302
|
+
raise AttestationError("attestation: score.total must be numeric")
|
|
303
|
+
return Score(
|
|
304
|
+
total=float(total),
|
|
305
|
+
grade=_str_field(obj, "grade"),
|
|
306
|
+
categories_assessed=_int_field(obj, "categoriesAssessed"),
|
|
307
|
+
categories_total=_int_field(obj, "categoriesTotal"),
|
|
308
|
+
)
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
def _parse_evaluation(raw_pred: Any) -> Evaluation:
|
|
312
|
+
pred = _require_mapping(raw_pred if raw_pred is not None else {}, "predicate")
|
|
313
|
+
basis_map = _require_mapping(pred.get("judgedAgainst") or {}, "predicate.judgedAgainst")
|
|
314
|
+
inst_map = _require_mapping(pred.get("instrument") or {}, "predicate.instrument")
|
|
315
|
+
counts_map = _require_mapping(pred.get("counts") or {}, "predicate.counts")
|
|
316
|
+
return Evaluation(
|
|
317
|
+
subject_kind=_str_field(pred, "subjectKind"),
|
|
318
|
+
target=_parse_target(pred.get("target")),
|
|
319
|
+
judged_against=Basis(
|
|
320
|
+
spec_revision=_str_field(basis_map, "specRevision"),
|
|
321
|
+
rubric=_str_field(basis_map, "rubric"),
|
|
322
|
+
check_inventory=_str_field(basis_map, "checkInventory"),
|
|
323
|
+
),
|
|
324
|
+
instrument=Instrument(
|
|
325
|
+
name=_str_field(inst_map, "name"),
|
|
326
|
+
version=_str_field(inst_map, "version"),
|
|
327
|
+
report_schema_version=_int_field(inst_map, "reportSchemaVersion"),
|
|
328
|
+
),
|
|
329
|
+
ran_at=_str_field(pred, "ranAt"),
|
|
330
|
+
took=_str_field(pred, "took"),
|
|
331
|
+
verdicts=_parse_verdicts(pred.get("verdicts")),
|
|
332
|
+
counts=Counts(
|
|
333
|
+
passed=_int_field(counts_map, "pass"),
|
|
334
|
+
warn=_int_field(counts_map, "warn"),
|
|
335
|
+
fail=_int_field(counts_map, "fail"),
|
|
336
|
+
skip=_int_field(counts_map, "skip"),
|
|
337
|
+
info=_int_field(counts_map, "info"),
|
|
338
|
+
),
|
|
339
|
+
score=_parse_score(pred.get("score")),
|
|
340
|
+
blocked=_str_field(pred, "blocked"),
|
|
341
|
+
trace_id=_str_field(pred, "traceId"),
|
|
342
|
+
)
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def _validate_subject(subjects: tuple[Subject, ...], target: Target, problems: list[str]) -> None:
|
|
346
|
+
if len(subjects) == 0:
|
|
347
|
+
problems.append("no subject: the statement is about nothing")
|
|
348
|
+
return
|
|
349
|
+
if len(subjects) > 1:
|
|
350
|
+
problems.append(f"{len(subjects)} subjects: a passmcp evaluation is about one server")
|
|
351
|
+
return
|
|
352
|
+
sub = subjects[0]
|
|
353
|
+
want = target.endpoint.strip()
|
|
354
|
+
if not sub.name.strip():
|
|
355
|
+
problems.append("the subject has no name")
|
|
356
|
+
elif sub.name.strip() != want:
|
|
357
|
+
problems.append(f'the subject is named "{sub.name}" but the predicate is about "{want}"')
|
|
358
|
+
found, mismatch = _check_digest_set(
|
|
359
|
+
sub.digest, target_descriptor(target.transport, target.endpoint)
|
|
360
|
+
)
|
|
361
|
+
if not found:
|
|
362
|
+
problems.append("the subject has no sha256 or sha512 digest")
|
|
363
|
+
elif mismatch:
|
|
364
|
+
problems.append(f"the subject {mismatch} digest does not cover the target it names")
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def _validate_predicate_fields(pred: Evaluation, problems: list[str]) -> None:
|
|
368
|
+
if pred.subject_kind != SUBJECT_KIND_DESCRIPTOR:
|
|
369
|
+
problems.append(
|
|
370
|
+
f'subjectKind is "{pred.subject_kind}", want "{SUBJECT_KIND_DESCRIPTOR}"'
|
|
371
|
+
)
|
|
372
|
+
if not pred.target.endpoint.strip():
|
|
373
|
+
problems.append("the target has no endpoint")
|
|
374
|
+
if pred.target.transport == "":
|
|
375
|
+
problems.append("the target has no transport")
|
|
376
|
+
elif pred.target.transport not in VALID_TRANSPORTS:
|
|
377
|
+
problems.append(f'unknown transport "{pred.target.transport}"')
|
|
378
|
+
if not pred.instrument.name.strip() or not pred.instrument.version.strip():
|
|
379
|
+
problems.append("the instrument is not identified")
|
|
380
|
+
if not _is_valid_timestamp(pred.ran_at):
|
|
381
|
+
problems.append("no run time: a verdict about a live service is a verdict about a moment")
|
|
382
|
+
if len(pred.verdicts) == 0:
|
|
383
|
+
problems.append("no verdicts")
|
|
384
|
+
if not pred.judged_against.check_inventory.strip():
|
|
385
|
+
problems.append("judgedAgainst.checkInventory is empty")
|
|
386
|
+
if pred.score is not None and not pred.judged_against.rubric.strip():
|
|
387
|
+
problems.append("a score with no rubric version")
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
def _validate_verdicts(pred: Evaluation, problems: list[str]) -> None:
|
|
391
|
+
tallied = {"pass": 0, "warn": 0, "fail": 0, "skip": 0, "info": 0}
|
|
392
|
+
for idx, verdict in enumerate(pred.verdicts):
|
|
393
|
+
if not verdict.id.strip():
|
|
394
|
+
problems.append(f"verdict {idx} has no id")
|
|
395
|
+
continue
|
|
396
|
+
if verdict.status not in VALID_STATUSES:
|
|
397
|
+
problems.append(f'verdict {verdict.id} has status "{verdict.status}"')
|
|
398
|
+
else:
|
|
399
|
+
tallied[verdict.status] += 1
|
|
400
|
+
if pred.verdicts and tallied != pred.counts.to_status_map():
|
|
401
|
+
problems.append("the counts disagree with the verdicts")
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
def parse_statement(raw: bytes | str) -> Statement:
|
|
405
|
+
"""Parse and strictly validate an in-toto passmcp evaluation statement offline."""
|
|
406
|
+
try:
|
|
407
|
+
text = raw.decode("utf-8") if isinstance(raw, (bytes, bytearray)) else raw
|
|
408
|
+
except UnicodeDecodeError as exc:
|
|
409
|
+
raise AttestationError(f"attestation: not a statement: {exc}") from exc
|
|
410
|
+
if not isinstance(text, str):
|
|
411
|
+
raise AttestationError("attestation: input must be bytes or str")
|
|
412
|
+
try:
|
|
413
|
+
decoded = json.loads(text)
|
|
414
|
+
except json.JSONDecodeError as exc:
|
|
415
|
+
raise AttestationError(f"attestation: not a statement: {exc}") from exc
|
|
416
|
+
root = _require_mapping(decoded, "not a statement: root")
|
|
417
|
+
stmt = Statement(
|
|
418
|
+
type=_str_field(root, "_type"),
|
|
419
|
+
subject=_parse_subjects(root.get("subject")),
|
|
420
|
+
predicate_type=_str_field(root, "predicateType"),
|
|
421
|
+
predicate=_parse_evaluation(root.get("predicate")),
|
|
422
|
+
)
|
|
423
|
+
problems: list[str] = []
|
|
424
|
+
if stmt.type != STATEMENT_TYPE:
|
|
425
|
+
problems.append(f'_type is "{stmt.type}", want "{STATEMENT_TYPE}"')
|
|
426
|
+
if stmt.predicate_type != PREDICATE_TYPE:
|
|
427
|
+
problems.append(f'predicateType is "{stmt.predicate_type}", want "{PREDICATE_TYPE}"')
|
|
428
|
+
_validate_subject(stmt.subject, stmt.predicate.target, problems)
|
|
429
|
+
_validate_predicate_fields(stmt.predicate, problems)
|
|
430
|
+
_validate_verdicts(stmt.predicate, problems)
|
|
431
|
+
if problems:
|
|
432
|
+
raise AttestationError("attestation: " + "; ".join(problems))
|
|
433
|
+
return stmt
|