###前提・実現したいこと
【使用するファイル】
■texteditor.php
■output.php
■log.txt
【前提】
xss対策をしたいので、サニタイズをしっかり行いたいです。
【概要】
texteditor.phpでlog.txtの中身を読み込み、テキストエリアに出力し編集可能にする。
output.phpでlog.txtを表示
【再現したいこと】
現状作成したテキストの表示と編集は再現できているのでやりたいことはほぼ再現出来ているのですが、このままでは脆弱性が残ったままかと思いますので、回答して頂ける皆様にはセキュリティ強化の提案をして頂きたいです。
また現状以下のソースでエラー等は発生しておりません。宜しくお願い致します。
【再現済みのもの】
指定されたディレクトリー内にあるファイルの取得と表示及び編集
###該当のソースコード
texteditor.php
php
1<!DOCTYPE html> 2<html lang="ja"> 3 4<head> 5 <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS"> 6 <title>編集画面</title> 7</head> 8 9<body> 10 11 <form method="post" action="texteditor.php"> 12 <table> 13 <tr> 14 <td> 15 <select name="file" size="20"> 16<?php 17// ファイル一覧を表示 18$cnt = 1; 19$dir = opendir("./textfiles/"); //ディレクトリハンドル取得 20 21while( $file = readdir($dir) ) { //textfilesディレクトリ内のファイル名を存在するだけ$fileに格納 22 if( is_file("./textfiles/$file") ) { //ファイルかどうかチェック 23 if ( $cnt==1 ) { 24 print "<option selected>$file</option>\n"; 25 } 26 print "<option>$file</option>\n"; 27 } 28 $cnt++; 29} 30 31closedir($dir); 32?> 33 </select> 34 </td> 35 36 <td> 37 <textarea name="contents" cols="130" rows="20"> 38<?php 39// ファイル内容を表示 40$file = $_POST['file']; 41if ( $_POST['open'] && $file ) { //ファイルを選択かつ開くボタンを押している状態の時のみ 42 $text = file_get_contents("./textfiles/$file"); 43 print $text; 44} 45?> 46 </textarea> 47 </td> 48 </tr> 49 <tr> 50 <td align="right"> 51 <input type="submit" name="open" value="開く"> 52 </td> 53 <td align="right"> 54 <input type="submit" name="save" value="保存"> 55 </td> 56 </tr> 57 </table> 58 <input type="hidden" name="editfile" value="<?php print "./textfiles/".$file ?>"> 59 </form> 60 61 62<?php 63// ファイルを保存 64$editfile = $_POST['editfile']; 65if ( isset($_POST['save']) ) { 66 if ( $_POST['save'] && $editfile ) { 67 $fp = @fopen($editfile, 'w'); 68 if ( !$fp ) { 69 print "このファイルには書き込みできません。<br>\n"; 70 } else { 71 $contents = $_POST['contents']; 72 fwrite($fp, $contents); 73 fclose($fp); 74 print "書き込み完了しました。<br>\n"; 75 } 76 } 77} 78?> 79 80</body> 81 82</html> 83
log.txt
html
12017/01/12 <a href="http://test.com/" class="overlay">更新1</a> 22017/01/12 <a href="http://test.com/" class="overlay"><img src="http://test.com/test.png"></a>更新履歴2 32017/01/11 更新履歴3
output.php
php
1<!DOCTYPE html> 2<html> 3 <head> 4 <meta charset="utf-8"> 5 <title>出力画面</title> 6 </head> 7 <body> 8 <div class=""> 9 <?php 10 $file_route = "./textfiles/log.txt"; 11 $i = file($file_route); 12 13 foreach ($i as $content) { 14 print "<li>".$content."</li>"; 15 } 16 ?> 17 </div> 18 </body> 19</html> 20