| 1 | """Extract a user-selected extension ZIP into a newly created staging directory.""" |
| 2 | import json |
| 3 | import pathlib |
| 4 | import stat |
| 5 | import re |
| 6 | import sys |
| 7 | import zipfile |
| 8 | |
| 9 | archive, destination = map(pathlib.Path, sys.argv[1:]) |
| 10 | with zipfile.ZipFile(archive) as source: |
| 11 | entries = source.infolist() |
| 12 | if len(entries) > 3000 or sum(item.file_size for item in entries) > 128 * 1024 * 1024: |
| 13 | raise ValueError('扩展解压后不能超过 128 MB 或 3000 个文件') |
| 14 | for item in entries: |
| 15 | path = pathlib.PurePosixPath(item.filename) |
| 16 | if not item.filename or '\\' in item.filename or '\x00' in item.filename or path.is_absolute() or '..' in path.parts or ':' in path.parts[0]: |
| 17 | raise ValueError('扩展包包含不安全的文件路径') |
| 18 | if stat.S_ISLNK(item.external_attr >> 16) or item.flag_bits & 1: |
| 19 | raise ValueError('扩展包不能包含符号链接或加密文件') |
| 20 | if item.file_size > 32 * 1024 * 1024: |
| 21 | raise ValueError('扩展包中的单个文件不能超过 32 MB') |
| 22 | source.extractall(destination) |
| 23 | manifest_path = destination / 'manifest.json' |
| 24 | if not manifest_path.is_file(): |
| 25 | candidates = list(destination.glob('*/manifest.json')) |
| 26 | if len(candidates) != 1: |
| 27 | raise ValueError('扩展包需要在根目录或单层文件夹内包含 manifest.json') |
| 28 | manifest_path = candidates[0] |
| 29 | if manifest_path.stat().st_size > 1024 * 1024: |
| 30 | raise ValueError('扩展配置过大') |
| 31 | manifest = json.loads(manifest_path.read_text(encoding='utf-8-sig')) |
| 32 | if re.fullmatch(r'[A-Za-z0-9_-]{1,32}', str(manifest.get('default_locale', ''))): |
| 33 | messages_path = manifest_path.parent / '_locales' / manifest['default_locale'] / 'messages.json' |
| 34 | if messages_path.is_file() and messages_path.stat().st_size < 1024 * 1024: |
| 35 | messages = json.loads(messages_path.read_text(encoding='utf-8-sig')) |
| 36 | for field in ('name', 'description'): |
| 37 | value = manifest.get(field, '') |
| 38 | if isinstance(value, str) and value.startswith('__MSG_') and value.endswith('__'): |
| 39 | manifest[field] = messages.get(value[6:-2], {}).get('message', value) |
| 40 | if manifest.get('manifest_version') != 3: |
| 41 | raise ValueError('仅支持 Manifest V3 的 Chromium 扩展') |
| 42 | if not isinstance(manifest.get('name'), str) or not manifest['name'] or not isinstance(manifest.get('version'), str): |
| 43 | raise ValueError('扩展缺少名称或版本') |
| 44 | permissions = manifest.get('permissions', []) |
| 45 | hosts = manifest.get('host_permissions', []) |
| 46 | |
| 47 | for script in manifest.get('content_scripts', []): |
| 48 | hosts = hosts + script.get('matches', []) |
| 49 | if not all(isinstance(items, list) and len(items) <= 300 and all(isinstance(value, str) and len(value) <= 2048 for value in items) for items in [permissions, hosts]): |
| 50 | raise ValueError('扩展权限声明无效') |
| 51 | print(json.dumps({'relative': str(manifest_path.parent.relative_to(destination)), |
| 52 | 'name': manifest['name'][:200], 'version': manifest['version'][:100], |
| 53 | 'description': str(manifest.get('description', ''))[:1000], |
| 54 | 'key': str(manifest.get('key', ''))[:8192], |
| 55 | 'permissions': list(dict.fromkeys(permissions)), 'hostPermissions': list(dict.fromkeys(hosts))}, ensure_ascii=False)) |