-
Notifications
You must be signed in to change notification settings - Fork 71
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
Possible bug in SQL string escaping logic? #218
Comments
Well, after playing with it a bit, the problem seems to be that you didn't unescape the string that you are supplying to postgresql-simple. It seems that the COPY command is emitting the extended escape syntax, whereas postgresql-simple uses the standard conforming syntax. (Though we might move to the extended escape syntax, but that wouldn't really help your problem.) With the extended escape syntax, |
Incidentally, the extended escape syntax appears to accept |
Probably a better way of handling this would be to load the file into your temporary table using a copy command, then retrieve the hstore from postgres via postgresql-simple, and let it handle the unescaping for you. |
Have I written the INSERT query incorrectly? What I gathered from the docs was that query substitution will handle all escaping for me. Also, the value being substituted is a regular Text and not a custom type (for which I may have incorrectly written the escaping logic). What if this string was generated by user input? Is the way I've written my query not the recommended way to insert user input into the DB? |
What is the underlying function in libpq being user here?
How does one use PQexecParams via pg-simple? |
I re-read the docs and am still wondering why this shouldn't have worked out of the box? At most, it may have resulted in an malformed HSTORE string syntax, but why did this result in a malformed SQL query?
|
It didn't result in a malformed query. That error message is coming from the hstore parser, not the sql parser. There is not an exploitable injection here, though there probably would be if you use the Also, you are taking a If you generated malformed hstore syntax from the You'll have the same problem with |
Concretely, the second line is what you should be supplying in this case:
|
Also, it might help to consider the multiple levels of escaping/unescaping that postgres performs. First the sql parser parses one of three string syntaxes, with three different unescaping rules, then passes the unescaped result off to the hstore textual format parser. And that has a second layer of unescaping rules, i.e. by escaping So in this case, the escape syntax generated by the copy command (which resembles the extended escape syntax) is passing through the postgresql-simple's libpq based escaper and postgres's string parser unchanged, and then misinterpreted by the hstore unescaping rules. |
I've hit a similar issue when doing QuickCheck/Arbitrary testing for HStoreMap in my application. The following test fails a lot of times:
Passing case:
Failing case:
|
@lpsmith would you be open to including Quickcheck in any one of the PRs that I've submitted? I'd like to put this test in the upstream test-suite to rule out any issue withthe pg-simple library. |
@lpsmith I'm also investigating whether the Text values being generated in |
Adding quickcheck to the test suite is quite acceptable. That failing log message I do find to be rather curious. Let me know when you figure something out. |
Also, if you just want to round-trip hstore syntax through postgres and postgresql-simple, you can write |
Which of the two PRs is the way forward? I'm voting for filtering out NULL values when populating the
Thanks for the tip! Did you mean to say, simply do |
@lpsmith I've modified the tests and implemented a
Here are some minimal failing test cases:
Another test case:
@lpsmith will you have time to replicate these test cases and confirm that something indeed is going wrong in the HStore => SQL conversion? |
Is it the |
@lpsmith Based on further investigation (given below), now I'm fairly convinced that there is some bug in the HSoreMap -> SQL conversion infrastructure. Similar data for JSON works perfectly fine, whereas it fails for HSTORE.
Output:
|
@lpsmith should we be reusing this logic from Aeson -- https://github.com/bos/aeson/blob/2bc6f22f164d172d0d77d878422b67433a57a9d8/Data/Aeson/Encoding/Builder.hs#L121-L142 |
@lpsmith your suggested way forward? |
I got a dump of live data in an HSTORE column from our production DB using:
One of the rows in the resulting text file was:
Next, I have a utility function to help me pump all this data into a throwaway table (for testing), only to read it back and test my custom serialisation function, which takes an
HStoreList -> CustomDataType
:The function above generates the following INSERT query:
which fails with the following error:
No other row fails this round-tripping test. Just this particular row because of the
\\"
Am I doing something wrong or is there a bug in the SQL quoting in PG-simple?The text was updated successfully, but these errors were encountered: