Issues (304)

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.

code/api/CountryURLProvider.php (6 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
/**
4
 * Usage:
5
 *     $myAnswer =
6
 *         CountryPrice_Translation::get_country_url_provider()
7
 *             ->getSomething();
8
 *
9
 */
10
11
class CountryURLProvider extends Object implements CountryURLProviderInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    private static $locale_get_parameter = 'ecomlocale';
17
18
    /**
19
     * returns the selected country code if there is one ...
20
     * as an uppercase code, e.g. NZ
21
     * @param string|null $url
22
     *
23
     * @return bool
24
     */
25
    public function hasCountrySegment($url = '')
26
    {
27
        return $this->CurrentCountrySegment($url) ? true : false;
28
    }
29
30
    /**
31
     * returns the selected country code if there is onCurrentCountrySegmente ...
32
     * as an uppercase code, e.g. NZ
33
     * @param string|null $url
34
     * @param bool $includeGetVariable
35
     *
36
     * @return string|null
37
     */
38
    public function CurrentCountrySegment($url = '', $includeGetVariable = true)
39
    {
40
        $potentialCountry = '';
41
        if ($includeGetVariable) {
42
            $getVar = Config::inst()->get('CountryURLProvider', 'locale_get_parameter');
43
            if (isset($_GET[$getVar])) {
44
                $potentialCountry = $_GET[$getVar];
45
            }
46
        }
47
        if (strlen($potentialCountry) !== 2) {
48
            $url = $this->getCurrentURL($url);
49
            $parts = parse_url($url);
50
            if (isset($parts['path'])) {
51
                $path = trim($parts['path'], '/');
52
                $array = explode('/', $path);
53
                $potentialCountry = isset($array[0]) ? trim($array[0]) : '';
54
            }
55
        }
56
        if (strlen($potentialCountry) === 2) {
57
            $potentialCountry = strtoupper($potentialCountry);
58
            $check = EcommerceCountry::get()->filter(['Code' => $potentialCountry])->count();
59
            if ($check) {
60
                return $potentialCountry;
61
            }
62
        }
63
    }
64
65
66
    /**
67
     * replaces a country code in a URL with another one
68
     *
69
     * @param  string $newCountryCode e.g. NZ / nz
70
     * @param  string $url
71
     *
72
     * @return string|null only returns a string if it is different from the original!
73
     */
74
    public function replaceCountryCodeInUrl($newCountryCode, $url = '')
75
    {
76
        $url = $this->getCurrentURL($url);
77
        $oldURL = $url;
78
        //debug::log($url);
79
80
        $newCountryCode = strtolower($newCountryCode);
81
        $parsedUrl = parse_url($url);
82
        if (isset($parsedUrl['path']) && isset($parsedUrl['host'])) {
83
            $path = $parsedUrl['path'];
84
            $path = trim($path, '/');
85
            $pathParts = explode('/', $path);
86
87
            $currentCountryCode = $this->CurrentCountrySegment($url, false);
0 ignored issues
show
It seems like $url defined by $this->getCurrentURL($url) on line 76 can also be of type false; however, CountryURLProvider::CurrentCountrySegment() does only seem to accept string|null, 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...
88
            if ($currentCountryCode) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $currentCountryCode of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
89
                $pathParts[0] = $newCountryCode;
90
            } else {
91
                array_unshift($pathParts, $newCountryCode);
92
            }
93
            $parsedUrl['path'] = implode('/', $pathParts);
94
            $newURL =
95
                $parsedUrl['scheme'] .
96
                '://' .
97
                Controller::join_links(
98
                    $parsedUrl['host'],
99
                    $parsedUrl['path']
100
                );
101
            if (isset($parsedUrl['query'])) {
102
                $newURL = $newURL . '?' . $parsedUrl['query'];
103
            }
104
        }
105
        if (trim($oldURL, '/') !== trim($newURL, '/')) {
106
            return $newURL;
0 ignored issues
show
The variable $newURL does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
107
        }
108
109
        return '';
110
    }
111
112
    /**
113
     *
114
     * @param  string|null $url can be a relative one or nothing at all ...
115
     *
116
     * @return string      full URL currently being called.
0 ignored issues
show
Should the return type not be false|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
117
     */
118
    public function getCurrentURL($url = '')
119
    {
120
        if ($url) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $url of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
121
            $url = Director::absoluteURL($url);
122
        } else {
123
            $protocol = Director::is_https() ? 'https://' : 'http://';
124
125
            $url = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
126
        }
127
        if (Director::is_site_url($url)) {
0 ignored issues
show
It seems like $url defined by \Director::absoluteURL($url) on line 121 can also be of type false; however, Director::is_site_url() does only seem to accept string, 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...
128
            return $url;
129
        } else {
130
            return Director::absoluteURL('/');
131
        }
132
    }
133
}
134