@@ -0,0 +1,102 @@
1+package redirect
2+
3+import (
4+ "bufio"
5+ "net/url"
6+ "os"
7+
8+ . "github.com/onsi/ginkgo"
9+ . "github.com/onsi/ginkgo/extensions/table"
10+ . "github.com/onsi/gomega"
11+)
12+
13+var _ = Describe("Validator suite", func() {
14+ var testAllowedDomains []string
15+
16+ BeforeEach(func() {
17+ testAllowedDomains = []string{
18+ "foo.bar",
19+ ".bar.foo",
20+ "port.bar:8080",
21+ ".sub.port.bar:8080",
22+ "anyport.bar:*",
23+ ".sub.anyport.bar:*",
24+ "www.whitelisteddomain.tld",
25+ }
26+ })
27+
28+ Context("OpenRedirect List", func() {
29+ file, err := os.Open("../../../testdata/openredirects.txt")
30+ Expect(err).ToNot(HaveOccurred())
31+ defer func() {
32+ Expect(file.Close()).To(Succeed())
33+ }()
34+
35+ scanner := bufio.NewScanner(file)
36+ for scanner.Scan() {
37+ rd := scanner.Text()
38+ It(rd, func() {
39+ rdUnescaped, err := url.QueryUnescape(rd)
40+ Expect(err).ToNot(HaveOccurred())
41+
42+ validator := NewValidator(testAllowedDomains)
43+ Expect(validator.IsValidRedirect(rdUnescaped)).To(BeFalse(), "Expected redirect not to be valid")
44+ })
45+ }
46+
47+ Expect(scanner.Err()).ToNot(HaveOccurred())
48+ })
49+
50+ Context("Validator", func() {
51+ DescribeTable("IsValidRedirect",
52+ func(testRedirect string, expected bool) {
53+ validator := NewValidator(testAllowedDomains)
54+ Expect(validator.IsValidRedirect(testRedirect)).To(Equal(expected))
55+ },
56+ Entry("No Redirect", "", false),
57+ Entry("Single Slash", "/redirect", true),
58+ Entry("Double Slash", "//redirect", false),
59+ Entry("Valid HTTP", "http://foo.bar/redirect", true),
60+ Entry("Valid HTTPS", "https://foo.bar/redirect", true),
61+ Entry("Invalid HTTP subdomain", "http://baz.foo.bar/redirect", false),
62+ Entry("Invalid HTTPS subdomain", "https://baz.foo.bar/redirect", false),
63+ Entry("Valid HTTP subdomain", "http://baz.bar.foo/redirect", true),
64+ Entry("Valid HTTPS subdomain", "https://baz.bar.foo/redirect", true),
65+ Entry("Valid HTTP Domain", "http://bar.foo/redirect", true), // Is this correct, do we want to match the root domain?
66+ Entry("Invalid HTTP Similar Domain", "http://foo.bar.evil.corp/redirect", false),
67+ Entry("Invalid HTTPS Similar Domain", "https://foo.bar.evil.corp/redirect", false),
68+ Entry("Invalid HTTP RD Parameter", "http://evil.corp/redirect?rd=foo.bar", false),
69+ Entry("Invalid HTTPS RD Parameter", "https://evil.corp/redirect?rd=foo.bar", false),
70+ Entry("Invalid Port and Domain", "https://evil.corp:3838/redirect", false),
71+ Entry("Invalid Port on Allowed Domain", "http://foo.bar:3838/redirect", false),
72+ Entry("Invalid Port on Allowed Subdomain", "http://baz.bar.foo:3838/redirect", false),
73+ Entry("Valid Specified Port and Domain", "http://port.bar:8080/redirect", true),
74+ Entry("Invalid Specified Port and Domain", "http://port.bar:3838/redirect", false),
75+ Entry("Valid Specified Port and Subdomain", "http://foo.sub.port.bar:8080/redirect", true),
76+ Entry("Invalid Specified Port andSubdomain", "http://foo.subport.bar:3838/redirect", false),
77+ Entry("Valid Any Port, Specified Domain", "http://anyport.bar:8080/redirect", true),
78+ Entry("Valid Different Any Port, Specified Domain", "http://anyport.bar:8081/redirect", true),
79+ Entry("Valid Any Port, Specified Subdomain", "http://a.sub.anyport.bar:8080/redirect", true),
80+ Entry("Valid Different Any Port, Specified Subdomain", "http://a.sub.anyport.bar:8081/redirect", true),
81+ Entry("Escape Double Slash", "/\\evil.com", false),
82+ Entry("Space Single Slash", "/ /evil.com", false),
83+ Entry("Space Double Slash", "/ \\evil.com", false),
84+ Entry("Tab Single Slash", "/\t/evil.com", false),
85+ Entry("Tab Double Slash", "/\t\\evil.com", false),
86+ Entry("Vertical Tab Single Slash", "/\v/evil.com", false),
87+ Entry("Vertiacl Tab Double Slash", "/\v\\evil.com", false),
88+ Entry("New Line Single Slash", "/\n/evil.com", false),
89+ Entry("New Line Double Slash", "/\n\\evil.com", false),
90+ Entry("Carriage Return Single Slash", "/\r/evil.com", false),
91+ Entry("Carriage Return Double Slash", "/\r\\evil.com", false),
92+ Entry("Double Tab", "/\t/\t\\evil.com", false),
93+ Entry("Triple Tab 1", "/\t\t/\t/evil.com", false),
94+ Entry("Triple Tab 2", "/\t\t\\\t/evil.com", false),
95+ Entry("Quad Tab 1", "/\t\t/\t\t\\evil.com", false),
96+ Entry("Quad Tab 2", "/\t\t\\\t\t/evil.com", false),
97+ Entry("Relative Path", "/./\\evil.com", false),
98+ Entry("Relative Subpath", "/./../../\\evil.com", false),
99+ Entry("Partial Subdomain", "evilbar.foo", false),
100+ )
101+ })
102+})