Path: blob/main/components/dashboard/src/utils.test.ts
2498 views
/**1* Copyright (c) 2022 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { inResource, getURLHash, isTrustedUrlOrPath } from "./utils";78test("inResource", () => {9// Given root path is a part of resources specified10expect(inResource("/app", ["new", "app", "teams"])).toBe(true);1112// Given path is a part of resources specified13expect(inResource("/app/testing", ["new", "app", "teams"])).toBe(true);1415// Empty resources16expect(inResource("/just/a/path", [])).toBe(false);1718// Both resources starting with '/'19expect(inResource("/app", ["/app"])).toBe(true);2021// Both resources ending with '/'22expect(inResource("app/", ["app/"])).toBe(true);2324// Both resources containing path with subdirectories25expect(inResource("/admin/teams/someTeam/somePerson", ["/admin/teams"])).toBe(true);26});2728test("urlHash and isTrustedUrlOrPath", () => {29global.window = Object.create(window);30Object.defineProperty(window, "location", {31value: {32hash: "#https://example.org/user/repo",33hostname: "example.org",34},35});3637expect(getURLHash()).toBe("https://example.org/user/repo");3839const isTrustedUrlOrPathCases: { location: string; trusted: boolean }[] = [40{ location: "https://example.org/user/repo", trusted: true },41{ location: "https://example.org/user", trusted: true },42{ location: "https://example2.org/user", trusted: false },43{ location: "/api/hello", trusted: true },44{ location: "/", trusted: true },45// eslint-disable-next-line no-script-url46{ location: "javascript:alert(1)", trusted: false },47// XSS bypass attempt with javascript: protocol and matching hostname48// eslint-disable-next-line no-script-url49{ location: "javascript://example.org/%250aalert(1)", trusted: false },50// Other protocol attempts51{ location: "data:text/html,<script>alert(1)</script>", trusted: false },52{ location: "vbscript:alert(1)", trusted: false },53];54isTrustedUrlOrPathCases.forEach(({ location, trusted }) => {55expect(isTrustedUrlOrPath(location)).toBe(trusted);56});57});585960