balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.86k stars 1.95k forks source link

Run hook before response is sent but already processed. #7040

Open edmondsylar opened 4 years ago

edmondsylar commented 4 years ago

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

sailsbot commented 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.

edmondsylar commented 4 years ago

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 = {

/***

}; This works but then creates an issue when querying with a post request.

eashaw commented 4 years ago

Hey @edmondsylar, I'm not sure I understand your use case, why are you encrypting data before your app runs its other business logic?

rachaelshaw commented 4 years ago

@edmondsylar if I'm understanding you right, it's technically possible to do this in a hook

edmondsylar commented 4 years ago

@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

edmondsylar commented 4 years ago

@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.

eashaw commented 4 years ago

Ni @edmondsylar, can you tell me more about your use case? You might not want at-rest encryption.

edmondsylar commented 4 years ago

@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.

eashaw commented 4 years ago

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.)

edmondsylar commented 4 years ago

@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.

eashaw commented 4 years ago

@edmondsylar How do you intend to query on an encrypted primary key?

edmondsylar commented 4 years ago

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

eashaw commented 4 years ago

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?

edmondsylar commented 4 years ago

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.