Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue working with non-standard ports #16455

Merged
merged 3 commits into from Mar 22, 2024

Conversation

dimasites
Copy link
Contributor

@dimasites dimasites commented Jul 1, 2023

What does it do?

I changed logic of modx working with non-standard ports because old logic in some cases works not correct: port :44333 MODX cut ":443" and "33" part breaks the links adresses. See #16428 for details.

Relying on here this and this I suggest using the answers in stackoverflow to check the port not $_SERVER['SERVER_PORT'] but parse_url($_SERVER['HTTP_HOST'], PHP_URL_PORT) because it works more reliably and correctly, as can be seen from my tests.

Why is it needed?

Currently, when using a non-standard port for https (any one other than 443) MODX modifies request URL with mistakes, and redirect users's browser to wrong url.

See my explaination on awesome handmade infographic :) image:
explaination for pull request v2

How to test

I have prepared a test script:

See code: test_modx_working_with_http_ports.php
var_dump(parse_url($_SERVER['HTTP_HOST'], PHP_URL_PORT)); echo ' ← parsed port from HTTP_HOST <br>';
var_dump($_SERVER['SERVER_PORT']); echo ' ← SERVER_PORT <br>';

$http_port = parse_url($_SERVER['HTTP_HOST'], PHP_URL_PORT) ?: $_SERVER['SERVER_PORT'];
var_dump($_SERVER['SERVER_PORT']); echo ' ← http_port for usage in fixed config <br>';

$http_host = array_key_exists('HTTP_HOST', $_SERVER) ? htmlspecialchars($_SERVER['HTTP_HOST'], ENT_QUOTES) : 'test-port.ltd';

//this is for test logic with non-standart port without server changes, COMMENT IR FOR REAL SERVER TEST
$http_host = 'test-port.ltd:44333';

echo '<br>';
var_dump($http_host); echo ' ← http_host (old logic)<br>';

$http_host_new_logic = parse_url($http_host, PHP_URL_HOST); 
var_dump($http_host_new_logic); echo ' ← http_host (new logic)<br>';


/*old logic */
if ($_SERVER['SERVER_PORT'] !== 80) {
	
	$orig_http_host = str_replace(':' . $_SERVER['SERVER_PORT'], '', $http_host);
	$fixed_http_host = str_replace(':' . $http_port, '', $http_host);
	$fixed_http_host_new_logic = str_replace(':' . $http_port, '', $http_host_new_logic);
	
	$http_host = str_replace(':' . $_SERVER['SERVER_PORT'], '', $http_host);
}
echo '<br>';
var_dump($orig_http_host); echo ' ← orig_http_host after check port and clean host (old logic)<br>';
var_dump($fixed_http_host); echo ' ← fixed_http_host after check port and clean host (old logic)<br>';
var_dump($fixed_http_host_new_logic); echo ' ← fixed_http_host after check port and clean host (new logic)<br>';

$http_host .= in_array($_SERVER['SERVER_PORT'], [80, 443]) ? '' : ':' . $_SERVER['SERVER_PORT'];

$orig_http_host = in_array($_SERVER['SERVER_PORT'], [80, 443]) ? $http_host.'' : $http_host.':' . $_SERVER['SERVER_PORT'];

$fixed_http_host = !in_array($http_port, [80, 443]) ? $fixed_http_host.'' : $fixed_http_host.':' . $http_port;

$fixed_http_host_new_logic = !in_array($http_port, [80, 443]) ? $fixed_http_host_new_logic.'' : $fixed_http_host_new_logic.':' . $http_port;
echo '<br>';
var_dump($orig_http_host); echo ' ← orig_http_host after replace (old logic)<br>';
var_dump($fixed_http_host); echo ' ← fixed_http_host after replace (old logic)<br>';
var_dump($fixed_http_host_new_logic); echo ' ← fixed_http_host after replace (new logic)<br>';


////////// SPEED TESTS //////////
echo '<br>';

