[+] lab7
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
from test_api import client,user,login
|
||||
from messenger.errors import ApiError
|
||||
|
||||
|
||||
def test_liveness_independent_of_secret_service(client,monkeypatch):
|
||||
from messenger import bao
|
||||
def unavailable(): raise ApiError(503,'unavailable','synthetic unavailable')
|
||||
monkeypatch.setattr(bao,'authorize',unavailable)
|
||||
assert client.get('/health/live').status_code==200
|
||||
assert client.get('/health/ready').status_code==503
|
||||
|
||||
|
||||
def test_login_rehashes_legacy_argon_parameters(client):
|
||||
from argon2 import PasswordHasher
|
||||
from messenger.security import HASHER
|
||||
account,body=user(client)
|
||||
old=PasswordHasher(time_cost=1,memory_cost=8192,parallelism=1).hash(body['password'])
|
||||
with client.app.state.db.pool.connection() as conn:
|
||||
conn.execute('UPDATE users SET password_hash=%s WHERE id=%s',(old,account['id']))
|
||||
login(client,body)
|
||||
with client.app.state.db.pool.connection() as conn:
|
||||
new=conn.execute('SELECT password_hash FROM users WHERE id=%s',(account['id'],)).fetchone()['password_hash']
|
||||
assert new!=old and not HASHER.check_needs_rehash(new)
|
||||
assert HASHER.verify(new,body['password'])
|
||||
|
||||
|
||||
def test_global_kdf_reject_does_not_allocate_username_buckets(client):
|
||||
import base64,hashlib
|
||||
account,body=user(client)
|
||||
db=client.app.state.db
|
||||
bucket='login:'+hashlib.sha256(body['username'].encode()).hexdigest()
|
||||
with db.pool.connection() as conn:
|
||||
w=conn.execute('SELECT floor(extract(epoch FROM clock_timestamp())/10)::bigint AS w').fetchone()['w']
|
||||
before=conn.execute("SELECT window_id,count FROM auth_rate_limits WHERE bucket='global-kdf' AND window_id IN (%s,%s)",(w,w+1)).fetchall()
|
||||
for window in (w,w+1):
|
||||
conn.execute("INSERT INTO auth_rate_limits(bucket,window_id,count) VALUES ('global-kdf',%s,120) ON CONFLICT(bucket,window_id) DO UPDATE SET count=120",(window,))
|
||||
try:
|
||||
credential=base64.b64encode((body['username']+':wrong-password-123').encode()).decode()
|
||||
response=client.post('/api/v1/auth/sessions',headers={'Authorization':'Basic '+credential})
|
||||
assert response.status_code==429
|
||||
with db.pool.connection() as conn:
|
||||
rows=conn.execute('SELECT 1 FROM auth_rate_limits WHERE bucket=%s',(bucket,)).fetchall()
|
||||
assert rows==[], 'Globally rejected names must not grow per-account bookkeeping'
|
||||
finally:
|
||||
with db.pool.connection() as conn:
|
||||
conn.execute("DELETE FROM auth_rate_limits WHERE bucket='global-kdf' AND window_id IN (%s,%s)",(w,w+1))
|
||||
for row in before:
|
||||
conn.execute("INSERT INTO auth_rate_limits(bucket,window_id,count) VALUES ('global-kdf',%s,%s)",(row['window_id'],row['count']))
|
||||
conn.execute('DELETE FROM auth_rate_limits WHERE bucket=%s',(bucket,))
|
||||
Reference in New Issue
Block a user