From 0c2562143c531ad7e58df62a617be05d33288ca3 Mon Sep 17 00:00:00 2001 From: Habu Date: Tue, 3 Apr 2018 23:18:35 +0900 Subject: [PATCH] =?utf8?q?[add]=E3=82=B9=E3=82=AF=E3=83=AA=E3=83=BC?= =?utf8?q?=E3=83=B3=E3=83=80=E3=83=B3=E3=83=97=E3=81=AE=E3=83=90=E3=83=AA?= =?utf8?q?=E3=83=87=E3=83=BC=E3=82=B7=E3=83=A7=E3=83=B3=E3=82=92=E8=BF=BD?= =?utf8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- score/register_score.php | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/score/register_score.php b/score/register_score.php index f83cd35..fb8d898 100644 --- a/score/register_score.php +++ b/score/register_score.php @@ -146,6 +146,39 @@ function create_db_insert_score_data($info) } +/** + * スクリーンダンプのバリデーションを行う + * + * スクリプト実行などの悪意を持ったスクリーンダンプを登録できないよう、 + * 使用可能なタグをhtml,body,pre,fontに制限する + * + * @param array $screen_dump_lines スクリーンダンプの文字列の配列 + * @return バリデーションに成功したらTRUE、失敗したらFALSE + */ +function validate_screen_dump($screen_dump_lines) +{ + if ($screen_dump_lines === FALSE) { + return FALSE; + } + + $allow_tags = ['html', 'body', 'pre', 'font']; + + $is_valid = TRUE; + foreach ($screen_dump_lines as $line) { + if (preg_match_all("|\s]+)(\s*[^>]+)?>|", $line, $matches, PREG_SET_ORDER) > 0) { + $invalid_tag_matches = array_filter($matches, function($m) use($allow_tags) { + return !in_array($m[1], $allow_tags); + }); + if (count($invalid_tag_matches) > 0) { + $is_valid = FALSE; + } + } + } + + return $is_valid; +} + + $recv_encoding = get_mb_encoding(); if ($recv_encoding === FALSE) { exit; @@ -174,7 +207,11 @@ if ($score_id === FALSE) { $dumpfile = new DumpFile($score_id); $dumpfile->save('dumps', 'txt', $split_contents[1]); -$dumpfile->save('screens', 'html', $split_contents[2]); +if (validate_screen_dump($split_contents[2])) { + $dumpfile->save('screens', 'html', $split_contents[2]); +} else { + $dumpfile->save('screens', 'html.bad', $split_contents[2]); +} // 登録成功、HTTPレスポンスコード 200 OK を返す http_response_code(200); -- 2.11.0