Changeset 46 in code


Ignore:
Timestamp:
Nov 28, 2016, 5:38:17 PM (9 years ago)
Author:
alex
Message:
  • ignore svg and math tags (most of attributes are ignored in the current implementation)
  • add hreflang attribute as a safe one ( allows <meta rel="alternate" hreflang="x" href="" /> )
  • if the meta tag contains a http_equiv attribute:

if the value is safe, output the meta tag with the http_equiv attribute (avoid the <meta content="IE=edge">)
if the value is not safe, ignore the meta tag

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/morty.go

    r45 r46  
    3939        []byte("embed"),
    4040        //[]byte("iframe"),
     41        []byte("math"),
    4142        []byte("script"),
     43        []byte("svg"),
    4244}
    4345
     
    5961        []byte("height"),
    6062        []byte("hidden"),
     63        []byte("hreflang"),
    6164        []byte("id"),
    6265        []byte("lang"),
     
    9699}
    97100
     101var LINK_REL_SAFE_VALUES [][]byte = [][]byte{
     102        []byte("alternate"),
     103        []byte("archives"),
     104        []byte("author"),
     105        []byte("copyright"),
     106        []byte("first"),
     107        []byte("help"),
     108        []byte("icon"),
     109        []byte("index"),
     110        []byte("last"),
     111        []byte("license"),
     112        []byte("manifest"),
     113        []byte("next"),
     114        []byte("pingback"),
     115        []byte("prev"),
     116        []byte("publisher"),
     117        []byte("search"),
     118        []byte("shortcut icon"),
     119        []byte("stylesheet"),
     120        []byte("up"),
     121}
     122
     123var LINK_HTTP_EQUIV_SAFE_VALUES [][]byte = [][]byte{
     124        // X-UA-Compatible will be added automaticaly, so it can be skipped
     125        []byte("date"),
     126        []byte("last-modified"),
     127        []byte("refresh"), // URL rewrite
     128        // []byte("location"), TODO URL rewrite
     129        []byte("content-language"),
     130}
     131
    98132type Proxy struct {
    99133        Key            []byte
     
    124158`
    125159
    126 var HTML_META_CONTENT_TYPE string = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"
     160var HTML_HEAD_CONTENT_TYPE string = `<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     161<meta http-equiv="X-UA-Compatible" content="IE=edge">
     162`
    127163
    128164func (p *Proxy) RequestHandler(ctx *fasthttp.RequestCtx) {
     
    167203        req.SetConnectionClose()
    168204
    169         requestURIStr := string(requestURI)
    170 
    171         log.Println("getting", requestURIStr)
    172 
    173         req.SetRequestURI(requestURIStr)
     205        reqQuery := parsedURI.Query()
     206        ctx.QueryArgs().VisitAll(func(key, value []byte) {
     207                reqQuery.Add(string(key), string(value))
     208        })
     209
     210        parsedURI.RawQuery = reqQuery.Encode()
     211
     212        uriStr := parsedURI.String()
     213
     214        log.Println("getting", uriStr)
     215
     216        req.SetRequestURI(uriStr)
    174217        req.Header.SetUserAgentBytes([]byte("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36"))
    175218
     
    210253                        }
    211254                }
    212                 error_message := fmt.Sprintf("invalid response: %d (%s)", resp.StatusCode(), requestURIStr)
     255                error_message := fmt.Sprintf("invalid response: %d", resp.StatusCode())
    213256                p.serveMainPage(ctx, resp.StatusCode(), errors.New(error_message))
    214257                return
     
    409452
    410453                                if bytes.Equal(tag, []byte("head")) {
    411                                         fmt.Fprintf(out, HTML_META_CONTENT_TYPE)
     454                                        fmt.Fprintf(out, HTML_HEAD_CONTENT_TYPE)
    412455                                }
    413456
     
    487530                attrValue := attr[1]
    488531                if bytes.Equal(attrName, []byte("rel")) {
    489                         if bytes.Equal(attrValue, []byte("dns-prefetch")) {
     532                        if !inArray(attrValue, LINK_REL_SAFE_VALUES) {
    490533                                exclude = true
    491534                                break
     
    518561                if bytes.Equal(attrName, []byte("http-equiv")) {
    519562                        http_equiv = bytes.ToLower(attrValue)
     563                        // exclude some <meta http-equiv="..." ..>
     564                        if !inArray(http_equiv, LINK_HTTP_EQUIV_SAFE_VALUES) {
     565                                return
     566                        }
    520567                }
    521568                if bytes.Equal(attrName, []byte("content")) {
     
    526573                        return
    527574                }
    528         }
    529 
    530         if bytes.Equal(http_equiv, []byte("content-type")) {
    531                 return
    532575        }
    533576
     
    547590                }
    548591        } else {
     592                if len(http_equiv) > 0 {
     593                        fmt.Fprintf(out, ` http-equiv="%s"`, http_equiv)
     594                }
    549595                sanitizeAttrs(rc, out, attrs)
    550596        }
     
    606652                return fmt.Sprintf("./?mortyurl=%s", url.QueryEscape(uri)), nil
    607653        }
    608 
    609654        return fmt.Sprintf("./?mortyhash=%s&mortyurl=%s", hash(uri, rc.Key), url.QueryEscape(uri)), nil
    610655}
Note: See TracChangeset for help on using the changeset viewer.