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

Build SQL expressions with Arel instead of string interpolation #2418

Merged
merged 1 commit into from
Dec 12, 2023

Conversation

thoughtbot-summer
Copy link
Member

There, but for the grace of column_exist?, go we.

In Administrate::Order, there are several places where we are building SQL expressions using string interpolation, e.g. order = "#{relation.table_name}.#{attribute} #{direction}". Doing this can be dangerous, because it's easy to introduce a SQL injection vulnerability. Thankfully, it seems like this class is carefully designed in a way that prevents such a vulnerability. Nevertheless, it feels precarious; if someone makes changes, they have to be very careful to keep the code safe.

This change replaces all string-interpolated SQL expressions with Arel expressions. The database adapter will then automatically quote & sanitize the components of these expressions.

Copy link
Member

@nickcharlton nickcharlton left a comment

Choose a reason for hiding this comment

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

An excellent idea, thanks!

@@ -0,0 +1,5 @@
RSpec::Matchers.define :to_sql do |sql|
match do |actual|
actual.to_sql == sql
Copy link
Member

Choose a reason for hiding this comment

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

Oh, nice.

In Administrate::Order, there are several places where we are building
SQL expressions using string interpolation, e.g. order =
"#{relation.table_name}.#{attribute} #{direction}". Doing this can be
dangerous, because it's easy to introduce a SQL injection vulnerability.
Thankfully, it seems like this class is carefully designed in a way that
prevents such a vulnerability. Nevertheless, it feels precarious; if
someone makes changes, they have to be very careful to keep the code
safe.

This change replaces all string-interpolated SQL expressions with Arel
expressions. The database adapter will then automatically quote &
sanitize the components of these expressions.
@nickcharlton nickcharlton force-pushed the replace-raw-sql-with-arel branch from e2e2b8f to b215833 Compare December 12, 2023 17:05
@nickcharlton
Copy link
Member

I just rebased (only actual change I made was to add the contents of your PR description to the commit itself) and I'll merge this in a bit.

@nickcharlton nickcharlton merged commit 0f27027 into main Dec 12, 2023
9 checks passed
@nickcharlton nickcharlton deleted the replace-raw-sql-with-arel branch December 12, 2023 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants