refactor(storage): 优化OSS存储初始化与URL格式,提升安全性和兼容性
- 添加判断oss2是否安装,未安装时打印错误日志 - 修改OSS桶不存在时不自动创建,避免误创建公共读桶 - OSS下载URL改为虚拟主机风格,提升兼容性 - OSS初始化错误、缺失配置时添加详细日志提示 - 其他存储类代码重排统一风格,提升可维护性
This commit is contained in:
@@ -629,6 +629,9 @@ func hasGetObjectAction(action interface{}) bool {
|
||||
// cosFieldPattern validates COS region and bucket name format to prevent URL injection.
|
||||
var cosFieldPattern = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._-]{0,62}$`)
|
||||
|
||||
// ossFieldPattern validates OSS region and bucket name format to prevent URL injection.
|
||||
var ossFieldPattern = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._-]{0,62}$`)
|
||||
|
||||
// sanitizeStorageCheckError converts a raw storage connectivity error into a safe
|
||||
// user-facing message that does not leak internal network details (hostnames, IPs, ports).
|
||||
func sanitizeStorageCheckError(err error) string {
|
||||
@@ -964,11 +967,21 @@ func (h *SystemHandler) checkOSS(c *gin.Context, ctx context.Context, cfg *types
|
||||
return
|
||||
}
|
||||
|
||||
if blocked, reason := isBlockedStorageEndpoint(endpoint); blocked {
|
||||
// Strip URL scheme before SSRF check — OSS endpoint may include http:// or https://
|
||||
ssrfEndpoint := strings.TrimPrefix(strings.TrimPrefix(endpoint, "https://"), "http://")
|
||||
if blocked, reason := isBlockedStorageEndpoint(ssrfEndpoint); blocked {
|
||||
logger.Warnf(ctx, "Storage check: OSS endpoint blocked by SSRF protection", "endpoint", endpoint)
|
||||
c.JSON(200, gin.H{"code": 0, "data": StorageCheckResponse{OK: false, Message: reason}})
|
||||
return
|
||||
}
|
||||
if !ossFieldPattern.MatchString(cfg.Region) {
|
||||
c.JSON(200, gin.H{"code": 0, "data": StorageCheckResponse{OK: false, Message: "Region 格式不正确,仅允许字母、数字、点、连字符"}})
|
||||
return
|
||||
}
|
||||
if !ossFieldPattern.MatchString(cfg.BucketName) {
|
||||
c.JSON(200, gin.H{"code": 0, "data": StorageCheckResponse{OK: false, Message: "Bucket 名称格式不正确,仅允许字母、数字、点、连字符"}})
|
||||
return
|
||||
}
|
||||
|
||||
err := file.CheckOssConnectivity(ctx, endpoint, cfg.Region, accessKey, secretKey, cfg.BucketName)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user