Skip to content

Commit

Permalink
fix(core): Improve axios error handling in nodes (n8n-io#5891)
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy authored and believe-Mahesh committed Apr 4, 2023
1 parent 51f1e62 commit 61fc2fd
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 51 deletions.
12 changes: 4 additions & 8 deletions packages/cli/src/controllers/passwordReset.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@ import { PasswordResetRequest } from '@/requests';
import type { IDatabaseCollections, IExternalHooksClass, IInternalHooksClass } from '@/Interfaces';
import { issueCookie, setCookie } from '@/auth/jwt';
import { isLdapEnabled } from '@/Ldap/helpers';
<<<<<<< HEAD
import { Role } from '@/databases/entities/Role';
import { randomBytes } from 'crypto';

=======
import { isSamlCurrentAuthenticationMethod } from '../sso/ssoHelpers';
>>>>>>> feat(core): Prevent non owners password reset when saml is enabled (#5788)

@RestController()
export class PasswordResetController {
Expand All @@ -45,7 +42,8 @@ export class PasswordResetController {

private userRepository: Repository<User>;

private roleRepository : Repository<Role>;
private roleRepository: Repository<Role>;

private readonly mailer: UserManagementMailer;


Expand Down Expand Up @@ -264,10 +262,8 @@ export class PasswordResetController {
throw new NotFoundError('');
}

const passwordHash = await hashPassword(validPassword);

await this.userRepository.update(userId, {
password: passwordHash,
password: await hashPassword(validPassword),
resetPasswordToken: null,
resetPasswordTokenExpiration: null,
});
Expand All @@ -290,7 +286,7 @@ export class PasswordResetController {
});
}

await this.externalHooks.run('user.password.update', [user.email, passwordHash]);
await this.externalHooks.run('user.password.update', [user.email, password]);
}

@Post('/authentication')
Expand Down
2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@types/crypto-js": "^4.0.1",
"@types/express": "^4.17.6",
"@types/lodash.get": "^4.4.6",
"@types/lodash.pick": "^4.4.7",
"@types/mime-types": "^2.1.0",
"@types/request-promise-native": "~1.0.15",
"@types/uuid": "^8.3.2"
Expand All @@ -54,6 +55,7 @@
"flatted": "^3.2.4",
"form-data": "^4.0.0",
"lodash.get": "^4.4.2",
"lodash.pick": "^4.4.0",
"mime-types": "^2.1.27",
"n8n-workflow": "workspace:*",
"oauth-1.0a": "^2.2.6",
Expand Down
70 changes: 34 additions & 36 deletions packages/core/src/NodeExecuteFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import {
ExpressionError,
} from 'n8n-workflow';

import pick from 'lodash.pick';
import { Agent } from 'https';
import { IncomingMessage } from 'http';
import { stringify } from 'qs';
Expand Down Expand Up @@ -676,50 +677,47 @@ async function proxyRequestToAxios(
return body;
}
} catch (error) {
const { request, response, isAxiosError, toJSON, config, ...errorData } = error;
if (configObject.simple === false && response) {
if (configObject.resolveWithFullResponse) {
return {
body: response.data,
headers: response.headers,
statusCode: response.status,
statusMessage: response.statusText,
};
} else {
return response.data;
}
}
const { config, response } = error;

// Axios hydrates the original error with more data. We extract them.
// https://github.com/axios/axios/blob/master/lib/core/enhanceError.js
// Note: `code` is ignored as it's an expected part of the errorData.
if (response) {
Logger.debug('Request proxied to Axios failed', { status: response.status });
let responseData = response.data;
if (error.isAxiosError) {
if (response) {
Logger.debug('Request proxied to Axios failed', { status: response.status });
let responseData = response.data;

if (Buffer.isBuffer(responseData) || responseData instanceof Readable) {
responseData = await binaryToBuffer(responseData).then((buffer) =>
buffer.toString('utf-8'),
);
}

if (Buffer.isBuffer(responseData) || responseData instanceof Readable) {
responseData = await binaryToBuffer(responseData).then((buffer) =>
buffer.toString('utf-8'),
);
if (configObject.simple === false) {
if (configObject.resolveWithFullResponse) {
return {
body: responseData,
headers: response.headers,
statusCode: response.status,
statusMessage: response.statusText,
};
} else {
return responseData;
}
}

const message = `${response.status as number} - ${JSON.stringify(responseData)}`;
throw Object.assign(new Error(message, { cause: error }), {
statusCode: response.status,
options: pick(config ?? {}, ['url', 'method', 'data', 'headers']),
});
} else {
throw Object.assign(new Error(error.message, { cause: error }), {
options: pick(config ?? {}, ['url', 'method', 'data', 'headers']),
});
}
error.message = `${response.status as number} - ${JSON.stringify(responseData)}`;
}

error.cause = errorData;
error.error = error.response?.data || errorData;
error.statusCode = error.response?.status;
error.options = config || {};

// Remove not needed data and so also remove circular references
error.request = undefined;
error.config = undefined;
error.options.adapter = undefined;
error.options.httpsAgent = undefined;
error.options.paramsSerializer = undefined;
error.options.transformRequest = undefined;
error.options.transformResponse = undefined;
error.options.validateStatus = undefined;

throw error;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ export class HttpRequestV3 implements INodeType {
if (autoDetectResponseFormat && response.reason.error instanceof Buffer) {
response.reason.error = Buffer.from(response.reason.error as Buffer).toString();
}
throw new NodeApiError(this.getNode(), response as JsonObject);
throw new NodeApiError(this.getNode(), response.reason as JsonObject);
} else {
// Return the actual reason as error
returnItems.push({
Expand Down
18 changes: 12 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 61fc2fd

Please sign in to comment.