-
Notifications
You must be signed in to change notification settings - Fork 0
/
security-practices
70 lines (52 loc) · 4.87 KB
/
security-practices
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
As a Frontend Engineer, you play a key role in implementing security measures that protect users from various vulnerabilities. Here’s a list of top security measures that you can take to secure a web application from the frontend:
1. Enforce HTTPS
Ensure that the website always loads using HTTPS. This ensures that data between the browser and the server is encrypted and protects users from man-in-the-middle (MITM) attacks.
Use HSTS (HTTP Strict Transport Security) headers to force browsers to only communicate via HTTPS.
2. Input Validation and Sanitization
Sanitize and validate all user inputs to prevent attacks like Cross-Site Scripting (XSS) and SQL Injection.
Avoid using dangerouslySetInnerHTML in React unless the content is properly sanitized with libraries like DOMPurify.
3. Prevent Cross-Site Scripting (XSS)
Ensure that user-generated content (like form inputs, comments, or user data) is sanitized before rendering on the page to avoid XSS attacks.
Use proper encoding/escaping of data when rendering on the DOM.
4. Use Content Security Policy (CSP)
Implement a Content Security Policy (CSP) header to restrict the sources of content that can be loaded in your app (like scripts, images, styles).
CSP helps prevent XSS by limiting the external sources from which the browser can load assets.
5. Secure Cookie Handling
Mark cookies with HttpOnly, Secure, and SameSite attributes to ensure they are not accessible via JavaScript and are only transmitted over HTTPS.
SameSite cookies can help mitigate Cross-Site Request Forgery (CSRF) attacks.
6. Cross-Site Request Forgery (CSRF) Protection
Implement CSRF tokens to ensure that only authorized requests are processed by the backend.
Even though CSRF prevention is generally done on the backend, frontend engineers must make sure the CSRF tokens are included in requests (e.g., via form submission or AJAX calls).
7. Avoid Storing Sensitive Data in Local Storage
Never store sensitive data like authentication tokens, passwords, or personal data in local storage or session storage. Instead, store tokens in secure HTTP-only cookies.
Data stored in local storage can be easily accessed by malicious scripts in case of an XSS attack.
8. Handle Errors Gracefully
Do not expose sensitive information in error messages (like stack traces or internal implementation details) to the user or client-side logs.
Handle errors on the frontend in a user-friendly way without exposing vulnerabilities.
9. Use Strong Authentication Methods
Ensure that the app supports strong authentication mechanisms like OAuth2, JWT tokens, or SAML.
Ensure proper handling of login forms, ensuring no credentials are transmitted over HTTP, and token-based authentication is done securely.
10. Third-Party Dependencies and Libraries Security
Regularly audit and update your dependencies and libraries. Frontend frameworks and libraries may have known vulnerabilities that can be exploited if they are not kept up to date.
Use tools like npm audit or Snyk to check for vulnerabilities in your dependencies.
11. Implement Rate Limiting on Frontend
Even though rate limiting is mostly done on the backend, you can implement basic rate limiting on the frontend (for actions like login attempts or API calls) to prevent abuse by bots or attackers.
12. Avoid Exposing API Keys or Sensitive Data
Ensure that API keys, secrets, or sensitive data are never exposed in frontend code. Use environment variables and secure configurations instead.
Public API keys should be obfuscated, and sensitive backend operations should be handled server-side.
13. Implement Secure Caching Strategies
Use caching headers (like Cache-Control) effectively to ensure that sensitive information is not cached in the browser or intermediary proxies.
Do not cache pages that include personal or sensitive information.
14. Content Spoofing Prevention
Ensure proper validation and sanitization of URLs, images, or any user-generated content that could be used for content spoofing (for example, URLs in phishing attacks).
15. Enable Security-Related HTTP Headers
Ensure the backend sets important security-related HTTP headers that protect against common threats like:
X-Frame-Options to prevent clickjacking.
X-Content-Type-Options to avoid MIME type sniffing.
Strict-Transport-Security (HSTS) to enforce HTTPS.
16. Limit Client-Side Debugging in Production
Disable console.log, debugger, and detailed error messages in the production environment to avoid leaking sensitive information to attackers.
17. Session Management
Ensure proper session management on the frontend, such as logging out users after a period of inactivity and revoking access tokens after session expiration.
18. Monitor for Security Issues
Set up monitoring for errors and security issues in the frontend using tools like Sentry or New Relic to detect suspicious activity.