Issues (4)

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.

src/Message/ResponseDataValidator.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
namespace Omnipay\Gtpay\Message;
3
4
use Omnipay\Gtpay\Exception\FailedPaymentException;
5
use Omnipay\Gtpay\Exception\ValidationException;
6
7
class ResponseDataValidator
8
{
9
    /**
10
     * @var \Omnipay\Common\Message\RequestInterface | AbstractRequest
11
     */
12
    private $request;
13
14
    private $data;
15
16
    private $response;
17
18 10
    public function __construct(CompletePurchaseResponse $response)
19
    {
20 10
        $this->request = $response->getRequest();
21 10
        $this->data = $response->getData();
22 10
        $this->response = $response;
23 10
    }
24
25 10
    public function validateTransactionRef($expectedRef, $returnedRef)
26
    {
27 10
        if (!$expectedRef || !$returnedRef) {
28
            return false;
29
        }
30 10
        return self::compareStrings($expectedRef, $returnedRef);
31
    }
32
33 10
    public static function compareStrings($one, $two)
34
    {
35 10
        return strnatcasecmp($one, $two) === 0;
36
    }
37
38 8
    public function verifyHashValue($gatewayHash, $expectedHash)
39
    {
40 8
        return  self::compareStrings($gatewayHash, $expectedHash);
41
    }
42
43
    /**
44
     * checks if amount paid is identical to amount due
45
     * @param float $gatewayAmount Amount returned by the gateway
46
     * @param float $dueAmount Expected Amount to be returned
47
     * @return bool
48
     */
49 2
    public function verifyCorrectAmount($gatewayAmount, $dueAmount)
50
    {
51 2
        $gatewayAmount = floatval($gatewayAmount);
52 2
        $dueAmount = floatval($dueAmount);
53
54 2
        return abs($dueAmount - $gatewayAmount)< 0.0001;
55
    }
56
57
    /**
58
     * @todo move most of this into a validator class
59
     * @throws FailedPaymentException
60
     * @throws ValidationException
61
     */
62 10
    public function validate()
63
    {
64 10
        $statusCode = $this->data['gtpay_tranx_status_code'];
65
66 10
        if (!$this->validateTransactionRef(
67 10
            $this->request->getTransactionId(),
68 10
            $this->data['gtpay_tranx_id']
69
        )
70
        ) {
71 1
            throw $this->determineException(sprintf(
72 1
                "Invalid Transaction ref: %s",
73 1
                $this->data['gtpay_tranx_id']
74 1
            ), $statusCode);
75
        }
76
77 9
        if (self::compareStrings(CompletePurchaseResponse::CANCELED_GATEWAY_CODE, $statusCode)
78
        ) {
79 1
            throw $this->determineException("Customer Cancellation", $statusCode);
80
        }
81
82 8
        if (!$this->verifyHashValue(
83 8
            $this->data['gtpay_full_verification_hash'],
84 8
            $this->getFullVerificationHash($statusCode)
85
        )) {
86 1
            $msg = "Data incompatibility reported. Please contact support";
87 1
            throw $this->determineException($msg, $statusCode);
88
        }
89 7
        if (!self::compareStrings($this->data['gtpay_cust_id'], $this->request->getCustomerId())) {
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Omnipay\Common\Message\RequestInterface as the method getCustomerId() does only exist in the following implementations of said interface: Omnipay\Gtpay\Message\AbstractRequest, Omnipay\Gtpay\Message\CompletePurchaseRequest, Omnipay\Gtpay\Message\PurchaseRequest.

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...
90 1
            $msg = "Received Customer Id: {$this->data['gtpay_cust_id']} does not match expected Customer Id";
91 1
            throw $this->determineException($msg, $statusCode);
92
        }
93 6
        if (!self::compareStrings($this->data['site_redirect_url'], $this->request->getNotifyUrl())) {
94 1
            throw $this->determineException("Redirect Url is wrong.", $statusCode);
95
        }
96 5
    }
97
98 4
    public function successValidate()
99
    {
100
101 4
        if (isset($this->data['TransactionCurrency'])) {
102 1
            if (!self::compareStrings($this->data['TransactionCurrency'], $this->request->getCurrency())) {
103 1
                throw new ValidationException("Transaction currency does not match expected currency.");
104
            }
105
        }
106
107 3
        if (!self::compareStrings($this->data['MertID'], $this->request->getMerchantId())) {
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Omnipay\Common\Message\RequestInterface as the method getMerchantId() does only exist in the following implementations of said interface: Omnipay\Gtpay\Message\AbstractRequest, Omnipay\Gtpay\Message\CompletePurchaseRequest, Omnipay\Gtpay\Message\PurchaseRequest.

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...
108 1
            throw new ValidationException("Wrong Merchant ID returned.");
109
        }
110 2
        if (!$this->verifyCorrectAmount($this->data['Amount'], $this->request->getAmountInteger())) {
111 1
            throw new ValidationException(
112 1
                sprintf(
113 1
                    "Incorrect Amount Paid. Expected Amount: %s, Amount Paid: %s",
114 1
                    $this->response->formatIntegerAmount($this->request->getAmountInteger()),
115 1
                    $this->response->formatIntegerAmount($this->data['Amount'])
116
                )
117
            );
118
        }
119 1
    }
120
121
    /**
122
     * Distinguishes between exceptions that have a failed status code from the gateway
123
     * and exceptions when the status code indicates success. The later may indicate fraud.
124
     * For validation exception, you may want to consider sending an email to admin as further investigation
125
     * may be required
126
     * @param $msg
127
     * @param $statusCode
128
     * @return FailedPaymentException|ValidationException
129
     */
130 5
    private function determineException($msg, $statusCode)
131
    {
132 5
        if ($this->response->hasSuccessCode($statusCode)) {
133 4
            return new ValidationException($msg);
134
        } else {
135 1
            return new FailedPaymentException($msg);
136
        }
137
    }
138
139 8
    public function getFullVerificationHash($statusCode)
140
    {
141 8
        $rawString = $this->response->getTransactionId().
142 8
            $this->response->getGatewayAmountInteger().
143 8
            $statusCode.
144 8
            $this->response->getGatewayNumericCurrency().
145 8
            $this->request->getHashKey();
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Omnipay\Common\Message\RequestInterface as the method getHashKey() does only exist in the following implementations of said interface: Omnipay\Gtpay\Message\AbstractRequest, Omnipay\Gtpay\Message\CompletePurchaseRequest, Omnipay\Gtpay\Message\PurchaseRequest.

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 8
        return hash('sha512', $rawString);
147
    }
148
}
149