Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated sanitizer #8

Merged
merged 2 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 11 additions & 37 deletions Service/MarkdownParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,26 @@

namespace Northern\MarkdownBundle\Service;

use HtmlSanitizer\Sanitizer;
use HtmlSanitizer\SanitizerInterface;
use Symfony\Component\HtmlSanitizer\HtmlSanitizer;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig;

class MarkdownParser implements MarkdownParserInterface
{
private \Parsedown $parsedown;

private SanitizerInterface $sanitizer;
private HtmlSanitizer $sanitizer;

public function __construct()
{
$this->parsedown = new \Parsedown();
$this->sanitizer = Sanitizer::create(
[
'max_input_length' => 1000000,
'extensions' => ['basic', 'list', 'table', 'image', 'code', 'extra'],
'tags' => [
'a' => [
'allowed_schemes' => ['http', 'https', null],
'allowed_attributes' => ['href', 'name', 'title'],
],
'code' => ['allowed_attributes' => ['class']],
'em' => ['allowed_attributes' => ['class']],
'th' => ['allowed_attributes' => ['style']],
'td' => ['allowed_attributes' => ['style']],
'h1' => [
'allowed_attributes' => ['id', 'name'],
],
'h2' => [
'allowed_attributes' => ['id', 'name'],
],
'h3' => [
'allowed_attributes' => ['id', 'name'],
],
'h4' => [
'allowed_attributes' => ['id', 'name'],
],
'h5' => [
'allowed_attributes' => ['id', 'name'],
],
'h6' => [
'allowed_attributes' => ['id', 'name'],
],
],
]
);

$sanitizerConfig = new HtmlSanitizerConfig();
$sanitizerConfig = $sanitizerConfig->withMaxInputLength(1_000_000);
$sanitizerConfig = $sanitizerConfig->allowSafeElements();
$sanitizerConfig = $sanitizerConfig->allowAttribute('class', '*');
$sanitizerConfig = $sanitizerConfig->allowAttribute('style', '*');

$this->sanitizer = new HtmlSanitizer($sanitizerConfig);
}

public function convertMarkdownToHtml(string $markdown): string
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"description": "Symfony bundle for including parsedown for converting markdown to html",
"type": "symfony-bundle",
"require": {
"php": ">=7.4",
"php": ">=8.1",
"erusev/parsedown": "^1.7",
"symfony/cache-contracts": "^2.0|^3.0",
"symfony/config": "^5.4|^6.0",
"symfony/dependency-injection": "^5.4|^6.0",
"symfony/framework-bundle": "^5.4|^6.0",
"tgalopin/html-sanitizer": "^1.3",
"symfony/html-sanitizer": "^6.1",
"twig/twig": "^2.12|^3.0"
},
"autoload": {
Expand Down