Skip to content

Commit 2676dac

Browse files
committed
feat: secret env variable case handle
1 parent 40575c4 commit 2676dac

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

src/httpsnippet.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { map as eventStreamMap } from 'event-stream';
22
import FormData from 'form-data/lib/form_data';
33
import { Param, PostDataCommon, Request as NpmHarRequest } from 'har-format';
4-
import { validateRequest } from 'har-validator-compiled';
54
import { stringify as queryStringify } from 'querystring';
65
import { format as urlFormat, parse as urlParse, UrlWithParsedQuery } from 'url';
76

@@ -38,6 +37,19 @@ type PostDataBase = PostDataCommon & {
3837
params?: Param[];
3938
};
4039

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+
4153
export type HarRequest = Omit<NpmHarRequest, 'postData'> & { postData: PostDataBase };
4254

4355
export interface RequestExtras {
@@ -113,8 +125,13 @@ export class HTTPSnippet {
113125
},
114126
};
115127

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));
118135
}
119136
});
120137
}
@@ -312,7 +329,16 @@ export class HTTPSnippet {
312329
...uriObj,
313330
}); //?
314331

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;
316342

317343
return {
318344
...request,

0 commit comments

Comments
 (0)