PHP 8.5.0 Beta 3 available for testing

ユーザーが投稿したデータ

多くの PHP のプログラムで最も脆弱な部分は、言語自体に起因するものではなく、 単にセキュリティを考慮して書かれていないコードの問題です。そのため、 指定したコードの部分の意味を常に時間をかけて吟味し、 予想外の変数が投稿された場合に有り得る損害を確かめる必要があります。

例1 危険な変数の使用

<?php
// ユーザーのホームディレクトリからファイルを削除します... または他の誰
// かのディレクトリかも?
unlink ($evil_var);

// 彼らのアクセスのログを書き込む.. または違うかも?
fputs ($fp, $evil_var);

// 何かちょっとしたことを実行.. または rm -rf *?
system ($evil_var);
exec ($evil_var);

?>
常に注意してコードをテストし、Webブラウザから投稿された全ての変数 について次のような点を確認してください。
  • このスクリプトは、意図したファイルのみを受け付けるか?
  • 例外的なまたは意図したもの以外のデータにより実行することが可能 か?
  • このスクリプトは意図した以外の方法で使用することが可能か?
  • このスクリプトは、悪い意味で他のスクリプトと組み合わせて使用す ることが可能か?
  • トランザクションは適切に記録されているか?

スクリプトを書いた後ではなく、書いている時にこれらの質問を適宜行う ことにより、セキュリティ改善のために不幸にして書き直しが必要になる ということを避けることができます。こうした考慮をまず行うことにより、 システムのセキュリティを保証できるわけではありませんが、改善の一助 にはなりえます。

入力データの出所や有効性、完全性を曖昧にする便利な設定を無効にし、 セキュリティを強化してください。暗黙的な変数の生成や未検証の入力は、 インジェクション攻撃やデータ操作といった脆弱性につながる可能性があります。

かつては register_globalsmagic_quotes (これらは両方、PHP 5.4.0 で削除されています) のような機能が、ユーザーの入力から自動的に変数を生成したり、 データを不完全な形でエスケープしたりすることで、セキュリティリスクを拡大することに貢献していました。 PHP ではそうしたリスクはなくなったものの、 入力処理の管理を誤れば、同様のリスクは依然として存在します。

error_reporting(E_ALL) を有効にし、未初期化の変数を検知し、入力を検証してください。 型安全を強制し、意図しない型変換を防止し、セキュリティ全体を改善するために、 strict モード (PHP 7 で導入された declare(strict_types=1)) を利用してください。

add a note

User Contributed Notes 2 notes

up
63
Uli Kusterer
20 years ago
One thing I would repeat in the docs here is what information actually comes from the user. Many people think a Cookie, since it's written by PHP, was safe. But the fact is that it's stored on the user's computer, transferred by the user's browser, and thus very easy to manipulate.

So, it'd be handy to mention here again that:

CGI parameters in the URL, HTTP POST data and cookie variables are considered "user data" and thus need to be validated. Session data and SQL database contents only need to be validated if they came from untrustworthy sources (like the ones just mentioned).

Not new, but I would have expected this info under this headline, at least as a short recap plus linlk to the actual docs.
up
7
Livingstone@stonyhills[dot]com
17 years ago
making sure your form is submitted from your page! Could also be adapted to url, by additing &token to the query string and checking this against session data(or what ever array you like) with $_GET, not that this string is randomly generated and stored. If you like you could build your own array to store the generated string if you dont want to use $_SESSION, say you could make yours like $tokens = array(), and in your easysecure class you store all the stuff in that array!

<?php

class easysecure {

var
$curr_user;
var
$curr_permission;
var
$curr_task;
var
$validpermission;
var
$error;


function &
setVar( $name, $value=null ) {
if (!
is_null( $value )) {
$this->$name = $value;
}
return
$this->$name;
}

function
maketoken($formname, $id){

$token = md5(uniqid(rand(), true));

$_SESSION[$formname.$id] = $token;

return
$token;
}

function
checktoken($token, $formname, $id){
//print_r($_SESSION);
//echo ($token);
//if we dont have a valid token, return invalid;
if(!$token){
$this->setVar('validpermission', 0);
$this->setVar('error', 'no token found, security bridgedetected');
return
false;
}

//if we have a valid token check that is is valid
$key = $_SESSION[$formname.$id];
if(
$key !== $token ){
$this->setVar('validpermission', 0);
$this->setVar('error', 'invalid token');
return
false;
}

if(
$this->validpermission !==1){
echo
'invalid Permissions to run this script';
return
false;
}else{
return
true;
}
}

}

?>

<?php $userid = *** //make it what ever id you like ?>
<form name="newform" action="index.php" method="post">
<input type="text" name="potentialeveilfield" value="" size 30 />
<input type="hidden" name="token" value="<?php echo maketoken(newform, $userid); //$userid here could be user profile id ?>" />
<input type="submit" />
</form>

Now when processing the form... check the value of your token

<?php

//well you know the form name
if(!checktoken($_POST['token'], 'newform', $userid))
{
//failed
exit(); //or what ever termination and notification method best suits you.
//you could also design the class your way to get more accurate fail (error messages from the var)
}

//you can now continue with input data clean up (validation)

?>
To Top