Security-Focused Prompts

Security-Focused Prompts

Introduction

Security-focused prompts are specialized instructions designed to guide AI systems in generating code that adheres to security best practices and mitigates common vulnerabilities. This collection provides templates and patterns to ensure that AI-generated code is not only functional but also secure by design.

Core Security Principles

When crafting security-focused prompts, ensure they embody these fundamental security principles:

  1. Defense in Depth: Request multiple layers of security controls rather than relying on a single protection mechanism

  2. Least Privilege: Specify that code should operate with minimal necessary permissions and access

  3. Input Validation: Explicitly request comprehensive validation of all external inputs

  4. Secure Defaults: Require secure default configurations rather than optional security features

  5. Fail Securely: Instruct that error conditions should never compromise security

  6. Security by Design: Embed security requirements from the beginning rather than as an afterthought

Prompt Templates

General Security-Focused Template

SITUATION: [Describe the system, existing architecture, and security context]
CHALLENGE: Create [describe component] that prioritizes security while ensuring functionality
AUDIENCE: Development team with security requirements for [specific compliance/security standard]
FORMAT:
- Implement proper input validation for all external data
- Use parameterized queries for all database operations
- Apply the principle of least privilege throughout
- Include comprehensive error handling that doesn't leak sensitive information
- Add security-focused comments explaining protection mechanisms
FOUNDATIONS:
- Follow OWASP Top 10 mitigation strategies
- Implement proper authentication and authorization checks
- Validate all inputs with appropriate sanitization
- Use secure cryptographic practices where needed
- Handle sensitive data according to [relevant compliance standards]
- Include security-focused logging without exposing sensitive information

Authentication Component Template

Data Access Security Template

API Security Template

Frontend Security Template

Component-Specific Security Prompts

Secure File Upload Implementation

Payment Processing Security

OWASP Top 10 Mitigation Prompts

Injection Prevention

Broken Authentication Mitigation

Sensitive Data Exposure Prevention

XML External Entity Attack Prevention

Broken Access Control Mitigation

Best Practices for Security-Focused Prompts

Request Threat Modeling

Include threat modeling considerations in your prompts:

Specify Security Testing Requirements

Request built-in security testing guidance:

Request Security Documentation

Explicitly request security-focused documentation:

Specify Compliance Requirements

Include relevant compliance standards:

Evaluating Security Results

When evaluating AI-generated code for security, consider these questions:

  1. Input Validation: Is all external input properly validated and sanitized?

  2. Authentication: Are authentication mechanisms implemented securely?

  3. Authorization: Are proper access controls implemented and enforced?

  4. Data Protection: Is sensitive data properly protected at rest and in transit?

  5. Error Handling: Do errors fail securely without information disclosure?

  6. Logging: Is security-relevant information properly logged without sensitive data exposure?

  7. Dependencies: Are dependencies secure and properly configured?

Example: Before and After

Before: Insecure Authentication Implementation

After: Secure Authentication Implementation

Security Anti-Patterns to Avoid

When reviewing AI-generated code, watch for these common security issues:

  1. String Concatenation in Queries: Using string concatenation instead of parameterized queries

  2. Inadequate Input Validation: Missing or incomplete validation of external input

  3. Hardcoded Credentials: Embedding secrets or credentials in code

  4. Missing Access Controls: Failing to check authorization before actions

  5. Insecure Cryptography: Using outdated or weak cryptographic algorithms

  6. Verbose Error Messages: Exposing sensitive information in error responses

  7. Client-Side Security Only: Relying solely on client-side validation

Include specific requirements to avoid these in your prompts.

Conclusion

Security-focused prompts are essential for generating code that's secure by design rather than as an afterthought. By explicitly guiding AI tools to implement proper security controls, you reduce the risk of vulnerabilities in your applications.

Remember that security is an ongoing process. Even with security-focused prompts, always perform proper security testing and code review to ensure protection against evolving threats. By investing in security from the start through well-crafted prompts, you build more resilient and trustworthy systems.

Last updated