Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Remove disabled headers
renderedRequest.headers = renderedRequest.headers.filter(p => !p.disabled);
// Remove disabled body params
if (renderedRequest.body && Array.isArray(renderedRequest.body.params)) {
renderedRequest.body.params = renderedRequest.body.params.filter(p => !p.disabled);
}
// Remove disabled authentication
if (renderedRequest.authentication && renderedRequest.authentication.disabled) {
renderedRequest.authentication = {};
}
// Default the proto if it doesn't exist
renderedRequest.url = setDefaultProtocol(renderedRequest.url);
return {
context: renderContext,
request: {
// Add the yummy cookies
cookieJar: renderedCookieJar,
cookies: [],
isPrivate: false,
// NOTE: Flow doesn't like Object.assign, so we have to do each property manually
// for now to convert Request to RenderedRequest.
_id: renderedRequest._id,
authentication: renderedRequest.authentication,
body: renderedRequest.body,
created: renderedRequest.created,
modified: renderedRequest.modified,
export function urlMatchesCertHost(certificateHost, requestUrl) {
const cHostWithProtocol = setDefaultProtocol(certificateHost, 'https:');
const { hostname, port } = urlParse(requestUrl);
const { hostname: cHostname, port: cPort } = certificateUrlParse(cHostWithProtocol);
const assumedPort = parseInt(port) || DEFAULT_PORT;
const assumedCPort = parseInt(cPort) || DEFAULT_PORT;
const cHostnameRegex = escapeRegex(cHostname || '').replace(/\\\*/g, '.*');
const cPortRegex = escapeRegex(cPort || '').replace(/\\\*/g, '.*');
// Check ports
if ((cPort + '').includes('*')) {
if (!(port || '').match(`^${cPortRegex}$`)) {
return false;
}
} else {
if (assumedCPort !== assumedPort) {
}
addTimelineText(
'Enable cookie sending with jar of ' +
`${cookies.length} cookie${cookies.length !== 1 ? 's' : ''}`,
);
} else {
addTimelineText('Disable cookie sending due to user setting');
}
// Set proxy settings if we have them
if (settings.proxyEnabled) {
const { protocol } = urlParse(renderedRequest.url);
const { httpProxy, httpsProxy, noProxy } = settings;
const proxyHost = protocol === 'https:' ? httpsProxy : httpProxy;
const proxy = proxyHost ? setDefaultProtocol(proxyHost) : null;
addTimelineText(`Enable network proxy for ${protocol || ''}`);
if (proxy) {
setOpt(Curl.option.PROXY, proxy);
setOpt(Curl.option.PROXYAUTH, Curl.auth.ANY);
}
if (noProxy) {
setOpt(Curl.option.NOPROXY, noProxy);
}
} else {
setOpt(Curl.option.PROXY, '');
}
// Set client certs if needed
const clientCertificates = await models.clientCertificate.findByParentId(workspace._id);
for (const certificate of clientCertificates) {
if (certificate.disabled) {
}
if (noProxy) {
setOpt(Curl.option.NOPROXY, noProxy);
}
} else {
setOpt(Curl.option.PROXY, '');
}
// Set client certs if needed
const clientCertificates = await models.clientCertificate.findByParentId(workspace._id);
for (const certificate of clientCertificates) {
if (certificate.disabled) {
continue;
}
const cHostWithProtocol = setDefaultProtocol(certificate.host, 'https:');
if (urlMatchesCertHost(cHostWithProtocol, renderedRequest.url)) {
const ensureFile = blobOrFilename => {
try {
fs.statSync(blobOrFilename);
} catch (err) {
// Certificate file not found!
// LEGACY: Certs used to be stored in blobs (not as paths), so let's write it to
// the temp directory first.
const fullBase = getTempDir();
const name = `${renderedRequest._id}_${renderedRequest.modified}`;
const fullPath = pathJoin(fullBase, name);
fs.writeFileSync(fullPath, Buffer.from(blobOrFilename, 'base64'));
// Set filename to the one we just saved
blobOrFilename = fullPath;
const headers = [
{ name: 'Content-Type', value: 'application/x-www-form-urlencoded' },
{
name: 'Accept',
value: 'application/x-www-form-urlencoded, application/json',
},
];
if (credentialsInBody) {
params.push({ name: c.P_CLIENT_ID, value: clientId });
params.push({ name: c.P_CLIENT_SECRET, value: clientSecret });
} else {
headers.push(getBasicAuthHeader(clientId, clientSecret));
}
const url = setDefaultProtocol(accessTokenUrl);
const response = await sendWithSettings(requestId, {
headers,
url,
method: 'POST',
body: models.request.newBodyFormUrlEncoded(params),
});
const statusCode = response.statusCode || 0;
const bodyBuffer = models.response.getBodyBuffer(response);
if (statusCode === 401) {
// If the refresh token was rejected due an unauthorized request, we will
// return a null access_token to trigger an authentication request to fetch
// brand new refresh and access tokens.
const headers = [
{ name: 'Content-Type', value: 'application/x-www-form-urlencoded' },
{
name: 'Accept',
value: 'application/x-www-form-urlencoded, application/json',
},
];
if (credentialsInBody) {
params.push({ name: c.P_CLIENT_ID, value: clientId });
params.push({ name: c.P_CLIENT_SECRET, value: clientSecret });
} else {
headers.push(getBasicAuthHeader(clientId, clientSecret));
}
const url = setDefaultProtocol(accessTokenUrl);
const responsePatch = await sendWithSettings(requestId, {
headers,
url,
method: 'POST',
body: models.request.newBodyFormUrlEncoded(params),
});
const response = await models.response.create(responsePatch);
const bodyBuffer = models.response.getBodyBuffer(response);
if (!bodyBuffer) {
return {
[c.X_ERROR]: `No body returned from ${url}`,
[c.X_RESPONSE_ID]: response._id,
};
const headers = [
{ name: 'Content-Type', value: 'application/x-www-form-urlencoded' },
{
name: 'Accept',
value: 'application/x-www-form-urlencoded, application/json',
},
];
if (credentialsInBody) {
params.push({ name: c.P_CLIENT_ID, value: clientId });
params.push({ name: c.P_CLIENT_SECRET, value: clientSecret });
} else {
headers.push(getBasicAuthHeader(clientId, clientSecret));
}
const url = setDefaultProtocol(accessTokenUrl);
const responsePatch = await network.sendWithSettings(requestId, {
url,
headers,
method: 'POST',
body: models.request.newBodyFormUrlEncoded(params),
});
const response = await models.response.create(responsePatch);
const bodyBuffer = models.response.getBodyBuffer(response);
if (!bodyBuffer) {
return {
[c.X_ERROR]: `No body returned from ${url}`,
[c.X_RESPONSE_ID]: response._id,
};