Completed
Push — hotfix/samesite-cookie ( 25bbba...3603e5 )
by Kiyotaka
06:11
created

SameSiteCookieHotfixListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A onKernelRequest() 0 15 3
A getSubscribedEvents() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\EventListener;
15
16
use Eccube\Request\Context;
17
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
20
21
/**
22
 * Safariの一部のバージョンでSameSite=Noneを正しく扱われないバグ対応.
23
 *
24
 * @see https://bugs.webkit.org/show_bug.cgi?id=198181
25
 */
26
class SameSiteCookieHotfixListener implements EventSubscriberInterface
27
{
28
    private static $TARGET_UA_PATTERNS = [
29
        '/^.*iPhone; CPU iPhone OS 1[0-2].*$/',
30
        '/^.*iPad; CPU OS 1[0-2].*$/',
31
        '/^.*iPod touch; CPU iPhone OS 1[0-2].*$/',
32
        '/^.*Macintosh; Intel Mac OS X.*Version\/1[0-2].*Safari.*$/',
33
    ];
34
35
    /**
36
     * @var Context
37
     */
38
    protected $requestContext;
39
40
    public function __construct(Context $requestContext)
41
    {
42
        $this->requestContext = $requestContext;
43
    }
44
45
    public function onKernelRequest(GetResponseEvent $event)
46
    {
47
        if (!$event->isMasterRequest()) {
48
            return;
49
        }
50
51
        $ua = $event->getRequest()->headers->get('User-Agent');
52
        $isUnsupported = array_filter(self::$TARGET_UA_PATTERNS, function ($pattern) use ($ua) {
53
            return preg_match($pattern, $ua);
54
        });
55
56
        if ($isUnsupported) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $isUnsupported of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
57
            $event->setResponse(new Response('お使いのブラウザーではご利用いただけません。'));
58
        }
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public static function getSubscribedEvents()
65
    {
66
        return [
67
            'kernel.request' => ['onKernelRequest', 256],
68
        ];
69
    }
70
}
71