forked from mtusi2026/mtusi2026lab2
108 lines
3.7 KiB
Python
108 lines
3.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Deterministically verify that a cache miss cannot repopulate a logged-out session."""
|
|
import hashlib
|
|
import secrets
|
|
import threading
|
|
|
|
from src.store import Store
|
|
|
|
|
|
class BlockingCache:
|
|
def __init__(self, cache):
|
|
self.cache = cache
|
|
self.set_started = threading.Event()
|
|
self.allow_set = threading.Event()
|
|
|
|
def __getattr__(self, name):
|
|
return getattr(self.cache, name)
|
|
|
|
def set(self, key, value, **kwargs):
|
|
self.set_started.set()
|
|
if not self.allow_set.wait(timeout=10):
|
|
raise RuntimeError("timed out waiting to release the paused cache write")
|
|
return self.cache.set(key, value, **kwargs)
|
|
|
|
|
|
def main():
|
|
store = Store()
|
|
store.open()
|
|
underlying_cache = store.cache
|
|
if underlying_cache is None:
|
|
store.close()
|
|
raise RuntimeError("Redis cache is required for this test")
|
|
username = "sessionrace_" + secrets.token_hex(8)
|
|
user = store.create_user(username, "Session race test", "unused-test-hash")
|
|
token, _ = store.create_session(user["id"])
|
|
token_hash = hashlib.sha256(token.encode("ascii")).digest()
|
|
cache_key = "messenger:session:" + token_hash.hex()
|
|
|
|
blocking_cache = BlockingCache(underlying_cache)
|
|
store.cache = blocking_cache
|
|
outcomes = {}
|
|
failures = []
|
|
revoker_done = threading.Event()
|
|
|
|
def read_session():
|
|
try:
|
|
outcomes["reader"] = store.session_user(token)
|
|
except Exception as exc:
|
|
failures.append(exc)
|
|
|
|
def revoke_session():
|
|
try:
|
|
outcomes["revoker"] = store.revoke_session(token)
|
|
except Exception as exc:
|
|
failures.append(exc)
|
|
finally:
|
|
revoker_done.set()
|
|
|
|
reader = threading.Thread(target=read_session)
|
|
revoker = None
|
|
try:
|
|
underlying_cache.delete(cache_key)
|
|
reader.start()
|
|
if not blocking_cache.set_started.wait(timeout=10):
|
|
raise RuntimeError("session read did not reach the cache write")
|
|
|
|
revoker_started = threading.Event()
|
|
|
|
def start_revocation():
|
|
revoker_started.set()
|
|
revoke_session()
|
|
|
|
revoker = threading.Thread(target=start_revocation)
|
|
revoker.start()
|
|
if not revoker_started.wait(timeout=5):
|
|
raise RuntimeError("logout thread did not start")
|
|
if revoker_done.wait(timeout=0.2):
|
|
raise RuntimeError("logout passed the read's session lock before cache write completed")
|
|
|
|
blocking_cache.allow_set.set()
|
|
reader.join(timeout=10)
|
|
revoker.join(timeout=10)
|
|
if reader.is_alive() or revoker.is_alive():
|
|
raise RuntimeError("session race test threads did not finish")
|
|
if failures:
|
|
raise RuntimeError("session race worker failed") from failures[0]
|
|
if outcomes.get("reader") != user or outcomes.get("revoker") is not True:
|
|
raise RuntimeError("session read/logout returned unexpected results")
|
|
if underlying_cache.get(cache_key) is not None:
|
|
raise RuntimeError("logged-out session remains in Redis")
|
|
if store.session_user(token) is not None:
|
|
raise RuntimeError("logged-out session is still accepted")
|
|
print("PASS: concurrent session cache fill and logout leave no cached or valid session")
|
|
finally:
|
|
blocking_cache.allow_set.set()
|
|
reader.join(timeout=10)
|
|
if revoker is not None:
|
|
revoker.join(timeout=10)
|
|
store.cache = underlying_cache
|
|
underlying_cache.delete(cache_key)
|
|
with store.pool.connection() as conn:
|
|
conn.execute("DELETE FROM users WHERE id = %s", (user["id"],))
|
|
store.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|