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

fix(error): remove model property from CastError to avoid printing all model properties to console #14568

Merged
merged 2 commits into from
May 6, 2024

Conversation

vkarpov15
Copy link
Collaborator

Fix #14529

Summary

The following script currently prints > 10k lines of output:

'use strict';

const mongoose = require('mongoose');

mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test2');

const TestModel = mongoose.model('Test', mongoose.Schema({ 
  num: { type: Number }
}));

TestModel.findOne({ num: 'foo' }).exec();

Because CastError contains the full model, and Node.js' inspect() seems to ignore custom inspect handlers for error instances. So the above script prints out TestModel and all its subprops, including connections and connection strings in plaintext. This is a bit of a nasty surprise as we saw in #14529.

I did a bunch of digging to try to find some way to get Node's error formatting to avoid printing model, the only approach that seems to work is making model a getter and storing the actual mapping of error -> model in a WeakMap. However that seems a bit excessive.

Examples

Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

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

Looks good to me, just consider maybe putting this in the next minor version instead of a patch

@vkarpov15 vkarpov15 added this to the 8.4 milestone May 6, 2024
@vkarpov15 vkarpov15 changed the base branch from master to 8.4 May 6, 2024 15:29
@vkarpov15
Copy link
Collaborator Author

I agree @hasezoey

@vkarpov15 vkarpov15 merged commit 8eefd65 into 8.4 May 6, 2024
23 checks passed
@hasezoey hasezoey deleted the vkarpov15/gh-14529-2 branch May 6, 2024 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dumps *gigantic* object into console on error
2 participants