From 3d044c4241203fca197e88935f3426edf8c4891e Mon Sep 17 00:00:00 2001 From: cgranleese-r7 Date: Fri, 3 May 2024 15:45:41 +0100 Subject: [PATCH] Fixes an issue were a regex mactch could have returned nil --- lib/postgres/postgres-pr/connection.rb | 20 ++++++++++++-------- spec/lib/rex/proto/postgresql/client_spec.rb | 9 ++++++++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/postgres/postgres-pr/connection.rb b/lib/postgres/postgres-pr/connection.rb index 8047c438c291..53962fe49c05 100644 --- a/lib/postgres/postgres-pr/connection.rb +++ b/lib/postgres/postgres-pr/connection.rb @@ -213,14 +213,18 @@ def map_compile_arch_to_architecture(compile_arch) def detect_platform_and_arch result = {} - query_result = query('select version()').rows.join.match(/on (?\w+)-\w+-(?\w+)/) - server_vars = { - 'version_compile_machine' => query_result[:architecture], - 'version_compile_os' => query_result[:platform] - } - - result[:arch] = map_compile_arch_to_architecture(server_vars['version_compile_machine']) - result[:platform] = map_compile_os_to_platform(server_vars['version_compile_os']) + query_result = query('select version()').rows[0][0] + match_platform_and_arch = query_result.match(/on (?\w+)-\w+-(?\w+)/) + + if match_platform_and_arch.nil? + arch = platform = query_result + else + arch = match_platform_and_arch[:architecture] + platform = match_platform_and_arch[:platform] + end + + result[:arch] = map_compile_arch_to_architecture(arch) + result[:platform] = map_compile_os_to_platform(platform) result end diff --git a/spec/lib/rex/proto/postgresql/client_spec.rb b/spec/lib/rex/proto/postgresql/client_spec.rb index e7f4a2faeef4..aa3a923a378c 100644 --- a/spec/lib/rex/proto/postgresql/client_spec.rb +++ b/spec/lib/rex/proto/postgresql/client_spec.rb @@ -68,7 +68,14 @@ [ { version: 'PostgreSQL 9.4.26 on x86_64-pc-linux-gnu (Debian 9.4.26-1.pgdg90+1), compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit', expected: { arch: 'x86_64', platform: 'Linux' } }, { version: 'PostgreSQL 14.11 (Debian 14.11-1.pgdg120+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit', expected: { arch: 'x86_64', platform: 'Linux' } }, - { version: 'PostgreSQL 14.11 (Homebrew) on x86_64-apple-darwin22.6.0, compiled by Apple clang version 15.0.0 (clang-1500.1.0.2.5), 64-bit', expected: { arch: 'x86_64', platform: 'OSX' } } + { version: 'PostgreSQL 14.11 (Homebrew) on x86_64-apple-darwin22.6.0, compiled by Apple clang version 15.0.0 (clang-1500.1.0.2.5), 64-bit', expected: { arch: 'x86_64', platform: 'OSX' } }, + { + version: 'PostgreSQL 14.11 (Homebrew) -, compiled by clang version 15.0.0 (clang-1500.1.0.2.5), ', + expected: { + arch: 'postgresql 14.11 (homebrew) -, compiled by clang version 15.0.0 (clang-1500.1.0.2.5), ', + platform: 'postgresql 14.11 (homebrew) -, compiled by clang version 15.0.0 (clang-1500.1.0.2.5), ' + } + } ].each do |test| context "when the database is version #{test[:version]}" do it "returns #{test[:expected]}" do