Table of Contents

pcreメタ文字のエスケープはデリミター文字を指定する

PHP 7.0未満のPHPではPCRE関数はe修飾子をサポートしている。ユーザー入力を正規表現に利用する場合、pcre_quote関数によるエスケープが必要だが、PCREのデリミター文字は任意の記号文字が指定できるため、オプションのデリミター文字を必ず指定しなければならない。指定しない場合、任意PHPコード実行が可能となる。

非適合コード例(デリミター文字なし)

         foreach ($parameters as $parameter => $replacement) {
             $quoted = preg_quote($parameter); // ここが非適合
             // making sure that :param does not apply values to :param1
             $sql_query = preg_replace(
                 '/' . $quoted . '([^a-zA-Z0-9_])/',
                 PMA\libraries\Util::sqlAddSlashes($replacement) . '${1}',
                 $sql_query
             );
             // for parameters the appear at the end of the string
             $sql_query = preg_replace(
                 '/' . $quoted . '$/',
                 PMA\libraries\Util::sqlAddSlashes($replacement),
                 $sql_query
             );
         }

非適合コード例(非互換エスケープ関数の利用)

quotemeta関数はPCRE正規表現のエスケープに利用できない。エスケープ対象文字が異る上、デリミター文字が指定できない。

         foreach ($parameters as $parameter => $replacement) {
             $quoted = quotemeta($parameter); // ここが非適合。quotemeta関数はPCRE用のエスケープ関数ではない。
             // making sure that :param does not apply values to :param1
             $sql_query = preg_replace(
                 '/' . $quoted . '([^a-zA-Z0-9_])/',
                 PMA\libraries\Util::sqlAddSlashes($replacement) . '${1}',
                 $sql_query
             );
             // for parameters the appear at the end of the string
             $sql_query = preg_replace(
                 '/' . $quoted . '$/',
                 PMA\libraries\Util::sqlAddSlashes($replacement),
                 $sql_query
             );
         }

適合コード例

         foreach ($parameters as $parameter => $replacement) {
             $quoted = preg_quote($parameter, '/'); // 必ずデリミター文字を指定する
             // making sure that :param does not apply values to :param1
             $sql_query = preg_replace(
                 '/' . $quoted . '([^a-zA-Z0-9_])/', // 必ずデリミター文字は一致しなければならない
                 PMA\libraries\Util::sqlAddSlashes($replacement) . '${1}',
                 $sql_query
             );
             // for parameters the appear at the end of the string
             $sql_query = preg_replace(
                 '/' . $quoted . '$/', // 必ずデリミター文字は一致しなければならない
                 PMA\libraries\Util::sqlAddSlashes($replacement),
                 $sql_query
             );
         }

例外

例外はない。必ずデリミター文字を指定する。

リスク評価

[ルール/推奨事項のリスク評価]

[評価例 - 英語表記に統一]

Rule Severity Likelihood Remediation Cost Priority Level
IDS05-J medium unlikely medium P4 L3

関連ガイドライン

参考文献