fix(security): potential XSS vulnerabilities (#896)

This commit is contained in:
hshpy
2025-07-31 12:57:20 +08:00
committed by GitHub
parent cf912dcf7a
commit 9469c95b14
2 changed files with 33 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package op
import ( import (
"context" "context"
"fmt" "fmt"
"reflect"
"runtime" "runtime"
"sort" "sort"
"strings" "strings"
@ -135,7 +136,11 @@ func initStorage(ctx context.Context, storage model.Storage, storageDriver drive
} }
storagesMap.Store(driverStorage.MountPath, storageDriver) storagesMap.Store(driverStorage.MountPath, storageDriver)
if err != nil { if err != nil {
if IsUseOnlineAPI(storageDriver) {
driverStorage.SetStatus(utils.SanitizeHTML(err.Error()))
} else {
driverStorage.SetStatus(err.Error()) driverStorage.SetStatus(err.Error())
}
err = errors.Wrap(err, "failed init storage") err = errors.Wrap(err, "failed init storage")
} else { } else {
driverStorage.SetStatus(WORK) driverStorage.SetStatus(WORK)
@ -144,6 +149,24 @@ func initStorage(ctx context.Context, storage model.Storage, storageDriver drive
return err return err
} }
func IsUseOnlineAPI(storageDriver driver.Driver) bool {
v := reflect.ValueOf(storageDriver.GetAddition())
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if !v.IsValid() || v.Kind() != reflect.Struct {
return false
}
field_v := v.FieldByName("UseOnlineAPI")
if !field_v.IsValid() {
return false
}
if field_v.Kind() != reflect.Bool {
return false
}
return field_v.Bool()
}
func EnableStorage(ctx context.Context, id uint) error { func EnableStorage(ctx context.Context, id uint) error {
storage, err := db.GetStorageById(id) storage, err := db.GetStorageById(id)
if err != nil { if err != nil {

9
pkg/utils/html.go Normal file
View File

@ -0,0 +1,9 @@
package utils
import "github.com/microcosm-cc/bluemonday"
var htmlSanitizePolicy = bluemonday.StrictPolicy()
func SanitizeHTML(s string) string {
return htmlSanitizePolicy.Sanitize(s)
}