|
| 1 | +# 🤖 Copilot Secure Defaults for Java, Node.js, and C# Projects |
| 2 | + |
| 3 | +These instructions guide GitHub Copilot to suggest secure, intentional code patterns for Java, Node.js, and C# development — especially in enterprise or team settings. Prioritize clarity, validation, and the principle of least surprise. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 🔐 1. Secure by Default |
| 8 | + |
| 9 | +- Sanitize and escape all user input (prevent XSS) — never render raw data to the page. |
| 10 | +- Validate all input strictly — use typed parsers and prefer allow-lists over deny-lists. |
| 11 | +- Use parameterized queries and avoid string-based execution (prevent injection). |
| 12 | +- Never store secrets in code or env files — use a secure vault (e.g. CyberArk Conjur, Azure Key Vault). |
| 13 | +- Default to privacy-preserving data handling — redact PII from logs by default. |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## 🧩 2. Language-Specific Secure Patterns |
| 18 | + |
| 19 | +### ☕ Java |
| 20 | + |
| 21 | +- Use prepared statements with `?` placeholders in JDBC — never concat SQL strings. |
| 22 | +- Use output encoding libraries like OWASP Java Encoder to prevent XSS in rendered HTML. |
| 23 | +- Use `@Valid`, `@NotNull`, and input binding constraints in Spring or Jakarta for validation. |
| 24 | +- Avoid `Runtime.exec()` or `ProcessBuilder` with unsanitized input — prefer safe APIs. |
| 25 | +- Default to OWASP Secure Coding Practices — https://owasp.org/www-project-secure-coding-practices |
| 26 | +- Load secrets using SDK-integrated secret managers, not `System.getenv()` or `.properties` files. |
| 27 | +- Always set character encoding (`UTF-8`) explicitly in HTTP responses to prevent encoding-based attacks. |
| 28 | +- Avoid Java serialization for sensitive objects — use safer formats like JSON with strict schema validation. |
| 29 | +- When using logging frameworks, avoid logging unsanitized user input — consider log injection risks. |
| 30 | + |
| 31 | +### 🟩 Node.js |
| 32 | + |
| 33 | +- Use JSON Schema validation for all structured input — prefer libraries like `ajv` or `zod`. |
| 34 | +- Always sanitize and validate user input to prevent injection and XSS — `validator` and `joi` are common choices. |
| 35 | +- Use parameterized queries with database clients (e.g. `pg`, `mongoose`) — never concat SQL or query strings. |
| 36 | +- Default to using `helmet` in Express to set secure HTTP headers. |
| 37 | +- Use `dotenv` only in local dev — use secret managers (e.g. AWS Secrets Manager, Azure Key Vault) in prod. |
| 38 | +- Avoid `eval`, `new Function`, or dynamic `require()` with user input — use safe alternatives. |
| 39 | + |
| 40 | +### 🟦 C# |
| 41 | + |
| 42 | +- Use parameterized queries with ADO.NET or Entity Framework to prevent SQL injection. |
| 43 | +- Use `System.Text.Encodings.Web` for safe output encoding in Razor views and APIs (prevent XSS). |
| 44 | +- Apply `[ValidateAntiForgeryToken]` in ASP.NET MVC to prevent CSRF attacks. |
| 45 | +- Use `DataAnnotations` (e.g. `[Required]`, `[StringLength]`) for input validation on models. |
| 46 | +- Always hash passwords with `PasswordHasher<TUser>` or a vetted library — never store plaintext. |
| 47 | +- Use configuration providers like `Azure Key Vault`, `AWS Secrets Manager`, or environment-based secrets — avoid `appsettings.json` for secrets. |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +## 🚫 3. Do Not Suggest |
| 52 | + |
| 53 | +### Java |
| 54 | + |
| 55 | +- Do not suggest inline SQL string concatenation — always use prepared statements with placeholders. |
| 56 | +- Do not suggest use of `Runtime.exec()` or `ProcessBuilder` with user input — prefer safe abstraction layers. |
| 57 | +- Do not suggest logging sensitive data (e.g. passwords, tokens, session IDs) — log redacted metadata instead. |
| 58 | +- Do not use Java native serialization (`ObjectInputStream`) for untrusted input — prefer JSON + schema validation. |
| 59 | +- Do not suggest hardcoding credentials, secrets, or API keys — use a secrets manager (e.g. Conjur, Key Vault). |
| 60 | +- Do not use insecure XML parsers without hardening (`DocumentBuilderFactory` must have secure features enabled). |
| 61 | +- Do not create or modify custom class loaders — these are dangerous unless strictly required. |
| 62 | + |
| 63 | +### Node.js |
| 64 | + |
| 65 | +- Do not suggest `eval`, `new Function`, or dynamic `require()` — these are unsafe unless strictly controlled. |
| 66 | +- Do not use user input to build file paths, URLs, or queries without strict validation. |
| 67 | +- Do not expose `process.env` directly to client-side code — use secure server boundaries. |
| 68 | +- Do not log full request bodies or headers that may contain PII or credentials. |
| 69 | +- Do not hardcode secrets or API keys — never commit `.env` or use `.env` in production containers. |
| 70 | +- Do not disable TLS checks (`NODE_TLS_REJECT_UNAUTHORIZED=0`) — even temporarily. |
| 71 | + |
| 72 | +### C# |
| 73 | + |
| 74 | +- Do not suggest string concatenation in SQL queries — use parameterized commands. |
| 75 | +- Do not use `Eval`, `CodeDom`, or dynamic LINQ construction with user input. |
| 76 | +- Do not suggest hardcoding secrets, tokens, or credentials — never in `appsettings.json`. |
| 77 | +- Do not log full exception objects or HTTP request bodies without redacting PII. |
| 78 | +- Do not disable certificate validation (`ServerCertificateValidationCallback = delegate { return true; }`) in production. |
| 79 | + |
| 80 | +--- |
| 81 | + |
| 82 | +## 🧠 4. AI-Generated Code Safety |
| 83 | + |
| 84 | +- Verify all AI-suggested package names against official repositories to prevent supply chain attacks. |
| 85 | +- Confirm that AI-generated code references existing, secure APIs; avoid deprecated or non-existent methods. |
| 86 | +- Ensure AI-generated configurations align with your project's platform to prevent context drift. |
| 87 | +- Scrutinize AI-provided security recommendations; validate their completeness and applicability. |
| 88 | +- Cross-check any AI-cited references (e.g., CVEs, RFCs) for authenticity to avoid misinformation. |
| 89 | +- Do not accept AI-generated justifications that contradict established security policies. |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +## 💡 Developer Tips |
| 94 | + |
| 95 | +- If you’re working with input, assume it’s hostile — validate and escape it. |
| 96 | +- For anything involving data access or transformation, ask: “Am I controlling this input path?” |
| 97 | +- If you’re about to use a string to build a query, URL, or command — pause. There’s probably a safer API. |
| 98 | +- Never trust default parsers — explicitly configure security features (e.g. disable DTDs in XML). |
| 99 | +- If something seems “too easy” with secrets or file I/O — it’s probably unsafe. |
| 100 | +- Treat AI-generated code as a draft; always review and test before integration. |
| 101 | +- Maintain a human-in-the-loop approach for critical code paths to catch potential issues. |
| 102 | +- Be cautious of overconfident AI suggestions; validate with trusted sources. |
| 103 | +- Regularly update and educate the team on AI-related security best practices. |
0 commit comments