Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow preceeding dash in css ident token #89

Merged
merged 1 commit into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion parser/v2/cssparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ var cssExpressionParser = parse.Func(func(pi *parse.Input) (r cssExpression, ok
})

// CSS property name parser.
var cssPropertyNameFirst = "abcdefghijklmnopqrstuvwxyz"
var cssPropertyNameFirst = "abcdefghijklmnopqrstuvwxyz-"
var cssPropertyNameSubsequent = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"
var cssPropertyNameParser = parse.Func(func(in *parse.Input) (name string, ok bool, err error) {
start := in.Position()
Expand Down
14 changes: 11 additions & 3 deletions parser/v2/cssparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestExpressionCSSPropertyParser(t *testing.T) {
var tests = []struct {
tests := []struct {
name string
input string
expected ExpressionCSSProperty
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestExpressionCSSPropertyParser(t *testing.T) {
}

func TestConstantCSSPropertyParser(t *testing.T) {
var tests = []struct {
tests := []struct {
name string
input string
expected ConstantCSSProperty
Expand All @@ -94,6 +94,14 @@ func TestConstantCSSPropertyParser(t *testing.T) {
Value: "#ffffff",
},
},
{
name: "css: single constant webkit property",
input: `-webkit-text-stroke-color: #ffffff;`,
expected: ConstantCSSProperty{
Name: "-webkit-text-stroke-color",
Value: "#ffffff",
},
},
}
for _, tt := range tests {
tt := tt
Expand All @@ -114,7 +122,7 @@ func TestConstantCSSPropertyParser(t *testing.T) {
}

func TestCSSParser(t *testing.T) {
var tests = []struct {
tests := []struct {
name string
input string
expected CSSTemplate
Expand Down
18 changes: 9 additions & 9 deletions safehtml/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func SanitizeCSS(property, value string) (string, string) {
// identifierPattern matches a subset of valid <ident-token> values defined in
// https://www.w3.org/TR/css-syntax-3/#ident-token-diagram. This pattern matches all generic family name
// keywords defined in https://drafts.csswg.org/css-fonts-3/#family-name-value.
var identifierPattern = regexp.MustCompile(`^[a-zA-Z][-a-zA-Z]+$`)
var identifierPattern = regexp.MustCompile(`^[-a-zA-Z]+$`)

var cssPropertyNameToValueSanitizer = map[string]func(string) string{
"background-image": sanitizeBackgroundImage,
Expand Down Expand Up @@ -124,14 +124,14 @@ const InnocuousPropertyValue = "zTemplUnsafeCSSPropertyValue"
// Specifically, it matches string where every '*' or '/' is followed by end-of-text or a safe rune
// (i.e. alphanumberics or runes in the set [+-.!#%_ \t]). This regex ensures that the following
// are disallowed:
// * "/*" and "*/", which are CSS comment markers.
// * "//", even though this is not a comment marker in the CSS specification. Disallowing
// this string minimizes the chance that browser peculiarities or parsing bugs will allow
// sanitization to be bypassed.
// * '(' and ')', which can be used to call functions.
// * ',', since it can be used to inject extra values into a property.
// * Runes which could be matched on CSS error recovery of a previously malformed token, such as '@'
// and ':'. See http://www.w3.org/TR/css3-syntax/#error-handling.
// - "/*" and "*/", which are CSS comment markers.
// - "//", even though this is not a comment marker in the CSS specification. Disallowing
// this string minimizes the chance that browser peculiarities or parsing bugs will allow
// sanitization to be bypassed.
// - '(' and ')', which can be used to call functions.
// - ',', since it can be used to inject extra values into a property.
// - Runes which could be matched on CSS error recovery of a previously malformed token, such as '@'
// and ':'. See http://www.w3.org/TR/css3-syntax/#error-handling.
var safeRegularPropertyValuePattern = regexp.MustCompile(`^(?:[*/]?(?:[0-9a-zA-Z+-.!#%_ \t]|$))*$`)

// safeEnumPropertyValuePattern matches strings that are safe to use as enumerated property values.
Expand Down
9 changes: 8 additions & 1 deletion safehtml/style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package safehtml
import "testing"

func TestSanitizeCSS(t *testing.T) {
var tests = []struct {
tests := []struct {
name string
inputProperty string
expectedProperty string
Expand Down Expand Up @@ -199,6 +199,13 @@ func TestSanitizeCSS(t *testing.T) {
inputValue: `url("http://safe.example.com/img.png"), url("https://safe.example.com/other.png")`,
expectedValue: `url("http://safe.example.com/img.png"), url("https://safe.example.com/other.png")`,
},
{
name: "-webkit-text-stroke-color safe webkit",
inputProperty: "-webkit-text-stroke-color",
expectedProperty: "-webkit-text-stroke-color",
inputValue: `#000`,
expectedValue: `#000`,
},
{
name: "escape attempt property name",
inputProperty: "</style><script>alert('hello')</script><style>",
Expand Down