Skip to content

Commit

Permalink
feat: single-quoted attribute value syntax support
Browse files Browse the repository at this point in the history
  • Loading branch information
feiin committed Nov 23, 2024
1 parent af681ae commit a496a53
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
11 changes: 11 additions & 0 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,17 @@ func SafeAttrValue(tag, name, value string) string {
}
```

### Customize output attribute value syntax for HTML
By specifying a `SingleQuotedAttributeValue`. Use `true` for `'`. Otherwise default `"` will be used
```golang
options.SingleQuotedAttributeValue = true

// With the configuration specified above, the following HTML:
// <a href="#">Hello</a>
// would become:
// <a href='#'>Hello</a>
```



### Quick Start
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,20 @@ func SafeAttrValue(tag, name, value string) string {
}
```

### 自定义属性值引用为单引号

`SingleQuotedAttributeValue`为false时默认为双引号,为true时则为单引号

```golang
options.SingleQuotedAttributeValue = true

// 设置属性为true后,以下HTML
// <a href="#">Hello</a>
// 输出结果:
// <a href='#'>Hello</a>
```


### 自定义 CSS 过滤器

TODO
Expand Down
11 changes: 9 additions & 2 deletions xss.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package xss
import (
// "errors"
// "bytes"
"github.com/feiin/pkg/arrays"
"fmt"
"strings"

"github.com/feiin/pkg/arrays"
// "io"
)

Expand Down Expand Up @@ -102,6 +104,11 @@ func (x *Xss) Process(html string) string {
OnIgnoreTagAttr := x.options.OnIgnoreTagAttr
whiteList := x.options.WhiteList

attributeWrapSign := "\""
if x.options.SingleQuotedAttributeValue {
attributeWrapSign = "'"
}

//remove invisible characters
if x.options.StripBlankChar {
html = stripBlankChar(html)
Expand Down Expand Up @@ -164,7 +171,7 @@ func (x *Xss) Process(html string) string {
if isWhiteAttr {
value = safeAttrValue(tag, name, value)
if len(value) > 0 {
return name + "=\"" + value + "\""
return fmt.Sprintf("%s=%s%s%s", name, attributeWrapSign, value, attributeWrapSign) // name + "=\"" + value + "\""
} else {
return name
}
Expand Down
3 changes: 2 additions & 1 deletion xss_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ type XssOption struct {
// remove html comments
AllowCommentTag bool

StripIgnoreTag bool
StripIgnoreTag bool
SingleQuotedAttributeValue bool

// StripIgnoreTagBody
StripIgnoreTagBody []string
Expand Down
28 changes: 28 additions & 0 deletions xss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1506,3 +1506,31 @@ func TestOnTagSanitizeHtml(t *testing.T) {
t.Errorf("TestStripIngoreBodyTag4 error %s", html)
}
}

func TestSingleQuotedAttributeValue(t *testing.T) {
source := "<a title=\"xx\">single-quoted</a>"

html := FilterXSS(source, XssOption{SingleQuotedAttributeValue: false})

if html != "<a title=\"xx\">single-quoted</a>" {
t.Errorf("TestSingleQuotedAttributeValue expect: %s but:%s", source, html)

}

html = FilterXSS(source, XssOption{SingleQuotedAttributeValue: true})

expect := "<a title='xx'>single-quoted</a>"
if html != expect {
t.Errorf("TestSingleQuotedAttributeValue expect:%s but:%s", expect, html)

}

source = "<a title='xx'>single-quoted</a>"

html = FilterXSS(source, XssOption{SingleQuotedAttributeValue: false})
expect = "<a title=\"xx\">single-quoted</a>"
if html != expect {
t.Errorf("TestSingleQuotedAttributeValue expect: %s but:%s", expect, html)

}
}

0 comments on commit a496a53

Please sign in to comment.