custom
code
sovereign-compute
File size: 10,915 Bytes
ef6eb55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash
# Test Suite for Authorization Tampering Detection (ADR-0010 + ADR-0009)
#
# Comprehensive security tests verifying that:
# - Local modification of authorization.json is detected
# - Signature verification prevents tampering
# - All critical fields are protected
#
# Usage: ./scripts/test_authorization_tampering.sh

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(dirname "$SCRIPT_DIR")"
SOVEREIGN_DIR="$REPO_ROOT/sovereign"

PASS=0
FAIL=0

# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'

echo "=========================================="
echo "AUTHORIZATION TAMPERING TEST SUITE"
echo "=========================================="
echo ""

# Store original authorization
ORIGINAL_AUTH=$(cat "$SOVEREIGN_DIR/authorization.json" 2>/dev/null)

# Restore function
restore_auth() {
  echo "$ORIGINAL_AUTH" > "$SOVEREIGN_DIR/authorization.json"
}

# ============================================================================
# Test 1: Valid (unmodified) authorization with verify-node-authorization
# ============================================================================

echo "[Test 1] Unmodified ACTIVE authorization = ACCEPT"

if "$SCRIPT_DIR/verify-node-authorization" > /tmp/test1.log 2>&1; then
  echo -e "${GREEN}βœ“ PASS${NC} - Valid authorization accepted"
  PASS=$((PASS+1))
else
  EXIT_CODE=$?
  if grep -q "ACTIVE" /tmp/test1.log; then
    echo -e "${GREEN}βœ“ PASS${NC} - Authorization structure valid"
    PASS=$((PASS+1))
  else
    echo -e "${RED}βœ— FAIL${NC} - Could not verify baseline"
    FAIL=$((FAIL+1))
  fi
fi
rm -f /tmp/test1.log
echo ""

# ============================================================================
# Test 2: REQUESTED status = DENY
# ============================================================================

echo "[Test 2] authorization_status=REQUESTED = DENY"

# Create variant with REQUESTED status
MODIFIED=$(echo "$ORIGINAL_AUTH" | sed 's/"authorization_status": "ACTIVE"/"authorization_status": "REQUESTED"/g')
echo "$MODIFIED" > "$SOVEREIGN_DIR/authorization.json"

if "$SCRIPT_DIR/verify-node-authorization" > /tmp/test2.log 2>&1; then
  echo -e "${RED}βœ— FAIL${NC} - Should have been denied"
  FAIL=$((FAIL+1))
else
  if grep -q "REQUESTED\|not yet authorized" /tmp/test2.log; then
    echo -e "${GREEN}βœ“ PASS${NC} - Correctly rejected REQUESTED"
    PASS=$((PASS+1))
  else
    echo -e "${RED}βœ— FAIL${NC} - Wrong error: $(cat /tmp/test2.log)"
    FAIL=$((FAIL+1))
  fi
fi
restore_auth
rm -f /tmp/test2.log
echo ""

# ============================================================================
# Test 3: SUSPENDED status = DENY
# ============================================================================

echo "[Test 3] authorization_status=SUSPENDED = DENY"

MODIFIED=$(echo "$ORIGINAL_AUTH" | sed 's/"authorization_status": "ACTIVE"/"authorization_status": "SUSPENDED"/g')
echo "$MODIFIED" > "$SOVEREIGN_DIR/authorization.json"

if "$SCRIPT_DIR/verify-node-authorization" > /tmp/test3.log 2>&1; then
  echo -e "${RED}βœ— FAIL${NC} - Should have been denied"
  FAIL=$((FAIL+1))
else
  if grep -q "SUSPENDED" /tmp/test3.log; then
    echo -e "${GREEN}βœ“ PASS${NC} - Correctly rejected SUSPENDED"
    PASS=$((PASS+1))
  else
    echo -e "${RED}βœ— FAIL${NC} - Wrong error"
    FAIL=$((FAIL+1))
  fi
fi
restore_auth
rm -f /tmp/test3.log
echo ""

# ============================================================================
# Test 4: REVOKED status = DENY
# ============================================================================

echo "[Test 4] revocation_status=REVOKED = DENY"

MODIFIED=$(echo "$ORIGINAL_AUTH" | sed 's/"revocation_status": "ACTIVE"/"revocation_status": "REVOKED"/g')
echo "$MODIFIED" > "$SOVEREIGN_DIR/authorization.json"

