#7983: feat(security): add secure coding guidelines to system prompt
docs
agents
stale
Cluster:
Security Enhancements and Fixes
## 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
#10514: Security: harden AGENTS.md with gateway, prompt injection, and supp...
by catpilothq · 2026-02-06
87.9%
#5922: fix(security): add instruction confidentiality directive to system ...
by dan-redcupit · 2026-02-01
84.8%
#8086: feat(security): Add prompt injection guard rail
by bobbythelobster · 2026-02-03
81.5%
#21291: feat: Add data plane security to default system prompt
by joetomasone · 2026-02-19
80.5%
#8821: Security: Holistic capability-based sandbox (replaces pattern-match...
by tonioloewald · 2026-02-04
78.9%
#15757: feat(security): add hardening gap audit checks
by saurabhsh5 · 2026-02-13
78.5%
#21055: security(cli): gate systemPromptReport behind --debug flag
by richvincent · 2026-02-19
78.4%
#14222: core: add needsApproval to before_tool_call; move AgentShield to ex...
by Eventedge · 2026-02-11
78.0%
#7346: Security: add hardening module and secure-bot extension
by AlphonseC · 2026-02-02
77.7%
#8197: [AI-Assisted] feat: Add "Hardball" Security Framework (MFA-protecte...
by rodgui · 2026-02-03
77.6%