|
| 1 | +/** |
| 2 | + * Copyright (c) 2024 Gitpod GmbH. All rights reserved. |
| 3 | + * Licensed under the GNU Affero General Public License (AGPL). |
| 4 | + * See License.AGPL.txt in the project root for license information. |
| 5 | + */ |
| 6 | + |
| 7 | +import { expect } from "chai"; |
| 8 | +import { GitpodHostUrl } from "@gitpod/gitpod-protocol/lib/util/gitpod-host-url"; |
| 9 | +import { validateLoginReturnToUrl, validateAuthorizeReturnToUrl } from "./authenticator"; |
| 10 | + |
| 11 | +describe("ReturnTo URL Validation", () => { |
| 12 | + const hostUrl = new GitpodHostUrl("https://gitpod.io"); |
| 13 | + |
| 14 | + describe("validateLoginReturnToUrl", () => { |
| 15 | + it("should accept any valid path after domain validation", () => { |
| 16 | + const validUrls = [ |
| 17 | + "https://gitpod.io/", |
| 18 | + "https://gitpod.io/workspaces", |
| 19 | + "https://gitpod.io/workspaces/123", |
| 20 | + "https://gitpod.io/settings", |
| 21 | + "https://gitpod.io/settings/integrations", |
| 22 | + "https://gitpod.io/user-settings", |
| 23 | + "https://gitpod.io/user-settings/account", |
| 24 | + "https://gitpod.io/integrations", |
| 25 | + "https://gitpod.io/integrations/github", |
| 26 | + "https://gitpod.io/repositories", |
| 27 | + "https://gitpod.io/repositories/123", |
| 28 | + "https://gitpod.io/prebuilds", |
| 29 | + "https://gitpod.io/prebuilds/456", |
| 30 | + "https://gitpod.io/members", |
| 31 | + "https://gitpod.io/billing", |
| 32 | + "https://gitpod.io/usage", |
| 33 | + "https://gitpod.io/insights", |
| 34 | + "https://gitpod.io/new", |
| 35 | + "https://gitpod.io/login", |
| 36 | + "https://gitpod.io/login/org-slug", |
| 37 | + "https://gitpod.io/quickstart", |
| 38 | + "https://gitpod.io/admin", // Now allowed since login doesn't restrict paths |
| 39 | + "https://gitpod.io/api/workspace", // Now allowed |
| 40 | + "https://gitpod.io/any-path", // Any path is allowed |
| 41 | + ]; |
| 42 | + |
| 43 | + validUrls.forEach((url) => { |
| 44 | + const result = validateLoginReturnToUrl(url, hostUrl); |
| 45 | + expect(result).to.equal(true, `Should accept valid login URL: ${url}`); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + it("should accept complete-auth with ONLY message parameter", () => { |
| 50 | + const validCompleteAuthUrls = [ |
| 51 | + "https://gitpod.io/complete-auth?message=success:123", |
| 52 | + "https://gitpod.io/complete-auth?message=success", |
| 53 | + ]; |
| 54 | + |
| 55 | + validCompleteAuthUrls.forEach((url) => { |
| 56 | + const result = validateLoginReturnToUrl(url, hostUrl); |
| 57 | + expect(result).to.equal(true, `Should accept complete-auth with only message: ${url}`); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it("should reject complete-auth with additional parameters", () => { |
| 62 | + const invalidCompleteAuthUrls = [ |
| 63 | + "https://gitpod.io/complete-auth?message=success&other=param", |
| 64 | + "https://gitpod.io/complete-auth?other=param&message=success", |
| 65 | + "https://gitpod.io/complete-auth", |
| 66 | + "https://gitpod.io/complete-auth?other=param", |
| 67 | + "https://gitpod.io/complete-auth?msg=success", // Wrong parameter name |
| 68 | + ]; |
| 69 | + |
| 70 | + invalidCompleteAuthUrls.forEach((url) => { |
| 71 | + const result = validateLoginReturnToUrl(url, hostUrl); |
| 72 | + expect(result).to.equal(false, `Should reject complete-auth with extra params: ${url}`); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should accept www.gitpod.io root only", () => { |
| 77 | + const result = validateLoginReturnToUrl("https://www.gitpod.io/", hostUrl); |
| 78 | + expect(result).to.equal(true, "Should accept www.gitpod.io root"); |
| 79 | + |
| 80 | + const invalidWwwUrls = [ |
| 81 | + "https://www.gitpod.io/workspaces", |
| 82 | + "https://www.gitpod.io/login", |
| 83 | + "http://www.gitpod.io/", // Wrong protocol |
| 84 | + ]; |
| 85 | + |
| 86 | + invalidWwwUrls.forEach((url) => { |
| 87 | + const result = validateLoginReturnToUrl(url, hostUrl); |
| 88 | + expect(result).to.equal(false, `Should reject www.gitpod.io non-root: ${url}`); |
| 89 | + }); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe("validateAuthorizeReturnToUrl", () => { |
| 94 | + it("should accept only specific allowlisted paths", () => { |
| 95 | + const validUrls = [ |
| 96 | + "https://gitpod.io/", // Root |
| 97 | + "https://gitpod.io/new", // Create workspace page |
| 98 | + "https://gitpod.io/quickstart", // Quickstart page |
| 99 | + ]; |
| 100 | + |
| 101 | + validUrls.forEach((url) => { |
| 102 | + const result = validateAuthorizeReturnToUrl(url, hostUrl); |
| 103 | + expect(result).to.equal(true, `Should accept valid authorize URL: ${url}`); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + it("should accept complete-auth with ONLY message parameter", () => { |
| 108 | + const validCompleteAuthUrls = [ |
| 109 | + "https://gitpod.io/complete-auth?message=success:123", |
| 110 | + "https://gitpod.io/complete-auth?message=success", |
| 111 | + ]; |
| 112 | + |
| 113 | + validCompleteAuthUrls.forEach((url) => { |
| 114 | + const result = validateAuthorizeReturnToUrl(url, hostUrl); |
| 115 | + expect(result).to.equal(true, `Should accept complete-auth with only message: ${url}`); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + it("should reject paths not in authorize allowlist", () => { |
| 120 | + const rejectedUrls = [ |
| 121 | + "https://gitpod.io/workspaces", |
| 122 | + "https://gitpod.io/workspaces/123", |
| 123 | + "https://gitpod.io/user-settings", |
| 124 | + "https://gitpod.io/integrations", |
| 125 | + "https://gitpod.io/prebuilds", |
| 126 | + "https://gitpod.io/members", |
| 127 | + "https://gitpod.io/billing", |
| 128 | + "https://gitpod.io/usage", |
| 129 | + "https://gitpod.io/insights", |
| 130 | + "https://gitpod.io/login", |
| 131 | + "https://gitpod.io/settings", |
| 132 | + "https://gitpod.io/repositories", |
| 133 | + "https://gitpod.io/admin", |
| 134 | + "https://gitpod.io/api/workspace", |
| 135 | + ]; |
| 136 | + |
| 137 | + rejectedUrls.forEach((url) => { |
| 138 | + const result = validateAuthorizeReturnToUrl(url, hostUrl); |
| 139 | + expect(result).to.equal(false, `Should reject authorize URL: ${url}`); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + it("should accept www.gitpod.io root only", () => { |
| 144 | + const result = validateAuthorizeReturnToUrl("https://www.gitpod.io/", hostUrl); |
| 145 | + expect(result).to.equal(true, "Should accept www.gitpod.io root"); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + describe("Common validation tests", () => { |
| 150 | + it("should reject different origins", () => { |
| 151 | + const invalidOriginUrls = [ |
| 152 | + "https://evil.com/workspaces", |
| 153 | + "http://gitpod.io/workspaces", // Different protocol |
| 154 | + "https://gitpod.io:8080/workspaces", // Different port |
| 155 | + "https://subdomain.gitpod.io/workspaces", // Different subdomain |
| 156 | + ]; |
| 157 | + |
| 158 | + invalidOriginUrls.forEach((url) => { |
| 159 | + expect(validateLoginReturnToUrl(url, hostUrl)).to.equal(false, `Login should reject: ${url}`); |
| 160 | + expect(validateAuthorizeReturnToUrl(url, hostUrl)).to.equal(false, `Authorize should reject: ${url}`); |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + it("should have different path restrictions for login vs authorize", () => { |
| 165 | + const pathsAllowedInLoginOnly = [ |
| 166 | + "https://gitpod.io/admin", |
| 167 | + "https://gitpod.io/workspace-123", |
| 168 | + "https://gitpod.io/api/workspace", |
| 169 | + "https://gitpod.io/workspaces", |
| 170 | + "https://gitpod.io/settings", |
| 171 | + "https://gitpod.io/any-arbitrary-path", |
| 172 | + ]; |
| 173 | + |
| 174 | + pathsAllowedInLoginOnly.forEach((url) => { |
| 175 | + expect(validateLoginReturnToUrl(url, hostUrl)).to.equal(true, `Login should allow: ${url}`); |
| 176 | + expect(validateAuthorizeReturnToUrl(url, hostUrl)).to.equal(false, `Authorize should reject: ${url}`); |
| 177 | + }); |
| 178 | + }); |
| 179 | + |
| 180 | + it("should reject invalid URLs", () => { |
| 181 | + const invalidUrls = [ |
| 182 | + "not-a-url", |
| 183 | + "javascript:alert('xss')", |
| 184 | + "data:text/html,<script>alert('xss')</script>", |
| 185 | + "", |
| 186 | + "//evil.com/workspaces", |
| 187 | + "ftp://gitpod.io/workspaces", |
| 188 | + ]; |
| 189 | + |
| 190 | + invalidUrls.forEach((url) => { |
| 191 | + expect(validateLoginReturnToUrl(url, hostUrl)).to.equal(false, `Login should reject: ${url}`); |
| 192 | + expect(validateAuthorizeReturnToUrl(url, hostUrl)).to.equal(false, `Authorize should reject: ${url}`); |
| 193 | + }); |
| 194 | + }); |
| 195 | + |
| 196 | + it("should work with different host configurations", () => { |
| 197 | + const previewHostUrl = new GitpodHostUrl("https://preview.gitpod-dev.com"); |
| 198 | + |
| 199 | + // Login should accept any path on same origin |
| 200 | + const validLoginUrls = [ |
| 201 | + "https://preview.gitpod-dev.com/workspaces", |
| 202 | + "https://preview.gitpod-dev.com/complete-auth?message=success", |
| 203 | + "https://preview.gitpod-dev.com/any-path", |
| 204 | + ]; |
| 205 | + |
| 206 | + validLoginUrls.forEach((url) => { |
| 207 | + expect(validateLoginReturnToUrl(url, previewHostUrl)).to.equal(true, `Login should accept: ${url}`); |
| 208 | + }); |
| 209 | + |
| 210 | + // Authorize should only accept allowlisted paths |
| 211 | + const validAuthorizeUrls = [ |
| 212 | + "https://preview.gitpod-dev.com/", // Root |
| 213 | + "https://preview.gitpod-dev.com/new", // Create workspace page |
| 214 | + "https://preview.gitpod-dev.com/quickstart", // Quickstart page |
| 215 | + "https://preview.gitpod-dev.com/complete-auth?message=success", |
| 216 | + ]; |
| 217 | + |
| 218 | + validAuthorizeUrls.forEach((url) => { |
| 219 | + expect(validateAuthorizeReturnToUrl(url, previewHostUrl)).to.equal( |
| 220 | + true, |
| 221 | + `Authorize should accept: ${url}`, |
| 222 | + ); |
| 223 | + }); |
| 224 | + |
| 225 | + // Should reject /workspaces for authorize since it's not in allowlist |
| 226 | + expect(validateAuthorizeReturnToUrl("https://preview.gitpod-dev.com/workspaces", previewHostUrl)).to.equal( |
| 227 | + false, |
| 228 | + ); |
| 229 | + |
| 230 | + // Should reject URLs for different hosts |
| 231 | + expect(validateLoginReturnToUrl("https://gitpod.io/workspaces", previewHostUrl)).to.equal(false); |
| 232 | + expect(validateAuthorizeReturnToUrl("https://gitpod.io/workspaces", previewHostUrl)).to.equal(false); |
| 233 | + }); |
| 234 | + |
| 235 | + it("should validate www.gitpod.io specifically", () => { |
| 236 | + // Test with production gitpod.io |
| 237 | + const prodHostUrl = new GitpodHostUrl("https://gitpod.io"); |
| 238 | + expect(validateLoginReturnToUrl("https://www.gitpod.io/", prodHostUrl)).to.equal(true); |
| 239 | + expect(validateAuthorizeReturnToUrl("https://www.gitpod.io/", prodHostUrl)).to.equal(true); |
| 240 | + |
| 241 | + // Test with different deployment - www.gitpod.io should still work as it's hardcoded |
| 242 | + const customHostUrl = new GitpodHostUrl("https://gitpod.example.com"); |
| 243 | + expect(validateLoginReturnToUrl("https://www.gitpod.io/", customHostUrl)).to.equal(true); |
| 244 | + expect(validateAuthorizeReturnToUrl("https://www.gitpod.io/", customHostUrl)).to.equal(true); |
| 245 | + |
| 246 | + // Test that other www subdomains don't work |
| 247 | + expect(validateLoginReturnToUrl("https://www.gitpod.example.com/", prodHostUrl)).to.equal(false); |
| 248 | + expect(validateAuthorizeReturnToUrl("https://www.gitpod.example.com/", prodHostUrl)).to.equal(false); |
| 249 | + |
| 250 | + // Test domain injection prevention |
| 251 | + expect(validateLoginReturnToUrl("https://www.gitpod.io.evil.com/", prodHostUrl)).to.equal(false); |
| 252 | + expect(validateAuthorizeReturnToUrl("https://www.gitpod.io.evil.com/", prodHostUrl)).to.equal(false); |
| 253 | + }); |
| 254 | + }); |
| 255 | +}); |
0 commit comments