balderdashy / sails

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

Using sails.sendNativeQuery to call stored procedure doesn't return when the procedure has a duplicate column #7005

Open anthozep opened 4 years ago

anthozep commented 4 years ago

Node version: 12.14.1 Sails version (sails): 1.2.4 ORM hook version (sails-hook-orm): 2.1.1 Sockets hook version (sails-hook-sockets): 2.0.0 Organics hook version (sails-hook-organics): N/A Grunt hook version (sails-hook-grunt): 3.1.0 Uploads hook version (sails-hook-uploads): N/A DB adapter & version (e.g. sails-mysql@5.55.5): sails-mysql@1.0.1 Skipper adapter & version (e.g. skipper-s3@5.55.5): N/A


Hi,

I have a MySQL stored procedure I'm calling via sails.sendNativeQuery like so:

try {
    let result = await sails.sendNativeQuery('CALL create_branch($1, $2, $3, $4, $5, $6);', [branch, project, step, 0, parent, user]);
    if (typeof result.rows !== 'undefined') {
        if (typeof result.rows[0] !== 'undefined') {
            for (let row in result.rows[0]) {
                if (typeof result.rows[0][row]['@full_error'] !== 'undefined') {
                    req.status(400).send('could not create branch: ' + result.rows[0][row]['@full_error']);
                    return;
                }
            }
        }
    }
} catch (err) {
    sails.log.error('could not create branch, params ' + req.params + err);
    res.status(400).send('could not create branch: ' + err);
    return;
}

The stored procedure does some preflight checks and executed SIGNAL SQLSTATE '45000' when there is an error, like so:

IF (SELECT id FROM table WHERE table.name = _branch_name AND project = _project AND step = _step LIMIT 1) IS NOT NULL THEN
SIGNAL SQLSTATE '45000'
  SET MESSAGE_TEXT = 'This branch already exists';
END IF;

This appears to work fine.

However, we recently had an issue where there was a duplicate column name in one of the queries in the procedure, it did not return any errors and the request hung. When I ran the procedure directly against the database, it told me there was a duplicate column and exited. Is there a way to catch those types of issues?

sailsbot commented 4 years ago

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

eashaw commented 4 years ago

Hi @anthozep, thanks for using Sails. You might be able to use .timeout() (chained onto the end of sendNativeQuery), passing in the number of MS to help with that. (https://sailsjs.com/documentation/reference/request-req/req-set-timeout#req-settimeout-)