
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
@tsmx/mongoose-encrypted-string
Advanced tools
EncryptedString
type for Mongoose schemas. Provides AES-256-CBC encryption-at-rest for strings.
var mongoose = require('mongoose');
const mes = require('@tsmx/mongoose-encrypted-string');
const key = 'YOUR KEY HERE';
// register the new type EncryptedString
mes.registerEncryptedString(mongoose, key);
// use EncryptedString in your schemas
Person = mongoose.model('Person', {
id: { type: String, required: true },
firstName: { type: mongoose.Schema.Types.EncryptedString },
lastName: { type: mongoose.Schema.Types.EncryptedString }
});
let testPerson = new Person();
testPerson.id = 'id-test';
testPerson.firstName = 'Hans'; // stored encrypted
testPerson.lastName = 'Müller'; // stored encrypted
await testPerson.save();
let queriedPerson = await Person.findOne({ id: 'id-test' });
console.log(queriedPerson.firstName); // 'Hans', decrypted automatically
console.log(queriedPerson.lastName); // 'Müller, decrypted automatically
Directly querying the MongoDB will return the encrypted data.
> db.persons.findOne({ id: 'id-test' });
{
"_id" : ObjectId("5f8576cc0a6ca01d8e5c479c"),
"id" : "id-test",
"firstName" : "66db1589b5c0de7f98f5260092e6799f|a6cb74bc05a52d1244addb125352bb0d",
"lastName" : "2b85f4ca2d98ad1234da376a6d0d9128|d5b0257d3797da7047bfea6dfa62e19c",
"__v" : 0
}
Registers the new type EncryptedString
in the mongoose
instance's schema types. Encryption/decryption is done with AES-256-CBC using the given key
. After calling this funtion you can start using the new type via mongoose.Schema.Types.EncryptedString
in your schemas.
The mongoose instance where EncryptedString
should be registered.
The key used for encryption/decryption. Length must be 32 bytes. See notes for details.
For performance reasons it maybe useful to use Mongoose's lean()
queries. Doing so, the query will return the raw JSON objects from the MongoDB database where all properties of type EncryptedString
are encrypted.
To get the clear text values back you can directly use @tsmx/string-crypto which is also used internally in this package for encryption and decryption.
const key = 'YOUR KEY HERE';
const sc = require('@tsmx/string-crypto');
// query raw objects with encrypted string values
let person = await Person.findOne({ id: 'id-test' }).lean();
// decrypt using string-crypto
let firstName = sc.decrypt(person.firstName, { key: key });
let lastName = sc.decrypt(person.lastName, { key: key });
EncryptedString
class or schema elements of this type. This would break the encryption.FAQs
EncryptedString type for Mongoose schemas.
The npm package @tsmx/mongoose-encrypted-string receives a total of 3 weekly downloads. As such, @tsmx/mongoose-encrypted-string popularity was classified as not popular.
We found that @tsmx/mongoose-encrypted-string demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.