Skip to content

Commit

Permalink
メールとブログの説明文について script タグを除外
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuring committed Oct 1, 2023
1 parent 8bd76f1 commit 5a20626
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
19 changes: 19 additions & 0 deletions lib/Baser/Lib/BcUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,4 +520,23 @@ public static function getAdminPrefix()
return Configure::read('BcAuthPrefix.admin.alias');
}

/**
* 文字列よりスクリプトタグを除去する
*
* @param string $value
* @return string
*/
public static function stripScriptTag($value)
{
$allows = [
'a', 'abbr', 'address', 'area', 'b', 'blockquote', 'body', 'br', 'button', 'caption', 'cite', 'code',
'col', 'colgroup', 'dd', 'del', 'dfn', 'div', 'dl', 'dt', 'em', 'fieldset', 'form', 'h1', 'h2', 'h3',
'h4', 'h5', 'h6', 'hr', 'i', 'iframe', 'img', 'input', 'ins', 'kbd', 'label', 'legend', 'li', 'link',
'map', 'meta', 'noscript', 'object', 'ol', 'optgroup', 'option', 'p', 'pre', 'q', 'samp', 'select',
'small', 'span', 'strong', 'sub', 'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead',
'title', 'tr', 'ul', 'var', 'style'
];
return strip_tags($value, '<' . implode('><', $allows) . '>');
}

}
2 changes: 1 addition & 1 deletion lib/Baser/Plugin/Blog/View/Helper/BlogHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function getTitle()
*/
public function getDescription()
{
return $this->blogContent['description'];
return BcUtil::stripScriptTag($this->blogContent['description']);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Baser/Plugin/Mail/View/Helper/MailHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function getMailTemplates($siteId = 0)
*/
public function getDescription()
{
return preg_replace('/<script.*?>(.*?)<\/script>/', '', $this->mailContent['description']);
return BcUtil::stripScriptTag($this->mailContent['description']);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions lib/Baser/Test/Case/Lib/BcUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,4 +481,29 @@ public function getSubDomainDataProvider()
];
}

/**
* test stripScriptTag
* @return void
* @dataProvider stripScriptTagDataProvider
*/
public function testStripScriptTag($content, $expect)
{
$result = BcUtil::stripScriptTag($content);
$this->assertEquals($expect, $result, 'scriptタグを削除できません。');
}

public function stripScriptTagDataProvider()
{
return [
[
'content' => '<script>hoge</script>',
'expect' => 'hoge'
],
[
'content' => '<a href="http://hoge.com" class="bca-action">hoge<script>hoge</script></a>',
'expect' => '<a href="http://hoge.com" class="bca-action">hogehoge</a>'
]
];
}

}

0 comments on commit 5a20626

Please sign in to comment.