From 56bf53a1fdf4aa42ff52979d8beb4a8e7ae50902 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Tue, 1 Sep 2026 20:34:57 +0800 Subject: [PATCH] fix(skills): raise skill zip cap and keep uninstall progress stable GitHub skill packages such as ppt-master exceed the 50MB knowledge-upload limit. Give skills their own 256MB download/upload cap, keep uninstalling visible if the list lags, and distinguish zip-entry vs skill-file errors. --- .env.example | 4 +- docker-compose.yml | 3 ++ frontend/docker-entrypoint.sh | 19 ++++++- frontend/public/config.js | 1 + .../components/SandboxSkillsPanel.test.mjs | 4 ++ .../src/components/SandboxSkillsPanel.vue | 49 ++++++++++++++----- frontend/src/i18n/locales/en-US.ts | 1 + frontend/src/i18n/locales/ko-KR.ts | 1 + frontend/src/i18n/locales/ru-RU.ts | 1 + frontend/src/i18n/locales/zh-CN.ts | 1 + frontend/src/utils/index.ts | 30 ++++++++++-- .../src/views/settings/SkillSettings.test.mjs | 2 + frontend/src/views/settings/SkillSettings.vue | 20 +++++--- .../service/tenant_skill_bundle.go | 4 +- .../service/tenant_skill_bundle_test.go | 43 +++++++++++++++- .../application/service/tenant_skill_files.go | 2 +- .../service/tenant_skill_source.go | 8 +-- .../service/tenant_skill_source_test.go | 18 +++++++ internal/handler/sandbox_skill.go | 4 +- internal/handler/sandbox_skill_test.go | 1 + internal/handler/skill_catalog.go | 2 +- internal/utils/filesize.go | 45 ++++++++++++++--- internal/utils/filesize_test.go | 40 +++++++++++++++ 23 files changed, 259 insertions(+), 44 deletions(-) create mode 100644 internal/utils/filesize_test.go diff --git a/.env.example b/.env.example index a152d401..59e86b47 100644 --- a/.env.example +++ b/.env.example @@ -185,8 +185,10 @@ STORAGE_TYPE=local LOCAL_STORAGE_BASE_DIR=/data/files # 本地存储路径前缀(legacy 回填用,一般留空)。 # LOCAL_STORAGE_PATH_PREFIX= -# 统一文件大小限制(MB,默认 50)。影响单文件上传、技能 zip / 来源下载、docreader gRPC 消息、frontend Nginx 请求体、浏览器客户端校验。属部署期配置:Go/Nginx/docreader/浏览器四层启动时读一次,运行中改不生效,改后须同步重启四层。 +# 统一文件大小限制(MB,默认 50)。影响知识库/附件单文件上传、docreader gRPC 消息、浏览器知识库校验。属部署期配置:Go/Nginx/docreader/浏览器四层启动时读一次,运行中改不生效,改后须同步重启四层。 # MAX_FILE_SIZE_MB=50 +# 技能 zip 上传与 GitHub/ClawHub 来源下载上限(MB,默认 256)。与知识库上传分开:ppt-master 一类技能包会超过 50MB。未设置时不低于 MAX_FILE_SIZE_MB,且不超过 512。Nginx 请求体取两者较大值。改后须重启 app 与 frontend。 +# MAX_SKILL_BUNDLE_SIZE_MB=256 # ========== B4. 对象存储 provider(按 STORAGE_TYPE 选其一)========== # ----- MinIO(STORAGE_TYPE=minio)----- diff --git a/docker-compose.yml b/docker-compose.yml index 7c45c11b..18535fae 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,6 +9,7 @@ services: - "${FRONTEND_PORT:-80}:80" environment: - MAX_FILE_SIZE_MB=${MAX_FILE_SIZE_MB:-50} + - MAX_SKILL_BUNDLE_SIZE_MB=${MAX_SKILL_BUNDLE_SIZE_MB:-256} - DEFAULT_LOCALE=${DEFAULT_LOCALE:-} - APP_HOST=${APP_HOST:-app} # APP_BACKEND_PORT: the port NGINX proxies to (default 8080). @@ -311,6 +312,8 @@ services: - JWT_SECRET=${JWT_SECRET:-} # File size limit (in MB) - MAX_FILE_SIZE_MB=${MAX_FILE_SIZE_MB:-50} + # Skill zip / GitHub source download cap (in MB); independent of knowledge uploads + - MAX_SKILL_BUNDLE_SIZE_MB=${MAX_SKILL_BUNDLE_SIZE_MB:-256} # 文档处理任务总超时(Go duration,默认 2h) - WEKNORA_DOCUMENT_PROCESS_TIMEOUT=${WEKNORA_DOCUMENT_PROCESS_TIMEOUT:-} # 单次 DocReader RPC 超时(默认 30m,须小于上一项) diff --git a/frontend/docker-entrypoint.sh b/frontend/docker-entrypoint.sh index 65ac2f44..e72286bc 100644 --- a/frontend/docker-entrypoint.sh +++ b/frontend/docker-entrypoint.sh @@ -7,15 +7,30 @@ case "${DEFAULT_LOCALE:-}" in esac # 生成运行时配置文件,注入环境变量到前端 +FILE_MB=${MAX_FILE_SIZE_MB:-50} +SKILL_MB=${MAX_SKILL_BUNDLE_SIZE_MB:-256} +if [ "$SKILL_MB" -lt "$FILE_MB" ] 2>/dev/null; then + SKILL_MB=$FILE_MB +fi +if [ "$SKILL_MB" -gt 512 ] 2>/dev/null; then + SKILL_MB=512 +fi +if [ "$FILE_MB" -gt "$SKILL_MB" ] 2>/dev/null; then + NGINX_MB=$FILE_MB +else + NGINX_MB=$SKILL_MB +fi + cat > /usr/share/nginx/html/config.js << EOF window.__RUNTIME_CONFIG__ = { - MAX_FILE_SIZE_MB: ${MAX_FILE_SIZE_MB:-50}, + MAX_FILE_SIZE_MB: ${FILE_MB}, + MAX_SKILL_BUNDLE_SIZE_MB: ${SKILL_MB}, DEFAULT_LOCALE: "${RUNTIME_DEFAULT_LOCALE}" }; EOF # 处理 nginx 配置 -export MAX_FILE_SIZE=${MAX_FILE_SIZE_MB}M +export MAX_FILE_SIZE=${NGINX_MB}M export APP_HOST=${APP_HOST:-app} export APP_PORT=${APP_PORT:-8080} export APP_SCHEME=${APP_SCHEME:-http} diff --git a/frontend/public/config.js b/frontend/public/config.js index b08839ea..08914524 100644 --- a/frontend/public/config.js +++ b/frontend/public/config.js @@ -1,6 +1,7 @@ // 运行时配置(本地开发默认值,Docker 环境会被 entrypoint 脚本覆盖) window.__RUNTIME_CONFIG__ = { MAX_FILE_SIZE_MB: 50, + MAX_SKILL_BUNDLE_SIZE_MB: 256, // Optional: serve embed on a dedicated origin, e.g. 'https://embed.example.com' EMBED_BASE_URL: '', // Optional: default UI locale for first-time visitors (zh-CN | en-US | ru-RU | ko-KR) diff --git a/frontend/src/components/SandboxSkillsPanel.test.mjs b/frontend/src/components/SandboxSkillsPanel.test.mjs index 839e9f88..c42857a3 100644 --- a/frontend/src/components/SandboxSkillsPanel.test.mjs +++ b/frontend/src/components/SandboxSkillsPanel.test.mjs @@ -20,6 +20,10 @@ test('focused skill management expands env vars and transcript without a mid-pag assert.match(manageBlock, /askRemove/) assert.match(manageBlock, /skillRemoveDone/) assert.match(manageBlock, /progressStageText/) + assert.match(source, /overlayUninstallStatus/) + assert.match(source, /uninstallingId\.value === skill\.id/) + assert.match(source, /MAX_SKILL_BUNDLE_SIZE_MB/) + assert.match(source, /skillBundleTooManyZipEntries/) assert.doesNotMatch(manageBlock, /t-icon name="delete"/) assert.doesNotMatch(manageBlock, /skill-manage__footer/) assert.doesNotMatch(manageBlock, /settings\.sandbox\.skillEnv\.save/) diff --git a/frontend/src/components/SandboxSkillsPanel.vue b/frontend/src/components/SandboxSkillsPanel.vue index 2a5fb636..926d9ee3 100644 --- a/frontend/src/components/SandboxSkillsPanel.vue +++ b/frontend/src/components/SandboxSkillsPanel.vue @@ -17,7 +17,7 @@

