You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Npgsql currently supports client-side parameter handling (as opposed to creating a prepared statement and sending the parameters to the backend). This is a pretty complicated task, involved SQL parsing inside Npgsql.
We're not sure how standard this feature is among ADO.NET providers, but would like to look into the possibility of getting rid of it if possible. If we do get rid of it, breakage should be minimized if possible.
When using Wireshark and SQL Server, I see that parameterized queries are sent in a single packet. First comes the query, including parameters. Then comes a list of all parameter names, data types and values. They call it an RPC call, instead of a normal SQLBatch. SQL Server automatically caches query plans.
MySQL seems to do it just like Npgsql: they rewrite the query and send it with substituted values.
It seems like PostgreSQL never caches query plans for non-prepared queries.
I'm not that into how Npgsql's parameter substitution works, but is it really that complicated? A lexer that carefully follows the same rules as in https://github.com/postgres/postgres/blob/master/src/backend/parser/scan.l should be enough. A parser shouldn't be required, since the only thing we want to do is to tokenize the query so we can replace parameter tokens. No tokens need to be examined further except for parameters. I guess the hardest parts are to know if we are in a string literal or not, with all the different escaping rules?
Yep, that's pretty much what we've got. It's not really a parser, it's
just a lot closer to one than anyone should be comfortable with. It does
just enough tokenizing to figure out when it's in a string literal, a
comment, or a parameter name. There's no parse tree or anything like that.
We run into complexity when we have to recognize the @ as a parameter name
marker, and also recognize @@ as something entirely different, or when we
need to handle backslash escapes (which it currently does not do) which
require us to recognize the E prefix depending on what version backend
we're working with.
When using Wireshark and SQL Server, I see that parameterized queries are
sent in a single packet. First comes the query, including parameters. Then
comes a list of all parameter names, data types and values. They call it an
RPC call, instead of a normal SQLBatch. SQL Server automatically caches
query plans.
MySQL seems to do it just like Npgsql: they rewrite the query and send it
with substituted values.
It seems like PostgreSQL never caches query plans for non-prepared queries.
I'm not that into how Npgsql's parameter substitution works, but is it
really that complicated? A lexer that carefully follows the same rules as
in https://github.com/postgres/postgres/blob/master/src/backend/parser/scan.l
should be enough. A parser shouldn't be required, since the only thing we
want to do is to tokenize the query so we can replace parameter tokens. No
tokens need to be examined further except for parameters. I guess the
hardest parts are to know if we are in a string literal or not, with all
the different escaping rules?
—
Reply to this email directly or view it on GitHub #309 (comment).
I think it's very serious that E prefixed strings aren't identified correctly.
I tried the following query:
select :a as a, E'\':a'
with parameter :a as the integer 123. My result row was 123, '((123)) when it should be 123, ':a.
This opens up for SQL injections. Consider a query like:
insert into some_table (something_secret, user_text)
values (:secret, E'(user input here correctly escaped)')
If the user now inputs ':secret the SQL string fed into Npgsql will look like
insert into some_table (something_secret, user_text)
values (:secret, E'\':secret')
and :secret will be wrongly inserted into the string and the user might later be able to read the secret value out.
"Why didn't the programmer parameterize all input fields"? That's a good question, but not something we demand either. Entity Framework, for example, uses a combination of literal values and parameter values depending on the situation.
I also notice that text inside double quotes aren't ignored either.
select :a from ":a"
should be transformed into (if parameter a is 123)
select 123 from ":a"
This is probably not a common case, but should still be handled correctly...
One quite easy optimization would be to ignore running the rewriter if there are no parameters in the parameter collection, which should be quite common.
Another optimization would be to combine GetDistinctTrimmedCommands and AppendCommandReplacingParameterValues. Both scan through the string and try to match tokens (;, :param and @PARAM). The codes are very similar and to have duplicated code is often a bad idea. Scanning the string once instead of twice will also result in better performance.
I honestly do not understand the purpose of splitting queries with GetDistinctTrimmedCommands. The parameter substitution algorithm works with multi-queries too. Of course it is a good idea for prepared statements to check if it is a multi-query (and then throw an error), but I don't see other use cases?
In order to make the world safe for Windows and Mac clients as well as Unix ones, we accept either \n or \r as a newline. A DOS-style \r\n sequence will be seen as two successive newlines, but that doesn't cause any problems.
This means ---comments run until either \r or \n. Now it only checks for \n.
What are the other options? Prepared statements and forcing the $1, $2...
syntax? If parameter processing is actually done correctly, I see no
problems using it. MySQL uses it, Postgresql jdbc uses it etc.
Den 8 aug 2014 01:49 skrev "Glen Parker" [email protected]:
Emill,
You are doing a phenomenal job of demonstrating why we should not be in
the
business of parameter substitutions :)
Although I'd really like to agree with @glenebob (I don't like the idea of having a parser in Npgsql very much), @Emill's last argument is pretty convincing; the Postgresql backend expects parameters with $1, $2, while the ADO.NET contract requires us to support @hello, @goodbye (even though we don't actually support this at the moment, right?).
So if we want to say we're an ADO.NET provider it doesn't seem like we have a choice but to do client-side parsing... I'd say this should be a major focus of for the next release (version 3.0?), including named parameter support if possible...
So if we want to say we're an ADO.NET provider it doesn't seem like we
have a choice but to do client-side parsing... I'd say this should be a
major focus of for the next release (version 3.0?), including named
parameter support if possible...
—
Reply to this email directly or view it on GitHub #309 (comment).
So if we want to say we're an ADO.NET provider it doesn't seem like we
have a choice but to do client-side parsing... I'd say this should be a
major focus of for the next release (version 3.0?), including named
parameter support if possible...
—
Reply to this email directly or view it on GitHub #309 (comment).
—
Reply to this email directly or view it on GitHub.
I think it's very serious that E prefixed strings aren't identified
correctly.
I tried the following query:
select :a as a, E'':a'
with parameter :a as the integer 123. My result row was 123, '((123))
when it should be 123, ':a.
This opens up for SQL injections. Consider a query like:
insert into some_table (something_secret, user_text)
values (:secret, E'(user input here correctly escaped)')
If the user now inputs ':secret the SQL string fed into Npgsql will look
like
insert into some_table (something_secret, user_text)
values (:secret, E'':secret')
and :secret will be wrongly inserted into the string and the user might
later be able to read the secret value out.
"Why didn't the programmer parameterize all input fields"? That's a good
question, but not something we demand either. Entity Framework, for
example, uses a combination of literal values and parameter values
depending on the situation.
I also notice that text inside double quotes aren't ignored either.
select :a from ":a"
should be transformed into (if parameter a is 123)
select 123 from ":a"
This is probably not a common case, but should still be handled
correctly...
Would be possible for those cases ' blah @BLAH' and "blah @BLAH" be handled
using regular expressions?
I mean, would it be possible that given the following query:
Select * from table where field1 = :param1 and field2 = ':param1' and
field3 = ":param3"
It would be possible to have an expression which would return us the
following tokens:
Select * from table where field1 =
:param1
and field2 = ':param1' and field3 = ":param3"
It would ignore :param syntax when they appear inside ' or ".
Another point which should be considered is that Glen's code is performance
oriented. I don't know if a regular expression, if possible to handle that,
would have a big performance impact.
—
Reply to this email directly or view it on GitHub.
I don't have numbers now, but the regex code I replaced was pretty slow.
The reason for that is that the query needed to be scanned at least once
for each parameter. I think it was twice to handle :param and @PARAM.
I'm not generally a big fan of regex, so I'm not very fluent with them, but
it seems like an extremely tall order for them to handle all the little
rules we have to follow here, perform well, and be the slightest bit
readable.
We have a decent lexer that's fast and works for most cases, so we're most
of the way there. Emill has discovered some additional cases that need to
be handled. Probly the worst of it will be the E prefix handling and
backslash escapes, because the lexer will need to know about server version
and make additional decisions. At any rate, what we have can be added to,
there's no reason to replace it, certainly not with regex.
-Glen
On Fri, Aug 8, 2014 at 6:43 AM, Francisco Figueiredo Jr. < [email protected]> wrote:
I think it's very serious that E prefixed strings aren't identified
correctly.
I tried the following query:
select :a as a, E'':a'
with parameter :a as the integer 123. My result row was 123, '((123))
when it should be 123, ':a.
This opens up for SQL injections. Consider a query like:
insert into some_table (something_secret, user_text)
values (:secret, E'(user input here correctly escaped)')
If the user now inputs ':secret the SQL string fed into Npgsql will look
like
insert into some_table (something_secret, user_text)
values (:secret, E'':secret')
and :secret will be wrongly inserted into the string and the user might
later be able to read the secret value out.
"Why didn't the programmer parameterize all input fields"? That's a good
question, but not something we demand either. Entity Framework, for
example, uses a combination of literal values and parameter values
depending on the situation.
I also notice that text inside double quotes aren't ignored either.
select :a from ":a"
should be transformed into (if parameter a is 123)
select 123 from ":a"
This is probably not a common case, but should still be handled
correctly...
Would be possible for those cases ' blah @BLAH' and "blah @BLAH" be
handled
using regular expressions?
I mean, would it be possible that given the following query:
Select * from table where field1 = :param1 and field2 = ':param1' and
field3 = ":param3"
It would be possible to have an expression which would return us the
following tokens:
Select * from table where field1 =
:param1
and field2 = ':param1' and field3 = ":param3"
It would ignore :param syntax when they appear inside ' or ".
Another point which should be considered is that Glen's code is
performance
oriented. I don't know if a regular expression, if possible to handle
that,
would have a big performance impact.
—
Reply to this email directly or view it on GitHub.
—
Reply to this email directly or view it on GitHub #309 (comment).
I don't have numbers now, but the regex code I replaced was pretty slow. The reason for that is that the query needed to be scanned at least once for each parameter. I think it was twice to handle :param and @PARAM.
I'm not generally a big fan of regex, so I'm not very fluent with them, but it seems like an extremely tall order for them to handle all the little rules we have to follow here, perform well, and be the slightest bit readable.
I see. I think your concerns are valid. I also don't know how well this would run too, regarding the performance and simplicity of the regex expression.
We have a decent lexer that's fast and works for most cases, so we're most of the way there. Emill has discovered some additional cases that need to be handled. Probly the worst of it will be the E prefix handling and backslash escapes, because the lexer will need to know about server version and make additional decisions. At any rate, what we have can be added to, there's no reason to replace it, certainly not with regex.
Ok. I think you are right. Let's check how to fix the problems pointed out by Emil.
Also, would you mind to write an example of a worst case query?
I honestly do not understand the purpose of splitting queries with GetDistinctTrimmedCommands. The parameter substitution algorithm works with multi-queries too. Of course it is a good idea for prepared statements to check if it is a multi-query (and then throw an error), but I don't see other use cases?
Can anyone comment that? One scan of the string would be better than two.
The simple answer is that it was easier that way. We need to (or at least
should - we don't want to waste cycles and IO bandwidth doing a bunch of
parameter subs only to find out from the backend that a conditions
exists that we could have easily checked for) enforce that a query buffer
contains only one distinct command when doing extended queries. The check
could be done as part of the parameter substitution routine as you
mentioned.
I honestly do not understand the purpose of splitting queries with
GetDistinctTrimmedCommands. The parameter substitution algorithm works with
multi-queries too. Of course it is a good idea for prepared statements to
check if it is a multi-query (and then throw an error), but I don't see
other use cases?
Can anyone comment that? One scan of the string would be better than two.
—
Reply to this email directly or view it on GitHub #309 (comment).
Activity
Emill commentedon Aug 6, 2014
ADO.NET is obviously designed for SQL Server...
When using Wireshark and SQL Server, I see that parameterized queries are sent in a single packet. First comes the query, including parameters. Then comes a list of all parameter names, data types and values. They call it an RPC call, instead of a normal SQLBatch. SQL Server automatically caches query plans.
MySQL seems to do it just like Npgsql: they rewrite the query and send it with substituted values.
It seems like PostgreSQL never caches query plans for non-prepared queries.
See http://blogs.msdn.com/b/sqlprogrammability/archive/2007/01/11/4-0-query-parameterization.aspx, http://use-the-index-luke.com/blog/2011-07-16/planning-for-reuse, http://stackoverflow.com/questions/7142335/how-does-postgresql-cache-statements-and-data, http://blogs.msdn.com/b/bindeshv/archive/2010/07/13/ef-query-execution-pattern-usage-of-sp-executesql-vs-direct-execution-of-sql-statement.aspx, http://technet.microsoft.com/en-us/library/ms175528.aspx
I'm not that into how Npgsql's parameter substitution works, but is it really that complicated? A lexer that carefully follows the same rules as in https://github.com/postgres/postgres/blob/master/src/backend/parser/scan.l should be enough. A parser shouldn't be required, since the only thing we want to do is to tokenize the query so we can replace parameter tokens. No tokens need to be examined further except for parameters. I guess the hardest parts are to know if we are in a string literal or not, with all the different escaping rules?
glenebob commentedon Aug 6, 2014
Yep, that's pretty much what we've got. It's not really a parser, it's
just a lot closer to one than anyone should be comfortable with. It does
just enough tokenizing to figure out when it's in a string literal, a
comment, or a parameter name. There's no parse tree or anything like that.
We run into complexity when we have to recognize the @ as a parameter name
marker, and also recognize @@ as something entirely different, or when we
need to handle backslash escapes (which it currently does not do) which
require us to recognize the E prefix depending on what version backend
we're working with.
-Glen
.
On Wed, Aug 6, 2014 at 3:54 PM, Emill [email protected] wrote:
Emill commentedon Aug 7, 2014
I think it's very serious that E prefixed strings aren't identified correctly.
I tried the following query:
with parameter :a as the integer 123. My result row was
123,'((123))when it should be123,':a.This opens up for SQL injections. Consider a query like:
If the user now inputs
':secretthe SQL string fed into Npgsql will look likeand
:secretwill be wrongly inserted into the string and the user might later be able to read the secret value out."Why didn't the programmer parameterize all input fields"? That's a good question, but not something we demand either. Entity Framework, for example, uses a combination of literal values and parameter values depending on the situation.
I also notice that text inside double quotes aren't ignored either.
should be transformed into (if parameter a is 123)
This is probably not a common case, but should still be handled correctly...
Emill commentedon Aug 7, 2014
One quite easy optimization would be to ignore running the rewriter if there are no parameters in the parameter collection, which should be quite common.
Another optimization would be to combine GetDistinctTrimmedCommands and AppendCommandReplacingParameterValues. Both scan through the string and try to match tokens (;, :param and @PARAM). The codes are very similar and to have duplicated code is often a bad idea. Scanning the string once instead of twice will also result in better performance.
I honestly do not understand the purpose of splitting queries with GetDistinctTrimmedCommands. The parameter substitution algorithm works with multi-queries too. Of course it is a good idea for prepared statements to check if it is a multi-query (and then throw an error), but I don't see other use cases?
Emill commentedon Aug 7, 2014
I also noted that PostgreSQL C-style comments
/* ... */nest, which means/* /* */ */is a valid comment. This is not handled correctly.Emill commentedon Aug 7, 2014
This means
---comments run until either\ror\n. Now it only checks for\n.Emill commentedon Aug 7, 2014
http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING should also be supported.
glenebob commentedon Aug 7, 2014
Emill,
You are doing a phenomenal job of demonstrating why we should not be in the
business of parameter substitutions :)
-Glen
On Thu, Aug 7, 2014 at 4:31 PM, Emill [email protected] wrote:
Emill commentedon Aug 8, 2014
What are the other options? Prepared statements and forcing the $1, $2...
syntax? If parameter processing is actually done correctly, I see no
problems using it. MySQL uses it, Postgresql jdbc uses it etc.
Den 8 aug 2014 01:49 skrev "Glen Parker" [email protected]:
roji commentedon Aug 8, 2014
Although I'd really like to agree with @glenebob (I don't like the idea of having a parser in Npgsql very much), @Emill's last argument is pretty convincing; the Postgresql backend expects parameters with $1, $2, while the ADO.NET contract requires us to support @hello, @goodbye (even though we don't actually support this at the moment, right?).
So if we want to say we're an ADO.NET provider it doesn't seem like we have a choice but to do client-side parsing... I'd say this should be a major focus of for the next release (version 3.0?), including named parameter support if possible...
Emill commentedon Aug 8, 2014
@PARAM is currently equivalent to :param
Den 8 aug 2014 10:01 skrev "Shay Rojansky" [email protected]:
roji commentedon Aug 8, 2014
Sorry, wasn't sure whether we supported this...
franciscojunior commentedon Aug 8, 2014
Em 08/08/2014 05:02, "Emill" [email protected] escreveu:
Initially , Npgsql only supported :param syntax. But then we added support
for @PARAM in order to facilitate code porting from Ado.net sqlclient code.
franciscojunior commentedon Aug 8, 2014
Em 07/08/2014 14:50, "Emill" [email protected] escreveu:
Would be possible for those cases ' blah @BLAH' and "blah @BLAH" be handled
using regular expressions?
I mean, would it be possible that given the following query:
Select * from table where field1 = :param1 and field2 = ':param1' and
field3 = ":param3"
It would be possible to have an expression which would return us the
following tokens:
Select * from table where field1 =
:param1
and field2 = ':param1' and field3 = ":param3"
It would ignore :param syntax when they appear inside ' or ".
Another point which should be considered is that Glen's code is performance
oriented. I don't know if a regular expression, if possible to handle that,
would have a big performance impact.
glenebob commentedon Aug 8, 2014
I don't have numbers now, but the regex code I replaced was pretty slow.
The reason for that is that the query needed to be scanned at least once
for each parameter. I think it was twice to handle :param and @PARAM.
I'm not generally a big fan of regex, so I'm not very fluent with them, but
it seems like an extremely tall order for them to handle all the little
rules we have to follow here, perform well, and be the slightest bit
readable.
We have a decent lexer that's fast and works for most cases, so we're most
of the way there. Emill has discovered some additional cases that need to
be handled. Probly the worst of it will be the E prefix handling and
backslash escapes, because the lexer will need to know about server version
and make additional decisions. At any rate, what we have can be added to,
there's no reason to replace it, certainly not with regex.
-Glen
On Fri, Aug 8, 2014 at 6:43 AM, Francisco Figueiredo Jr. <
[email protected]> wrote:
franciscojunior commentedon Aug 8, 2014
I see. I think your concerns are valid. I also don't know how well this would run too, regarding the performance and simplicity of the regex expression.
Ok. I think you are right. Let's check how to fix the problems pointed out by Emil.
Also, would you mind to write an example of a worst case query?
Thanks in advance.
Emill commentedon Aug 8, 2014
Can anyone comment that? One scan of the string would be better than two.
glenebob commentedon Aug 8, 2014
The simple answer is that it was easier that way. We need to (or at least
should - we don't want to waste cycles and IO bandwidth doing a bunch of
parameter subs only to find out from the backend that a conditions
exists that we could have easily checked for) enforce that a query buffer
contains only one distinct command when doing extended queries. The check
could be done as part of the parameter substitution routine as you
mentioned.
-Glen
On Fri, Aug 8, 2014 at 12:30 PM, Emill [email protected] wrote:
roji commentedon Aug 8, 2014
Agree with @glenebob, doing one pass over the query for both parameter substitution and multi-query handling.
Emill commentedon Aug 9, 2014
Escaped strings can constist of multiple parts (split by whitespaces and at least one newline).
For example:
same as
E'a\'bstring continues here \':)'I'm currently enhancing the parser to handle all cases I've written about, so hold on ;)
roji commentedon Aug 14, 2014
Thanks for the contributions on this @Emill. I think we're all in agreement that there's no choice but to keep doing client-side substitution.
I'll go ahead and close this PR, if we locate any other bugs we can treat them in separate issues/PRs.