Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8926463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:58:41+00:00 2026-06-15T07:58:41+00:00

How can I avoid a brakeman warning in Rails when constructing an order method

  • 0

How can I avoid a brakeman warning in Rails when constructing an order method from parameters?

def index
  @methods = [:name, :manager, :deadline]
  assignments = Assignment.order(sort_column(@methods) + " " + sort_direction).received(current_user).root
end

def sort_column(column_names)
  column_names.each do |column|
    return column if column == params[:sort]
  end
  return 'updated_at'
end

def sort_direction
  params[:direction] == 'asc' ? 'asc' : 'desc'
end

I’m working hard to avoid ever putting user-generated code directly into the query, but brakeman still alerts (medium confidence) that this is a SQL injection vulnerability.

Is this a false positive? If not, how do I correct the vulnerability?

If so, is there an easy way to avoid the false positive?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:58:42+00:00Added an answer on June 15, 2026 at 7:58 am

    Okay, this is too long for a comment.

    From my testing, moving the string building into a method like this does make the warning go away:

    def index
      @methods = [:name, :manager, :deadline]
      assignments = Assignment.order(sort_order).received(current_user).root
    end
    
    def sort_order
      sort_column(@methods) + " " + sort_direction
    end
    

    However, that’s just hiding the problem. I would suggest adding something like this to the Assignment model instead:

    class Assignment < ActiveRecord::Base
    
      def self.sorted_by(column, direction)
        direction = direction.downcase == 'asc' ? 'asc' : 'desc'
        column = sanitize_sql(column)
        order("#{column} #{direction}")
      end
    
    end
    

    Just keep in mind that sometimes you have to choose between keeping a tool happy and keeping your code reasonable. As for the false positive, I don’t see this particular issue being resolved, since it is not simple to inspect sort_column and know it is safe.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know in Jenkins you can avoid duplicate builds from running on top of
Can I avoid third party code from creating new threads, starting new VMs, or
Can anybody tell me about how I can avoid mapping some property from my
Is there a way, where i can avoid the '\' character from a string
How i can avoid cross-site-scripting, by knowing, from what site user is requesting data?
The str_replace function with a strpos checking can avoid extra work? METHOD 1 ...
With C# anonymous delegates, we can avoid declaration of extra method, I know in
Is there any way I can avoid using array_flip to optimize performance. I am
I read of a useful trick about how you can avoid using the wrong
Can I avoid the open DataReader exception (There is already an open DataReader associated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.