Skip to content

Commit ab3fec3

Browse files
committed
Disable Security/Eval for code snippet wrappers
`README.md` code snippets often reference or define local variables, requiring us to resort to `eval` to set/read them. All the code being `eval`'d is version controlled though, so this should be safe.
1 parent b526ea0 commit ab3fec3

File tree

9 files changed

+12
-10
lines changed

9 files changed

+12
-10
lines changed

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ inherit_gem:
44
plugins:
55
- rubocop-minitest
66
- rubocop-rake
7+
8+
Security/Eval:
9+
Exclude:
10+
- test/fixtures/files/code_snippet_wrappers/**/*.rb # We must often resort to eval to access local variable

test/fixtures/files/code_snippet_wrappers/readme/instrumentation_callback.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require "mcp"
44

55
MCP.configure do |config|
6-
eval(File.read("code_snippet.rb"), binding) # rubocop:disable Security/Eval -- We need to run the snippet in this context to pass the config
6+
eval(File.read("code_snippet.rb"), binding)
77

88
config.instrumentation_callback.call({ example: "data" })
99
end

test/fixtures/files/code_snippet_wrappers/readme/per_server_configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def notify(exception)
2626
end
2727

2828
b = binding
29-
eval(File.read("code_snippet.rb"), b) # rubocop:disable Security/Eval -- We need to run the snippet in this context
29+
eval(File.read("code_snippet.rb"), b)
3030
server = b.local_variable_get(:server)
3131

3232
server.define_tool(name: "error_tool") { raise "boom" }

test/fixtures/files/code_snippet_wrappers/readme/prompt_class_definition.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
require_relative "code_snippet"
66

77
b = binding
8-
eval(File.read("code_snippet.rb"), b) # rubocop:disable Security/Eval -- We need to run the snippet in this context to extract the prompt
8+
eval(File.read("code_snippet.rb"), b)
99
prompt = b.local_variable_get(:prompt)
1010

1111
server = MCP::Server.new(prompts: [prompt])

test/fixtures/files/code_snippet_wrappers/readme/prompts_usage.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
def current_user.id = 123
2929

3030
b = binding
31-
eval(File.read("code_snippet.rb"), b) # rubocop:disable Security/Eval -- We need to run the snippet in this context
31+
eval(File.read("code_snippet.rb"), b)
3232
server = b.local_variable_get(:server)
3333

3434
puts server.handle_json({

test/fixtures/files/code_snippet_wrappers/readme/resources.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require "mcp"
44

55
b = binding
6-
eval(File.read("code_snippet.rb"), b) # rubocop:disable Security/Eval -- We need to be able to extract the server local variable
6+
eval(File.read("code_snippet.rb"), b)
77
server = b.local_variable_get(:server)
88

99
puts server.handle_json({ jsonrpc: "2.0", id: "1", method: "resources/list" }.to_json)

test/fixtures/files/code_snippet_wrappers/readme/resources_read_handler.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
server = MCP::Server.new
66

77
b = binding
8-
eval(File.read("code_snippet.rb"), b) # rubocop:disable Security/Eval -- We need the snippet to have access to the server variable
8+
eval(File.read("code_snippet.rb"), b)
99

1010
puts server.handle_json({
1111
jsonrpc: "2.0",

test/fixtures/files/code_snippet_wrappers/readme/server_context.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ def current_user.id = 123
99
def request.uuid = "...uuid..."
1010

1111
b = binding
12-
13-
eval(File.read("code_snippet.rb"), b) # rubocop:disable Security/Eval -- We need to run the snippet in this context to extract the server
14-
12+
eval(File.read("code_snippet.rb"), b)
1513
server = b.local_variable_get(:server)
1614

1715
puts server.server_context.to_json

test/fixtures/files/code_snippet_wrappers/readme/tool_class_definition.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
require_relative "code_snippet"
66

77
b = binding
8-
eval(File.read("code_snippet.rb"), b) # rubocop:disable Security/Eval -- We need to run the snippet in this context to extract the tool
8+
eval(File.read("code_snippet.rb"), b)
99
tool = b.local_variable_get(:tool)
1010

1111
puts MCP::Server.new(tools: [tool]).handle_json(

0 commit comments

Comments
 (0)