-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattacks_test.py
More file actions
168 lines (145 loc) · 7.32 KB
/
Copy pathattacks_test.py
File metadata and controls
168 lines (145 loc) · 7.32 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
"""
attacks_test.py -- prove every threat-model finding is closed.
authorize() returns (ok, claims, reason): ok at [0], reason at [2].
Run: python3 attacks_test.py
"""
import time
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey
import pop_crypto as pc
from stores import InMemoryAtomicStore, TokenBucketRateLimiter
from pop_server import AuthServer, Client, ResourceServer, RequestContext
AUD = "https://api.example.com"
URL = "https://api.example.com/orders"
M = "GET"
passed = 0
def check(name, cond):
global passed
print(" [%s] %s" % ("PASS" if cond else "FAIL", name))
assert cond, name
passed += 1
def fleet():
auth = AuthServer(issuer="https://auth.example.com")
replay = InMemoryAtomicStore()
nonces = InMemoryAtomicStore()
revoc = auth.revocations
rs_a = ResourceServer(auth.jwks(), auth.issuer, AUD, replay, nonces, revoc)
rs_b = ResourceServer(auth.jwks(), auth.issuer, AUD, replay, nonces, revoc)
return auth, rs_a, rs_b
def ctx(method=M, url=URL, channel=None):
return RequestContext(method, url, channel_value=channel)
print("baseline + H1 (cross-replica replay blocked by shared atomic store)")
auth, rs_a, rs_b = fleet()
alice = Client()
tok, jti, exp = auth.issue_bound_token("alice", alice.jwk(AUD), AUD)
proof = alice.make_proof(M, URL, tok, AUD)
check("legit request on replica A allowed", rs_a.authorize(tok, proof, ctx())[0])
check("H1: SAME proof replayed on replica B denied",
rs_b.authorize(tok, proof, ctx())[2] == "proof_replayed")
print("H1 (revocation propagates across replicas)")
auth, rs_a, rs_b = fleet()
tok, jti, exp = auth.issue_bound_token("alice", alice.jwk(AUD), AUD)
auth.revoke(jti, ttl=300)
check("revoked token denied on replica B",
rs_b.authorize(tok, alice.make_proof(M, URL, tok, AUD), ctx())[2]
== "token_revoked")
print("H2 (no bearer fallback: downgraded/unbound token rejected)")
auth, rs_a, _ = fleet()
kid, sk = auth.keys.signer()
now = int(time.time())
unbound = pc.jws_sign({"alg": "EdDSA", "typ": "JWT", "kid": kid},
{"iss": auth.issuer, "aud": AUD, "sub": "alice",
"iat": now, "nbf": now, "exp": now + 300, "jti": "x"}, sk)
check("H2: token lacking cnf/pop rejected",
rs_a.authorize(unbound, alice.make_proof(M, URL, unbound, AUD), ctx())[2]
== "token_not_sender_constrained")
print("H3 (server derives htu from trusted request; client value ignored)")
auth, rs_a, _ = fleet()
tok, _, _ = auth.issue_bound_token("alice", alice.jwk(AUD), AUD)
hdr = {"alg": "EdDSA", "typ": "pop+jwt", "jwk": alice.jwk(AUD)}
lying = pc.jws_sign(hdr, {"htm": M, "htu": "https://api.example.com/admin",
"iat": now, "jti": "j1",
"ath": pc.b64u(pc.sha256(tok.encode()))},
alice._key(AUD))
check("H3: client-controlled htu can't redirect the binding",
rs_a.authorize(tok, lying, ctx(url=URL))[2] == "uri_mismatch")
auth, rs_a, _ = fleet()
tok, _, _ = auth.issue_bound_token("alice", alice.jwk(AUD), AUD)
check("H3: query string is canonicalized away (same resource)",
rs_a.authorize(tok, alice.make_proof(M, URL + "?page=2", tok, AUD),
ctx(url=URL + "?page=2"))[0])
print("M1 (channel binding for high-value tokens)")
chan = b"tls-exporter-or-client-cert-der"
auth, rs_a, _ = fleet()
tok, _, _ = auth.issue_bound_token("a", alice.jwk(AUD), AUD, require_channel=True)
check("M1: missing channel binding denied",
rs_a.authorize(tok, alice.make_proof(M, URL, tok, AUD),
ctx(channel=chan))[2] == "channel_binding_mismatch")
auth, rs_a, _ = fleet()
tok, _, _ = auth.issue_bound_token("a", alice.jwk(AUD), AUD, require_channel=True)
check("M1: no channel presented at all denied",
rs_a.authorize(tok, alice.make_proof(M, URL, tok, AUD), ctx())[2]
== "channel_binding_required")
auth, rs_a, _ = fleet()
tok, _, _ = auth.issue_bound_token("a", alice.jwk(AUD), AUD, require_channel=True)
check("M1: correct channel binding allowed",
rs_a.authorize(tok, alice.make_proof(M, URL, tok, AUD, channel_value=chan),
ctx(channel=chan))[0])
auth, rs_a, _ = fleet()
tok, _, _ = auth.issue_bound_token("a", alice.jwk(AUD), AUD, require_channel=True)
check("M1: wrong channel value denied",
rs_a.authorize(tok, alice.make_proof(M, URL, tok, AUD, channel_value=b"evil"),
ctx(channel=chan))[2] == "channel_binding_mismatch")
print("M2 (key rotation with overlap; unknown kid rejected)")
auth, _, _ = fleet()
tok_old, _, _ = auth.issue_bound_token("a", alice.jwk(AUD), AUD)
auth.keys.rotate()
tok_new, _, _ = auth.issue_bound_token("a", alice.jwk(AUD), AUD)
rs = ResourceServer(auth.jwks(), auth.issuer, AUD, InMemoryAtomicStore(),
InMemoryAtomicStore(), auth.revocations)
check("M2: pre-rotation token still validates (overlap)",
rs.authorize(tok_old, alice.make_proof(M, URL, tok_old, AUD), ctx())[0])
check("M2: new-key token validates",
rs.authorize(tok_new, alice.make_proof(M, URL, tok_new, AUD), ctx())[0])
print("M3 (claim confidentiality via nested encryption)")
auth, _, _ = fleet()
rs_priv = X25519PrivateKey.generate()
enc_tok, _, _ = auth.issue_bound_token("a", alice.jwk(AUD), AUD,
encrypt_to_x25519_pub=rs_priv.public_key())
check("M3: token is opaque/encrypted on the wire", pc.is_encrypted(enc_tok))
rs = ResourceServer(auth.jwks(), auth.issuer, AUD, InMemoryAtomicStore(),
InMemoryAtomicStore(), auth.revocations, decryption_key=rs_priv)
check("M3: RS decrypts then authorizes",
rs.authorize(enc_tok, alice.make_proof(M, URL, enc_tok, AUD), ctx())[0])
tampered = enc_tok[:-2] + ("AA" if enc_tok[-2:] != "AA" else "BB")
check("M3: tampered ciphertext rejected",
rs.authorize(tampered, alice.make_proof(M, URL, tampered, AUD), ctx())[2]
== "decryption_failed")
print("L1 (per-audience keys distinct -> reduced linkability)")
check("L1: different audience -> different PoP key",
alice.jwk(AUD)["x"] != alice.jwk("https://other.example.com")["x"])
print("L4 (issuer is checked)")
auth, _, _ = fleet()
wrong = ResourceServer(auth.jwks(), "https://evil.example.com", AUD,
InMemoryAtomicStore(), InMemoryAtomicStore(), auth.revocations)
tok, _, _ = auth.issue_bound_token("a", alice.jwk(AUD), AUD)
check("L4: unexpected issuer denied",
wrong.authorize(tok, alice.make_proof(M, URL, tok, AUD), ctx())[2]
== "wrong_issuer")
print("L6 (alg allowlist: forged 'none' rejected)")
auth, rs_a, _ = fleet()
tok, _, _ = auth.issue_bound_token("a", alice.jwk(AUD), AUD)
none_proof = pc.b64u(b'{"alg":"none","typ":"pop+jwt","jwk":{}}') + "." + \
pc.b64u(b'{"htm":"GET"}') + "."
check("L6: alg:none proof rejected",
rs_a.authorize(tok, none_proof, ctx())[0] is False)
print("L2 (rate limiting blunts proof floods)")
auth, _, _ = fleet()
rs = ResourceServer(auth.jwks(), auth.issuer, AUD, InMemoryAtomicStore(),
InMemoryAtomicStore(), auth.revocations,
rate_limiter=TokenBucketRateLimiter(rate_per_sec=0.0, burst=3))
tok, _, _ = auth.issue_bound_token("a", alice.jwk(AUD), AUD)
results = [rs.authorize(tok, alice.make_proof(M, URL, tok, AUD), ctx())
for _ in range(6)]
check("L2: floods eventually rate-limited",
any(r[2] == "rate_limited" for r in results))
print("\nALL %d CHECKS PASSED" % passed)