mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
* fix(sync): bind device tokens to their device; dial only known push-pull hosts A client token paired to one device could read, reconfigure, delete, sync saves for and record play sessions as any other device of the same user. Device CRUD, sync negotiate/complete, push-pull trigger, the save device resolver and play-session ingest now treat another device as not found for a bound token, through a shared require_token_device helper. GET /api/devices lists only the bound device. Push-pull let any user with devices.write point ssh_host at any host and port, and the connection error surfaced through the sync session, so the worker could be used to probe the internal network. The SSH handler now refuses a host with no trusted key or CA in the admin's known_hosts before dialling it, which keeps LAN handhelds working while nothing else is reached. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(device): revoke a deleted device's tokens; scope registration matches Addresses Greptile review on #5460. Deleting a device now deletes the client tokens paired to it, since the foreign key's SET NULL turned them into unbound tokens that reach every device. Registration with a bound token only matches its own device by fingerprint, so it can no longer reset syncs or overwrite capabilities on another device. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(sync): bind heartbeat, sync session and online reads; pin push-pull ports pr-ready step 2 (code-review xhigh --fix). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(device): one require_device helper; parse known_hosts once pr-ready step 3 (simplify). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(sync): narrow await_args before reading known_hosts pr-ready step 4 (review-polish). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(sync): plainer comments on the push-pull allowlist and token revoke pr-ready step 5 (humanizer). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(device): keep bound tokens off other tokens; cascade tokens with their device A device-bound token can no longer create tokens, or regenerate, pair or revoke a token of another device or an unbound one. The save session_id counter only counts a session of the caller's bound device. client_tokens.device_id now cascades on device delete (0159), so no path can leave an unbound token behind, and deleting a device records a client_token.revoke audit event for each token it takes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore(env): drop the push-pull allowlist note from env.template Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
167 lines
4.8 KiB
Python
167 lines
4.8 KiB
Python
from collections.abc import Iterable, Sequence
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
from sqlalchemy import delete, func, or_, select, update
|
|
from sqlalchemy.orm import Session, joinedload
|
|
|
|
from decorators.database import INJECTED_SESSION, begin_session
|
|
from models.client_token import ClientToken
|
|
|
|
from .base_handler import DBBaseHandler, affected_rows
|
|
|
|
LAST_USED_DEBOUNCE = timedelta(minutes=5)
|
|
|
|
|
|
class DBClientTokensHandler(DBBaseHandler):
|
|
@begin_session
|
|
def add_token(
|
|
self,
|
|
token: ClientToken,
|
|
session: Session = INJECTED_SESSION,
|
|
) -> ClientToken:
|
|
return session.merge(token)
|
|
|
|
@begin_session
|
|
def get_token_by_hash(
|
|
self,
|
|
hashed_token: str,
|
|
session: Session = INJECTED_SESSION,
|
|
) -> ClientToken | None:
|
|
return session.scalar(
|
|
select(ClientToken).where(ClientToken.hashed_token == hashed_token)
|
|
)
|
|
|
|
@begin_session
|
|
def get_tokens_by_user(
|
|
self,
|
|
user_id: int,
|
|
session: Session = INJECTED_SESSION,
|
|
) -> Sequence[ClientToken]:
|
|
return session.scalars(
|
|
select(ClientToken)
|
|
.where(ClientToken.user_id == user_id)
|
|
.order_by(ClientToken.created_at.desc())
|
|
).all()
|
|
|
|
@begin_session
|
|
def get_token_ids_by_users(
|
|
self,
|
|
user_ids: Iterable[int],
|
|
session: Session = None, # type: ignore[assignment]
|
|
) -> Sequence[int]:
|
|
return session.scalars(
|
|
select(ClientToken.id).where(ClientToken.user_id.in_(list(user_ids)))
|
|
).all()
|
|
|
|
@begin_session
|
|
def get_tokens_by_device(
|
|
self,
|
|
device_id: str,
|
|
session: Session = None, # type: ignore[assignment]
|
|
) -> Sequence[ClientToken]:
|
|
return session.scalars(
|
|
select(ClientToken).where(ClientToken.device_id == device_id)
|
|
).all()
|
|
|
|
@begin_session
|
|
def get_all_tokens(
|
|
self,
|
|
session: Session = INJECTED_SESSION,
|
|
) -> Sequence[ClientToken]:
|
|
return (
|
|
session.scalars(
|
|
select(ClientToken)
|
|
.options(joinedload(ClientToken.user))
|
|
.order_by(ClientToken.created_at.desc())
|
|
)
|
|
.unique()
|
|
.all()
|
|
)
|
|
|
|
@begin_session
|
|
def delete_token(
|
|
self,
|
|
token_id: int,
|
|
user_id: int | None = None,
|
|
session: Session = INJECTED_SESSION,
|
|
) -> int:
|
|
stmt = delete(ClientToken).where(ClientToken.id == token_id)
|
|
if user_id is not None:
|
|
stmt = stmt.where(ClientToken.user_id == user_id)
|
|
|
|
result = session.execute(stmt.execution_options(synchronize_session="evaluate"))
|
|
return affected_rows(result)
|
|
|
|
@begin_session
|
|
def update_last_used(
|
|
self,
|
|
token_id: int,
|
|
session: Session = INJECTED_SESSION,
|
|
) -> None:
|
|
now = datetime.now(timezone.utc)
|
|
# Debounced in the WHERE clause, so concurrent bumps have no earlier read to race.
|
|
session.execute(
|
|
update(ClientToken)
|
|
.where(
|
|
ClientToken.id == token_id,
|
|
or_(
|
|
ClientToken.last_used_at.is_(None),
|
|
ClientToken.last_used_at < now - LAST_USED_DEBOUNCE,
|
|
),
|
|
)
|
|
.values(last_used_at=now)
|
|
.execution_options(synchronize_session=False)
|
|
)
|
|
|
|
@begin_session
|
|
def update_hashed_token(
|
|
self,
|
|
token_id: int,
|
|
new_hash: str,
|
|
user_id: int | None = None,
|
|
session: Session = INJECTED_SESSION,
|
|
) -> ClientToken | None:
|
|
stmt = (
|
|
update(ClientToken)
|
|
.where(ClientToken.id == token_id)
|
|
.values(hashed_token=new_hash, last_used_at=None)
|
|
.execution_options(synchronize_session="evaluate")
|
|
)
|
|
|
|
if user_id is not None:
|
|
stmt = stmt.where(ClientToken.user_id == user_id)
|
|
|
|
result = session.execute(stmt)
|
|
if affected_rows(result) == 0:
|
|
return None
|
|
|
|
return session.get(ClientToken, token_id)
|
|
|
|
@begin_session
|
|
def count_tokens_by_user(
|
|
self,
|
|
user_id: int,
|
|
session: Session = INJECTED_SESSION,
|
|
) -> int:
|
|
return (
|
|
session.scalar(
|
|
select(func.count())
|
|
.select_from(ClientToken)
|
|
.where(ClientToken.user_id == user_id)
|
|
)
|
|
or 0
|
|
)
|
|
|
|
@begin_session
|
|
def get_token(
|
|
self,
|
|
token_id: int,
|
|
user_id: int | None = None,
|
|
session: Session = INJECTED_SESSION,
|
|
) -> ClientToken | None:
|
|
stmt = select(ClientToken).where(ClientToken.id == token_id)
|
|
if user_id is not None:
|
|
stmt = stmt.where(ClientToken.user_id == user_id)
|
|
|
|
return session.scalar(stmt)
|