Skip to content

Commit 0e4003d

Browse files
committed
Fixes #38862 - Handle nil host UUIDs properly
1 parent 3ee169d commit 0e4003d

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

lib/smart_proxy_container_gateway/container_gateway_api.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ class Api < ::Sinatra::Base
262262
database.connection.transaction(isolation: :serializable, retry_on: [Sequel::SerializationFailure]) do
263263
hosts_table = database.connection[:hosts]
264264
hosts_table.delete
265-
hosts_table.import(%i[uuid], hosts.map { |host| [host['uuid']] })
265+
valid_hosts = hosts.filter_map { |host| [host['uuid']] if host['uuid'].present? }
266+
hosts_table.import(%i[uuid], valid_hosts) unless valid_hosts.empty?
266267
end
267268
{}
268269
end
@@ -277,6 +278,8 @@ class Api < ::Sinatra::Base
277278
do_authorize_any
278279
params['hosts'].flat_map do |host_map|
279280
host_map.filter_map do |host_uuid, repos|
281+
next if host_uuid.nil? || host_uuid.to_s.strip.empty?
282+
280283
if repos.nil? || repos.empty?
281284
repo_names = []
282285
else

lib/smart_proxy_container_gateway/container_gateway_main.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ def build_host_repository_mapping(host_repo_maps)
214214
end
215215

216216
def build_host_entries(hosts, repositories, host_uuid, repos)
217+
return if host_uuid.nil? || host_uuid.to_s.strip.empty?
218+
217219
host = hosts[{ uuid: host_uuid }]
218220
return unless host
219221
return if repos.nil? || repos.empty?
@@ -232,7 +234,11 @@ def extract_auth_required_repo_names(repos)
232234
end
233235

234236
def update_host_repositories(uuid, repositories)
237+
return if uuid.nil? || uuid.to_s.strip.empty?
238+
235239
host = find_or_create_host(uuid)
240+
return unless host
241+
236242
hosts_repositories = database.connection[:hosts_repositories]
237243
database.connection.transaction(isolation: :serializable,
238244
retry_on: [Sequel::SerializationFailure],

test/container_gateway_api_test.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,4 +427,68 @@ def test_flatpak_static_index_server_error
427427
assert_equal 500, last_response.status
428428
assert_equal '{"error": "internal server error"}', last_response.body
429429
end
430+
431+
def test_update_hosts_filters_nil_uuids
432+
hosts = [
433+
{ 'uuid' => 'host-uuid-1' },
434+
{ 'uuid' => nil },
435+
{ 'uuid' => 'host-uuid-2' }
436+
]
437+
438+
put '/update_hosts', { 'hosts' => hosts }
439+
assert last_response.ok?
440+
441+
# Verify only valid hosts were inserted (old behavior would have failed or inserted nil)
442+
assert @database.connection[:hosts].count >= 2
443+
assert_not_nil @database.connection[:hosts].first(uuid: 'host-uuid-1')
444+
assert_not_nil @database.connection[:hosts].first(uuid: 'host-uuid-2')
445+
end
446+
447+
def test_update_hosts_filters_empty_string_uuids
448+
hosts = [
449+
{ 'uuid' => 'host-uuid-1' },
450+
{ 'uuid' => '' },
451+
{ 'uuid' => 'host-uuid-2' }
452+
]
453+
454+
put '/update_hosts', { 'hosts' => hosts }
455+
assert last_response.ok?
456+
457+
# Verify only valid hosts were inserted
458+
assert @database.connection[:hosts].count >= 2
459+
assert_not_nil @database.connection[:hosts].first(uuid: 'host-uuid-1')
460+
assert_not_nil @database.connection[:hosts].first(uuid: 'host-uuid-2')
461+
end
462+
463+
def test_update_hosts_handles_missing_uuid_key
464+
hosts = [
465+
{ 'uuid' => 'host-uuid-1' },
466+
{ 'name' => 'host-without-uuid' },
467+
{ 'uuid' => 'host-uuid-2' }
468+
]
469+
470+
put '/update_hosts', { 'hosts' => hosts }
471+
assert last_response.ok?
472+
473+
# Verify only valid hosts were inserted
474+
assert @database.connection[:hosts].count >= 2
475+
assert_not_nil @database.connection[:hosts].first(uuid: 'host-uuid-1')
476+
assert_not_nil @database.connection[:hosts].first(uuid: 'host-uuid-2')
477+
end
478+
479+
def test_update_hosts_with_empty_array
480+
put '/update_hosts', { 'hosts' => [] }
481+
assert last_response.ok?
482+
end
483+
484+
def test_update_hosts_with_all_invalid_uuids
485+
hosts = [
486+
{ 'uuid' => nil },
487+
{ 'uuid' => '' },
488+
{ 'name' => 'no-uuid' }
489+
]
490+
491+
put '/update_hosts', { 'hosts' => hosts }
492+
assert last_response.ok?
493+
end
430494
end

0 commit comments

Comments
 (0)