$time_start = microtime(true);
for($i=0;$i<=1000000;$i++){
    // Method 1 Original modx 2.8.5
		$http_host = array_key_exists('HTTP_HOST', $_SERVER) ? htmlspecialchars($_SERVER['HTTP_HOST'], ENT_QUOTES) : 'test-port.ltd:44333';
		
        if ($_SERVER['SERVER_PORT'] !== 80) {
            $http_host = str_replace(':' . $_SERVER['SERVER_PORT'], '', $http_host);
        }
        $http_host .= in_array($_SERVER['SERVER_PORT'], [80, 443]) ? '' : ':' . $_SERVER['SERVER_PORT'];
        define('MODX_HTTP_HOST', $http_host);
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Original modx 2.8.5 config test: ($time seconds)\n<br />";



$time_start = microtime(true);
for($i=0;$i<=1000000;$i++){
    // Method2 Fixed working with ports by dimasites
		$http_host = array_key_exists('HTTP_HOST', $_SERVER) ? parse_url($_SERVER['HTTP_HOST'], PHP_URL_HOST) : 'test-port.ltd:44333';
		
		$http_port = parse_url($_SERVER['HTTP_HOST'], PHP_URL_PORT)?:$_SERVER['SERVER_PORT'];
		
        $http_host .= in_array($http_port, [80, 443]) ? '' : ':' . $http_port;
        
		define('MODX_HTTP_HOST', $http_host);

}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Fixed working with ports config: ($time seconds)\n";

/*
My results on PHP 7.4 about:
Hosting server 1 (client:mlk):
Original modx 2.8.5 config test: (3.4942800998688 seconds)
Fixed working with ports config: (3.3323838710785 seconds)

Hosting server 2 (main):
Original modx 2.8.5 config test: (1.6090040206909 seconds)
Fixed working with ports config: (1.4196999073029 seconds)


For URL: https://test-port.ltd/test_modx_working_with_http_ports.php
NULL ← parsed port from HTTP_HOST
string(3) "443" ← SERVER_PORT
string(3) "443" ← http_port for usage in fixed config

string(19) "test-port.ltd:44333" ← http_host (old logic)
string(13) "test-port.ltd" ← http_host (new logic)

string(15) "test-port.ltd33" ← orig_http_host after check port and clean host (old logic)
string(15) "test-port.ltd33" ← fixed_http_host after check port and clean host (old logic)
string(13) "test-port.ltd" ← fixed_http_host after check port and clean host (new logic)

string(15) "test-port.ltd33" ← orig_http_host after replace (old logic)
string(19) "test-port.ltd33:443" ← fixed_http_host after replace (old logic)
string(17) "test-port.ltd:443" ← fixed_http_host after replace (new logic)

Original modx 2.8.5 config test: (1.7936890125275 seconds)
Fixed working with ports config: (1.5702078342438 seconds)


For URL: https://test-port.ltd:44333/test_modx_working_with_http_ports.php
int(44333) ← parsed port from HTTP_HOST
string(3) "443" ← SERVER_PORT
string(3) "443" ← http_port for usage in fixed config

string(19) "test-port.ltd:44333" ← http_host (old logic)
string(13) "test-port.ltd" ← http_host (new logic)

string(15) "test-port.ltd33" ← orig_http_host after check port and clean host (old logic)
string(13) "test-port.ltd" ← fixed_http_host after check port and clean host (old logic)
string(13) "test-port.ltd" ← fixed_http_host after check port and clean host (new logic)

string(15) "test-port.ltd33" ← orig_http_host after replace (old logic)
string(13) "test-port.ltd" ← fixed_http_host after replace (old logic)
string(13) "test-port.ltd" ← fixed_http_host after replace (new logic)

Original modx 2.8.5 config test: (3.5288610458374 seconds)
Fixed working with ports config: (3.4201400279999 seconds)

*/

with a test and comparison of values, as well as speed tests in relation to the replacement of string processing functions (and new logic even a little faster)

Related issue(s)/PR(s)

Resolves #16428 and this hand crutch in MODX Docker project

@dimasites dimasites marked this pull request as ready for review February 23, 2024 21:02
@dimasites dimasites changed the title 2.x fix working with non-standart ports [2.x] fix working with non-standart ports Feb 23, 2024
core/docs/config.inc.tpl Outdated Show resolved Hide resolved
@dimasites dimasites force-pushed the 2.x-fix-working-with-ports branch 3 times, most recently from d24ca1a to c1b07af Compare February 23, 2024 22:53
dimasites referenced this pull request in Pixmill-Gmbh/modx-docker Feb 28, 2024
@opengeek opengeek changed the title [2.x] fix working with non-standart ports Fix issue working with non-standard ports Mar 14, 2024
@opengeek
Copy link
Member

@dimasites — can you review and sign the CLA at https://modx.com/community/cla-introduction/ so we can consider getting this contribution into the next release?

@dimasites
Copy link
Contributor Author

Hi @opengeek i signed it via gitgub's cla-bot when pushed commit earlier, and now additionally signed CLA on modx.com:
image

And if you finished review with no suggestions, i can check MODX 3 behavior with non-standart ports. Now waiting you response, thx

@opengeek
Copy link
Member

Hi @opengeek i signed it via gitgub's cla-bot when pushed commit earlier, and now additionally signed CLA on modx.com:

The cla-bot stuff isn't working any longer for us, and it should have just directed you to sign on modx.com anyway. Very odd. But I have confirmed now via our internal systems.

And if you finished review with no suggestions, i can check MODX 3 behavior with non-standart ports. Now waiting you response, thx

See my review comment…

@dimasites dimasites force-pushed the 2.x-fix-working-with-ports branch 4 times, most recently from afe6b68 to c2d2460 Compare March 18, 2024 02:31
@dimasites dimasites requested a review from opengeek March 18, 2024 02:52
@opengeek opengeek added this to the v2.8.7 milestone Mar 18, 2024
@opengeek opengeek merged commit 1371d06 into modxcms:2.x Mar 22, 2024
5 checks passed
@opengeek opengeek added the bug The issue in the code or project, which should be addressed. label Mar 22, 2024
@dimasites dimasites deleted the 2.x-fix-working-with-ports branch March 22, 2024 17:08
opengeek pushed a commit that referenced this pull request Mar 25, 2024
Fixed In default config, setup process and setup wizard.

This is port from 2.x branch, all context here:
#16455
opengeek pushed a commit that referenced this pull request Mar 25, 2024
Fixed In default config, setup process and setup wizard.

This is port from 2.x branch, all context here:
#16455
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-setup bug The issue in the code or project, which should be addressed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants