Changeset 9 in code for trunk


Ignore:
Timestamp:
Oct 23, 2016, 5:30:02 PM (9 years ago)
Author:
asciimoo
Message:

[mod] use io.Writer in sanitizers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/morty.go

    r8 r9  
    241241}
    242242
    243 func sanitizeCSS(rc *RequestConfig, ctx *fasthttp.RequestCtx, css []byte) {
     243func sanitizeCSS(rc *RequestConfig, out io.Writer, css []byte) {
    244244        // TODO
    245245
     
    247247
    248248        if urlSlices == nil {
    249                 ctx.Write(css)
     249                out.Write(css)
    250250                return
    251251        }
     
    258258
    259259                if uri, err := proxifyURI(rc, string(css[urlStart:urlEnd])); err == nil {
    260                         ctx.Write(css[startIndex:urlStart])
    261                         ctx.Write([]byte(uri))
     260                        out.Write(css[startIndex:urlStart])
     261                        out.Write([]byte(uri))
    262262                        startIndex = urlEnd
    263263                } else {
     
    266266        }
    267267        if startIndex < len(css) {
    268                 ctx.Write(css[startIndex:len(css)])
    269         }
    270 }
    271 
    272 func sanitizeHTML(rc *RequestConfig, ctx *fasthttp.RequestCtx, htmlDoc []byte) {
     268                out.Write(css[startIndex:len(css)])
     269        }
     270}
     271
     272func sanitizeHTML(rc *RequestConfig, out io.Writer, htmlDoc []byte) {
    273273        r := bytes.NewReader(htmlDoc)
    274274        decoder := html.NewTokenizer(r)
     
    307307                                }
    308308                                var attrs [][][]byte
    309                                 fmt.Fprintf(ctx, "<%s", tag)
     309                                fmt.Fprintf(out, "<%s", tag)
    310310                                if hasAttrs {
    311311                                        for {
     
    317317                                        }
    318318                                        if bytes.Equal(tag, []byte("meta")) {
    319                                                 sanitizeMetaAttrs(rc, ctx, attrs)
     319                                                sanitizeMetaAttrs(rc, out, attrs)
    320320                                        } else {
    321                                                 sanitizeAttrs(rc, ctx, attrs)
     321                                                sanitizeAttrs(rc, out, attrs)
    322322                                        }
    323323                                }
    324324                                if token == html.SelfClosingTagToken {
    325                                         fmt.Fprintf(ctx, " />")
     325                                        fmt.Fprintf(out, " />")
    326326                                } else {
    327                                         fmt.Fprintf(ctx, ">")
     327                                        fmt.Fprintf(out, ">")
    328328                                        if bytes.Equal(tag, []byte("style")) {
    329329                                                state = STATE_IN_STYLE
     
    347347                                                key = hash(urlStr, rc.Key)
    348348                                        }
    349                                         fmt.Fprintf(ctx, HTML_FORM_EXTENSION, urlStr, key)
     349                                        fmt.Fprintf(out, HTML_FORM_EXTENSION, urlStr, key)
    350350
    351351                                }
     
    356356                                switch string(tag) {
    357357                                case "body":
    358                                         fmt.Fprintf(ctx, HTML_BODY_EXTENSION, rc.baseURL.String())
     358                                        fmt.Fprintf(out, HTML_BODY_EXTENSION, rc.baseURL.String())
    359359                                case "style":
    360360                                        state = STATE_DEFAULT
     
    365365                                // skip noscript tags - only the tag, not the content, because javascript is sanitized
    366366                                if writeEndTag {
    367                                         fmt.Fprintf(ctx, "</%s>", tag)
     367                                        fmt.Fprintf(out, "</%s>", tag)
    368368                                }
    369369
     
    371371                                switch state {
    372372                                case STATE_DEFAULT:
    373                                         fmt.Fprintf(ctx, "%s", decoder.Raw())
     373                                        fmt.Fprintf(out, "%s", decoder.Raw())
    374374                                case STATE_IN_STYLE:
    375                                         sanitizeCSS(rc, ctx, decoder.Raw())
     375                                        sanitizeCSS(rc, out, decoder.Raw())
    376376                                case STATE_IN_NOSCRIPT:
    377                                         sanitizeHTML(rc, ctx, decoder.Raw())
     377                                        sanitizeHTML(rc, out, decoder.Raw())
    378378                                }
    379379
    380380                        case html.DoctypeToken, html.CommentToken:
    381                                 ctx.Write(decoder.Raw())
     381                                out.Write(decoder.Raw())
    382382                        }
    383383                } else {
     
    399399}
    400400
    401 func sanitizeMetaAttrs(rc *RequestConfig, ctx *fasthttp.RequestCtx, attrs [][][]byte) {
     401func sanitizeMetaAttrs(rc *RequestConfig, out io.Writer, attrs [][][]byte) {
    402402        var http_equiv []byte
    403403        var content []byte
     
    417417                parts := bytes.SplitN(content, []byte(";url="), 2)
    418418                if uri, err := proxifyURI(rc, string(parts[1])); err == nil {
    419                         fmt.Fprintf(ctx, ` http-equiv="refresh" content="%s;%s"`, parts[0], uri)
     419                        fmt.Fprintf(out, ` http-equiv="refresh" content="%s;%s"`, parts[0], uri)
    420420                }
    421421        } else {
    422                 sanitizeAttrs(rc, ctx, attrs)
    423         }
    424 
    425 }
    426 
    427 func sanitizeAttrs(rc *RequestConfig, ctx *fasthttp.RequestCtx, attrs [][][]byte) {
     422                sanitizeAttrs(rc, out, attrs)
     423        }
     424
     425}
     426
     427func sanitizeAttrs(rc *RequestConfig, out io.Writer, attrs [][][]byte) {
    428428        for _, attr := range attrs {
    429                 sanitizeAttr(rc, ctx, attr[0], attr[1])
    430         }
    431 }
    432 
    433 func sanitizeAttr(rc *RequestConfig, ctx *fasthttp.RequestCtx, attrName, attrValue []byte) {
     429                sanitizeAttr(rc, out, attr[0], attr[1])
     430        }
     431}
     432
     433func sanitizeAttr(rc *RequestConfig, out io.Writer, attrName, attrValue []byte) {
    434434        if inArray(attrName, SAFE_ATTRIBUTES) {
    435                 fmt.Fprintf(ctx, " %s=\"%s\"", attrName, attrValue)
     435                fmt.Fprintf(out, " %s=\"%s\"", attrName, attrValue)
    436436                return
    437437        }
     
    439439        case "src", "href", "action":
    440440                if uri, err := proxifyURI(rc, string(attrValue)); err == nil {
    441                         fmt.Fprintf(ctx, " %s=\"%s\"", attrName, uri)
     441                        fmt.Fprintf(out, " %s=\"%s\"", attrName, uri)
    442442                } else {
    443443                        log.Println("cannot proxify uri:", attrValue)
    444444                }
    445445        case "style":
    446                 fmt.Fprintf(ctx, " %s=\"", attrName)
    447                 sanitizeCSS(rc, ctx, attrValue)
    448                 ctx.Write([]byte("\""))
     446                fmt.Fprintf(out, " %s=\"", attrName)
     447                sanitizeCSS(rc, out, attrValue)
     448                out.Write([]byte("\""))
    449449        }
    450450}
Note: See TracChangeset for help on using the changeset viewer.