Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Removing stack trace info in production env #11657

Merged
merged 22 commits into from
Aug 21, 2023

Conversation

MomentQYC
Copy link
Contributor

@MomentQYC MomentQYC commented Aug 10, 2023

What

Remove stack trace information from production errors, and replace a small number of unnecessary errors with other types of output.

Why

This is what instance/queue/ currently looks like when accessed without logging in (e.g. misskey.io/queue)
image
Compare the changed look
image
Many times in production environments stack trace information is unnecessary and a security risk.
For a small number of output changes, I think an average user would prefer a program that only outputs logs as opposed to a program that reports errors when a login action is required.

Additional info (optional)

Checklist

  • Read the contribution guide
  • Test working in a local environment
  • (If needed) Add story of storybook
  • (If needed) Update CHANGELOG.md
  • (If possible) Add tests

@github-actions github-actions bot added packages/frontend Client side specific issue/PR packages/backend Server side specific issue/PR labels Aug 10, 2023
@codecov
Copy link

codecov bot commented Aug 10, 2023

Codecov Report

Merging #11657 (91b83b8) into develop (792622a) will decrease coverage by 0.11%.
The diff coverage is 66.66%.

@@             Coverage Diff             @@
##           develop   #11657      +/-   ##
===========================================
- Coverage    78.83%   78.73%   -0.11%     
===========================================
  Files          922      922              
  Lines        97709    97709              
  Branches      7767     7741      -26     
===========================================
- Hits         77032    76929     -103     
- Misses       20677    20780     +103     
Files Changed Coverage Δ
...ages/backend/src/server/web/ClientServerService.ts 93.74% <66.66%> (+0.28%) ⬆️

... and 16 files with indirect coverage changes

@tamaina
Copy link
Contributor

tamaina commented Aug 11, 2023

I think it's better to use ApiError in API execution service

@tamaina
Copy link
Contributor

tamaina commented Aug 11, 2023

There should be no need to change the frontend

@tamaina
Copy link
Contributor

tamaina commented Aug 11, 2023

I don't know why the test is failing

@tamaina
Copy link
Contributor

tamaina commented Aug 11, 2023

Somehow Jest can't use ./error.js

@MomentQYC
Copy link
Contributor Author

There should be no need to change the frontend

The main consideration is to facilitate centralized management and reduce unnecessary errors.

@MomentQYC
Copy link
Contributor Author

There should be no need to change the frontend

The main consideration is to facilitate centralized management and reduce unnecessary errors.

For Example, https://github.com/misskey-dev/misskey/blob/827616f63094997bd44458b8c072a50da176af32/packages/frontend/src/scripts/please-login.ts#L25C3-L25C3

@tamaina
Copy link
Contributor

tamaina commented Aug 11, 2023

facilitate centralized management and reduce unnecessary errors.

I don't want them on the frontend.

@tamaina
Copy link
Contributor

tamaina commented Aug 11, 2023

I think it is not preferable to remove stack traces or change Errors to console.log on the front end.

@MomentQYC
Copy link
Contributor Author

I think it is not preferable to remove stack traces or change Errors to console.log on the front end.

I have modified it to use console.log in the production environment, while still throwing an error in other environments. Is this feasible?

@tamaina
Copy link
Contributor

tamaina commented Aug 11, 2023

Do not rewrite throw Errors in frontend. They are there to stop calling(upstream) functions, for example.

@MomentQYC
Copy link
Contributor Author

Do not rewrite throw Errors in frontend. They are there to stop calling(upstream) functions, for example.

OK, I will remove all frontend changes first.

@tamaina
Copy link
Contributor

tamaina commented Aug 11, 2023

This PR should not include frontend changes.

@github-actions github-actions bot removed the packages/frontend Client side specific issue/PR label Aug 11, 2023
@MomentQYC MomentQYC marked this pull request as ready for review August 11, 2023 15:50
@saschanaz
Copy link
Member

I'm not sure we want throw ErrorHandling, because errors can be from anywhere, from libraries and native JS functions. What we probably want is to customize Fastify error handler: https://fastify.dev/docs/latest/Reference/Server/#seterrorhandler

Copy link
Member

@saschanaz saschanaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

@MomentQYC
Copy link
Contributor Author

