php-src icon indicating copy to clipboard operation
php-src copied to clipboard

http_response_code(400) in file_exists bug when using variable (in if block)

Open Bugistan opened this issue 4 years ago • 10 comments

<?php
include 'system/func/sessions.php';
include 'system/func/template.php';
include 'system/func/db.php';
include 'system/func/core.php';
include 'system/func/get-func.php';
include 'system/func/post-func.php';
include 'system/func/all-func.php';

function router(){
	$path = $_GET;
	$path = html_clear($path);

	$allPath = [
		'index'		=> 'index', 
		'prod'		=> 'prod-detail', 
		'cat'	    => 'cat', 
		'brand'		=> 'brand', 
		'contact'	=> 'contact', 
		'faq'		=> 'faq', 
		'blog'		=> 'blog-detail',
		'blogs'	    => 'blogs', 
		'check'		=> 'check', 
		'pay'		=> 'pay', 
		'success'	=> 'success', 
		'warn'		=> 'warn-file', 
		'account'	=> 'account-detail', 
		'fav'	    => 'fav',
		'404'		=> '404'
	];
	
	$parsePath = explode('/', $path['u']);


	if(!isset($parsePath[0])){
		$parsePath 	= '404';
	}else{
		if(isset($parsePath[1])){
			$_GET['q'] = permalink($parsePath[1]);
		}else{
			$_GET['q'] = '';
		}
		if(isset($allPath[$parsePath[0]])){
			$_GET['u'] = permalink($allPath[$parsePath[0]]);
		}else{
			$_GET['u'] = '404';
		}
	}
}

if(empty($_GET['u'])){
	$_GET['u'] = CURRENT_HOME;
}

if(!isset($_GET['u'])){
	$_GET['u'] = '404';
}

router();

/**
 * NOT WORKING -->
 */
if(!file_exists('view/templates/'.CURRENT_DIR.'/' . $_GET['u'] . '.php')){
    http_response_code(404);
	include 'view/templates/'.CURRENT_DIR.'/404.php';
}
/**
 * WORKING -->
 */
if(!file_exists('view/templates/default/file_name.php')){
    http_response_code(404);
	include 'view/templates/default/404.php';
}


include 'view/templates/'.CURRENT_DIR.'/' . $_GET['u'] . '.php';
?>


My Htaccess file:
Options +FollowSymlinks
Options -Indexes
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
    RewriteRule ^(.+)$ index.php?u=$1 [QSA,L]
</IfModule>
`


PHP Ver: 8.1.1
OS: Windows 10 Pro

Bugistan avatar Jan 31 '22 22:01 Bugistan

Could it be because$_GET['u'] is not defined?

kamil-tekiela avatar Jan 31 '22 22:01 kamil-tekiela

Could it be because$_GET['u'] is not defined?

I didn't feel the need to share. I'm correcting. My fault.

Bugistan avatar Jan 31 '22 22:01 Bugistan

@ReturnMe Could you var_dump($_GET['u']); just to be completely sure it is what you expect it to be? What does the URL look like? This seems like an unlikely bug.

iluuu1994 avatar Jan 31 '22 23:01 iluuu1994

@ReturnMe Could you var_dump($_GET['u']); just to be completely sure it is what you expect it to be? What does the URL look like? This seems like an unlikely bug.

I wrote my full codes, I'm adding htaccess below. I'll add the var_dump results shortly.

Bugistan avatar Jan 31 '22 23:01 Bugistan

Please also note your code is probably vulnerable to inclusion of unintended scripts by adding ../ to the u GET parameter.

iluuu1994 avatar Jan 31 '22 23:01 iluuu1994

Please also note your code is probably vulnerable to inclusion of unintended scripts by adding ../ to the u GET parameter.

Output of var_dump just before file_exist:

array(2) {
   ["u"]=>
   string(5) "asasfaasasa"
   ["q"]=>
   string(0) ""
}

I think I cleared well enough.

Including the 404 page (So, the correct if block is working.). But it doesn't make the HTTP response code 404. But, by removing the variables; If I write by hand. It's making the HTTP code 404.

I think I'll take a video.

Bugistan avatar Jan 31 '22 23:01 Bugistan

@ReturnMe Your previous edit said u was index, which would explain why file_exists(... $_GET['u'] . '.php') would return true. This would indicate that most likely something with your Apache configuration isn't ok and your $_GET params are getting lost. You can check by var_dump($_GET); at the top of your script and opening an URL with ?foo=bar.

iluuu1994 avatar Jan 31 '22 23:01 iluuu1994

@ReturnMe Your previous edit said u was index, which would explain why file_exists(... $_GET['u'] . '.php') would return true. This would indicate that most likely something with your Apache configuration isn't ok and your $_GET params are getting lost. You can check by var_dump($_GET); at the top of your script and opening an URL with ?foo=bar.

Ahh, absolutely! I already have my existing file, 404.php YES!

I'm so sorry, I took your time. You are perfect. I am very grateful, thank you very much.

I realized after posting the video :( I understood much better with your comment. Again, I promise I won't report bugs while I'm sleepless :(

Bugistan avatar Jan 31 '22 23:01 Bugistan

@ReturnMe Don't worry, it's fine. Happy to have helped.

iluuu1994 avatar Jan 31 '22 23:01 iluuu1994

@ReturnMe Don't worry, it's fine. Happy to have helped.

You are perfect.

Bugistan avatar Jan 31 '22 23:01 Bugistan