[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 |
|
---|
[53] | 15 | type StringTestCase struct {
|
---|
| 16 | Input string
|
---|
| 17 | ExpectedOutput string
|
---|
| 18 | }
|
---|
| 19 |
|
---|
[16] | 20 | var attrTestData []*AttrTestCase = []*AttrTestCase{
|
---|
| 21 | &AttrTestCase{
|
---|
| 22 | []byte("href"),
|
---|
| 23 | []byte("./x"),
|
---|
| 24 | []byte(` href="./?mortyurl=http%3A%2F%2F127.0.0.1%2Fx"`),
|
---|
| 25 | },
|
---|
| 26 | &AttrTestCase{
|
---|
| 27 | []byte("src"),
|
---|
| 28 | []byte("http://x.com/y"),
|
---|
| 29 | []byte(` src="./?mortyurl=http%3A%2F%2Fx.com%2Fy"`),
|
---|
| 30 | },
|
---|
| 31 | &AttrTestCase{
|
---|
| 32 | []byte("action"),
|
---|
| 33 | []byte("/z"),
|
---|
| 34 | []byte(` action="./?mortyurl=http%3A%2F%2F127.0.0.1%2Fz"`),
|
---|
| 35 | },
|
---|
| 36 | &AttrTestCase{
|
---|
| 37 | []byte("onclick"),
|
---|
| 38 | []byte("console.log(document.cookies)"),
|
---|
| 39 | nil,
|
---|
| 40 | },
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[53] | 43 | var urlTestData []*StringTestCase = []*StringTestCase{
|
---|
| 44 | &StringTestCase{
|
---|
| 45 | "http://x.com/",
|
---|
| 46 | "./?mortyurl=http%3A%2F%2Fx.com%2F",
|
---|
| 47 | },
|
---|
| 48 | &StringTestCase{
|
---|
| 49 | "http://a@x.com/",
|
---|
| 50 | "./?mortyurl=http%3A%2F%2Fa%40x.com%2F",
|
---|
| 51 | },
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[16] | 54 | func TestAttrSanitizer(t *testing.T) {
|
---|
| 55 | u, _ := url.Parse("http://127.0.0.1/")
|
---|
[22] | 56 | rc := &RequestConfig{BaseURL: u}
|
---|
[16] | 57 | for _, testCase := range attrTestData {
|
---|
| 58 | out := bytes.NewBuffer(nil)
|
---|
[22] | 59 | sanitizeAttr(rc, out, testCase.AttrName, testCase.AttrValue, testCase.AttrValue)
|
---|
[16] | 60 | res, _ := out.ReadBytes(byte(0))
|
---|
| 61 | if !bytes.Equal(res, testCase.ExpectedOutput) {
|
---|
| 62 | t.Errorf(
|
---|
[53] | 63 | `Attribute parse error. Name: "%s", Value: "%s", Expected: %s, Got: "%s"`,
|
---|
[16] | 64 | testCase.AttrName,
|
---|
| 65 | testCase.AttrValue,
|
---|
| 66 | testCase.ExpectedOutput,
|
---|
| 67 | res,
|
---|
| 68 | )
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
[22] | 72 |
|
---|
[53] | 73 | func TestURLProxifier(t *testing.T) {
|
---|
| 74 | u, _ := url.Parse("http://127.0.0.1/")
|
---|
| 75 | rc := &RequestConfig{BaseURL: u}
|
---|
| 76 | for _, testCase := range urlTestData {
|
---|
| 77 | newUrl, err := rc.ProxifyURI(testCase.Input)
|
---|
| 78 | if err != nil {
|
---|
| 79 | t.Errorf("Failed to parse URL: %s", testCase.Input)
|
---|
| 80 | }
|
---|
| 81 | if newUrl != testCase.ExpectedOutput {
|
---|
| 82 | t.Errorf(
|
---|
| 83 | `URL proxifier error. Expected: "%s", Got: "%s"`,
|
---|
| 84 | testCase.ExpectedOutput,
|
---|
| 85 | newUrl,
|
---|
| 86 | )
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[22] | 91 | var BENCH_SIMPLE_HTML []byte = []byte(`<!doctype html>
|
---|
| 92 | <html>
|
---|
| 93 | <head>
|
---|
| 94 | <title>test</title>
|
---|
| 95 | </head>
|
---|
| 96 | <body>
|
---|
| 97 | <h1>Test heading</h1>
|
---|
| 98 | </body>
|
---|
| 99 | </html>`)
|
---|
| 100 |
|
---|
| 101 | func BenchmarkSanitizeSimpleHTML(b *testing.B) {
|
---|
| 102 | u, _ := url.Parse("http://127.0.0.1/")
|
---|
| 103 | rc := &RequestConfig{BaseURL: u}
|
---|
| 104 | b.ResetTimer()
|
---|
| 105 | for i := 0; i < b.N; i++ {
|
---|
| 106 | out := bytes.NewBuffer(nil)
|
---|
| 107 | sanitizeHTML(rc, out, BENCH_SIMPLE_HTML)
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | var BENCH_COMPLEX_HTML []byte = []byte(`<!doctype html>
|
---|
| 112 | <html>
|
---|
| 113 | <head>
|
---|
| 114 | <noscript><meta http-equiv="refresh" content="0; URL=./xy"></noscript>
|
---|
| 115 | <title>test 2</title>
|
---|
| 116 | <script> alert('xy'); </script>
|
---|
| 117 | <link rel="stylesheet" href="./core.bundle.css">
|
---|
| 118 | <style>
|
---|
| 119 | html { background: url(./a.jpg); }
|
---|
| 120 | </style
|
---|
| 121 | </head>
|
---|
| 122 | <body>
|
---|
| 123 | <h1>Test heading</h1>
|
---|
| 124 | <img src="b.png" alt="imgtitle" />
|
---|
| 125 | <form action="/z">
|
---|
| 126 | <input type="submit" style="background: url(http://aa.bb/cc)" >
|
---|
| 127 | </form>
|
---|
| 128 | </body>
|
---|
| 129 | </html>`)
|
---|
| 130 |
|
---|
| 131 | func BenchmarkSanitizeComplexHTML(b *testing.B) {
|
---|
| 132 | u, _ := url.Parse("http://127.0.0.1/")
|
---|
| 133 | rc := &RequestConfig{BaseURL: u}
|
---|
| 134 | b.ResetTimer()
|
---|
| 135 | for i := 0; i < b.N; i++ {
|
---|
| 136 | out := bytes.NewBuffer(nil)
|
---|
| 137 | sanitizeHTML(rc, out, BENCH_COMPLEX_HTML)
|
---|
| 138 | }
|
---|
| 139 | }
|
---|