forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_hmac_auth.py
More file actions
371 lines (290 loc) · 14 KB
/
Copy pathtest_hmac_auth.py
File metadata and controls
371 lines (290 loc) · 14 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
"""Tests for HMAC-SHA256 federation authentication.
Covers:
1. Sign and verify round-trip
2. Replay protection (duplicate nonce rejection)
3. Timestamp expiry (stale request rejection)
4. Key rotation (version bump, old key invalid)
5. Signature mismatch (tampered body)
6. Integration with registry (auto-generate on add_peer)
"""
from __future__ import annotations
import time
from pathlib import Path
from unittest.mock import patch
import pytest
from hub.federation.hmac_auth import (
sign_request,
verify_request,
build_auth_headers,
extract_auth_headers,
_NonceCache,
MAX_AGE_SECONDS,
)
from hub.federation import secrets as secret_store
from hub.federation.registry import FederationRegistry
@pytest.fixture
def secrets_dir(tmp_path):
"""Isolate secrets to a temp directory."""
with patch.object(secret_store, "_SECRETS_DIR", tmp_path / "secrets"):
yield tmp_path / "secrets"
@pytest.fixture
def node_with_secret(secrets_dir):
"""Register a node and return its ID."""
node_id = "test-node-alpha"
secret_store.generate_shared_secret(node_id)
return node_id
def _now():
"""Current time as float, for test convenience."""
return time.time()
# ── 1. Sign & Verify Round-Trip ──────────────────────────────────────────────
class TestSignAndVerify:
def test_sign_produces_valid_headers(self, node_with_secret):
"""sign_request returns (sig, timestamp, body, key_version)."""
sig, ts, body, kv = sign_request(
node_with_secret, "POST", "/api/sync", b'{"data": 1}'
)
assert len(sig) == 64 # SHA-256 hex = 64 chars
assert ts.isdigit()
assert kv >= 1
def test_verify_accepts_valid_request(self, node_with_secret):
"""verify_request accepts a properly signed request."""
body = b'{"lesson": "test"}'
sig, ts, _, kv = sign_request(node_with_secret, "POST", "/federation/sync", body)
ok, reason = verify_request(
node_with_secret, "POST", "/federation/sync", body, sig, ts, kv
)
assert ok is True
assert reason == "ok"
def test_build_auth_headers_round_trip(self, node_with_secret):
"""build_auth_headers + extract_auth_headers round-trips correctly."""
body = b"hello"
headers = build_auth_headers(node_with_secret, "PUT", "/api/lesson", body)
sig, ts, kv = extract_auth_headers(headers)
ok, reason = verify_request(
node_with_secret, "PUT", "/api/lesson", body, sig, ts, kv
)
assert ok is True
def test_different_methods_produce_different_signatures(self, node_with_secret):
"""GET and POST to the same path produce different signatures."""
body = b""
ts_fixed = _now()
sig_get, _, _, _ = sign_request(node_with_secret, "GET", "/api/x", body, timestamp=ts_fixed)
sig_post, _, _, _ = sign_request(node_with_secret, "POST", "/api/x", body, timestamp=ts_fixed)
assert sig_get != sig_post
# ── 2. Replay Protection ─────────────────────────────────────────────────────
class TestReplayProtection:
def test_duplicate_request_rejected(self, node_with_secret):
"""Sending the same signed request twice is rejected as replay."""
body = b'{"data": "replay-me"}'
ts_fixed = _now()
sig, ts, _, kv = sign_request(
node_with_secret, "POST", "/api/sync", body, timestamp=ts_fixed
)
# First time: ok
ok1, _ = verify_request(
node_with_secret, "POST", "/api/sync", body, sig, ts, kv
)
assert ok1 is True
# Second time: replay detected
ok2, reason = verify_request(
node_with_secret, "POST", "/api/sync", body, sig, ts, kv
)
assert ok2 is False
assert "replay" in reason
def test_different_body_not_replay(self, node_with_secret):
"""Same timestamp but different body is not a replay."""
ts_fixed = _now()
body_a = b'{"id": 1}'
body_b = b'{"id": 2}'
sig_a, ts_a, _, kv_a = sign_request(
node_with_secret, "POST", "/api/x", body_a, timestamp=ts_fixed
)
sig_b, ts_b, _, kv_b = sign_request(
node_with_secret, "POST", "/api/x", body_b, timestamp=ts_fixed
)
ok1, _ = verify_request(
node_with_secret, "POST", "/api/x", body_a, sig_a, ts_a, kv_a
)
ok2, _ = verify_request(
node_with_secret, "POST", "/api/x", body_b, sig_b, ts_b, kv_b
)
assert ok1 is True
assert ok2 is True
# ── 3. Timestamp Expiry ──────────────────────────────────────────────────────
class TestTimestampExpiry:
def test_expired_timestamp_rejected(self, node_with_secret):
"""Request with timestamp older than MAX_AGE_SECONDS is rejected."""
old_ts = _now() - MAX_AGE_SECONDS - 10
body = b"stale"
sig, ts, _, kv = sign_request(
node_with_secret, "GET", "/api/status", body, timestamp=old_ts
)
ok, reason = verify_request(
node_with_secret, "GET", "/api/status", body, sig, ts, kv
)
assert ok is False
assert "expired" in reason
def test_future_timestamp_within_window_accepted(self, node_with_secret):
"""Request with timestamp slightly in the future is accepted (clock skew)."""
future_ts = _now() + 30 # 30s in future, within 5min window
body = b"future"
sig, ts, _, kv = sign_request(
node_with_secret, "GET", "/api/ping", body, timestamp=future_ts
)
ok, reason = verify_request(
node_with_secret, "GET", "/api/ping", body, sig, ts, kv
)
assert ok is True
def test_invalid_timestamp_format_rejected(self, node_with_secret):
"""Non-numeric timestamp is rejected."""
ok, reason = verify_request(
node_with_secret, "GET", "/api/x", b"", "bad-sig", "not-a-number", 1
)
assert ok is False
assert "invalid_timestamp" in reason
# ── 4. Key Rotation ──────────────────────────────────────────────────────────
class TestKeyRotation:
def test_key_version_bumps_on_rotation(self, secrets_dir, node_with_secret):
"""Rotating a key increments the version."""
assert secret_store.get_key_version(node_with_secret) == 1
secret_store.generate_shared_secret(node_with_secret, rotate=True)
assert secret_store.get_key_version(node_with_secret) == 2
def test_old_key_invalid_after_rotation(self, secrets_dir, node_with_secret):
"""After rotation, signatures made with the old key are rejected."""
ts_fixed = _now()
body = b"old-key-data"
sig_v1, ts, _, kv_v1 = sign_request(
node_with_secret, "POST", "/api/sync", body, timestamp=ts_fixed
)
assert kv_v1 == 1
# Rotate key
secret_store.generate_shared_secret(node_with_secret, rotate=True)
assert secret_store.get_key_version(node_with_secret) == 2
# Verify with v1 signature should fail (server now has v2 key)
ok, reason = verify_request(
node_with_secret, "POST", "/api/sync", body, sig_v1, ts, kv_v1
)
assert ok is False
assert "signature_mismatch" in reason
def test_new_key_signs_and_verifies(self, secrets_dir, node_with_secret):
"""After rotation, new key signs and verifies correctly."""
secret_store.generate_shared_secret(node_with_secret, rotate=True)
body = b"new-key-data"
sig, ts, _, kv = sign_request(
node_with_secret, "POST", "/api/sync", body, timestamp=_now()
)
assert kv == 2
ok, reason = verify_request(
node_with_secret, "POST", "/api/sync", body, sig, ts, kv
)
assert ok is True
# ── 5. Signature Mismatch ────────────────────────────────────────────────────
class TestSignatureMismatch:
def test_tampered_body_rejected(self, node_with_secret):
"""Changing the body after signing invalidates the signature."""
ts_fixed = _now()
original_body = b'{"lesson": "original"}'
tampered_body = b'{"lesson": "TAMPERED"}'
sig, ts, _, kv = sign_request(
node_with_secret, "POST", "/api/sync", original_body, timestamp=ts_fixed
)
ok, reason = verify_request(
node_with_secret, "POST", "/api/sync", tampered_body, sig, ts, kv
)
assert ok is False
assert "signature_mismatch" in reason
def test_tampered_path_rejected(self, node_with_secret):
"""Changing the path after signing invalidates the signature."""
ts_fixed = _now()
body = b"data"
sig, ts, _, kv = sign_request(
node_with_secret, "POST", "/api/sync", body, timestamp=ts_fixed
)
ok, reason = verify_request(
node_with_secret, "POST", "/api/DIFFERENT", body, sig, ts, kv
)
assert ok is False
assert "signature_mismatch" in reason
def test_unknown_node_rejected(self, secrets_dir):
"""Request from an unregistered node is rejected."""
ok, reason = verify_request(
"nonexistent-node", "GET", "/api/x", b"", "fake-sig", str(int(_now())), 0
)
assert ok is False
assert "unknown_node" in reason
# ── 6. Registry Integration ──────────────────────────────────────────────────
class TestRegistryIntegration:
def test_add_peer_generates_secret(self, secrets_dir):
"""FederationRegistry.add_peer auto-generates an HMAC shared secret."""
config_path = secrets_dir.parent / "config.yaml"
registry = FederationRegistry.from_config(config_path)
registry.add_peer("hub-node-1", "https://github.com/peer/repo")
secret = secret_store.get_shared_secret("hub-node-1")
assert secret is not None
assert len(secret) == 64 # 256-bit hex
def test_remove_peer_revokes_secret(self, secrets_dir):
"""FederationRegistry.remove_peer revokes the HMAC shared secret."""
config_path = secrets_dir.parent / "config.yaml"
registry = FederationRegistry.from_config(config_path)
registry.add_peer("hub-node-2", "https://github.com/peer/repo")
assert secret_store.get_shared_secret("hub-node-2") is not None
registry.remove_peer("hub-node-2")
assert secret_store.get_shared_secret("hub-node-2") is None
def test_full_flow_register_sign_verify(self, secrets_dir):
"""End-to-end: register peer → sign request → verify request."""
config_path = secrets_dir.parent / "config.yaml"
registry = FederationRegistry.from_config(config_path)
registry.add_peer("federation-hub", "https://hub.misaka.net")
body = b'{"lessons": ["pip-ssl-fix", "git-rebase"]}'
headers = build_auth_headers("federation-hub", "POST", "/api/federation/sync", body)
sig, ts, kv = extract_auth_headers(headers)
ok, reason = verify_request(
"federation-hub", "POST", "/api/federation/sync", body, sig, ts, kv
)
assert ok is True
# ── 7. NonceCache Unit Tests ─────────────────────────────────────────────────
class TestNonceCache:
def test_fresh_nonce_accepted(self):
cache = _NonceCache(max_age=60)
assert cache.check_and_record("nonce-1") is True
def test_duplicate_nonce_rejected(self):
cache = _NonceCache(max_age=60)
cache.check_and_record("nonce-2")
assert cache.check_and_record("nonce-2") is False
def test_expired_nonce_cleaned_up(self):
cache = _NonceCache(max_age=0) # instant expiry
cache.check_and_record("nonce-3")
# Force cleanup by advancing time
with patch("time.time", return_value=cache._last_cleanup + 120):
assert cache.check_and_record("nonce-3") is True # should be cleaned
# ── 8. Edge Cases ────────────────────────────────────────────────────────────
class TestEdgeCases:
def test_empty_body(self, node_with_secret):
"""Empty body (b'') is valid."""
sig, ts, _, kv = sign_request(node_with_secret, "GET", "/health", b"")
ok, _ = verify_request(node_with_secret, "GET", "/health", b"", sig, ts, kv)
assert ok is True
def test_large_body(self, node_with_secret):
"""Large body (1MB) signs and verifies correctly."""
body = b"x" * (1024 * 1024)
sig, ts, _, kv = sign_request(
node_with_secret, "POST", "/api/upload", body
)
ok, _ = verify_request(
node_with_secret, "POST", "/api/upload", body, sig, ts, kv
)
assert ok is True
def test_unicode_body(self, node_with_secret):
"""UTF-8 body with CJK characters signs correctly."""
body = '{"lesson": "pip SSL 证书修复"}'.encode("utf-8")
sig, ts, _, kv = sign_request(
node_with_secret, "POST", "/api/lesson", body
)
ok, _ = verify_request(
node_with_secret, "POST", "/api/lesson", body, sig, ts, kv
)
assert ok is True
def test_missing_headers_raises(self):
"""extract_auth_headers raises on missing headers."""
with pytest.raises(ValueError, match="Missing HMAC"):
extract_auth_headers({})