[16] | 1 | package main
|
---|
| 2 |
|
---|
| 3 | import (
|
---|
| 4 | "bytes"
|
---|
| 5 | "net/url"
|
---|
| 6 | "testing"
|
---|
| 7 | )
|
---|
| 8 |
|
---|
| 9 | type AttrTestCase struct {
|
---|
| 10 | AttrName []byte
|
---|
| 11 | AttrValue []byte
|
---|
| 12 | ExpectedOutput []byte
|
---|
| 13 | }
|
---|
| 14 |
|
---|
| 15 | var attrTestData []*AttrTestCase = []*AttrTestCase{
|
---|
| 16 | &AttrTestCase{
|
---|
| 17 | []byte("href"),
|
---|
| 18 | []byte("./x"),
|
---|
| 19 | []byte(` href="./?mortyurl=http%3A%2F%2F127.0.0.1%2Fx"`),
|
---|
| 20 | },
|
---|
| 21 | &AttrTestCase{
|
---|
| 22 | []byte("src"),
|
---|
| 23 | []byte("http://x.com/y"),
|
---|
| 24 | []byte(` src="./?mortyurl=http%3A%2F%2Fx.com%2Fy"`),
|
---|
| 25 | },
|
---|
| 26 | &AttrTestCase{
|
---|
| 27 | []byte("action"),
|
---|
| 28 | []byte("/z"),
|
---|
| 29 | []byte(` action="./?mortyurl=http%3A%2F%2F127.0.0.1%2Fz"`),
|
---|
| 30 | },
|
---|
| 31 | &AttrTestCase{
|
---|
| 32 | []byte("onclick"),
|
---|
| 33 | []byte("console.log(document.cookies)"),
|
---|
| 34 | nil,
|
---|
| 35 | },
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | func TestAttrSanitizer(t *testing.T) {
|
---|
| 39 | u, _ := url.Parse("http://127.0.0.1/")
|
---|
[22] | 40 | rc := &RequestConfig{BaseURL: u}
|
---|
[16] | 41 | for _, testCase := range attrTestData {
|
---|
| 42 | out := bytes.NewBuffer(nil)
|
---|
[22] | 43 | sanitizeAttr(rc, out, testCase.AttrName, testCase.AttrValue, testCase.AttrValue)
|
---|
[16] | 44 | res, _ := out.ReadBytes(byte(0))
|
---|
| 45 | if !bytes.Equal(res, testCase.ExpectedOutput) {
|
---|
| 46 | t.Errorf(
|
---|
| 47 | `Attribute parse error. Name: "%s", Value: "%s", Expected: %s, Got: %s`,
|
---|
| 48 | testCase.AttrName,
|
---|
| 49 | testCase.AttrValue,
|
---|
| 50 | testCase.ExpectedOutput,
|
---|
| 51 | res,
|
---|
| 52 | )
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
[22] | 56 |
|
---|
| 57 | var BENCH_SIMPLE_HTML []byte = []byte(`<!doctype html>
|
---|
| 58 | <html>
|
---|
| 59 | <head>
|
---|
| 60 | <title>test</title>
|
---|
| 61 | </head>
|
---|
| 62 | <body>
|
---|
| 63 | <h1>Test heading</h1>
|
---|
| 64 | </body>
|
---|
| 65 | </html>`)
|
---|
| 66 |
|
---|
| 67 | func BenchmarkSanitizeSimpleHTML(b *testing.B) {
|
---|
| 68 | u, _ := url.Parse("http://127.0.0.1/")
|
---|
| 69 | rc := &RequestConfig{BaseURL: u}
|
---|
| 70 | b.ResetTimer()
|
---|
| 71 | for i := 0; i < b.N; i++ {
|
---|
| 72 | out := bytes.NewBuffer(nil)
|
---|
| 73 | sanitizeHTML(rc, out, BENCH_SIMPLE_HTML)
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | var BENCH_COMPLEX_HTML []byte = []byte(`<!doctype html>
|
---|
| 78 | <html>
|
---|
| 79 | <head>
|
---|
| 80 | <noscript><meta http-equiv="refresh" content="0; URL=./xy"></noscript>
|
---|
| 81 | <title>test 2</title>
|
---|
| 82 | <script> alert('xy'); </script>
|
---|
| 83 | <link rel="stylesheet" href="./core.bundle.css">
|
---|
| 84 | <style>
|
---|
| 85 | html { background: url(./a.jpg); }
|
---|
| 86 | </style
|
---|
| 87 | </head>
|
---|
| 88 | <body>
|
---|
| 89 | <h1>Test heading</h1>
|
---|
| 90 | <img src="b.png" alt="imgtitle" />
|
---|
| 91 | <form action="/z">
|
---|
| 92 | <input type="submit" style="background: url(http://aa.bb/cc)" >
|
---|
| 93 | </form>
|
---|
| 94 | </body>
|
---|
| 95 | </html>`)
|
---|
| 96 |
|
---|
| 97 | func BenchmarkSanitizeComplexHTML(b *testing.B) {
|
---|
| 98 | u, _ := url.Parse("http://127.0.0.1/")
|
---|
| 99 | rc := &RequestConfig{BaseURL: u}
|
---|
| 100 | b.ResetTimer()
|
---|
| 101 | for i := 0; i < b.N; i++ {
|
---|
| 102 | out := bytes.NewBuffer(nil)
|
---|
| 103 | sanitizeHTML(rc, out, BENCH_COMPLEX_HTML)
|
---|
| 104 | }
|
---|
| 105 | }
|
---|