|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { |
| 3 | + sanitizeUrl, |
| 4 | + isUrlSafe, |
| 5 | + isValidMessageLength, |
| 6 | + sanitizeMessageContent, |
| 7 | + MAX_MESSAGE_LENGTH, |
| 8 | +} from "../sanitize"; |
| 9 | + |
| 10 | +describe("sanitizeUrl", () => { |
| 11 | + describe("valid URLs", () => { |
| 12 | + it("allows https URLs", () => { |
| 13 | + expect(sanitizeUrl("https://example.com")).toBe("https://example.com"); |
| 14 | + }); |
| 15 | + |
| 16 | + it("allows http URLs", () => { |
| 17 | + expect(sanitizeUrl("http://example.com")).toBe("http://example.com"); |
| 18 | + }); |
| 19 | + |
| 20 | + it("allows URLs with paths", () => { |
| 21 | + expect(sanitizeUrl("https://example.com/path/to/page")).toBe( |
| 22 | + "https://example.com/path/to/page" |
| 23 | + ); |
| 24 | + }); |
| 25 | + |
| 26 | + it("allows URLs with query strings", () => { |
| 27 | + expect(sanitizeUrl("https://example.com?foo=bar&baz=qux")).toBe( |
| 28 | + "https://example.com?foo=bar&baz=qux" |
| 29 | + ); |
| 30 | + }); |
| 31 | + |
| 32 | + it("allows URLs with fragments", () => { |
| 33 | + expect(sanitizeUrl("https://example.com#section")).toBe( |
| 34 | + "https://example.com#section" |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + it("trims whitespace", () => { |
| 39 | + expect(sanitizeUrl(" https://example.com ")).toBe("https://example.com"); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe("XSS attack vectors", () => { |
| 44 | + it("blocks javascript: URLs", () => { |
| 45 | + expect(sanitizeUrl("javascript:alert('xss')")).toBeNull(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("blocks javascript: URLs with encoding", () => { |
| 49 | + expect(sanitizeUrl("javascript:alert(1)")).toBeNull(); |
| 50 | + }); |
| 51 | + |
| 52 | + it("blocks data: URLs", () => { |
| 53 | + expect(sanitizeUrl("data:text/html,<script>alert(1)</script>")).toBeNull(); |
| 54 | + }); |
| 55 | + |
| 56 | + it("blocks data: URLs with base64", () => { |
| 57 | + expect(sanitizeUrl("data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==")).toBeNull(); |
| 58 | + }); |
| 59 | + |
| 60 | + it("blocks vbscript: URLs", () => { |
| 61 | + expect(sanitizeUrl("vbscript:msgbox('xss')")).toBeNull(); |
| 62 | + }); |
| 63 | + |
| 64 | + it("blocks file: URLs", () => { |
| 65 | + expect(sanitizeUrl("file:///etc/passwd")).toBeNull(); |
| 66 | + }); |
| 67 | + |
| 68 | + it("blocks ftp: URLs", () => { |
| 69 | + expect(sanitizeUrl("ftp://ftp.example.com")).toBeNull(); |
| 70 | + }); |
| 71 | + |
| 72 | + it("blocks javascript: URLs with mixed case", () => { |
| 73 | + expect(sanitizeUrl("JaVaScRiPt:alert(1)")).toBeNull(); |
| 74 | + }); |
| 75 | + |
| 76 | + it("blocks javascript: URLs with whitespace", () => { |
| 77 | + expect(sanitizeUrl(" javascript:alert(1) ")).toBeNull(); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe("edge cases", () => { |
| 82 | + it("returns null for empty string", () => { |
| 83 | + expect(sanitizeUrl("")).toBeNull(); |
| 84 | + }); |
| 85 | + |
| 86 | + it("returns null for whitespace only", () => { |
| 87 | + expect(sanitizeUrl(" ")).toBeNull(); |
| 88 | + }); |
| 89 | + |
| 90 | + it("returns null for null input", () => { |
| 91 | + expect(sanitizeUrl(null as unknown as string)).toBeNull(); |
| 92 | + }); |
| 93 | + |
| 94 | + it("returns null for undefined input", () => { |
| 95 | + expect(sanitizeUrl(undefined as unknown as string)).toBeNull(); |
| 96 | + }); |
| 97 | + |
| 98 | + it("returns null for non-string input", () => { |
| 99 | + expect(sanitizeUrl(123 as unknown as string)).toBeNull(); |
| 100 | + }); |
| 101 | + |
| 102 | + it("returns null for relative URLs", () => { |
| 103 | + expect(sanitizeUrl("/path/to/page")).toBeNull(); |
| 104 | + }); |
| 105 | + |
| 106 | + it("returns null for invalid URLs", () => { |
| 107 | + expect(sanitizeUrl("not a url")).toBeNull(); |
| 108 | + }); |
| 109 | + }); |
| 110 | +}); |
| 111 | + |
| 112 | +describe("isUrlSafe", () => { |
| 113 | + it("returns true for https URLs", () => { |
| 114 | + expect(isUrlSafe("https://example.com")).toBe(true); |
| 115 | + }); |
| 116 | + |
| 117 | + it("returns true for http URLs", () => { |
| 118 | + expect(isUrlSafe("http://example.com")).toBe(true); |
| 119 | + }); |
| 120 | + |
| 121 | + it("returns false for javascript: URLs", () => { |
| 122 | + expect(isUrlSafe("javascript:alert(1)")).toBe(false); |
| 123 | + }); |
| 124 | + |
| 125 | + it("returns false for empty string", () => { |
| 126 | + expect(isUrlSafe("")).toBe(false); |
| 127 | + }); |
| 128 | +}); |
| 129 | + |
| 130 | +describe("isValidMessageLength", () => { |
| 131 | + it("returns true for messages under limit", () => { |
| 132 | + expect(isValidMessageLength("Hello")).toBe(true); |
| 133 | + }); |
| 134 | + |
| 135 | + it("returns true for messages at limit", () => { |
| 136 | + const message = "a".repeat(MAX_MESSAGE_LENGTH); |
| 137 | + expect(isValidMessageLength(message)).toBe(true); |
| 138 | + }); |
| 139 | + |
| 140 | + it("returns false for messages over limit", () => { |
| 141 | + const message = "a".repeat(MAX_MESSAGE_LENGTH + 1); |
| 142 | + expect(isValidMessageLength(message)).toBe(false); |
| 143 | + }); |
| 144 | + |
| 145 | + it("returns true for empty string", () => { |
| 146 | + expect(isValidMessageLength("")).toBe(true); |
| 147 | + }); |
| 148 | + |
| 149 | + it("returns false for non-string input", () => { |
| 150 | + expect(isValidMessageLength(123 as unknown as string)).toBe(false); |
| 151 | + }); |
| 152 | +}); |
| 153 | + |
| 154 | +describe("sanitizeMessageContent", () => { |
| 155 | + it("trims whitespace", () => { |
| 156 | + expect(sanitizeMessageContent(" hello ")).toBe("hello"); |
| 157 | + }); |
| 158 | + |
| 159 | + it("truncates messages over limit", () => { |
| 160 | + const message = "a".repeat(MAX_MESSAGE_LENGTH + 100); |
| 161 | + const result = sanitizeMessageContent(message); |
| 162 | + expect(result.length).toBe(MAX_MESSAGE_LENGTH); |
| 163 | + }); |
| 164 | + |
| 165 | + it("returns empty string for null input", () => { |
| 166 | + expect(sanitizeMessageContent(null as unknown as string)).toBe(""); |
| 167 | + }); |
| 168 | + |
| 169 | + it("returns empty string for undefined input", () => { |
| 170 | + expect(sanitizeMessageContent(undefined as unknown as string)).toBe(""); |
| 171 | + }); |
| 172 | + |
| 173 | + it("returns empty string for non-string input", () => { |
| 174 | + expect(sanitizeMessageContent(123 as unknown as string)).toBe(""); |
| 175 | + }); |
| 176 | + |
| 177 | + it("preserves valid message content", () => { |
| 178 | + expect(sanitizeMessageContent("Hello, world!")).toBe("Hello, world!"); |
| 179 | + }); |
| 180 | +}); |
0 commit comments