Building Secure Python Applications
Best practices for writing secure Python code to protect against common vulnerabilities and ensure data integrity.
0 likes
8 views
Rule Content
# Title: Building Secure Python Applications # Description: Best practices for writing secure Python code to protect against common vulnerabilities and ensure data integrity. # Category: Python Cursor Rules ## Secure Coding Standards - **Avoid Hardcoding Sensitive Information**: Store API keys and credentials in environment variables or configuration files excluded from version control. - **Use Virtual Environments**: Isolate project dependencies using virtual environments to prevent conflicts and enhance security. - **Input Validation and Sanitization**: Validate and sanitize all user inputs to prevent injection attacks and ensure data integrity. - **Implement Least Privilege Principle**: Assign the minimum necessary permissions to processes and users to reduce security risks. - **Secure Dependency Management**: Regularly update dependencies and monitor for known vulnerabilities using tools like `pip-audit` or `safety`. - **Error Handling and Logging**: Implement comprehensive error handling and logging without exposing sensitive information. - **Avoid Dangerous Functions**: Refrain from using functions like `eval()`, `exec()`, and `os.system()` due to security risks. - **Use Secure Libraries**: Prefer libraries with a strong security track record and avoid deprecated or unmaintained packages. - **Secure Configuration Management**: Ensure configuration files are secure and do not contain sensitive information. - **Regular Security Audits**: Conduct periodic security audits and code reviews to identify and mitigate potential vulnerabilities. ## Tools and Plugins - **Flake8 Secure Coding Standard Plugin**: Integrate the `flake8-secure-coding-standard` plugin to enforce secure coding practices. - **Pylint Secure Coding Standard Plugin**: Use the `pylint-secure-coding-standard` plugin to identify security issues in the codebase. - **Bandit**: Incorporate Bandit into the development workflow to detect common security issues in Python code. ## Documentation - **Comprehensive Documentation**: Maintain up-to-date documentation for all modules, functions, and classes, including security considerations. - **Docstrings**: Use docstrings to describe the purpose and usage of code components, adhering to PEP 257 conventions. ## Testing - **Security Testing**: Implement security-focused tests to identify vulnerabilities and ensure the application handles malicious inputs gracefully. - **Automated Testing**: Utilize automated testing frameworks to maintain code quality and detect issues early in the development cycle. ## Continuous Integration/Continuous Deployment (CI/CD) - **Integrate Security Checks**: Incorporate security linters and vulnerability scanners into the CI/CD pipeline to catch issues before deployment. - **Environment Configuration**: Ensure that CI/CD environments are securely configured and do not expose sensitive information. ## Compliance - **Adhere to Standards**: Follow industry standards and guidelines such as OWASP Top 10 and CWE Top 25 to mitigate common security risks. - **Regular Training**: Provide ongoing security training for developers to stay informed about emerging threats and best practices.