Skip to content

Commit

Permalink
fix(core): Apply correct hostname to redirected requests (n8n-io#8674)
Browse files Browse the repository at this point in the history
  • Loading branch information
elsmr authored Feb 20, 2024
1 parent c943a51 commit 0e36aeb
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions packages/core/src/NodeExecuteFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,24 @@ const getHostFromRequestObject = (
}
};

const getBeforeRedirectFn =
(agentOptions: AgentOptions, axiosConfig: AxiosRequestConfig) =>
(redirectedRequest: Record<string, any>) => {
const redirectAgent = new Agent({
...agentOptions,
servername: redirectedRequest.hostname,
});
redirectedRequest.agent = redirectAgent;
redirectedRequest.agents.https = redirectAgent;

if (axiosConfig.headers?.Authorization) {
redirectedRequest.headers.Authorization = axiosConfig.headers.Authorization;
}
if (axiosConfig.auth) {
redirectedRequest.auth = `${axiosConfig.auth.username}:${axiosConfig.auth.password}`;
}
};

export async function parseRequestObject(requestObject: IRequestOptions) {
// This function is a temporary implementation
// That translates all http requests done via
Expand Down Expand Up @@ -475,22 +493,6 @@ export async function parseRequestObject(requestObject: IRequestOptions) {
axiosConfig.maxRedirects = 0;
}

axiosConfig.beforeRedirect = (redirectedRequest) => {
if (axiosConfig.headers?.Authorization) {
redirectedRequest.headers.Authorization = axiosConfig.headers.Authorization;
}
if (axiosConfig.auth) {
redirectedRequest.auth = `${axiosConfig.auth.username}:${axiosConfig.auth.password}`;
}
};

if (requestObject.rejectUnauthorized === false) {
axiosConfig.httpsAgent = new Agent({
rejectUnauthorized: false,
secureOptions: crypto.constants.SSL_OP_LEGACY_SERVER_CONNECT,
});
}

const host = getHostFromRequestObject(requestObject);
const agentOptions: AgentOptions = {};
if (host) {
Expand All @@ -502,6 +504,8 @@ export async function parseRequestObject(requestObject: IRequestOptions) {
}
axiosConfig.httpsAgent = new Agent(agentOptions);

axiosConfig.beforeRedirect = getBeforeRedirectFn(agentOptions, axiosConfig);

if (requestObject.timeout !== undefined) {
axiosConfig.timeout = requestObject.timeout;
}
Expand Down Expand Up @@ -894,6 +898,8 @@ function convertN8nRequestToAxios(n8nRequest: IHttpRequestOptions): AxiosRequest
}
axiosRequest.httpsAgent = new Agent(agentOptions);

axiosRequest.beforeRedirect = getBeforeRedirectFn(agentOptions, axiosRequest);

if (n8nRequest.arrayFormat !== undefined) {
axiosRequest.paramsSerializer = (params) => {
return stringify(params, { arrayFormat: n8nRequest.arrayFormat });
Expand Down

0 comments on commit 0e36aeb

Please sign in to comment.