Skip to content

*: introduce new API ParseWithParams#22499

Merged
ti-srebot merged 21 commits intopingcap:masterfrom
xhebox:injection
Jan 26, 2021
Merged

*: introduce new API ParseWithParams#22499
ti-srebot merged 21 commits intopingcap:masterfrom
xhebox:injection

Conversation

@xhebox
Copy link
Contributor

@xhebox xhebox commented Jan 22, 2021

What problem does this PR solve?

Problem Summary: This PR adds a new API ParseWithParams to help process unsafe arguments than just fmt.Sprintf. Also a helper API ExecuteInternal that is using ParseWithParams and ExecuteStmt.

I did not use PrepareStmt since it is not possible to use placeholder like select * from t where c in ?. But we do have such requirement.

ExecuteInternal is redefined to use ParseWithParams and always use utf8 charset for safety. But it is still needed to modify cases like ExecuteInternal(fmt.Sprintf(...)).

ExecRestrictedSQL is too large, thus it is both annoying and duplicated to write a new RestrictedSQLExecutor based on ParseWithParams. From the git history, this is a very legacy API that is 4 or 5 years old. It should be removed/refactored in further works. So the current plan is, write like ExecRestrictedSQL(session.EscapeSQL(sql, args...)). And it will goes to the modified ExecuteInternal eventually to use utf8 charset to prevent attacks based on charsets.

Check List

Tests

  • Unit test
  • Integration test

Release note

  • No release note

Signed-off-by: xhe <xw897002528@gmail.com>
Signed-off-by: xhe <xw897002528@gmail.com>
@xhebox xhebox requested a review from a team as a code owner January 22, 2021 09:59
@xhebox xhebox requested review from wshwsh12 and removed request for a team January 22, 2021 09:59
@github-actions github-actions bot added sig/execution SIG execution sig/sql-infra SIG: SQL Infra labels Jan 22, 2021
Signed-off-by: xhe <xw897002528@gmail.com>
Signed-off-by: xhe <xw897002528@gmail.com>
Signed-off-by: xhe <xw897002528@gmail.com>
Signed-off-by: xhe <xw897002528@gmail.com>
@morgo
Copy link
Contributor

morgo commented Jan 24, 2021

LGTM

@ti-srebot
Copy link
Contributor

@morgo, Thanks for your review. The bot only counts LGTMs from Reviewers and higher roles, but you're still welcome to leave your comments. See the corresponding SIG page for more information. Related SIGs: execution(slack),sql-infra(slack).

@tiancaiamao tiancaiamao self-requested a review January 25, 2021 03:04
buf[pos] = '\\'
buf[pos+1] = '\\'
pos += 2
default:
Copy link
Contributor

Choose a reason for hiding this comment

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

Is \t a special one?

Copy link
Contributor Author

@xhebox xhebox Jan 25, 2021

Choose a reason for hiding this comment

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

I think \t should be OK. It is just another type of whitespace. And the original port did not include that, either.

Copy link
Contributor

Choose a reason for hiding this comment

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

In the MySQL document, there is a "Table 9.1 Special Character Escape Sequences"
https://dev.mysql.com/doc/refman/8.0/en/string-literals.html

I'm not sure are they the same things.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that is the reverse of escaping. The table means that mysql will interpret \t as 0x09 in string datum. And it only takes effect if NO_BACKSLASH_ESCAPES is off.

Here what we want is, disallow the parser/lexer treating any character in the string as any special term/token.

Signed-off-by: xhe <xw897002528@gmail.com>
Signed-off-by: xhe <xw897002528@gmail.com>
Signed-off-by: xhe <xw897002528@gmail.com>
Signed-off-by: xhe <xw897002528@gmail.com>
Signed-off-by: xhe <xw897002528@gmail.com>
@xhebox
Copy link
Contributor Author

xhebox commented Jan 27, 2021

/label needs-cherry-pick-3.0

@xhebox
Copy link
Contributor Author

xhebox commented Jan 27, 2021

/run-cherry-pick

ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Jan 27, 2021
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-srebot
Copy link
Contributor

cherry pick to release-5.0-rc in PR #22547

@ti-srebot
Copy link
Contributor

cherry pick to release-4.0 in PR #22548

@ti-srebot
Copy link
Contributor

cherry pick to release-3.1 failed

@ti-srebot
Copy link
Contributor

cherry pick to release-3.0 in PR #22549

@xhebox xhebox changed the title *: preventing SQL injection *: introduce new API ParseWithParams Jan 27, 2021
ti-srebot added a commit that referenced this pull request Feb 2, 2021
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
Signed-off-by: xhe <xw897002528@gmail.com>
@xhebox
Copy link
Contributor Author

xhebox commented Feb 20, 2021

/label needs-cherry-pick-2.1

@ti-srebot
Copy link
Contributor

These labels are not found needs-cherry-pick-2.1.

xhebox added a commit to ti-srebot/tidb that referenced this pull request Feb 20, 2021
Signed-off-by: xhe <xw897002528@gmail.com>
xhebox added a commit to xhebox/tidb that referenced this pull request Feb 20, 2021
Signed-off-by: xhe <xw897002528@gmail.com>
ti-srebot added a commit that referenced this pull request Feb 20, 2021
Signed-off-by: xhe <xw897002528@gmail.com>
xhebox added a commit to ti-srebot/tidb that referenced this pull request Feb 25, 2021
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

require-LGT3 Indicates that the PR requires three LGTM. sig/execution SIG execution sig/sql-infra SIG: SQL Infra status/can-merge Indicates a PR has been approved by a committer. status/LGT3 The PR has already had 3 LGTM.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants