You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 19, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: guides/v2.1/ext-best-practices/extension-coding/security-performance-data-bp.md
+61-24Lines changed: 61 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,44 +10,81 @@ functional_areas:
10
10
11
11
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.
12
12
13
-
### Avoid using low-level functionality
13
+
## Avoid using low-level functionality
14
+
14
15
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.
15
16
16
17
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"}.
17
18
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:
. 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
24
57
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.
27
59
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.
30
61
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
33
63
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.
36
65
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
38
67
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.
40
71
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
43
73
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.
### 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
+
}
49
83
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.
### 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.
0 commit comments