Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 399b7de

Browse files
authored
Merge branch 'master' into patch-1
2 parents a28aeb4 + 98fe910 commit 399b7de

File tree

1 file changed

+61
-24
lines changed

1 file changed

+61
-24
lines changed

guides/v2.1/ext-best-practices/extension-coding/security-performance-data-bp.md

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,81 @@ functional_areas:
1010

1111
You should make sure that your [extension](https://glossary.magento.com/extension) handles data with care in order to prevent sensitive information from being exposed. Incorrect handling of data requests or class usage can negatively impact your extension and create security vulnerabilities. Consider applying the following best practices to your extension to improve performance and security.
1212

13-
### Avoid using low-level functionality
13+
## Avoid using low-level functionality
14+
1415
The Magento application is made up of a variety of components that work together to perform different business functions. We discourage the use of low-level functionality such as the [PHP](https://glossary.magento.com/php) `curl_*` functions and encourage the use of high-level components such as [`\Magento\Framework\HTTP\Adapter\Curl`]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/HTTP/Adapter/Curl.php). The use of low-level functionality can make Magento behave in unexpected ways that effectively disable built-in protection mechanisms, introduce exploitable inconsistencies, or otherwise expose the application to attack.
1516

1617
For a list of discouraged low-level functions, review the [`Magento2/Sniffs/Functions/DiscouragedFunctionSniff.php`](https://github.com/magento/magento-coding-standard/blob/develop/Magento2/Sniffs/Functions/DiscouragedFunctionSniff.php){:target="_blank"} file and the [Magento Coding Standard](https://github.com/magento/magento-coding-standard){:target="_blank"}.
1718

18-
### Use wrappers instead of superglobal variables
19-
Make sure that your Magento application does not directly use any PHP superglobals such as:
20-
```
19+
## Use wrappers instead of superglobal variables
20+
21+
Make sure that your Magento application does not directly use any PHP superglobals such as:
22+
23+
```php
2124
$GLOBALS, $_SERVER, $_GET, $_POST, $_FILES, $_COOKIE, $_SESSION, $_REQUEST, $_ENV
2225
```
23-
. Instead use the [`Magento\Framework\HTTP\PhpEnvironment\Request`]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/HTTP/PhpEnvironment/Request.php){:target="_blank"} wrapper class to safely access these values.
26+
Instead use the [`Magento\Framework\HTTP\PhpEnvironment\Request`]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/HTTP/PhpEnvironment/Request.php){:target="_blank"} wrapper class to safely access these values.
27+
28+
## Use the correct MySQL data types
29+
30+
MySQL offers a range of numeric, string, and time data types. If you are storing a date, use a DATE or DATETIME field. Using an INTEGER or STRING can make SQL queries more complicated, if not impossible. It is often tempting to invent your own data formats; for example, storing serialized PHP objects in string. Database management may be easier, but MySQL will become a dumb data store and it may lead to problems later.
31+
32+
## Get the correct data from the correct object
33+
34+
Be sure to retrieve data from the correct object. For example, get [$1](https://glossary.magento.com/$1) data from the Product instead of the Order object.
35+
36+
## Avoid raw SQL queries
37+
38+
Raw SQL queries can lead to potential security vulnerabilities and database portability issues. Use data adapter capabilities ([`Magento\Framework\DB\Adapter\Pdo\Mysql`]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php){:target="_blank"} by default) to build and execute queries and move all data access code to a resource model. Use prepared statements to make sure that queries are safe to execute.
39+
40+
## Use well-defined indexes
41+
42+
Foreign keys should have indexes. If you are using a field in a WHERE clause of an SQL query you should have an index on it. Such indexes should cover multiple columns based on the queries needed. As a general rule of thumb, indexes should be applied to any column named in the WHERE clause of a SELECT query.
43+
44+
For example, assume we have a user table with a numeric ID (the primary key) and an email address. During log on, MySQL must locate the correct ID by searching for an email. With an index, MySQL can use a fast search algorithm to locate the email almost instantly. Without an index, MySQL must check every record in sequence until the address is found.
45+
46+
It is tempting to add indexes to every column, however, they are regenerated during every table INSERT or UPDATE. That can hit performance; only add indexes when necessary.
47+
48+
## Avoid using global events
49+
50+
Only on rare occasions would it be necessary to use a global [event](https://glossary.magento.com/event). You should use [frontend](https://glossary.magento.com/frontend) or [adminhtml](https://glossary.magento.com/adminhtml) to narrow the scope instead.
51+
52+
## Use Magento data collections
53+
54+
Execution of a SQL query is one of the most resource-taxing operations. Running SQL queries in a loop often results in a performance bottleneck. To load the EAV model, several heavy queries are required to execute. As the number of executed queries is multiplied with the number of categories, the result is extremely inefficient and slow code. Instead of loading models in a loop, Magento data collections can help to load a set of models in a very efficient manner.
55+
56+
## Validate input and properly encode or escape output
2457

25-
### Use the correct MySQL data types
26-
MySQL offers a range of numeric, string, and time data types. If you are storing a date, use a DATE or DATETIME field. Using an INTEGER or STRING can make SQL queries more complicated, if not impossible. It is often tempting to invent your own data formats; for example, storing serialized PHP objects in string. Database management may be easier, but MySQL will become a dumb data store and it may lead to problems later.
58+
Remember to always validate data from non-trusted data sources. Sanitizing data coming into your extension and produced by it will improve overall security.
2759

28-
### Get the correct data from the correct object
29-
Be sure to retrieve data from the correct object. For example, get [$1](https://glossary.magento.com/$1) data from the Product instead of the Order object.
60+
For example, to prevent XSS vulnerability, avoid creating methods that output non-validated user-supplied data without proper [HTML](https://glossary.magento.com/html) encoding.
3061

31-
### Avoid raw SQL queries
32-
Raw SQL queries can lead to potential security vulnerabilities and database portability issues. Use data adapter capabilities ([`Magento\Framework\DB\Adapter\Pdo\Mysql`]({{ site.mage2bloburl }}/{{ page.guide_version }}/lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php){:target="_blank"} by default) to build and execute queries and move all data access code to a resource model. Use prepared statements to make sure that queries are safe to execute.
62+
## Always encrypt sensitive data or configurations
3363

34-
### Use well-defined indexes
35-
Foreign keys should have indexes. If you're using a field in a WHERE clause of an SQL query you should have an index on it. Such indexes should cover multiple columns based on the queries needed. As a general rule of thumb, indexes should be applied to any column named in the WHERE clause of a SELECT query.
64+
Never store sensitive information in clear text within a resource that might be accessible to another control sphere. This type of information should be encrypted or otherwise protected.
3665

37-
For example, assume we have a user table with a numeric ID (the primary key) and an email address. During log on, MySQL must locate the correct ID by searching for an email. With an index, MySQL can use a fast search algorithm to locate the email almost instantly. Without an index, MySQL must check every record in sequence until the address is found.
66+
## Avoid unnecessary logic execution
3867

39-
It's tempting to add indexes to every column, however, they are regenerated during every table INSERT or UPDATE. That can hit performance; only add indexes when necessary.
68+
Make sure that you never run code that will not be used in the next step.
69+
70+
Check the below example where we always get the `customerId` and `storeId`, but we are not going to use them.
4071

41-
### Avoid using global events
42-
Only on rare occasions would it be necessary to use a global [event](https://glossary.magento.com/event). You should use [frontend](https://glossary.magento.com/frontend) or [adminhtml](https://glossary.magento.com/adminhtml) to narrow the scope instead.
72+
### Example
4373

44-
### Use Magento data collections
45-
Execution of a SQL query is one of the most resource-taxing operations. Running SQL queries in a loop often results in a performance bottleneck. To load the EAV model, several heavy queries are required to execute. As the number of executed queries is multiplied with the number of categories, the result is extremely inefficient and slow code. Instead of loading models in a loop, Magento data collections can help to load a set of models in a very efficient manner.
74+
```php
75+
public function getCustomerCart()
76+
{
77+
$customerId = (int) $this->getSession()->getCustomerId();
78+
$storeId = (int) $this->getSession()->getStoreId();
4679

47-
### Validate input and properly encode or escape output
48-
Remember to always validate data from non-trusted data sources. Sanitizing data coming into your extension and produced by it will improve overall security.
80+
if ($this->_cart !== null) {
81+
return $this->_cart;
82+
}
4983

50-
For example, to prevent XSS vulnerability, avoid creating methods that output non-validated user-supplied data without proper [HTML](https://glossary.magento.com/html) encoding.
84+
...
85+
$this->_cart = $this->quoteRepository->getForCustomer($customerId, [$storeId]);
86+
...
5187

52-
### Always encrypt sensitive data or configurations
53-
Never store sensitive information in clear text within a resource that might be accessible to another control sphere. This type of information should be encrypted or otherwise protected.
88+
return $this->_cart;
89+
}
90+
```

0 commit comments

Comments
 (0)