| 1 | package ffuf |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestTemplatePresent(t *testing.T) { |
| 9 | template := "§" |
| 10 | |
| 11 | headers := make(map[string]string) |
| 12 | headers["foo"] = "§bar§" |
| 13 | headers["omg"] = "bbq" |
| 14 | headers["§world§"] = "Ooo" |
| 15 | |
| 16 | goodConf := Config{ |
| 17 | Url: "https://example.com/fooo/bar?test=§value§&order[§0§]=§foo§", |
| 18 | Method: "PO§ST§", |
| 19 | Headers: headers, |
| 20 | Data: "line=Can we pull back the §veil§ of §static§ and reach in to the source of §all§ being?&commit=true", |
| 21 | } |
| 22 | |
| 23 | if !templatePresent(template, &goodConf) { |
| 24 | t.Errorf("Expected-good config failed validation") |
| 25 | } |
| 26 | |
| 27 | badConfMethod := Config{ |
| 28 | Url: "https://example.com/fooo/bar?test=§value§&order[§0§]=§foo§", |
| 29 | Method: "POST§", |
| 30 | Headers: headers, |
| 31 | Data: "line=Can we pull back the §veil§ of §static§ and reach in to the source of §all§ being?&commit=§true§", |
| 32 | } |
| 33 | |
| 34 | if templatePresent(template, &badConfMethod) { |
| 35 | t.Errorf("Expected-bad config (Method) failed validation") |
| 36 | } |
| 37 | |
| 38 | badConfURL := Config{ |
| 39 | Url: "https://example.com/fooo/bar?test=§value§&order[0§]=§foo§", |
| 40 | Method: "§POST§", |
| 41 | Headers: headers, |
| 42 | Data: "line=Can we pull back the §veil§ of §static§ and reach in to the source of §all§ being?&commit=§true§", |
| 43 | } |
| 44 | |
| 45 | if templatePresent(template, &badConfURL) { |
| 46 | t.Errorf("Expected-bad config (URL) failed validation") |
| 47 | } |
| 48 | |
| 49 | badConfData := Config{ |
| 50 | Url: "https://example.com/fooo/bar?test=§value§&order[§0§]=§foo§", |
| 51 | Method: "§POST§", |
| 52 | Headers: headers, |
| 53 | Data: "line=Can we pull back the §veil of §static§ and reach in to the source of §all§ being?&commit=§true§", |
| 54 | } |
| 55 | |
| 56 | if templatePresent(template, &badConfData) { |
| 57 | t.Errorf("Expected-bad config (Data) failed validation") |
| 58 | } |
| 59 | |
| 60 | headers["kingdom"] = "§candy" |
| 61 | |
| 62 | badConfHeaderValue := Config{ |
| 63 | Url: "https://example.com/fooo/bar?test=§value§&order[§0§]=§foo§", |
| 64 | Method: "PO§ST§", |
| 65 | Headers: headers, |
| 66 | Data: "line=Can we pull back the §veil§ of §static§ and reach in to the source of §all§ being?&commit=true", |
| 67 | } |
| 68 | |
| 69 | if templatePresent(template, &badConfHeaderValue) { |
| 70 | t.Errorf("Expected-bad config (Header value) failed validation") |
| 71 | } |
| 72 | |
| 73 | headers["kingdom"] = "candy" |
| 74 | headers["§kingdom"] = "candy" |
| 75 | |
| 76 | badConfHeaderKey := Config{ |
| 77 | Url: "https://example.com/fooo/bar?test=§value§&order[§0§]=§foo§", |
| 78 | Method: "PO§ST§", |
| 79 | Headers: headers, |
| 80 | Data: "line=Can we pull back the §veil§ of §static§ and reach in to the source of §all§ being?&commit=true", |
| 81 | } |
| 82 | |
| 83 | if templatePresent(template, &badConfHeaderKey) { |
| 84 | t.Errorf("Expected-bad config (Header key) failed validation") |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func TestProxyParsing(t *testing.T) { |
| 89 | configOptions := NewConfigOptions() |
| 90 | errorString := "Bad proxy url (-x) format. Expected http, https or socks5 url" |
| 91 | |
| 92 | // http should work |
| 93 | configOptions.HTTP.ProxyURL = "http://127.0.0.1:8080" |
| 94 | _, err := ConfigFromOptions(configOptions, nil, nil) |
| 95 | if strings.Contains(err.Error(), errorString) { |
| 96 | t.Errorf("Expected http proxy string to work") |
| 97 | } |
| 98 | |
| 99 | // https should work |
| 100 | configOptions.HTTP.ProxyURL = "https://127.0.0.1" |
| 101 | _, err = ConfigFromOptions(configOptions, nil, nil) |
| 102 | if strings.Contains(err.Error(), errorString) { |
| 103 | t.Errorf("Expected https proxy string to work") |
| 104 | } |
| 105 | |
| 106 | // socks5 should work |
| 107 | configOptions.HTTP.ProxyURL = "socks5://127.0.0.1" |
| 108 | _, err = ConfigFromOptions(configOptions, nil, nil) |
| 109 | if strings.Contains(err.Error(), errorString) { |
| 110 | t.Errorf("Expected socks5 proxy string to work") |
| 111 | } |
| 112 | |
| 113 | // garbage data should FAIL |
| 114 | configOptions.HTTP.ProxyURL = "Y0 y0 it's GREASE" |
| 115 | _, err = ConfigFromOptions(configOptions, nil, nil) |
| 116 | if !strings.Contains(err.Error(), errorString) { |
| 117 | t.Errorf("Expected garbage proxy string to fail") |
| 118 | } |
| 119 | |
| 120 | // Opaque URLs with the right scheme should FAIL |
| 121 | configOptions.HTTP.ProxyURL = "http:sixhours@dungeon" |
| 122 | _, err = ConfigFromOptions(configOptions, nil, nil) |
| 123 | if !strings.Contains(err.Error(), errorString) { |
| 124 | t.Errorf("Expected opaque proxy string to fail") |
| 125 | } |
| 126 | |
| 127 | // Unsupported protocols should FAIL |
| 128 | configOptions.HTTP.ProxyURL = "imap://127.0.0.1" |
| 129 | _, err = ConfigFromOptions(configOptions, nil, nil) |
| 130 | if !strings.Contains(err.Error(), errorString) { |
| 131 | t.Errorf("Expected proxy string with unsupported protocol to fail") |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | func TestReplayProxyParsing(t *testing.T) { |
| 136 | configOptions := NewConfigOptions() |
| 137 | errorString := "Bad replay-proxy url (-replay-proxy) format. Expected http, https or socks5 url" |
| 138 | |
| 139 | // http should work |
| 140 | configOptions.HTTP.ReplayProxyURL = "http://127.0.0.1:8080" |
| 141 | _, err := ConfigFromOptions(configOptions, nil, nil) |
| 142 | if strings.Contains(err.Error(), errorString) { |
| 143 | t.Errorf("Expected http replay proxy string to work") |
| 144 | } |
| 145 | |
| 146 | // https should work |
| 147 | configOptions.HTTP.ReplayProxyURL = "https://127.0.0.1" |
| 148 | _, err = ConfigFromOptions(configOptions, nil, nil) |
| 149 | if strings.Contains(err.Error(), errorString) { |
| 150 | t.Errorf("Expected https proxy string to work") |
| 151 | } |
| 152 | |
| 153 | // socks5 should work |
| 154 | configOptions.HTTP.ReplayProxyURL = "socks5://127.0.0.1" |
| 155 | _, err = ConfigFromOptions(configOptions, nil, nil) |
| 156 | if strings.Contains(err.Error(), errorString) { |
| 157 | t.Errorf("Expected socks5 proxy string to work") |
| 158 | } |
| 159 | |
| 160 | // garbage data should FAIL |
| 161 | configOptions.HTTP.ReplayProxyURL = "Y0 y0 it's GREASE" |
| 162 | _, err = ConfigFromOptions(configOptions, nil, nil) |
| 163 | if !strings.Contains(err.Error(), errorString) { |
| 164 | t.Errorf("Expected garbage proxy string to fail") |
| 165 | } |
| 166 | |
| 167 | // Opaque URLs with the right scheme should FAIL |
| 168 | configOptions.HTTP.ReplayProxyURL = "http:sixhours@dungeon" |
| 169 | _, err = ConfigFromOptions(configOptions, nil, nil) |
| 170 | if !strings.Contains(err.Error(), errorString) { |
| 171 | t.Errorf("Expected opaque proxy string to fail") |
| 172 | } |
| 173 | |
| 174 | // Unsupported protocols should FAIL |
| 175 | configOptions.HTTP.ReplayProxyURL = "imap://127.0.0.1" |
| 176 | _, err = ConfigFromOptions(configOptions, nil, nil) |
| 177 | if !strings.Contains(err.Error(), errorString) { |
| 178 | t.Errorf("Expected proxy string with unsupported protocol to fail") |
| 179 | } |
| 180 | } |