|
1 | 1 | import { map as eventStreamMap } from 'event-stream'; |
2 | 2 | import FormData from 'form-data/lib/form_data'; |
3 | 3 | import { Param, PostDataCommon, Request as NpmHarRequest } from 'har-format'; |
4 | | -import { validateRequest } from 'har-validator-compiled'; |
5 | 4 | import { stringify as queryStringify } from 'querystring'; |
6 | 5 | import { format as urlFormat, parse as urlParse, UrlWithParsedQuery } from 'url'; |
7 | 6 |
|
@@ -38,6 +37,19 @@ type PostDataBase = PostDataCommon & { |
38 | 37 | params?: Param[]; |
39 | 38 | }; |
40 | 39 |
|
| 40 | +/** |
| 41 | + * Regex to match environment variables in the format `<<variable_name>>`. |
| 42 | + * This is used to identify if the request contains environment variables that need to be handled specially. |
| 43 | + */ |
| 44 | +const HOPP_ENVIRONMENT_REGEX = /(<<[a-zA-Z0-9-_]+>>)/g; |
| 45 | + |
| 46 | +/** |
| 47 | + * |
| 48 | + * @param str The string to test for environment variables. |
| 49 | + * @returns A boolean indicating whether the string contains environment variables. |
| 50 | + */ |
| 51 | +const isContainsEnvVariables = (str: string) => HOPP_ENVIRONMENT_REGEX.test(str); |
| 52 | + |
41 | 53 | export type HarRequest = Omit<NpmHarRequest, 'postData'> & { postData: PostDataBase }; |
42 | 54 |
|
43 | 55 | export interface RequestExtras { |
@@ -113,8 +125,13 @@ export class HTTPSnippet { |
113 | 125 | }, |
114 | 126 | }; |
115 | 127 |
|
116 | | - if (validateRequest(req)) { |
117 | | - this.requests.push(this.prepare(req)); |
| 128 | + // if the request has a url, prepare it |
| 129 | + // otherwise, skip it |
| 130 | + // Note: Validation for URLs (validateRequest ) containing `<<` is skipped to support secret environment variables |
| 131 | + // in code generation, as used in the Hoppscotch app. |
| 132 | + |
| 133 | + if (req.url) { |
| 134 | + this.requests.push(this.prepare(req as HarRequest)); |
118 | 135 | } |
119 | 136 | }); |
120 | 137 | } |
@@ -312,7 +329,16 @@ export class HTTPSnippet { |
312 | 329 | ...uriObj, |
313 | 330 | }); //? |
314 | 331 |
|
315 | | - const decodedFullUrl = decodeURIComponent(fullUrl); |
| 332 | + const decodedURL = decodeURIComponent(fullUrl); |
| 333 | + |
| 334 | + // if the URL contains environment variables, decode it without decoding the environment variables |
| 335 | + // this is to ensure that environment variables are not decoded and remain in the format `<<variable_name>>` |
| 336 | + // this is useful for code generation where environment variables are used to store sensitive information |
| 337 | + // such as API keys, secrets, etc. |
| 338 | + // if the URL does not contain environment variables, decode it normally |
| 339 | + const decodedFullUrl = isContainsEnvVariables(decodedURL) |
| 340 | + ? decodeURIComponent(fullUrl) |
| 341 | + : fullUrl; |
316 | 342 |
|
317 | 343 | return { |
318 | 344 | ...request, |
|
0 commit comments