|
| 1 | +import { assert } from "chai"; |
| 2 | +import { describe, it } from "mocha"; |
| 3 | +import { sanitizeUrl, cleanUpUrlParams, cleanUrlSensitiveDataFromQuery, cleanUrlSensitiveDataFromValue } from "./serviceWorker"; |
| 4 | + |
| 5 | +describe("serviceWorker", () => { |
| 6 | + |
| 7 | + describe("sanitizeUrl", () => { |
| 8 | + it("should remove sensitive data from query parameters", () => { |
| 9 | + const url = "https://example.com/path?client_secret=abc&token=xyz&other=123"; |
| 10 | + const sanitizedUrl = sanitizeUrl(url); |
| 11 | + assert.equal(sanitizedUrl, "https://example.com/path?client_secret=***&token=***&other=123"); |
| 12 | + }); |
| 13 | + |
| 14 | + it("should remove sensitive data from hash parameters", () => { |
| 15 | + const url = "https://example.com/path#client_secret=abc&token=xyz&other=123"; |
| 16 | + const sanitizedUrl = sanitizeUrl(url); |
| 17 | + assert.equal(sanitizedUrl, "https://example.com/path#client_secret=***&token=***&other=***"); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should handle URLs without sensitive data", () => { |
| 21 | + const url = "https://example.com/path?other=123"; |
| 22 | + const sanitizedUrl = sanitizeUrl(url); |
| 23 | + assert.equal(sanitizedUrl, "https://example.com/path?other=123"); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should handle URLs with only allowed parameters in hash", () => { |
| 27 | + const url = "https://example.com/path#state=abc&session_state=xyz&client_secret=abc"; |
| 28 | + const sanitizedUrl = sanitizeUrl(url); |
| 29 | + assert.equal(sanitizedUrl, "https://example.com/path#state=abc&session_state=xyz&client_secret=***"); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should handle null or undefined URLs", () => { |
| 33 | + assert.equal(sanitizeUrl(null), null); |
| 34 | + assert.equal(sanitizeUrl(undefined), undefined); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should handle empty URLs", () => { |
| 38 | + assert.equal(sanitizeUrl(""), ""); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + |
| 43 | + describe("cleanUpUrlParams", () => { |
| 44 | + it("should replace sensitive parameters with ***", () => { |
| 45 | + const url = "https://example.com/path#client_secret=abc&token=xyz&other=123"; |
| 46 | + const cleanedUrl = cleanUpUrlParams(url); |
| 47 | + assert.equal(cleanedUrl, "https://example.com/path#client_secret=***&token=***&other=***"); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should leave allowed parameters unchanged", () => { |
| 51 | + const url = "https://example.com/path#state=abc&session_state=xyz&client_secret=abc"; |
| 52 | + const cleanedUrl = cleanUpUrlParams(url); |
| 53 | + assert.equal(cleanedUrl, "https://example.com/path#state=abc&session_state=xyz&client_secret=***"); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should handle URLs without hash", () => { |
| 57 | + const url = "https://example.com/path"; |
| 58 | + const cleanedUrl = cleanUpUrlParams(url); |
| 59 | + assert.equal(cleanedUrl, "https://example.com/path"); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should handle null or undefined URLs", () => { |
| 63 | + assert.equal(cleanUpUrlParams(null), null); |
| 64 | + assert.equal(cleanUpUrlParams(undefined), undefined); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should handle empty URLs", () => { |
| 68 | + assert.equal(cleanUpUrlParams(""), ""); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe("cleanUrlSensitiveDataFromQuery", () => { |
| 73 | + it("should replace sensitive query parameters with ***", () => { |
| 74 | + const url = "https://example.com/path?client_secret=abc&token=xyz&other=123"; |
| 75 | + const cleanedUrl = cleanUrlSensitiveDataFromQuery(url); |
| 76 | + assert.equal(cleanedUrl, "https://example.com/path?client_secret=***&token=***&other=123"); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should handle URLs without query parameters", () => { |
| 80 | + const url = "https://example.com/path"; |
| 81 | + const cleanedUrl = cleanUrlSensitiveDataFromQuery(url); |
| 82 | + assert.equal(cleanedUrl, "https://example.com/path"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should handle null or undefined URLs", () => { |
| 86 | + assert.equal(cleanUrlSensitiveDataFromQuery(null), null); |
| 87 | + assert.equal(cleanUrlSensitiveDataFromQuery(undefined), undefined); |
| 88 | + }); |
| 89 | + |
| 90 | + it("should handle empty URLs", () => { |
| 91 | + assert.equal(cleanUrlSensitiveDataFromQuery(""), ""); |
| 92 | + }); |
| 93 | + |
| 94 | + it("should handle complex URLs with multiple parameters", () => { |
| 95 | + const url = "https://example.com/api/v1?client_secret=abc123&api_key=xyz789&user=john&password=pass123&normal=value"; |
| 96 | + const cleanedUrl = cleanUrlSensitiveDataFromQuery(url); |
| 97 | + assert.equal(cleanedUrl, "https://example.com/api/v1?client_secret=***&api_key=xyz789&user=***&password=***&normal=value"); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should handle URLs with encoded characters", () => { |
| 101 | + const url = "https://example.com/path?token=abc%26xyz&user_name=john%20doe"; |
| 102 | + const cleanedUrl = cleanUrlSensitiveDataFromQuery(url); |
| 103 | + assert.equal(cleanedUrl, "https://example.com/path?token=***&user_name=***"); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should handle special cases like access_token and user_name", () => { |
| 107 | + const url = "https://example.com/oauth?access_token=abc123&user_name=john"; |
| 108 | + const cleanedUrl = cleanUrlSensitiveDataFromQuery(url); |
| 109 | + assert.equal(cleanedUrl, "https://example.com/oauth?access_token=***&user_name=***"); |
| 110 | + }); |
| 111 | + |
| 112 | + it("should handle malformed URLs by using fallback mechanism", () => { |
| 113 | + const url = "invalid://url with spaces?token=abc"; |
| 114 | + const cleanedUrl = cleanUrlSensitiveDataFromQuery(url); |
| 115 | + // Should still sanitize using regex fallback |
| 116 | + assert.equal(cleanedUrl, "invalid://url with spaces?token=***"); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe("cleanUrlSensitiveDataFromValue", () => { |
| 121 | + it("should replace sensitive data in header values with ***", () => { |
| 122 | + const dataValue = "client_secret=abc&token=xyz&other=123"; |
| 123 | + const cleanedValue = cleanUrlSensitiveDataFromValue(dataValue); |
| 124 | + assert.equal(cleanedValue, "client_secret=***&token=***&other=123"); |
| 125 | + }); |
| 126 | + |
| 127 | + it("should handle values without sensitive data", () => { |
| 128 | + const dataValue = "other=123"; |
| 129 | + const cleanedValue = cleanUrlSensitiveDataFromValue(dataValue); |
| 130 | + assert.equal(cleanedValue, "other=123"); |
| 131 | + }); |
| 132 | + |
| 133 | + it("should handle null or undefined values", () => { |
| 134 | + assert.equal(cleanUrlSensitiveDataFromValue(null), null); |
| 135 | + assert.equal(cleanUrlSensitiveDataFromValue(undefined), undefined); |
| 136 | + }); |
| 137 | + |
| 138 | + it("should handle empty values", () => { |
| 139 | + assert.equal(cleanUrlSensitiveDataFromValue(""), ""); |
| 140 | + }); |
| 141 | + }); |
| 142 | +}); |
0 commit comments