Skip to content

Commit a02405a

Browse files
chris1984claude
andcommitted
Sanitize hosts param and add edge case tests
Filter empty/malformed host values from the query string so that an empty or whitespace-only hosts param falls back to GET instead of sending an invalid POST with blank entries. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent f5566ad commit a02405a

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

app/services/foreman_rh_cloud/url_remediations_retriever.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ def initialize(url:, organization_id:, payload: '', headers: {}, logger: Logger.
1010
hosts_param = query_params.delete('hosts')
1111

1212
if hosts_param.present?
13-
@host_uuids = hosts_param.flat_map { |v| v.split(',') }
13+
@host_uuids = hosts_param.flat_map { |v| v.split(',') }.map(&:strip).reject(&:blank?)
14+
@host_uuids = nil if @host_uuids.empty?
1415
parsed_url.query = query_params.any? ? URI.encode_www_form(query_params) : nil
1516
@url = parsed_url.to_s
1617
else

test/unit/services/foreman_rh_cloud/url_remediations_retriever_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,40 @@ class URLRemediationsRetrieverTest < ActiveSupport::TestCase
6767

6868
retriever.create_playbook
6969
end
70+
71+
test 'Merges multiple hosts query params into a single array' do
72+
retriever = ForemanRhCloud::URLRemediationsRetriever.new(
73+
organization_id: FactoryBot.create(:organization).id,
74+
url: 'http://test.example.com/api/remediations/1234/playbook?hosts=uuid-1&hosts=uuid-2'
75+
)
76+
77+
retriever.stubs(:cert_auth_available?).returns(true)
78+
79+
response = mock('response')
80+
response.stubs(:body).returns('TEST_PLAYBOOK')
81+
retriever.expects(:execute_cloud_request).with do |params|
82+
params[:method] == :post &&
83+
params[:url] == 'http://test.example.com/api/remediations/1234/playbook' &&
84+
JSON.parse(params[:payload]) == ['uuid-1', 'uuid-2']
85+
end.returns(response)
86+
87+
retriever.create_playbook
88+
end
89+
90+
test 'Falls back to GET when hosts query param is empty' do
91+
retriever = ForemanRhCloud::URLRemediationsRetriever.new(
92+
organization_id: FactoryBot.create(:organization).id,
93+
url: 'http://test.example.com/api/remediations/1234/playbook?hosts='
94+
)
95+
96+
retriever.stubs(:cert_auth_available?).returns(true)
97+
98+
response = mock('response')
99+
response.stubs(:body).returns('TEST_PLAYBOOK')
100+
retriever.expects(:execute_cloud_request).with do |params|
101+
params[:method] == :get
102+
end.returns(response)
103+
104+
retriever.create_playbook
105+
end
70106
end

0 commit comments

Comments
 (0)