|
1 | 1 | "use strict"; |
2 | | -var __importDefault = (this && this.__importDefault) || function (mod) { |
3 | | - return (mod && mod.__esModule) ? mod : { "default": mod }; |
4 | | -}; |
5 | 2 | Object.defineProperty(exports, "__esModule", { value: true }); |
6 | 3 | exports.ProcessURL = void 0; |
7 | | -var query_string_1 = __importDefault(require("query-string")); |
8 | 4 | var logger_1 = require("./logger"); |
9 | 5 | var ProcessURL = /** @class */ (function () { |
10 | 6 | function ProcessURL(request, debug) { |
@@ -35,88 +31,90 @@ var ProcessURL = /** @class */ (function () { |
35 | 31 | specialParameters: this.specialParameters, |
36 | 32 | }; |
37 | 33 | } |
38 | | - //Extract query string from this.path |
39 | | - function extractQueryString(path) { |
40 | | - var queryString; |
41 | | - if (path.includes("?")) { |
42 | | - queryString = path.split("?")[1]; |
43 | | - } |
44 | | - return queryString; |
45 | | - } |
46 | | - function formatQueryString(q) { |
47 | | - if (q) { |
48 | | - return query_string_1.default.parse(q, { sort: false }); |
49 | | - } |
50 | | - } |
51 | | - var unprocessedQueryString; |
52 | | - unprocessedQueryString = extractQueryString(this.path); |
53 | | - if (unprocessedQueryString) { |
54 | | - this.queryString = formatQueryString(unprocessedQueryString); |
55 | | - } |
56 | | - //Destructure special params from query string if they are present |
57 | | - var _a = this.queryString || {}, chCode = _a["ch-code"], chID = _a["ch-id"], chIDSignature = _a["ch-id-signature"], chPublicKey = _a["ch-public-key"], chRequested = _a["ch-requested"]; |
58 | | - //Override chCode value if the current one is unusable |
59 | | - if (!chCode || chCode === "undefined" || chCode === "null") { |
60 | | - chCode = ""; |
61 | | - } |
62 | | - this.specialParameters.chCode = chCode; |
63 | | - //Override chID value if the current one is unusable |
64 | | - if (!chID || chID === "undefined" || chID === "null") { |
65 | | - chID = ""; |
66 | | - } |
67 | | - this.specialParameters.chID = chID; |
68 | | - //Override chIDSignature value if the current one is unusable |
69 | | - if (!chIDSignature || |
70 | | - chIDSignature === "undefined" || |
71 | | - chIDSignature === "null") { |
72 | | - chIDSignature = ""; |
73 | | - } |
74 | | - this.specialParameters.chIDSignature = chIDSignature; |
75 | | - //Override chPublicKey value if the current one is unusable |
76 | | - if (!chPublicKey || chPublicKey === "undefined" || chPublicKey === "null") { |
77 | | - chPublicKey = ""; |
| 34 | + // Extract raw query string from path (preserving original encoding) |
| 35 | + if (this.path.includes("?")) { |
| 36 | + this.rawQueryString = this.path.split("?")[1]; |
78 | 37 | } |
79 | | - this.specialParameters.chPublicKey = chPublicKey; |
80 | | - //Override chRequested value if the current one is unusable |
81 | | - if (!chRequested || chRequested === "undefined" || chRequested === "null") { |
82 | | - chRequested = ""; |
83 | | - } |
84 | | - this.specialParameters.chRequested = chRequested; |
85 | | - // Process the query string |
86 | | - var processedQueryString = this.processQueryString(this.queryString); |
87 | | - //URL encode the targetURL to be used later in redirects |
88 | | - var targetURL; |
89 | | - //We no longer need the query string in the path |
90 | | - this.path = this.path.split("?")[0]; |
| 38 | + // Extract ch-* parameter values using regex (decode for actual use) |
| 39 | + var chCode = this.extractParamValue("ch-code"); |
| 40 | + var chID = this.extractParamValue("ch-id"); |
| 41 | + var chIDSignature = this.extractParamValue("ch-id-signature"); |
| 42 | + var chPublicKey = this.extractParamValue("ch-public-key"); |
| 43 | + var chRequested = this.extractParamValue("ch-requested"); |
| 44 | + // Set special parameters (with validation) |
| 45 | + this.specialParameters.chCode = this.sanitizeParam(chCode); |
| 46 | + this.specialParameters.chID = this.sanitizeParam(chID); |
| 47 | + this.specialParameters.chIDSignature = this.sanitizeParam(chIDSignature); |
| 48 | + this.specialParameters.chPublicKey = this.sanitizeParam(chPublicKey); |
| 49 | + this.specialParameters.chRequested = this.sanitizeParam(chRequested); |
| 50 | + // Remove ch-* params from query string while preserving everything else |
| 51 | + var processedQueryString = this.removeChParams(this.rawQueryString); |
| 52 | + // Extract path without query string |
| 53 | + var cleanPath = this.path.split("?")[0]; |
| 54 | + // Construct targetURL |
91 | 55 | if (processedQueryString) { |
92 | | - this.targetURL = encodeURIComponent("https://".concat(this.host).concat(this.path, "?").concat(processedQueryString)); |
| 56 | + this.targetURL = encodeURIComponent("https://".concat(this.host).concat(cleanPath, "?").concat(processedQueryString)); |
93 | 57 | } |
94 | 58 | else { |
95 | | - this.targetURL = encodeURIComponent("https://".concat(this.host).concat(this.path)); |
| 59 | + this.targetURL = encodeURIComponent("https://".concat(this.host).concat(cleanPath)); |
96 | 60 | } |
97 | 61 | return { |
98 | 62 | targetURL: this.targetURL, |
99 | 63 | specialParameters: this.specialParameters, |
100 | 64 | }; |
101 | 65 | }; |
102 | | - ProcessURL.prototype.processQueryString = function (queryString) { |
103 | | - var processedQueryString; |
104 | | - if (queryString) { |
105 | | - delete queryString["ch-code"]; |
106 | | - delete queryString["ch-fresh"]; |
107 | | - delete queryString["ch-id"]; |
108 | | - delete queryString["ch-id-signature"]; |
109 | | - delete queryString["ch-public-key"]; |
110 | | - delete queryString["ch-requested"]; |
111 | | - } |
112 | | - //Convert to usable querystring format |
113 | | - if (queryString && Object.keys(queryString).length !== 0) { |
114 | | - processedQueryString = query_string_1.default.stringify(queryString, { sort: false }); |
| 66 | + /** |
| 67 | + * Extract a parameter value from the raw query string using regex. |
| 68 | + * Decodes the value for actual use. |
| 69 | + */ |
| 70 | + ProcessURL.prototype.extractParamValue = function (paramName) { |
| 71 | + if (!this.rawQueryString) |
| 72 | + return ""; |
| 73 | + // Match the parameter in the query string |
| 74 | + var regex = new RegExp("(?:^|&)".concat(paramName, "=([^&]*)"), "i"); |
| 75 | + var match = this.rawQueryString.match(regex); |
| 76 | + if (match && match[1]) { |
| 77 | + try { |
| 78 | + return decodeURIComponent(match[1]); |
| 79 | + } |
| 80 | + catch (_a) { |
| 81 | + return match[1]; |
| 82 | + } |
115 | 83 | } |
116 | | - else { |
117 | | - processedQueryString = ""; |
| 84 | + return ""; |
| 85 | + }; |
| 86 | + /** |
| 87 | + * Sanitize a parameter value - return empty string for unusable values. |
| 88 | + */ |
| 89 | + ProcessURL.prototype.sanitizeParam = function (value) { |
| 90 | + if (!value || value === "undefined" || value === "null") { |
| 91 | + return ""; |
118 | 92 | } |
119 | | - return processedQueryString; |
| 93 | + return value; |
| 94 | + }; |
| 95 | + /** |
| 96 | + * Remove ch-* parameters from the query string while preserving |
| 97 | + * the original encoding of all other parameters. |
| 98 | + */ |
| 99 | + ProcessURL.prototype.removeChParams = function (queryString) { |
| 100 | + if (!queryString) |
| 101 | + return ""; |
| 102 | + // List of ch-* parameters to remove |
| 103 | + var chParams = [ |
| 104 | + "ch-code", |
| 105 | + "ch-fresh", |
| 106 | + "ch-id", |
| 107 | + "ch-id-signature", |
| 108 | + "ch-public-key", |
| 109 | + "ch-requested", |
| 110 | + ]; |
| 111 | + // Split into individual params, filter out ch-* params, rejoin |
| 112 | + var params = queryString.split("&"); |
| 113 | + var filteredParams = params.filter(function (param) { |
| 114 | + var key = param.split("=")[0]; |
| 115 | + return !chParams.includes(key.toLowerCase()); |
| 116 | + }); |
| 117 | + return filteredParams.join("&"); |
120 | 118 | }; |
121 | 119 | return ProcessURL; |
122 | 120 | }()); |
|
0 commit comments