Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

feat(handler): validate bucket name format and improve MinIO error handling

wizardchena78182cfc757
diff --git a/internal/handler/system.go b/internal/handler/system.go
index f067f407c..1af6391d2 100644
--- a/internal/handler/system.go
+++ b/internal/handler/system.go
@@ -824,6 +824,11 @@ func (h *SystemHandler) checkMinio(c *gin.Context, ctx context.Context, cfg *typ
return
}
+ if cfg.BucketName != "" && !cosFieldPattern.MatchString(cfg.BucketName) {
+ c.JSON(200, gin.H{"code": 0, "data": StorageCheckResponse{OK: false, Message: "Bucket 名称格式不正确,仅允许字母、数字、点、连字符"}})
+ return
+ }
+
endpoint, accessKeyID, secretAccessKey := cfg.Endpoint, cfg.AccessKeyID, cfg.SecretAccessKey
if cfg.Mode != "remote" {
endpoint = os.Getenv("MINIO_ENDPOINT")
@@ -846,7 +851,7 @@ func (h *SystemHandler) checkMinio(c *gin.Context, ctx context.Context, cfg *typ
err := file.CheckMinioConnectivity(ctx, endpoint, accessKeyID, secretAccessKey, cfg.BucketName, cfg.UseSSL)
if err != nil {
errMsg := err.Error()
- // If bucket does not exist, auto-create it with public-read policy
+ // If bucket does not exist, auto-create it
if strings.Contains(errMsg, "does not exist") && cfg.BucketName != "" {
logger.Info(ctx, "Storage check: bucket does not exist, attempting auto-creation", "bucket", cfg.BucketName)
minioClient, clientErr := minio.New(endpoint, &minio.Options{
@@ -854,39 +859,16 @@ func (h *SystemHandler) checkMinio(c *gin.Context, ctx context.Context, cfg *typ
Secure: cfg.UseSSL,
})
if clientErr != nil {
- c.JSON(200, gin.H{"code": 0, "data": StorageCheckResponse{OK: false, Message: fmt.Sprintf("创建 MinIO 客户端失败: %s", sanitizeStorageCheckError(clientErr))}})
+ c.JSON(200, gin.H{"code": 0, "data": StorageCheckResponse{OK: false, Message: fmt.Sprintf("Failed to create MinIO client: %s", sanitizeStorageCheckError(clientErr))}})
return
}
if mkErr := minioClient.MakeBucket(ctx, cfg.BucketName, minio.MakeBucketOptions{}); mkErr != nil {
logger.Error(ctx, "Storage check: failed to create bucket", "bucket", cfg.BucketName, "error", mkErr)
- c.JSON(200, gin.H{"code": 0, "data": StorageCheckResponse{OK: false, Message: fmt.Sprintf("自动创建 Bucket「%s」失败: %s", cfg.BucketName, sanitizeStorageCheckError(mkErr))}})
- return
- }
- // Set public-read policy
- publicReadPolicy := fmt.Sprintf(`{
- "Version": "2012-10-17",
- "Statement": [
- {
- "Effect": "Allow",
- "Principal": {"AWS": ["*"]},
- "Action": ["s3:GetBucketLocation", "s3:ListBucket"],
- "Resource": ["arn:aws:s3:::%s"]
- },
- {
- "Effect": "Allow",
- "Principal": {"AWS": ["*"]},
- "Action": ["s3:GetObject"],
- "Resource": ["arn:aws:s3:::%s/*"]
- }
- ]
- }`, cfg.BucketName, cfg.BucketName)
- if policyErr := minioClient.SetBucketPolicy(ctx, cfg.BucketName, publicReadPolicy); policyErr != nil {
- logger.Error(ctx, "Storage check: bucket created but failed to set public-read policy", "bucket", cfg.BucketName, "error", policyErr)
- c.JSON(200, gin.H{"code": 0, "data": StorageCheckResponse{OK: true, BucketCreated: true, Message: fmt.Sprintf("Bucket「%s」已自动创建,但设置公有读策略失败,请手动配置权限", cfg.BucketName)}})
+ c.JSON(200, gin.H{"code": 0, "data": StorageCheckResponse{OK: false, Message: fmt.Sprintf("Failed to auto-create Bucket '%s': %s", cfg.BucketName, sanitizeStorageCheckError(mkErr))}})
return
}
- logger.Info(ctx, "Storage check: bucket created with public-read policy", "bucket", cfg.BucketName)
- c.JSON(200, gin.H{"code": 0, "data": StorageCheckResponse{OK: true, BucketCreated: true, Message: fmt.Sprintf("Bucket「%s」不存在,已自动创建并设置公有读权限", cfg.BucketName)}})
+ logger.Info(ctx, "Storage check: bucket created", "bucket", cfg.BucketName)
+ c.JSON(200, gin.H{"code": 0, "data": StorageCheckResponse{OK: true, BucketCreated: true, Message: fmt.Sprintf("Bucket '%s' does not exist, and has been automatically created", cfg.BucketName)}})
return
}
logger.Error(ctx, "Storage check: MinIO connectivity failed", "error", err)