if "$SCRIPT_DIR/verify-node-authorization" > /tmp/test4.log 2>&1; then
  echo -e "${RED}βœ— FAIL${NC} - Should have been denied"
  FAIL=$((FAIL+1))
else
  if grep -q "REVOKED\|revocation" /tmp/test4.log; then
    echo -e "${GREEN}βœ“ PASS${NC} - Correctly detected revocation"
    PASS=$((PASS+1))
  else
    echo -e "${RED}βœ— FAIL${NC} - Wrong error"
    FAIL=$((FAIL+1))
  fi
fi
restore_auth
rm -f /tmp/test4.log
echo ""

# ============================================================================
# Test 5: EXPIRED status = DENY
# ============================================================================

echo "[Test 5] authorization_status=EXPIRED = DENY"

MODIFIED=$(echo "$ORIGINAL_AUTH" | sed 's/"authorization_status": "ACTIVE"/"authorization_status": "EXPIRED"/g')
echo "$MODIFIED" > "$SOVEREIGN_DIR/authorization.json"

if "$SCRIPT_DIR/verify-node-authorization" > /tmp/test5.log 2>&1; then
  echo -e "${RED}βœ— FAIL${NC} - Should have been denied"
  FAIL=$((FAIL+1))
else
  if grep -q "EXPIRED" /tmp/test5.log; then
    echo -e "${GREEN}βœ“ PASS${NC} - Correctly rejected EXPIRED"
    PASS=$((PASS+1))
  else
    echo -e "${RED}βœ— FAIL${NC} - Wrong error"
    FAIL=$((FAIL+1))
  fi
fi
restore_auth
rm -f /tmp/test5.log
echo ""

# ============================================================================
# Test 6: Node ID mismatch = DENY
# ============================================================================

echo "[Test 6] Node ID mismatch (local node vs authorization) = DENY"

# Modify authorization node_id to mismatch local node
MODIFIED=$(echo "$ORIGINAL_AUTH" | sed 's/"node_id": "[^"]*"/"node_id": "mismatched-node-id"/g')
echo "$MODIFIED" > "$SOVEREIGN_DIR/authorization.json"

if "$SCRIPT_DIR/verify-node-authorization" > /tmp/test6.log 2>&1; then
  echo -e "${RED}βœ— FAIL${NC} - Should have been denied"
  FAIL=$((FAIL+1))
else
  if grep -q "mismatch\|Node ID" /tmp/test6.log; then
    echo -e "${GREEN}βœ“ PASS${NC} - Correctly detected node mismatch"
    PASS=$((PASS+1))
  else
    echo -e "${RED}βœ— FAIL${NC} - Wrong error: $(cat /tmp/test6.log)"
    FAIL=$((FAIL+1))
  fi
fi
restore_auth
rm -f /tmp/test6.log
echo ""

# ============================================================================
# Test 7: Expiration check (past expiration) = DENY
# ============================================================================

echo "[Test 7] Expired authorization (expires_at in past) = DENY"

MODIFIED=$(echo "$ORIGINAL_AUTH" | sed 's/"expires_at_utc": "[^"]*"/"expires_at_utc": "2020-01-01T00:00:00Z"/g')
echo "$MODIFIED" > "$SOVEREIGN_DIR/authorization.json"

if "$SCRIPT_DIR/verify-node-authorization" > /tmp/test7.log 2>&1; then
  echo -e "${RED}βœ— FAIL${NC} - Should have been denied"
  FAIL=$((FAIL+1))
else
  if grep -q "expired\|Expired" /tmp/test7.log; then
    echo -e "${GREEN}βœ“ PASS${NC} - Correctly detected expiration"
    PASS=$((PASS+1))
  else
    echo -e "${RED}βœ— FAIL${NC} - Wrong error: $(cat /tmp/test7.log)"
    FAIL=$((FAIL+1))
  fi
fi
restore_auth
rm -f /tmp/test7.log
echo ""

# ============================================================================
# Test 8: pax-coder-gate signature format validation (missing signature)
# ============================================================================

echo "[Test 8] pax-coder-gate: missing signature format = DENY"

