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
Update tests and docs for ActiveRecord::Sanitization #44429
Open
kmcphillips
wants to merge
1
commit into
rails:main
Choose a base branch
from
kmcphillips:ar-sanitization-docs
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+285
−13
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# frozen_string_literal: true | ||
|
||
require "cases/helper" | ||
require "models/post" | ||
|
||
class Mysql2SanitizeTest < ActiveRecord::Mysql2TestCase | ||
def test_sanitize_sql_for_conditions_from_docs | ||
actual = ActiveRecord::Base.sanitize_sql_for_conditions(["name=? and group_id=?", "foo'bar", 4]) | ||
expected = "name='foo\\'bar' and group_id='4'" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_for_conditions(["name=:name and group_id=:group_id", name: "foo'bar", group_id: 4]) | ||
expected = "name='foo\\'bar' and group_id='4'" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_for_conditions(["name='%s' and group_id='%s'", "foo'bar", 4]) | ||
expected = "name='foo\\'bar' and group_id='4'" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_for_conditions("name='foo''bar' and group_id='4'") | ||
expected = "name='foo''bar' and group_id='4'" | ||
assert_equal expected, actual | ||
end | ||
|
||
def test_sanitize_sql_for_assignment_from_docs | ||
actual = Post.sanitize_sql_for_assignment(["name=? and group_id=?", nil, 4]) | ||
expected = "name=NULL and group_id='4'" | ||
assert_equal expected, actual | ||
|
||
actual = Post.sanitize_sql_for_assignment(["name=:name and group_id=:group_id", name: nil, group_id: 4]) | ||
expected = "name=NULL and group_id='4'" | ||
assert_equal expected, actual | ||
|
||
actual = Post.sanitize_sql_for_assignment({ name: nil, group_id: 4 }) | ||
expected = "`posts`.`name` = NULL, `posts`.`group_id` = 4" | ||
assert_equal expected, actual | ||
|
||
actual = Post.sanitize_sql_for_assignment("name=NULL and group_id='4'") | ||
expected = "name=NULL and group_id='4'" | ||
assert_equal expected, actual | ||
end | ||
|
||
def test_sanitize_sql_for_order_from_docs | ||
skip "See https://github.com/rails/rails/issues/44312" | ||
actual = Post.sanitize_sql_for_order([Arel.sql("field(id, ?)"), [1, 3, 2]]) | ||
expected = "field(id, 1,3,2)" | ||
assert_equal expected, actual | ||
|
||
actual = Post.sanitize_sql_for_order("id ASC") | ||
expected = "id ASC" | ||
assert_equal expected, actual | ||
end | ||
|
||
def test_sanitize_sql_hash_for_assignment_from_docs | ||
actual = Post.sanitize_sql_hash_for_assignment({ status: nil, group_id: 1 }, "posts") | ||
expected = "`posts`.`status` = NULL, `posts`.`group_id` = 1" | ||
assert_equal expected, actual | ||
end | ||
|
||
def test_sanitize_sql_like_from_docs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically the |
||
actual = ActiveRecord::Base.sanitize_sql_like("100%") | ||
expected = "100\\%" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_like("snake_cased_string") | ||
expected = "snake\\_cased\\_string" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_like("100%", "!") | ||
expected = "100!%" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_like("snake_cased_string", "!") | ||
expected = "snake!_cased!_string" | ||
assert_equal expected, actual | ||
end | ||
|
||
def test_sanitize_sql_array_from_docs | ||
actual = ActiveRecord::Base.sanitize_sql_array(["name=? and group_id=?", "foo'bar", 4]) | ||
expected = "name='foo\\'bar' and group_id='4'" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_array(["name=:name and group_id=:group_id", name: "foo'bar", group_id: 4]) | ||
expected = "name='foo\\'bar' and group_id='4'" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_array(["name='%s' and group_id='%s'", "foo'bar", 4]) | ||
expected = "name='foo\\'bar' and group_id='4'" | ||
assert_equal expected, actual | ||
end | ||
end |
90 changes: 90 additions & 0 deletions
90
activerecord/test/cases/adapters/postgresql/sanitize_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# frozen_string_literal: true | ||
|
||
require "cases/helper" | ||
require "models/post" | ||
|
||
class PostgreSQLSanitizeTest < ActiveRecord::PostgreSQLTestCase | ||
def test_sanitize_sql_for_conditions_from_docs | ||
actual = ActiveRecord::Base.sanitize_sql_for_conditions(["name=? and group_id=?", "foo'bar", 4]) | ||
expected = "name='foo''bar' and group_id=4" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_for_conditions(["name=:name and group_id=:group_id", name: "foo'bar", group_id: 4]) | ||
expected = "name='foo''bar' and group_id=4" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_for_conditions(["name='%s' and group_id='%s'", "foo'bar", 4]) | ||
expected = "name='foo''bar' and group_id='4'" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_for_conditions("name='foo''bar' and group_id='4'") | ||
expected = "name='foo''bar' and group_id='4'" | ||
assert_equal expected, actual | ||
end | ||
|
||
def test_sanitize_sql_for_assignment_from_docs | ||
actual = Post.sanitize_sql_for_assignment(["name=? and group_id=?", nil, 4]) | ||
expected = "name=NULL and group_id=4" | ||
assert_equal expected, actual | ||
|
||
actual = Post.sanitize_sql_for_assignment(["name=:name and group_id=:group_id", name: nil, group_id: 4]) | ||
expected = "name=NULL and group_id=4" | ||
assert_equal expected, actual | ||
|
||
actual = Post.sanitize_sql_for_assignment({ name: nil, group_id: 4 }) | ||
expected = "\"name\" = NULL, \"group_id\" = 4" | ||
assert_equal expected, actual | ||
|
||
actual = Post.sanitize_sql_for_assignment("name=NULL and group_id='4'") | ||
expected = "name=NULL and group_id='4'" | ||
assert_equal expected, actual | ||
end | ||
|
||
def test_sanitize_sql_for_order_from_docs | ||
actual = Post.sanitize_sql_for_order([Arel.sql("field(id, ?)"), [1, 3, 2]]) | ||
expected = "field(id, 1,3,2)" | ||
assert_equal expected, actual | ||
|
||
actual = Post.sanitize_sql_for_order("id ASC") | ||
expected = "id ASC" | ||
assert_equal expected, actual | ||
end | ||
|
||
def test_sanitize_sql_hash_for_assignment_from_docs | ||
actual = Post.sanitize_sql_hash_for_assignment({ status: nil, group_id: 1 }, "posts") | ||
expected = "\"status\" = NULL, \"group_id\" = 1" | ||
assert_equal expected, actual | ||
end | ||
|
||
def test_sanitize_sql_like_from_docs | ||
actual = ActiveRecord::Base.sanitize_sql_like("100%") | ||
expected = "100\\%" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_like("snake_cased_string") | ||
expected = "snake\\_cased\\_string" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_like("100%", "!") | ||
expected = "100!%" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_like("snake_cased_string", "!") | ||
expected = "snake!_cased!_string" | ||
assert_equal expected, actual | ||
end | ||
|
||
def test_sanitize_sql_array_from_docs | ||
actual = ActiveRecord::Base.sanitize_sql_array(["name=? and group_id=?", "foo'bar", 4]) | ||
expected = "name='foo''bar' and group_id=4" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_array(["name=:name and group_id=:group_id", name: "foo'bar", group_id: 4]) | ||
expected = "name='foo''bar' and group_id=4" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_array(["name='%s' and group_id='%s'", "foo'bar", 4]) | ||
expected = "name='foo''bar' and group_id='4'" | ||
assert_equal expected, actual | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# frozen_string_literal: true | ||
|
||
require "cases/helper" | ||
require "models/post" | ||
|
||
class SQLite3SanitizeTest < ActiveRecord::SQLite3TestCase | ||
def test_sanitize_sql_for_conditions_from_docs | ||
actual = ActiveRecord::Base.sanitize_sql_for_conditions(["name=? and group_id=?", "foo'bar", 4]) | ||
expected = "name='foo''bar' and group_id=4" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_for_conditions(["name=:name and group_id=:group_id", name: "foo'bar", group_id: 4]) | ||
expected = "name='foo''bar' and group_id=4" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_for_conditions(["name='%s' and group_id='%s'", "foo'bar", 4]) | ||
expected = "name='foo''bar' and group_id='4'" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_for_conditions("name='foo''bar' and group_id='4'") | ||
expected = "name='foo''bar' and group_id='4'" | ||
assert_equal expected, actual | ||
end | ||
|
||
def test_sanitize_sql_for_assignment_from_docs | ||
actual = Post.sanitize_sql_for_assignment(["name=? and group_id=?", nil, 4]) | ||
expected = "name=NULL and group_id=4" | ||
assert_equal expected, actual | ||
|
||
actual = Post.sanitize_sql_for_assignment(["name=:name and group_id=:group_id", name: nil, group_id: 4]) | ||
expected = "name=NULL and group_id=4" | ||
assert_equal expected, actual | ||
|
||
actual = Post.sanitize_sql_for_assignment({ name: nil, group_id: 4 }) | ||
expected = "\"name\" = NULL, \"group_id\" = 4" | ||
assert_equal expected, actual | ||
|
||
actual = Post.sanitize_sql_for_assignment("name=NULL and group_id='4'") | ||
expected = "name=NULL and group_id='4'" | ||
assert_equal expected, actual | ||
end | ||
|
||
def test_sanitize_sql_for_order_from_docs | ||
actual = Post.sanitize_sql_for_order([Arel.sql("field(id, ?)"), [1, 3, 2]]) | ||
expected = "field(id, 1,3,2)" | ||
assert_equal expected, actual | ||
|
||
actual = Post.sanitize_sql_for_order("id ASC") | ||
expected = "id ASC" | ||
assert_equal expected, actual | ||
end | ||
|
||
def test_sanitize_sql_hash_for_assignment_from_docs | ||
actual = Post.sanitize_sql_hash_for_assignment({ status: nil, group_id: 1 }, "posts") | ||
expected = "\"status\" = NULL, \"group_id\" = 1" | ||
assert_equal expected, actual | ||
end | ||
|
||
def test_sanitize_sql_like_from_docs | ||
actual = ActiveRecord::Base.sanitize_sql_like("100%") | ||
expected = "100\\%" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_like("snake_cased_string") | ||
expected = "snake\\_cased\\_string" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_like("100%", "!") | ||
expected = "100!%" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_like("snake_cased_string", "!") | ||
expected = "snake!_cased!_string" | ||
assert_equal expected, actual | ||
end | ||
|
||
def test_sanitize_sql_array_from_docs | ||
actual = ActiveRecord::Base.sanitize_sql_array(["name=? and group_id=?", "foo'bar", 4]) | ||
expected = "name='foo''bar' and group_id=4" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_array(["name=:name and group_id=:group_id", name: "foo'bar", group_id: 4]) | ||
expected = "name='foo''bar' and group_id=4" | ||
assert_equal expected, actual | ||
|
||
actual = ActiveRecord::Base.sanitize_sql_array(["name='%s' and group_id='%s'", "foo'bar", 4]) | ||
expected = "name='foo''bar' and group_id='4'" | ||
assert_equal expected, actual | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is currently broken #44312 and is what I am working to fix next.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Best to fix it all in the same PR, I'd be worried the skip may be forgotten otherwise.