Skip to content

Commit 341da79

Browse files
authored
Merge pull request #4 from Josgu/main
fix: 防止获取标签属性panic
2 parents a496a53 + c30ca01 commit 341da79

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

xss.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type AttrResult struct {
6262
Closing bool
6363
}
6464

65+
// GetAttrs 获取标签属性
6566
func GetAttrs(html string) AttrResult {
6667
i := spaceIndex(html)
6768
if i == -1 {
@@ -72,7 +73,10 @@ func GetAttrs(html string) AttrResult {
7273
}
7374
html = strings.TrimSpace(html[i+1 : len(html)-1])
7475

75-
isClosing := html[len(html)-1] == '/'
76+
var isClosing = false
77+
if len(html) > 0 {
78+
isClosing = html[len(html)-1] == '/'
79+
}
7680

7781
if isClosing {
7882
html = strings.TrimSpace(html[0 : len(html)-1])

xss_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,17 @@ func TestOnIgnoreTagAttr(t *testing.T) {
13291329
}
13301330
}
13311331

1332+
// TestOnIgnoreWhiteListTagNullAttr 测试忽略tag 空属性
1333+
func TestOnIgnoreWhiteListTagNullAttr(t *testing.T) {
1334+
source := "<a >bb</a>"
1335+
1336+
html := FilterXSS(source, XssOption{WhiteList: GetDefaultWhiteList()})
1337+
1338+
if html != "<a>bb</a>" {
1339+
t.Errorf("TestOnIgnoreWhiteListTagNullAttr error")
1340+
}
1341+
}
1342+
13321343
func TestOnIgnoreTagAttrWithReturn(t *testing.T) {
13331344
source := "<a href=\"#\" target=\"_blank\" checked data-a=\"b\">hi</a href=\"d\">"
13341345

0 commit comments

Comments
 (0)