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

Allow relative links, add a config for disallowing #9

Merged
merged 3 commits into from
Apr 25, 2023
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
23 changes: 23 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Northern\MarkdownBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('northern_markdown');
$treeBuilder->getRootNode()
cpkdevries marked this conversation as resolved.
Show resolved Hide resolved
->children()
->booleanNode('allow_relative_links')
->info('Allow relative links when sanitizing.')
->defaultValue(true)
->end()
->end();

return $treeBuilder;
}
}
6 changes: 6 additions & 0 deletions DependencyInjection/NorthernMarkdownExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');

$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$maintenanceSubscriberDefinition = $container->getDefinition('northern_markdown.markdown_parser');
$maintenanceSubscriberDefinition->setArgument('$allowRelativeLinks', $config['allow_relative_links']);
}
}
10 changes: 8 additions & 2 deletions Service/MarkdownParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ class MarkdownParser implements MarkdownParserInterface

private HtmlSanitizer $sanitizer;

public function __construct()
{
public function __construct(
bool $allowRelativeLinks
) {
$this->parsedown = new \Parsedown();

$sanitizerConfig = new HtmlSanitizerConfig();
$sanitizerConfig = $sanitizerConfig->withMaxInputLength(1_000_000);
$sanitizerConfig = $sanitizerConfig->allowSafeElements();

if ($allowRelativeLinks) {
$sanitizerConfig = $sanitizerConfig->allowRelativeLinks();
}

$sanitizerConfig = $sanitizerConfig->allowAttribute('class', '*');
$sanitizerConfig = $sanitizerConfig->allowAttribute('style', '*');

Expand Down