Magento 2 Security
Before writing code
Fetch live docs:
- Web-search
site:experienceleague.adobe.com commerce security for security best practices
- Web-search
site:developer.adobe.com commerce php development security for developer security guide
- Web-search
magento 2 security patches latest for recent security updates
Content Security Policy (CSP)
What It Does
Protects against XSS and code injection by restricting which resources (scripts, styles, images, fonts) can load.
Configuration
etc/csp_whitelist.xml — whitelist external domains per CSP directive
- Modes: report-only (logs violations) and restrict (blocks violations)
- Directives:
script-src, style-src, img-src, font-src, connect-src, frame-src
Adding Allowed Sources
Whitelist third-party domains for payment gateways, analytics, CDNs:
- Declare in
csp_whitelist.xml under the appropriate directive
- Use
report-only mode first to identify missing whitelists
Two-Factor Authentication (2FA)
- Mandatory for all admin users since Magento 2.4.0
- Supported providers: Google Authenticator, Duo Security, Authy, U2F keys
- Rate limiting on OTP validation (configurable retry limit and lockout)
- Cannot be disabled in production (security requirement)
CSRF Protection
form_key — 16-character token included in all admin forms
- Validated on every POST request in admin
- SameSite cookie attribute prevents cross-site request forgery
- Admin Secret Key in URLs adds additional protection
Admin Security Configuration
Available at Stores > Settings > Configuration > Advanced > Admin > Security:
- Custom admin URL path (obscure the
/admin path)
- Add Secret Key to URLs
- Password lifetime (force periodic changes)
- Max login failures before lockout
- Lockout duration
- Session lifetime
- Allowed countries for admin access
Input Validation and Output Escaping
Input Validation
- Validate all user input on the server side
- Use Magento's validation classes and form validators
- Never trust client-side validation alone
- Validate types, lengths, formats, and allowed values
Output Escaping (XSS Prevention)
In PHTML templates, always escape output:
$escaper->escapeHtml($value) — HTML context
$escaper->escapeUrl($url) — URL context
$escaper->escapeJs($value) — JavaScript context
$escaper->escapeHtmlAttr($value) — HTML attribute context
$escaper->escapeCss($value) — CSS context
- Never use
echo $value directly in templates
reCAPTCHA
- Native Google reCAPTCHA v2/v3 support since 2.3
- Configurable per form: login, registration, forgot password, checkout, contact
- Admin configuration at Stores > Configuration > Security > reCAPTCHA
API Security
- Bearer token authentication for REST/SOAP
- ACL-based authorization for all endpoints
- Rate limiting on authentication endpoints
- OAuth 1.0a for third-party integrations
Best Practices
- Apply security patches promptly — subscribe to Adobe Security Bulletins
- Use a custom admin URL (not
/admin)
- Enable 2FA for all admin accounts
- Set strong password policies (length, complexity, expiry)
- Use HTTPS everywhere (frontend + admin)
- Restrict admin access by IP where possible
- Enable CSP in restrict mode (not just report-only)
- Escape all output in templates
- Keep Magento and all extensions up to date
- Run periodic security scans (Adobe Security Scan Tool)
- Review third-party extensions for security before installing
Fetch the security documentation for current CSP directives, 2FA configuration options, and latest security patches before implementing.
1---2name: magento-security3description: Implement Magento 2 security — CSP, 2FA, CSRF protection, ACL, admin security configuration, input validation, and security best practices. Use when hardening a Magento installation or reviewing security posture.4---5
6# Magento 2 Security
7
8## Before writing code
9
10**Fetch live docs**:
111. Web-search `site:experienceleague.adobe.com commerce security` for security best practices
122. Web-search `site:developer.adobe.com commerce php development security` for developer security guide
133. Web-search `magento 2 security patches latest` for recent security updates
14
15## Content Security Policy (CSP)
16
17### What It Does
18
19Protects against XSS and code injection by restricting which resources (scripts, styles, images, fonts) can load.
20
21### Configuration
22
23- `etc/csp_whitelist.xml` — whitelist external domains per CSP directive
24- Modes: **report-only** (logs violations) and **restrict** (blocks violations)
25- Directives: `script-src`, `style-src`, `img-src`, `font-src`, `connect-src`, `frame-src`
26
27### Adding Allowed Sources
28
29Whitelist third-party domains for payment gateways, analytics, CDNs:
30- Declare in `csp_whitelist.xml` under the appropriate directive
31- Use `report-only` mode first to identify missing whitelists
32
33## Two-Factor Authentication (2FA)
34
35- **Mandatory** for all admin users since Magento 2.4.0
36- Supported providers: Google Authenticator, Duo Security, Authy, U2F keys
37- Rate limiting on OTP validation (configurable retry limit and lockout)
38- Cannot be disabled in production (security requirement)
39
40## CSRF Protection
41
42- `form_key` — 16-character token included in all admin forms
43- Validated on every POST request in admin
44- **SameSite** cookie attribute prevents cross-site request forgery
45- Admin Secret Key in URLs adds additional protection
46
47## Admin Security Configuration
48
49Available at Stores > Settings > Configuration > Advanced > Admin > Security:
50- Custom admin URL path (obscure the `/admin` path)
51- Add Secret Key to URLs
52- Password lifetime (force periodic changes)
53- Max login failures before lockout
54- Lockout duration
55- Session lifetime
56- Allowed countries for admin access
57
58## Input Validation and Output Escaping
59
60### Input Validation
61
62- Validate all user input on the server side
63- Use Magento's validation classes and form validators
64- Never trust client-side validation alone
65- Validate types, lengths, formats, and allowed values
66
67### Output Escaping (XSS Prevention)
68
69In PHTML templates, always escape output:
70- `$escaper->escapeHtml($value)` — HTML context
71- `$escaper->escapeUrl($url)` — URL context
72- `$escaper->escapeJs($value)` — JavaScript context
73- `$escaper->escapeHtmlAttr($value)` — HTML attribute context
74- `$escaper->escapeCss($value)` — CSS context
75- Never use `echo $value` directly in templates
76
77## reCAPTCHA
78
79- Native Google reCAPTCHA v2/v3 support since 2.3
80- Configurable per form: login, registration, forgot password, checkout, contact
81- Admin configuration at Stores > Configuration > Security > reCAPTCHA
82
83## API Security
84
85- Bearer token authentication for REST/SOAP
86- ACL-based authorization for all endpoints
87- Rate limiting on authentication endpoints
88- OAuth 1.0a for third-party integrations
89
90## Best Practices
91
92- Apply security patches promptly — subscribe to Adobe Security Bulletins
93- Use a custom admin URL (not `/admin`)
94- Enable 2FA for all admin accounts
95- Set strong password policies (length, complexity, expiry)
96- Use HTTPS everywhere (frontend + admin)
97- Restrict admin access by IP where possible
98- Enable CSP in restrict mode (not just report-only)
99- Escape all output in templates
100- Keep Magento and all extensions up to date
101- Run periodic security scans (Adobe Security Scan Tool)
102- Review third-party extensions for security before installing
103
104Fetch the security documentation for current CSP directives, 2FA configuration options, and latest security patches before implementing.