Skip to content

Commit 50363ba

Browse files
committed
refac
1 parent 860b90f commit 50363ba

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

backend/open_webui/utils/access_control/__init__.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,47 @@ async def filter_allowed_access_grants(
257257
return access_grants
258258

259259

260+
async def has_base_model_access(
261+
user_id: str,
262+
model_info,
263+
*,
264+
user_group_ids: set[str] | None = None,
265+
db=None,
266+
) -> bool:
267+
"""
268+
Walk the ``base_model_id`` chain and verify the caller has read access
269+
at every hop.
270+
271+
Returns ``True`` when access is granted (or the chain ends at a raw
272+
provider model that has no per-model ACL). Returns ``False`` the
273+
moment a registered base model denies access.
274+
"""
275+
from open_webui.models.models import Models
276+
from open_webui.models.access_grants import AccessGrants
277+
278+
base_model_id = getattr(model_info, 'base_model_id', None)
279+
seen = {model_info.id}
280+
while base_model_id and base_model_id not in seen:
281+
seen.add(base_model_id)
282+
base_model_info = await Models.get_model_by_id(base_model_id, db=db)
283+
if base_model_info is None:
284+
break # Raw provider model — no per-model ACL
285+
if not (
286+
user_id == base_model_info.user_id
287+
or await AccessGrants.has_access(
288+
user_id=user_id,
289+
resource_type='model',
290+
resource_id=base_model_info.id,
291+
permission='read',
292+
user_group_ids=user_group_ids,
293+
db=db,
294+
)
295+
):
296+
return False
297+
base_model_id = getattr(base_model_info, 'base_model_id', None)
298+
return True
299+
300+
260301
async def check_model_access(
261302
user: UserModel,
262303
model_info,
@@ -296,6 +337,12 @@ async def check_model_access(
296337
)
297338
):
298339
raise HTTPException(status_code=403, detail='Model not found')
340+
341+
# Enforce access on chained base models
342+
if not await has_base_model_chain_access(
343+
user.id, model_info, user_group_ids=user_group_ids
344+
):
345+
raise HTTPException(status_code=403, detail='Model not found')
299346
else:
300347
if user.role != 'admin':
301348
raise HTTPException(status_code=403, detail='Model not found')

backend/open_webui/utils/models.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
load_function_module_by_id,
2323
get_function_module_from_cache,
2424
)
25-
from open_webui.utils.access_control import has_access
25+
from open_webui.utils.access_control import has_access, has_base_model_access
2626

2727

2828
from open_webui.config import (
@@ -404,6 +404,11 @@ async def check_model_access(user, model, db=None):
404404
):
405405
raise Exception('Model not found')
406406

407+
# Enforce access on chained base models
408+
if not await has_base_model_access(user.id, model_info, db=db):
409+
raise Exception('Model not found')
410+
411+
407412

408413
async def get_filtered_models(models, user, db=None):
409414
# Filter out models that the user does not have access to

0 commit comments

Comments
 (0)