|
1 | | -import { validateRedirectUrl } from "../urlValidation"; |
| 1 | +import { validateRedirectUrl, isPrivateUrl } from "../urlValidation"; |
2 | 2 |
|
3 | 3 | describe("validateRedirectUrl", () => { |
4 | 4 | describe("valid URLs", () => { |
@@ -132,4 +132,148 @@ describe("validateRedirectUrl", () => { |
132 | 132 | ); |
133 | 133 | }); |
134 | 134 | }); |
| 135 | + |
| 136 | + describe("SSRF protection", () => { |
| 137 | + it("should block localhost", () => { |
| 138 | + expect(() => validateRedirectUrl("http://localhost/callback")).toThrow( |
| 139 | + "private/internal address", |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + it("should block localhost with port", () => { |
| 144 | + expect(() => |
| 145 | + validateRedirectUrl("http://localhost:3000/callback"), |
| 146 | + ).toThrow("private/internal address"); |
| 147 | + }); |
| 148 | + |
| 149 | + it("should block 127.0.0.1", () => { |
| 150 | + expect(() => validateRedirectUrl("http://127.0.0.1/callback")).toThrow( |
| 151 | + "private/internal address", |
| 152 | + ); |
| 153 | + }); |
| 154 | + |
| 155 | + it("should block 127.x.x.x range", () => { |
| 156 | + expect(() => validateRedirectUrl("http://127.0.0.2:8080/")).toThrow( |
| 157 | + "private/internal address", |
| 158 | + ); |
| 159 | + }); |
| 160 | + |
| 161 | + it("should block private 10.x.x.x range", () => { |
| 162 | + expect(() => validateRedirectUrl("http://10.0.0.1/callback")).toThrow( |
| 163 | + "private/internal address", |
| 164 | + ); |
| 165 | + expect(() => validateRedirectUrl("http://10.255.255.255/")).toThrow( |
| 166 | + "private/internal address", |
| 167 | + ); |
| 168 | + }); |
| 169 | + |
| 170 | + it("should block private 172.16-31.x.x range", () => { |
| 171 | + expect(() => validateRedirectUrl("http://172.16.0.1/callback")).toThrow( |
| 172 | + "private/internal address", |
| 173 | + ); |
| 174 | + expect(() => validateRedirectUrl("http://172.31.255.255/")).toThrow( |
| 175 | + "private/internal address", |
| 176 | + ); |
| 177 | + }); |
| 178 | + |
| 179 | + it("should allow non-private 172.x.x.x", () => { |
| 180 | + // 172.15.x.x and 172.32.x.x are public |
| 181 | + expect(() => |
| 182 | + validateRedirectUrl("http://172.15.0.1/callback"), |
| 183 | + ).not.toThrow(); |
| 184 | + expect(() => |
| 185 | + validateRedirectUrl("http://172.32.0.1/callback"), |
| 186 | + ).not.toThrow(); |
| 187 | + }); |
| 188 | + |
| 189 | + it("should block private 192.168.x.x range", () => { |
| 190 | + expect(() => validateRedirectUrl("http://192.168.0.1/callback")).toThrow( |
| 191 | + "private/internal address", |
| 192 | + ); |
| 193 | + expect(() => validateRedirectUrl("http://192.168.255.255/")).toThrow( |
| 194 | + "private/internal address", |
| 195 | + ); |
| 196 | + }); |
| 197 | + |
| 198 | + it("should block link-local 169.254.x.x", () => { |
| 199 | + expect(() => validateRedirectUrl("http://169.254.1.1/callback")).toThrow( |
| 200 | + "private/internal address", |
| 201 | + ); |
| 202 | + }); |
| 203 | + |
| 204 | + it("should block AWS/GCP metadata endpoint 169.254.169.254", () => { |
| 205 | + expect(() => |
| 206 | + validateRedirectUrl("http://169.254.169.254/latest/meta-data/"), |
| 207 | + ).toThrow("private/internal address"); |
| 208 | + }); |
| 209 | + |
| 210 | + it("should block IPv6 localhost [::1]", () => { |
| 211 | + expect(() => validateRedirectUrl("http://[::1]/callback")).toThrow( |
| 212 | + "private/internal address", |
| 213 | + ); |
| 214 | + }); |
| 215 | + |
| 216 | + it("should block IPv6 link-local [fe80::]", () => { |
| 217 | + expect(() => validateRedirectUrl("http://[fe80::1]/callback")).toThrow( |
| 218 | + "private/internal address", |
| 219 | + ); |
| 220 | + }); |
| 221 | + |
| 222 | + it("should allow private IPs with allowPrivateIPs option", () => { |
| 223 | + expect(() => |
| 224 | + validateRedirectUrl("http://localhost/callback", { |
| 225 | + allowPrivateIPs: true, |
| 226 | + }), |
| 227 | + ).not.toThrow(); |
| 228 | + expect(() => |
| 229 | + validateRedirectUrl("http://127.0.0.1/callback", { |
| 230 | + allowPrivateIPs: true, |
| 231 | + }), |
| 232 | + ).not.toThrow(); |
| 233 | + expect(() => |
| 234 | + validateRedirectUrl("http://192.168.1.1/callback", { |
| 235 | + allowPrivateIPs: true, |
| 236 | + }), |
| 237 | + ).not.toThrow(); |
| 238 | + }); |
| 239 | + |
| 240 | + it("should allow public IPs", () => { |
| 241 | + expect(() => |
| 242 | + validateRedirectUrl("https://api.example.com/callback"), |
| 243 | + ).not.toThrow(); |
| 244 | + expect(() => |
| 245 | + validateRedirectUrl("https://8.8.8.8/callback"), |
| 246 | + ).not.toThrow(); |
| 247 | + expect(() => |
| 248 | + validateRedirectUrl("https://1.1.1.1/callback"), |
| 249 | + ).not.toThrow(); |
| 250 | + }); |
| 251 | + }); |
| 252 | +}); |
| 253 | + |
| 254 | +describe("isPrivateUrl", () => { |
| 255 | + it("should return true for localhost", () => { |
| 256 | + expect(isPrivateUrl("http://localhost/")).toBe(true); |
| 257 | + }); |
| 258 | + |
| 259 | + it("should return true for private IPs", () => { |
| 260 | + expect(isPrivateUrl("http://127.0.0.1/")).toBe(true); |
| 261 | + expect(isPrivateUrl("http://10.0.0.1/")).toBe(true); |
| 262 | + expect(isPrivateUrl("http://192.168.1.1/")).toBe(true); |
| 263 | + expect(isPrivateUrl("http://172.16.0.1/")).toBe(true); |
| 264 | + }); |
| 265 | + |
| 266 | + it("should return true for metadata endpoints", () => { |
| 267 | + expect(isPrivateUrl("http://169.254.169.254/")).toBe(true); |
| 268 | + }); |
| 269 | + |
| 270 | + it("should return false for public IPs", () => { |
| 271 | + expect(isPrivateUrl("https://example.com/")).toBe(false); |
| 272 | + expect(isPrivateUrl("https://8.8.8.8/")).toBe(false); |
| 273 | + expect(isPrivateUrl("https://api.github.com/")).toBe(false); |
| 274 | + }); |
| 275 | + |
| 276 | + it("should return false for invalid URLs", () => { |
| 277 | + expect(isPrivateUrl("not a url")).toBe(false); |
| 278 | + }); |
135 | 279 | }); |
0 commit comments