Changeset 60 in code for trunk/morty_test.go


Ignore:
Timestamp:
Dec 1, 2016, 12:45:38 PM (9 years ago)
Author:
alex
Message:

[enh] ignore all special characters in the URI protocol (example : jav	ascript:alert('XSS'))

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/morty_test.go

    r55 r60  
    1313}
    1414
     15type SanitizeURITestCase struct {
     16        Input          []byte
     17        ExpectedOutput []byte
     18        ExpectedScheme string
     19}
     20
    1521type StringTestCase struct {
    1622        Input          string
     
    3844                []byte("console.log(document.cookies)"),
    3945                nil,
     46        },
     47}
     48
     49var sanitizeUriTestData []*SanitizeURITestCase = []*SanitizeURITestCase{
     50        &SanitizeURITestCase{
     51                []byte("http://example.com/"),
     52                []byte("http://example.com/"),
     53                "http:",
     54        },
     55        &SanitizeURITestCase{
     56                []byte("HtTPs://example.com/     \t"),
     57                []byte("https://example.com/"),
     58                "https:",
     59        },
     60        &SanitizeURITestCase{
     61                []byte("      Ht  TPs://example.com/     \t"),
     62                []byte("https://example.com/"),
     63                "https:",
     64        },
     65        &SanitizeURITestCase{
     66                []byte("javascript:void(0)"),
     67                []byte("javascript:void(0)"),
     68                "javascript:",
     69        },
     70        &SanitizeURITestCase{
     71                []byte("      /path/to/a/file/without/protocol     "),
     72                []byte("/path/to/a/file/without/protocol"),
     73                "",
     74        },
     75        &SanitizeURITestCase{
     76                []byte("      #fragment     "),
     77                []byte("#fragment"),
     78                "",
     79        },
     80        &SanitizeURITestCase{
     81                []byte("      qwertyuiop     "),
     82                []byte("qwertyuiop"),
     83                "",
     84        },
     85        &SanitizeURITestCase{
     86                []byte(""),
     87                []byte(""),
     88                "",
     89        },
     90        &SanitizeURITestCase{
     91                []byte(":"),
     92                []byte(":"),
     93                ":",
     94        },
     95        &SanitizeURITestCase{
     96                []byte("   :"),
     97                []byte(":"),
     98                ":",
     99        },
     100        &SanitizeURITestCase{
     101                []byte("schéma:"),
     102                []byte("schéma:"),
     103                "schéma:",
    40104        },
    41105}
     
    75139}
    76140
     141func TestSanitizeURI(t *testing.T) {
     142        for _, testCase := range sanitizeUriTestData {
     143                newUrl, scheme := sanitizeURI(testCase.Input)
     144                if !bytes.Equal(newUrl, testCase.ExpectedOutput) {
     145                        t.Errorf(
     146                                `URL proxifier error. Expected: "%s", Got: "%s"`,
     147                                testCase.ExpectedOutput,
     148                                newUrl,
     149                        )
     150                }
     151                if scheme != testCase.ExpectedScheme {
     152                        t.Errorf(
     153                                `URL proxifier error. Expected: "%s", Got: "%s"`,
     154                                testCase.ExpectedScheme,
     155                                scheme,
     156                        )
     157                }
     158        }
     159}
     160
    77161func TestURLProxifier(t *testing.T) {
    78162        u, _ := url.Parse("http://127.0.0.1/")
    79163        rc := &RequestConfig{BaseURL: u}
    80164        for _, testCase := range urlTestData {
    81                 newUrl, err := rc.ProxifyURI(testCase.Input)
     165                newUrl, err := rc.ProxifyURI([]byte(testCase.Input))
    82166                if err != nil {
    83167                        t.Errorf("Failed to parse URL: %s", testCase.Input)
Note: See TracChangeset for help on using the changeset viewer.