Include safety

2019-07-31 08:25发布

<?php
if (preg_match('/^[a-z0-9]+$/', $_GET['page'])) {
$page = realpath('includes/'.$_GET['page'].'.php');
$tpl = realpath('templates/'.$_GET['page'].'.html');
if ($page && $tpl) {
    include $page;
    include $tpl;
} else {
    // log error!
}
} else {
// log error!
}
?>

How safe would you say this is? Gumbo here on Stack Overflow wrote it.
Dynamic Include Safety

I wanna hear your opinions.

cheers

标签: php include
4条回答
我命由我不由天
2楼-- · 2019-07-31 08:51

My first thought isn't about safety, but about why in the world would you do that?

查看更多
看我几分像从前
3楼-- · 2019-07-31 08:55

I could see some potential issues there, especially if the 'page' variable contained '..' or other such things that could allow them to see something they weren't supposed to be able to see.

I do something similar on a few sites of mine, but I would first check 'page' to make sure it references one of a set of allowed pages.

查看更多
走好不送
4楼-- · 2019-07-31 08:58

you including your own code. how safe is it?

查看更多
5楼-- · 2019-07-31 09:03

I'd say it's pretty safe. Just don't allow anything to write to those folders. PHP files are traditionally inside the web root of a server which is dangerous to start with. It would be better to place the files being loaded to an area that's absolutely inaccessible to the outside given a configuration error or a .htaccess file going missing.

查看更多
登录 后发表回答