Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions sentry-ruby/lib/sentry/cron/monitor_check_ins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
module Cron
module MonitorCheckIns
MAX_SLUG_LENGTH = 50
MAX_NAME_LENGTH = 128
SLUG_HASH_LENGTH = 10

module Patch
def perform(*args, **opts)
Expand Down Expand Up @@ -59,8 +61,19 @@

def sentry_monitor_slug(name: self.name)
@sentry_monitor_slug ||= begin
slug = name.gsub("::", "-").downcase
slug[-MAX_SLUG_LENGTH..-1] || slug
slug = name.gsub("::", "-").gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on a
library input
may run slow on strings with many repetitions of 'A'.
if slug.length > MAX_SLUG_LENGTH
diff_length = slug.length + 1 + SLUG_HASH_LENGTH - MAX_SLUG_LENGTH
trim_part = ""
slug.scan(/([^_-]+)([_-])/) do |match, separator|
trim_part = "#{trim_part}#{match}#{separator}"
break if trim_part.length >= diff_length
end
trim_part = slug[0...diff_length] if trim_part.empty?
hash = OpenSSL::Digest::SHA256.hexdigest(trim_part)[0..SLUG_HASH_LENGTH-1]
slug = "#{hash}_#{slug.sub(trim_part, '')}"
end
slug
end
end

Expand Down
2 changes: 1 addition & 1 deletion sentry-ruby/spec/sentry/cron/monitor_check_ins_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def perform

it 'truncates from the beginning and parameterizes slug' do
slug = VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::Job.sentry_monitor_slug
expect(slug).to eq('ongoutermodule-veryveryveryverylonginnermodule-job')
expect(slug).to eq('675905e0c9_very_very_very_long_inner_module-job')
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@

it 'truncates from the beginning and parameterizes slug' do
expect(VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::Job.ancestors).to include(Sentry::Cron::MonitorCheckIns)
expect(VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::Job.sentry_monitor_slug).to eq('ongoutermodule-veryveryveryverylonginnermodule-job')
expect(VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::Job.sentry_monitor_slug.length).to be <= 50
expect(VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::Job.sentry_monitor_slug).to eq('675905e0c9_very_very_very_long_inner_module-job')
expect(VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::Job.sentry_monitor_config).to be_a(Sentry::Cron::MonitorConfig)
expect(VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::Job.sentry_monitor_config.schedule).to be_a(Sentry::Cron::MonitorSchedule::Crontab)
expect(VeryLongOuterModule::VeryVeryVeryVeryLongInnerModule::Job.sentry_monitor_config.schedule.value).to eq('* * * * *')
Expand Down
4 changes: 2 additions & 2 deletions sentry-sidekiq/spec/sentry/sidekiq_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def retry_last_failed_job
expect(first.to_hash).to include(
type: 'check_in',
check_in_id: check_in_id,
monitor_slug: "happyworkerwithcron",
monitor_slug: "happy_worker_with_cron",
status: :in_progress
)

Expand All @@ -330,7 +330,7 @@ def retry_last_failed_job
:duration,
type: 'check_in',
check_in_id: check_in_id,
monitor_slug: "happyworkerwithcron",
monitor_slug: "happy_worker_with_cron",
status: :ok
)
end
Expand Down
Loading