跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

browser-extension-archive.py

55 行 · 3.3 KB历史下载
1"""Extract a user-selected extension ZIP into a newly created staging directory."""
2import json
3import pathlib
4import stat
5import re
6import sys
7import zipfile
8
9archive, destination = map(pathlib.Path, sys.argv[1:])
10with 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)
23manifest_path = destination / 'manifest.json'
24if 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]
29if manifest_path.stat().st_size > 1024 * 1024:
30 raise ValueError('扩展配置过大')
31manifest = json.loads(manifest_path.read_text(encoding='utf-8-sig'))
32if 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)
40if manifest.get('manifest_version') != 3:
41 raise ValueError('仅支持 Manifest V3 的 Chromium 扩展')
42if not isinstance(manifest.get('name'), str) or not manifest['name'] or not isinstance(manifest.get('version'), str):
43 raise ValueError('扩展缺少名称或版本')
44permissions = manifest.get('permissions', [])
45hosts = manifest.get('host_permissions', [])
46# Content scripts can read matched pages without a separate host_permissions list.
47for script in manifest.get('content_scripts', []):
48 hosts = hosts + script.get('matches', [])
49if 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('扩展权限声明无效')
51print(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))