Skip to content

Commit 2d765cb

Browse files
authored
GitHub fixes (#1580)
Changes relating to GitHub code scanners. The code scanners reported a bunch of small issues, none of them critical. Fixes here. Sometimes, it's just writing the code in a slighly different way.
1 parent 3768023 commit 2d765cb

18 files changed

Lines changed: 62 additions & 58 deletions

File tree

.github/workflows/cbrain_ci.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ jobs:
2020
runs-on: ubuntu-24.04
2121
env:
2222
RAILS_ENV: test
23+
permissions:
24+
contents: read
25+
pull-requests: read
2326

2427
###########################################################
2528
services:

Bourreau/spec/boutiques/boutiques_tester_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@
392392
@task.user_id, @task.group_id = UID, GID
393393
# Generate a simulated exit file, as if the task had run
394394
@simExitFile = @task.exit_cluster_filename
395-
IO.write( @simExitFile, "0\n" )
395+
File.write( @simExitFile, "0\n" )
396396
# The basic properties for the required output file
397397
@reqOutfileProps = {:name => @fname_base, :data_provider_id => @provider.id}
398398
# Optional output file properties
@@ -428,11 +428,11 @@
428428
expect( @task.save_results ).to be false
429429
end
430430
it "save_results is false if the exit status file has invalid content" do
431-
IO.write( @simExitFile, "abcde\n" )
431+
File.write( @simExitFile, "abcde\n" )
432432
expect( @task.save_results ).to be false
433433
end
434434
it "save_results is false if the exit status file contains a value greater than 1" do
435-
IO.write( @simExitFile, "3\n" )
435+
File.write( @simExitFile, "3\n" )
436436
expect( @task.save_results ).to be false
437437
end
438438

BrainPortal/app/controllers/portal_controller.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -424,16 +424,6 @@ def report #:nodoc:
424424
def search
425425
@search = params[:search]
426426
@limit = 20 # used by interface only
427-
428-
# In development mode, classes are loaded at first use. This means a dev
429-
# will sometimes NOT see a class (e.g. TextFile) until first use, which means
430-
# that some parts of the interface will not show them. This trick allows a dev
431-
# to force the load of a class just by typing the name in the search box.
432-
# The string HAS to be something like 'TextFile' or 'TarArchive' etc.
433-
if Rails.env == 'development' && @search.present? && @search.to_s =~ /\A[A-Z]\w+\z/
434-
eval @search.to_s rescue nil # just load a class, if needed
435-
end
436-
437427
@results = @search.present? ? ModelsReport.search_for_token(@search, current_user) : {}
438428
end
439429

BrainPortal/app/controllers/quotas_controller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,10 +395,10 @@ def base_scope #:nodoc:
395395
# Tries to turn strings like '3 mb' into 3_000_000 etc.
396396
# Supported suffixes are T, G, M, K, TB, GB, MB, KB, B (case insensitive).
397397
def guess_size_units(sizestring)
398-
match = sizestring.match(/\A\s*(-?\d*\.?\d+)\s*([tgmk]?)\s*b?\s*\z/i)
398+
match = sizestring.match(/\A\s*(-?\d{1,5}(\.\d{1,2})?)\s*([tgmk]?)\s*b?\s*\z/i)
399399
return "" unless match # parsing error
400400
number = match[1]
401-
suffix = match[2].presence&.downcase || 'u'
401+
suffix = match[3].presence&.downcase || 'u'
402402
mult = { 't' => 1_000_000_000_000, 'g' => 1_000_000_000, 'm' => 1_000_000, 'k' => 1_000, 'u' => 1 }
403403
totbytes = number.to_f * mult[suffix]
404404
totbytes = totbytes.to_i
@@ -409,10 +409,10 @@ def guess_size_units(sizestring)
409409
# Supported suffixes are s, h, d, m, w, and y (case insensitive).
410410
# Minutes not supported because of the sad existance of months.
411411
def guess_time_units(timestring)
412-
match = timestring.match(/\A\s*(\d*\.?\d+)\s*([shdwmy]?)\s*\z/i)
412+
match = timestring.match(/\A\s*(\d{1,4}(\.\d{1,2})?)\s*([shdwmy]?)\s*\z/i)
413413
return "" unless match # parsing error
414414
number = match[1]
415-
suffix = match[2].presence&.downcase || 's'
415+
suffix = match[3].presence&.downcase || 's'
416416
mult = { 's' => 1.second, 'h' => 1.hour, 'd' => 1.day,
417417
'w' => 1.week, 'm' => 1.month, 'y' => 1.year, }
418418
tottime = number.to_f * mult[suffix].to_i

BrainPortal/app/controllers/userfiles_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def display
370370

371371
# No viewer
372372
if ! @viewer
373-
render :html => "<div class=\"warning\">Could not find viewer #{viewer_name}.</div>".html_safe, :status => "404"
373+
render :html => "<div class=\"warning\">Could not find viewer #{ERB::Util.html_escape(viewer_name || '(Unset)')}.</div>".html_safe, :status => "404"
374374
return
375375
end
376376

@@ -401,7 +401,7 @@ def display
401401
:description => "An internal error occurred when trying to display the contents of #{@userfile.name}."
402402
)
403403