I'm not sure we want throw ErrorHandling, because errors can be from anywhere, from libraries and native JS functions. What we probably want is to customize Fastify error handler: https://fastify.dev/docs/latest/Reference/Server/#seterrorhandler

I would like to address the existing problems first and then focus on improving error-related problems in the future. Since I'm not very familiar with TypeScript, it might take some time for me to make changes to the code (if necessary).

@MomentQYC MomentQYC requested a review from saschanaz August 13, 2023 17:14
@saschanaz
Copy link
Member

Given that this PR now sets an error handler that drops stack info, the changes in other files are not needed, right?

@MomentQYC
Copy link
Contributor Author

Given that this PR now sets an error handler that drops stack info, the changes in other files are not needed, right?

My original intention was to minimize stack trace information in production environments as much as possible, and in particular, as much as possible, not to expose the user to stack trace information especially from the back-end part.
So I think especially in scenarios like the one in the very first screenshot, targeted removal of stack trace information is necessary, other than that it's optional.

@saschanaz
Copy link
Member

You mean there will be any situation that the stack will still be exposed even with the error handler?

@MomentQYC
Copy link
Contributor Author

You mean there will be any situation that the stack will still be exposed even with the error handler?

When I tested along the lines of the code here in the production environment, I still found the stack trace.
https://github.com/misskey-dev/misskey/blob/ab58b651f79e182c20a238f9c07f0c0006a58599/packages/backend/src/server/web/ClientServerService.ts#L148C5-L151C6

@saschanaz
Copy link
Member

Hmm, that's specific to Misskey's bull-board usage where the library registers its own error handler (and thus yours is being ignored). It seems reasonable to not throw an error but explicitly send an object instead in https://github.com/misskey-dev/misskey/blob/ab58b651f79e182c20a238f9c07f0c0006a58599/packages/backend/src/server/web/ClientServerService.ts#L148C5-L151C6, as their example does, and later investigate this bull-board issue separately.

For other cases the error handler should be sufficient.

@MomentQYC
Copy link
Contributor Author

This is where it's most likely to be seen by the user, so here's where I'd like to apply additional error handling (removing the stack trace).
This has been changed to reply.code().send() in the latest changes.
Like this:
image

@syuilo
Copy link
Member

syuilo commented Aug 14, 2023

やる場合、new Errorの使用を禁止する何らかの機構(lintとかgithub actionsとか)が無いとこのPRの目的を維持するのは難しそう

@saschanaz
Copy link
Member

This is where it's most likely to be seen by the user, so here's where I'd like to apply additional error handling (removing the stack trace).
This has been changed to reply.code().send() in the latest changes.
Like this:
image

What I'm saying is that it's doable with error handler, it's the sole purpose of error handler anyway. The reason it's not working for bull-board one is explained above, and I suggest using send() only for that exception for now and reverting others. And maybe add some test by adding some test-only route.

@MomentQYC
Copy link
Contributor Author

reverting others

Do you mean like now?

packages/backend/src/error.ts Outdated Show resolved Hide resolved
packages/backend/src/misc/fastify-reply-error.ts Outdated Show resolved Hide resolved
packages/backend/src/server/oauth/OAuth2ProviderService.ts Outdated Show resolved Hide resolved
packages/backend/src/misc/error.ts Outdated Show resolved Hide resolved
packages/backend/src/misc/error.ts Outdated Show resolved Hide resolved
@MomentQYC MomentQYC requested a review from saschanaz August 14, 2023 14:08
@saschanaz
Copy link
Member

saschanaz commented Aug 15, 2023

Hmm, I locally tested what Fastify does by adding a random error, but it seems Fastify already does not expose error stack information by default at all. The details field you saw in /queue was from bull-board custom error handler and nothing to do with Fastify default behavior, which only adds statusCode, error, and message.

tl;dr: we don't need setErrorHandler at all, this PR should be sufficient only with changes on ClientServerService.ts.

@MomentQYC
Copy link
Contributor Author

we don't need setErrorHandler at all

Reverted

Copy link
Member

@saschanaz saschanaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@syuilo syuilo merged commit 388448f into misskey-dev:develop Aug 21, 2023
@syuilo
Copy link
Member

syuilo commented Aug 21, 2023

👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
packages/backend:test packages/backend Server side specific issue/PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants