console: an SVG logo's url() target allows one opening quote
svgFuncsOK trimmed every leading quote, so url("'#a") passed: the
second quote is the target's first character, not a fragment. The
target is now read past whitespace and at most one ' or ", then must
start with "#".
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+31
−2
internal/console/oplegal.go+13 −2internal/console/oplegal_test.go+18 −0
Intent
diff --git a/internal/console/oplegal.go b/internal/console/oplegal.go| index f688b3ff3..8374b26a4 100644 |
| --- a/internal/console/oplegal.go |
| +++ b/internal/console/oplegal.go |
| @@ -741,7 +741,7 @@ var svgFuncs = map[string]bool{ |
| // svgFuncsOK is true when v holds no CSS comment and every function call in |
| // it (an identifier straight before "(", ignoring case) is in svgFuncs, with |
| -// each url( target, past whitespace and quotes, a fragment reference. |
| +// each url( target a fragment reference (svgURLTargetOK). |
| func svgFuncsOK(v string) bool { |
| v = strings.ToLower(v) |
| if strings.Contains(v, "/*") { |
| @@ -762,13 +762,24 @@ func svgFuncsOK(v string) bool { |
| if !svgFuncs[name] { |
| return false |
| } |
| - if name == "url" && !strings.HasPrefix(strings.TrimLeft(v[i+1:], " \t\r\n\f'\""), "#") { |
| + if name == "url" && !svgURLTargetOK(v[i+1:]) { |
| return false |
| } |
| } |
| return true |
| } |
| +// svgURLTargetOK is true when rest, what follows "url(", is a fragment |
| +// reference: past whitespace and at most one opening quote, a "#". A second |
| +// quote is the target's first character, so "url(\"'#a\")" is not one. |
| +func svgURLTargetOK(rest string) bool { |
| + rest = strings.TrimLeft(rest, " \t\r\n\f") |
| + if rest != "" && (rest[0] == '\'' || rest[0] == '"') { |
| + rest = rest[1:] |
| + } |
| + return strings.HasPrefix(rest, "#") |
| +} |
| + |
| // svgIdentByte is a byte a CSS function name may hold. Anything not ASCII |
| // counts too, so a name cannot hide behind a non-ASCII letter. |
| func svgIdentByte(c byte) bool { |
diff --git a/internal/console/oplegal_test.go b/internal/console/oplegal_test.go| index b88aeb0ef..a87999edb 100644 |
| --- a/internal/console/oplegal_test.go |
| +++ b/internal/console/oplegal_test.go |
| @@ -518,6 +518,11 @@ func TestOpLegalLogoCheck(t *testing.T) { |
| `<svg ` + ns + ` xmlns:xlink="http://www.w3.org/1999/xlink"><use xlink:href="#p"/><rect fill="url( '#g' )"/></svg>`, |
| `<svg ` + ns + `><g transform="translate(10 20) rotate(45)"><rect fill="rgb(1,2,3)" stroke="url(#grad)" aria-label="Shop (Ltd)"/></g></svg>`, |
| `<svg ` + ns + `><rect transform="matrix(1 0 0 1 0 0) scale(2) skewX(3) skewY(4)" fill="hsla(1,2%,3%,0.5)" stroke="RGBA(0,0,0,1)"/></svg>`, |
| + // A url target: past whitespace, at most one opening quote, then "#". |
| + `<svg ` + ns + `><rect mask="url(#a)"/></svg>`, |
| + `<svg ` + ns + `><rect mask="url('#a')"/></svg>`, |
| + `<svg ` + ns + `><rect mask='url("#a")'/></svg>`, |
| + `<svg ` + ns + `><rect mask="url( #a)"/></svg>`, |
| } |
| for _, s := range good { |
| if got := letterheadLogoType([]byte(s)); got != "image/svg+xml" { |
| @@ -568,12 +573,25 @@ func TestOpLegalLogoCheck(t *testing.T) { |
| `<svg ` + ns + `><rect fill="u/**/rl(x)"/></svg>`, |
| `<svg ` + ns + `><rect fill="image('https://e.com/x.png')"/></svg>`, |
| `<svg ` + ns + `><rect fill="URL(https://e.com/x)"/></svg>`, |
| + // Two quotes of different kinds before the "#": the second is the |
| + // target's first character, not a fragment. |
| + `<svg ` + ns + `><rect mask="url("'#a")"/></svg>`, |
| + `<svg ` + ns + `><rect mask="url('"#a')"/></svg>`, |
| } |
| for _, s := range bad { |
| if got := letterheadLogoType([]byte(s)); got != "" { |
| t.Errorf("accepted %q as %s", s, got) |
| } |
| } |
| + // The url target rule on attribute values as the decoder hands them over. |
| + for v, want := range map[string]bool{ |
| + `url(#a)`: true, `url('#a')`: true, `url("#a")`: true, `url( #a)`: true, `url( "#a" )`: true, |
| + `url("'#a")`: false, `url('"#a')`: false, `url(''#a')`: false, `url(' #a')`: false, `url(a#b)`: false, |
| + } { |
| + if got := svgFuncsOK(v); got != want { |
| + t.Errorf("svgFuncsOK(%q) = %v, want %v", v, got, want) |
| + } |
| + } |
| png := oplPNG(t) |
| if got := letterheadLogoType(png); got != "image/png" { |
| t.Errorf("png = %q", got) |