Skip to content
Snippets Groups Projects
Commit 1b3bf936 authored by GitLab Release Tools Bot's avatar GitLab Release Tools Bot
Browse files

Merge branch 'security-273771-confidential-issue-14-9' into '14-9-stable-ee'

Modify release link format check to avoid regex if string is too long

See merge request gitlab-org/security/gitlab!2307
parents 46e0ec3f f516d883
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -9,10 +9,20 @@ module Releases
# See https://gitlab.com/gitlab-org/gitlab/-/issues/218753
# Regex modified to prevent catastrophic backtracking
FILEPATH_REGEX = %r{\A\/[^\/](?!.*\/\/.*)[\-\.\w\/]+[\da-zA-Z]+\z}.freeze
FILEPATH_MAX_LENGTH = 128
 
validates :url, presence: true, addressable_url: { schemes: %w(http https ftp) }, uniqueness: { scope: :release }
validates :name, presence: true, uniqueness: { scope: :release }
validates :filepath, uniqueness: { scope: :release }, format: { with: FILEPATH_REGEX }, allow_blank: true, length: { maximum: 128 }
validates :filepath, uniqueness: { scope: :release }, allow_blank: true
validate :filepath_format_valid?
# we use a custom validator here to prevent running the regex if the string is too long
# see https://gitlab.com/gitlab-org/gitlab/-/issues/273771
def filepath_format_valid?
return if filepath.nil? # valid use case
return errors.add(:filepath, "is too long (maximum is #{FILEPATH_MAX_LENGTH} characters)") if filepath.length > FILEPATH_MAX_LENGTH
return errors.add(:filepath, 'is in an invalid format') unless FILEPATH_REGEX.match? filepath
end
 
scope :sorted, -> { order(created_at: :desc) }
 
Loading
Loading
Loading
Loading
@@ -113,6 +113,17 @@ RSpec.describe Releases::Link do
end
end
 
describe 'when filepath is greater than max length' do
let!(:invalid_link) { build(:release_link, filepath: 'x' * (Releases::Link::FILEPATH_MAX_LENGTH + 1), release: release) }
it 'will not execute regex' do
invalid_link.filepath_format_valid?
expect(invalid_link.errors[:filepath].size).to eq(1)
expect(invalid_link.errors[:filepath].first).to start_with("is too long")
end
end
describe 'FILEPATH_REGEX with table' do
using RSpec::Parameterized::TableSyntax
 
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment