Issues (28)

Security Analysis    no request data  

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.

src/Provider/OpenExchangeRatesProvider.php (2 issues)

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
namespace BenTools\Currency\Provider;
4
5
use BenTools\Currency\Model\CurrencyInterface;
6
use BenTools\Currency\Model\ExchangeRateFactoryInterface;
7
use BenTools\Currency\Model\ExchangeRateInterface;
8
use BenTools\Currency\Model\ExchangeRateNotFoundException;
9
use BenTools\Currency\Model\NativeExchangeRateFactory;
10
use DateTime;
11
use DateTimeImmutable;
12
use DateTimeInterface;
13
use DateTimeZone;
14
use Http\Client\HttpClient;
15
use Http\Discovery\HttpClientDiscovery;
16
use Http\Discovery\MessageFactoryDiscovery;
17
use Http\Message\RequestFactory;
18
use Psr\SimpleCache\CacheInterface;
19
20
final class OpenExchangeRatesProvider implements ExchangeRateProviderInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    private $appId;
26
27
    /**
28
     * @var HttpClient|null
29
     */
30
    private $client;
31
32
    /**
33
     * @var RequestFactory|null
34
     */
35
    private $requestFactory;
36
37
    /**
38
     * @var ExchangeRateFactoryInterface|null
39
     */
40
    private $exchangeRateFactory;
41
42
    /**
43
     * OpenExchangeRatesProvider constructor.
44
     * @param string                            $appId
45
     * @param HttpClient|null                   $client
46
     * @param RequestFactory|null               $requestFactory
47
     * @param ExchangeRateFactoryInterface|null $exchangeRateFactory
48
     * @throws \Http\Discovery\Exception\NotFoundException
49
     */
50
    public function __construct(
51
        string $appId,
52
        HttpClient $client = null,
53
        RequestFactory $requestFactory = null,
54
        ExchangeRateFactoryInterface $exchangeRateFactory = null
55
    ) {
56
        $this->appId = $appId;
57
        $this->client = $client ?? HttpClientDiscovery::find();
58
        $this->requestFactory = $requestFactory ?? MessageFactoryDiscovery::find();
59
        $this->exchangeRateFactory = $exchangeRateFactory ?? new NativeExchangeRateFactory();
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function getExchangeRate(CurrencyInterface $sourceCurrency, CurrencyInterface $targetCurrency, DateTimeInterface $date = null): ExchangeRateInterface
66
    {
67
        if (null === $date) {
68
            $date = new DateTimeImmutable('now', new DateTimeZone('UTC'));
69
        }
70
71
        if ($date instanceof DateTime) {
72
            $date = DateTimeImmutable::createFromMutable($date)->setTimezone(new DateTimeZone('UTC'));
73
        }
74
75
        if (!in_array('USD', [$sourceCurrency->getCode(), $targetCurrency->getCode()])) {
76
            throw new ExchangeRateNotFoundException($sourceCurrency, $targetCurrency, "OpenExchangeRates Free plan only provide USD-based currency conversions.");
77
        }
78
79
        // Same currencies
80
        if ($sourceCurrency->getCode() === $targetCurrency->getCode()) {
81
            return $this->exchangeRateFactory->create($sourceCurrency, $targetCurrency, 1);
82
        }
83
84
        // Invert currencies
85
        if ('USD' === $targetCurrency->getCode()) { // OpenExchangeRates only provide USD -> *
86
            $revertExchangeRate = $this->getExchangeRate($targetCurrency, $sourceCurrency, $date);
0 ignored issues
show
It seems like $date defined by \DateTimeImmutable::crea...w \DateTimeZone('UTC')) on line 72 can also be of type false; however, BenTools\Currency\Provid...ider::getExchangeRate() does only seem to accept null|object<DateTimeInterface>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
87
            return $this->exchangeRateFactory->create($sourceCurrency, $targetCurrency, 1 / $revertExchangeRate->getRatio());
88
        }
89
90
        $url = sprintf('https://openexchangerates.org/api/historical/%s.json?app_id=%s', $date->format('Y-m-d'), $this->appId);
0 ignored issues
show
It seems like $date is not always an object, but can also be of type false. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
91
        $response = $this->client->sendRequest($this->requestFactory->createRequest('GET', $url));
92
        $json = json_decode((string) $response->getBody(), true);
93
        if (isset($json['rates'][$targetCurrency->getCode()])) {
94
            return $this->exchangeRateFactory->create($sourceCurrency, $targetCurrency, $json['rates'][$targetCurrency->getCode()]);
95
        }
96
97
        throw new ExchangeRateNotFoundException($sourceCurrency, $targetCurrency);
98
    }
99
}
100