Remove get_session_context setting of host (now read only op as expected). Make session host, ip and user_agent always set (the ua potentially empty string).

This commit is contained in:
Leo Vasanko
2026-01-28 02:14:34 +00:00
parent 78a7c4f07a
commit 9983245652
6 changed files with 35 additions and 35 deletions
+3 -3
View File
@@ -176,17 +176,17 @@ class JsonlStore:
if data_dict:
# Preserve original state before migrations (deep copy for nested dicts)
original_dict = copy.deepcopy(data_dict)
# Apply schema migrations (modifies data_dict in place)
migrated = apply_migrations(data_dict)
decoder = msgspec.json.Decoder(DB)
self.db = decoder.decode(msgspec.json.encode(data_dict))
self.db._store = self
# Update previous state to migrated data FIRST (to avoid transaction hardening reset)
self._previous_builtins = data_dict
# Persist migration by manually computing and queueing the diff
if migrated:
diff = compute_diff(original_dict, data_dict)
+4 -4
View File
@@ -12,16 +12,16 @@ _logger = logging.getLogger(__name__)
def apply_migrations(data_dict: dict) -> bool:
"""Apply any pending schema migrations to the database dictionary.
Args:
data_dict: The raw database dictionary loaded from JSONL
Returns:
True if any migrations were applied, False otherwise
"""
db_version = data_dict.get("v", 0)
migrated = False
if db_version == 0:
# Migration v0 -> v1: Remove created_at from orgs (field removed from schema)
if "orgs" in data_dict:
@@ -30,5 +30,5 @@ def apply_migrations(data_dict: dict) -> bool:
data_dict["v"] = 1
migrated = True
_logger.info("Applied schema migration: v0 -> v1 (removed org.created_at)")
return migrated
+19 -19
View File
@@ -41,16 +41,21 @@ _logger = logging.getLogger(__name__)
_db = DB()
_store = JsonlStore(_db)
_db._store = _store
_initialized = False
async def init(*args, **kwargs):
"""Load database from JSONL file."""
global _db
global _db, _initialized
if _initialized:
_logger.debug("Database already initialized, skipping reload")
return
db_path = os.environ.get("PASKIA_DB", DB_PATH_DEFAULT)
if db_path.startswith("json:"):
db_path = db_path[5:]
await _store.load(db_path)
_db = _store.db
_initialized = True
# -------------------------------------------------------------------------
@@ -150,15 +155,10 @@ def get_session_context(
if s.expiry < datetime.now(timezone.utc):
return None
# Handle host binding
if host is not None:
if s.host is None:
# Bind session to this host
with _db.transaction("bind_session_host"):
s.host = host
elif s.host != host:
# Session bound to different host
return None
# Validate host matches (sessions are always created with a host)
if host is not None and s.host != host:
# Session bound to different host
return None
# Validate user exists
if s.user not in _db.users:
@@ -558,9 +558,9 @@ def create_session(
key: str,
user_uuid: UUID,
credential_uuid: UUID,
host: str | None,
ip: str | None,
user_agent: str | None,
host: str,
ip: str,
user_agent: str,
expiry: datetime,
*,
ctx: SessionContext | None = None,
@@ -708,9 +708,9 @@ def _create_token() -> str:
def login(
user_uuid: UUID,
credential: Credential,
host: str | None,
ip: str | None,
user_agent: str | None,
host: str,
ip: str,
user_agent: str,
expiry: datetime,
) -> str:
"""Update user/credential on login and create session in a single transaction.
@@ -755,9 +755,9 @@ def login(
def create_credential_session(
user_uuid: UUID,
credential: Credential,
host: str | None,
ip: str | None,
user_agent: str | None,
host: str,
ip: str,
user_agent: str,
display_name: str | None = None,
reset_key: bytes | None = None,
) -> str:
+3 -3
View File
@@ -190,9 +190,9 @@ class Session(msgspec.Struct, dict=True):
user: UUID
credential: UUID
host: str | None
ip: str | None
user_agent: str | None
host: str
ip: str
user_agent: str
expiry: datetime
def __post_init__(self):
+2 -2
View File
@@ -19,8 +19,8 @@ AUTH_COOKIE = Cookie(None, alias=AUTH_COOKIE_NAME)
def infodict(request: Request | WebSocket, type: str) -> dict:
"""Extract client information from request."""
return {
"ip": request.client.host if request.client else None,
"user_agent": request.headers.get("user-agent", "")[:500] or None,
"ip": request.client.host if request.client else "",
"user_agent": request.headers.get("user-agent", "")[:500],
"session_type": type,
}
+4 -4
View File
@@ -65,8 +65,8 @@ async def websocket_register_add(
reset_key=(s.key if reset is not None else None),
display_name=user_name,
host=host,
ip=metadata.get("ip"),
user_agent=metadata.get("user_agent"),
ip=metadata["ip"],
user_agent=metadata["user_agent"],
)
auth = token
@@ -117,8 +117,8 @@ async def websocket_authenticate(ws: WebSocket, auth=AUTH_COOKIE):
user_uuid=cred.user,
credential=cred,
host=normalized_host,
ip=metadata.get("ip") or "",
user_agent=metadata.get("user_agent") or "",
ip=metadata["ip"],
user_agent=metadata["user_agent"],
expiry=expires(),
)