From d2ada90a471495bf4131e24801ebebc012b53155 Mon Sep 17 00:00:00 2001 From: Iisyourdad Date: Tue, 7 Apr 2026 22:41:15 -0500 Subject: [PATCH] fix(git): harden ssh URL normalization --- bootstrap/helpers/shared.php | 8 +++--- tests/Feature/ConvertingGitUrlsTest.php | 36 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.php index 88ac01819..011c149c9 100644 --- a/bootstrap/helpers/shared.php +++ b/bootstrap/helpers/shared.php @@ -3660,14 +3660,16 @@ function convertGitUrl(string $gitRepository, string $deploymentType, GithubApp| } } - if (str($gitRepository)->contains('://')) { - $parsedRepository = parse_url($gitRepository); + $normalizedRepository = $repository; + + if (str($normalizedRepository)->contains('://')) { + $parsedRepository = parse_url($normalizedRepository); if ($parsedRepository !== false && array_key_exists('port', $parsedRepository)) { $providerInfo['port'] = (string) $parsedRepository['port']; } } else { - preg_match('/^(?[^:]+):(?\d+)\/(?.+)$/', $gitRepository, $matches); + preg_match('/^(?[^:]+):(?\d+)\/(?.+)$/', $normalizedRepository, $matches); if (! empty($matches['port'])) { $providerInfo['port'] = $matches['port']; diff --git a/tests/Feature/ConvertingGitUrlsTest.php b/tests/Feature/ConvertingGitUrlsTest.php index 5ff1c8365..96b19fcc9 100644 --- a/tests/Feature/ConvertingGitUrlsTest.php +++ b/tests/Feature/ConvertingGitUrlsTest.php @@ -68,3 +68,39 @@ 'port' => '22222', ]); }); + +test('convertGitUrlsForSourceAndSshUrlSchemeWithCustomPortAndIpv6Host', function () { + $result = convertGitUrl('ssh://git@[2001:db8::10]:22222/group/project.git', 'source', null); + expect($result)->toBe([ + 'repository' => 'ssh://git@[2001:db8::10]:22222/group/project.git', + 'port' => '22222', + ]); +}); + +test('convertGitUrlsForDeployKeyAndGithubAppWithCustomPort', function () { + $githubApp = new GithubApp([ + 'html_url' => 'https://github.example.com', + 'custom_user' => 'git', + 'custom_port' => 22222, + ]); + + $result = convertGitUrl('andrasbacsai/coolify-examples.git', 'deploy_key', $githubApp); + expect($result)->toBe([ + 'repository' => 'ssh://git@github.example.com:22222/andrasbacsai/coolify-examples.git', + 'port' => '22222', + ]); +}); + +test('convertGitUrlsForDeployKeyAndGithubAppWithCustomPortAndIpv6Host', function () { + $githubApp = new GithubApp([ + 'html_url' => 'https://[2001:db8::10]', + 'custom_user' => 'git', + 'custom_port' => 22222, + ]); + + $result = convertGitUrl('andrasbacsai/coolify-examples.git', 'deploy_key', $githubApp); + expect($result)->toBe([ + 'repository' => 'ssh://git@[2001:db8::10]:22222/andrasbacsai/coolify-examples.git', + 'port' => '22222', + ]); +});