Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only position bindings if bindings #888

Closed
wants to merge 2 commits into from
Closed

Conversation

soldair
Copy link

@soldair soldair commented Jul 7, 2015

if you have a query with a '?' mark in it, it is replaced with a numbered param for pg prepared statements.
if you provide no bindings your queries are not run as a prepared statements and ? should be preserved in text fields.

...
trx.raw("insert into table(name) values('foo?');")
...

right now this will insert a row in your "table" table with a name value of "foo$1"

select * from table;
[name]
foo$1

after this fix "?" is preserved as it should be.

note: you cannot use bindings when you are running batch queries/ multiples statements so you will have a time where you need to pass escaped user input in the sql string.

@rhys-vdw
Copy link
Member

rhys-vdw commented Jul 8, 2015

Hi @soldair. Thanks for the PR. Sounds like a reasonable change. Would you be able to test coverage for the problem for posterity?

@elhigu
Copy link
Member

elhigu commented Jul 8, 2015

Sounds like the same issue with #519 and old pull request to fixit, which was never taken in https://github.com/tgriesser/knex/compare/qs-escapement

@soldair
Copy link
Author

soldair commented Jul 8, 2015

happy to write the test :)
ill take a peek at the other pull to see if it's better

@rhys-vdw
Copy link
Member

rhys-vdw commented Jul 9, 2015

@soldair Thanks mate. I'm actually not very familiar with the knex code base - just been granted collaborator status because I'm working on Bookshelf. So I have to be cautious with what I let through. Thanks for understanding.

@rhys-vdw
Copy link
Member

rhys-vdw commented Jul 9, 2015

@soldair Just a thought, but this correction could be taken further to make the variable substitution smarter. If I understand correctly this would still break with your fix:

trx.raw("insert into table(id, name) values(?, 'foo?');", [5])

Not sure of the exact rules that would be correct. Perhaps just checking for ?s enclosed in quotes.

Anyway, happy to take the less complete fix of course.

@soldair
Copy link
Author

soldair commented Jul 9, 2015

you are correct. it will still break. you will have to pass 'foo?' as a bound param.
I'm going to stay away from more complicated parsing for now because i don't have the time to take the security implications into account. I'll ping again when i have the tests.

Hindsight but.. I think raw should build a plain query not a prepared statement and perhaps a prepared statement should be another call on its own because it impacts the kinds of queries postgres allows. Most notably only allowing single statements per call to query.

@rhys-vdw
Copy link
Member

rhys-vdw commented Jul 9, 2015

@soldair Cool. Beyond my code base knowledge, I just wanted to mention that for consideration. Any work here is welcome and I'm happy to merge future PRs. Cheers! 👍

@soldair
Copy link
Author

soldair commented Jul 11, 2015

i still have to figure out the right way to add tests for this.

@elhigu
Copy link
Member

elhigu commented Aug 24, 2015

I did some digging and wrote new suggestion how to handle this with allowing question mark escape sequence and based on @tgriesser's fix for #519. @soldair will this work for your problem as well?

@soldair
Copy link
Author

soldair commented Aug 24, 2015

will be resolved by merging #946

@rhys-vdw
Copy link
Member

Okay, I'll close this one. Thanks guys.

@rhys-vdw rhys-vdw closed this Aug 25, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants