Skip to content

Commit 9469c95

Browse files
authored
fix(security): potential XSS vulnerabilities (#896)
1 parent cf912dc commit 9469c95

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

internal/op/storage.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package op
33
import (
44
"context"
55
"fmt"
6+
"reflect"
67
"runtime"
78
"sort"
89
"strings"
@@ -135,7 +136,11 @@ func initStorage(ctx context.Context, storage model.Storage, storageDriver drive
135136
}
136137
storagesMap.Store(driverStorage.MountPath, storageDriver)
137138
if err != nil {
138-
driverStorage.SetStatus(err.Error())
139+
if IsUseOnlineAPI(storageDriver) {
140+
driverStorage.SetStatus(utils.SanitizeHTML(err.Error()))
141+
} else {
142+
driverStorage.SetStatus(err.Error())
143+
}
139144
err = errors.Wrap(err, "failed init storage")
140145
} else {
141146
driverStorage.SetStatus(WORK)
@@ -144,6 +149,24 @@ func initStorage(ctx context.Context, storage model.Storage, storageDriver drive
144149
return err
145150
}
146151

152+
func IsUseOnlineAPI(storageDriver driver.Driver) bool {
153+
v := reflect.ValueOf(storageDriver.GetAddition())
154+
if v.Kind() == reflect.Ptr {
155+
v = v.Elem()
156+
}
157+
if !v.IsValid() || v.Kind() != reflect.Struct {
158+
return false
159+
}
160+
field_v := v.FieldByName("UseOnlineAPI")
161+
if !field_v.IsValid() {
162+
return false
163+
}
164+
if field_v.Kind() != reflect.Bool {
165+
return false
166+
}
167+
return field_v.Bool()
168+
}
169+
147170
func EnableStorage(ctx context.Context, id uint) error {
148171
storage, err := db.GetStorageById(id)
149172
if err != nil {

pkg/utils/html.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package utils
2+
3+
import "github.com/microcosm-cc/bluemonday"
4+
5+
var htmlSanitizePolicy = bluemonday.StrictPolicy()
6+
7+
func SanitizeHTML(s string) string {
8+
return htmlSanitizePolicy.Sanitize(s)
9+
}

0 commit comments

Comments
 (0)