Skip to content
Merged
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
4 changes: 2 additions & 2 deletions lib/mcp/string_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def demodulize(path)

def underscore(camel_cased_word)
camel_cased_word
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
.gsub(/(?<=[A-Z])(?=[A-Z][a-z])/, "_")
.gsub(/(?<=[a-z\d])(?=[A-Z])/, "_")
.tr("-", "_")
.downcase
end
Expand Down
14 changes: 14 additions & 0 deletions test/mcp/string_utils_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "test_helper"
require "timeout"

module MCP
class StringUtilsTest < Minitest::Test
Expand All @@ -18,5 +19,18 @@ def test_handle_from_class_name_returns_the_class_name_without_the_module_for_a_
assert_equal("test", StringUtils.handle_from_class_name("Module::Submodule::Test"))
assert_equal("test_class", StringUtils.handle_from_class_name("Module::Submodule::TestClass"))
end

def test_handle_from_class_name_does_not_cause_redos
# A long string of uppercase letters followed by a non-lowercase character
# would trigger catastrophic backtracking with the vulnerable regex patterns.
malicious_input = "A" * 50_000 + "!"

result = nil
Timeout.timeout(1) do
result = StringUtils.handle_from_class_name(malicious_input)
end

assert_equal("a" * 50_000 + "!", result)
end
end
end