-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_verification.py
More file actions
324 lines (278 loc) · 11.4 KB
/
test_verification.py
File metadata and controls
324 lines (278 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
"""Tests for offline and resolver-based verification."""
from schemapin.bundle import SchemaPinTrustBundle, create_bundled_discovery
from schemapin.core import SchemaPinCore
from schemapin.crypto import KeyManager, SignatureManager
from schemapin.resolver import TrustBundleResolver
from schemapin.revocation import (
RevocationReason,
add_revoked_key,
build_revocation_document,
)
from schemapin.verification import (
ErrorCode,
KeyPinStore,
verify_schema_offline,
verify_schema_with_resolver,
)
def _make_key_and_sign(schema):
"""Generate a key pair, sign the schema, and return (pub_pem, sig, fingerprint)."""
private_key, public_key = KeyManager.generate_keypair()
pub_pem = KeyManager.export_public_key_pem(public_key)
schema_hash = SchemaPinCore.canonicalize_and_hash(schema)
sig = SignatureManager.sign_schema_hash(schema_hash, private_key)
fp = KeyManager.calculate_key_fingerprint(public_key)
return pub_pem, sig, fp
class TestKeyPinStore:
"""Tests for the in-memory key pin store."""
def test_first_use(self):
"""First use of a tool@domain returns 'first_use'."""
store = KeyPinStore()
result = store.check_and_pin("tool1", "example.com", "sha256:aaa")
assert result == "first_use"
def test_pinned(self):
"""Same fingerprint returns 'pinned'."""
store = KeyPinStore()
store.check_and_pin("tool1", "example.com", "sha256:aaa")
result = store.check_and_pin("tool1", "example.com", "sha256:aaa")
assert result == "pinned"
def test_changed(self):
"""Different fingerprint returns 'changed'."""
store = KeyPinStore()
store.check_and_pin("tool1", "example.com", "sha256:aaa")
result = store.check_and_pin("tool1", "example.com", "sha256:bbb")
assert result == "changed"
def test_different_tools(self):
"""Different tool_ids are independent."""
store = KeyPinStore()
store.check_and_pin("tool1", "example.com", "sha256:aaa")
result = store.check_and_pin("tool2", "example.com", "sha256:bbb")
assert result == "first_use"
def test_different_domains(self):
"""Different domains are independent."""
store = KeyPinStore()
store.check_and_pin("tool1", "a.com", "sha256:aaa")
result = store.check_and_pin("tool1", "b.com", "sha256:bbb")
assert result == "first_use"
def test_serde_roundtrip(self):
"""Serialize and deserialize the pin store."""
store = KeyPinStore()
store.check_and_pin("tool1", "example.com", "sha256:aaa")
store.check_and_pin("tool2", "other.com", "sha256:bbb")
json_str = store.to_json()
restored = KeyPinStore.from_json(json_str)
assert restored.check_and_pin("tool1", "example.com", "sha256:aaa") == "pinned"
assert restored.check_and_pin("tool2", "other.com", "sha256:bbb") == "pinned"
def test_get_pinned(self):
"""Get a pinned fingerprint."""
store = KeyPinStore()
store.check_and_pin("tool1", "example.com", "sha256:aaa")
assert store.get_pinned("tool1", "example.com") == "sha256:aaa"
assert store.get_pinned("tool2", "example.com") is None
class TestVerifySchemaOffline:
"""Tests for verify_schema_offline."""
def test_happy_path(self):
"""Valid schema, signature, and key passes verification."""
schema = {"name": "test_tool", "description": "A test"}
pub_pem, sig, fp = _make_key_and_sign(schema)
discovery = {
"schema_version": "1.2",
"developer_name": "Test Dev",
"public_key_pem": pub_pem,
}
store = KeyPinStore()
result = verify_schema_offline(
schema, sig, "example.com", "tool1", discovery, None, store
)
assert result.valid is True
assert result.domain == "example.com"
assert result.developer_name == "Test Dev"
assert result.key_pinning is not None
assert result.key_pinning.status == "first_use"
assert result.error_code is None
def test_pinned_on_second_call(self):
"""Second call with same key returns pinned status."""
schema = {"name": "test_tool", "description": "A test"}
pub_pem, sig, fp = _make_key_and_sign(schema)
discovery = {
"schema_version": "1.2",
"developer_name": "Test Dev",
"public_key_pem": pub_pem,
}
store = KeyPinStore()
verify_schema_offline(
schema, sig, "example.com", "tool1", discovery, None, store
)
result = verify_schema_offline(
schema, sig, "example.com", "tool1", discovery, None, store
)
assert result.valid is True
assert result.key_pinning.status == "pinned"
def test_invalid_signature(self):
"""Invalid signature fails."""
schema = {"name": "test_tool", "description": "A test"}
pub_pem, sig, fp = _make_key_and_sign(schema)
discovery = {
"schema_version": "1.2",
"developer_name": "Test Dev",
"public_key_pem": pub_pem,
}
store = KeyPinStore()
result = verify_schema_offline(
schema, "invalid_signature_base64", "example.com", "tool1",
discovery, None, store
)
assert result.valid is False
assert result.error_code == ErrorCode.SIGNATURE_INVALID
def test_tampered_schema(self):
"""Tampered schema fails verification."""
schema = {"name": "test_tool", "description": "A test"}
pub_pem, sig, fp = _make_key_and_sign(schema)
tampered_schema = {"name": "test_tool", "description": "TAMPERED"}
discovery = {
"schema_version": "1.2",
"developer_name": "Test Dev",
"public_key_pem": pub_pem,
}
store = KeyPinStore()
result = verify_schema_offline(
tampered_schema, sig, "example.com", "tool1",
discovery, None, store
)
assert result.valid is False
assert result.error_code == ErrorCode.SIGNATURE_INVALID
def test_revoked_key_simple_list(self):
"""Key in simple revocation list fails."""
schema = {"name": "test_tool", "description": "A test"}
pub_pem, sig, fp = _make_key_and_sign(schema)
discovery = {
"schema_version": "1.2",
"developer_name": "Test Dev",
"public_key_pem": pub_pem,
"revoked_keys": [fp],
}
store = KeyPinStore()
result = verify_schema_offline(
schema, sig, "example.com", "tool1", discovery, None, store
)
assert result.valid is False
assert result.error_code == ErrorCode.KEY_REVOKED
def test_revoked_key_standalone_doc(self):
"""Key in standalone revocation doc fails."""
schema = {"name": "test_tool", "description": "A test"}
pub_pem, sig, fp = _make_key_and_sign(schema)
discovery = {
"schema_version": "1.2",
"developer_name": "Test Dev",
"public_key_pem": pub_pem,
}
rev = build_revocation_document("example.com")
add_revoked_key(rev, fp, RevocationReason.KEY_COMPROMISE)
store = KeyPinStore()
result = verify_schema_offline(
schema, sig, "example.com", "tool1", discovery, rev, store
)
assert result.valid is False
assert result.error_code == ErrorCode.KEY_REVOKED
def test_key_pin_change_rejected(self):
"""Key change is rejected."""
schema = {"name": "test_tool", "description": "A test"}
pub_pem1, sig1, fp1 = _make_key_and_sign(schema)
pub_pem2, sig2, fp2 = _make_key_and_sign(schema)
disc1 = {
"schema_version": "1.2",
"developer_name": "Dev",
"public_key_pem": pub_pem1,
}
disc2 = {
"schema_version": "1.2",
"developer_name": "Dev",
"public_key_pem": pub_pem2,
}
store = KeyPinStore()
# Pin first key
result1 = verify_schema_offline(
schema, sig1, "example.com", "tool1", disc1, None, store
)
assert result1.valid is True
# Try with different key
result2 = verify_schema_offline(
schema, sig2, "example.com", "tool1", disc2, None, store
)
assert result2.valid is False
assert result2.error_code == ErrorCode.KEY_PIN_MISMATCH
def test_invalid_discovery(self):
"""Invalid discovery document fails."""
schema = {"name": "test_tool"}
store = KeyPinStore()
result = verify_schema_offline(
schema, "sig", "example.com", "tool1",
{"schema_version": "1.2"}, None, store
)
assert result.valid is False
assert result.error_code == ErrorCode.DISCOVERY_INVALID
def test_missing_domain_in_discovery(self):
"""Discovery without public_key_pem fails."""
schema = {"name": "test_tool"}
store = KeyPinStore()
result = verify_schema_offline(
schema, "sig", "example.com", "tool1", {}, None, store
)
assert result.valid is False
assert result.error_code == ErrorCode.DISCOVERY_INVALID
def test_result_to_dict(self):
"""VerificationResult serializes correctly."""
schema = {"name": "test_tool", "description": "A test"}
pub_pem, sig, fp = _make_key_and_sign(schema)
discovery = {
"schema_version": "1.2",
"developer_name": "Test Dev",
"public_key_pem": pub_pem,
}
store = KeyPinStore()
result = verify_schema_offline(
schema, sig, "example.com", "tool1", discovery, None, store
)
d = result.to_dict()
assert d["valid"] is True
assert d["domain"] == "example.com"
assert d["developer_name"] == "Test Dev"
assert d["key_pinning"]["status"] == "first_use"
class TestVerifySchemaWithResolver:
"""Tests for verify_schema_with_resolver."""
def test_happy_path_with_resolver(self):
"""Verify using a TrustBundleResolver."""
schema = {"name": "test_tool", "description": "A test"}
pub_pem, sig, fp = _make_key_and_sign(schema)
well_known = {
"schema_version": "1.2",
"developer_name": "Bundle Dev",
"public_key_pem": pub_pem,
}
doc = create_bundled_discovery("example.com", well_known)
bundle = SchemaPinTrustBundle(
schemapin_bundle_version="1.2",
created_at="2026-01-01T00:00:00+00:00",
documents=[doc],
revocations=[],
)
resolver = TrustBundleResolver(bundle)
store = KeyPinStore()
result = verify_schema_with_resolver(
schema, sig, "example.com", "tool1", resolver, store
)
assert result.valid is True
assert result.developer_name == "Bundle Dev"
def test_missing_domain(self):
"""Missing domain in resolver fails."""
schema = {"name": "test_tool"}
bundle = SchemaPinTrustBundle(
schemapin_bundle_version="1.2",
created_at="2026-01-01T00:00:00+00:00",
)
resolver = TrustBundleResolver(bundle)
store = KeyPinStore()
result = verify_schema_with_resolver(
schema, "sig", "missing.com", "tool1", resolver, store
)
assert result.valid is False
assert result.error_code == ErrorCode.DISCOVERY_FETCH_FAILED