{{ $t('settings.sandbox.skillSourceSection') }}

-

{{ $t('settings.sandbox.skillSourceSectionHint', { size: MAX_FILE_SIZE_MB }) }}

+

{{ $t('settings.sandbox.skillSourceSectionHint', { size: MAX_SKILL_BUNDLE_SIZE_MB }) }}

{{ $t('settings.sandbox.skillUploadSection') }}

-

{{ $t('settings.sandbox.skillUploadSectionHint', { size: MAX_FILE_SIZE_MB }) }}

+

{{ $t('settings.sandbox.skillUploadSectionHint', { size: MAX_SKILL_BUNDLE_SIZE_MB }) }}

`${skill.id}:${skill.status}:${skill.enabled ? 1 : 0}`).join('|') } +function overlayUninstallStatus(list: ConfigSkill[]): ConfigSkill[] { + const id = uninstallingId.value + if (!id || uninstallDone.value) return list + if (progressById.value[id]?.stage === 'failed') return list + return list.map((item) => { + if (item.id !== id) return item + if (item.status === 'removed' || item.status === 'failed' || item.status === 'removing') { + return item + } + return { ...item, status: 'removing' } + }) +} + async function loadSkills(silent = false) { if (!props.record) return if (!silent) loading.value = true @@ -1120,7 +1136,7 @@ async function loadSkills(silent = false) { const wasBusy = skills.value.some(isBusy) try { const res = await listConfigSkills(props.record.id) - skills.value = res?.data || [] + skills.value = overlayUninstallStatus(res?.data || []) followBusySkills() ensurePoll() if (skillsSignature(skills.value) !== previous) { @@ -1199,11 +1215,19 @@ function isZipFile(file: File): boolean { function skillBundleErrorMessage(err: any, fallbackKey: string): string { const raw = String(err?.message || '') if (/cannot exceed \d+\s*MB/i.test(raw)) { - return t('settings.sandbox.skillBundleTooLarge', { size: MAX_FILE_SIZE_MB }) + return t('settings.sandbox.skillBundleTooLarge', { size: MAX_SKILL_BUNDLE_SIZE_MB }) } - const tooMany = raw.match(/archive holds more than (\d+) files/i) - if (tooMany) { - return t('settings.sandbox.skillBundleTooManyFiles', { count: tooMany[1] }) + const tooManyFiles = raw.match(/skill directory holds more than (\d+) files/i) + if (tooManyFiles) { + return t('settings.sandbox.skillBundleTooManyFiles', { count: tooManyFiles[1] }) + } + const tooManyEntries = raw.match(/archive has more than (\d+) zip entries/i) + if (tooManyEntries) { + return t('settings.sandbox.skillBundleTooManyZipEntries', { count: tooManyEntries[1] }) + } + const legacyTooMany = raw.match(/archive holds more than (\d+) files/i) + if (legacyTooMany) { + return t('settings.sandbox.skillBundleTooManyFiles', { count: legacyTooMany[1] }) } return raw || t(fallbackKey) } @@ -1218,8 +1242,8 @@ async function uploadFile(file: File) { MessagePlugin.error(t('settings.sandbox.skillUploadFailed')) return } - if (file.size > MAX_FILE_SIZE_BYTES) { - MessagePlugin.error(t('settings.sandbox.skillBundleTooLarge', { size: MAX_FILE_SIZE_MB })) + if (file.size > MAX_SKILL_BUNDLE_SIZE_BYTES) { + MessagePlugin.error(t('settings.sandbox.skillBundleTooLarge', { size: MAX_SKILL_BUNDLE_SIZE_MB })) return } uploading.value = true @@ -1344,6 +1368,9 @@ async function removeSkill(skill: ConfigSkill) { await refreshImage() followProgress(skill.id) } catch (e: any) { + uninstallingId.value = '' + uninstallingName.value = '' + sawRemoving.value = false MessagePlugin.error(e?.message || t('common.deleteFailed')) } finally { deletingId.value = '' diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.ts index eb007e0c..51ab08d6 100755 --- a/frontend/src/i18n/locales/en-US.ts +++ b/frontend/src/i18n/locales/en-US.ts @@ -1334,6 +1334,7 @@ export default { skillUploadFailed: 'Failed to upload the skill', skillBundleTooLarge: 'The skill bundle cannot exceed {size} MB.', skillBundleTooManyFiles: 'The skill directory cannot hold more than {count} files.', + skillBundleTooManyZipEntries: 'The archive cannot have more than {count} zip entries.', skillUploading: 'Uploading {percent}%', skillUploadAccepted: 'Skill install started', skillStatusInstalling: 'Installing', diff --git a/frontend/src/i18n/locales/ko-KR.ts b/frontend/src/i18n/locales/ko-KR.ts index f999fb03..193a9065 100755 --- a/frontend/src/i18n/locales/ko-KR.ts +++ b/frontend/src/i18n/locales/ko-KR.ts @@ -5254,6 +5254,7 @@ export default { skillUploadFailed: 'Failed to upload the skill', skillBundleTooLarge: '스킬 압축 파일은 {size} MB를 넘을 수 없습니다.', skillBundleTooManyFiles: '스킬 디렉터리의 파일은 {count}개를 넘을 수 없습니다.', + skillBundleTooManyZipEntries: '압축 파일 항목은 {count}개를 넘을 수 없습니다.', skillUploading: 'Uploading {percent}%', skillUploadAccepted: 'Skill install started', skillStatusInstalling: 'Installing', diff --git a/frontend/src/i18n/locales/ru-RU.ts b/frontend/src/i18n/locales/ru-RU.ts index a4eee1f1..714068f8 100755 --- a/frontend/src/i18n/locales/ru-RU.ts +++ b/frontend/src/i18n/locales/ru-RU.ts @@ -5254,6 +5254,7 @@ export default { skillUploadFailed: 'Failed to upload the skill', skillBundleTooLarge: 'Архив навыка не может превышать {size} МБ.', skillBundleTooManyFiles: 'В каталоге навыка не может быть больше {count} файлов.', + skillBundleTooManyZipEntries: 'В архиве не может быть больше {count} записей.', skillUploading: 'Uploading {percent}%', skillUploadAccepted: 'Skill install started', skillStatusInstalling: 'Installing', diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts index 24eedc35..00b0a321 100755 --- a/frontend/src/i18n/locales/zh-CN.ts +++ b/frontend/src/i18n/locales/zh-CN.ts @@ -5256,6 +5256,7 @@ export default { skillUploadFailed: '技能上传失败', skillBundleTooLarge: '技能压缩包不能超过 {size} MB。', skillBundleTooManyFiles: '技能目录里的文件不能超过 {count} 个。', + skillBundleTooManyZipEntries: '压缩包条目不能超过 {count} 个。', skillUploading: '正在上传 {percent}%', skillUploadAccepted: '已开始安装技能', skillStatusInstalling: '安装中', diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.ts index 414609b6..40a47d12 100644 --- a/frontend/src/utils/index.ts +++ b/frontend/src/utils/index.ts @@ -7,17 +7,39 @@ declare global { interface Window { __RUNTIME_CONFIG__?: { MAX_FILE_SIZE_MB?: number; + MAX_SKILL_BUNDLE_SIZE_MB?: number; DEFAULT_LOCALE?: string; }; } } +function positiveMegabytes(value: unknown, fallback: number): number { + const n = typeof value === 'number' ? value : Number(value) + return Number.isFinite(n) && n > 0 ? n : fallback +} + // 从运行时配置获取最大文件大小(MB),支持 Docker 环境动态配置 // 优先级:运行时配置 > 构建时环境变量 > 默认值 50MB -export const MAX_FILE_SIZE_MB = window.__RUNTIME_CONFIG__?.MAX_FILE_SIZE_MB - || Number(import.meta.env.VITE_MAX_FILE_SIZE_MB) - || 50; -export const MAX_FILE_SIZE_BYTES = MAX_FILE_SIZE_MB * 1024 * 1024; +export const MAX_FILE_SIZE_MB = positiveMegabytes( + window.__RUNTIME_CONFIG__?.MAX_FILE_SIZE_MB ?? import.meta.env.VITE_MAX_FILE_SIZE_MB, + 50, +) +export const MAX_FILE_SIZE_BYTES = MAX_FILE_SIZE_MB * 1024 * 1024 + +// Skill zips are larger than knowledge files (GitHub zipballs, ppt-master). +// Never below the knowledge cap; ceiling matches the Go uncompressed archive cap. +export const MAX_SKILL_BUNDLE_SIZE_MB = Math.min( + 512, + Math.max( + positiveMegabytes( + window.__RUNTIME_CONFIG__?.MAX_SKILL_BUNDLE_SIZE_MB + ?? import.meta.env.VITE_MAX_SKILL_BUNDLE_SIZE_MB, + 256, + ), + MAX_FILE_SIZE_MB, + ), +) +export const MAX_SKILL_BUNDLE_SIZE_BYTES = MAX_SKILL_BUNDLE_SIZE_MB * 1024 * 1024 export function generateRandomString(length: number) { let result = ""; diff --git a/frontend/src/views/settings/SkillSettings.test.mjs b/frontend/src/views/settings/SkillSettings.test.mjs index f9d98525..4862722c 100644 --- a/frontend/src/views/settings/SkillSettings.test.mjs +++ b/frontend/src/views/settings/SkillSettings.test.mjs @@ -43,6 +43,8 @@ test('catalog cards can install onto sandboxes and manage one install', () => { assert.match(source, /skillSourceSectionHint/) assert.match(source, /skillUploadSectionHint/) assert.match(source, /maxSkillBundleMB/) + assert.match(source, /MAX_SKILL_BUNDLE_SIZE_MB/) + assert.match(source, /skillBundleTooManyZipEntries/) assert.match(source, /align-items: stretch/) assert.match(source, /margin-top: auto/) assert.match(source, /installsView/) diff --git a/frontend/src/views/settings/SkillSettings.vue b/frontend/src/views/settings/SkillSettings.vue index 8646cb81..034d6236 100644 --- a/frontend/src/views/settings/SkillSettings.vue +++ b/frontend/src/views/settings/SkillSettings.vue @@ -438,7 +438,7 @@ import ModelSelector from '@/components/ModelSelector.vue' import { useConfirmDelete } from '@/components/settings/useConfirmDelete' import { SKILL_ICON } from '@/types/mention' import { useUIStore } from '@/stores/ui' -import { MAX_FILE_SIZE_BYTES, MAX_FILE_SIZE_MB } from '@/utils' +import { MAX_SKILL_BUNDLE_SIZE_BYTES, MAX_SKILL_BUNDLE_SIZE_MB } from '@/utils' import { deleteSkillCatalog, installSkillCatalog, @@ -510,7 +510,7 @@ const skillConfigs = computed(() => ) const addBusy = computed(() => uploading.value || addingFromSource.value) -const maxSkillBundleMB = MAX_FILE_SIZE_MB +const maxSkillBundleMB = MAX_SKILL_BUNDLE_SIZE_MB const addSteps = computed(() => [ { key: 'register', title: t('settings.skills.addStepRegister') }, @@ -911,7 +911,7 @@ function acceptPendingFile(file: File) { MessagePlugin.error(t('settings.sandbox.skillUploadFailed')) return } - if (file.size > MAX_FILE_SIZE_BYTES) { + if (file.size > MAX_SKILL_BUNDLE_SIZE_BYTES) { MessagePlugin.error(t('settings.sandbox.skillBundleTooLarge', { size: maxSkillBundleMB })) return } @@ -923,9 +923,17 @@ function skillRegisterErrorMessage(err: any, fromFile: boolean): string { if (/cannot exceed \d+\s*MB/i.test(raw)) { return t('settings.sandbox.skillBundleTooLarge', { size: maxSkillBundleMB }) } - const tooMany = raw.match(/archive holds more than (\d+) files/i) - if (tooMany) { - return t('settings.sandbox.skillBundleTooManyFiles', { count: tooMany[1] }) + const tooManyFiles = raw.match(/skill directory holds more than (\d+) files/i) + if (tooManyFiles) { + return t('settings.sandbox.skillBundleTooManyFiles', { count: tooManyFiles[1] }) + } + const tooManyEntries = raw.match(/archive has more than (\d+) zip entries/i) + if (tooManyEntries) { + return t('settings.sandbox.skillBundleTooManyZipEntries', { count: tooManyEntries[1] }) + } + const legacyTooMany = raw.match(/archive holds more than (\d+) files/i) + if (legacyTooMany) { + return t('settings.sandbox.skillBundleTooManyFiles', { count: legacyTooMany[1] }) } if (raw) return raw return fromFile diff --git a/internal/application/service/tenant_skill_bundle.go b/internal/application/service/tenant_skill_bundle.go index 3d1bf4ea..90bd3d4d 100644 --- a/internal/application/service/tenant_skill_bundle.go +++ b/internal/application/service/tenant_skill_bundle.go @@ -138,7 +138,7 @@ func skillZipEntries(archive []byte, opts SkillBundleParseOptions) ([]skillZipEn return nil, fmt.Errorf("%w: not a readable zip archive: %v", ErrSkillBundleInvalid, err) } if len(reader.File) > maxSkillBundleZipEntries { - return nil, fmt.Errorf("%w: archive holds more than %d files", + return nil, fmt.Errorf("%w: archive has more than %d zip entries", ErrSkillBundleInvalid, maxSkillBundleZipEntries) } @@ -184,7 +184,7 @@ func skillZipEntries(archive []byte, opts SkillBundleParseOptions) ([]skillZipEn totalBytes += item.size out = append(out, item) if len(out) > maxSkillBundleFiles { - return nil, fmt.Errorf("%w: archive holds more than %d files", + return nil, fmt.Errorf("%w: skill directory holds more than %d files", ErrSkillBundleInvalid, maxSkillBundleFiles) } } diff --git a/internal/application/service/tenant_skill_bundle_test.go b/internal/application/service/tenant_skill_bundle_test.go index b3a4f496..3c6b7fa1 100644 --- a/internal/application/service/tenant_skill_bundle_test.go +++ b/internal/application/service/tenant_skill_bundle_test.go @@ -112,6 +112,35 @@ func zipBundleWithDeclaredSize(t *testing.T, name string, size uint64) []byte { return buf.Bytes() } +func zipGitHubStyleWithOversizeExtra(t *testing.T) []byte { + t.Helper() + var buf bytes.Buffer + w := zip.NewWriter(&buf) + + f, err := w.Create("repo-main/skills/pdf/SKILL.md") + require.NoError(t, err) + _, err = f.Write([]byte(validSkillMD)) + require.NoError(t, err) + + f, err = w.Create("repo-main/skills/pdf/run.py") + require.NoError(t, err) + _, err = f.Write([]byte("print(1)\n")) + require.NoError(t, err) + + header := &zip.FileHeader{ + Name: "repo-main/vendor/huge.bin", + Method: zip.Store, + UncompressedSize64: uint64(maxSkillBundleFileBytes + 1), + CompressedSize64: 0, + CRC32: 0, + } + _, err = w.CreateRaw(header) + require.NoError(t, err) + + require.NoError(t, w.Close()) + return buf.Bytes() +} + type repeatedByteReader byte func (r repeatedByteReader) Read(p []byte) (int, error) { @@ -342,6 +371,16 @@ Use the scripts in this skill. require.NotContains(t, bundle.Files, "README.md") }) + t.Run("remote options ignore oversize files outside the skill subtree", func(t *testing.T) { + bundle, err := ParseSkillBundleWithOptions(zipGitHubStyleWithOversizeExtra(t), SkillBundleParseOptions{ + AllowExtraFiles: true, + AllowNestedSkill: true, + }) + require.NoError(t, err) + require.Equal(t, "pdf-tools", bundle.Name) + require.Contains(t, bundle.Files, "run.py") + }) + t.Run("directory entries do not count toward the file cap", func(t *testing.T) { bundle, err := ParseSkillBundle(zipBundleWithDirEntries(t, 3000)) require.NoError(t, err) @@ -349,10 +388,10 @@ Use the scripts in this skill. require.Len(t, bundle.Files, 1) }) - t.Run("rejects too many entries", func(t *testing.T) { + t.Run("rejects too many skill files", func(t *testing.T) { _, err := ParseSkillBundle(zipBundleWithNFiles(t, maxSkillBundleFiles)) require.ErrorIs(t, err, ErrSkillBundleInvalid) - require.ErrorContains(t, err, "archive holds more than") + require.ErrorContains(t, err, "skill directory holds more than") }) t.Run("rejects per-entry oversize", func(t *testing.T) { diff --git a/internal/application/service/tenant_skill_files.go b/internal/application/service/tenant_skill_files.go index 79fcb05e..46d1015c 100644 --- a/internal/application/service/tenant_skill_files.go +++ b/internal/application/service/tenant_skill_files.go @@ -31,7 +31,7 @@ const ( // The settings drawer lists the tree then immediately opens SKILL.md, and // every later click is another read of the same zip. A handful of recent - // archives covers that without holding a 256 MiB upload in RAM. + // archives covers that without holding a 512 MiB upload in RAM. skillBundleArchiveCacheSlots = 8 skillBundleArchiveCacheBytes = 64 << 20 // 64 MiB across cached zips ) diff --git a/internal/application/service/tenant_skill_source.go b/internal/application/service/tenant_skill_source.go index caca05f9..ad1675f5 100644 --- a/internal/application/service/tenant_skill_source.go +++ b/internal/application/service/tenant_skill_source.go @@ -26,7 +26,7 @@ var ErrSkillSourceInvalid = errors.New("skill source is invalid") const ( defaultSkillRegistryOrigin = "https://clawhub.ai" skillSourceUserAgent = "WeKnora-SkillInstaller (+https://github.com/Tencent/WeKnora)" - skillSourceFetchTimeout = 60 * time.Second + skillSourceFetchTimeout = 5 * time.Minute skillSourceMaxHops = 3 ) @@ -630,10 +630,10 @@ func getSkillURL( } defer func() { _ = resp.Body.Close() }() - maxBytes := secutils.GetMaxFileSize() + maxBytes := secutils.GetMaxSkillBundleSize() if resp.ContentLength > maxBytes { return nil, "", fmt.Errorf("%w: skill bundle cannot exceed %d MB", - ErrSkillSourceInvalid, secutils.GetMaxFileSizeMB()) + ErrSkillSourceInvalid, secutils.GetMaxSkillBundleSizeMB()) } if resp.StatusCode < 200 || resp.StatusCode >= 300 { preview, _ := io.ReadAll(io.LimitReader(resp.Body, 512)) @@ -650,7 +650,7 @@ func getSkillURL( } if int64(len(body)) > maxBytes { return nil, "", fmt.Errorf("%w: skill bundle cannot exceed %d MB", - ErrSkillSourceInvalid, secutils.GetMaxFileSizeMB()) + ErrSkillSourceInvalid, secutils.GetMaxSkillBundleSizeMB()) } if len(body) == 0 { return nil, "", fmt.Errorf("%w: remote returned an empty body", ErrSkillSourceInvalid) diff --git a/internal/application/service/tenant_skill_source_test.go b/internal/application/service/tenant_skill_source_test.go index ba4059e9..c415b996 100644 --- a/internal/application/service/tenant_skill_source_test.go +++ b/internal/application/service/tenant_skill_source_test.go @@ -2,6 +2,7 @@ package service import ( "encoding/json" + "fmt" "net/http" "net/http/httptest" "net/url" @@ -401,3 +402,20 @@ func TestFetchSkillArchiveRejectsNonSkillHTML(t *testing.T) { require.True(t, strings.Contains(err.Error(), "skill archive") || strings.Contains(err.Error(), "zip skill bundle")) } + +func TestFetchSkillArchiveRejectsOversizeBody(t *testing.T) { + t.Setenv("MAX_FILE_SIZE_MB", "1") + t.Setenv("MAX_SKILL_BUNDLE_SIZE_MB", "1") + payload := strings.Repeat("z", 2<<20) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "application/zip") + w.Header().Set("Content-Length", fmt.Sprintf("%d", len(payload))) + _, _ = w.Write([]byte(payload)) + })) + t.Cleanup(server.Close) + allowLoopbackSkillFetch(t) + + _, err := fetchSkillArchive(t.Context(), server.URL+"/demo.zip", server.Client()) + require.ErrorIs(t, err, ErrSkillSourceInvalid) + require.ErrorContains(t, err, "1 MB") +} diff --git a/internal/handler/sandbox_skill.go b/internal/handler/sandbox_skill.go index d00b6bd0..a80cf663 100644 --- a/internal/handler/sandbox_skill.go +++ b/internal/handler/sandbox_skill.go @@ -299,7 +299,7 @@ func (h *SandboxSkillHandler) GetFile(c *gin.Context) { // @Security ApiKeyAuth // @Router /sandbox-configs/{id}/skills [post] func (h *SandboxSkillHandler) Upload(c *gin.Context) { - maxBytes := secutils.GetMaxFileSize() + maxBytes := secutils.GetMaxSkillBundleSize() // The cap is applied to the body before it is parsed: multipart parsing // buffers the whole request, spilling to temp files, so checking only the // declared part size would let an unbounded body through first. @@ -409,7 +409,7 @@ func (h *SandboxSkillHandler) Reinstall(c *gin.Context) { func skillTooLargeError() error { return apperrors.NewBadRequestError( - fmt.Sprintf("skill bundle cannot exceed %d MB", secutils.GetMaxFileSizeMB())) + fmt.Sprintf("skill bundle cannot exceed %d MB", secutils.GetMaxSkillBundleSizeMB())) } type skillPatchRequest struct { diff --git a/internal/handler/sandbox_skill_test.go b/internal/handler/sandbox_skill_test.go index 34b8b8f1..67586c52 100644 --- a/internal/handler/sandbox_skill_test.go +++ b/internal/handler/sandbox_skill_test.go @@ -304,6 +304,7 @@ func skillUploadRequest(t *testing.T, configID string, archive []byte) *http.Req // layer, before the whole body is buffered for the parser. func TestSandboxSkillUploadOverLimitReturns400(t *testing.T) { t.Setenv("MAX_FILE_SIZE_MB", "1") + t.Setenv("MAX_SKILL_BUNDLE_SIZE_MB", "1") svc := &fakeSandboxSkillService{installID: "skill-1"} router := newSkillTestRouter(NewSandboxSkillHandler(svc, nil)) diff --git a/internal/handler/skill_catalog.go b/internal/handler/skill_catalog.go index 333947aa..3aa058be 100644 --- a/internal/handler/skill_catalog.go +++ b/internal/handler/skill_catalog.go @@ -50,7 +50,7 @@ func (h *SkillHandler) RegisterCatalog(c *gin.Context) { _ = c.Error(apperrors.NewInternalServerError("skill catalog is not configured")) return } - maxBytes := secutils.GetMaxFileSize() + maxBytes := secutils.GetMaxSkillBundleSize() c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxBytes+skillUploadEnvelopeSlack) if strings.HasPrefix(c.ContentType(), "application/json") { diff --git a/internal/utils/filesize.go b/internal/utils/filesize.go index 4c92a60d..0fadb5b2 100644 --- a/internal/utils/filesize.go +++ b/internal/utils/filesize.go @@ -5,6 +5,14 @@ import ( "strconv" ) +const ( + defaultMaxFileSizeMB = 50 + defaultMaxSkillBundleSizeMB = 256 + // maxSkillBundleSizeMBCeiling matches the install-time uncompressed + // archive cap: a download larger than that cannot become a valid skill. + maxSkillBundleSizeMBCeiling = 512 +) + // GetMaxFileSize returns the maximum file upload size in bytes. // Default is 50MB, can be configured via MAX_FILE_SIZE_MB environment variable. // @@ -22,21 +30,42 @@ import ( // in lockstep without container restarts, every call site must read // the env directly via this helper. func GetMaxFileSize() int64 { - if sizeStr := os.Getenv("MAX_FILE_SIZE_MB"); sizeStr != "" { - if size, err := strconv.ParseInt(sizeStr, 10, 64); err == nil && size > 0 { - return size * 1024 * 1024 - } - } - return 50 * 1024 * 1024 // default 50MB + return GetMaxFileSizeMB() * 1024 * 1024 } // GetMaxFileSizeMB returns the maximum file upload size in MB. Same // caveat as GetMaxFileSize — handlers should prefer SystemSettingService.GetInt. func GetMaxFileSizeMB() int64 { - if sizeStr := os.Getenv("MAX_FILE_SIZE_MB"); sizeStr != "" { + return envSizeMB("MAX_FILE_SIZE_MB", defaultMaxFileSizeMB) +} + +// GetMaxSkillBundleSize is the compressed zip / source-download cap for +// skills. Knowledge uploads stay on GetMaxFileSize: packages such as +// ppt-master exceed 50MB, but raising the document limit would also +// inflate docreader gRPC messages. +func GetMaxSkillBundleSize() int64 { + return GetMaxSkillBundleSizeMB() * 1024 * 1024 +} + +// GetMaxSkillBundleSizeMB returns the skill zip cap in MB. +// MAX_SKILL_BUNDLE_SIZE_MB defaults to 256, is never below MAX_FILE_SIZE_MB, +// and is capped at 512. +func GetMaxSkillBundleSizeMB() int64 { + skillMB := envSizeMB("MAX_SKILL_BUNDLE_SIZE_MB", defaultMaxSkillBundleSizeMB) + if fileMB := GetMaxFileSizeMB(); skillMB < fileMB { + skillMB = fileMB + } + if skillMB > maxSkillBundleSizeMBCeiling { + return maxSkillBundleSizeMBCeiling + } + return skillMB +} + +func envSizeMB(key string, fallback int64) int64 { + if sizeStr := os.Getenv(key); sizeStr != "" { if size, err := strconv.ParseInt(sizeStr, 10, 64); err == nil && size > 0 { return size } } - return 50 // default 50MB + return fallback } diff --git a/internal/utils/filesize_test.go b/internal/utils/filesize_test.go new file mode 100644 index 00000000..e3c4fbc2 --- /dev/null +++ b/internal/utils/filesize_test.go @@ -0,0 +1,40 @@ +package utils + +import "testing" + +func TestGetMaxSkillBundleSizeMB(t *testing.T) { + t.Run("defaults above the knowledge upload cap", func(t *testing.T) { + t.Setenv("MAX_FILE_SIZE_MB", "") + t.Setenv("MAX_SKILL_BUNDLE_SIZE_MB", "") + if got := GetMaxSkillBundleSizeMB(); got != defaultMaxSkillBundleSizeMB { + t.Fatalf("GetMaxSkillBundleSizeMB() = %d, want %d", got, defaultMaxSkillBundleSizeMB) + } + if GetMaxFileSizeMB() != defaultMaxFileSizeMB { + t.Fatalf("knowledge cap must stay %d MB", defaultMaxFileSizeMB) + } + }) + + t.Run("honours an explicit skill cap above the knowledge cap", func(t *testing.T) { + t.Setenv("MAX_FILE_SIZE_MB", "50") + t.Setenv("MAX_SKILL_BUNDLE_SIZE_MB", "300") + if got := GetMaxSkillBundleSizeMB(); got != 300 { + t.Fatalf("GetMaxSkillBundleSizeMB() = %d, want 300", got) + } + }) + + t.Run("never below the knowledge upload cap", func(t *testing.T) { + t.Setenv("MAX_FILE_SIZE_MB", "200") + t.Setenv("MAX_SKILL_BUNDLE_SIZE_MB", "64") + if got := GetMaxSkillBundleSizeMB(); got != 200 { + t.Fatalf("GetMaxSkillBundleSizeMB() = %d, want 200", got) + } + }) + + t.Run("caps at the uncompressed archive ceiling", func(t *testing.T) { + t.Setenv("MAX_FILE_SIZE_MB", "50") + t.Setenv("MAX_SKILL_BUNDLE_SIZE_MB", "4096") + if got := GetMaxSkillBundleSizeMB(); got != maxSkillBundleSizeMBCeiling { + t.Fatalf("GetMaxSkillBundleSizeMB() = %d, want %d", got, maxSkillBundleSizeMBCeiling) + } + }) +}