Files
modelscope/tests/utils/test_owner_group_path_safety.py
suluyana ac61e0bd8f [Fix]dynamic module auto map double dash (#1761)
* fix: resolve cross-repo auto_map when cache paths contain --

modelscope_hub 0.1.x layout embeds -- in local dirs; rejoining that
path into class_reference broke transformers' split("--"). Pass the
local snapshot as pretrained_model_name_or_path with a bare class name
instead, and stop mutating the args tuple in place.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: reject empty model name in trusted owner cache paths

check_model_from_owner_group treated paths like iic--/snapshots/v1 as
trusted because split('--') still yields two parts. Require both owner
and name segments to be non-empty.

Co-authored-by: Cursor <cursoragent@cursor.com>

* test: cover remote pretrained args tuple rebuild in dynamic module patcher

The TypeError from mutating *args was already fixed with the auto_map
double-dash change; add an explicit regression test for that branch.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: derive valid module names from snapshot revision paths

Path(model_dir).stem on revisions like v1.0.4 produced v1.0, so
importlib treated it as package v1 and raised ModuleNotFoundError.
Use the owner--name (+ revision) segment and sanitize to an identifier.

Co-authored-by: Cursor <cursoragent@cursor.com>

* test: cover module naming for dotted snapshot revisions

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix: handle pretrained_model_name_or_path passed via kwargs

When the arg is only in kwargs, downloading and then forcing a
positional overwrite caused a duplicate-keyword TypeError. Resolve and
update kwargs or args consistently for both the download and cross-repo
branches.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix lint

* fix(docker): catch JSONDecodeError when querying Docker Hub tags

Non-JSON Hub responses (block pages, 5xx HTML) would bypass URLError
handling and crash json.load; surface them as RuntimeError instead.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: suluyan <suluyan.sly@aliabab-inc.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-20 19:49:53 +08:00

31 lines
1.1 KiB
Python

# Copyright (c) Alibaba, Inc. and its affiliates.
import unittest
from modelscope.utils.automodel_utils import check_model_from_owner_group
class OwnerGroupPathSafetyTest(unittest.TestCase):
"""Safety checks for trusted-owner cache path recognition."""
def test_empty_name_cache_path_rejected(self):
# modelscope_hub layout: {cache}/{owner}--{name}/snapshots/{rev}
# Empty name ("iic--") must not be treated as a trusted owner path.
self.assertFalse(
check_model_from_owner_group('/cache/iic--/snapshots/v1'))
self.assertFalse(
check_model_from_owner_group('/cache/damo--/snapshots/v1'))
def test_valid_and_spoof_cache_paths(self):
self.assertTrue(
check_model_from_owner_group('/cache/iic--x/snapshots/v1'))
self.assertFalse(
check_model_from_owner_group('/cache/--iic/snapshots/v1'))
self.assertFalse(
check_model_from_owner_group(
'/cache/iic--hacked--evil/snapshots/v1'))
self.assertTrue(check_model_from_owner_group('/cache/iic/some_model'))
if __name__ == '__main__':
unittest.main()