forked from Dipraise1/Engram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_auth.py
More file actions
280 lines (212 loc) · 11.6 KB
/
Copy pathtest_auth.py
File metadata and controls
280 lines (212 loc) · 11.6 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
"""
Tests for engram/miner/auth.py — hotkey signature verification.
Covers:
- sign_request / verify_request round-trip (happy path)
- Replay attack blocked (nonce outside ±30s window)
- Tampered payload detected
- Tampered signature detected
- Wrong hotkey (sig valid but for a different key)
- Missing sig in hard mode (REQUIRE_HOTKEY_SIG=true) → AuthError
- Missing sig in soft mode (REQUIRE_HOTKEY_SIG=false) → allowed with None/hotkey
- Allowlist enforcement (ALLOWED_VALIDATOR_HOTKEYS)
- Anonymous request (no hotkey) is allowed in soft mode
"""
from __future__ import annotations
import time
from unittest.mock import patch
import pytest
# We test purely with Python-level mocks so bittensor isn't required in CI.
# The real bt.Keypair path is covered by test_bt_keypair_roundtrip below.
from engram.miner.auth import (
AuthError,
_canonical_message,
_payload_hash,
sign_request,
verify_request,
)
# ── Minimal fake keypair ──────────────────────────────────────────────────────
import hashlib
import hmac as _hmac
class _FakeKeypair:
"""
Deterministic fake keypair that signs with HMAC-SHA256.
Lets us test the auth module without bittensor installed.
The 'secret' is never transmitted — only the ss58_address and signature.
"""
def __init__(self, ss58_address: str, secret: bytes = b"testkey"):
self.ss58_address = ss58_address
self._secret = secret
def sign(self, message: bytes) -> bytes:
return _hmac.new(self._secret, message, hashlib.sha256).digest()
def verify(self, message: bytes, signature: bytes) -> bool:
expected = _hmac.new(self._secret, message, hashlib.sha256).digest()
return _hmac.compare_digest(expected, signature)
KEYPAIR_A = _FakeKeypair("5FakeHotkeyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", secret=b"secret_a")
KEYPAIR_B = _FakeKeypair("5FakeHotkeyBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB", secret=b"secret_b")
def _bt_verify_patch(ss58_address, message, sig_bytes):
"""Route bt.Keypair.verify calls to our fake keypair."""
kp = KEYPAIR_A if ss58_address == KEYPAIR_A.ss58_address else KEYPAIR_B
return kp.verify(message, sig_bytes)
# ── Helpers ───────────────────────────────────────────────────────────────────
def _sign(keypair: _FakeKeypair, endpoint: str, body: dict) -> dict:
nonce = int(time.time() * 1000)
body_with_meta = {**body, "hotkey": keypair.ss58_address, "nonce": nonce}
msg = _canonical_message(nonce, endpoint, body_with_meta)
sig = "0x" + keypair.sign(msg).hex()
return {**body_with_meta, "signature": sig}
def _do_verify(body: dict, endpoint: str = "IngestSynapse", require: bool = False):
with (
patch("engram.miner.auth._bt_keypair_verify", side_effect=_bt_verify_patch),
patch("engram.miner.auth.REQUIRE_SIG", require),
patch("engram.miner.auth.ALLOWED_HOTKEYS", None),
):
return verify_request(body, endpoint)
# ── Round-trip ────────────────────────────────────────────────────────────────
def test_sign_and_verify_roundtrip():
body = {"text": "hello world", "metadata": {}}
signed = _sign(KEYPAIR_A, "IngestSynapse", body)
result = _do_verify(signed, "IngestSynapse")
assert result == KEYPAIR_A.ss58_address
def test_roundtrip_different_endpoints():
for endpoint in ("IngestSynapse", "QuerySynapse", "ChallengeSynapse"):
body = {"query_vector": [0.1, 0.2], "top_k": 5}
signed = _sign(KEYPAIR_A, endpoint, body)
result = _do_verify(signed, endpoint)
assert result == KEYPAIR_A.ss58_address
# ── Replay protection ─────────────────────────────────────────────────────────
def test_old_nonce_rejected():
body = {"text": "stale"}
signed = _sign(KEYPAIR_A, "IngestSynapse", body)
# Wind the nonce back 60 seconds — outside the ±30s window
signed["nonce"] = int((time.time() - 60) * 1000)
# Recompute signature with the old nonce so it would be valid if not for the replay check
msg = _canonical_message(signed["nonce"], "IngestSynapse", signed)
signed["signature"] = "0x" + KEYPAIR_A.sign(msg).hex()
with pytest.raises(AuthError, match="replay window"):
_do_verify(signed)
def test_future_nonce_rejected():
body = {"text": "future"}
signed = _sign(KEYPAIR_A, "IngestSynapse", body)
signed["nonce"] = int((time.time() + 60) * 1000)
msg = _canonical_message(signed["nonce"], "IngestSynapse", signed)
signed["signature"] = "0x" + KEYPAIR_A.sign(msg).hex()
with pytest.raises(AuthError, match="replay window"):
_do_verify(signed)
# ── Tamper detection ──────────────────────────────────────────────────────────
def test_tampered_payload_rejected_in_hard_mode():
body = {"text": "original content"}
signed = _sign(KEYPAIR_A, "IngestSynapse", body)
signed["text"] = "injected content" # change payload after signing
with pytest.raises(AuthError, match="Signature verification failed"):
_do_verify(signed, require=True)
def test_tampered_payload_warns_in_soft_mode(caplog):
import logging
body = {"text": "original content"}
signed = _sign(KEYPAIR_A, "IngestSynapse", body)
signed["text"] = "injected content"
# Soft mode — should NOT raise, but logs warning
result = _do_verify(signed, require=False)
assert result == KEYPAIR_A.ss58_address # hotkey still returned
def test_invalid_signature_hex_rejected():
body = {"text": "test"}
signed = _sign(KEYPAIR_A, "IngestSynapse", body)
signed["signature"] = "not-valid-hex"
with pytest.raises(AuthError, match="hex"):
_do_verify(signed, require=True)
# ── Wrong keypair ─────────────────────────────────────────────────────────────
def test_wrong_keypair_rejected_hard_mode():
"""Sign with keypair B but claim to be keypair A."""
body = {"text": "impersonation attempt"}
# Sign with B's key but put A's hotkey in the body
nonce = int(time.time() * 1000)
signed = {
"text": "impersonation attempt",
"hotkey": KEYPAIR_A.ss58_address, # claimed identity
"nonce": nonce,
}
msg = _canonical_message(nonce, "IngestSynapse", signed)
signed["signature"] = "0x" + KEYPAIR_B.sign(msg).hex() # signed by B
with pytest.raises(AuthError, match="Signature verification failed"):
_do_verify(signed, require=True)
# ── Missing signature ─────────────────────────────────────────────────────────
def test_missing_sig_hard_mode_raises():
body = {"text": "unsigned", "hotkey": KEYPAIR_A.ss58_address}
with pytest.raises(AuthError, match="signed requests"):
_do_verify(body, require=True)
def test_missing_sig_soft_mode_allowed():
body = {"text": "unsigned", "hotkey": KEYPAIR_A.ss58_address}
result = _do_verify(body, require=False)
assert result == KEYPAIR_A.ss58_address
def test_anonymous_request_soft_mode_allowed():
"""No hotkey at all — anonymous SDK/direct-HTTP caller."""
body = {"text": "anonymous"}
result = _do_verify(body, require=False)
assert result is None
def test_anonymous_request_hard_mode_raises():
body = {"text": "anonymous"}
with pytest.raises(AuthError, match="signed requests"):
_do_verify(body, require=True)
# ── Allowlist ─────────────────────────────────────────────────────────────────
def test_allowlist_accepts_known_hotkey():
body = _sign(KEYPAIR_A, "QuerySynapse", {"query_vector": [0.1], "top_k": 5})
with (
patch("engram.miner.auth._bt_keypair_verify", side_effect=_bt_verify_patch),
patch("engram.miner.auth.REQUIRE_SIG", False),
patch("engram.miner.auth.ALLOWED_HOTKEYS", {KEYPAIR_A.ss58_address}),
):
result = verify_request(body, "QuerySynapse")
assert result == KEYPAIR_A.ss58_address
def test_allowlist_rejects_unknown_hotkey():
body = _sign(KEYPAIR_B, "QuerySynapse", {"query_vector": [0.1], "top_k": 5})
with (
patch("engram.miner.auth._bt_keypair_verify", side_effect=_bt_verify_patch),
patch("engram.miner.auth.REQUIRE_SIG", False),
patch("engram.miner.auth.ALLOWED_HOTKEYS", {KEYPAIR_A.ss58_address}),
):
with pytest.raises(AuthError, match="allowed-validator"):
verify_request(body, "QuerySynapse")
# ── Payload hash stability ────────────────────────────────────────────────────
def test_payload_hash_excludes_auth_fields():
"""Auth fields must not affect the payload hash — allows sign-then-add-sig."""
payload = {"text": "hello", "metadata": {"k": "v"}}
h1 = _payload_hash({**payload})
h2 = _payload_hash({**payload, "hotkey": "5Fx...", "nonce": 123, "signature": "0xabc"})
assert h1 == h2
def test_payload_hash_detects_change():
h1 = _payload_hash({"text": "hello"})
h2 = _payload_hash({"text": "HELLO"})
assert h1 != h2
# ── Metagraph registration check ──────────────────────────────────────────────
def test_unregistered_hotkey_rejected_in_hard_mode():
body = _sign(KEYPAIR_A, "IngestSynapse", {"text": "test"})
with (
patch("engram.miner.auth._bt_keypair_verify", side_effect=_bt_verify_patch),
patch("engram.miner.auth.REQUIRE_SIG", False),
patch("engram.miner.auth.ALLOWED_HOTKEYS", None),
patch("engram.miner.auth.REQUIRE_METAGRAPH_REG", True),
patch("engram.miner.auth._is_registered", return_value=False),
):
with pytest.raises(AuthError, match="not registered on subnet"):
verify_request(body, "IngestSynapse")
def test_unregistered_hotkey_warns_in_soft_mode():
body = _sign(KEYPAIR_A, "IngestSynapse", {"text": "test"})
with (
patch("engram.miner.auth._bt_keypair_verify", side_effect=_bt_verify_patch),
patch("engram.miner.auth.REQUIRE_SIG", False),
patch("engram.miner.auth.ALLOWED_HOTKEYS", None),
patch("engram.miner.auth.REQUIRE_METAGRAPH_REG", False),
patch("engram.miner.auth._is_registered", return_value=False),
):
result = verify_request(body, "IngestSynapse")
assert result == KEYPAIR_A.ss58_address
def test_registered_hotkey_passes():
body = _sign(KEYPAIR_A, "IngestSynapse", {"text": "test"})
with (
patch("engram.miner.auth._bt_keypair_verify", side_effect=_bt_verify_patch),
patch("engram.miner.auth.REQUIRE_SIG", False),
patch("engram.miner.auth.ALLOWED_HOTKEYS", None),
patch("engram.miner.auth.REQUIRE_METAGRAPH_REG", True),
patch("engram.miner.auth._is_registered", return_value=True),
):
result = verify_request(body, "IngestSynapse")
assert result == KEYPAIR_A.ss58_address