From 6df319864ae2c62a7722f91a480a1e2d539a2834 Mon Sep 17 00:00:00 2001 From: Justin Talbott Date: Mon, 27 Oct 2025 21:38:38 -0400 Subject: [PATCH] allow passing ssl_context through --- lib/network.rb | 11 +++++++++-- lib/statsig_options.rb | 7 ++++++- test/test_network.rb | 21 +++++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/network.rb b/lib/network.rb index 1694318..d3c22a3 100644 --- a/lib/network.rb +++ b/lib/network.rb @@ -27,6 +27,7 @@ def initialize(server_secret, options, backoff_mult = 10) @backoff_multiplier = backoff_mult @post_logs_retry_backoff = options.post_logs_retry_backoff @post_logs_retry_limit = options.post_logs_retry_limit + @ssl_context = options.ssl_context @session_id = SecureRandom.uuid @connection_pool = ConnectionPool.new(size: 3) do meta = Statsig.get_statsig_metadata @@ -111,11 +112,17 @@ def request(method, url, body, retries = 0, backoff = 1, zipped = false, event_c 'STATSIG-EVENT-COUNT' => event_count == 0 ? nil : event_count.to_s ) + options = {} + if @ssl_context + options[:ssl_context] = @ssl_context + end + case method when :GET - request.get(url) + request.get(url, **options) when :POST - request.post(url, body: body) + options[:body] = body + request.post(url, **options) end end rescue StandardError => e diff --git a/lib/statsig_options.rb b/lib/statsig_options.rb index 73a9570..4c15981 100644 --- a/lib/statsig_options.rb +++ b/lib/statsig_options.rb @@ -92,6 +92,9 @@ class StatsigOptions # default: false attr_accessor :disable_evaluation_memoization + # set a custom SSL context for the network requests + attr_accessor :ssl_context + def initialize( environment = nil, download_config_specs_url: nil, @@ -115,7 +118,8 @@ def initialize( post_logs_retry_limit: 3, post_logs_retry_backoff: nil, user_persistent_storage: nil, - disable_evaluation_memoization: false + disable_evaluation_memoization: false, + ssl_context: nil ) @environment = environment.is_a?(Hash) ? environment : nil @@ -146,5 +150,6 @@ def initialize( @post_logs_retry_backoff = post_logs_retry_backoff @user_persistent_storage = user_persistent_storage @disable_evaluation_memoization = disable_evaluation_memoization + @ssl_context = ssl_context end end diff --git a/test/test_network.rb b/test/test_network.rb index 92a1234..1a79a97 100644 --- a/test/test_network.rb +++ b/test/test_network.rb @@ -37,6 +37,27 @@ def teardown WebMock.disable! end + def test_ssl_context + ssl_context = OpenSSL::SSL::SSLContext.new.tap { |ctx| ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER } + options = StatsigOptions.new(ssl_context: ssl_context) + + @net = Statsig::Network.new('secret-abc', options, 1) + spy = Spy.on_instance_method(HTTP::Client, :post).and_return( + HTTP::Response.new( + { + status: 200, + body: 'hello', + version: "1.1", + headers: {}, + request: nil, + }, + ) + ) + + res, _ = @net.post('https://statsigapi.net/v1/log_event', '{}', 5, 1) + assert_equal(spy.calls.first.kwargs[:ssl_context], ssl_context) + end + def test_retries_succeed stub_request(:post, 'https://statsigapi.net/v1/log_event').to_return(status: lambda { |req| status_lambda(req) }, body: 'hello')