fix(tenant): 封堵审计文件与历史用户跨站访问

This commit is contained in:
yml
2026-11-15 22:46:58 +08:00
parent 0cb385d84f
commit 1108df195d
28 changed files with 531 additions and 60 deletions
+34 -10
View File
@@ -14,24 +14,33 @@ import (
)
type Handler struct {
service *Service
storage *Storage
objectAuthorizer ObjectAuthorizer
uploadOwnerRecorder UploadOwnerRecorder
service *Service
storage *Storage
objectAuthorizer ObjectAuthorizer
adminObjectAuthorizer AdminObjectAuthorizer
uploadOwnerRecorder UploadOwnerRecorder
}
// ObjectAuthorizer 校验用户是否可读取指定私有对象。
type ObjectAuthorizer func(ctx context.Context, userID uint64, key string) (bool, error)
// AdminObjectAuthorizer 校验管理员是否可在当前经营站点读取指定私有对象。
type AdminObjectAuthorizer func(ctx context.Context, adminID uint64, key string) (bool, error)
// UploadOwnerRecorder 保存用户上传私有文件的归属。
type UploadOwnerRecorder func(ctx context.Context, userID uint64, objectKeys []string) error
func NewHandler(service *Service, storage *Storage, objectAuthorizer ObjectAuthorizer, uploadOwnerRecorder UploadOwnerRecorder) *Handler {
func NewHandler(service *Service, storage *Storage, objectAuthorizer ObjectAuthorizer, uploadOwnerRecorder UploadOwnerRecorder, adminObjectAuthorizers ...AdminObjectAuthorizer) *Handler {
var adminObjectAuthorizer AdminObjectAuthorizer
if len(adminObjectAuthorizers) > 0 {
adminObjectAuthorizer = adminObjectAuthorizers[0]
}
return &Handler{
service: service,
storage: storage,
objectAuthorizer: objectAuthorizer,
uploadOwnerRecorder: uploadOwnerRecorder,
service: service,
storage: storage,
objectAuthorizer: objectAuthorizer,
adminObjectAuthorizer: adminObjectAuthorizer,
uploadOwnerRecorder: uploadOwnerRecorder,
}
}
@@ -99,7 +108,22 @@ func (h *Handler) writeObject(c *gin.Context, publicOnly bool) {
return
}
if !publicOnly {
if _, isAdmin := c.Get("admin_id"); !isAdmin {
if adminIDValue, isAdmin := c.Get("admin_id"); isAdmin {
adminID, ok := adminIDValue.(uint64)
if !ok || h.adminObjectAuthorizer == nil {
response.Error(c, http.StatusForbidden, "forbidden", "无权访问该文件")
return
}
allowed, err := h.adminObjectAuthorizer(c.Request.Context(), adminID, key)
if err != nil {
response.ServiceUnavailable(c, "文件权限校验失败")
return
}
if !allowed {
response.Error(c, http.StatusForbidden, "forbidden", "无权访问该文件")
return
}
} else {
userID, ok := c.Get("user_id")
if !ok {
response.Unauthorized(c, "缺少用户上下文")