Changeset 21 in code for trunk/morty.go


Ignore:
Timestamp:
Oct 30, 2016, 12:24:56 PM (9 years ago)
Author:
asciimoo
Message:

[fix] attribute escaping - better solution in the future: https://github.com/golang/go/issues/17667

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/morty.go

    r18 r21  
    2929
    3030var CLIENT *fasthttp.Client = &fasthttp.Client{
     31        Dial:                fasthttp.DialDualStack,
    3132        MaxResponseBodySize: 10 * 1024 * 1024, // 10M
    3233}
     
    343344                                        for {
    344345                                                attrName, attrValue, moreAttr := decoder.TagAttr()
    345                                                 attrs = append(attrs, [][]byte{attrName, attrValue})
     346                                                attrs = append(attrs, [][]byte{
     347                                                        attrName,
     348                                                        attrValue,
     349                                                        []byte(html.EscapeString(string(attrValue))),
     350                                                })
    346351                                                if !moreAttr {
    347352                                                        break
     
    464469                out.Write([]byte("<link"))
    465470                for _, attr := range attrs {
    466                         sanitizeAttr(rc, out, attr[0], attr[1])
     471                        sanitizeAttr(rc, out, attr[0], attr[1], attr[2])
    467472                }
    468473                out.Write([]byte(">"))
     
    499504func sanitizeAttrs(rc *RequestConfig, out io.Writer, attrs [][][]byte) {
    500505        for _, attr := range attrs {
    501                 sanitizeAttr(rc, out, attr[0], attr[1])
    502         }
    503 }
    504 
    505 func sanitizeAttr(rc *RequestConfig, out io.Writer, attrName, attrValue []byte) {
     506                sanitizeAttr(rc, out, attr[0], attr[1], attr[2])
     507        }
     508}
     509
     510func sanitizeAttr(rc *RequestConfig, out io.Writer, attrName, attrValue, escapedAttrValue []byte) {
    506511        if inArray(attrName, SAFE_ATTRIBUTES) {
    507                 fmt.Fprintf(out, " %s=\"%s\"", attrName, attrValue)
     512                fmt.Fprintf(out, " %s=\"%s\"", attrName, escapedAttrValue)
    508513                return
    509514        }
     
    516521                }
    517522        case "style":
    518                 fmt.Fprintf(out, " %s=\"", attrName)
    519                 sanitizeCSS(rc, out, attrValue)
    520                 out.Write([]byte("\""))
     523                cssAttr := bytes.NewBuffer(nil)
     524                sanitizeCSS(rc, cssAttr, attrValue)
     525                fmt.Fprintf(out, " %s=\"%s\"", attrName, html.EscapeString(string(cssAttr.Bytes())))
    521526        }
    522527}
Note: See TracChangeset for help on using the changeset viewer.