404-
render :html => "<div class=\"warning\">Error generating view code for viewer '#{params[:viewer]}'. Admins have been notified and will look into the problem. In the meantime, there's not much you can do about this.</div>".html_safe
404+
render :html => "<div class=\"warning\">Error generating view code for viewer '#{ERB::Util.html_escape(params[:viewer] || '(Unset)')}'. Admins have been notified and will look into the problem. In the meantime, there's not much you can do about this.</div>".html_safe
405405
end
406406

407407
def show #:nodoc:

BrainPortal/app/models/boutiques_portal_task.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ def sanitize_param(input)
532532
# Some presets for convenience; at most one 'if' will trigger because regex != string always
533533
charset_regex = /\A[\w,\.\:\-]+\z/ if charset_regex == ':basename:' # "a0_,.:-"
534534
charset_regex = /\A[\w,\.\:\-\?\*]+\z/ if charset_regex == ':basename-pattern:' # "a0_,.:-*?"
535-
charset_regex = /\A[\w,\.\/\:\-]+(\/[\w,\.\/\:\-]*)*\z/ if charset_regex == ':relative-path:' # "base" or "/base/base/..."
535+
charset_regex = /\A[\w,\.\/\:\-]+(\/[\w,\.\:\-]+)*\/?\z/ if charset_regex == ':relative-path:' # "base" or "/base/base/..."
536536
charset_regex = /\A\S+\z/ if charset_regex == ':any-no-blanks:' # can be dangerous! YOU MUST VALIDATE TOOL'S ESCAPING PROPERLY!
537537
charset_regex = /\A[\w,\.\:\-\{\}]+\z/ if charset_regex == ':id-with-curlies:' # allows "abc" and "abc-{4}" etc
538538
charset_regex = /\A[\w,\.\:\-\+]+(\ +[\w,\.\:\-\+]+)*\z/ if charset_regex == ':ids-with-spaces:' # allows "abc" and "abc def xyz" etc

BrainPortal/app/models/help_document.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def full_path
5656

5757
# Pseudo-attribute representing the document's contents
5858
def contents
59-
@contents ||= (File.file?(self.full_path) ? IO.read(self.full_path) : nil)
59+
@contents ||= (File.file?(self.full_path) ? File.read(self.full_path) : nil)
6060
end
6161

6262
def contents=(contents) #:nodoc:
@@ -79,7 +79,7 @@ def self.from_existing_file!(key, path = nil)
7979

8080
# FIXME Inefficient; the file is re-written in the before_save callback.
8181
doc = self.new(:key => key, :path => path);
82-
doc.contents = IO.read(doc.full_path)
82+
doc.contents = File.read(doc.full_path)
8383
doc.save!
8484
doc
8585
end
@@ -99,7 +99,7 @@ def write_doc
9999

100100
if @contents
101101
FileUtils.mkpath(doc_dir) unless File.file?(doc_path) || File.directory?(doc_dir)
102-
IO.write(doc_path, @contents)
102+
File.write(doc_path, @contents)
103103
else
104104
File.unlink(doc_path) if File.file?(doc_path)
105105
end

BrainPortal/app/models/task_custom_filter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ def scope_types(scope)
175175
def scope_description(scope)
176176
query = 'cbrain_tasks.description'
177177
term = self.data_description_term
178+
term = "do-not-match-everything-#{rand(1000000)}" if term =~ /\A[\%\_\s]+\z/ # don't try matching all
178179
if self.data_description_type == 'match'
179180
query += ' = ?'
180181
else

BrainPortal/app/models/userfile.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class Userfile < ApplicationRecord
106106
attr_accessor :sync_select_patterns
107107

108108
# Utility named scopes
109-
scope :name_like, -> (n) { where("userfiles.name LIKE ?", "%#{n.strip}%") }
109+
scope :name_like, -> (n) { where("userfiles.name LIKE ? ESCAPE '!'", "%#{n.strip.gsub(/([%_!])/,'!\1')}%") }
110110

111111
scope :has_no_parent, -> { where(parent_id: nil) }
112112

BrainPortal/app/models/userfile_custom_filter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ def filter_scope(scope)
207207
def scope_name(scope)
208208
query = 'userfiles.name'
209209
term = self.data_file_name_term
210+
term = "do-not-match-everything-#{rand(1000000)}" if term =~ /\A[\%\_]+\z/ # don't try matching all
210211
if self.data_file_name_type == 'match'
211212
query += ' = ?'
212213
else

0 commit comments

Comments
 (0)