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

フロント入力項目のサニタイズ #5081

Merged
merged 9 commits into from
Jul 9, 2021
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
1 change: 1 addition & 0 deletions app/config/eccube/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
SunCat\MobileDetectBundle\MobileDetectBundle::class => ['all' => true],
Knp\Bundle\PaginatorBundle\KnpPaginatorBundle::class => ['all' => true],
Exercise\HTMLPurifierBundle\ExerciseHTMLPurifierBundle::class => ['all' => true],
];
6 changes: 6 additions & 0 deletions app/config/eccube/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,9 @@ services:
- { name: mobile_detect.mobile_detector.default }
- { name: mobile_detect.mobile_detector }
public: true

Eccube\Form\Extension\HTMLPurifierTextTypeExtension:
arguments:
- '@Eccube\Request\Context'
tags:
- { name: form.type_extension, priority: -99, extended_type: Symfony\Component\Form\Extension\Core\Type\TextType }
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"easycorp/easy-log-handler": "^1.0",
"ec-cube/plugin-installer": "^2.0",
"egulias/email-validator": "^2.1",
"exercise/htmlpurifier-bundle": "^3.1",
"friendsofphp/php-cs-fixer": "^2.16",
"guzzlehttp/guzzle": "^6.3",
"knplabs/knp-paginator-bundle": "^2.7",
Expand Down
113 changes: 111 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions src/Eccube/Form/EventListener/HTMLPurifierListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Eccube\Form\EventListener;


use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

class HTMLPurifierListener implements EventSubscriberInterface
{
/**
* @return array[]
*/
public static function getSubscribedEvents(): array
{
return [
FormEvents::PRE_SUBMIT => ['purifySubmittedData', /* as soon as possible */ 1000001],
];
}

public function purifySubmittedData(FormEvent $event): void
{
$event->setData(str_replace(['<', '>', '&'], ['<', '>', '&'], $event->getData()));
}
}
67 changes: 67 additions & 0 deletions src/Eccube/Form/Extension/HTMLPurifierTextTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Eccube\Form\Extension;


use Eccube\Form\EventListener\HTMLPurifierListener;
use Eccube\Request\Context;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class HTMLPurifierTextTypeExtension extends AbstractTypeExtension
{
/**
* @var Context
*/
private $context;

public function __construct(Context $context)
{
$this->context = $context;
}

public function configureOptions(OptionsResolver $resolver)
{
if ($this->context->isFront()) {
$resolver->setDefault('purify_html', true);
}
}

/**
* @return string
*/
public function getExtendedType(): string
{
return TextType::class;
}

/**
* @return iterable
*/
public static function getExtendedTypes(): iterable
{
yield TextType::class;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
if ($options['purify_html']) {
$builder->addEventSubscriber(
new HTMLPurifierListener()
);
}
}
}
12 changes: 12 additions & 0 deletions symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@
"egulias/email-validator": {
"version": "1.2.14"
},
"exercise/htmlpurifier-bundle": {
"version": "3.0",
"recipe": {
"repo": "github.com/symfony/recipes-contrib",
"branch": "master",
"version": "3.0",
"ref": "4667fd2103dc4645d234591e1c85f727c6fe0d11"
}
},
"ezyang/htmlpurifier": {
"version": "v4.13.0"
},
"friendsofphp/php-cs-fixer": {
"version": "2.2",
"recipe": {
Expand Down
4 changes: 2 additions & 2 deletions tests/Eccube/Tests/Web/ContactControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ public function testCompleteWithSanitize()
$this->actual = $Message->getSubject();
$this->verify();

$this->assertContains('<Sanitize&>', $Message->getBody(), 'テキストメールがサニタイズされている');
$this->assertContains('<Sanitize>', $Message->getBody(), 'テキストメールがサニタイズされている');

$MultiPart = $Message->getChildren();
foreach ($MultiPart as $Part) {
if ($Part->getContentType() == 'text/html') {
$this->assertContains('&lt;Sanitize&amp;&gt;', $Part->getBody(), 'HTMLメールがサニタイズされている');
$this->assertContains('Sanitize&>', $Part->getBody(), 'HTMLメールがサニタイズされている');
}
}
}
Expand Down
38 changes: 36 additions & 2 deletions tests/Eccube/Tests/Web/EntryControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ public function testCompleteWithActivateWithMultipartSanitize()
$this->actual = $Message->getSubject();
$this->verify();

$this->assertContains('<Sanitize&>', $Message->getBody(), 'テキストメールがサニタイズされている');
$this->assertContains('<Sanitize>', $Message->getBody(), 'テキストメールがサニタイズされている');

$MultiPart = $Message->getChildren();
foreach ($MultiPart as $Part) {
if ($Part->getContentType() == 'text/html') {
$this->assertContains('&lt;Sanitize&amp;&gt;', $Part->getBody(), 'HTMLメールがサニタイズされている');
$this->assertContains('Sanitize&>', $Part->getBody(), 'HTMLメールがサニタイズされている');
}
}
}
Expand Down Expand Up @@ -275,4 +275,38 @@ public function testActivateWithAbort()
$this->actual = $this->client->getResponse()->getStatusCode();
$this->verify();
}

public function testConfirmWithDangerousText()
{
$formData = $this->createFormData();
$formData['company_name'] = '<script>alert()</script>';

$crawler = $this->client->request('POST',
$this->generateUrl('entry'),
[
'entry' => $formData,
'mode' => 'confirm',
]
);

self::assertEquals('新規会員登録(確認)', $crawler->filter('.ec-pageHeader > h1')->text());
self::assertEquals('<script>alert()</script>', $crawler->filter('#entry_company_name')->attr('value'));
}

public function testConfirmWithAmpersand()
{
$formData = $this->createFormData();
$formData['company_name'] = '&';

$crawler = $this->client->request('POST',
$this->generateUrl('entry'),
[
'entry' => $formData,
'mode' => 'confirm',
]
);

self::assertEquals('新規会員登録(確認)', $crawler->filter('.ec-pageHeader > h1')->text());
self::assertEquals('&', $crawler->filter('#entry_company_name')->attr('value'));
}
}