balderdashy / sails

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

PostgreSQL: transaction() doesn't guarantee use of a database transaction #7068

Open alxndrsn opened 3 years ago

alxndrsn commented 3 years ago

Node version: 14.15.0 Sails version (sails): 1.4.0 ORM hook version (sails-hook-orm): 2.1.1 DB adapter & version (e.g. sails-mysql@5.55.5): sails-postgresql@2.0.0


Observed

Calling datastore.transaction(during) when using the sails-postgresql adapter for Waterline doesn't guarantee that during will be run inside a database transaction.

If there is no transaction-specific code in the executed SQL, the during code will complete without error, so the user may not be aware that their query or queries were not transactional.

Steps to recreate

Example project: https://github.com/alxndrsn/sailsjs-postgres-transactions-maybe


Maybe related to https://github.com/balderdashy/sails/pull/7017.

sailsbot commented 3 years ago

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

alxndrsn commented 3 years ago

I've opened a PR to fix this at https://github.com/balderdashy/sails-hook-orm/pull/22, but there are other instances of the same/similar technique in:

This suggests there might be surprising behaviour in other places than just datastore.transaction().

I'd be happy to provide PRs for some of these other packages if https://github.com/balderdashy/sails-hook-orm/pull/22 is accepted.

eashaw commented 3 years ago

Hi @alxndrsn, the code in the files you link is to determine whether or not a function is async. Due to the ad hoc way Sails sniffs async functions and function arguments, it doesn't support certain shorthand for passing in functions. Would you mind explaining how these files relate to this issue, and are you running into issues with this when you use documented syntax?

alxndrsn commented 3 years ago

it doesn't support certain shorthand for passing in functions

@eashaw :+1: that's the cause of the reported bug, which is fixed in my PR

alxndrsn commented 3 years ago

Hi @eashaw, just revisiting this.

Given the expectations around transactional guarantees when working with databases, I think it's dangerously misleading that during may be silently executed outside a database transaction.

It would be great if either a fix can be applied, or the documentation can be updated to warn about this possibility.

I suspect the sails-mysql adapter is also affected by this, although I haven't had a chance to test this yet.

alxndrsn commented 3 years ago

Related: https://github.com/brianc/node-postgres/issues/2561