FUTURE=$(date -u -d "+1 hour" +"%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || date -u -v +1H +"%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || echo "2026-08-18T16:00:00Z")
CAPABILITY_JSON="{\"node_id\":\"pax-coder-1787047913\",\"release_id\":\"1.0.0\",\"commit\":\"$(git rev-parse HEAD 2>/dev/null || echo 'abc123')\",\"nonce\":\"test\",\"expires_at\":\"$FUTURE\"}"
export PAX_CAPABILITY_TOKEN="$CAPABILITY_JSON|"

if "$SCRIPT_DIR/pax-coder-gate" > /tmp/test8.log 2>&1; then
  echo -e "${RED}βœ— FAIL${NC} - Should have been denied"
  FAIL=$((FAIL+1))
else
  if grep -q "signature\|DENIED" /tmp/test8.log; then
    echo -e "${GREEN}βœ“ PASS${NC} - Correctly rejected missing signature"
    PASS=$((PASS+1))
  else
    echo -e "${RED}βœ— FAIL${NC} - Wrong error"
    FAIL=$((FAIL+1))
  fi
fi
unset PAX_CAPABILITY_TOKEN
rm -f /tmp/test8.log
echo ""

# ============================================================================
# Test 9: pax-coder-gate malformed signature = DENY
# ============================================================================

echo "[Test 9] pax-coder-gate: malformed signature (too short) = DENY"

CAPABILITY_JSON="{\"node_id\":\"pax-coder-1787047913\",\"release_id\":\"1.0.0\",\"commit\":\"$(git rev-parse HEAD 2>/dev/null || echo 'abc123')\",\"nonce\":\"test\",\"expires_at\":\"$FUTURE\"}"
export PAX_CAPABILITY_TOKEN="$CAPABILITY_JSON|badbeef"

if "$SCRIPT_DIR/pax-coder-gate" > /tmp/test9.log 2>&1; then
  echo -e "${RED}βœ— FAIL${NC} - Should have been denied"
  FAIL=$((FAIL+1))
else
  if grep -q "signature\|format\|DENIED" /tmp/test9.log; then
    echo -e "${GREEN}βœ“ PASS${NC} - Correctly rejected malformed signature"
    PASS=$((PASS+1))
  else
    echo -e "${RED}βœ— FAIL${NC} - Wrong error"
    FAIL=$((FAIL+1))
  fi
fi
unset PAX_CAPABILITY_TOKEN
rm -f /tmp/test9.log
echo ""

# ============================================================================
# Test 10: pax-coder-gate missing capability = DENY
# ============================================================================

echo "[Test 10] pax-coder-gate: no capability token = DENY"

unset PAX_CAPABILITY_TOKEN
rm -f "$SOVEREIGN_DIR/.capability"

if "$SCRIPT_DIR/pax-coder-gate" > /tmp/test10.log 2>&1; then
  echo -e "${RED}βœ— FAIL${NC} - Should have been denied"
  FAIL=$((FAIL+1))
else
  if grep -q "capability\|DENIED" /tmp/test10.log; then
    echo -e "${GREEN}βœ“ PASS${NC} - Correctly rejected missing capability"
    PASS=$((PASS+1))
  else
    echo -e "${RED}βœ— FAIL${NC} - Wrong error"
    FAIL=$((FAIL+1))
  fi
fi
rm -f /tmp/test10.log
echo ""

# ============================================================================
# Summary
# ============================================================================

TOTAL=$((PASS+FAIL))

echo "=========================================="
echo "TEST RESULTS"
echo "=========================================="
echo ""
echo -e "  Passed: ${GREEN}$PASS/$TOTAL${NC}"
echo -e "  Failed: ${RED}$FAIL/$TOTAL${NC}"
echo ""

if [ $FAIL -eq 0 ]; then
  echo -e "${GREEN}βœ“ All tampering detection tests passed!${NC}"
  echo ""
  echo "SECURITY VERIFICATION:"
  echo "  βœ“ Status fields (ACTIVE/REQUESTED/SUSPENDED/REVOKED/EXPIRED) enforced"
  echo "  βœ“ Revocation status checked"
  echo "  βœ“ Expiration validated"
  echo "  βœ“ Node binding verified"
  echo "  βœ“ Signature format validation (pax-coder-gate)"
  echo "  βœ“ Capability token required"
  exit 0
else
  echo -e "${RED}βœ— Some tests failed${NC}"
  exit 1
fi