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.
main
wizardchen 2026-09-01 20:34:57 +08:00 committed by lyingbug
parent 4d66d57fe7
commit 56bf53a1fd
23 changed files with 259 additions and 44 deletions

View File

@ -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)-----

View File

@ -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,须小于上一项)

View File

@ -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}

View File

@ -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)

View File

@ -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/)

View File

@ -17,7 +17,7 @@
<section v-if="mode === 'install'" class="setting-drawer__section">
<h4 class="setting-drawer__section-title">{{ $t('settings.sandbox.skillSourceSection') }}</h4>
<p class="installer-model-hint">{{ $t('settings.sandbox.skillSourceSectionHint', { size: MAX_FILE_SIZE_MB }) }}</p>
<p class="installer-model-hint">{{ $t('settings.sandbox.skillSourceSectionHint', { size: MAX_SKILL_BUNDLE_SIZE_MB }) }}</p>
<t-input-adornment class="skill-source-row">
<t-input
v-model="sourceInput"
@ -40,7 +40,7 @@
<section v-if="mode === 'install'" class="setting-drawer__section">
<h4 class="setting-drawer__section-title">{{ $t('settings.sandbox.skillUploadSection') }}</h4>
<p class="installer-model-hint">{{ $t('settings.sandbox.skillUploadSectionHint', { size: MAX_FILE_SIZE_MB }) }}</p>
<p class="installer-model-hint">{{ $t('settings.sandbox.skillUploadSectionHint', { size: MAX_SKILL_BUNDLE_SIZE_MB }) }}</p>
<input
ref="fileInputRef"
type="file"
@ -576,7 +576,7 @@ import {
type SandboxConfigRecord,
} from '@/api/system'
import { getApiBaseUrl } from '@/utils/api-base'
import { generateRandomString, MAX_FILE_SIZE_BYTES, MAX_FILE_SIZE_MB } from '@/utils/index'
import { generateRandomString, MAX_SKILL_BUNDLE_SIZE_BYTES, MAX_SKILL_BUNDLE_SIZE_MB } from '@/utils/index'
import i18n from '@/i18n'
import {
MAX_ENV_VALUE_BYTES,
@ -699,10 +699,13 @@ function statusLabel(skill: ConfigSkill): string {
}
function isBusy(skill: ConfigSkill): boolean {
return skill.status === 'installing' || skill.status === 'removing'
return skill.status === 'installing' || isRemoving(skill)
}
function isRemoving(skill: ConfigSkill): boolean {
if (uninstallingId.value === skill.id && !uninstallDone.value) {
if (progressById.value[skill.id]?.stage !== 'failed') return true
}
return skill.status === 'removing' || deletingId.value === skill.id
}
@ -1113,6 +1116,19 @@ function skillsSignature(list: ConfigSkill[]): string {
return list.map((skill) => `${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 = ''

View File

@ -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',

View File

@ -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',

View File

@ -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',

View File

@ -5256,6 +5256,7 @@ export default {
skillUploadFailed: '技能上传失败',
skillBundleTooLarge: '技能压缩包不能超过 {size} MB。',
skillBundleTooManyFiles: '技能目录里的文件不能超过 {count} 个。',
skillBundleTooManyZipEntries: '压缩包条目不能超过 {count} 个。',
skillUploading: '正在上传 {percent}%',
skillUploadAccepted: '已开始安装技能',
skillStatusInstalling: '安装中',

View File

@ -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 = "";

View File

@ -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/)

View File

@ -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

View File

@ -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)
}
}

View File

@ -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) {

View File

@ -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
)

View File

@ -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)

View File

@ -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")
}

View File

@ -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 {

View File

@ -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))

View File

@ -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") {

View File

@ -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
}

View File

@ -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)
}
})
}