Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export async function exportHarWithRenderedRequest(
renderedRequest: RenderedRequest,
addContentLength: boolean = false,
): Promise {
const url = smartEncodeUrl(renderedRequest.url, renderedRequest.settingEncodeUrl);
if (addContentLength) {
const hasContentLengthHeader =
misc.filterHeaders(renderedRequest.headers, 'Content-Length').length > 0;
if (!hasContentLengthHeader) {
const name = 'Content-Length';
const value = Buffer.byteLength((renderedRequest.body || {}).text || '').toString();
renderedRequest.headers.push({ name, value });
}
}
// Set auth header if we have it
if (!misc.hasAuthHeader(renderedRequest.headers)) {
const header = await getAuthHeader(renderedRequest, url);
if (header) {
let result;
try {
result = await props.handleRender({
url: request.url,
parameters: enabledParameters,
});
} catch (err) {
// Just ignore failures
}
if (result) {
const { url, parameters } = result;
const qs = buildQueryStringFromParams(parameters);
const fullUrl = joinUrlAndQueryString(url, qs);
this.setState({
string: smartEncodeUrl(fullUrl, request.settingEncodeUrl),
});
}
}
return 0;
}
addTimeline(name, content);
return 0; // Must be here
});
// Set the headers (to be modified as we go)
const headers = clone(renderedRequest.headers);
// Set the URL, including the query parameters
const qs = buildQueryStringFromParams(renderedRequest.parameters);
const url = joinUrlAndQueryString(renderedRequest.url, qs);
const isUnixSocket = url.match(/https?:\/\/unix:\//);
const finalUrl = smartEncodeUrl(url, renderedRequest.settingEncodeUrl);
if (isUnixSocket) {
// URL prep will convert "unix:/path" hostname to "unix/path"
const match = finalUrl.match(/(https?:)\/\/unix:?(\/[^:]+):\/(.+)/);
const protocol = (match && match[1]) || '';
const socketPath = (match && match[2]) || '';
const socketUrl = (match && match[3]) || '';
curl.setUrl(`${protocol}//${socketUrl}`);
setOpt(Curl.option.UNIX_SOCKET_PATH, socketPath);
} else {
curl.setUrl(finalUrl);
}
addTimelineText('Preparing request to ' + finalUrl);
addTimelineText(`Using ${Curl.getVersion()}`);
addTimelineText('Current time is ' + new Date().toISOString());
// Set timeout
async function getRequestUrl(context, request) {
const url = await context.util.render(request.url);
const parameters = [];
for (const p of request.parameters) {
parameters.push({
name: await context.util.render(p.name),
value: await context.util.render(p.value),
});
}
const qs = buildQueryStringFromParams(parameters);
const finalUrl = joinUrlAndQueryString(url, qs);
return smartEncodeUrl(finalUrl, request.settingEncodeUrl);
}
module.exports = function (context) {
//handling for comma values because the gateway expects it to be percent encoded
context.request.getParameters().forEach( (entry) => {
context.request.setParameter(entry.name, entry.value.replace(/,/g, "%25252C"));
});
const qs = buildQueryStringFromParams(context.request.getParameters());
const fullUrl = joinUrlAndQueryString(context.request.getUrl(), qs);
const url = smartEncodeUrl(fullUrl, true);
const mastercard = context.request.getEnvironmentVariable('mastercard');
if (mastercard) {
try {
const p12Content = fs.readFileSync(mastercard.keystoreP12Path, 'binary');
const p12Asn1 = forge.asn1.fromDer(p12Content, false);
const p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, false, mastercard.keystorePassword);
const keyObj = p12.getBags({
friendlyName: mastercard.keyAlias,
bagType: forge.pki.oids.pkcs8ShroudedKeyBag
}).friendlyName[0];
const signingKey = forge.pki.privateKeyToPem(keyObj.key);
const authHeader = oauth.getAuthorizationHeader(URL.parse(url), context.request.getMethod(), context.request.getBodyText(), mastercard.consumerKey, signingKey);
context.request.setHeader('Authorization', authHeader);
} catch (err) {