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

Handle broken encoding in #write_query? #43821

Merged
merged 1 commit into from
Dec 10, 2021

Conversation

casperisfine
Copy link
Contributor

@casperisfine casperisfine commented Dec 9, 2021

If the SQL encoding somehow is invalid, Regexp#match? raises ArgumentError.

Best we can do is to copy the string and try to match the regexp in "binary" mode.

Hopefully these cases are rare enough that the string copy shouldn't be an important overhead.

cc @rafaelfranca (we might want to backport this to 7.0).
cc @CelineBen who initially reported the issue to me.
cc @kamipo @eileencodes because I believe you worked on this area.

Also PS: I though this code was introduced in 7, but it might actually be older than that.

@ghiculescu
Copy link
Member

Also PS: I though this code was introduced in 7, but it might actually be older than that.

It was added in Rails 6 #34505

Copy link
Member

@eileencodes eileencodes left a comment

Choose a reason for hiding this comment

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

Note this code was changed around a bit since implementation in 6.0, but yes this should be backported to any affected version.

@casperisfine
Copy link
Contributor Author

👍 I can open the backport PRs tomorrow.

If the SQL encoding somehow is invalid, `Regexp#match?` raises `ArgumentError`.

Best we can do is to copy the string and try to match
the regexp in "binary" mode.

Hopefully these cases are rare enough that the string copy
should be an important overhead.
@byroot byroot merged commit aa32f7e into rails:main Dec 10, 2021
casperisfine pushed a commit to Shopify/rails that referenced this pull request Dec 10, 2021
Backport of rails#43821

If the SQL encoding somehow is invalid, `Regexp#match?` raises `ArgumentError`.

Best we can do is to copy the string and try to match
the regexp in "binary" mode.

Hopefully these cases are rare enough that the string copy
should be an important overhead.
@casperisfine
Copy link
Contributor Author

Since I don't think this can be considered a security issue, I created backport PRs for 7.0 and 6.1.

@casperisfine
Copy link
Contributor Author

Urk, I just noticed part of my change can cause a warning:

>> /foo/n.match?("SELECT '€'")
warning: historical binary regexp match /.../n against UTF-8 string

In the end the /n isn't necessary, it was just extra caution, I'll remove it.

casperisfine pushed a commit to Shopify/rails that referenced this pull request Dec 10, 2021
Backport of rails#43821

If the SQL encoding somehow is invalid, `Regexp#match?` raises `ArgumentError`.

Best we can do is to copy the string and try to match
the regexp in "binary" mode.

Hopefully these cases are rare enough that the string copy
should be an important overhead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants