Issues (116)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

Bundle/AppBundle/Command/Locale/AddCommand.php (3 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 * 
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 * 
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\AppBundle\Command\Locale;
14
15
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console\Question\ChoiceQuestion;
19
use Symfony\Component\Intl\Intl;
20
use WellCommerce\Bundle\AppBundle\DataSet\Admin\CurrencyDataSet;
21
use WellCommerce\Bundle\AppBundle\DataSet\Admin\LocaleDataSet;
22
use WellCommerce\Bundle\AppBundle\Entity\Currency;
23
use WellCommerce\Bundle\AppBundle\Entity\Locale;
24
use WellCommerce\Bundle\AppBundle\Service\Locale\Copier\LocaleCopierInterface;
25
use WellCommerce\Bundle\CoreBundle\Manager\ManagerInterface;
26
27
/**
28
 * Class AddLocaleCommand
29
 *
30
 * @author  Adam Piotrowski <[email protected]>
31
 */
32
final class AddCommand extends ContainerAwareCommand
33
{
34
    /**
35
     * @var ManagerInterface
36
     */
37
    protected $localeManager;
38
    
39
    /**
40
     * @var LocaleDataSet
41
     */
42
    protected $localeDataSet;
43
    
44
    /**
45
     * @var CurrencyDataSet
46
     */
47
    protected $currencyDataSet;
48
    
49
    /**
50
     * @var array
51
     */
52
    protected $installedLocales = [];
53
    
54
    /**
55
     * @var array
56
     */
57
    protected $installedCurrencies = [];
58
    
59
    /**
60
     * @var array
61
     */
62
    protected $availableLocales = [];
63
    
64
    protected function configure()
65
    {
66
        $this->setDescription('Adds a new locale and copies translatable entities');
67
        $this->setName('locale:add');
68
    }
69
    
70
    protected function initialize(InputInterface $input, OutputInterface $output)
71
    {
72
        $this->localeManager       = $this->getLocaleManager();
73
        $this->localeDataSet       = $this->getLocaleDataSet();
74
        $this->currencyDataSet     = $this->getCurrencyDataSet();
75
        $this->installedLocales    = $this->getInstalledLocales();
76
        $this->installedCurrencies = $this->getInstalledCurrencies();
77
        $this->availableLocales    = $this->getAvailableLocales();
78
    }
79
    
80
    protected function execute(InputInterface $input, OutputInterface $output)
81
    {
82
        $sourceLocale   = $this->chooseSourceLocale($input, $output);
83
        $targetLocale   = $this->chooseTargetLocale($input, $output);
84
        $targetCurrency = $this->chooseTargetCurrency($input, $output);
85
        
86
        $locale = $this->createLocale($targetLocale, $targetCurrency);
87
        $output->writeln(sprintf('<info>Created a new locale "%s"</info>', $locale->getCode()));
88
        
89
        $this->copyLocaleData($sourceLocale, $targetLocale);
90
        
91
        $output->writeln(sprintf('<info>Finished copying "%s" data</info>', $locale->getCode()));
92
    }
93
    
94
    private function createLocale(string $localeCode, string $targetLocaleCurrency): Locale
95
    {
96
        $currency = $this->getTargetCurrency($targetLocaleCurrency);
97
        /** @var Locale $locale */
98
        $locale = $this->localeManager->initResource();
99
        $locale->setCode($localeCode);
100
        $locale->setEnabled(true);
101
        $locale->setCurrency($currency);
102
        $this->localeManager->createResource($locale);
103
        
104
        return $locale;
105
    }
106
    
107
    private function findLocale(string $code): Locale
108
    {
109
        return $this->localeManager->getRepository()->findOneBy(['code' => $code]);
110
    }
111
    
112
    private function copyLocaleData(string $sourceLocaleCode, string $targetLocaleCode)
113
    {
114
        $sourceLocale = $this->findLocale($sourceLocaleCode);
115
        $targetLocale = $this->findLocale($targetLocaleCode);
116
        
117
        $this->getLocaleCopier()->copyLocaleData($sourceLocale, $targetLocale);
118
    }
119
    
120
    private function getTargetCurrency($targetCurrency): Currency
121
    {
122
        return $this->getContainer()->get('currency.repository')->findOneBy(['code' => $targetCurrency]);
123
    }
124
    
125
    private function chooseSourceLocale(InputInterface $input, OutputInterface $output): string
126
    {
127
        $defaultLocale = current($this->installedLocales);
128
        $question      = new ChoiceQuestion(
129
            sprintf(
130
                'Please select source locale from which new entities will be copied (defaults to "%s"):',
131
                $defaultLocale
132
            ),
133
            $this->installedLocales,
134
            $defaultLocale
135
        );
136
        
137
        $sourceLocale = $this->getHelper('question')->ask($input, $output, $question);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method ask() does only exist in the following implementations of said interface: Sensio\Bundle\GeneratorB...d\Helper\QuestionHelper, Symfony\Component\Console\Helper\QuestionHelper, Symfony\Component\Consol...r\SymfonyQuestionHelper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
138
        
139
        return $sourceLocale;
140
    }
141
    
142
    private function chooseTargetLocale(InputInterface $input, OutputInterface $output): string
143
    {
144
        $question     = new ChoiceQuestion('Please select target locale to which new entities will be copied:', $this->availableLocales);
145
        $targetLocale = $this->getHelper('question')->ask($input, $output, $question);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method ask() does only exist in the following implementations of said interface: Sensio\Bundle\GeneratorB...d\Helper\QuestionHelper, Symfony\Component\Console\Helper\QuestionHelper, Symfony\Component\Consol...r\SymfonyQuestionHelper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
146
        
147
        return $targetLocale;
148
    }
149
    
150
    private function chooseTargetCurrency(InputInterface $input, OutputInterface $output): string
151
    {
152
        $question             = new ChoiceQuestion('Please select a default currency for new locale:', $this->installedCurrencies);
153
        $targetLocaleCurrency = $this->getHelper('question')->ask($input, $output, $question);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method ask() does only exist in the following implementations of said interface: Sensio\Bundle\GeneratorB...d\Helper\QuestionHelper, Symfony\Component\Console\Helper\QuestionHelper, Symfony\Component\Consol...r\SymfonyQuestionHelper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
154
        
155
        return $targetLocaleCurrency;
156
    }
157
    
158
    private function getInstalledCurrencies(): array
159
    {
160
        return $this->currencyDataSet->getResult('select', ['order_by' => 'code'], [
161
            'label_column' => 'code',
162
            'value_column' => 'code',
163
        ]);
164
    }
165
    
166
    private function getInstalledLocales(): array
167
    {
168
        return $this->localeDataSet->getResult('select', ['order_by' => 'code'], [
169
            'label_column' => 'code',
170
            'value_column' => 'code',
171
        ]);
172
    }
173
    
174
    private function getAvailableLocales(): array
175
    {
176
        $locales    = [];
177
        $collection = Intl::getLocaleBundle()->getLocaleNames();
178
        
179
        foreach ($collection as $locale => $name) {
180
            if (!in_array($locale, $this->installedLocales)) {
181
                $locales[$locale] = $name;
182
            }
183
        }
184
        
185
        return $locales;
186
    }
187
    
188
    private function getLocaleDataSet(): LocaleDataSet
189
    {
190
        return $this->getContainer()->get('locale.dataset.admin');
191
    }
192
    
193
    private function getCurrencyDataSet(): CurrencyDataSet
194
    {
195
        return $this->getContainer()->get('currency.dataset.admin');
196
    }
197
    
198
    private function getLocaleManager(): ManagerInterface
199
    {
200
        return $this->getContainer()->get('locale.manager');
201
    }
202
    
203
    private function getLocaleCopier(): LocaleCopierInterface
204
    {
205
        return $this->getContainer()->get('locale.copier');
206
    }
207
}
208