← Back to PRs

#7983: feat(security): add secure coding guidelines to system prompt

by TGambit65 open 2026-02-03 12:16 View on GitHub →
docs agents stale
## Summary Adds automatic security best practices for AI-generated code. When OpenClaw agents write or modify code, these guidelines are included in the system prompt to encourage secure coding practices. ## 🤖 AI Assistance **Built with:** OpenClaw + Codex CLI (gpt-5.2-codex) **Testing level:** ✅ Fully tested (pnpm check passes) **I understand what this code does:** Yes - injects security guidelines into agent system prompts when coding tools are available. --- ## What's Included ### Security Guidelines When the agent generates code, it's reminded to: **Credentials & Secrets:** - Never hardcode API keys, tokens, or passwords - Use environment variables or secret management - Set file permissions to 0600 for credential files **Input Validation:** - Validate and sanitize all user inputs - Use parameterized queries for SQL - Escape output to prevent XSS **Dependencies:** - Prefer well-maintained packages - Pin dependency versions - Run security audits before committing **File Operations:** - Validate file paths to prevent traversal attacks - Use restrictive permissions - Prefer recoverable deletions **Error Handling:** - Never expose stack traces to end users - Log errors without sensitive data - Fail securely (deny by default) **Pre-Commit Checks:** - Review diffs for secret exposure - Remove debug code and backdoors ## Configuration Enabled by default when coding tools (write, edit, exec, apply_patch) are available. To disable: ```json5 { agents: { defaults: { secureCodingGuidelines: false } } } ``` ## Files Changed - **NEW:** `src/agents/system-prompt-secure-coding.ts` — Build function for guidelines section - **NEW:** `docs/concepts/secure-coding.md` — Documentation - `src/agents/system-prompt.ts` — Integrate new section after Safety - `src/config/types.agent-defaults.ts` — Add `secureCodingGuidelines` config option ## Why This Matters Discussion #7606 (ClawHavoc) showed how malicious code can compromise systems. While that focused on skill security, this PR addresses the other side: ensuring agents themselves write secure code. This complements PR #7953 (credential encryption) by: 1. **PR #7953**: Protects OpenClaw's own credentials 2. **This PR**: Ensures agents write secure code for users --- Co-authored-by: Kelly Gibbons <thoderstreams@gmail.com> <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR adds a new “Secure Coding Practices” section that can be injected into the agent system prompt when code-modifying tools (write/edit/exec/apply_patch) are available. The prompt assembly in `src/agents/system-prompt.ts` now calls `buildSecureCodingSection` right after the existing Safety section, and a new config field `secureCodingGuidelines?: boolean` is added to the agent defaults type. Key issue: the new config knob is not currently wired to the prompt builder. The runtime control path uses `secureCodingEnabled` as a parameter to `buildAgentSystemPrompt`, but there are no references mapping `AgentDefaultsConfig.secureCodingGuidelines` into that parameter, so the documented `agents.defaults.secureCodingGuidelines: false` toggle won’t take effect as-is. <h3>Confidence Score: 3/5</h3> - Mostly safe to merge, but the advertised config toggle appears ineffective without additional plumbing. - The prompt-section injection itself is straightforward and gated by tool availability, but the PR introduces a user-facing config field and docs that currently don’t match the implemented control path (`secureCodingGuidelines` vs `secureCodingEnabled`). That mismatch will likely cause confusion and broken configuration behavior. - src/config/types.agent-defaults.ts, src/agents/system-prompt.ts, docs/concepts/secure-coding.md <!-- greptile_other_comments_section --> <sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub> <!-- /greptile_comment -->

Most Similar PRs