|
| 1 | +import { validateRedirectUrl } from "../urlValidation"; |
| 2 | + |
| 3 | +describe("validateRedirectUrl", () => { |
| 4 | + describe("valid URLs", () => { |
| 5 | + it("should allow HTTP URLs", () => { |
| 6 | + expect(() => validateRedirectUrl("http://example.com")).not.toThrow(); |
| 7 | + }); |
| 8 | + |
| 9 | + it("should allow HTTPS URLs", () => { |
| 10 | + expect(() => validateRedirectUrl("https://example.com")).not.toThrow(); |
| 11 | + }); |
| 12 | + |
| 13 | + it("should allow URLs with ports", () => { |
| 14 | + expect(() => |
| 15 | + validateRedirectUrl("https://example.com:8080"), |
| 16 | + ).not.toThrow(); |
| 17 | + }); |
| 18 | + |
| 19 | + it("should allow URLs with paths", () => { |
| 20 | + expect(() => |
| 21 | + validateRedirectUrl("https://example.com/path/to/auth"), |
| 22 | + ).not.toThrow(); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should allow URLs with query parameters", () => { |
| 26 | + expect(() => |
| 27 | + validateRedirectUrl("https://example.com?param=value"), |
| 28 | + ).not.toThrow(); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + describe("invalid URLs - XSS vectors", () => { |
| 33 | + it("should block javascript: protocol", () => { |
| 34 | + expect(() => validateRedirectUrl("javascript:alert('XSS')")).toThrow( |
| 35 | + "Authorization URL must be HTTP or HTTPS", |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should block javascript: with encoded characters", () => { |
| 40 | + expect(() => |
| 41 | + validateRedirectUrl("javascript:alert%28%27XSS%27%29"), |
| 42 | + ).toThrow("Authorization URL must be HTTP or HTTPS"); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should block data: protocol", () => { |
| 46 | + expect(() => |
| 47 | + validateRedirectUrl("data:text/html,<script>alert('XSS')</script>"), |
| 48 | + ).toThrow("Authorization URL must be HTTP or HTTPS"); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should block vbscript: protocol", () => { |
| 52 | + expect(() => validateRedirectUrl("vbscript:msgbox")).toThrow( |
| 53 | + "Authorization URL must be HTTP or HTTPS", |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should block file: protocol", () => { |
| 58 | + expect(() => validateRedirectUrl("file:///etc/passwd")).toThrow( |
| 59 | + "Authorization URL must be HTTP or HTTPS", |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should block about: protocol", () => { |
| 64 | + expect(() => validateRedirectUrl("about:blank")).toThrow( |
| 65 | + "Authorization URL must be HTTP or HTTPS", |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should block custom protocols", () => { |
| 70 | + expect(() => validateRedirectUrl("custom://example")).toThrow( |
| 71 | + "Authorization URL must be HTTP or HTTPS", |
| 72 | + ); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe("edge cases", () => { |
| 77 | + it("should handle malformed URLs", () => { |
| 78 | + expect(() => validateRedirectUrl("not a url")).toThrow( |
| 79 | + "Invalid URL: not a url", |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should handle empty string", () => { |
| 84 | + expect(() => validateRedirectUrl("")).toThrow("Invalid URL: "); |
| 85 | + }); |
| 86 | + |
| 87 | + it("should handle URLs with unicode characters", () => { |
| 88 | + expect(() => validateRedirectUrl("https://例え.jp")).not.toThrow(); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should handle URLs with case variations", () => { |
| 92 | + expect(() => validateRedirectUrl("HTTPS://EXAMPLE.COM")).not.toThrow(); |
| 93 | + expect(() => validateRedirectUrl("HtTpS://example.com")).not.toThrow(); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should handle protocol-relative URLs as invalid", () => { |
| 97 | + expect(() => validateRedirectUrl("//example.com")).toThrow( |
| 98 | + "Invalid URL: //example.com", |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + it("should handle URLs with authentication", () => { |
| 103 | + expect(() => |
| 104 | + validateRedirectUrl("https://user:[email protected]"), |
| 105 | + ).not.toThrow(); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe("security considerations", () => { |
| 110 | + it("should not be fooled by whitespace", () => { |
| 111 | + expect(() => validateRedirectUrl(" javascript:alert('XSS')")).toThrow(); |
| 112 | + expect(() => validateRedirectUrl("javascript:alert('XSS') ")).toThrow(); |
| 113 | + }); |
| 114 | + |
| 115 | + it("should handle null bytes", () => { |
| 116 | + expect(() => |
| 117 | + validateRedirectUrl("java\x00script:alert('XSS')"), |
| 118 | + ).toThrow(); |
| 119 | + }); |
| 120 | + |
| 121 | + it("should handle tab characters", () => { |
| 122 | + expect(() => validateRedirectUrl("java\tscript:alert('XSS')")).toThrow(); |
| 123 | + }); |
| 124 | + |
| 125 | + it("should handle newlines", () => { |
| 126 | + expect(() => validateRedirectUrl("java\nscript:alert('XSS')")).toThrow(); |
| 127 | + }); |
| 128 | + |
| 129 | + it("should handle mixed case protocols", () => { |
| 130 | + expect(() => validateRedirectUrl("JaVaScRiPt:alert('XSS')")).toThrow( |
| 131 | + "Authorization URL must be HTTP or HTTPS", |
| 132 | + ); |
| 133 | + }); |
| 134 | + }); |
| 135 | +}); |
0 commit comments