Taint support for PHP 5.2.5
taintモードを組み込んだPHP5.2.5が出ていたので、サラッとだけ試してみました。
環境はいつものCentOS4.6 on VMware Playerです。ソースはhttp://wiki.php.net/rfc/taint/からダウンロードし、通常通りbuild。configureオプションはとりあえずこんな感じ。「--enable-taint」が追加されてました。
Taint support for PHP 5.2.5 has been updated. The 20080423 version
improves support for PCRE, and fixes a harmless read-after-free bug.
./configure \ --with-apxs2=/usr/local/apache2/bin/apxs \ --prefix=/usr/local/lib/php5 \ --with-pear=/usr/local/lib/php5/pear \ --with-config-file-path=/usr/local/lib/php5/ini/5.2.5 \ --with-config-file-scan-dir=/usr/local/lib/php5/ini.d \ --enable-zend-multibyte \ --enable-mbstring=shared \ --enable-dom \ --enable-filter=shared \ --with-gettext=shared \ --with-mcrypt=shared \ --with-gd=shared \ --with-jpeg-dir \ --with-png-dir \ --with-zlib-dir \ --with-ttf \ --with-freetype-dir \ --enable-gd-native-ttf \ --enable-gd-jis-conv \ --enable-soap=shared \ --enable-pdo=shared \ --with-pdo-sqlite=shared \ --with-sqlite=shared \ --enable-sqlite-utf8 \ --with-openssl=shared \ --with-curl=shared \ --enable-bcmath=shared \ --enable-pcntl=shared \ --enable-sockets=shared \ --with-oci8=shared,instantclient,/usr/lib/oracle/11.1.0.1/client/lib/ \ --with-pdo-oci=shared,instantclient,/usr,11.1.0.1 \ --enable-sigchild \ --with-xsl=shared \ --enable-zip=shared \ --with-bz2=shared \ --with-mysql=shared \ --with-pdo-mysql=shared \ --enable-taint
インストール後、phpinfoを表示。結構パラメータが増えてます。この辺はhttp://wiki.php.net/rfc/taint/に情報があります。
で、掲載されているサンプルをちょっと変えてブラウザから呼び出し。
<?php /** * @see http://wiki.php.net/rfc/taint */ ini_set("taint_error_level", E_WARNING); $inputfield = isset($_GET['inputfield']) ? $_GET['inputfield'] : ''; echo "You entered: $inputfield\n"; ?> <form action="" method="get"> <input type="text" name="inputfield"> <input type="submit"> </form>
適当に入力してsubmitすると
Warning: echo() [function.echo]: Argument contains data that is not converted with htmlspecialchars() or htmlentities() in /home/shimooka/public_html/taint/taint01.php on line 8
とWarningが発生しました。
で、echoしている部分を
<?php : printf('You entered: %s<br>', htmlspecialchars($inputfield, ENT_QUOTES)); :
とすると、先ほどのWarningは発生しませんでした。$inputfieldに代入するときにhtmlspecialcharsを噛ましても同じでした。
なるほど。これはいいなぁ。PECL形式でリリースされないかな。
なお、DBや外部コマンド実行などについても適用できるようですが、これはあとで試してみよう。