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

Create and save should sanitize input object the same way set() does with strict mode off. #11001

Closed
sgpinkus opened this issue Nov 20, 2021 · 1 comment
Milestone

Comments

@sgpinkus
Copy link
Contributor

sgpinkus commented Nov 20, 2021

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
save, create, set work differently with strict: false.

If the current behavior is a bug, please provide the steps to reproduce.

Test script

const mongoose = require('mongoose');
const { model, Schema } = mongoose;

var TestSchema =  new Schema({
  text: { type: String, default: 'text' }
}, {
  strict: false
});
TestSchema.methods.someFn = function() { console.log('hi'); }

var Test = model('Test', TestSchema);

async function main() {
  await mongoose.connect('mongodb://localhost:27017/test', { useUnifiedTopology: true, useNewUrlParser: true });
  await mongoose.connection.dropDatabase();

  var unTrusted = { someFn: () => console.log('pwnd') }

  var x = await Test.create(unTrusted);
  await x.save();
  x.someFn();

  var x = new Test(unTrusted);
  await x.save();
  x.someFn();

  var x = await Test.create({});
  await x.set(unTrusted);
  x.someFn();
}

main()
  .then(() => process.exit())
  .catch((e) => { console.error(e); process.exit(); })

Output strict: false

pwnd
pwnd
hi

Output strict: true

hi
hi
hi

What is the expected behavior?
Create and save should sanitize input object the same way set() does, and the same as in strict mode.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
v14.18.1

> require('mongoose').version
'6.0.13'
> require('mongoose').version
'6.0.13'
@vkarpov15 vkarpov15 modified the milestones: 6.0.14, 6.0.15 Nov 27, 2021
@sgpinkus
Copy link
Contributor Author

sgpinkus commented Dec 7, 2021

Thanks @vkarpov15.

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

No branches or pull requests

2 participants