Open edmondsylar opened 4 years ago
@edmondsylar Thanks for posting! We'll take a look as soon as possible.
In the mean time, there are a few ways you can help speed things along:
Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly.
For help with questions about Sails, click here.
I Have actually figured out a way that I could accomplish this without using hooks. I have modified my model's page to look like this
/**
module.exports.models = {
/***
.create()
and .update()
should ignore *true
. *Note that
schema: false
is not supported by every database. *
For example, if you are using a SQL database, then relevant models *
are always effectively
schema: true
. And if noschema
setting is *
provided whatsoever, the behavior is left up to the database adapter. *
*
For more info, see: *
https://sailsjs.com/docs/concepts/orm/model-settings#?schema *
* ***/
// schema: true,
/***
Note that, when running in a production environment, this will be *
automatically set to
migrate: 'safe'
, no matter what you configure *
here. This is a failsafe to prevent Sails from accidentally running *
auto-migrations on your production database. *
*
For more info, see: *
https://sailsjs.com/docs/concepts/orm/model-settings#?migrate *
* ***/
// migrate: 'alter',
/***
id
), as well as two *For more info, see: *
https://sailsjs.com/docs/concepts/orm/model-settings#?attributes *
* ***/
attributes: {
createdAt: { type: 'number', autoCreatedAt: true, },
updatedAt: { type: 'number', autoUpdatedAt: true, },
id: { type: 'number', autoIncrement: true, },
//--------------------------------------------------------------------------
// /\ Using MongoDB?
// || Replace id
above with this instead:
//
// // id: { type: 'string', columnName: '_id' }, //
//
// Plus, don't forget to configure MongoDB as your default datastore:
// https://sailsjs.com/docs/tutorials/using-mongo-db
//--------------------------------------------------------------------------
},
customToJSON: function () { var keys = Object.keys(this); keys.forEach(element => { this['process'] = 'encryption can happen here.'; });
return _.omit(this, ['password', 'id']); },
beforeCreate:function(valuesToSet, proceed){ /**
function since we might need to eliminate some fields when encrypting our data. */ cleanUp=(arr)=> { var newArray = []; const fieldsToEliminate = ['updatedAt', 'id', 'createdAt'];
arr.forEach(element => { if (!fieldsToEliminate.includes(element)) { newArray.unshift(element); } });
return newArray; }
var keys = cleanUp(Object.keys(valuesToSet)); keys.forEach(element => { valuesToSet[element] = null; });
return proceed() },
/**
encrypt: true
. *The
default
DEK is used for all new encryptions, but multiple DEKs *
can be configured to allow for key rotation. In production, be sure to *
manage these keys like you would any other sensitive credential. *
For more info, see: *
https://sailsjs.com/docs/concepts/orm/model-settings#?dataEncryptionKeys *
* **/
dataEncryptionKeys: { default: 'UYVCo+v+/Mf5dtLAhroe858zCcg8Dj1h18sclIThZg0=' },
/***
config/env/production.js
.) ** ***/
cascadeOnDestroy: true
}; This works but then creates an issue when querying with a post request.
Hey @edmondsylar, I'm not sure I understand your use case, why are you encrypting data before your app runs its other business logic?
@edmondsylar if I'm understanding you right, it's technically possible to do this in a hook
@eashaw Currently I don't know of any other way I can pull it off, haven't used the framework for too long but the essence is to have all the data in my database encrypted just as an extra layer of security for my app data,
If there is a way that I can have this implemented differently, please it would surely be a pleasure if you helped
@rachaelshaw Yes, My first attempt was in a hook and i was modifying the data from the request as it comes in but I failed to decrypt the data from the request before I could send it back to the user that's why I decided do it no the model instead
Again am not sure if this is the best way I can do this but am open to suggestions.
Ni @edmondsylar, can you tell me more about your use case? You might not want at-rest encryption.
@eashaw I simply want to have all the data in my database to be encrypted, which I have achieved using beforeCreate callback function on the main model configuration file.
So currently I want to know if there is a way I can execute a function when the data is being fetched, I have tried embedding my logic in the customToJSON function but its not executed, don't know why, tho when I try the same in a different sails App
it works, I don't know why it doesn't apply in my main application.
Hello @edmondsylar, I'm not sure what kind of app you’re working on, and what the security requirements might be, but I would strongly encourage you to take a step back and reconsider whether encryption-at-rest for all of your project’s data is actually needed; this type of approach comes with a lot of overhead and extra complexity that just isn’t necessary for the majority of apps (In case you haven’t gone through these yet: it may help to take a look at the security and deployment docs.)
@eashaw I hadn't surely gone through the security docs for sailsjs
but am going to take some time off and check them,
The encryption-at-rest is really required for the kind of application am working on, though I have come to find that this kind of methodology might not be very popular because am really not getting much information about it.
but again to answer your question, yes, the data encryption is super relevant for me in this application.
@edmondsylar How do you intend to query on an encrypted primary key?
My primary keys which in this case are auto increments are actually not encrypted but just in case I was to encrypt them, It still would work since even my query is made with encrypted data, I have a function that encrypts all data that comes in from get requests.
The only issue is that I managed to achieve this using a policy which I surely don't think is a very smart move but its the only one I could get to work, And now I have a problem that I can't call more than one policy one a specific set of routes,
Example
'*' : ['policy-one', 'policy-two', ...]
I don't know if this is possible but if so, please help out with a fix
Hi @edmondsylar, you should be able to link multiple policies on an action. If you're seeing issues with the documented usage would you mind creating a minimal repo reproducing the issue you are seeing with multiple policies?
Looking into this ASAP and reverting, Am going to go through my use case and see if am doing it right because I have the same exact implementation that the document suggests.
hello. I'm wondering if it is possible to take action on a get request after its been processed but before the data is returned to the user. Senario I have a hook that encrypts all my data in a POST request before its actually processed and stored in the database, but now I need to decrypt that same data when requested by a user from. I have been working with a very basic or method where I had to decrypting the data from controllers but the method is a little lengthy and hectic and the same all through, Is there a way I could have a function that I call on every GET request before the data is returned to the user but after fetching it from the database?
Thanks in advance