Issues (2002)

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/tasks/EcommerceTaskOrderItemsPerCustomer.php (5 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
 * set the order id number.
5
 *
6
 * @authors: Nicolaas [at] Sunny Side Up .co.nz
7
 * @package: ecommerce
8
 * @sub-package: tasks
9
 * @inspiration: Silverstripe Ltd, Jeremy
10
 **/
11
class EcommerceTaskOrderItemsPerCustomer extends BuildTask
12
{
13
    protected $title = 'Export all order items to CSV per customer';
14
15
    protected $description = 'Allows download of all sales items with all details as CSV. Excludes sales made by Admins';
16
17
    public function run($request)
18
    {
19
        //reset time limit
20
        set_time_limit(1200);
21
22
        //file data
23
        $now = Date('d-m-Y-H-i');
24
        $fileName = "export-$now.csv";
25
26
        //data object variables
27
        $orderStatusSubmissionLog = EcommerceConfig::get('OrderStatusLog', 'order_status_log_class_used_for_submitting_order');
28
        $fileData = '';
29
        $offset = 0;
30
        $count = 50;
31
32
        while (
33
            $orders = Order::get()
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: $orders = (\Order::get()...nt = $orders->count())), Probably Intended Meaning: ($orders = \Order::get()...ount = $orders->count()
Loading history...
34
                ->sort('"Order"."ID" ASC')
35
                ->innerJoin('OrderStatusLog', '"Order"."ID" = "OrderStatusLog"."OrderID"')
36
                ->innerJoin($orderStatusSubmissionLog, "\"$orderStatusSubmissionLog\".\"ID\" = \"OrderStatusLog\".\"ID\"")
37
                ->leftJoin('Member', '"Member"."ID" = "Order"."MemberID"')
38
                ->limit($count, $offset) &&
39
            $ordersCount = $orders->count()
0 ignored issues
show
The variable $orders 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...
The method count cannot be called on $orders (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
40
        ) {
41
            $offset = $offset + $count;
42
            foreach ($orders as $order) {
0 ignored issues
show
The expression $orders of type boolean is not traversable.
Loading history...
43
                if ($order->IsSubmitted()) {
44
                    $memberIsOK = false;
45
                    if (!$order->MemberID) {
46
                        $memberIsOK = true;
47
                    } elseif (!$order->Member()) {
48
                        $memberIsOK = true;
49
                    } elseif ($member = $order->Member()) {
50
                        $memberIsOK = true;
51
                        if ($member->IsShopAssistant()) {
52
                            $memberIsOK = false;
53
                        }
54
                    }
55
                    if ($memberIsOK) {
56
                        $items = OrderItem::get()->filter(array('OrderID' => $order->ID));
57
                        if ($items && $items->count()) {
58
                            $fileData .= $this->generateExportFileData($order->getOrderEmail(), $order->SubmissionLog()->Created, $items);
59
                        }
60
                    }
61
                }
62
            }
63
            unset($orders);
64
        }
65
        if ($fileData) {
66
            SS_HTTPRequest::send_file($fileData, $fileName, 'text/csv');
67
        } else {
68
            user_error('No records found', E_USER_ERROR);
69
        }
70
    }
71
72
    public function generateExportFileData($email, $date, $orderItems)
73
    {
74
        $separator = ',';
75
        $fileData = '';
76
        $columnData = array();
0 ignored issues
show
$columnData is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
77
        $exportFields = array(
78
            'OrderID',
79
            'InternalItemID',
80
            'TableTitle',
81
            'TableSubTitleNOHTML',
82
            'UnitPrice',
83
            'Quantity',
84
            'CalculatedTotal',
85
        );
86
87
        if ($orderItems) {
88
            foreach ($orderItems as $item) {
89
                $columnData = array();
90
                $columnData[] = '"'.$email.'"';
91
                $columnData[] = '"'.$date.'"';
92
                foreach ($exportFields as $field) {
93
                    $value = $item->$field;
94
                    $value = preg_replace('/\s+/', ' ', $value);
95
                    $value = str_replace(array("\r", "\n"), "\n", $value);
96
                    $tmpColumnData = '"'.str_replace('"', '\"', $value).'"';
97
                    $columnData[] = $tmpColumnData;
98
                }
99
                $fileData .= implode($separator, $columnData);
100
                $fileData .= "\n";
101
                $item->destroy();
102
                unset($item);
103
                unset($columnData);
104
            }
105
106
            return $fileData;
107
        } else {
108
            return '';
109
        }
110
    }
111
}
112