mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-10-21 07:55:18 +08:00
fix(api): scope TOTP oversight to the caller's workspace
The second-factor listing was instance-wide and the three mutating endpoints never checked that the target Account belongs to the caller's Workspace, so an owner or admin could see and reset another tenant's account. The listing now resolves the Workspace members and the mutations answer 404 for a foreign account, which does not disclose that it exists. Also fixes the TOTP write path on Postgres: six write points passed timezone-aware datetimes into timestamp-without-time-zone columns, which asyncpg rejects, so every enroll, confirm, revoke and recovery-code consumption raised a 500 there. SQLite tolerated the same values. The panel derives its oversight view from the API authorization instead of the length of the account list, which would have hidden it in a single-member Workspace.
This commit is contained in:
@@ -914,22 +914,52 @@ class UserRouterGroup(group.RouterGroup):
|
||||
if access.membership.role not in ('owner', 'admin'):
|
||||
raise PermissionError('Only Workspace owners and admins may manage other Accounts')
|
||||
|
||||
async def _require_target_in_workspace(
|
||||
request_context: RequestContext,
|
||||
target_account_uuid: str,
|
||||
) -> bool:
|
||||
"""Report whether the target Account belongs to the caller's Workspace.
|
||||
|
||||
A cross-Workspace Account is reported as absent, so the response never
|
||||
confirms another tenant's Account existence.
|
||||
"""
|
||||
|
||||
try:
|
||||
await self.ap.workspace_collaboration_service.resolve_account_workspace(
|
||||
target_account_uuid,
|
||||
request_context.workspace_uuid,
|
||||
)
|
||||
except Exception:
|
||||
return False
|
||||
return True
|
||||
|
||||
@self.route('/totp/accounts', methods=['GET'], auth_type=group.AuthType.USER_TOKEN)
|
||||
async def _(request_context: RequestContext) -> str:
|
||||
"""List the second-factor state of every Account for owners/admins.
|
||||
"""List the second-factor state of the caller's Workspace Accounts.
|
||||
|
||||
Oversight is deliberately instance-wide: managing the second factor
|
||||
of any Account is an owner/admin responsibility and is not scoped to
|
||||
the caller's Workspace.
|
||||
Oversight is a Workspace responsibility: owners and admins answer for
|
||||
the Accounts that belong to their own Workspace and must never see
|
||||
another tenant's Accounts.
|
||||
"""
|
||||
try:
|
||||
await _require_workspace_manager(request_context)
|
||||
access = await self.ap.workspace_collaboration_service.resolve_account_workspace(
|
||||
request_context.account_uuid,
|
||||
request_context.workspace_uuid,
|
||||
)
|
||||
if access.membership.role not in ('owner', 'admin'):
|
||||
raise PermissionError('Only Workspace owners and admins may manage other Accounts')
|
||||
members = await self.ap.workspace_collaboration_service.list_members(
|
||||
request_context.workspace_uuid,
|
||||
access.membership,
|
||||
)
|
||||
except PermissionError as e:
|
||||
return self.http_status(403, 'permission_denied', str(e))
|
||||
except Exception:
|
||||
return self.http_status(403, 'permission_denied', 'Not permitted')
|
||||
|
||||
accounts = await self.ap.totp_service.list_account_states()
|
||||
accounts = await self.ap.totp_service.list_account_states(
|
||||
account_uuids=[member.membership.account_uuid for member in members],
|
||||
)
|
||||
return self.success(data={'accounts': accounts})
|
||||
|
||||
@self.route('/totp/accounts/<target_account_uuid>', methods=['DELETE'], auth_type=group.AuthType.USER_TOKEN)
|
||||
@@ -948,6 +978,9 @@ class UserRouterGroup(group.RouterGroup):
|
||||
except Exception:
|
||||
return self.http_status(403, 'permission_denied', 'Not permitted')
|
||||
|
||||
if not await _require_target_in_workspace(request_context, target_account_uuid):
|
||||
return self.http_status(404, 'account_not_found', 'Account not found')
|
||||
|
||||
revoked = await self.ap.totp_service.revoke_for_account(target_account_uuid)
|
||||
if not revoked:
|
||||
return self.http_status(404, 'totp_not_enrolled', 'TOTP is not enabled for that Account')
|
||||
@@ -978,6 +1011,9 @@ class UserRouterGroup(group.RouterGroup):
|
||||
except Exception:
|
||||
return self.http_status(403, 'permission_denied', 'Not permitted')
|
||||
|
||||
if not await _require_target_in_workspace(request_context, target_account_uuid):
|
||||
return self.http_status(404, 'account_not_found', 'Account not found')
|
||||
|
||||
target = await self.ap.totp_service.get_account(target_account_uuid)
|
||||
if target is None:
|
||||
return self.http_status(404, 'account_not_found', 'Account not found')
|
||||
@@ -1022,6 +1058,9 @@ class UserRouterGroup(group.RouterGroup):
|
||||
if not isinstance(code, str) or not code:
|
||||
return self.fail(1, 'Missing code parameter')
|
||||
|
||||
if not await _require_target_in_workspace(request_context, target_account_uuid):
|
||||
return self.http_status(404, 'account_not_found', 'Account not found')
|
||||
|
||||
try:
|
||||
result = await self.ap.totp_service.confirm_enrollment(target_account_uuid, code)
|
||||
except totp_module.TotpNotEnrolledError as e:
|
||||
|
||||
@@ -49,6 +49,17 @@ _TOTP_MAX_ATTEMPTS = 5
|
||||
_RECOVERY_ALPHABET = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'
|
||||
|
||||
|
||||
def _utc_now_naive() -> datetime.datetime:
|
||||
"""UTC now without tzinfo, matching the credential ``timestamp`` columns.
|
||||
|
||||
The TOTP tables store ``timestamp without time zone``. asyncpg rejects an
|
||||
offset-aware value for such a column (SQLite accepts it, which is why this
|
||||
only surfaced on PostgreSQL), so every write converts at the boundary.
|
||||
"""
|
||||
|
||||
return datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
|
||||
|
||||
|
||||
class TotpError(ValueError):
|
||||
"""Base class for TOTP second-factor failures."""
|
||||
|
||||
@@ -363,7 +374,7 @@ class TotpService:
|
||||
await session.execute(
|
||||
sqlalchemy.update(totp_entity.TotpCredential)
|
||||
.where(totp_entity.TotpCredential.account_uuid == account_uuid)
|
||||
.values(disabled_at=datetime.datetime.now(datetime.timezone.utc))
|
||||
.values(disabled_at=_utc_now_naive())
|
||||
)
|
||||
await session.execute(
|
||||
sqlalchemy.delete(totp_entity.TotpRecoveryCode).where(
|
||||
@@ -409,7 +420,7 @@ class TotpService:
|
||||
|
||||
counter = self._verify_counter(credential, code)
|
||||
codes = _generate_recovery_codes()
|
||||
now = datetime.datetime.now(datetime.timezone.utc)
|
||||
now = _utc_now_naive()
|
||||
|
||||
async with self._session_factory()() as session:
|
||||
async with session.begin():
|
||||
@@ -479,7 +490,7 @@ class TotpService:
|
||||
if not await self.verify_code(account_uuid, code, allow_recovery=True):
|
||||
raise TotpInvalidCodeError('Invalid TOTP code')
|
||||
|
||||
now = datetime.datetime.now(datetime.timezone.utc)
|
||||
now = _utc_now_naive()
|
||||
async with self._session_factory()() as session:
|
||||
async with session.begin():
|
||||
await session.execute(
|
||||
@@ -497,11 +508,16 @@ class TotpService:
|
||||
# ------------------------------------------------------------------
|
||||
# owner/admin oversight
|
||||
# ------------------------------------------------------------------
|
||||
async def list_account_states(self) -> list[dict[str, typing.Any]]:
|
||||
"""Second-factor state for every Account, for owner/admin oversight.
|
||||
async def list_account_states(
|
||||
self,
|
||||
*,
|
||||
account_uuids: typing.Sequence[str],
|
||||
) -> list[dict[str, typing.Any]]:
|
||||
"""Second-factor state for the Accounts a Workspace owner/admin may see.
|
||||
|
||||
Oversight is instance-wide by design: an owner/admin may manage the
|
||||
second factor of any Account.
|
||||
Oversight belongs to the Workspace, so the caller supplies the member
|
||||
Account UUIDs of its own Workspace: an owner/admin must never observe or
|
||||
manage another tenant's Accounts.
|
||||
|
||||
Only state is returned: no secret and no recovery-code material.
|
||||
"""
|
||||
@@ -525,6 +541,7 @@ class TotpService:
|
||||
credential.last_used_at,
|
||||
sqlalchemy.func.coalesce(unused_codes.c.unused, 0),
|
||||
)
|
||||
.where(user.User.uuid.in_(tuple(account_uuids)))
|
||||
.outerjoin(
|
||||
credential,
|
||||
sqlalchemy.and_(
|
||||
@@ -558,7 +575,7 @@ class TotpService:
|
||||
outstanding recovery codes are destroyed.
|
||||
"""
|
||||
|
||||
now = datetime.datetime.now(datetime.timezone.utc)
|
||||
now = _utc_now_naive()
|
||||
async with self._session_factory()() as session:
|
||||
async with session.begin():
|
||||
result = await session.execute(
|
||||
@@ -630,7 +647,7 @@ class TotpService:
|
||||
raise
|
||||
|
||||
if consume_counter:
|
||||
now = datetime.datetime.now(datetime.timezone.utc)
|
||||
now = _utc_now_naive()
|
||||
async with self._session_factory()() as session:
|
||||
async with session.begin():
|
||||
await session.execute(
|
||||
@@ -680,7 +697,7 @@ class TotpService:
|
||||
totp_entity.TotpRecoveryCode.id == matched_id,
|
||||
totp_entity.TotpRecoveryCode.used_at.is_(None),
|
||||
)
|
||||
.values(used_at=datetime.datetime.now(datetime.timezone.utc))
|
||||
.values(used_at=_utc_now_naive())
|
||||
)
|
||||
await session.commit()
|
||||
return bool(result.rowcount)
|
||||
|
||||
Reference in New Issue
Block a user