Prevent revoked session refills and escape proxy error identifiers
This commit is contained in:
@@ -69,10 +69,10 @@ class CloseSessionHandler:
|
||||
self._revocations = revocations
|
||||
|
||||
async def __call__(self, command: CloseSession) -> None:
|
||||
await self._revocations.revoke(command.session)
|
||||
try:
|
||||
async with self._uow.transaction():
|
||||
await self._uow.sessions.remove(command.session.token_hash)
|
||||
await self._revocations.revoke(command.session)
|
||||
self._uow.record(SessionClosed(user_id=command.session.user_id))
|
||||
await self._uow.commit()
|
||||
except BaseException:
|
||||
|
||||
@@ -166,10 +166,12 @@ class PostgresSessionDirectory:
|
||||
async with self._database.session() as session:
|
||||
row = (
|
||||
await session.execute(
|
||||
select(SessionRow).where(
|
||||
select(SessionRow)
|
||||
.where(
|
||||
SessionRow.token_hash == token_hash,
|
||||
SessionRow.expires_at > now,
|
||||
)
|
||||
.with_for_update(read=True, key_share=True)
|
||||
)
|
||||
).scalar_one_or_none()
|
||||
if row is None:
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import time
|
||||
from collections.abc import Awaitable
|
||||
from datetime import datetime, timedelta
|
||||
from uuid import UUID
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from redis.asyncio import Redis
|
||||
from redis.exceptions import RedisError
|
||||
@@ -13,6 +13,16 @@ from messenger.shared.application.ports import Logger
|
||||
|
||||
REVOKED = "-"
|
||||
FAILURES = (RedisError, OSError)
|
||||
FILL = """
|
||||
if redis.call('GET', KEYS[2]) ~= ARGV[1] then return false end
|
||||
redis.call('DEL', KEYS[2])
|
||||
if ARGV[2] == '' then return false end
|
||||
return redis.call('SET', KEYS[1], ARGV[2], 'PXAT', ARGV[3], 'NX')
|
||||
"""
|
||||
REVOKE = """
|
||||
redis.call('SET', KEYS[1], ARGV[1], 'PXAT', ARGV[2])
|
||||
return redis.call('DEL', KEYS[2])
|
||||
"""
|
||||
|
||||
|
||||
def key_of(token_hash: bytes) -> str:
|
||||
@@ -72,18 +82,26 @@ class CachedSessionDirectory:
|
||||
if cached is not None:
|
||||
session = decode(token_hash, cached)
|
||||
return session if session.expires_at > now else None
|
||||
marker, nonce = f"{key}:fill", uuid4().hex
|
||||
until = milliseconds(now + self._limit)
|
||||
reserved = self._closed() and await self._quietly(
|
||||
self._client.set(marker, nonce, pxat=until, nx=True)
|
||||
)
|
||||
session = await self._origin.find(token_hash, now)
|
||||
if session is not None and self._closed():
|
||||
until = milliseconds(min(session.expires_at, now + self._limit))
|
||||
await self._quietly(self._client.set(key, encode(session), pxat=until, nx=True))
|
||||
if reserved and self._closed():
|
||||
value = encode(session) if session is not None else ""
|
||||
if session is not None:
|
||||
until = min(until, milliseconds(session.expires_at))
|
||||
await self._quietly(self._client.eval(FILL, 2, key, marker, nonce, value, until))
|
||||
return session
|
||||
|
||||
async def revoke(self, session: Session) -> None:
|
||||
if not self._closed():
|
||||
raise Unavailable("кэш сессий недоступен")
|
||||
key = key_of(session.token_hash)
|
||||
try:
|
||||
await self._client.set(
|
||||
key_of(session.token_hash), REVOKED, pxat=milliseconds(session.expires_at)
|
||||
await self._client.eval(
|
||||
REVOKE, 2, key, f"{key}:fill", REVOKED, milliseconds(session.expires_at)
|
||||
)
|
||||
except FAILURES as error:
|
||||
self._trip(error)
|
||||
|
||||
Reference in New Issue
Block a user