Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

Modified code to support ANSI_QUOTES enabled servers #31

Closed
wants to merge 2 commits into from

Conversation

zivc
Copy link

@zivc zivc commented Mar 17, 2015

This whole library made use of double quotes. On MySQL servers with ANSI_QUOTES enabled, this was causing the following issue: https://github.com/balderdashy/sails-mysql/issues/195

I've also added a utils.stringLiteral method so instead of having '"'+value+'"' around the place, it is now a little more tidier.

I've also added an identifierCharacter option which defaults to the ` character.

More info on ANSI_QUOTES here: http://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_ansi_quotes

zivc and others added 2 commits March 17, 2015 23:03

utils.stringLiteral = function stringLiteral(val, escapeCharacter) {
var regex = new RegExp(escapeCharacter, 'g');
return escapeCharacter+val.replace(regex, '\\'+escapeCharacter)+escapeCharacter;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned about this approach. Consider the javascript string literal "bl\'ue" turning into 'bl\\\'ue' in SQL.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What other approach do you recommend? I don't see anything wrong with it personally. I welcome your thoughts though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The concern is that malicious users can unescape the escape characters by inserting unescaped slashes. Escaping string literals should honestly be done in reference to the connection (see https://www.npmjs.com/package/mysql#escaping-query-values), but I don't think that will be simply achieved here. In the meantime, perhaps we should make use of mysql.escape? @tjwebb ?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@devinivy are you comfortable if I import this to do the sql escaping?

@particlebanana
Copy link
Contributor

Hey guys the whole backtick vs single quote vs double quote debate was the reason for the escapeCharater in the first place. If there is somewhere where it's not being used then we just need to wrap it. It looks like the values being wrapped in hardcoded double quotes was an issue.

We should make the sql options configurable in the adapters instead of being hardcoded.

@zivc
Copy link
Author

zivc commented Aug 4, 2015

This has bitten me in the arse again at work today. I will take a look this weekend and make another PR.

@zivc zivc closed this Aug 4, 2015
@rkt2spc
Copy link

rkt2spc commented Nov 15, 2018

@zivc can I continue your work on this?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants