Skip to content

Commit cb025ae

Browse files
author
Ramraj Bishnoie
committed
feat: added support to generate language-rule mappings in SKILLS using the script
1 parent 5d8ea9f commit cb025ae

4 files changed

Lines changed: 168 additions & 6 deletions

File tree

rules/codeguard-SKILLS.md.template

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
name: software-security
3+
description: A software security skill that integrates with Project CodeGuard to help AI coding agents write secure code and prevent common vulnerabilities. Use this skill when writing, reviewing, or modifying code to ensure secure-by-default practices are followed.
4+
metadata:
5+
codeguard-version: "1.0.0"
6+
framework: "Project CodeGuard"
7+
purpose: "Embed secure-by-default practices into AI coding workflows"
8+
---
9+
10+
# Software Security Skill (Project CodeGuard)
11+
This skill provides comprehensive security guidance to help AI coding agents generate secure code and prevent common vulnerabilities. It is based on **Project CodeGuard**, an open-source, model-agnostic security framework that embeds secure-by-default practices into AI coding workflows.
12+
13+
## When to Use This Skill
14+
This skill should be activated when:
15+
- Writing new code in any language
16+
- Reviewing or modifying existing code
17+
- Implementing security-sensitive features (authentication, cryptography, data handling, etc.)
18+
- Working with user input, databases, APIs, or external services
19+
- Configuring cloud infrastructure, CI/CD pipelines, or containers
20+
- Handling sensitive data, credentials, or cryptographic operations
21+
22+
## How to Use This Skill
23+
When writing or reviewing code:
24+
1. Always-Apply Rules: Some rules MUST be checked on every code operation:
25+
- `codeguard-1-hardcoded-credentials.md` - Never hardcode secrets, passwords, API keys, or tokens
26+
- `codeguard-1-crypto-algorithms.md` - Use only modern, secure cryptographic algorithms
27+
- `codeguard-1-digital-certificates.md` - Validate and manage digital certificates securely
28+
- `codeguard-1-safe-c-functions.md` - Avoid unsafe C/C++ functions and use safe alternatives
29+
2. Context-Specific Rules: Apply rules from /rules directory based on the language of the feature being implemented using the table given below:
30+
<!-- LANGUAGE_MAPPINGS_START -->
31+
<!-- LANGUAGE_MAPPINGS_END -->
32+
3. Proactive Security: Don't just avoid vulnerabilities-actively implement secure patterns:
33+
- Use parameterized queries for database access
34+
- Validate and sanitize all user input
35+
- Apply least-privilege principles
36+
- Use modern cryptographic algorithms and libraries
37+
- Implement defense-in-depth strategies
38+
39+
## CodeGuard Security Rules
40+
The security rules are available in the `rules/` directory.
41+
42+
### Usage Workflow
43+
When generating or reviewing code, follow this workflow:
44+
45+
### 1. Initial Security Check
46+
Before writing any code:
47+
- Check: Will this handle credentials? → Apply codeguard-1-hardcoded-credentials
48+
- Check: What language am I using? → Identify applicable language-specific rules
49+
- Check: What security domains are involved? → Load relevant rule files
50+
51+
### 2. Code Generation
52+
While writing code:
53+
- Apply secure-by-default patterns from relevant Project CodeGuard rules
54+
- Add security-relevant comments explaining choices
55+
56+
### 3. Security Review
57+
After writing code:
58+
- Review against implementation checklists in each rule
59+
- Verify no hardcoded credentials or secrets
60+
- Validate that all the rules have been successfully followed when applicable.
61+
- Explain which security rules were applied
62+
- Highlight security features implemented

skills/software-security/SKILL.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,34 @@ When writing or reviewing code:
2626
- `codeguard-1-crypto-algorithms.md` - Use only modern, secure cryptographic algorithms
2727
- `codeguard-1-digital-certificates.md` - Validate and manage digital certificates securely
2828
- `codeguard-1-safe-c-functions.md` - Avoid unsafe C/C++ functions and use safe alternatives
29-
2. Context-Specific Rules: Apply rules based on the technology, language, or feature being implemented:
30-
- Review the relevant rule files in the `rules/` directory
31-
- Match rules to the specific languages and technologies in use
32-
- Follow the guidance, checklists, and examples provided
29+
2. Context-Specific Rules: Apply rules from /rules directory based on the language of the feature being implemented using the table given below:
30+
31+
32+
| Language | Rule Files to Apply |
33+
|----------|---------------------|
34+
| c | codeguard-0-additional-cryptography.md, codeguard-0-api-web-services.md, codeguard-0-authentication-mfa.md, codeguard-0-authorization-access-control.md, codeguard-0-client-side-web-security.md, codeguard-0-data-storage.md, codeguard-0-file-handling-and-uploads.md, codeguard-0-framework-and-languages.md, codeguard-0-iac-security.md, codeguard-0-input-validation-injection.md, codeguard-0-logging.md, codeguard-0-session-management-and-cookies.md, codeguard-0-xml-and-serialization.md |
35+
| d | codeguard-0-iac-security.md |
36+
| docker | codeguard-0-devops-ci-cd-containers.md, codeguard-0-supply-chain-security.md |
37+
| go | codeguard-0-additional-cryptography.md, codeguard-0-api-web-services.md, codeguard-0-authentication-mfa.md, codeguard-0-authorization-access-control.md, codeguard-0-file-handling-and-uploads.md, codeguard-0-input-validation-injection.md, codeguard-0-session-management-and-cookies.md, codeguard-0-xml-and-serialization.md |
38+
| html | codeguard-0-client-side-web-security.md, codeguard-0-input-validation-injection.md, codeguard-0-session-management-and-cookies.md |
39+
| java | codeguard-0-additional-cryptography.md, codeguard-0-api-web-services.md, codeguard-0-authentication-mfa.md, codeguard-0-authorization-access-control.md, codeguard-0-file-handling-and-uploads.md, codeguard-0-framework-and-languages.md, codeguard-0-input-validation-injection.md, codeguard-0-mobile-apps.md, codeguard-0-session-management-and-cookies.md, codeguard-0-xml-and-serialization.md |
40+
| javascript | codeguard-0-additional-cryptography.md, codeguard-0-api-web-services.md, codeguard-0-authentication-mfa.md, codeguard-0-authorization-access-control.md, codeguard-0-client-side-web-security.md, codeguard-0-cloud-orchestration-kubernetes.md, codeguard-0-data-storage.md, codeguard-0-devops-ci-cd-containers.md, codeguard-0-file-handling-and-uploads.md, codeguard-0-framework-and-languages.md, codeguard-0-iac-security.md, codeguard-0-input-validation-injection.md, codeguard-0-logging.md, codeguard-0-mobile-apps.md, codeguard-0-privacy-data-protection.md, codeguard-0-session-management-and-cookies.md, codeguard-0-supply-chain-security.md |
41+
| kotlin | codeguard-0-additional-cryptography.md, codeguard-0-authentication-mfa.md, codeguard-0-framework-and-languages.md, codeguard-0-mobile-apps.md |
42+
| matlab | codeguard-0-additional-cryptography.md, codeguard-0-authentication-mfa.md, codeguard-0-mobile-apps.md, codeguard-0-privacy-data-protection.md |
43+
| perl | codeguard-0-mobile-apps.md |
44+
| php | codeguard-0-additional-cryptography.md, codeguard-0-api-web-services.md, codeguard-0-authentication-mfa.md, codeguard-0-authorization-access-control.md, codeguard-0-client-side-web-security.md, codeguard-0-file-handling-and-uploads.md, codeguard-0-framework-and-languages.md, codeguard-0-input-validation-injection.md, codeguard-0-session-management-and-cookies.md, codeguard-0-xml-and-serialization.md |
45+
| powershell | codeguard-0-devops-ci-cd-containers.md, codeguard-0-iac-security.md, codeguard-0-input-validation-injection.md |
46+
| python | codeguard-0-additional-cryptography.md, codeguard-0-api-web-services.md, codeguard-0-authentication-mfa.md, codeguard-0-authorization-access-control.md, codeguard-0-file-handling-and-uploads.md, codeguard-0-framework-and-languages.md, codeguard-0-input-validation-injection.md, codeguard-0-session-management-and-cookies.md, codeguard-0-xml-and-serialization.md |
47+
| ruby | codeguard-0-additional-cryptography.md, codeguard-0-api-web-services.md, codeguard-0-authentication-mfa.md, codeguard-0-authorization-access-control.md, codeguard-0-file-handling-and-uploads.md, codeguard-0-framework-and-languages.md, codeguard-0-iac-security.md, codeguard-0-input-validation-injection.md, codeguard-0-session-management-and-cookies.md, codeguard-0-xml-and-serialization.md |
48+
| shell | codeguard-0-devops-ci-cd-containers.md, codeguard-0-iac-security.md, codeguard-0-input-validation-injection.md |
49+
| sql | codeguard-0-data-storage.md, codeguard-0-input-validation-injection.md |
50+
| swift | codeguard-0-additional-cryptography.md, codeguard-0-authentication-mfa.md, codeguard-0-mobile-apps.md |
51+
| typescript | codeguard-0-additional-cryptography.md, codeguard-0-api-web-services.md, codeguard-0-authentication-mfa.md, codeguard-0-authorization-access-control.md, codeguard-0-client-side-web-security.md, codeguard-0-file-handling-and-uploads.md, codeguard-0-framework-and-languages.md, codeguard-0-input-validation-injection.md, codeguard-0-session-management-and-cookies.md |
52+
| vlang | codeguard-0-client-side-web-security.md |
53+
| xml | codeguard-0-additional-cryptography.md, codeguard-0-api-web-services.md, codeguard-0-devops-ci-cd-containers.md, codeguard-0-framework-and-languages.md, codeguard-0-mobile-apps.md, codeguard-0-xml-and-serialization.md |
54+
| yaml | codeguard-0-additional-cryptography.md, codeguard-0-api-web-services.md, codeguard-0-authorization-access-control.md, codeguard-0-cloud-orchestration-kubernetes.md, codeguard-0-data-storage.md, codeguard-0-devops-ci-cd-containers.md, codeguard-0-framework-and-languages.md, codeguard-0-iac-security.md, codeguard-0-logging.md, codeguard-0-privacy-data-protection.md, codeguard-0-supply-chain-security.md |
55+
56+
3357
3. Proactive Security: Don't just avoid vulnerabilities-actively implement secure patterns:
3458
- Use parameterized queries for database access
3559
- Validate and sanitize all user input

src/converter.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class ConversionResult:
4444
filename: Original filename (e.g., 'my-rule.md')
4545
basename: Filename without extension (e.g., 'my-rule')
4646
outputs: Dictionary mapping format names to their outputs
47-
47+
languages: List of programming languages the rule applies to, empty list if always applies
4848
Example:
4949
result = ConversionResult(
5050
filename="my-rule.md",
@@ -55,13 +55,15 @@ class ConversionResult:
5555
extension=".mdc",
5656
subpath=".cursor/rules"
5757
)
58-
}
58+
},
59+
languages=["python", "javascript"]
5960
)
6061
"""
6162

6263
filename: str
6364
basename: str
6465
outputs: dict[str, FormatOutput]
66+
languages: list[str]
6567

6668

6769
class RuleConverter:
@@ -239,4 +241,5 @@ def convert(self, filepath: str) -> ConversionResult:
239241
filename=filename,
240242
basename=basename,
241243
outputs=outputs,
244+
languages=rule.languages,
242245
)

src/unified_to_all.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,58 @@
1010
"""
1111

1212
from pathlib import Path
13+
from collections import defaultdict
1314

1415
from converter import RuleConverter
1516
from formats import CursorFormat, WindsurfFormat, CopilotFormat
1617
from utils import get_version_from_pyproject
1718

1819

20+
def update_skill_md(language_to_rules: dict[str, list[str]], skill_path: str) -> None:
21+
"""
22+
Update SKILL.md with language-to-rules mapping table.
23+
24+
Args:
25+
language_to_rules: Dictionary mapping languages to rule files
26+
skill_path: Path to SKILL.md file
27+
"""
28+
# Generate markdown table
29+
table_lines = [
30+
"| Language | Rule Files to Apply |",
31+
"|----------|---------------------|",
32+
]
33+
34+
for language in sorted(language_to_rules.keys()):
35+
rules = sorted(language_to_rules[language])
36+
rules_str = ", ".join(rules)
37+
table_lines.append(f"| {language} | {rules_str} |")
38+
39+
table = "\n".join(table_lines)
40+
41+
# Markers for the language mappings section
42+
start_marker = "<!-- LANGUAGE_MAPPINGS_START -->"
43+
end_marker = "<!-- LANGUAGE_MAPPINGS_END -->"
44+
45+
# Read SKILL.md
46+
skill_file = Path(skill_path)
47+
content = skill_file.read_text(encoding="utf-8")
48+
49+
if not start_marker in content or not end_marker in content:
50+
raise RuntimeError(
51+
"Invalid SKILLS.md template: Language mappings section not found in SKILL.md"
52+
)
53+
54+
# Replace entire section including markers with just the table
55+
start_idx = content.index(start_marker)
56+
end_idx = content.index(end_marker) + len(end_marker)
57+
new_section = f"\n\n{table}\n\n"
58+
updated_content = content[:start_idx] + new_section + content[end_idx:]
59+
60+
# Write back to SKILL.md
61+
skill_file.write_text(updated_content, encoding="utf-8")
62+
print(f"Updated SKILL.md with language mappings")
63+
64+
1965
def convert_rules(input_path: str, output_dir: str = ".") -> dict[str, list[str]]:
2066
"""
2167
Convert rule file(s) to all supported IDE formats using RuleConverter.
@@ -68,6 +114,8 @@ def convert_rules(input_path: str, output_dir: str = ".") -> dict[str, list[str]
68114

69115
results = {"success": [], "errors": []}
70116

117+
language_to_rules = defaultdict(list)
118+
71119
# Process each file
72120
for md_file in files_to_process:
73121
try:
@@ -92,6 +140,10 @@ def convert_rules(input_path: str, output_dir: str = ".") -> dict[str, list[str]
92140
print(f"Success: {result.filename}{', '.join(output_files)}")
93141
results["success"].append(result.filename)
94142

143+
# Update language mappings for SKILL.md
144+
for language in result.languages:
145+
language_to_rules[language].append(result.filename)
146+
95147
except FileNotFoundError as e:
96148
error_msg = f"{md_file.name}: File not found - {e}"
97149
print(f"Error: {error_msg}")
@@ -112,6 +164,27 @@ def convert_rules(input_path: str, output_dir: str = ".") -> dict[str, list[str]
112164
f"\nResults: {len(results['success'])} success, {len(results['errors'])} errors"
113165
)
114166

167+
# Write language mappings to SKILL.md
168+
if language_to_rules:
169+
# Determine rules directory (where template should be)
170+
rules_dir = path if path.is_dir() else path.parent
171+
template_path = rules_dir / "codeguard-SKILLS.md.template"
172+
output_skill_dir = output_base / "skills" / "software-security"
173+
output_skill_path = output_skill_dir / "SKILL.md"
174+
175+
# Create output directory if it doesn't exist
176+
output_skill_dir.mkdir(parents=True, exist_ok=True)
177+
178+
# Copy template to output location and update with mappings
179+
if not template_path.exists():
180+
raise FileNotFoundError(f"Template not found at {template_path}")
181+
182+
output_skill_path.write_text(
183+
template_path.read_text(encoding="utf-8"), encoding="utf-8"
184+
)
185+
# Update with language mappings
186+
update_skill_md(language_to_rules, str(output_skill_path))
187+
115188
return results
116189

117190

0 commit comments

Comments
 (0)