1
0
mirror of https://github.com/flarum/core.git synced 2025-08-12 03:14:33 +02:00

Add HTMLPurifier after formatters are run.

After a morning of searching, it seems there is no PHP Markdown library
that has built-in XSS/sanitization support. The recommended solution is
to use HTMLPurifier.

This actually works out OK, though, as it’s probably a good idea to
enforce sanitization regardless of which formatters are enabled, and to
not leave them with the responsibility of sanitization (it’s a big
responsibility). Since we cache rendered posts, the slow speed of
HTMLPurifier isn’t a concern.

Note that HTMLPurifier requires a file to be loaded by Composer, but
Studio does not yet support this, so for now I have included it
manually.
This commit is contained in:
Toby Zerner
2015-06-02 11:36:25 +09:30
parent fb3038d128
commit 6cf1dbe648
3 changed files with 122 additions and 67 deletions

View File

@@ -1,6 +1,8 @@
<?php namespace Flarum\Core\Formatter;
use Illuminate\Container\Container;
use HTMLPurifier;
use HTMLPurifier_Config;
class FormatterManager
{
@@ -60,7 +62,20 @@ class FormatterManager
$text = $this->container->make($formatter)->format($text, $post);
}
return $text;
// Studio does not yet merge autoload_files...
// https://github.com/franzliedke/studio/commit/4f0f4314db4ed3e36c869a5f79b855c97bdd1be7
require __DIR__.'/../../../vendor/ezyang/htmlpurifier/library/HTMLPurifier.composer.php';
$config = HTMLPurifier_Config::createDefault();
$config->set('Core.Encoding', 'UTF-8');
$config->set('Core.EscapeInvalidTags', true);
$config->set('HTML.Doctype', 'HTML 4.01 Strict');
$config->set('HTML.Allowed', 'p,em,strong,a[href|title],ul,ol,li,code,pre,blockquote,h1,h2,h3,h4,h5,h6,br');
$config->set('HTML.Nofollow', true);
$purifier = new HTMLPurifier($config);
return $purifier->purify($text);
}
public function strip($text)