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

Prevent SQL Injection #142

Open
alencarandre opened this issue Feb 15, 2018 · 2 comments
Open

Prevent SQL Injection #142

alencarandre opened this issue Feb 15, 2018 · 2 comments

Comments

@alencarandre
Copy link

Please, help me.

I'm searching a way to prevent SQL Injection using Smart Listing.

For instance:

In my view:

<th><%= smart_listing.sortable ScheduledService.human_attribute_name("customer"), :customer_id %></th>

Params generated by Smart Listing:

?scheduled_service_smart_listing[page]=&scheduled_service_smart_listing[per_page]=10&scheduled_service_smart_listing[sort][customer_id]=asc

If I change scheduled_service_smart_listing[sort][customer_id]=asc for scheduled_service_smart_listing[sort][customer_id; delete from schedule_services where id = 1; --]=asc

Give me this error

PG::SyntaxError: ERROR:  cannot insert multiple commands into a prepared statement
: SELECT  "scheduled_services".* FROM "scheduled_services" ORDER BY customer_id; delete from schedule_services where id = 1; -- asc LIMIT $1 OFFSET $2

See that the DELETE instruction was delivered to database. Not executed, but, delivered and it's a problem. Has a way to avoid that?

@sigra
Copy link

sigra commented Apr 9, 2018

I faced with the same problem and found a decision that works for me.

So, when you use sort_attributes option then library will use only that attributes and skip bad one. I don't know why this is not said in the official documentation.

Example:

sort_aliases = [
  [:id, "users.id"],
  [:email, "users.email"],
  [:name, "users.metadata->>'name'"] # you can sort JSONB too
]
@users = smart_listing_create(:users, User, sort_attributes: sort_aliases)

# view part
<th><%= smart_listing.sortable 'ID', :id %></th>
<th><%= smart_listing.sortable 'Name', :name %></th>

So, when I change params for "sort"=>{"id;TRUNCATE users;--"=>"asc"}} it just ignored and nothing will happen.

@korun
Copy link

korun commented Jul 24, 2019

Fixed in 0794ed4 (v1.2.3), but it can cause some issues on update for some complicated queries with sort on joined tables (see #158 for more)

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

No branches or pull requests

3 participants