|
| 1 | +import { test } from "tap"; |
| 2 | +import { validateRedirectUrl } from "../../src/app/utils/redirect.js"; |
| 3 | +import config from "../../src/config.js"; |
| 4 | + |
| 5 | +const original = config.allowed_redirects; |
| 6 | + |
| 7 | +test("redirect validation - exact match", async (t) => { |
| 8 | + config.allowed_redirects = ["http://localhost:3000/demo"]; |
| 9 | + |
| 10 | + t.after(() => { |
| 11 | + config.allowed_redirects = original; |
| 12 | + }); |
| 13 | + |
| 14 | + t.equal( |
| 15 | + validateRedirectUrl("http://localhost:3000/demo"), |
| 16 | + "http://localhost:3000/demo", |
| 17 | + ); |
| 18 | + t.equal(validateRedirectUrl("http://evil.com/steal"), "/profile"); |
| 19 | + t.equal(validateRedirectUrl(""), "/profile"); |
| 20 | + t.equal(validateRedirectUrl(null), "/profile"); |
| 21 | + t.equal(validateRedirectUrl(" "), "/profile"); |
| 22 | +}); |
| 23 | + |
| 24 | +test("allows subdomain wildcard", async (t) => { |
| 25 | + config.allowed_redirects = ["https://*.codebar.io"]; |
| 26 | + |
| 27 | + t.after(() => { |
| 28 | + config.allowed_redirects = original; |
| 29 | + }); |
| 30 | + |
| 31 | + t.equal( |
| 32 | + validateRedirectUrl("https://auth.codebar.io"), |
| 33 | + "https://auth.codebar.io", |
| 34 | + ); |
| 35 | + t.equal( |
| 36 | + validateRedirectUrl("https://staging.codebar.io"), |
| 37 | + "https://staging.codebar.io", |
| 38 | + ); |
| 39 | + t.equal( |
| 40 | + validateRedirectUrl("https://app.codebar.io"), |
| 41 | + "https://app.codebar.io", |
| 42 | + ); |
| 43 | +}); |
| 44 | + |
| 45 | +test("rejects non-matching domain with wildcard", async (t) => { |
| 46 | + config.allowed_redirects = ["https://*.codebar.io"]; |
| 47 | + |
| 48 | + t.after(() => { |
| 49 | + config.allowed_redirects = original; |
| 50 | + }); |
| 51 | + |
| 52 | + t.equal(validateRedirectUrl("https://codebar.io"), "/profile"); |
| 53 | + t.equal(validateRedirectUrl("https://evil.com"), "/profile"); |
| 54 | +}); |
| 55 | + |
| 56 | +test("allows path wildcard", async (t) => { |
| 57 | + config.allowed_redirects = ["https://codebar.io/*"]; |
| 58 | + |
| 59 | + t.after(() => { |
| 60 | + config.allowed_redirects = original; |
| 61 | + }); |
| 62 | + |
| 63 | + t.equal( |
| 64 | + validateRedirectUrl("https://codebar.io/profile"), |
| 65 | + "https://codebar.io/profile", |
| 66 | + ); |
| 67 | + t.equal( |
| 68 | + validateRedirectUrl("https://codebar.io/anything/here"), |
| 69 | + "https://codebar.io/anything/here", |
| 70 | + ); |
| 71 | +}); |
| 72 | + |
| 73 | +test("allows multiple wildcards", async (t) => { |
| 74 | + config.allowed_redirects = ["https://*.example.com/*"]; |
| 75 | + |
| 76 | + t.after(() => { |
| 77 | + config.allowed_redirects = original; |
| 78 | + }); |
| 79 | + |
| 80 | + t.equal( |
| 81 | + validateRedirectUrl("https://auth.example.com/page"), |
| 82 | + "https://auth.example.com/page", |
| 83 | + ); |
| 84 | + t.equal( |
| 85 | + validateRedirectUrl("https://api.example.com/v1/users"), |
| 86 | + "https://api.example.com/v1/users", |
| 87 | + ); |
| 88 | +}); |
| 89 | + |
| 90 | +test("exact match takes precedence over wildcard", async (t) => { |
| 91 | + config.allowed_redirects = [ |
| 92 | + "http://localhost:3000/demo", |
| 93 | + "http://localhost:3000/*", |
| 94 | + ]; |
| 95 | + |
| 96 | + t.after(() => { |
| 97 | + config.allowed_redirects = original; |
| 98 | + }); |
| 99 | + |
| 100 | + t.equal( |
| 101 | + validateRedirectUrl("http://localhost:3000/demo"), |
| 102 | + "http://localhost:3000/demo", |
| 103 | + ); |
| 104 | +}); |
0 commit comments