*: refactor the RestrictedSQLExecutor interface#22579
*: refactor the RestrictedSQLExecutor interface#22579ti-srebot merged 7 commits intopingcap:masterfrom
Conversation
| // Attention: it does not prevent you from doing parse("select '%?", ";SQL injection!;") => "select '';SQL injection!;'". | ||
| // One argument should be a standalone entity. It should not "concat" with other placeholders and characters. | ||
| // This function only saves you from processing potentially unsafe parameters. | ||
| ParseWithParams(ctx context.Context, sql string, args ...interface{}) ([]ast.StmtNode, error) |
There was a problem hiding this comment.
What is the reason to remove it in the interface?
There was a problem hiding this comment.
The external SQL all use ExecuteStmt or ExecutePrepared ...
And we have the ExecuteInternal for internal SQL already, and also the RestrictedSQLExecutor, there is no need to include this method in the interface.
There was a problem hiding this comment.
Do we know that we can convert most uses of Execute to ExecuteInternal safely? My understanding is that ExecuteInternal is similar to Restricted, which is that it does not binary log, runs in the server permissions etc.
If we expect that COM_QUERY is the only valid case of "Execute", then that's fine. It is already switched over to ExecuteStmt (as you mention).
There was a problem hiding this comment.
OK, but you still leave it in the RestrictedSQLExecutor interface. I would like both-existing or both-none.
And you can completely extract SQL execution methods into another subinterface... Session could embed all sql executor interfaces,
There was a problem hiding this comment.
We can clean up the code after the refactor finish:
Step1: introduce the new API for RestrictedSQLExecutor
Step2: use the new API to replace the old one
Step3: remove the old API
We can embed RestrictedSQLExecutor into the Session interface like this:
type Session interface {
RestrictedSQLExecutor
...
}
Keep each interface small and composable.... after we clean the usage of the old ones.
|
I like it - much cleaner. Please fix CI: |
xhebox
left a comment
There was a problem hiding this comment.
ParseWithParams with handle quotes for you. Adding quotes will cause problems.
|
LGTM |
| // Parse is deprecated, use ParseWithParams() instead. | ||
| Parse(ctx context.Context, sql string) ([]ast.StmtNode, error) |
There was a problem hiding this comment.
If we remove ParseWithParams, this comment is now incorrect.
Btw: I discovered in one of my PRs that Parse can not be deprecated. It is important to have a Parse function which does not use placeholders so that a statement like this one can be parsed and then executed by ExecuteStmt:
mysql> select count(*) from t1 where formatted like '%ndan%'; |
ERROR 1105 (HY000): missing arguments, need 1-th arg, but only got 0 args
There was a problem hiding this comment.
okay, when I finish the rewrite, I'll clean up some deprecated methods and update the comment here.
|
LGTM |
|
/merge @morgo 's review is valid. pingcap/community#398 |
|
LGTM |
|
/merge |
|
/run-all-tests |
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
|
cherry pick to release-4.0 in PR #22621 |
|
Do we backport this to 5.0 and 4.0? If not, we can close the cherry-pick PRs. @tiancaiamao |
|
/label needs-cherry-pick-5.0-rc |
|
/run-cherry-picks |
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
|
cherry pick to release-5.0-rc in PR #22687 |
What problem does this PR solve?
Problem Summary:
Improve security for our code.
What is changed and how it works?
The initial definition of
RestrictedSQLExecutorlooks like this:It's not good for security reasons, the usage pattern looks like this:
Later on, we add more methods to the interface which IMO, ugly:
I propose to use this one as its new definition:
This interface is secure, and easy to use:
What's Changed:
ParseWithParams()now returnsast.StmtNoderather than[]ast.StmtNode, multiple statements are not allowed here.Update the "util", "store", and "domain" packages, ensure they use the new (secure) API.
How it Works:
Related changes
Check List
Tests
Side effects
API change
Release note