From e92eef04ccaf4f6a3944dcac93ad0510756c71d0 Mon Sep 17 00:00:00 2001 From: fiveNinePlusR Date: Sun, 29 Sep 2024 19:58:37 -0700 Subject: [PATCH 1/2] add some security fixes for brakeman --- .../v1/location_machine_xrefs_controller.rb | 6 +- .../v1/location_picture_xrefs_controller.rb | 4 +- app/models/location.rb | 4 +- app/models/machine_score_xref.rb | 8 +- app/models/suggested_location.rb | 12 ++- app/models/user.rb | 2 +- app/views/locations/_form.html.haml | 2 +- app/views/locations/_locations.html.haml | 2 +- bin/bundle-audit | 27 ++++++ bin/bundler-audit | 27 ++++++ config/brakeman.ignore | 94 +++++++++++++++++++ config/initializers/cookies_serializer.rb | 2 +- 12 files changed, 170 insertions(+), 20 deletions(-) create mode 100755 bin/bundle-audit create mode 100755 bin/bundler-audit create mode 100644 config/brakeman.ignore diff --git a/app/controllers/api/v1/location_machine_xrefs_controller.rb b/app/controllers/api/v1/location_machine_xrefs_controller.rb index f55a90568..1334c37f6 100644 --- a/app/controllers/api/v1/location_machine_xrefs_controller.rb +++ b/app/controllers/api/v1/location_machine_xrefs_controller.rb @@ -38,12 +38,12 @@ def create return return_response(AUTH_REQUIRED_MSG, 'errors') if user.nil? - location_id = params[:location_id] - machine_id = params[:machine_id] + location_id = params[:location_id].to_i + machine_id = params[:machine_id].to_i condition = params[:condition] status_code = 200 - return return_response('Failed to find machine', 'errors') if machine_id.nil? || location_id.nil? || !Machine.exists?(machine_id) || !Location.exists?(location_id) + return return_response('Failed to find machine', 'errors') if machine_id.zero? || location_id.zero? || !Machine.exists?(machine_id) || !Location.exists?(location_id) lmx = LocationMachineXref.find_by_location_id_and_machine_id(location_id, machine_id) diff --git a/app/controllers/api/v1/location_picture_xrefs_controller.rb b/app/controllers/api/v1/location_picture_xrefs_controller.rb index 867a831f3..a9579071e 100644 --- a/app/controllers/api/v1/location_picture_xrefs_controller.rb +++ b/app/controllers/api/v1/location_picture_xrefs_controller.rb @@ -21,8 +21,8 @@ def show def create return return_response(AUTH_REQUIRED_MSG, 'errors') if current_user.nil? - location_id = params[:location_id] - return return_response('Failed to find location', 'errors') if location_id.nil? || !Location.exists?(location_id) + location_id = params[:location_id].to_i + return return_response('Failed to find location', 'errors') if location_id.zero? || !Location.exists?(location_id) photo = params[:photo] return return_response('Missing photo to add', 'errors') if photo.nil? diff --git a/app/models/location.rb b/app/models/location.rb index e14302fd3..e3352c82d 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -3,8 +3,8 @@ class Location < ApplicationRecord validates_presence_of :name, :street, :city, :country validates :phone, phone: { possible: true, allow_blank: true, message: 'Phone format not valid.' } - validates :website, format: { with: %r{http(s?)://}, message: 'must begin with http:// or https://' }, if: :website? - validates :name, :street, :city, format: { with: /^\S.*/, message: "Can't start with a blank", multiline: true } + validates :website, format: { with: %r{\Ahttp(s?)://}, message: 'must begin with http:// or https://' }, if: :website? + validates :name, :street, :city, format: { with: /\A\S.*/, message: "Can't start with a blank", multiline: true } validates :lat, :lon, presence: { message: 'Latitude/Longitude failed to generate. Please double check address and try again, or manually enter the lat/lon' } belongs_to :location_type, optional: true diff --git a/app/models/machine_score_xref.rb b/app/models/machine_score_xref.rb index c55b44955..6f17986a0 100644 --- a/app/models/machine_score_xref.rb +++ b/app/models/machine_score_xref.rb @@ -8,8 +8,8 @@ class MachineScoreXref < ApplicationRecord scope :zone_id, lambda { |id| joins(:location_machine_xref).joins(:location).where(" - locations.zone_id = #{id} - ") + locations.zone_id = ? + ", id) } scope :region, lambda { |name| @@ -17,8 +17,8 @@ class MachineScoreXref < ApplicationRecord joins(:location_machine_xref).joins(:location).where(" location_machine_xrefs.id = machine_score_xrefs.location_machine_xref_id and locations.id = location_machine_xrefs.location_id - and locations.region_id = #{r.id} - ") + and locations.region_id = ? + ", r.id) } def username diff --git a/app/models/suggested_location.rb b/app/models/suggested_location.rb index 98e4fced1..d62fb5850 100644 --- a/app/models/suggested_location.rb +++ b/app/models/suggested_location.rb @@ -5,8 +5,8 @@ class SuggestedLocation < ApplicationRecord validates_presence_of :name, :machines, on: :create validates_presence_of :street, :city, :zip, on: :update - validates :website, format: { with: %r{http(s?)://}, message: 'must begin with http:// or https://' }, if: :website?, on: :update - validates :name, :street, :city, format: { with: /^\S.*/, message: "Can't start with a blank", multiline: true }, on: :update + validates :website, format: { with: %r{\Ahttp(s?)://}, message: 'must begin with http:// or https://' }, if: :website?, on: :update + validates :name, :street, :city, format: { with: /\A\S.*/, message: "Can't start with a blank", multiline: true }, on: :update validates :lat, :lon, presence: { message: 'Latitude/Longitude failed to generate. Please double check address and try again, or manually enter the lat/lon' }, on: :update belongs_to :region, optional: true @@ -97,19 +97,21 @@ def convert_to_location(user_email) delete - ActiveRecord::Base.connection.execute(< Date: Sun, 29 Sep 2024 20:08:06 -0700 Subject: [PATCH 2/2] make rubocop happy --- app/controllers/machines_controller.rb | 8 ++++---- app/controllers/pages_controller.rb | 4 ++-- spec/features/location_machine_xrefs_controller_spec.rb | 1 - spec/features/locations_controller_spec.rb | 2 -- spec/features/pages_controller_spec.rb | 2 +- spec/requests/api/v1/locations_controller_spec.rb | 2 +- spec/spec_helper.rb | 1 - 7 files changed, 8 insertions(+), 12 deletions(-) diff --git a/app/controllers/machines_controller.rb b/app/controllers/machines_controller.rb index ace761c55..f5fa9dc47 100644 --- a/app/controllers/machines_controller.rb +++ b/app/controllers/machines_controller.rb @@ -28,11 +28,11 @@ def autocomplete sanitized_sql = ActiveRecord::Base.sanitize_sql_array([sql, { term: params[:term] }]) results = ActiveRecord::Base.connection.select_all(sanitized_sql) - .map do |m| - name_year = "#{m['name']} (#{m['manufacturer']}, #{m['year']})" + .map do |m| + name_year = "#{m['name']} (#{m['manufacturer']}, #{m['year']})" - { label: name_year, value: name_year, id: m['id'], group_id: m['machine_group_id'] } - end + { label: name_year, value: name_year, id: m['id'], group_id: m['machine_group_id'] } + end end diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index cca1c3dae..59fa33ccb 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -52,7 +52,7 @@ def map params[:user_faved] = user.id if user && !params[:user_faved].blank? - if !params[:by_location_id].blank? && loc = Location.where(id: params[:by_location_id]).first + if !params[:by_location_id].blank? && (loc = Location.where(id: params[:by_location_id]).first) @title_params[:title] = loc.name location_type = loc.location_type.name + ' - ' unless loc.location_type.nil? machine_list = ' - ' + loc.machine_names_first_no_year.join(', ') unless loc.machine_names_first_no_year.empty? @@ -74,7 +74,7 @@ def region @location_count = @locations.count @lmx_count = @region.machines_count - if !params[:by_location_id].blank? && loc = Location.where(id: params[:by_location_id]).first + if !params[:by_location_id].blank? && (loc = Location.where(id: params[:by_location_id]).first) @title_params[:title] = loc.name location_type = loc.location_type.name + ' - ' unless loc.location_type.nil? machine_list = ' - ' + loc.machine_names_first_no_year.join(', ') unless loc.machine_names_first_no_year.empty? diff --git a/spec/features/location_machine_xrefs_controller_spec.rb b/spec/features/location_machine_xrefs_controller_spec.rb index ca55153ac..f3637f68f 100644 --- a/spec/features/location_machine_xrefs_controller_spec.rb +++ b/spec/features/location_machine_xrefs_controller_spec.rb @@ -658,7 +658,6 @@ end it 'allows case insensitive searches of a region' do - chicago_region = FactoryBot.create(:region, name: 'chicago', full_name: 'Chicago') FactoryBot.create(:location, id: 23, region: chicago_region, name: 'Chicago Location') diff --git a/spec/features/locations_controller_spec.rb b/spec/features/locations_controller_spec.rb index 99ab2e266..e08199799 100644 --- a/spec/features/locations_controller_spec.rb +++ b/spec/features/locations_controller_spec.rb @@ -111,7 +111,6 @@ before(:each) do @location = FactoryBot.create(:location, region_id: @region.id, name: 'Cleo') @machine = FactoryBot.create(:machine, name: 'Bawb') - end it 'removes a machine from a location' do @@ -531,7 +530,6 @@ @user = FactoryBot.create(:user) login(@user) - @location = FactoryBot.create(:location, region: @region, name: 'Cleo') end diff --git a/spec/features/pages_controller_spec.rb b/spec/features/pages_controller_spec.rb index 2737ca6db..62f1d2974 100644 --- a/spec/features/pages_controller_spec.rb +++ b/spec/features/pages_controller_spec.rb @@ -527,7 +527,7 @@ visit "/#{@region.name}/high_rollers" expect(page).to have_title('High Scores') - visit "/map/?by_location_id=1234" + visit '/map/?by_location_id=1234' expect(page).to have_title('Pinball Map') visit "/map/?by_location_id=#{@location.id}" diff --git a/spec/requests/api/v1/locations_controller_spec.rb b/spec/requests/api/v1/locations_controller_spec.rb index 1da8fc3e4..08553bb99 100644 --- a/spec/requests/api/v1/locations_controller_spec.rb +++ b/spec/requests/api/v1/locations_controller_spec.rb @@ -831,7 +831,7 @@ it 'respects no_details and shows fewer location fields' do lmx = FactoryBot.create(:location_machine_xref, location: @location, machine: FactoryBot.create(:machine, id: 7777, name: 'Cleo')) FactoryBot.create(:machine_condition, location_machine_xref_id: lmx.id, comment: 'foo bar') - FactoryBot.create(:machine_score_xref, location_machine_xref: lmx, score: 567890) + FactoryBot.create(:machine_score_xref, location_machine_xref: lmx, score: 567_890) get "/api/v1/locations/#{@location.id}.json", params: { no_details: 1 } expect(response.body).to include('Satchmo') diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7eab97aab..0398cfa81 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -14,7 +14,6 @@ include Sprockets::Rails::Helper include ActiveSupport::Testing::TimeHelpers - SimpleCov.start Coveralls.wear!