[
  {
    "path": ".claude-plugin/marketplace.json",
    "content": "{\n  \"name\": \"project-codeguard\",\n  \"metadata\": {\n    \"description\": \"Official Project CodeGuard plugins for secure AI-assisted coding\"\n  },\n  \"owner\": {\n    \"name\": \"Project CodeGuard\",\n    \"url\": \"https://project-codeguard.org\",\n    \"email\": \"contact@project-codeguard.org\"\n  },\n  \"plugins\": [\n    {\n      \"name\": \"codeguard-security\",\n      \"source\": \"./\",\n      \"description\": \"Comprehensive security rules for AI coding agents\",\n      \"version\": \"1.2.0\",\n      \"repository\": \"https://github.com/project-codeguard/rules.git\",\n      \"tags\": [\n        \"security\",\n        \"code-review\",\n        \"vulnerability-prevention\"\n      ]\n    }\n  ]\n}\n"
  },
  {
    "path": ".claude-plugin/plugin.json",
    "content": "{\n  \"name\": \"codeguard-security\",\n  \"description\": \"Security code review skill based on Project CodeGuard's comprehensive security rules. Helps AI coding agents write secure code and prevent common vulnerabilities.\",\n  \"version\": \"1.2.0\",\n  \"author\": {\n    \"name\": \"Project CodeGuard\",\n    \"url\": \"https://project-codeguard.org\"\n  },\n  \"license\": \"CC-BY-4.0 (rules), Apache-2.0 (tools)\",\n  \"homepage\": \"https://github.com/project-codeguard/rules\",\n  \"repository\": \"https://github.com/project-codeguard/rules.git\",\n  \"keywords\": [\n    \"security\",\n    \"secure-coding\",\n    \"vulnerability-prevention\",\n    \"code-review\",\n    \"appsec\"\n  ]\n}\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/new-rule.yml",
    "content": "name: ✨ New Rule Request\ndescription: Share your new rule request\nlabels: [\"new-rule\"]\n\nbody:\n  - type: markdown\n    attributes:\n      value: |\n        Thank you for your new rule request! Please provide as much detail as possible. Use any of the existing rules as a reference. You only have to provide the rule contents (markdown), not the rule metadata. We will handle the rest. In other words, convert your rule into all the formats (Cursor, Windsurf, Copilot, Antigravity).\n\n  - type: textarea\n    id: description\n    attributes:\n      label: Description\n      description: Provide the new rule contents (markdown).\n      placeholder: Enter the new rule contents (markdown) here...\n    validations:\n      required: true\n\n  - type: dropdown\n    id: language\n    attributes:\n      label: What language(s) are the rule for?\n      multiple: true\n      options:\n        - 'python'\n        - 'javascript'\n        - 'typescript'\n        - 'java'\n        - 'c'\n        - 'c++'\n        - 'go'\n        - 'rust'\n        - 'ruby'\n        - 'php'\n        - 'swift'\n        - 'kotlin'\n        - 'scala'\n        - 'r'\n        - 'matlab'\n        - 'julia'\n        - 'dart'\n        - 'lua'\n        - 'perl'\n        - 'shell'\n        - 'powershell'\n        - 'fsharp'\n        - 'erlang'\n        - 'ocaml'\n        - 'yaml'\n        - 'docker'\n        - 'xml'\n        - 'vue'\n        - 'elm'\n        - 'cuda'\n        - 'opencl'\n        - 'other (please specify)'\n    validations:\n      required: true\n  - type: textarea\n    id: other\n    attributes:\n      label: Other\n      description: Please specify the language(s) you are requesting. Only specify this if the language is not listed above.\n      placeholder: Add the language(s) here...\n    validations:\n      required: false\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/rule-feedback.yml",
    "content": "---\nname: 📝 Rule Feedback\ndescription: Share your feedback, suggestions, or report issues\nlabels: [\"rule-feedback\"]\n\nbody:\n  - type: markdown\n    attributes:\n      value: |\n        Thank you for your feedback! Please provide as much detail as possible.\n\n  - type: textarea\n    id: description\n    attributes:\n      label: Description\n      description: Provide detailed feedback.\n      placeholder: Describe your feedback here... Add any additional context, such as screenshots, related issues, or why this matters.\n    validations:\n      required: true\n\n  - type: dropdown\n    id: ai-coding-tool\n    attributes:\n      label: What AI Coding Tool are you Using?\n      options:\n        - Cursor\n        - GitHub Copilot\n        - Windsurf\n        - Antigravity\n        - Codex\n        - Augment Code\n        - Sourcegraph\n        - Claude Code\n        - Other\n    validations:\n      required: true\n\n  - type: textarea\n    id: ai-model\n    attributes:\n      label: AI Model Used\n      description: What AI model are you using?\n      placeholder: Add the model name... (e.g. GPT-5, Claude 4.5 Sonnet, Gemini 2.5 Pro, etc.)\n\n  - type: dropdown\n    id: feedback-type\n    attributes:\n      label: The feedback pertains to which aspect of the project?\n      options:\n        - Rule definition\n        - Rule invocation or execution\n        - Rule distribution\n        - Other\n    validations:\n      required: true\n"
  },
  {
    "path": ".github/workflows/build-ide-bundles.yml",
    "content": "---\nname: Build and Release IDE Bundles\n\non:\n  release:\n    types: [published]\n\njobs:\n  build-and-release:\n    runs-on: ubuntu-latest\n    permissions:\n      contents: write\n    \n    steps:\n      - name: Checkout repository\n        uses: actions/checkout@v4\n        with:\n          ref: ${{ github.event.release.tag_name }}\n\n      - name: Install uv\n        uses: astral-sh/setup-uv@v4\n        with:\n          enable-cache: true\n\n      - name: Set up Python\n        uses: actions/setup-python@v5\n        with:\n          python-version: '3.11'\n\n      - name: Install dependencies\n        run: uv sync\n\n      - name: Get version from release\n        id: get_version\n        run: |\n          TAG=\"${{ github.event.release.tag_name }}\"\n          VERSION=${TAG#v}\n          echo \"tag=$TAG\" >> $GITHUB_OUTPUT\n          echo \"version=$VERSION\" >> $GITHUB_OUTPUT\n          echo \"Release version: $VERSION (tag: $TAG)\"\n\n      - name: Validate rules\n        run: uv run python src/validate_unified_rules.py sources/\n\n      - name: Validate versions match tag\n        run: uv run python src/validate_versions.py ${{ steps.get_version.outputs.version }}\n\n      - name: Generate IDE bundles\n        run: uv run python src/convert_to_ide_formats.py\n\n      - name: Create release archives\n        run: |\n          cd dist\n          zip -r ../ide-rules-cursor.zip .cursor/\n          zip -r ../ide-rules-windsurf.zip .windsurf/\n          zip -r ../ide-rules-copilot.zip .github/\n          zip -r ../ide-rules-antigravity.zip .agent/\n          cd ..\n          zip -r ide-rules-all.zip dist/\n          ls -lh ide-rules-*.zip\n\n      - name: Upload release assets\n        env:\n          GH_TOKEN: ${{ github.token }}\n        run: |\n          gh release upload \"${{ steps.get_version.outputs.tag }}\" \\\n            ide-rules-all.zip \\\n            ide-rules-cursor.zip \\\n            ide-rules-windsurf.zip \\\n            ide-rules-copilot.zip \\\n            ide-rules-antigravity.zip \\\n            --clobber\n\n"
  },
  {
    "path": ".github/workflows/deploy-docs.yml",
    "content": "---\nname: Deploy Documentation\n\non:\n  workflow_dispatch:\n\npermissions:\n  contents: write\n\njobs:\n  deploy:\n    runs-on: ubuntu-latest\n    \n    steps:\n      - name: Generate GitHub App Token\n        id: generate-token\n        uses: actions/create-github-app-token@v1\n        with:\n          app-id: ${{ secrets.CODEGUARD_APP_ID }}\n          private-key: ${{ secrets.CODEGUARD_APP_PRIVATE_KEY }}\n      \n      - name: Checkout repository\n        uses: actions/checkout@v4\n        with:\n          fetch-depth: 0\n          token: ${{ steps.generate-token.outputs.token }}\n\n      - name: Install uv\n        uses: astral-sh/setup-uv@v4\n        with:\n          enable-cache: true\n\n      - name: Set up Python\n        uses: actions/setup-python@v5\n        with:\n          python-version: '3.11'\n\n      - name: Install dependencies\n        run: uv sync\n\n      - name: Configure Git\n        run: |\n          git config --local user.email \"github-actions[bot]@users.noreply.github.com\"\n          git config --local user.name \"github-actions[bot]\"\n\n      - name: Deploy to GitHub Pages\n        env:\n          GH_TOKEN: ${{ steps.generate-token.outputs.token }}\n        run: uv run mkdocs gh-deploy --force --clean --verbose\n\n"
  },
  {
    "path": ".github/workflows/stale.yml",
    "content": "---\n    # This workflow warns and then closes issues and PRs that have had no activity for a specified amount of time.\n    # For more information, see: https://github.com/actions/stale\n    name: 'Close Stale Issues and PRs'\n    \n    on:\n      schedule:\n        # Runs at 02:33 UTC every day. You can adjust the schedule as needed.\n        - cron: '33 2 * * *'\n    \n    jobs:\n      stale:\n        # The type of runner that the job will run on.\n        runs-on: ubuntu-latest\n    \n        # Permissions for the GITHUB_TOKEN to allow the action to modify issues and PRs.\n        permissions:\n          issues: write\n          pull-requests: write\n    \n        steps:\n          - uses: actions/stale@v9\n            with:\n              # The token for the repository. Required.\n              repo-token: ${{ secrets.GITHUB_TOKEN }}\n    \n              # The message to post on stale issues.\n              stale-issue-message: >\n                This issue has been automatically marked as stale because it has not had\n                recent activity. It will be closed if no further activity occurs.\n                Thank you for your contributions.\n    \n              # The message to post on stale PRs.\n              stale-pr-message: >\n                This pull request has been automatically marked as stale because it has not had\n                recent activity. It will be closed if no further activity occurs.\n                Thank you for your contributions.\n    \n              # The label to apply to stale issues and PRs.\n              stale-issue-label: 'stale'\n              stale-pr-label: 'stale'\n    \n              # The number of days of inactivity before an issue or PR is marked as stale.\n              days-before-stale: 60\n    \n              # The number of days of inactivity before a stale issue or PR is closed.\n              # Set to -1 to disable closing.\n              days-before-close: 7\n    \n              # Labels that prevent an issue or PR from being marked as stale.\n              exempt-issue-labels: 'pinned,security'\n              exempt-pr-labels: 'pinned,security'\n    \n              # Exempt all issues and PRs with an assignee.\n              exempt-all-issue-assignees: true\n              exempt-all-pr-assignees: true\n    \n              # The maximum number of operations to perform per run.\n              operations-per-run: 100"
  },
  {
    "path": ".github/workflows/validate-rules.yml",
    "content": "---\nname: Validate Rules\n\npermissions:\n  contents: read\n\non:\n  pull_request:\n    paths:\n      - 'sources/**'\n      - 'src/**'\n      - 'pyproject.toml'\n  push:\n    branches:\n      - main\n      - develop\n    paths:\n      - 'sources/**'\n      - 'src/**'\n      - 'pyproject.toml'\n  workflow_dispatch:\n\njobs:\n  validate:\n    runs-on: ubuntu-latest\n    \n    steps:\n      - name: Checkout repository\n        uses: actions/checkout@v4\n\n      - name: Install uv\n        uses: astral-sh/setup-uv@v4\n        with:\n          enable-cache: true\n\n      - name: Set up Python\n        uses: actions/setup-python@v5\n        with:\n          python-version: '3.11'\n\n      - name: Install dependencies\n        run: uv sync\n\n      - name: Validate unified rules\n        run: uv run python src/validate_unified_rules.py sources/\n\n      - name: Check required core rule files exist\n        run: |\n          echo \"Checking for required core rule files...\"\n          required_files=(\n            \"sources/core/codeguard-1-hardcoded-credentials.md\"\n            \"sources/core/codeguard-1-crypto-algorithms.md\"\n            \"sources/core/codeguard-1-digital-certificates.md\"\n            \"sources/core/codeguard-SKILLS.md.template\"\n          )\n          \n          missing=0\n          for file in \"${required_files[@]}\"; do\n            if [ ! -f \"$file\" ]; then\n              echo \"❌ Missing required file: $file\"\n              missing=1\n            else\n              echo \"✅ Found: $file\"\n            fi\n          done\n          \n          if [ $missing -eq 1 ]; then\n            exit 1\n          fi\n\n      - name: Test conversion to IDE formats\n        run: |\n          echo \"Testing IDE format conversion...\"\n          uv run python src/convert_to_ide_formats.py --output-dir test-output\n          \n          # Check that files were generated\n          if [ ! -d \"test-output/.cursor\" ]; then\n            echo \"❌ Cursor rules not generated\"\n            exit 1\n          fi\n          \n          if [ ! -d \"test-output/.windsurf\" ]; then\n            echo \"❌ Windsurf rules not generated\"\n            exit 1\n          fi\n          \n          if [ ! -d \"test-output/.github\" ]; then\n            echo \"❌ Copilot instructions not generated\"\n            exit 1\n          fi\n          \n          if [ ! -d \"test-output/.agent\" ]; then\n            echo \"❌ Antigravity rules not generated\"\n            exit 1\n          fi\n          \n          echo \"✅ All IDE formats generated successfully\"\n\n      - name: Check skills/ directory is up-to-date\n        run: |\n          echo \"Checking if committed skills/ directory is up-to-date...\"\n          \n          # Save current skills\n          mv skills skills-committed\n          \n          # Regenerate skills (core rules only, matching default)\n          uv run python src/convert_to_ide_formats.py\n          \n          # Compare\n          if ! diff -r skills/ skills-committed/ > /dev/null 2>&1; then\n            echo \"❌ The skills/ directory is out of date!\"\n            echo \"Please regenerate by running: python src/convert_to_ide_formats.py\"\n            echo \"Then: git add skills/\"\n            mv skills-committed skills\n            exit 1\n          fi\n          \n          # Restore original\n          rm -rf skills\n          mv skills-committed skills\n          echo \"✅ Committed skills/ directory is up-to-date\"\n\n      - name: Summary\n        if: success()\n        run: |\n          echo \"✅ All validation checks passed!\"\n          echo \"\"\n          echo \"Rule validation: ✅\"\n          echo \"Required files: ✅\"\n          echo \"IDE conversion: ✅\"\n          echo \"Skills directory: ✅\"\n\n"
  },
  {
    "path": ".gitignore",
    "content": "# Byte-compiled / optimized / DLL files\n__pycache__/\n*.py[cod]\n*$py.class\n\n# C extensions\n*.so\n\n# Distribution / packaging\n.Python\nbuild/\ndist/\ndownloads/\neggs/\n.eggs/\nlib/\nlib64/\nparts/\nsdist/\nvar/\nwheels/\n*.egg-info/\n.installed.cfg\n*.egg\n\n# PyInstaller\n#  Usually these files are written by a python script from a template\n#  before PyInstaller builds the exe, so as to inject date/other infos into it.\n*.manifest\n*.spec\n\n# Installer logs\npip-log.txt\npip-delete-this-directory.txt\n\n# Unit test / coverage reports\nhtmlcov/\n.tox/\n.nox/\n.coverage\n.coverage.*\n.cache\nnosetests.xml\ncoverage.xml\n*.cover\n.hypothesis/\n.pytest_cache/\n\n# Translations\n*.mo\n*.pot\n\n# Django stuff:\n*.log\nlocal_settings.py\ndb.sqlite3\n\n# Flask stuff:\ninstance/\n.webassets-cache\n\n# Scrapy stuff:\n.scrapy\n\n# Sphinx documentation\ndocs/_build/\n\n# PyBuilder\ntarget/\n\n# Jupyter Notebook\n.ipynb_checkpoints\n\n# IPython\nprofile_default/\nipython_config.py\n\n# pyenv\n.python-version\n\n# celery beat schedule file\ncelerybeat-schedule\n\n# SageMath parsed files\n*.sage.py\n\n# Environments\n.env\n.venv\nenv/\nvenv/\nENV/\nenv.bak/\nvenv.bak/\n\n# Spyder project settings\n.spyderproject\n.spyproject\n\n# Rope project settings\n.ropeproject\n\n# mkdocs documentation\n/site\n\n# mypy\n.mypy_cache/\n.dmypy.json\ndmypy.json\n\n# Pyre type checker\n.pyre/\n\n# Node.js\nnode_modules/\nnpm-debug.log\nyarn-error.log\n\n# macOS\n.DS_Store\n.AppleDouble\n.LSOverride\n\n# Thumbnails\n._*\n\n# Files that might appear in the root of a volume\n.DocumentRevisions-V100\n.fseventsd\n.Spotlight-V100\n.TemporaryItems\n.Trashes\n.VolumeIcon.icns\n.com.apple.timemachine.donotpresent\n\n# Directories potentially created on remote AFP share\n.AppleDB\n.AppleDesktop\nNetwork Trash Folder\nTemporary Items\n.apdisk\n\n# VSCode\n.vscode/\n\n# JetBrains\n.idea/\n\n# Log files\n*.log\n\n# Environment variables\n.env\n.env.local\n.env.development.local\n.env.test.local\n.env.production.local\n\n# Coding Agents\nCLAUDE.md \nAGENTS.md\n\n# Claude Code Plugin\n.claude-plugin/.cache\n.claude/settings.local.json\n\n# Generated IDE-specific rule bundles (not committed, built for releases)\ndist/\n"
  },
  {
    "path": "CODE_OF_CONDUCT.md",
    "content": "# Contributor Covenant Code of Conduct\n\n## Our Pledge\n\nWe as members, contributors, and leaders pledge to make participation in our\ncommunity a harassment-free experience for everyone, regardless of age, body\nsize, visible or invisible disability, ethnicity, sex characteristics, gender\nidentity and expression, level of experience, education, socio-economic status,\nnationality, personal appearance, race, caste, color, religion, or sexual\nidentity and orientation.\n\nWe pledge to act and interact in ways that contribute to an open, welcoming,\ndiverse, inclusive, and healthy community.\n\n## Our Standards\n\nExamples of behavior that contributes to a positive environment for our\ncommunity include:\n\n* Demonstrating empathy and kindness toward other people\n* Being respectful of differing opinions, viewpoints, and experiences\n* Giving and gracefully accepting constructive feedback\n* Accepting responsibility and apologizing to those affected by our mistakes,\n  and learning from the experience\n* Focusing on what is best not just for us as individuals, but for the overall\n  community\n\nExamples of unacceptable behavior include:\n\n* The use of sexualized language or imagery, and sexual attention or advances of\n  any kind\n* Trolling, insulting or derogatory comments, and personal or political attacks\n* Public or private harassment\n* Publishing others' private information, such as a physical or email address,\n  without their explicit permission\n* Other conduct which could reasonably be considered inappropriate in a\n  professional setting\n\n## Enforcement Responsibilities\n\nCommunity leaders are responsible for clarifying and enforcing our standards of\nacceptable behavior and will take appropriate and fair corrective action in\nresponse to any behavior that they deem inappropriate, threatening, offensive,\nor harmful.\n\nCommunity leaders have the right and responsibility to remove, edit, or reject\ncomments, commits, code, wiki edits, issues, and other contributions that are\nnot aligned to this Code of Conduct, and will communicate reasons for moderation\ndecisions when appropriate.\n\n## Scope\n\nThis Code of Conduct applies within all community spaces, and also applies when\nan individual is officially representing the community in public spaces.\nExamples of representing our community include using an official email address,\nposting via an official social media account, or acting as an appointed\nrepresentative at an online or offline event.\n\n## Enforcement\n\nInstances of abusive, harassing, or otherwise unacceptable behavior may be\nreported to the community leaders responsible for enforcement at\noss-security@cisco.com. All complaints will be reviewed and investigated\npromptly and fairly.\n\nAll community leaders are obligated to respect the privacy and security of the\nreporter of any incident.\n\n## Enforcement Guidelines\n\nCommunity leaders will follow these Community Impact Guidelines in determining\nthe consequences for any action they deem in violation of this Code of Conduct:\n\n### 1. Correction\n\n**Community Impact**: Use of inappropriate language or other behavior deemed\nunprofessional or unwelcome in the community.\n\n**Consequence**: A private, written warning from community leaders, providing\nclarity around the nature of the violation and an explanation of why the\nbehavior was inappropriate. A public apology may be requested.\n\n### 2. Warning\n\n**Community Impact**: A violation through a single incident or series of\nactions.\n\n**Consequence**: A warning with consequences for continued behavior. No\ninteraction with the people involved, including unsolicited interaction with\nthose enforcing the Code of Conduct, for a specified period of time. This\nincludes avoiding interactions in community spaces as well as external channels\nlike social media. Violating these terms may lead to a temporary or permanent\nban.\n\n### 3. Temporary Ban\n\n**Community Impact**: A serious violation of community standards, including\nsustained inappropriate behavior.\n\n**Consequence**: A temporary ban from any sort of interaction or public\ncommunication with the community for a specified period of time. No public or\nprivate interaction with the people involved, including unsolicited interaction\nwith those enforcing the Code of Conduct, is allowed during this period.\nViolating these terms may lead to a permanent ban.\n\n### 4. Permanent Ban\n\n**Community Impact**: Demonstrating a pattern of violation of community\nstandards, including sustained inappropriate behavior, harassment of an\nindividual, or aggression toward or disparagement of classes of individuals.\n\n**Consequence**: A permanent ban from any sort of public interaction within the\ncommunity.\n\n## Attribution\n\nThis Code of Conduct is adapted from the [Contributor Covenant][homepage],\nversion 2.1, available at\n[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].\n\nCommunity Impact Guidelines were inspired by [Mozilla's code of conduct\nenforcement ladder][Mozilla CoC].\n\nFor answers to common questions about this code of conduct, see the FAQ at\n[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at\n[https://www.contributor-covenant.org/translations][translations].\n\n[homepage]: https://www.contributor-covenant.org\n[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html\n[Mozilla CoC]: https://github.com/mozilla/diversity\n[FAQ]: https://www.contributor-covenant.org/faq\n[translations]: https://www.contributor-covenant.org/translations"
  },
  {
    "path": "CONTRIBUTING.md",
    "content": "# Contributing\n\n## How to contribute?\n\nProject CodeGuard is an open-source initiative actively seeking contributions from the community. This document outlines how to get involved and the process we follow for proposing, reviewing, and merging changes.\n\nIn general, a Project CodeGuard Contributor is expected to:\n- be knowledgeable in one or more areas related to the project\n- contribute to developing and finalizing workstream deliverables\n- be reliable in completing issues they take on\n- show commitment over time with one or more PRs merged\n- follow the project style and testing guidelines\n- follow branch, PR, and code style conventions\n- contribute in ways that substantially improve the quality of the project and the experience of people who use it\n\nWhen contributing to any CodeGuard repository, please first discuss the change you wish to make via a GitHub issue. For small fixes, opening a PR directly with clear context is fine; for larger changes or new features, please open an issue first to align on scope and approach.\n\nPlease note that all of your interactions in the project are subject to our [Code of Conduct](CODE_OF_CONDUCT.md). This includes creation of issues or pull requests, commenting on issues or pull requests, and extends to all interactions in any real-time space.\n\nPlease be respectful of differing opinions when discussing potential contributions. We aim for a welcoming, collaborative environment for all contributors.\n\n## Content Update Governance Process\n\nCodeGuard uses a two-stage governance process for updates to the project’s rules, controls, and other structured content. This separates technical review from community governance, ensuring both content quality and project alignment.\n\n### What Constitutes a Content Update\n\nContent updates include changes to:\n- Rules definitions, categories, and metadata\n- Security control specifications and mappings\n- Component or taxonomy elements and relationships\n- Content-oriented documentation and guidance materials\n\n### Two-Stage Process Overview\n\n**Stage 1: Technical Review** – Content `feature` branches merge to the `develop` branch after standard PR review\n\n**Stage 2: Community Review** – Bi-weekly governance review of the `develop` branch’s accumulated changes before release to `main`\n\n```\nfeature-branch  →  develop    →    main\n     ↑                 ↑             ↑\n  Stage 1         Stage 2        Release\n(Technical)       (Community)\n```\n\n### Non-Content Changes\n\nThe following types of changes are not covered by the two-stage content update process and continue to follow existing workflows:\n- Bug fixes – technical corrections and error resolution\n- Implementation changes – updates to code logic, algorithms, or system functionality\n- Infrastructure updates – CI/CD, build processes, deployment configurations\n- Documentation fixes – corrections to technical documentation, README updates, etc.\n- Security patches – critical security-related fixes requiring immediate deployment\n- Dependency updates – library upgrades and security patches for dependencies\n\nThese excluded change types may follow direct-to-`main` workflows as determined by repository maintainers and policies.\n\n## Repository Structure\n\n- **`sources/`** - Source rules (edit these: `core/` has 22 files, `owasp/` has 88)\n- **`src/`** - Conversion and validation tools\n- **`skills/`** - Claude Code plugin (generated, committed)\n- **`dist/`** - Other IDE bundles (generated, not committed)\n\n## First-time contributors\n\nIf you are new to the project and looking for an entry point, check the open issues. Issues tagged `good first issue` are meant to be small, well-scoped tasks suitable for first-time contributors. If you find one you’d like to work on, comment on the issue and/or assign yourself, and a maintainer can confirm assignment.\n\n## Submitting a new issue\n\nIf you want to create a new issue that doesn't exist already, open a new one and include:\n- a concise problem statement or feature request\n- steps to reproduce (for bugs)\n- expected vs. actual behavior\n- environment details (versions, OS) if applicable\n- proposed approach or alternatives (optional but helpful)\n\n**If you discover a security bug, please do not report it through GitHub. Instead, please see security procedures in [SECURITY.md](SECURITY.md).**\n\n## Submitting a new pull request and review process\n\nThe process for submitting pull requests depends on the type of change:\n\n### For Content Updates (Two-Stage Process)\n\nFollow these steps when submitting content updates:\n\n1. Fork this repo into your GitHub account. If you have write access, you may create a branch directly.\n2. Create a new branch, based on the `develop` branch, with a name that concisely describes what you’re working on.\n3. Ensure that your changes pass validation and do not cause any existing tests to fail.\n4. Submit a pull request against the `develop` branch.\n\n#### Content Update PR Review\n\n**Stage 1 Review**: Your PR to `develop` will be reviewed for technical criteria including content correctness, formatting and schema validation (where applicable), code hygiene, and commit message quality.\n\n**Stage 2 Review**: On a regular cadence (typically bi-weekly), a PR will be created from `develop` to `main` containing all merged content changes from the cycle period. This undergoes community review by maintainers and contributors using lazy consensus and/or simple voting procedures as needed.\n\n### For Non-Content Changes (Standard Process)\n\nFollow these steps when submitting non-content changes (bug fixes, implementation changes, infrastructure updates, etc.):\n\n1. Fork this repo into your GitHub account (or create a branch if you have write access).\n2. Create a new branch, based on the `main` branch, with a name that concisely describes what you’re working on.\n3. Ensure that your changes do not cause any existing tests to fail.\n4. Submit a pull request against the `main` branch.\n\n#### Non-Content PR Review\n1. PRs will be reviewed by maintainers. Additional reviewers may be requested depending on scope.\n2. Reviewer responses are typically due within 3 business days.\n\n### General Review Guidelines\n\nMaintainers aim to review pull requests and issues within 3 business days.\n\n[Lazy consensus](https://openoffice.apache.org/docs/governance/lazyConsensus.html) is practiced for this project. Major changes in GitHub or to a project document on any official platform should be accompanied by an issue or discussion with a comment period of no less than seven (7) business days, mindful of major world holidays.\n\n## Branch naming and commit messages\n\n### Branch naming\n\n- `main` – main development branch and authoritative source; updated after community approval for content changes\n- `develop` – staging area for community review of content updates; feature branches for content changes target this branch\n- `feature` – feature/this-is-a-new-feature-branch (target `develop` for content updates, `main` for non-content changes)\n- `codebugfix` – codebugfix/description-of-the-bug (typically targets `main`)\n- `languagefix` – languagefix/description-of-the-language-fix (typically targets `main`)\n- `docs` – docs/description-of-the-documentation-change (typically targets `main`; documentation changes are exempt from the content update rule above)\n- `release` – release/description-of-the-release – cut from `main` when ready\n\n### Commit messages format\n\nWrite commit messages that clearly explain the change by continuing the sentence “This commit …”.\n\nExamples of good commit messages:\n- \"This commit renames the examples folder to reference-implementations.\"\n- \"This commit bumps dependency versions to address security advisories.\"\n\n## Release Process (Maintainers)\n\n### Step 1: Update Version\n\n```bash\n# Edit pyproject.toml and update the version number\nvi pyproject.toml  # Change version = \"1.2.3\"\n\n# Regenerate all IDE bundles (auto-syncs version to JSON files)\nuv run python src/convert_to_ide_formats.py\n\n# Review changes\ngit diff\n\n# Commit changes\ngit add -A\ngit commit -m \"Bump version to 1.2.3\"\n\n# Push to main (via PR or direct)\ngit push origin main\n```\n\n**Note**: The conversion script automatically syncs the version from `pyproject.toml` to:\n- `.claude-plugin/plugin.json` and `marketplace.json` (Claude Code plugin metadata)\n- All generated IDE rule files (Cursor `.mdc`, Windsurf `.md`, Copilot `.instructions.md`, Claude Code `.md`, Antigravity `.md`)\n\nThis ensures version consistency across all artifacts.\n\n### Step 2: Create Release (GitHub UI)\n\n1. Go to: https://github.com/project-codeguard/rules/releases/new\n2. **Tag**: Enter `vX.Y.Z` (must match version in pyproject.toml)\n3. **Title**: `Project CodeGuard vX.Y.Z`\n4. Click **\"Generate release notes\"** (auto-generates changelog)\n5. Click **\"Publish release\"**\n\nGitHub Actions will automatically:\n- ✅ Validate versions match the tag\n- ✅ Build IDE bundles (Cursor, Windsurf, Copilot, Antigravity)\n- ✅ Upload ZIP artifacts to the release\n\n## Testing Your Changes\n\n```bash\n# Validate rules (required if you modified rules)\npython src/validate_unified_rules.py sources/\n\n# Regenerate skills/ (required if you modified rules)\npython src/convert_to_ide_formats.py\ngit add skills/  # skills/ is version-controlled\n\n# Test with all sources (core + owasp)\npython src/convert_to_ide_formats.py --source core owasp -o test-output/\n# Check: test-output/.cursor/, test-output/.windsurf/, etc.\n\n# Get help\npython src/convert_to_ide_formats.py --help\n```\n\n## Feedback\n\nQuestions or comments about this project may be composed as GitHub issues or PR comments. If a broader discussion is needed, open or reference a GitHub Discussion (if enabled) or propose a short design doc in an issue."
  },
  {
    "path": "LICENSE",
    "content": "Creative Commons Attribution 4.0 International (CC BY 4.0)\n\nCopyright © 2025 Cisco Systems, Inc.\n\nLicensed under the Creative Commons Attribution 4.0 International License.\nYou may obtain a copy of the License at:\n\n    https://creativecommons.org/licenses/by/4.0/\n\nUnless required by applicable law or agreed to in writing, content\ndistributed under this license is distributed on an “AS IS” BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n\nAttribution Requirements\n— You must give appropriate credit, provide a link to the license, and\n  indicate if changes were made. You may do so in any reasonable manner,\n  but not in any way that suggests the licensor endorses you or your use.\n— No additional restrictions — You may not apply legal terms or\n  technological measures that legally restrict others from doing anything\n  the license permits.\n\nFor the full legal code of CC BY 4.0, see:\n\n    https://creativecommons.org/licenses/by/4.0/legalcode\n\n\n"
  },
  {
    "path": "README.md",
    "content": "## Project CodeGuard has been donated to the Coalition for Secure AI (CoSAI)!\n\nThis project is now maintained by the Coalition for Secure AI (CoSAI).\n\n**➡️ New Repository: [github.com/cosai-oasis/project-codeguard](https://github.com/cosai-oasis/project-codeguard)**\n\n\nPlease visit the new repository for the latest updates and how to contribute to the project.\n"
  },
  {
    "path": "SECURITY.md",
    "content": "# Security Policies and Procedures\n\nThis document outlines security procedures and general policies for the\nProject CodeGuard project.\n\n- [Disclosing a security issue](#disclosing-a-security-issue)\n- [Vulnerability management](#vulnerability-management)\n- [Suggesting changes](#suggesting-changes)\n\n## Disclosing a security issue\n\nThe Project CodeGuard maintainers take all security issues in the project\nseriously. Thank you for improving the security of Project CodeGuard. We\nappreciate your dedication to responsible disclosure and will make every effort to\nacknowledge your contributions.\n\nProject CodeGuard leverages GitHub's private vulnerability reporting.\n\nTo learn more about this feature and how to submit a vulnerability report,\nreview [GitHub's documentation on private\nreporting](https://docs.github.com/code-security/security-advisories/guidance-on-reporting-and-writing-information-about-vulnerabilities/privately-reporting-a-security-vulnerability).\n\nHere are some helpful details to include in your report:\n\n- a detailed description of the issue\n- the steps required to reproduce the issue\n- versions of the project that may be affected by the issue\n- if known, any mitigations for the issue\n\nA maintainer will acknowledge the report within three (3) business days, and\nwill send a more detailed response within an additional three (3) business days\nindicating the next steps in handling your report.\n\nIf you've been unable to successfully draft a vulnerability report via GitHub\nor have not received a response during the alloted response window, please\nreach out via the [Cisco Open security contact email](mailto:oss-security@cisco.com).\n\nAfter the initial reply to your report, the maintainers will endeavor to keep\nyou informed of the progress towards a fix and full announcement, and may ask\nfor additional information or guidance.\n\n## Vulnerability management\n\nWhen the maintainers receive a disclosure report, they will assign it to a\nprimary handler.\n\nThis person will coordinate the fix and release process, which involves the\nfollowing steps:\n\n- confirming the issue\n- determining affected versions of the project\n- auditing code to find any potential similar problems\n- preparing fixes for all releases under maintenance\n\n## Suggesting changes\n\nIf you have suggestions on how this process could be improved please submit an\nissue or pull request."
  },
  {
    "path": "pyproject.toml",
    "content": "[project]\nname = \"project-codeguard\"\nversion = \"1.2.0\"\ndescription = \"AI Coding Rules for Security and Best Practices\"\nrequires-python = \">=3.11\"\ndependencies = [\n    \"pyyaml~=6.0.2\",\n    \"mkdocs~=1.6.1\",\n    \"mkdocs-material~=9.6.21\",\n]\n"
  },
  {
    "path": "skills/software-security/SKILL.md",
    "content": "---\nname: software-security\ndescription: 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.\ncodeguard-version: \"1.2.0\"\nframework: \"Project CodeGuard\"\npurpose: \"Embed secure-by-default practices into AI coding workflows\"\n---\n\n# Software Security Skill (Project CodeGuard)\nThis 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.\n\n## When to Use This Skill\nThis skill should be activated when:\n- Writing new code in any language\n- Reviewing or modifying existing code\n- Implementing security-sensitive features (authentication, cryptography, data handling, etc.)\n- Working with user input, databases, APIs, or external services\n- Configuring cloud infrastructure, CI/CD pipelines, or containers\n- Handling sensitive data, credentials, or cryptographic operations\n\n## How to Use This Skill\nWhen writing or reviewing code:\n1. Always-Apply Rules: Some rules MUST be checked on every code operation:\n- `codeguard-1-hardcoded-credentials.md` - Never hardcode secrets, passwords, API keys, or tokens\n- `codeguard-1-crypto-algorithms.md` - Use only modern, secure cryptographic algorithms\n- `codeguard-1-digital-certificates.md` - Validate and manage digital certificates securely\n2. Context-Specific Rules: Apply rules from /rules directory based on the language of the feature being implemented using the table given below:\n\n\n| Language | Rule Files to Apply |\n|----------|---------------------|\n| apex | codeguard-0-input-validation-injection.md |\n| 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-safe-c-functions.md, codeguard-0-session-management-and-cookies.md, codeguard-0-xml-and-serialization.md |\n| cpp | codeguard-0-safe-c-functions.md |\n| d | codeguard-0-iac-security.md |\n| docker | codeguard-0-devops-ci-cd-containers.md, codeguard-0-supply-chain-security.md |\n| 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-mcp-security.md, codeguard-0-session-management-and-cookies.md, codeguard-0-xml-and-serialization.md |\n| html | codeguard-0-client-side-web-security.md, codeguard-0-input-validation-injection.md, codeguard-0-session-management-and-cookies.md |\n| 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-mcp-security.md, codeguard-0-mobile-apps.md, codeguard-0-session-management-and-cookies.md, codeguard-0-xml-and-serialization.md |\n| 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-mcp-security.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 |\n| kotlin | codeguard-0-additional-cryptography.md, codeguard-0-authentication-mfa.md, codeguard-0-framework-and-languages.md, codeguard-0-mobile-apps.md |\n| matlab | codeguard-0-additional-cryptography.md, codeguard-0-authentication-mfa.md, codeguard-0-mobile-apps.md, codeguard-0-privacy-data-protection.md |\n| perl | codeguard-0-mobile-apps.md |\n| 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 |\n| powershell | codeguard-0-devops-ci-cd-containers.md, codeguard-0-iac-security.md, codeguard-0-input-validation-injection.md |\n| 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-mcp-security.md, codeguard-0-session-management-and-cookies.md, codeguard-0-xml-and-serialization.md |\n| 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 |\n| rust | codeguard-0-mcp-security.md |\n| shell | codeguard-0-devops-ci-cd-containers.md, codeguard-0-iac-security.md, codeguard-0-input-validation-injection.md |\n| sql | codeguard-0-data-storage.md, codeguard-0-input-validation-injection.md |\n| swift | codeguard-0-additional-cryptography.md, codeguard-0-authentication-mfa.md, codeguard-0-mobile-apps.md |\n| 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-mcp-security.md, codeguard-0-session-management-and-cookies.md |\n| vlang | codeguard-0-client-side-web-security.md |\n| 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 |\n| 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 |\n\n\n3. Proactive Security: Don't just avoid vulnerabilities-actively implement secure patterns:\n- Use parameterized queries for database access\n- Validate and sanitize all user input\n- Apply least-privilege principles\n- Use modern cryptographic algorithms and libraries\n- Implement defense-in-depth strategies\n\n## CodeGuard Security Rules\nThe security rules are available in the `rules/` directory.\n\n### Usage Workflow\nWhen generating or reviewing code, follow this workflow:\n\n### 1. Initial Security Check\nBefore writing any code:\n- Check: Will this handle credentials? → Apply codeguard-1-hardcoded-credentials\n- Check: What language am I using? → Identify applicable language-specific rules\n- Check: What security domains are involved? → Load relevant rule files\n\n### 2. Code Generation\nWhile writing code:\n- Apply secure-by-default patterns from relevant Project CodeGuard rules\n- Add security-relevant comments explaining choices\n\n### 3. Security Review\nAfter writing code:\n- Review against implementation checklists in each rule\n- Verify no hardcoded credentials or secrets\n- Validate that all the rules have been successfully followed when applicable.\n- Explain which security rules were applied\n- Highlight security features implemented\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-0-additional-cryptography.md",
    "content": "---\ndescription: Additional Cryptography guidance\nlanguages:\n- c\n- go\n- java\n- javascript\n- kotlin\n- matlab\n- php\n- python\n- ruby\n- swift\n- typescript\n- xml\n- yaml\nalwaysApply: false\n---\n\nrule_id: codeguard-0-additional-cryptography\n\n## Additional Cryptography & TLS\n\nApply modern, vetted cryptography for data at rest and in transit. Manage keys safely, configure TLS correctly, deploy HSTS, and consider pinning only when appropriate.\n\n### Algorithms and Modes\n- Symmetric: AES‑GCM or ChaCha20‑Poly1305 preferred. Avoid ECB. CBC/CTR only with encrypt‑then‑MAC.\n- Asymmetric: RSA ≥2048 or modern ECC (Curve25519/Ed25519). Use OAEP for RSA encryption.\n- Hashing: SHA‑256+ for integrity; avoid MD5/SHA‑1.\n- RNG: Use CSPRNG appropriate to platform (e.g., SecureRandom, crypto.randomBytes, secrets module). Never use non‑crypto RNGs.\n\n### Key Management\n- Generate keys within validated modules (HSM/KMS) and never from passwords or predictable inputs.\n- Separate keys by purpose (encryption, signing, wrapping). Rotate on compromise, cryptoperiod, or policy.\n- Store keys in KMS/HSM or vault; never hardcode; avoid plain env vars. Use KEK to wrap DEKs; store separately.\n- Control access to trust stores; validate updates; audit all key access and operations.\n\n### Data at Rest\n- Encrypt sensitive data; minimize stored secrets; tokenize where possible.\n- Use authenticated encryption; manage nonces/IVs properly; keep salts unique per item.\n- Protect backups: encrypt, restrict access, test restores, manage retention.\n\n### TLS Configuration\n- Protocols: TLS 1.3 preferred; allow TLS 1.2 only for legacy compatibility; disable TLS 1.0/1.1 and SSL. Enable TLS_FALLBACK_SCSV.\n- Ciphers: prefer AEAD suites; disable NULL/EXPORT/anon. Keep libraries updated; disable compression.\n- Key exchange groups: prefer x25519/secp256r1; configure secure FFDHE groups if needed.\n- Certificates: 2048‑bit+ keys, SHA‑256, correct CN/SAN. Manage lifecycle and revocation (OCSP stapling).\n- Application: HTTPS site‑wide; redirect HTTP→HTTPS; prevent mixed content; set cookies `Secure`.\n\n### HSTS\n- Send Strict‑Transport‑Security only over HTTPS. Phase rollout:\n  - Test: short max‑age (e.g., 86400) with includeSubDomains\n  - Prod: ≥1 year max‑age; includeSubDomains when safe\n  - Optional preload once mature; understand permanence and subdomain impact\n\n### Pinning\n- Avoid browser HPKP. Consider pinning only for controlled clients (e.g., mobile) and when you own both ends.\n- Prefer SPKI pinning with backup pins; plan secure update channels; never allow user bypass.\n- Thoroughly test rotation and failure handling; understand operational risk.\n\n### Implementation Checklist\n- AEAD everywhere; vetted libraries only; no custom crypto.\n- Keys generated and stored in KMS/HSM; purpose‑scoped; rotation documented.\n- TLS 1.3/1.2 with strong ciphers; compression off; OCSP stapling on.\n- HSTS deployed per phased plan; mixed content eliminated.\n- Pinning used only where justified, with backups and update path.\n\n### Test Plan\n- Automated config scans (e.g., SSL Labs, testssl.sh) for protocol/cipher/HSTS.\n- Code review for crypto API misuse; tests for key rotation, backup/restore.\n- Pinning simulations for rotation/failures if deployed.\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-0-api-web-services.md",
    "content": "---\ndescription: API & Web services security (REST/GraphQL/SOAP), schema validation, authn/z, SSRF\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\n- xml\n- yaml\nalwaysApply: false\n---\n\nrule_id: codeguard-0-api-web-services\n\n## API & Web Services Security\n\nSecure REST, GraphQL, and SOAP/WS services end‑to‑end: transport, authn/z, schema validation, SSRF controls, DoS limits, and microservice‑safe patterns.\n\n### Transport and TLS\n- HTTPS only; consider mTLS for high‑value/internal services. Validate certs (CN/SAN, revocation) and prevent mixed content.\n\n### Authentication and Tokens\n- Use standard flows (OAuth2/OIDC) for clients; avoid custom schemes. For services, use mTLS or signed service tokens.\n- JWTs: pin algorithms; validate iss/aud/exp/nbf; short lifetimes; rotation; denylist on logout/revoke. Prefer opaque tokens when revocation is required and central store is available.\n- API keys: scope narrowly; rate limit; monitor usage; do not use alone for sensitive operations.\n\n### Authorization\n- Enforce per‑endpoint, per‑resource checks server‑side; deny by default.\n- For microservices, authorize at gateway (coarse) and service (fine) layers; propagate signed internal identity, not external tokens.\n\n### Input and Content Handling\n- Validate inputs via contracts: OpenAPI/JSON Schema, GraphQL SDL, XSD. Reject unknown fields and oversize payloads; set limits.\n- Content types: enforce explicit Content‑Type/Accept; reject unsupported combinations. Harden XML parsers against XXE/expansion.\n\n### SQL/Injection Safety in Resolvers and Handlers\n- Use parameterized queries/ORM bind parameters; never concatenate user input into queries or commands.\n\n### GraphQL‑Specific Controls\n- Limit query depth and overall complexity; enforce pagination; timeouts on execution; disable introspection and IDEs in production.\n- Implement field/object‑level authorization to prevent IDOR/BOLA; validate batching and rate limit per object type.\n\n### SSRF Prevention for Outbound Calls\n- Do not accept raw URLs. Validate domains/IPs using libraries; restrict to HTTP/HTTPS only (block file://, gopher://, ftp://, etc.).\n- Case 1 (fixed partners): strict allow‑lists; disable redirects; network egress allow‑lists.\n- Case 2 (arbitrary): block private/link‑local/localhost ranges; resolve and verify all IPs are public; require signed tokens from the target where feasible.\n\n### SOAP/WS and XML Safety\n- Validate SOAP payloads with XSD; limit message sizes; enable XML signatures/encryption where required.\n- Configure parsers against XXE, entity expansion, and recursive payloads; scan attachments.\n\n### Rate Limiting and DoS\n- Apply per‑IP/user/client limits, circuit breakers, and timeouts. Use server‑side batching and caching to reduce load.\n\n### Management Endpoints\n- Do not expose over the Internet. Require strong auth (MFA), network restrictions, and separate ports/hosts.\n\n### Testing and Assessment\n- Maintain formal API definitions; drive contract tests and fuzzing from specs.\n- Assess endpoints for authn/z bypass, SSRF, injection, and information leakage; log token validation failures.\n\n### Microservices Practices\n- Policy‑as‑code with embedded decision points; sidecar or library PDPs.\n- Service identity via mTLS or signed tokens; never reuse external tokens internally.\n- Centralized structured logging with correlation IDs; sanitize sensitive data.\n\n### Implementation Checklist\n- HTTPS/mTLS configured; certs managed; no mixed content.\n- Contract validation at the edge and service; unknown fields rejected; size/time limits enforced.\n- Strong authn/z per endpoint; GraphQL limits applied; introspection disabled in prod.\n- SSRF protections at app and network layers; redirects disabled; allow‑lists where possible.\n- Rate limiting, circuit breakers, and resilient patterns in place.\n- Management endpoints isolated and strongly authenticated.\n- Logs structured and privacy‑safe with correlation IDs.\n\n### Test Plan\n- Contract tests for schema adherence; fuzzing with schema‑aware tools.\n- Pen tests for SSRF, IDOR/BOLA, and authz bypass; performance tests for DoS limits.\n- Test all HTTP methods per endpoint; discover parameters in URL paths, headers, and structured data beyond obvious query strings.\n- Automated checks for token validation and revocation behavior.\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-0-authentication-mfa.md",
    "content": "---\ndescription: Authentication and MFA best practices (passwords, MFA, OAuth/OIDC, SAML, recovery, tokens)\nlanguages:\n- c\n- go\n- java\n- javascript\n- kotlin\n- matlab\n- php\n- python\n- ruby\n- swift\n- typescript\nalwaysApply: false\n---\n\nrule_id: codeguard-0-authentication-mfa\n\n## Authentication & MFA\n\nBuild a resilient, user-friendly authentication system that resists credential attacks, protects secrets, and supports strong, phishing-resistant MFA and secure recovery.\n\n### Account Identifiers and UX\n- Use non-public, random, and unique internal user identifiers. Allow login via verified email or username.\n- Always return generic error messages (e.g., \"Invalid username or password\"). Keep timing consistent to prevent account enumeration.\n- Support password managers: `<input type=\"password\">`, allow paste, no JS blocks.\n\n### Password Policy\n- Accept passphrases and full Unicode; minimum 8 characters; avoid composition rules. Set only a reasonable maximum length (64+).\n- Check new passwords against breach corpora (e.g., k‑anonymity APIs); reject breached/common passwords.\n\n### Password Storage (Hashing)\n- Hash, do not encrypt. Use slow, memory‑hard algorithms with unique per‑user salts and constant‑time comparison.\n- Preferred order and parameters (tune to your hardware; target <1s on server):\n  - Argon2id: m=19–46 MiB, t=2–1, p=1 (or equivalent security trade‑offs)\n  - scrypt: N=2^17, r=8, p=1 (or equivalent)\n  - bcrypt (legacy only): cost ≥10, be aware of 72‑byte input limit\n  - PBKDF2 (FIPS): PBKDF2‑HMAC‑SHA‑256 ≥600k, or SHA‑1 ≥1.3M\n- Optional pepper: store outside DB (KMS/HSM); if used, apply via HMAC or pre‑hashing. Plan for user resets if pepper rotates.\n- Unicode and null bytes must be supported end‑to‑end by the library.\n\n### Authentication Flow Hardening\n- Enforce TLS for all auth endpoints and token transport; enable HSTS.\n- Implement rate limits per IP, account, and globally; add proof‑of‑work or CAPTCHA only as last resort.\n- Lockouts/throttling: progressive backoff; avoid permanent lockout via resets/alerts.\n- Uniform responses and code paths to reduce oracle/timing signals.\n\n### Multi‑Factor Authentication (MFA)\n- Adopt phishing‑resistant factors by default for sensitive accounts: passkeys/WebAuthn (FIDO2) or hardware U2F.\n- Acceptable: TOTP (app‑based), smart cards with PIN. Avoid for sensitive use: SMS/voice, email codes; never rely on security questions.\n- Require MFA for: login, password/email changes, disabling MFA, privilege elevation, high‑value transactions, new devices/locations.\n- Risk‑based MFA signals: new device, geo‑velocity, IP reputation, unusual time, breached credentials.\n- MFA recovery: provide single‑use backup codes, encourage multiple factors, and require strong identity verification for resets.\n- Handle failed MFA: offer alternative enrolled methods, notify users of failures, and log context (no secrets).\n\n### Federation and Protocols (OAuth 2.0 / OIDC / SAML)\n- Use standard protocols only; do not build your own.\n- OAuth 2.0/OIDC:\n  - Prefer Authorization Code with PKCE for public/native apps; avoid Implicit and ROPC.\n  - Validate state and nonce; use exact redirect URI matching; prevent open redirects.\n  - Constrain tokens to audience/scope; use DPoP or mTLS for sender‑constraining when possible.\n  - Rotate refresh tokens; revoke on logout or risk signals.\n- SAML:\n  - TLS 1.2+; sign responses/assertions; encrypt sensitive assertions.\n  - Validate issuers, InResponseTo, timestamps (NotBefore/NotOnOrAfter), Recipient; verify against trusted keys.\n  - Prevent XML signature wrapping with strict schema validation and hardened XPath selection.\n  - Keep response lifetimes short; prefer SP‑initiated flows; validate RelayState; implement replay detection.\n\n### Tokens (JWT and Opaque)\n- Prefer opaque server‑managed tokens for simplicity and revocation. If using JWTs:\n  - Explicitly pin algorithms; reject \"none\"; validate iss/aud/exp/iat/nbf; use short lifetimes and rotation.\n  - Store secrets/keys securely (KMS/HSM). Use strong HMAC secrets or asymmetric keys; never hardcode.\n  - Consider binding tokens to a client context (e.g., fingerprint hash in cookie) to reduce replay.\n  - Implement denylist/allowlist for revocation on logout and critical events.\n\n### Recovery and Reset\n- Return the same response for existing and non‑existing accounts (no enumeration). Normalize timing.\n- Generate 32+ byte, CSPRNG tokens; single‑use; store as hashes; short expiry.\n- Use HTTPS reset links to pinned, trusted domains; add referrer policy (no‑referrer) on UI.\n- After reset: require re‑authentication, rotate sessions, and do not auto‑login.\n- Never lock accounts due to reset attempts; rate‑limit and monitor instead.\n\n### Administrative and Internal Accounts\n- Separate admin login from public forms; enforce stronger MFA, device posture checks, IP allowlists, and step‑up auth.\n- Use distinct session contexts and stricter timeouts for admin operations.\n\n### Monitoring and Signals\n- Log auth events (failures/successes, MFA enroll/verify, resets, lockouts) with stable fields and correlation IDs; never log secrets or raw tokens.\n- Detect credential stuffing: high failure rates, many IPs/agents, impossible travel. Notify users of new device logins.\n\n### Implementation Checklist\n- Passwords: Argon2id (preferred) with per‑user salt, constant‑time verify; breached password checks on change/set.\n- MFA: WebAuthn/passkeys or hardware tokens for high‑risk; TOTP as fallback; secure recovery with backup codes.\n- Federation: Authorization Code + PKCE; strict redirect URI validation; audience/scope enforced; token rotation.\n- Tokens: short‑lived, sender‑constrained where possible; revocation implemented; secrets in KMS/HSM.\n- Recovery: single‑use, hashed, time‑boxed tokens; consistent responses; re‑auth required after reset; sessions rotated.\n- Abuse: rate limits, throttling, and anomaly detection on auth endpoints; uniform error handling.\n- Admin: isolated flows with stricter policies and device checks.\n\n### Test Plan\n- Unit/integration tests for login, MFA enroll/verify, resets, and lockouts with uniform errors.\n- Protocol tests: PKCE, state/nonce, redirect URI validation, token audience/scope.\n- Dynamic tests for credential stuffing resistance and token replay; validate revocation after logout and role change.\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-0-authorization-access-control.md",
    "content": "---\ndescription: Authorization and access control (RBAC/ABAC/ReBAC, IDOR, mass assignment, transaction auth)\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\n- yaml\nalwaysApply: false\n---\n\nrule_id: codeguard-0-authorization-access-control\n\n## Authorization & Access Control\n\nEnforce least privilege and precise access decisions for every request and resource, prevent IDOR and mass assignment, and provide strong transaction authorization where necessary.\n\n### Core Principles\n1.  Deny by Default: The default for any access request should be 'deny'. Explicitly grant permissions to roles or users rather than explicitly denying them. When no allow rule matches, return HTTP 403 Forbidden.\n2.  Principle of Least Privilege: Grant users the minimum level of access required to perform their job functions. Regularly audit permissions to ensure they are not excessive.\n3.  Validate Permissions on Every Request: Check authorization for every single request, regardless of source (AJAX, API, direct). Use middleware/filters to ensure consistent enforcement.\n4.  Prefer ABAC/ReBAC over RBAC: Use Attribute-Based Access Control (ABAC) or Relationship-Based Access Control (ReBAC) for fine-grained permissions instead of simple role-based access control.\n\n### Systemic Controls\n- Centralize authorization at service boundaries via middleware/policies/filters.\n- Model permissions at the resource level (ownership/tenancy) and enforce scoping in data queries.\n- Return generic 403/404 responses to avoid leaking resource existence.\n- Log all denials with user, action, resource identifier (non-PII), and rationale code.\n\n### Preventing IDOR\n- Never trust user-supplied identifiers alone. Always verify access to each object instance.\n- Resolve resources through user-scoped queries or server-side lookups. Example: `currentUser.projects.find(id)` instead of `Project.find(id)`.\n- Use non-enumerable identifiers (UUIDs/random) as defense-in-depth. Do not rely on obscurity alone.\n\n### Preventing Mass Assignment\n- Do not bind request bodies directly to domain objects containing sensitive fields.\n- Expose only safe, editable fields via DTOs. Maintain explicit allow-lists for patch/update.\n- Use framework features to block-list sensitive fields if allow-listing is infeasible.\n\n### Transaction Authorization (Step-Up)\n- Require a second factor for sensitive actions (wire transfers, privilege elevation, data export). Apply What‑You‑See‑Is‑What‑You‑Sign: show critical fields for user confirmation.\n- Use unique, time‑limited authorization credentials per transaction; reject on data changes mid‑flow.\n- Enforce the chosen authorization method server-side; prevent client‑side downgrades.\n- Protect against brute-force with throttling and complete flow restarts after failures.\n\n### Testing and Automation\n- Maintain an authorization matrix (YAML/JSON) listing endpoints/resources, roles/attributes, and expected outcomes.\n- Automate integration tests that iterate the matrix, mint role tokens, and assert allow/deny results—including token expiry/revocation cases.\n- Exercise negative tests: swapped IDs, downgraded roles, missing scopes, and bypass attempts.\n\n### Implementation Checklist\n- Middleware/policies enforce deny-by-default and resource checks on every endpoint.\n- Query scoping ensures users only access permitted rows/objects.\n- DTOs and allow-lists prevent mass assignment; sensitive fields never bindable.\n- Step-up authorization in place for sensitive operations with unique, short-lived credentials.\n- Authorization matrix drives CI tests; failures block merges.\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-0-client-side-web-security.md",
    "content": "---\ndescription: Client-side web security (XSS/DOM XSS, CSP, CSRF, clickjacking, XS-Leaks, third-party JS)\nlanguages:\n- c\n- html\n- javascript\n- php\n- typescript\n- vlang\nalwaysApply: false\n---\n\nrule_id: codeguard-0-client-side-web-security\n\n## Client‑side Web Security\n\nProtect browser clients against code injection, request forgery, UI redress, cross‑site leaks, and unsafe third‑party scripts with layered, context‑aware controls.\n\n### XSS Prevention (Context‑Aware)\n- HTML context: prefer `textContent`. If HTML is required, sanitize with a vetted library (e.g., DOMPurify) and strict allow‑lists.\n- Attribute context: always quote attributes and encode values.\n- JavaScript context: do not build JS from untrusted strings; avoid inline event handlers; use `addEventListener`.\n- URL context: validate protocol/domain and encode; block `javascript:` and data URLs where inappropriate.\n- Redirects/forwards: never use user input directly for destinations; use server-side mapping (ID→URL) or validate against trusted domain allowlists.\n- CSS context: allow‑list values; never inject raw style text from users.\n\nExample sanitization:\n```javascript\nconst clean = DOMPurify.sanitize(userHtml, {\n  ALLOWED_TAGS: ['b','i','p','a','ul','li'],\n  ALLOWED_ATTR: ['href','target','rel'],\n  ALLOW_DATA_ATTR: false\n});\n```\n\n### DOM‑based XSS and Dangerous Sinks\n- Prohibit `innerHTML`, `outerHTML`, `document.write` with untrusted data.\n- Prohibit `eval`, `new Function`, string‑based `setTimeout/Interval`.\n- Validate and encode data before assigning to `location` or event handler properties.\n- Use strict mode and explicit variable declarations to prevent global namespace pollution from DOM clobbering.\n- Adopt Trusted Types and enforce strict CSP to prevent DOM sinks exploitation.\n\nTrusted Types + CSP:\n```http\nContent-Security-Policy: script-src 'self' 'nonce-{random}'; object-src 'none'; base-uri 'self'; require-trusted-types-for 'script'\n```\n\n### Content Security Policy (CSP)\n- Prefer nonce‑based or hash‑based CSP over domain allow‑lists.\n- Start with Report‑Only mode; collect violations; then enforce.\n- Baseline to aim for: `default-src 'self'; style-src 'self' 'unsafe-inline'; frame-ancestors 'self'; form-action 'self'; object-src 'none'; base-uri 'none'; upgrade-insecure-requests`.\n\n### CSRF Defense\n- Fix XSS first; then layer CSRF defenses.\n- Use framework‑native CSRF protections and synchronizer tokens on all state‑changing requests.\n- Cookie settings: `SameSite=Lax` or `Strict`; sessions `Secure` and `HttpOnly`; use `__Host-` prefix when possible.\n- Validate Origin/Referer; require custom headers for API mutations in SPA token models.\n- Never use GET for state changes; validate tokens on POST/PUT/DELETE/PATCH only. Enforce HTTPS for all token transmission.\n\n### Clickjacking Defense\n- Primary: `Content-Security-Policy: frame-ancestors 'none'` or a specific allow‑list.\n- Fallback for legacy browsers: `X-Frame-Options: DENY` or `SAMEORIGIN`.\n- Consider UX confirmations for sensitive actions when framing is required.\n\n### Cross‑Site Leaks (XS‑Leaks) Controls\n- Use `SameSite` cookies appropriately; prefer `Strict` for sensitive actions.\n- Adopt Fetch Metadata protections to block suspicious cross‑site requests.\n- Isolate browsing contexts: COOP/COEP and CORP where applicable.\n- Disable caching and add user‑unique tokens for sensitive responses to prevent cache probing.\n\n### Third‑Party JavaScript\n- Minimize and isolate: prefer sandboxed iframes with `sandbox` and postMessage origin checks.\n- Use Subresource Integrity (SRI) for external scripts and monitor for changes.\n- Provide a first‑party, sanitized data layer; deny direct DOM access from tags where possible.\n- Govern via tag manager controls and vendor contracts; keep libraries updated.\n\nSRI example:\n```html\n<script src=\"https://cdn.vendor.com/app.js\"\n  integrity=\"sha384-...\" crossorigin=\"anonymous\"></script>\n```\n\n### HTML5, CORS, WebSockets, Storage\n- postMessage: always specify exact target origin; verify `event.origin` on receive.\n- CORS: avoid `*`; allow‑list origins; validate preflights; do not rely on CORS for authz.\n- WebSockets: require `wss://`, origin checks, auth, message size limits, and safe JSON parsing.\n- Client storage: never store secrets in `localStorage`/`sessionStorage`; prefer HttpOnly cookies; if unavoidable, isolate via Web Workers.\n- Links: add `rel=\"noopener noreferrer\"` to external `target=_blank` links.\n\n### HTTP Security Headers (Client Impact)\n- HSTS: enforce HTTPS everywhere.\n- X‑Content‑Type‑Options: `nosniff`.\n- Referrer‑Policy and Permissions‑Policy: restrict sensitive signals and capabilities.\n\n### AJAX and Safe DOM APIs\n- Avoid dynamic code execution; use function callbacks, not strings.\n- Build JSON with `JSON.stringify`; never via string concatenation.\n- Prefer creating elements and setting `textContent`/safe attributes over raw HTML insertion.\n\n### Implementation Checklist\n- Contextual encoding/sanitization for every sink; no dangerous APIs without guards.\n- Strict CSP with nonces and Trusted Types; violations monitored.\n- CSRF tokens on all state‑changing requests; secure cookie attributes.\n- Frame protections set; XS‑Leak mitigations enabled (Fetch Metadata, COOP/COEP/CORP).\n- Third‑party JS isolated with SRI and sandbox; vetted data layer only.\n- HTML5/CORS/WebSocket usage hardened; no secrets in web storage.\n- Security headers enabled and validated.\n\n### Test Plan\n- Automated checks for dangerous DOM/API patterns.\n- E2E tests for CSRF and clickjacking; CSP report monitoring.\n- Manual probes for XS‑Leaks (frame count, timing, cache) and open redirect behavior.\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-0-cloud-orchestration-kubernetes.md",
    "content": "---\ndescription: Kubernetes hardening (RBAC, admission policies, network policies, secrets, supply chain)\nlanguages:\n- javascript\n- yaml\nalwaysApply: false\n---\n\nrule_id: codeguard-0-cloud-orchestration-kubernetes\n\n## Cloud & Orchestration (Kubernetes)\n\nKubernetes cluster and workload hardening: identity, policy, networking, secrets, and supply chain controls.\n\n### Controls\n- Identity & RBAC: least privilege for users and service accounts; separate namespaces; bind only needed roles.\n- Policy: admission controls (OPA/Gatekeeper/Kyverno) for image sources, capabilities, root, network policies, and required labels/annotations.\n- Networking: default‑deny with network policies; explicit egress allow‑lists; service identity/mTLS within mesh where applicable.\n- Secrets: use KMS providers; avoid plaintext in manifests; rotate regularly; restrict secret mount paths.\n- Nodes: hardened OS, auto‑updates, minimal attack surface; isolate sensitive workloads with taints/tolerations and dedicated nodes.\n- Supply chain: verify image signatures; enforce provenance (SLSA/Sigstore) in admission.\n\n### Checklist\n- Namespaces per team/app; RBAC roles scoped; audit logging enabled.\n- Admission policies enforce image provenance, non‑root, dropped capabilities, read‑only root FS, and network policy presence.\n- Network policies in place for ingress/egress; service accounts scoped per deployment.\n\n### Verification\n- Cluster conformance and CIS benchmark scans.\n- Policy tests in CI for manifests (OPA unit tests); periodic admission dry‑run.\n\n### Incident Readiness\n- Enable audit logs and centralize; restrict access to etcd; backup/restore tested.\n- Define break‑glass roles with MFA and time‑bound approvals.\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-0-data-storage.md",
    "content": "---\ndescription: Data & storage security (DB isolation, TLS, least privilege, RLS/CLS, backups, auditing)\nlanguages:\n- c\n- javascript\n- sql\n- yaml\nalwaysApply: false\n---\n\nrule_id: codeguard-0-data-storage\n\n## Database Security Guidelines\n\nThis rule advises on securely configuring SQL and NoSQL databases to protect against data breaches and unauthorized access:\n\n- Backend Database Protection\n  - Isolate database servers from other systems and limit host connections.\n  - Disable network (TCP) access when possible; use local socket files or named pipes.\n  - Configure database to bind only on localhost when appropriate.\n  - Restrict network port access to specific hosts with firewall rules.\n  - Place database server in separate DMZ isolated from application server.\n  - Never allow direct connections from thick clients to backend database.\n\n- Transport Layer Security\n  - Configure database to only allow encrypted connections.\n  - Install trusted digital certificates on database servers.\n  - Use TLSv1.2+ with modern ciphers (AES-GCM, ChaCha20) for client connections.\n  - Verify digital certificate validity in client applications.\n  - Ensure all database traffic is encrypted, not just initial authentication.\n\n- Secure Authentication Configuration\n  - Always require authentication, including from local server connections.\n  - Protect accounts with strong, unique passwords.\n  - Use dedicated accounts per application or service.\n  - Configure minimum required permissions only.\n  - Regularly review accounts and permissions.\n  - Remove accounts when applications are decommissioned.\n  - Change passwords when staff leave or compromise is suspected.\n\n- Database Credential Storage\n  - Never store credentials in application source code.\n  - Store credentials in configuration files outside web root.\n  - Set appropriate file permissions for credential access.\n  - Never check credential files into source code repositories.\n  - Encrypt credential storage using built-in functionality when available.\n  - Use environment variables or secrets management solutions.\n\n- Secure Permission Management\n  - Apply principle of least privilege to all database accounts.\n  - Do not use built-in root, sa, or SYS accounts.\n  - Do not grant administrative rights to application accounts.\n  - Restrict account connections to allowed hosts only.\n  - Use separate databases and accounts for Development, UAT, and Production.\n  - Grant only required permissions (SELECT, UPDATE, DELETE as needed).\n  - Avoid making accounts database owners to prevent privilege escalation.\n  - Implement table-level, column-level, and row-level permissions when needed.\n\n- Database Configuration and Hardening\n  - Install required security updates and patches regularly.\n  - Run database services under low-privileged user accounts.\n  - Remove default accounts and sample databases.\n  - Store transaction logs on separate disk from main database files.\n  - Configure regular encrypted database backups with proper permissions.\n  - Disable unnecessary stored procedures and dangerous features.\n  - Implement database activity monitoring and alerting.\n\n- Platform-Specific Hardening\n  - SQL Server: Disable xp_cmdshell, CLR execution, SQL Browser service, Mixed Mode Authentication (unless required).\n  - MySQL/MariaDB: Run mysql_secure_installation, disable FILE privilege for users.\n  - PostgreSQL: Follow PostgreSQL security documentation guidelines.\n  - MongoDB: Implement MongoDB security checklist requirements.\n  - Redis: Follow Redis security guide recommendations.\n\nSummary:  \nIsolate database systems, enforce encrypted connections, implement strong authentication, store credentials securely using secrets management, apply least privilege permissions, harden database configurations, and maintain regular security updates and monitoring.\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-0-devops-ci-cd-containers.md",
    "content": "---\ndescription: DevOps, CI/CD, and containers (pipeline hardening, artifacts, Docker/K8s images, virtual patching, toolchain)\nlanguages:\n- docker\n- javascript\n- powershell\n- shell\n- xml\n- yaml\nalwaysApply: false\n---\n\nrule_id: codeguard-0-devops-ci-cd-containers\n\n## DevOps, CI/CD, and Containers\n\nSecure the build, packaging, and deployment supply chain: protect pipelines and artifacts, harden containers, and use virtual patching and toolchain flags when necessary.\n\n### CI/CD Pipeline Security\n- Repos: protected branches; mandatory reviews; signed commits.\n- Secrets: never hardcode; fetch at runtime from vault/KMS; mask in logs.\n- Least privilege: ephemeral, isolated runners with minimal permissions.\n- Security gates in CI: SAST, SCA, DAST, IaC scanning; block on criticals.\n- Dependencies: pin via lockfiles; verify integrity; use private registries.\n- Sign everything: commits and artifacts (containers/jars) and verify prior to deploy; adopt SLSA provenance.\n\n### Docker and Container Hardening\n- User: run as non‑root; set `USER` in Dockerfile\n- Use `--security-opt=no-new-privileges` to prevent privilege escalation.\n- Capabilities: `--cap-drop all` and add only what you need; never `--privileged`.\n- Daemon socket: never mount `/var/run/docker.sock`\n- DO NOT enable TCP Docker daemon socket (`-H tcp://0.0.0.0:XXX`) without TLS.\n- Avoid `- \"/var/run/docker.sock:/var/run/docker.sock\"` in docker-compose files.\n- Filesystems: read‑only root, tmpfs for temp write; resource limits (CPU/mem).\n- Networks: avoid host network; define custom networks; limit exposed ports.\n- Images: minimal base (distroless/alpine), pin tags and digests; remove package managers and tools from final image; add `HEALTHCHECK`.\n- Secrets: Docker/Kubernetes secrets; never in layers/env; mount via runtime secrets.\n- Scanning: scan images on build and admission; block high‑severity vulns.\n\n### Node.js in Containers\n- Deterministic builds: `npm ci --omit=dev`; pin base image with digest.\n- Production env: `ENV NODE_ENV=production`.\n- Non‑root: copy with correct ownership and drop to `USER node`.\n- Signals: use an init (e.g., `dumb-init`) and implement graceful shutdown handlers.\n- Multi‑stage builds: separate build and runtime; mount secrets via BuildKit; use `.dockerignore`.\n\n### Virtual Patching (Temporary Mitigation)\n- Use WAF/IPS/ModSecurity for immediate protection when code fixes are not yet possible.\n- Prefer positive security rules (allow‑list) for accuracy; avoid exploit‑specific signatures.\n- Process: prepare tooling in advance; analyze CVEs; implement patches in log‑only first, then enforce; track and retire after code fix.\n\n### C/C++ Toolchain Hardening (when applicable)\n- Compiler: `-Wall -Wextra -Wconversion`, `-fstack-protector-all`, PIE (`-fPIE`/`-pie`), `_FORTIFY_SOURCE=2`, CFI (`-fsanitize=cfi` with LTO).\n- Linker: RELRO/now, noexecstack, NX/DEP and ASLR.\n- Debug vs Release: enable sanitizers in debug; enable hardening flags in release; assert in debug only.\n- CI checks: verify flags (`checksec`) and fail builds if protections missing.\n\n### Implementation Checklist\n- Pipeline: secrets in vault; ephemeral runners; security scans; signed artifacts with provenance.\n- Containers: non‑root, least privilege, read‑only FS, resource limits; no daemon socket mounts.\n- Images: minimal, pinned, scanned; healthchecks; `.dockerignore` maintained.\n- Node images: `npm ci`, `NODE_ENV=production`, proper init and shutdown.\n- Virtual patching: defined process; accurate rules; logs; retirement after fix.\n- Native builds: hardening flags enabled and verified in CI.\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-0-file-handling-and-uploads.md",
    "content": "---\ndescription: Secure file handling & uploads (validation, storage isolation, scanning, safe delivery)\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\nalwaysApply: false\n---\n\nrule_id: codeguard-0-file-handling-and-uploads\n\n## File Upload Security Guidelines\n\nThis rule advises on secure file upload practices to prevent malicious file attacks and protect system integrity:\n\n- Extension Validation\n  - List allowed extensions only for business-critical functionality.\n  - Ensure input validation is applied before validating extensions.\n  - Avoid double extensions (e.g., `.jpg.php`) and null byte injection (e.g., `.php%00.jpg`).\n  - Use allowlist approach rather than denylist for file extensions.\n  - Validate extensions after decoding filename to prevent bypass attempts.\n\n- Content Type and File Signature Validation\n  - Never trust client-supplied Content-Type headers as they can be spoofed.\n  - Validate file signatures (magic numbers) in conjunction with Content-Type checking.\n  - Implement allowlist approach for MIME types as a quick protection layer.\n  - Use file signature validation but not as a standalone security measure.\n\n- Filename Security\n  - Generate random filenames (UUID/GUID) instead of using user-supplied names.\n  - If user filenames required, implement maximum length limits.\n  - Restrict characters to alphanumeric, hyphens, spaces, and periods only.\n  - Prevent leading periods (hidden files) and sequential periods (directory traversal).\n  - Avoid leading hyphens or spaces for safer shell script processing.\n\n- File Content Validation\n  - For images, apply image rewriting techniques to destroy malicious content.\n  - For Microsoft documents, use Apache POI for validation.\n  - Avoid ZIP files due to numerous attack vectors.\n  - Implement manual file review in sandboxed environments when resources allow.\n  - Integrate antivirus scanning and Content Disarm & Reconstruct (CDR) for applicable file types.\n\n- Storage Security\n  - Store files on different servers for complete segregation when possible.\n  - Store files outside webroot with administrative access only.\n  - If storing in webroot, set write-only permissions with proper access controls.\n  - Use application handlers that map IDs to filenames for public access.\n  - Consider database storage for specific use cases with DBA expertise.\n\n- Access Control and Authentication\n  - Require user authentication before allowing file uploads.\n  - Implement proper authorization levels for file access and modification.\n  - Set filesystem permissions on principle of least privilege.\n  - Scan files before execution if execution permission is required.\n\n- Upload and Download Limits\n  - Set proper file size limits for upload protection.\n  - Consider post-decompression size limits for compressed files.\n  - Implement request limits for download services to prevent DoS attacks.\n  - Use secure methods to calculate ZIP file sizes safely.\n\n- Additional Security Measures\n  - Protect file upload endpoints from CSRF attacks.\n  - Keep all file processing libraries securely configured and updated.\n  - Implement logging and monitoring for upload activities.\n  - Provide user reporting mechanisms for illegal content.\n  - Use secure extraction methods for compressed files.\n\nSummary:  \nImplement defense-in-depth for file uploads through multi-layered validation, secure storage practices, proper access controls, and comprehensive monitoring. Never rely on single validation methods and always generate safe filenames to prevent attacks.\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-0-framework-and-languages.md",
    "content": "---\ndescription: Framework & language security guides (Django/DRF, Laravel/Symfony/Rails, .NET, Java/JAAS, Node.js, PHP config)\nlanguages:\n- c\n- java\n- javascript\n- kotlin\n- php\n- python\n- ruby\n- typescript\n- xml\n- yaml\nalwaysApply: false\n---\n\nrule_id: codeguard-0-framework-and-languages\n\n## Framework & Language Guides\n\nApply secure‑by‑default patterns per platform. Harden configurations, use built‑in protections, and avoid common pitfalls.\n\n### Django\n- Disable DEBUG in production; keep Django and deps updated.\n- Enable `SecurityMiddleware`, clickjacking middleware, MIME sniffing protection.\n- Force HTTPS (`SECURE_SSL_REDIRECT`); configure HSTS; set secure cookie flags (`SESSION_COOKIE_SECURE`, `CSRF_COOKIE_SECURE`).\n- CSRF: ensure `CsrfViewMiddleware` and `{% csrf_token %}` in forms; proper AJAX token handling.\n- XSS: rely on template auto‑escaping; avoid `mark_safe` unless trusted; use `json_script` for JS.\n- Auth: use `django.contrib.auth`; validators in `AUTH_PASSWORD_VALIDATORS`.\n- Secrets: generate via `get_random_secret_key`; store in env/secrets manager.\n\n### Django REST Framework (DRF)\n- Set `DEFAULT_AUTHENTICATION_CLASSES` and restrictive `DEFAULT_PERMISSION_CLASSES`; never leave `AllowAny` for protected endpoints.\n- Always call `self.check_object_permissions(request, obj)` for object‑level authz.\n- Serializers: explicit `fields=[...]`; avoid `exclude` and `\"__all__\"`.\n- Throttling: enable rate limits (and/or at gateway/WAF).\n- Disable unsafe HTTP methods where not needed. Avoid raw SQL; use ORM/parameters.\n\n### Laravel\n- Production: `APP_DEBUG=false`; generate app key; secure file perms.\n- Cookies/sessions: enable encryption middleware; set `http_only`, `same_site`, `secure`, short lifetimes.\n- Mass assignment: use `$request->only()` / `$request->validated()`; avoid `$request->all()`.\n- SQLi: use Eloquent parameterization; validate dynamic identifiers.\n- XSS: rely on Blade escaping; avoid `{!! ... !!}` for untrusted data.\n- File uploads: validate `file`, size, and `mimes`; sanitize filenames with `basename`.\n- CSRF: ensure middleware and form tokens enabled.\n\n### Symfony\n- XSS: Twig auto‑escaping; avoid `|raw` unless trusted.\n- CSRF: use `csrf_token()` and `isCsrfTokenValid()` for manual flows; Forms include tokens by default.\n- SQLi: Doctrine parameterized queries; never concatenate inputs.\n- Command execution: avoid `exec/shell_exec`; use Filesystem component.\n- Uploads: validate with `#[File(...)]`; store outside public; unique names.\n- Directory traversal: validate `realpath`/`basename` and enforce allowed roots.\n- Sessions/security: configure secure cookies and authentication providers/firewalls.\n\n### Ruby on Rails\n- Avoid dangerous functions:\n\n```ruby\neval(\"ruby code here\")\nsystem(\"os command here\")\n`ls -al /` # (backticks contain os command)\nexec(\"os command here\")\nspawn(\"os command here\")\nopen(\"| os command here\")\nProcess.exec(\"os command here\")\nProcess.spawn(\"os command here\")\nIO.binread(\"| os command here\")\nIO.binwrite(\"| os command here\", \"foo\")\nIO.foreach(\"| os command here\") {}\nIO.popen(\"os command here\")\nIO.read(\"| os command here\")\nIO.readlines(\"| os command here\")\nIO.write(\"| os command here\", \"foo\")\n```\n\n- SQLi: always parameterize; use `sanitize_sql_like` for LIKE patterns.\n- XSS: default auto‑escape; avoid `raw`, `html_safe` on untrusted data; use `sanitize` allow‑lists.\n- Sessions: database‑backed store for sensitive apps; force HTTPS (`config.force_ssl = true`).\n- Auth: use Devise or proven libraries; configure routes and protected areas.\n- CSRF: `protect_from_forgery` for state‑changing actions.\n- Secure redirects: validate/allow‑list targets.\n- Headers/CORS: set secure defaults; configure `rack-cors` carefully.\n\n### .NET (ASP.NET Core)\n- Keep runtime and NuGet packages updated; enable SCA in CI.\n- Authz: use `[Authorize]` attributes; perform server‑side checks; prevent IDOR.\n- Authn/sessions: ASP.NET Identity; lockouts; cookies `HttpOnly`/`Secure`; short timeouts.\n- Crypto: use PBKDF2 for passwords, AES‑GCM for encryption; DPAPI for local secrets; TLS 1.2+.\n- Injection: parameterize SQL/LDAP; validate with allow‑lists.\n- Config: enforce HTTPS redirects; remove version headers; set CSP/HSTS/X‑Content‑Type‑Options.\n- CSRF: anti‑forgery tokens on state‑changing actions; validate on server.\n\n### Java and JAAS\n- SQL/JPA: use `PreparedStatement`/named parameters; never concatenate input.\n- XSS: allow‑list validation; sanitize output with reputable libs; encode for context.\n- Logging: parameterized logging to prevent log injection.\n- Crypto: AES‑GCM; secure random nonces; never hardcode keys; use KMS/HSM.\n- JAAS: configure `LoginModule` stanzas; implement `initialize/login/commit/abort/logout`; avoid exposing credentials; segregate public/private credentials; manage subject principals properly.\n\n### Node.js\n- Limit request sizes; validate and sanitize input; escape output.\n- Avoid `eval`, `child_process.exec` with user input; use `helmet` for headers; `hpp` for parameter pollution.\n- Rate limit auth endpoints; monitor event loop health; handle uncaught exceptions cleanly.\n- Cookies: set `secure`, `httpOnly`, `sameSite`; set `NODE_ENV=production`.\n- Keep packages updated; run `npm audit`; use security linters and ReDoS testing.\n\n### PHP Configuration\n- Production php.ini: `expose_php=Off`, log errors not display; restrict `allow_url_fopen/include`; set `open_basedir`.\n- Disable dangerous functions; set session cookie flags (`Secure`, `HttpOnly`, `SameSite=Strict`); enable strict session mode.\n- Constrain upload size/number; set resource limits (memory, post size, execution time).\n- Use Snuffleupagus or similar for additional hardening.\n\n### Implementation Checklist\n- Use each framework’s built‑in CSRF/XSS/session protections and secure cookie flags.\n- Parameterize all data access; avoid dangerous OS/exec functions with untrusted input.\n- Enforce HTTPS/HSTS; set secure headers.\n- Centralize secret management; never hardcode secrets; lock down debug in production.\n- Validate/allow‑list redirects and dynamic identifiers.\n- Keep dependencies and frameworks updated; run SCA and static analysis regularly.\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-0-iac-security.md",
    "content": "---\ndescription: Infrastructure as Code Security\nlanguages:\n- c\n- d\n- javascript\n- powershell\n- ruby\n- shell\n- yaml\nalwaysApply: false\n---\n\nrule_id: codeguard-0-iac-security\n\n# Infrastructure as Code (IaC) Security\n\nWhen designing cloud infrastructure and writing Infrastructure as Code (IaC) in languages like Terraform and CloudFormation, always use secure practices and defaults such as preventing public exposure and follow the principle of least privilege. Actively identify security misconfigurations and provide secure alternatives.\n\n## Critical Security Patterns In Infrastructure as Code\n\n### Network security\n- **ALWAYS** restrict the access to remote administrative services, databases, LDAP, TACACS+, or other sensitive services. No service should be accessible from the entire Internet if it does not need to be. Instead, restrict access to a specific set of IP addresses or CIDR blocks which require access.\n    - Security Group and ACL inbound rules should **NEVER** allow `0.0.0.0/0` to remote administration ports (such as SSH 22, RDP 3389).\n    - Security Group and ACL inbound rules should **NEVER** allow `0.0.0.0/0` to database ports (such as 3306, 5432, 1433, 1521, 27017).\n    - Kubernetes API endpoints allow lists should **NEVER** allow `0.0.0.0/0`. EKS, AKS, GKE, and any other Kubernetes API endpoint should be restricted to an allowed list of CIDR addresses which require administrative access.\n    - **NEVER** expose cloud platform database services (RDS, Azure SQL, Cloud SQL) to all IP addresses `0.0.0.0/0`.\n- Generally prefer private networking, such as internal VPC, VNET, VPN, or other internal transit unless public network access is required.\n- **ALWAYS** enable VPC/VNET flow logs for network monitoring and security analysis.\n- **ALWAYS** implement default deny rules and explicit allow rules for required traffic only.\n- Generally prefer blocking egress traffic to the Internet by default. If egress is required appropriate traffic control solutions might include:\n    - Egress firewall or proxy with rules allowing access to specific required services.\n    - Egress security group (SG) or access control list (ACL) with rules allowing access to specific required IPs or CIDR blocks.\n    - DNS filtering to prevent access to malicious domains.\n\n### Data protection\n- **ALWAYS** configure data encryption at rest for all storage services including databases, file systems, object storage, and block storage.\n    - Enable encryption for cloud storage services (S3, Azure Blob, GCS buckets).\n    - Configure database encryption at rest for all database engines (RDS, Azure SQL, Cloud SQL, DocumentDB, etc.).\n    - Enable EBS/disk encryption for virtual machine storage volumes.\n- **ALWAYS** configure encryption in transit for all data communications.\n    - Use TLS 1.2 or higher for all HTTPS/API communications.\n    - Configure SSL/TLS for database connections with certificate validation.\n    - Enable encryption for inter-service communication within VPCs/VNETs.\n    - Use encrypted protocols for remote access (SSH, HTTPS, secure RDP).\n- **ALWAYS** implement data classification and protection controls based on sensitivity levels.\n    - Apply stricter encryption and access controls for PII, PHI, financial data, and intellectual property.\n    - Use separate encryption keys for different data classification levels.\n- **ALWAYS** configure secure data retention and disposal policies.\n    - Define data retention periods based on regulatory and business requirements.\n    - Implement automated data lifecycle management with secure deletion.\n- **ALWAYS** enable comprehensive data access monitoring and auditing.\n    - Log all data access, modification, and deletion operations.\n    - Monitor for unusual data access patterns and potential data exfiltration.\n    - Implement real-time alerting for sensitive data access violations.\n- **ALWAYS** encrypt data backups.\n    - Encrypt all backup data using separate encryption keys from production data.\n    - Store backups in geographically distributed locations with appropriate access controls.\n    - Test backup restoration procedures regularly and verify backup integrity.\n\n### Access control\n- **NEVER** leave critical administration or data services with anonymous access (backups, storage, container registries, file shares) unless otherwise labeled as public classification or intended to be public.\n- **NEVER** use wildcard permissions in IAM policies or cloud RBAC (`\"Action\": \"*\"`, `\"Resource\": \"*\"`)\n- **NEVER** overprivilege service accounts with Owner/Admin roles when it is not necessary.\n- **NEVER** use service API Keys and client secrets and instead use workload identity with role-based access control to eliminate the need for long-lived credentials.\n- **NEVER** enable or use the legacy Instance Metadata Service version 1 (IMDSv1) in AWS.\n- **NEVER** use legacy or outdated authentication methods (such as local users) when there is a more secure alternative such as OAuth.\n\n### Container and VM images\n- **NEVER** use non-hardened VM and container images.\n- **ALWAYS** choose distroless or minimal container images.\n- **RECOMMEND** using secure baseline virtual machine images from trusted sources.\n- **RECOMMEND** using minimal distroless container images from trusted sources.\n\n### Logging and administrative access\n- **NEVER** disable administrative activity logging for sensitive services.\n- **ALWAYS** enable audit logging for privileged operations.\n\n### Secrets management\n- **NEVER** hardcode secrets, passwords, API keys, or certificates directly in IaC source code.\n- **ALWAYS** in Terraform mark secrets with \"sensitive = true\", in other IaC code use appropriate annotations or metadata to indicate sensitive values.\n\n### Backup and data recovery\n- **NEVER** create backups without encryption at rest and in transit.\n- **ALWAYS** configure multi-region data storage for backups with cross-region replication.\n- **NEVER** configure backups without retention policies and lifecycle management.\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-0-input-validation-injection.md",
    "content": "---\ndescription: Input validation and injection defense (SQL/SOQL/LDAP/OS), parameterization, prototype pollution\nlanguages:\n- apex\n- c\n- go\n- html\n- java\n- javascript\n- php\n- powershell\n- python\n- ruby\n- shell\n- sql\n- typescript\nalwaysApply: false\n---\n\nrule_id: codeguard-0-input-validation-injection\n\n## Input Validation & Injection Defense\n\nEnsure untrusted input is validated and never interpreted as code. Prevent injection across SQL, LDAP, OS commands, templating, and JavaScript runtime object graphs.\n\n### Core Strategy\n- Validate early at trust boundaries with positive (allow‑list) validation and canonicalization.\n- Treat all untrusted input as data, never as code. Use safe APIs that separate code from data.\n- Parameterize queries/commands; escape only as last resort and context‑specific.\n\n### Validation Playbook\n- Syntactic validation: enforce format, type, ranges, and lengths for each field.\n- Semantic validation: enforce business rules (e.g., start ≤ end date, enum allow‑lists).\n- Normalization: canonicalize encodings before validation; validate complete strings (regex anchors ^$); beware ReDoS.\n- Free‑form text: define character class allow‑lists; normalize Unicode; set length bounds.\n- Files: validate by content type (magic), size caps, and safe extensions; server‑generate filenames; scan; store outside web root.\n\n### SQL Injection Prevention\n- Use prepared statements and parameterized queries for 100% of data access.\n- Use bind variables for any dynamic SQL construction within stored procedures and never concatenate user input into SQL.\n- Prefer least‑privilege DB users and views; never grant admin to app accounts.\n- Escaping is fragile and discouraged; parameterization is the primary defense.\n\nExample (Java PreparedStatement):\n```java\nString custname = request.getParameter(\"customerName\");\nString query = \"SELECT account_balance FROM user_data WHERE user_name = ? \";  \nPreparedStatement pstmt = connection.prepareStatement( query );\npstmt.setString( 1, custname);\nResultSet results = pstmt.executeQuery( );\n```\n\n### SOQL/SOSL Injection (Salesforce)\n\nSOQL and SOSL are query/search languages (no SQL-style DDL/DML). Data changes are performed via Apex DML or Database methods. Note: SOQL can lock rows via `FOR UPDATE`.\n\n- Primary risk: data exfiltration by bypassing intended query filters/business logic; impact is amplified when Apex runs with elevated access (system mode) or when CRUD/FLS aren't enforced.\n- Second-order risk (conditional): if queried records are passed to DML, injection can broaden the record set and cause unintended mass updates/deletes.\n- Prefer static SOQL/SOSL with bind variables: `[SELECT Id FROM Account WHERE Name = :userInput]` or `FIND :term`.\n- For dynamic SOQL, use `Database.queryWithBinds()`; for dynamic SOSL, use `Search.query()`. Allow‑list any dynamic identifiers. If concatenation is unavoidable, escape string values with `String.escapeSingleQuotes()`.\n- Enforce CRUD/FLS with `WITH USER_MODE` or `WITH SECURITY_ENFORCED` (don't combine both). Enforce record sharing with `with sharing` or user-mode operations. Use `Security.stripInaccessible()` before DML.\n\n### LDAP Injection Prevention\n- Always apply context‑appropriate escaping:\n  - DN escaping for `\\ # + < > , ; \" =` and leading/trailing spaces\n  - Filter escaping for `* ( ) \\ NUL`\n- Validate inputs with allow‑lists before constructing queries; use libraries that provide DN/filter encoders.\n- Use least‑privilege LDAP connections with bind authentication; avoid anonymous binds for application queries.\n\n### OS Command Injection Defense\n- Prefer built‑in APIs instead of shelling out (e.g., library calls over `exec`).\n- If unavoidable, use structured execution that separates command and arguments (e.g., ProcessBuilder). Do not invoke shells.\n- Strictly allow‑list commands and validate arguments with allow‑list regex; exclude metacharacters (& | ; $ > < ` \\ ! ' \" ( ) and whitespace as needed).\n- Use `--` to delimit arguments where supported to prevent option injection.\n\nExample (Java ProcessBuilder):\n```java\nProcessBuilder pb = new ProcessBuilder(\"TrustedCmd\", \"Arg1\", \"Arg2\");\nMap<String,String> env = pb.environment();\npb.directory(new File(\"TrustedDir\"));\nProcess p = pb.start();\n```\n\n### Query Parameterization Guidance\n- Use the platform’s parameterization features (JDBC PreparedStatement, .NET SqlCommand, Ruby ActiveRecord bind params, PHP PDO, SQLx bind, etc.).\n- For stored procedures, ensure parameters are bound; never build dynamic SQL via string concatenation inside procedures.\n\n### Prototype Pollution (JavaScript)\n- Developers should use `new Set()` or `new Map()` instead of using object literals\n- When objects are required, create with `Object.create(null)` or `{ __proto__: null }` to avoid inherited prototypes.\n- Freeze or seal objects that should be immutable; consider Node `--disable-proto=delete` as defense‑in‑depth.\n- Avoid unsafe deep merge utilities; validate keys against allow‑lists and block `__proto__`, `constructor`, `prototype`.\n\n### Caching and Transport\n- Apply `Cache-Control: no-store` on responses containing sensitive data; enforce HTTPS across data flows.\n\n### Implementation Checklist\n- Central validators: types, ranges, lengths, enums; canonicalization before checks.\n- 100% parameterization coverage for SQL; dynamic identifiers via allow‑lists only.\n- LDAP DN/filter escaping in use; inputs validated prior to query.\n- No shell invocation for untrusted input; if unavoidable, structured exec + allow‑list + regex validation.\n- JS object graph hardened: safe constructors, blocked prototype paths, safe merge utilities.\n- File uploads validated by content, size, and extension; stored outside web root and scanned.\n\n### Test Plan\n- Static checks for string concatenation in queries/commands and dangerous DOM/merge sinks.\n- Fuzzing for SQL/LDAP/OS injection vectors; unit tests for validator edge cases.\n- Negative tests exercising blocked prototype keys and deep merge behavior.\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-0-logging.md",
    "content": "---\ndescription: Logging & monitoring (structured telemetry, redaction, integrity, detection & alerting)\nlanguages:\n- c\n- javascript\n- yaml\nalwaysApply: false\n---\n\nrule_id: codeguard-0-logging\n\n## Logging & Monitoring\n\nProduce structured, privacy‑aware telemetry that supports detection, response, and forensics without exposing secrets.\n\n### What to Log\n- Authn/authz events; admin actions; config changes; sensitive data access; input validation failures; security errors.\n- Include correlation/request IDs, user/session IDs (non‑PII), source IP, user agent, timestamps (UTC, RFC3339).\n\n### How to Log\n- Structured logs (JSON) with stable field names; avoid free‑form text for critical signals.\n- Sanitize all log inputs to prevent log injection (strip CR/LF/delimiters); validate data from other trust zones.\n- Redact/tokenize secrets and sensitive fields; never log credentials, tokens, recovery codes, or raw session IDs.\n- Ensure integrity: append‑only or WORM storage; tamper detection; centralized aggregation; access controls and retention policies.\n\n### Detection & Alerting\n- Build alerts for auth anomalies (credential stuffing patterns, impossible travel), privilege changes, excessive failures, SSRF indicators, and data exfil patterns.\n- Tune thresholds; provide runbooks; ensure on‑call coverage; test alert flows.\n\n### Storage & Protection\n- Isolate log storage (separate partition/database); strict file/directory permissions; store outside web‑accessible locations.\n- Synchronize time across systems; use secure protocols for transmission; implement tamper detection and monitoring.\n\n### Privacy & Compliance\n- Maintain data inventory and classification; minimize personal data in logs; honor retention and deletion policies.\n- Provide mechanisms to trace and delete user‑linked log data where required by policy.\n\n### Implementation Checklist\n- JSON logging enabled; log injection sanitization active; redaction filters active; correlation IDs on all requests.\n- Isolated log storage with tamper detection; centralized log pipeline with integrity protections; retention configured.\n- Security alerts defined and tested; dashboards and reports in place.\n\n### Validation\n- Unit/integration tests assert presence/absence of key fields; redaction unit tests.\n- Periodic audits for secret/PII leakage; tabletop exercises for incident workflows.\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-0-mcp-security.md",
    "content": "---\ndescription: MCP (Model Context Protocol) Security based on CoSAI MCP Security guidelines\nlanguages:\n- python\n- javascript\n- typescript\n- go\n- rust\n- java\nalwaysApply: false\n---\n\nrule_id: codeguard-0-mcp-security\n\n# MCP (Model Context Protocol) Security Guidelines\n\nNEVER deploy MCP servers or clients without implementing proper security controls.\n\n### Workload Identity and Authentication\n- Use SPIFFE/SPIRE for cryptographic workload identities\n  - SPIFFE (Secure Production Identity Framework For Everyone) provides a standard for service identity\n  - SPIRE (SPIFFE Runtime Environment) issues and rotates short-lived cryptographic identities (SVIDs)\n\n### Input and Data Sanitization\n- Validate ALL inputs using allowlists at every trust boundary\n- Sanitize file paths through canonicalization\n- Use parameterized queries for database operations\n- Apply context-aware output encoding (SQL, shell, HTML)\n- Sanitize tool outputs: return only minimum fields, redact all PII and sensitive data\n- Treat ALL inputs, tool schemas, metadata, prompts, and resource content as untrusted input\n- Deploy prompt injection detection systems\n- Use strict JSON schemas to maintain boundaries between instructions and data\n\n### Sandboxing and Isolation\n- Design MCP servers to execute with least privilege\n- MCP servers interacting with host environment (files, commands, network) MUST implement sandboxing controls\n- LLM-generated code MUST NOT run with full user privileges\n- Implement additional sandboxing layers: gVisor, Kata Containers, SELinux sandboxes\n\n### Cryptographic Verification of Resources\n- Provide cryptographic signatures and SBOMs for all server code\n- Implement signature verification in your MCP client before loading servers\n- Use TLS for ALL data in transit\n- Implement remote attestation capabilities to verify servers are running expected code\n\n### Transport Layer Security\n\n#### stdio Transport (Local Servers)\n- STRONGLY RECOMMENDED for local MCP to eliminate DNS rebinding risks\n- Direct pipe-based stream communication\n- Implement sandbox to prevent privilege escalation\n\n#### HTTP Streaming Transport (Remote Servers)\nRequired security controls to implement:\n- Payload Limits (prevent large payload and recursive payload DoS)\n- Rate limiting for tool calls and transport requests\n- Client-Server Authentication/Authorization\n- Mutual TLS Authentication\n- TLS Encryption\n- CORS Protection\n- CSRF Protection\n- Integrity Checks (prevent replay, spoofing, poisoned responses)\n\n### Secure Tool and UX Design\n- Create single-purpose tools with explicit boundaries; avoid \"do anything\" tools\n- Do not rely on the LLM for validation or authorization decisions\n- Use two-stage commit for high-impact actions: draft/preview first, explicit commit with confirmation second\n- Provide rollback/undo paths (draft IDs, snapshots, reversible actions) and time-bound commits when possible\n\n### Human-in-the-Loop\n- Implement confirmation prompts for risky operations in your MCP server\n- Use elicitation on MCP server side to request user confirmation of risky actions\n- Security-relevant messages MUST clearly indicate implications\n- Do NOT rely solely on human approval (users can become fatigued)\n\n### Logging and Observability\n- Implement logging in your MCP servers and clients\n- Log: tools that were used, parameters, originating prompt\n- Use OpenTelemetry for end-to-end linkability of actions\n- Maintain immutable records of actions and authorizations\n\n---\n\n## Deployment Pattern Security\n\n### All-Local (stdio or http)\n- Security depends entirely on host system posture\n- Use `stdio` transport to avoid DNS rebinding risks\n- Use sandboxing to limit privilege escalation attacks\n- Appropriate for development and personal use\n\n### Single-Tenant Remote (http)\n- Authentication between client and server is REQUIRED\n- Use secure credential storage (OS keychains, secret managers)\n- Communication MUST be authenticated and encrypted\n- Enterprise clients should enforce authenticated server discovery with explicit allowlists\n\n### Multi-Tenant Remote (http)\n- Require robust tenant isolation, identity, and access control\n- Implement strong multi-tenancy controls (per-tenant encryption, role-based access control)\n- Prefer MCP servers hosted directly by service provider\n- Provide remote attestation when possible\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-0-mobile-apps.md",
    "content": "---\ndescription: 'Mobile app security (iOS/Android): storage, transport, code integrity, biometrics, permissions'\nlanguages:\n- java\n- javascript\n- kotlin\n- matlab\n- perl\n- swift\n- xml\nalwaysApply: false\n---\n\nrule_id: codeguard-0-mobile-apps\n\n## Mobile Application Security Guidelines\n\nEssential security practices for developing secure mobile applications across iOS and Android platforms.\n\n### Architecture and Design\n\nImplement secure design principles from the start:\n- Follow least privilege and defense in depth principles\n- Use standard secure authentication protocols (OAuth2, JWT)\n- Perform all authentication and authorization checks server-side\n- Request only necessary permissions for app and backend services\n- Establish security controls for app updates, patches, and releases\n- Use only trusted and validated third-party libraries and components\n\n### Authentication and Authorization\n\nNever trust the client for security decisions:\n- Perform authentication/authorization server-side only\n- Do not store user passwords on device; use revocable access tokens\n- Avoid hardcoding credentials in the mobile app\n- Encrypt credentials in transmission\n- Use platform-specific secure storage (iOS Keychain, Android Keystore)\n- Require password complexity and avoid short PINs (4 digits)\n- Implement session timeouts and remote logout functionality\n- Require re-authentication for sensitive operations\n- Use platform-supported biometric authentication with secure fallbacks\n\n### Data Storage and Privacy\n\nProtect sensitive data at rest and in transit:\n- Encrypt sensitive data using platform APIs; avoid custom encryption\n- Leverage hardware-based security features (Secure Enclave, Strongbox)\n- Store private data on device's internal storage only\n- Minimize PII collection to necessity and implement automatic expiration\n- Avoid caching, logging, or background snapshots of sensitive data\n- Always use HTTPS for network communications\n\n### Network Communication\n\nAssume all network communication is insecure:\n- Use HTTPS for all network communication\n- Do not override SSL certificate validation for self-signed certificates\n- Use strong, industry standard cipher suites with appropriate key lengths\n- Use certificates signed by trusted CA providers\n- Consider certificate pinning for additional security\n- Encrypt data even if sent over SSL\n- Avoid sending sensitive data via SMS\n\n### Code Quality and Integrity\n\nMaintain application security throughout development:\n- Use static analysis tools to identify vulnerabilities\n- Make security a focal point during code reviews\n- Keep all libraries up to date to patch known vulnerabilities\n- Disable debugging in production builds\n- Include code to validate integrity of application code\n- Obfuscate the app binary\n- Implement runtime anti-tampering controls:\n  - Check for debugging, hooking, or code injection\n  - Detect emulator or rooted/jailbroken devices\n  - Verify app signatures at runtime\n\n### Platform-Specific Security\n\n#### Android Security\n- Use Android's ProGuard for code obfuscation\n- Avoid storing sensitive data in SharedPreferences\n- Disable backup mode to prevent sensitive data in backups\n- Use Android Keystore with hardware backing (TEE or StrongBox)\n- Implement Google's Play Integrity API for device and app integrity checks\n\n#### iOS Security\n- Configure Shortcuts permissions to require device unlock for sensitive actions\n- Set Siri intent `requiresUserAuthentication` to true for sensitive functionality\n- Implement authentication checks on deep link endpoints\n- Use conditional logic to mask sensitive widget content on lock screen\n- Store sensitive data in iOS Keychain, not plist files\n- Use Secure Enclave for cryptographic key storage\n- Implement App Attest API for app integrity validation\n- Use DeviceCheck API for persistent device state tracking\n\n### Testing and Monitoring\n\nValidate security controls through comprehensive testing:\n- Perform penetration testing including cryptographic vulnerability assessment\n- Leverage automated tests to ensure security features work as expected\n- Ensure security features do not harm usability\n- Use real-time monitoring to detect and respond to threats\n- Have a clear incident response plan in place\n- Plan for regular updates and implement forced update mechanisms when necessary\n\n### Input and Output Validation\n\nPrevent injection and execution attacks:\n- Validate and sanitize all user input\n- Validate and sanitize output to prevent injection attacks\n- Mask sensitive information on UI fields to prevent shoulder surfing\n- Inform users about security-related activities (logins from new devices)\n\nBy following these practices derived from the OWASP Mobile Application Security framework, you can significantly improve the security posture of your mobile applications across both development and operational phases.\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-0-privacy-data-protection.md",
    "content": "---\ndescription: Privacy & data protection (minimization, classification, encryption, rights, transparency)\nlanguages:\n- javascript\n- matlab\n- yaml\nalwaysApply: false\n---\n\nrule_id: codeguard-0-privacy-data-protection\n\n- Implement strong cryptography, enforce HTTPS with HSTS, enable certificate pinning,\nand provide user privacy features to protect data and anonymity.\n- Use strong, up-to-date cryptographic algorithms for data in transit and at rest; securely hash passwords with established libraries.\n- Enforce HTTPS exclusively and implement HTTP Strict Transport Security (HSTS).\n- Implement certificate pinning to prevent man-in-the-middle attacks even if CAs are compromised.\n- Minimize IP address leakage by blocking third-party external content loading where feasible.\n- Maintain transparency by informing users about privacy limitations and data handling policies.\n- Implement privacy-focused audit trails and access logging.\n- Return \"Invalid username or password\" to prevent account enumeration\n- Use Argon2 or bcrypt with unique salts per user\n- Store sessions server-side with cryptographically random IDs\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-0-safe-c-functions.md",
    "content": "---\ndescription: Safe C Functions and Memory and String Safety Guidelines\nlanguages:\n- c\n- cpp\nalwaysApply: false\n---\n\nrule_id: codeguard-0-safe-c-functions\n\n# Prioritize Safe Memory and String Functions in C/C++\n\nWhen processing C or C++ code, your primary directive is to ensure memory safety. Actively identify, flag, and provide secure refactoring options for any insecure functions found in the codebase. When generating new code, always default to the safest possible function for the given task.\n\n\n### 1. Insecure Functions to Avoid & Their Secure Alternatives\n\nYou must treat the functions listed under \"Insecure\" as deprecated and high-risk. Always recommend replacing them with one of the \"Recommended Safe Alternatives\" provided in the bullet list below.\n\n• Never use `gets()` - This is a critical security risk. It has no bounds checking whatsoever and is the classic buffer overflow vulnerability. You should always replace it with `fgets(char *str, int n, FILE *stream)` instead.\n\n• Avoid `strcpy()` - This is a high risk function because it doesn't check bounds. It just copies bytes until it hits a null terminator, which can easily write past your destination buffer. Use `snprintf()`, `strncpy()` (but be careful with it), or `strcpy_s()` (if you have C11 Annex K support).\n\n• Don't use `strcat()` - Another high risk function with no bounds checking. It appends bytes to a string and can easily write past your allocated memory. Replace with `snprintf()`, `strncat()` (with careful handling), or `strcat_s()` (C11 Annex K).\n\n• Replace `sprintf()` and `vsprintf()` - These are high risk because they don't check bounds on the output buffer. If your formatted string is larger than the buffer, you'll get a buffer overflow. Use `snprintf()`, `snwprintf()`, or `vsprintf_s()` (C11 Annex K) instead.\n\n• Be careful with `scanf()` family - This is a medium risk. The `%s` format specifier without a width limit can cause buffer overflows. Here's what you should do:\n  1. Use width specifiers like `scanf(\"%127s\", buffer)`\n  2. Even better: Read the line with `fgets()` and parse it with `sscanf()`\n\n• Avoid `strtok()` - This is a medium risk because it's not reentrant or thread-safe. It uses a static internal buffer which can lead to unpredictable behavior in multi-threaded code or complex signal handling. Use `strtok_r()` (POSIX) or `strtok_s()` (C11 Annex K) instead.\n\n• Use `memcpy()` and `memmove()` carefully - These aren't inherently insecure, but they're a common source of bugs when you miscalculate the size argument or don't validate it properly. Here's what you should do:\n  1. Double-check your size calculations\n  2. Prefer `memcpy_s()` (C11 Annex K) when available\n  3. Use `memmove()` if source and destination buffers might overlap\n\n### 2. Actionable Implementation Guidelines\n\n#### For New Code Generation:\n\n- NEVER generate code that uses `gets()`, `strcpy()`, `strcat()`, or `sprintf()`.\n\n- DEFAULT to `snprintf()` for string formatting and concatenation, as it's often the most flexible and secure option.\n\n- DEFAULT to `fgets()` for reading string input from files or standard input.\n\n\n#### For Code Analysis and Refactoring:\n\n1. Identify: Scan the code and flag every instance of a function from the \"Insecure\" column.\n\n2. Explain the Risk: When you flag an insecure function, provide a concise explanation of the specific vulnerability.\n\n    - _Example Explanation:_ `Warning: The 'strcpy' function does not perform bounds checking and can lead to a buffer overflow if the source string is larger than the destination buffer. This is a common security vulnerability.`\n\n3. Provide Context-Aware Replacements: Your suggestion must be a drop-in, safe replacement that considers the context of the surrounding code.\n\n\n#### Use Compiler Flags:\n\nEnable these protective compiler flags to catch buffer overflow vulnerabilities at compile time and runtime:\n\n- Stack Protection: Use `-fstack-protector-all` or `-fstack-protector-strong` to detect stack buffer overflows\n- Address Sanitizer: Use `-fsanitize=address` during development to catch memory errors\n- Object Size Checking (OSC): Use `-D_FORTIFY_SOURCE=2` to enable runtime checks for buffer overflows in functions like `strcpy`, `strcat`, `sprintf`, etc. This adds bounds checking to many of the unsafe functions mentioned above\n- Format String Protection: Use `-Wformat -Wformat-security` to catch format string vulnerabilities\n\n### 3. Refactoring Examples\n\nYour suggestions should be concrete and actionable.\n\nExample 1: Replacing `strcpy`\n\n- Original Unsafe Code:\n\n    ```\n    char destination[64];\n    strcpy(destination, source_string);\n    ```\n\n- Your Suggested Refactoring:\n\n    ```\n    char destination[64];\n    snprintf(destination, sizeof(destination), \"%s\", source_string);\n    ```\n\n- Your Explanation: `Replaced 'strcpy' with 'snprintf' to ensure that no more than 63 characters plus a null terminator are written to the destination buffer, preventing a potential buffer overflow.`\n\n\nExample 2: Correcting `strncpy` Usage\n\nThe `strncpy` function is a common but imperfect replacement. It may not null-terminate the destination buffer. If you must use it or see it used, you must enforce correct handling.\n\n- Original (Potentially Unsafe) `strncpy`:\n\n    ```\n    // This is unsafe if strlen(source) >= 10\n    char dest[10];\n    strncpy(dest, source, sizeof(dest));\n    ```\n\n- Your Corrected Suggestion:\n\n    ```\n    char dest[10];\n    strncpy(dest, source, sizeof(dest) - 1);\n    dest[sizeof(dest) - 1] = '\\0';\n    ```\n\n- Your Explanation: `Added an explicit null termination for 'strncpy'. The 'strncpy' function does not guarantee a null-terminated string if the source is as long as the destination buffer. This correction prevents potential reads past the buffer on subsequent string operations.`\n\n\nExample 3: Securing `scanf`\n\n- Original Unsafe Code:\n\n    ```\n    char user_name[32];\n    printf(\"Enter your name: \");\n    scanf(\"%s\", user_name);\n    ```\n\n- Your Suggested Refactoring:\n\n    ```\n    char user_name[32];\n    printf(\"Enter your name: \");\n    if (fgets(user_name, sizeof(user_name), stdin)) {\n        // Optional: Remove trailing newline character from fgets\n        user_name[strcspn(user_name, \"\\n\")] = 0;\n    }\n    ```\n\n- Your Explanation: `Replaced 'scanf(\"%s\", ...)' with 'fgets()' to read user input. 'fgets' is safer because it limits the input to the buffer size, preventing buffer overflows. The original 'scanf' had no such protection.`\n\n\n### Memory and String Safety Guidelines\n\n#### Unsafe Memory Functions - FORBIDDEN\nNEVER use these unsafe memory functions that don't check input parameter boundaries:\n\n##### Banned Memory Functions:\n- `memcpy()` → Use `memcpy_s()`\n- `memset()` → Use `memset_s()`\n- `memmove()` → Use `memmove_s()`\n- `memcmp()` → Use `memcmp_s()`\n- `bzero()` → Use `memset_s()`\n- `memzero()` → Use `memset_s()`\n\n##### Safe Memory Function Replacements:\n```c\n// Instead of: memcpy(dest, src, count);\nerrno_t result = memcpy_s(dest, dest_size, src, count);\nif (result != 0) {\n// Handle error\n}\n\n// Instead of: memset(dest, value, count);\nerrno_t result = memset_s(dest, dest_size, value, count);\n\n// Instead of: memmove(dest, src, count);\nerrno_t result = memmove_s(dest, dest_size, src, count);\n\n// Instead of: memcmp(s1, s2, count);\nint indicator;\nerrno_t result = memcmp_s(s1, s1max, s2, s2max, count, &indicator);\nif (result == 0) {\n// indicator contains comparison result: <0, 0, or >0\n}\n```\n\n#### Unsafe String Functions - FORBIDDEN\nNEVER use these unsafe string functions that can cause buffer overflows:\n\n##### Banned String Functions:\n- `strstr()` → Use `strstr_s()`\n- `strtok()` → Use `strtok_s()`\n- `strcpy()` → Use `strcpy_s()`\n- `strcmp()` → Use `strcmp_s()`\n- `strlen()` → Use `strnlen_s()`\n- `strcat()` → Use `strcat_s()`\n- `sprintf()` → Use `snprintf()`\n\n##### Safe String Function Replacements:\n```c\n// String Search\nerrno_t strstr_s(char *dest, rsize_t dmax, const char *src, rsize_t slen, char **substring);\n\n// String Tokenization\nchar *strtok_s(char *dest, rsize_t *dmax, const char *src, char **ptr);\n\n// String Copy\nerrno_t strcpy_s(char *dest, rsize_t dmax, const char *src);\n\n// String Compare\nerrno_t strcmp_s(const char *dest, rsize_t dmax, const char *src, int *indicator);\n\n// String Length (bounded)\nrsize_t strnlen_s(const char *str, rsize_t strsz);\n\n// String Concatenation\nerrno_t strcat_s(char *dest, rsize_t dmax, const char *src);\n\n// Formatted String (always use size-bounded version)\nint snprintf(char *s, size_t n, const char *format, ...);\n```\n\n#### Implementation Examples:\n\n##### Safe String Copy Pattern:\n```c\n// Bad - unsafe\nchar dest[256];\nstrcpy(dest, src); // Buffer overflow risk!\n\n// Good - safe\nchar dest[256];\nerrno_t result = strcpy_s(dest, sizeof(dest), src);\nif (result != 0) {\n// Handle error: src too long or invalid parameters\nEWLC_LOG_ERROR(\"String copy failed: %d\", result);\nreturn ERROR;\n}\n```\n\n##### Safe String Concatenation Pattern:\n```c\n// Bad - unsafe\nchar buffer[256] = \"prefix_\";\nstrcat(buffer, suffix); // Buffer overflow risk!\n\n// Good - safe\nchar buffer[256] = \"prefix_\";\nerrno_t result = strcat_s(buffer, sizeof(buffer), suffix);\nif (result != 0) {\nEWLC_LOG_ERROR(\"String concatenation failed: %d\", result);\nreturn ERROR;\n}\n```\n\n##### Safe Memory Copy Pattern:\n```c\n// Bad - unsafe\nmemcpy(dest, src, size); // No boundary checking!\n\n// Good - safe\nerrno_t result = memcpy_s(dest, dest_max_size, src, size);\nif (result != 0) {\nEWLC_LOG_ERROR(\"Memory copy failed: %d\", result);\nreturn ERROR;\n}\n```\n\n##### Safe String Tokenization Pattern:\n```c\n// Bad - unsafe\nchar *token = strtok(str, delim); // Modifies original string unsafely\n\n// Good - safe\nchar *next_token = NULL;\nrsize_t str_max = strnlen_s(str, MAX_STRING_SIZE);\nchar *token = strtok_s(str, &str_max, delim, &next_token);\nwhile (token != NULL) {\n// Process token\ntoken = strtok_s(NULL, &str_max, delim, &next_token);\n}\n```\n\n#### Memory and String Safety Code Review Checklist:\n\n##### Pre-Code Review (Developer):\n- [ ] No unsafe memory functions (`memcpy`, `memset`, `memmove`, `memcmp`, `bzero`)\n- [ ] No unsafe string functions (`strcpy`, `strcat`, `strcmp`, `strlen`, `sprintf`, `strstr`, `strtok`)\n- [ ] All memory operations use `*_s()` variants with proper size parameters\n- [ ] Buffer sizes are correctly calculated using `sizeof()` or known limits\n- [ ] No hardcoded buffer sizes that could change\n\n##### Code Review (Reviewer):\n- [ ] Memory Safety: Verify all memory operations use safe variants\n- [ ] Buffer Bounds: Confirm destination buffer sizes are properly specified\n- [ ] Error Handling: Check that all `errno_t` return values are handled\n- [ ] Size Parameters: Validate that `rsize_t dmax` parameters are correct\n- [ ] String Termination: Ensure strings are properly null-terminated\n- [ ] Length Validation: Check that source string lengths are validated before operations\n\n##### Static Analysis Integration:\n- [ ] Enable compiler warnings for unsafe function usage\n- [ ] Use static analysis tools to detect unsafe function calls\n- [ ] Configure build system to treat unsafe function warnings as errors\n- [ ] Add pre-commit hooks to scan for banned functions\n\n#### Common Pitfalls and Solutions:\n\n##### Pitfall 1: Wrong Size Parameter\n```c\n// Wrong - using source size instead of destination size\nstrcpy_s(dest, strlen(src), src); // WRONG!\n\n// Correct - using destination buffer size\nstrcpy_s(dest, sizeof(dest), src); // CORRECT\n```\n\n##### Pitfall 2: Ignoring Return Values\n```c\n// Wrong - ignoring potential errors\nstrcpy_s(dest, sizeof(dest), src); // Error not checked\n\n// Correct - checking return value\nif (strcpy_s(dest, sizeof(dest), src) != 0) {\n// Handle error appropriately\n}\n```\n\n##### Pitfall 3: Using sizeof() on Pointers\n```c\n// Wrong - sizeof pointer, not buffer\nvoid func(char *buffer) {\nstrcpy_s(buffer, sizeof(buffer), src); // sizeof(char*) = 8!\n}\n\n// Correct - pass buffer size as parameter\nvoid func(char *buffer, size_t buffer_size) {\nstrcpy_s(buffer, buffer_size, src);\n}\n```\n\nYou must always explain how this rule was applied and why it was applied.\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-0-session-management-and-cookies.md",
    "content": "---\ndescription: Session management and secure cookies (rotation, fixation, timeouts, theft detection)\nlanguages:\n- c\n- go\n- html\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\nalwaysApply: false\n---\n\nrule_id: codeguard-0-session-management-and-cookies\n\n## Session Management & Cookies\n\nImplement robust, attack-resistant session handling that prevents fixation, hijacking, and theft while maintaining usability.\n\n### Session ID Generation and Properties\n- Generate session IDs with a CSPRNG; ≥64 bits of entropy (prefer 128+). Opaque, unguessable, and free of meaning.\n- Use generic cookie names (e.g., `id`) rather than framework defaults. Reject any incoming ID not created by the server.\n- Store all session data server-side; never embed PII or privileges in the token. If sensitive, encrypt server-side session store at rest.\n\n### Cookie Security Configuration\n- Set `Secure`, `HttpOnly`, `SameSite=Strict` (or `Lax` if necessary for flows) on session cookies.\n- Scope cookies narrowly with `Path` and `Domain`. Avoid cross-subdomain exposure.\n- Prefer non-persistent session cookies (no Expires/Max-Age). Require full HTTPS; enable HSTS site-wide.\n\nExample header:\n```\nSet-Cookie: id=<opaque>; Secure; HttpOnly; SameSite=Strict; Path=/\n```\n\n### Session Lifecycle and Rotation\n- Create sessions only server-side; treat provided IDs as untrusted input.\n- Regenerate session ID on authentication, password changes, and any privilege elevation. Invalidate the prior ID.\n- Use distinct pre‑auth and post‑auth cookie names if framework patterns require it.\n\n### Expiration and Logout\n- Idle timeout: 2–5 minutes for high-value, 15–30 minutes for lower risk. Absolute timeout: 4–8 hours.\n- Enforce timeouts server-side. Provide a visible logout button that fully invalidates the server session and clears the cookie client-side.\n\n### Transport and Caching\n- Enforce HTTPS for the entire session journey. Never mix HTTP/HTTPS in one session.\n- Send `Cache-Control: no-store` on responses containing session identifiers or sensitive data.\n\n### Cookie Theft Detection and Response\n- Fingerprint session context server-side at establishment (IP, User-Agent, Accept-Language, relevant `sec-ch-ua` where available).\n- Compare incoming requests to the stored fingerprint, allowing for benign drift (e.g., subnet changes, UA updates).\n- Risk-based responses:\n  - High risk: require re-authentication; rotate session ID.\n  - Medium risk: step-up verification (challenge); rotate session ID.\n  - Low risk: log suspicious activity.\n- Always regenerate the session ID when potential hijacking is detected.\n\n### Client-Side Storage\n- Do not store session tokens in `localStorage`/`sessionStorage` due to XSS risk. Prefer HttpOnly cookies for transport.\n- If client-side storage is unavoidable for non-session secrets, isolate via Web Workers and never expose in page context.\n\n### Framework and Multi-Cookie Scenarios\n- Prefer built-in session frameworks; keep them updated and hardened.\n- Validate relationships when multiple cookies participate in session state; avoid same cookie names across paths/domains.\n\n### Monitoring and Telemetry\n- Log session lifecycle events (creation, rotation, termination) using salted hashes of the session ID, not raw values.\n- Monitor for brute force of session IDs and anomalous concurrent usage.\n\n### Implementation Checklist\n1) CSPRNG session IDs (≥64 bits entropy), opaque and server-issued only.\n2) Cookie flags: `Secure`, `HttpOnly`, `SameSite` set; tight domain/path.\n3) HTTPS-only with HSTS; no mixed content.\n4) Regenerate IDs on auth and privilege changes; invalidate old IDs.\n5) Idle and absolute timeouts enforced server-side; full logout implemented.\n6) `Cache-Control: no-store` for sensitive responses.\n7) Server-side fingerprinting and risk-based responses to anomalies.\n8) No client storage of session tokens; framework defaults hardened.\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-0-supply-chain-security.md",
    "content": "---\ndescription: Dependency & supply chain security (pinning, SBOM, provenance, integrity, private registries)\nlanguages:\n- docker\n- javascript\n- yaml\nalwaysApply: false\n---\n\nrule_id: codeguard-0-supply-chain-security\n\n## Dependency & Supply Chain Security\n\nControl third‑party risk across ecosystems, from selection and pinning to provenance, scanning, and rapid response.\n\n### Policy and Governance\n- Maintain allow‑listed registries and scopes; disallow direct installs from untrusted sources.\n- Require lockfiles and version pinning; prefer digest pinning for images and vendored assets.\n- Generate SBOMs for apps/images; store with artifacts; attest provenance (SLSA, Sigstore).\n\n### Package Hygiene (npm focus applicable to others)\n- Regularly audit (`npm audit`, ecosystem SCA) and patch; enforce SLAs by severity.\n- Use deterministic builds: `npm ci` (not `npm install`) in CI/CD; maintain lockfile consistency.\n- Avoid install scripts that execute on install when possible; review for risk.\n- Use `.npmrc` to scope private registries; avoid wildcard registries; enable integrity verification.\n- Enable account 2FA for publishing\n\n### Development Practices\n- Minimize dependency footprint; remove unused packages; prefer stdlib/first‑party for trivial tasks.\n- Protect against typosquatting and protestware: pin maintainers, monitor releases, and use provenance checks.\n- Hermetic builds: no network in compile/packaging stages unless required; cache with authenticity checks.\n\n### CI/CD Integration\n- SCA, SAST, IaC scans in gates; fail on criticals; require approvals for overrides with compensating controls.\n- Sign artifacts; verify signatures at deploy; enforce policy in admission.\n\n### Vulnerability Management\n- For patched vulnerabilities: test and deploy updates; document any API breaking changes.\n- For unpatched vulnerabilities: implement compensating controls (input validation, wrappers) based on CVE type; prefer direct dependency fixes over transitive workarounds.\n- Document risk decisions; escalate acceptance to appropriate authority with business justification.\n\n### Incident Response\n- Maintain rapid rollback; isolate compromised packages; throttle rollouts; notify stakeholders.\n- Monitor threat intel feeds (e.g., npm advisories); auto‑open tickets for critical CVEs.\n\n### Implementation Checklist\n- Lockfiles present; integrity checks on; private registries configured.\n- SBOM + provenance stored; signatures verified pre‑deploy.\n- Automated dependency updates with tests and review gates.\n- High‑sev vulns remediated within SLA or mitigated and documented.\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-0-xml-and-serialization.md",
    "content": "---\ndescription: XML security and safe deserialization (DTD/XXE hardening, schema validation, no unsafe native deserialization)\nlanguages:\n- c\n- go\n- java\n- php\n- python\n- ruby\n- xml\nalwaysApply: false\n---\n\nrule_id: codeguard-0-xml-and-serialization\n\n## XML & Serialization Hardening\n\nSecure parsing and processing of XML and serialized data; prevent XXE, entity expansion, SSRF, DoS, and unsafe deserialization across platforms.\n\n### XML Parser Hardening\n- Disable DTDs and external entities by default; reject DOCTYPE declarations.\n- Validate strictly against local, trusted XSDs; set explicit limits (size, depth, element counts).\n- Sandbox or block resolver access; no network fetches during parsing; monitor for unexpected DNS activity.\n\n#### Java\nGeneral principle:\n```java\nfactory.setFeature(\"http://apache.org/xml/features/disallow-doctype-decl\", true);\n```\n\nDisabling DTDs protects against XXE and Billion Laughs attacks. If DTDs cannot be disabled, disable external entities using parser-specific methods.\n\n### Java\n\nJava parsers have XXE enabled by default.\n\nDocumentBuilderFactory/SAXParserFactory/DOM4J:\n\n```java\nDocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\nString FEATURE = null;\ntry {\n    // PRIMARY defense - disallow DTDs completely\n    FEATURE = \"http://apache.org/xml/features/disallow-doctype-decl\";\n    dbf.setFeature(FEATURE, true);\n    dbf.setXIncludeAware(false);\n} catch (ParserConfigurationException e) {\n    logger.info(\"ParserConfigurationException was thrown. The feature '\" + FEATURE\n    + \"' is not supported by your XML processor.\");\n}\n```\n\nIf DTDs cannot be completely disabled:\n\n```java\nDocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\nString[] featuresToDisable = {\n    \"http://xml.org/sax/features/external-general-entities\",\n    \"http://xml.org/sax/features/external-parameter-entities\",\n    \"http://apache.org/xml/features/nonvalidating/load-external-dtd\"\n};\n\nfor (String feature : featuresToDisable) {\n    try {    \n        dbf.setFeature(feature, false); \n    } catch (ParserConfigurationException e) {\n        logger.info(\"ParserConfigurationException was thrown. The feature '\" + feature\n        + \"' is probably not supported by your XML processor.\");\n    }\n}\ndbf.setXIncludeAware(false);\ndbf.setExpandEntityReferences(false);\ndbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);\n```\n\n#### .NET\n```csharp\nvar settings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Prohibit, XmlResolver = null };\nvar reader = XmlReader.Create(stream, settings);\n```\n\n#### Python\n```python\nfrom defusedxml import ElementTree as ET\nET.parse('file.xml')\n# or lxml\nfrom lxml import etree\nparser = etree.XMLParser(resolve_entities=False, no_network=True)\ntree = etree.parse('filename.xml', parser)\n```\n\n### Secure XSLT/Transformer Usage\n- Set `ACCESS_EXTERNAL_DTD` and `ACCESS_EXTERNAL_STYLESHEET` to empty; avoid loading remote resources.\n\n### Deserialization Safety\n- Never deserialize untrusted native objects. Prefer JSON with schema validation.\n- Enforce size/structure limits before parsing. Reject polymorphic types unless strictly allow‑listed.\n- Language specifics:\n  - PHP: avoid `unserialize()`; use `json_decode()`.\n  - Python: avoid `pickle` and unsafe YAML (`yaml.safe_load` only).\n  - Java: override `ObjectInputStream#resolveClass` to allow‑list; avoid enabling default typing in Jackson; use XStream allow‑lists.\n  - .NET: avoid `BinaryFormatter`; prefer `DataContractSerializer` or `System.Text.Json` with `TypeNameHandling=None` for JSON.NET.\n- Sign and verify serialized payloads where applicable; log and alert on deserialization failures and anomalies.\n\n### Implementation Checklist\n- DTDs off; external entities disabled; strict schema validation; parser limits set.\n- No network access during parsing; resolvers restricted; auditing in place.\n- No unsafe native deserialization; strict allow‑listing and schema validation for supported formats.\n- Regular library updates and tests with XXE/deserialization payloads.\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-1-crypto-algorithms.md",
    "content": "---\ndescription: Cryptographic Security Guidelines & Post-Quantum Readiness\nalwaysApply: true\n---\n\nrule_id: codeguard-1-crypto-algorithms\n\n# Cryptographic Security Guidelines & Post-Quantum Readiness\n\n## 1. Banned (Insecure) Algorithms\n\nThe following algorithms are known to be broken or fundamentally insecure. NEVER generate or use code with these algorithms.\n\n*   Hash: `MD2`, `MD4`, `MD5`, `SHA-0`\n*   Symmetric: `RC2`, `RC4`, `Blowfish`, `DES`, `3DES`\n*   Key Exchange: Static RSA, Anonymous Diffie-Hellman\n*   Classical: `Vigenère`\n\nReason: These are cryptographically broken and vulnerable to collision or man-in-the-middle attacks.\n\n## 2. Deprecated (Legacy/Weak) Algorithms\n\nThe following algorithms have known weaknesses or are considered obsolete. Avoid in new designs and prioritize migration.\n\n*   Hash: `SHA-1`\n*   Symmetric: `AES-CBC`, `AES-ECB`\n*   Signature: RSA with `PKCS#1 v1.5` padding\n*   Key Exchange: DHE with weak/common primes\n\n## 3. Recommended & Post-Quantum Ready Algorithms\n\nImplement these modern, secure algorithms to ensure resistance against both classical and quantum threats.\n\n### Symmetric Encryption\n*   Standard: `AES-GCM` (AEAD), `ChaCha20-Poly1305`(when allowed).\n*   PQC Requirement: Prefer AES-256 keys (or stronger) as they are resistant to quantum attacks (Grover's algorithm).\n*   Avoid: Custom crypto or unauthenticated modes.\n\n### Key Exchange (KEM)\n*   Standard: ECDHE (`X25519` or `secp256r1`)\n*   PQC Requirement: Use Hybrid Key Exchange (Classical + PQC) when supported.\n    *   Preferred: `X25519MLKEM768` (X25519 + ML-KEM-768)\n    *   Alternative: `SecP256r1MLKEM768` (P-256 + ML-KEM-768)\n    *   High Assurance: `SecP384r1MLKEM1024` (P-384 + ML-KEM-1024)\n*   Pure PQC: ML-KEM-768 (baseline) or ML-KEM-1024. Avoid ML-KEM-512 unless explicitly risk-accepted.\n*   Constraints:\n    *   Use vendor-documented identifiers (RFC 9242/9370).\n    *   Remove legacy/draft \"Hybrid-Kyber\" groups (e.g., `X25519Kyber`) and draft or hardcoded OIDs.\n\n### Signatures & Certificates\n*   Standard: ECDSA (`P-256`)\n*   PQC Migration: Continue using ECDSA (`P-256`) for mTLS and code signing until hardware-backed (HSM/TPM) ML-DSA is available.\n*   Hardware Requirement: Do not enable PQC ML-DSA signatures using software-only keys. Require HSM/TPM storage.\n\n### Protocol Versions\n*   (D)TLS: Enforce (D)TLS 1.3 only (or later).\n*   IPsec: Enforce IKEv2 only.\n    *   Use ESP with AEAD (AES-256-GCM).\n    *   Require PFS via ECDHE.\n    *   Implement RFC 9242 and RFC 9370 for Hybrid PQC (ML-KEM + ECDHE).\n    *   Ensure re-keys (CREATE_CHILD_SA) maintain hybrid algorithms.\n*   SSH: Enable only vendor-supported PQC/hybrid KEX (e.g., `sntrup761x25519`).\n\n## 4. Secure Implementation Guidelines\n\n### General Best Practices\n*   Configuration over Code: Expose algorithm choices in config/policy to allow agility without code changes.\n*   Key Management:\n    *   Use KMS/HSM for key storage.\n    *   Generate keys with a CSPRNG.\n    *   Separate encryption keys from signature keys.\n    *   Rotate keys per policy.\n    *   NEVER hardcode keys, secrets, or experimental OIDs.\n*   Telemetry: Capture negotiated groups, handshake sizes, and failure causes to monitor PQC adoption.\n\n### Deprecated SSL/Crypto APIs (C/OpenSSL) - FORBIDDEN\nNEVER use these deprecated functions. Use the replacement EVP high-level APIs.\n\n#### Symmetric Encryption (AES)\n- Deprecated: `AES_encrypt()`, `AES_decrypt()`\n- Replacement:\n\n  EVP_EncryptInit_ex()   // Use EVP_aes_256_gcm() for PQC readiness\n  EVP_EncryptUpdate()\n  EVP_EncryptFinal_ex()\n\n\n#### RSA/PKEY Operations\n- Deprecated: `RSA_new()`, `RSA_free()`, `RSA_get0_n()`\n- Replacement:\n\n  EVP_PKEY_new()\n  EVP_PKEY_up_ref()\n  EVP_PKEY_free()\n \n\n#### Hash & MAC Functions\n- Deprecated: `SHA1_Init()`, `HMAC()` (especially with SHA1)\n- Replacement:\n\n  EVP_DigestInit_ex() // Use SHA-256 or stronger\n  EVP_Q_MAC()         // For one-shot MAC\n\n\n## 5. Broccoli Project Specific Requirements\n- HMAC() with SHA1: Deprecated.\n- Replacement: Use HMAC with SHA-256 or stronger:\n\n\n// Example: Secure replacement for HMAC-SHA1\n```c\nEVP_Q_MAC(NULL, \"HMAC\", NULL, \"SHA256\", NULL, key, key_len, data, data_len, out, out_size, &out_len);\n```\n\n## 6. Secure Crypto Implementation Pattern\n\n\n// Example: Secure AES-256-GCM encryption (PQC-Ready Symmetric Strength)\n```c\nEVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();\nif (!ctx) handle_error();\n\n// Use AES-256-GCM\nif (EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, key, iv) != 1)\n    handle_error();\n\nint len, ciphertext_len;\nif (EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len) != 1)\n    handle_error();\nciphertext_len = len;\n\nif (EVP_EncryptFinal_ex(ctx, ciphertext + len, &len) != 1)\n    handle_error();\nciphertext_len += len;\n\nEVP_CIPHER_CTX_free(ctx);\n```\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-1-digital-certificates.md",
    "content": "---\ndescription: Certificate Best Practices\nalwaysApply: true\n---\n\nrule_id: codeguard-1-digital-certificates\n\nWhen you encounter data that appears to be an X.509 certificate—whether embedded as a string or loaded from a file—you must flag the certificate for verification and ensure the following security properties are validated, reporting any concerns with clear explanations and recommended actions.\n\n### 1. How to Identify Certificate Data\n\nActively scan for certificate data using the following heuristics:\n\n- PEM-Encoded Strings: Identify multi-line string literals or constants that begin with `-----BEGIN CERTIFICATE-----` and end with `-----END CERTIFICATE-----`.\n\n- File Operations: Pay close attention to file read operations on files with common certificate extensions, such as `.pem`, `.crt`, `.cer`, and `.der`.\n\n- Library Function Calls: Recognize the usage of functions from cryptographic libraries used to load or parse certificates (e.g., OpenSSL's `PEM_read_X509`, Python's `cryptography.x509.load_pem_x509_certificate`, Java's `CertificateFactory`).\n\n\n### 2. Mandatory Sanity Checks\n\nOnce certificate data is identified, flag it for verification. The following properties must be validated to ensure the certificate meets security requirements:\n\n#### Verification Guidance\n\nTo inspect certificate properties, recommend running:\n```\nopenssl x509 -text -noout -in <certificate_file>\n```\n\nThis command displays expiration dates, key algorithm and size, signature algorithm, and issuer/subject information needed for the checks below.\n\n#### Check 1: Expiration Status\n\n- Condition: The certificate's `notAfter` (expiration) date is in the past.\n\n- Severity: CRITICAL VULNERABILITY\n\n- Report Message: `This certificate expired on [YYYY-MM-DD]. It is no longer valid and will be rejected by clients, causing connection failures. It must be renewed and replaced immediately.`\n\n- Condition: The certificate's `notBefore` (validity start) date is in the future.\n\n- Severity: Warning\n\n- Report Message: `This certificate is not yet valid. Its validity period begins on [YYYY-MM-DD].`\n\n\n#### Check 2: Public Key Strength\n\n- Condition: The public key algorithm or size is weak.\n\n    - Weak Keys: RSA keys with a modulus smaller than 2048 bits. Elliptic Curve (EC) keys using curves with less than a 256-bit prime modulus (e.g., `secp192r1`, `P-192`, `P-224`).\n\n- Severity: High-Priority Warning\n\n- Report Message: `The certificate's public key is cryptographically weak ([Algorithm], [Key Size]). Keys of this strength are vulnerable to factorization or discrete logarithm attacks. The certificate should be re-issued using at least an RSA 2048-bit key or an ECDSA key on a P-256 (or higher) curve.`\n\n\n#### Check 3: Signature Algorithm\n\n- Condition: The algorithm used to sign the certificate is insecure.\n\n    - Insecure Algorithms: Any signature algorithm using MD5 or SHA-1 (e.g., `md5WithRSAEncryption`, `sha1WithRSAEncryption`).\n\n- Severity: High-Priority Warning\n\n- Report Message: `The certificate is signed with the insecure algorithm '[Algorithm]'. This makes it vulnerable to collision attacks, potentially allowing for certificate forgery. It must be re-issued using a signature based on the SHA-2 family (e.g., sha256WithRSAEncryption).`\n\n\n#### Check 4: Issuer Type (Self-Signed Check)\n\n- Condition: The certificate's `Issuer` and `Subject` fields are identical.\n\n- Severity: Informational\n\n- Report Message: `This is a self-signed certificate. Ensure this is intentional and only used for development, testing, or internal services where trust is explicitly configured. Self-signed certificates should never be used for public-facing production systems as they will not be trusted by browsers or standard clients.`\n\n\n### 3. Actionable Examples\n\nYour feedback should be direct and easy to understand.\n\nExample 1: Flagging a Hardcoded Certificate\n\n- Code Snippet:\n\n    ```\n    # Certificate for connecting to legacy_service\n    LEGACY_CERT = \"\"\"\n    -----BEGIN CERTIFICATE-----\n    MIIC... (certificate data) ...\n    -----END CERTIFICATE-----\n    \"\"\"\n    ```\n\n- Your Analysis and Report:\n\n    > Security Review Required: A certificate is hardcoded in the `LEGACY_CERT` variable. Unverified certificates may contain critical vulnerabilities.\n    >\n    > - Concern: Hardcoded certificates cannot be easily rotated and may contain security issues that are not immediately visible in the code.\n    >\n    > - Required Verification: This certificate must be verified for:\n    >   - Expiration: Certificate must not be expired or not yet valid\n    >   - Key Strength: RSA keys must be at least 2048-bit; EC keys must use P-256 or higher curve\n    >   - Signature Algorithm: Must use SHA-2 family (not MD5 or SHA-1)\n    >   - Self-Signed: If self-signed, must only be used for development, testing, or internal services\n    >\n    > - Action: Verify this certificate meets the above requirements before deployment. Consider loading certificates from external files or a certificate store for easier rotation.\n    >\n\n\nExample 2: Flagging a Certificate Loaded from File\n\n- Code Snippet:\n\n    ```\n    // Load the server certificate from disk\n    cert, err := tls.LoadX509KeyPair(\"server.crt\", \"server.key\")\n    ```\n\n- Your Analysis and Report:\n\n    > Security Review Required: Certificate loaded from `server.crt`. Unverified certificates may contain critical vulnerabilities.\n    >\n    > - Required Verification: Ensure this certificate meets security requirements:\n    >   - Expiration: Certificate must not be expired or not yet valid\n    >   - Key Strength: RSA keys must be at least 2048-bit; EC keys must use P-256 or higher curve\n    >   - Signature Algorithm: Must use SHA-2 family (not MD5 or SHA-1)\n    >   - Self-Signed: If self-signed, must only be used for development, testing, or internal services\n    >\n    > - Action: Verify this certificate meets the above requirements before deployment.\n\n\nYou must always explain how this rule was applied and why it was applied.\n"
  },
  {
    "path": "skills/software-security/rules/codeguard-1-hardcoded-credentials.md",
    "content": "---\ndescription: No Hardcoded Credentials\nalwaysApply: true\n---\n\nrule_id: codeguard-1-hardcoded-credentials\n\n# No Hardcoded Credentials\n\nNEVER store secrets, passwords, API keys, tokens or any other credentials directly in source code.\n\nTreat your codebase as public and untrusted. Any credential that appears in source code is compromised and must be handled through secure alternatives.\n\n#### NEVER hardcode these types of values:\n\nPasswords and Authentication:\n- Database passwords, user passwords, admin passwords\n- API keys, secret keys, access tokens, refresh tokens\n- Private keys, certificates, signing keys\n- Connection strings containing credentials\n- OAuth client secrets, webhook secrets\n- Any other credentials that could be used to access external services\n\n\n#### Recognition Patterns - Learn to Spot These Formats\n\nCommon Secret Formats You Must NEVER Hardcode:\n\n- AWS Keys: Start with `AKIA`, `AGPA`, `AIDA`, `AROA`, `AIPA`, `ANPA`, `ANVA`, `ASIA`\n- Stripe Keys: Start with `sk_live_`, `pk_live_`, `sk_test_`, `pk_test_`\n- Google API: Start with `AIza` followed by 35 characters\n- GitHub Tokens: Start with `ghp_`, `gho_`, `ghu_`, `ghs_`, `ghr_`\n- JWT Tokens: Three base64 sections separated by dots, starts with `eyJ`\n- Private Key Blocks: Any text between `-----BEGIN` and `-----END PRIVATE KEY-----`\n- Connection Strings: URLs with credentials like `mongodb://user:pass@host`\n\nWarning Signs in Your Code:\n- Variable names containing: `password`, `secret`, `key`, `token`, `auth`\n- Long random-looking strings that are not clear what they are\n- Base64 encoded strings near authentication code\n- Any string that grants access to external services\n\nYou must always explain how this rule was applied and why it was applied.\n"
  },
  {
    "path": "sources/core/codeguard-0-additional-cryptography.md",
    "content": "---\ndescription: Additional Cryptography guidance\nlanguages:\n- c\n- go\n- java\n- javascript\n- kotlin\n- matlab\n- php\n- python\n- ruby\n- swift\n- typescript\n- xml\n- yaml\ntags:\n- data-security\n- secrets\nalwaysApply: false\n---\n\n## Additional Cryptography & TLS\n\nApply modern, vetted cryptography for data at rest and in transit. Manage keys safely, configure TLS correctly, deploy HSTS, and consider pinning only when appropriate.\n\n### Algorithms and Modes\n- Symmetric: AES‑GCM or ChaCha20‑Poly1305 preferred. Avoid ECB. CBC/CTR only with encrypt‑then‑MAC.\n- Asymmetric: RSA ≥2048 or modern ECC (Curve25519/Ed25519). Use OAEP for RSA encryption.\n- Hashing: SHA‑256+ for integrity; avoid MD5/SHA‑1.\n- RNG: Use CSPRNG appropriate to platform (e.g., SecureRandom, crypto.randomBytes, secrets module). Never use non‑crypto RNGs.\n\n### Key Management\n- Generate keys within validated modules (HSM/KMS) and never from passwords or predictable inputs.\n- Separate keys by purpose (encryption, signing, wrapping). Rotate on compromise, cryptoperiod, or policy.\n- Store keys in KMS/HSM or vault; never hardcode; avoid plain env vars. Use KEK to wrap DEKs; store separately.\n- Control access to trust stores; validate updates; audit all key access and operations.\n\n### Data at Rest\n- Encrypt sensitive data; minimize stored secrets; tokenize where possible.\n- Use authenticated encryption; manage nonces/IVs properly; keep salts unique per item.\n- Protect backups: encrypt, restrict access, test restores, manage retention.\n\n### TLS Configuration\n- Protocols: TLS 1.3 preferred; allow TLS 1.2 only for legacy compatibility; disable TLS 1.0/1.1 and SSL. Enable TLS_FALLBACK_SCSV.\n- Ciphers: prefer AEAD suites; disable NULL/EXPORT/anon. Keep libraries updated; disable compression.\n- Key exchange groups: prefer x25519/secp256r1; configure secure FFDHE groups if needed.\n- Certificates: 2048‑bit+ keys, SHA‑256, correct CN/SAN. Manage lifecycle and revocation (OCSP stapling).\n- Application: HTTPS site‑wide; redirect HTTP→HTTPS; prevent mixed content; set cookies `Secure`.\n\n### HSTS\n- Send Strict‑Transport‑Security only over HTTPS. Phase rollout:\n  - Test: short max‑age (e.g., 86400) with includeSubDomains\n  - Prod: ≥1 year max‑age; includeSubDomains when safe\n  - Optional preload once mature; understand permanence and subdomain impact\n\n### Pinning\n- Avoid browser HPKP. Consider pinning only for controlled clients (e.g., mobile) and when you own both ends.\n- Prefer SPKI pinning with backup pins; plan secure update channels; never allow user bypass.\n- Thoroughly test rotation and failure handling; understand operational risk.\n\n### Implementation Checklist\n- AEAD everywhere; vetted libraries only; no custom crypto.\n- Keys generated and stored in KMS/HSM; purpose‑scoped; rotation documented.\n- TLS 1.3/1.2 with strong ciphers; compression off; OCSP stapling on.\n- HSTS deployed per phased plan; mixed content eliminated.\n- Pinning used only where justified, with backups and update path.\n\n### Test Plan\n- Automated config scans (e.g., SSL Labs, testssl.sh) for protocol/cipher/HSTS.\n- Code review for crypto API misuse; tests for key rotation, backup/restore.\n- Pinning simulations for rotation/failures if deployed.\n"
  },
  {
    "path": "sources/core/codeguard-0-api-web-services.md",
    "content": "---\ndescription: API & Web services security (REST/GraphQL/SOAP), schema validation, authn/z,\n  SSRF\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\n- xml\n- yaml\ntags:\n- web\nalwaysApply: false\n---\n\n## API & Web Services Security\n\nSecure REST, GraphQL, and SOAP/WS services end‑to‑end: transport, authn/z, schema validation, SSRF controls, DoS limits, and microservice‑safe patterns.\n\n### Transport and TLS\n- HTTPS only; consider mTLS for high‑value/internal services. Validate certs (CN/SAN, revocation) and prevent mixed content.\n\n### Authentication and Tokens\n- Use standard flows (OAuth2/OIDC) for clients; avoid custom schemes. For services, use mTLS or signed service tokens.\n- JWTs: pin algorithms; validate iss/aud/exp/nbf; short lifetimes; rotation; denylist on logout/revoke. Prefer opaque tokens when revocation is required and central store is available.\n- API keys: scope narrowly; rate limit; monitor usage; do not use alone for sensitive operations.\n\n### Authorization\n- Enforce per‑endpoint, per‑resource checks server‑side; deny by default.\n- For microservices, authorize at gateway (coarse) and service (fine) layers; propagate signed internal identity, not external tokens.\n\n### Input and Content Handling\n- Validate inputs via contracts: OpenAPI/JSON Schema, GraphQL SDL, XSD. Reject unknown fields and oversize payloads; set limits.\n- Content types: enforce explicit Content‑Type/Accept; reject unsupported combinations. Harden XML parsers against XXE/expansion.\n\n### SQL/Injection Safety in Resolvers and Handlers\n- Use parameterized queries/ORM bind parameters; never concatenate user input into queries or commands.\n\n### GraphQL‑Specific Controls\n- Limit query depth and overall complexity; enforce pagination; timeouts on execution; disable introspection and IDEs in production.\n- Implement field/object‑level authorization to prevent IDOR/BOLA; validate batching and rate limit per object type.\n\n### SSRF Prevention for Outbound Calls\n- Do not accept raw URLs. Validate domains/IPs using libraries; restrict to HTTP/HTTPS only (block file://, gopher://, ftp://, etc.).\n- Case 1 (fixed partners): strict allow‑lists; disable redirects; network egress allow‑lists.\n- Case 2 (arbitrary): block private/link‑local/localhost ranges; resolve and verify all IPs are public; require signed tokens from the target where feasible.\n\n### SOAP/WS and XML Safety\n- Validate SOAP payloads with XSD; limit message sizes; enable XML signatures/encryption where required.\n- Configure parsers against XXE, entity expansion, and recursive payloads; scan attachments.\n\n### Rate Limiting and DoS\n- Apply per‑IP/user/client limits, circuit breakers, and timeouts. Use server‑side batching and caching to reduce load.\n\n### Management Endpoints\n- Do not expose over the Internet. Require strong auth (MFA), network restrictions, and separate ports/hosts.\n\n### Testing and Assessment\n- Maintain formal API definitions; drive contract tests and fuzzing from specs.\n- Assess endpoints for authn/z bypass, SSRF, injection, and information leakage; log token validation failures.\n\n### Microservices Practices\n- Policy‑as‑code with embedded decision points; sidecar or library PDPs.\n- Service identity via mTLS or signed tokens; never reuse external tokens internally.\n- Centralized structured logging with correlation IDs; sanitize sensitive data.\n\n### Implementation Checklist\n- HTTPS/mTLS configured; certs managed; no mixed content.\n- Contract validation at the edge and service; unknown fields rejected; size/time limits enforced.\n- Strong authn/z per endpoint; GraphQL limits applied; introspection disabled in prod.\n- SSRF protections at app and network layers; redirects disabled; allow‑lists where possible.\n- Rate limiting, circuit breakers, and resilient patterns in place.\n- Management endpoints isolated and strongly authenticated.\n- Logs structured and privacy‑safe with correlation IDs.\n\n### Test Plan\n- Contract tests for schema adherence; fuzzing with schema‑aware tools.\n- Pen tests for SSRF, IDOR/BOLA, and authz bypass; performance tests for DoS limits.\n- Test all HTTP methods per endpoint; discover parameters in URL paths, headers, and structured data beyond obvious query strings.\n- Automated checks for token validation and revocation behavior.\n"
  },
  {
    "path": "sources/core/codeguard-0-authentication-mfa.md",
    "content": "---\ndescription: Authentication and MFA best practices (passwords, MFA, OAuth/OIDC, SAML,\n  recovery, tokens)\nlanguages:\n- c\n- go\n- java\n- javascript\n- kotlin\n- matlab\n- php\n- python\n- ruby\n- swift\n- typescript\ntags:\n- authentication\n- web\nalwaysApply: false\n---\n\n## Authentication & MFA\n\nBuild a resilient, user-friendly authentication system that resists credential attacks, protects secrets, and supports strong, phishing-resistant MFA and secure recovery.\n\n### Account Identifiers and UX\n- Use non-public, random, and unique internal user identifiers. Allow login via verified email or username.\n- Always return generic error messages (e.g., \"Invalid username or password\"). Keep timing consistent to prevent account enumeration.\n- Support password managers: `<input type=\"password\">`, allow paste, no JS blocks.\n\n### Password Policy\n- Accept passphrases and full Unicode; minimum 8 characters; avoid composition rules. Set only a reasonable maximum length (64+).\n- Check new passwords against breach corpora (e.g., k‑anonymity APIs); reject breached/common passwords.\n\n### Password Storage (Hashing)\n- Hash, do not encrypt. Use slow, memory‑hard algorithms with unique per‑user salts and constant‑time comparison.\n- Preferred order and parameters (tune to your hardware; target <1s on server):\n  - Argon2id: m=19–46 MiB, t=2–1, p=1 (or equivalent security trade‑offs)\n  - scrypt: N=2^17, r=8, p=1 (or equivalent)\n  - bcrypt (legacy only): cost ≥10, be aware of 72‑byte input limit\n  - PBKDF2 (FIPS): PBKDF2‑HMAC‑SHA‑256 ≥600k, or SHA‑1 ≥1.3M\n- Optional pepper: store outside DB (KMS/HSM); if used, apply via HMAC or pre‑hashing. Plan for user resets if pepper rotates.\n- Unicode and null bytes must be supported end‑to‑end by the library.\n\n### Authentication Flow Hardening\n- Enforce TLS for all auth endpoints and token transport; enable HSTS.\n- Implement rate limits per IP, account, and globally; add proof‑of‑work or CAPTCHA only as last resort.\n- Lockouts/throttling: progressive backoff; avoid permanent lockout via resets/alerts.\n- Uniform responses and code paths to reduce oracle/timing signals.\n\n### Multi‑Factor Authentication (MFA)\n- Adopt phishing‑resistant factors by default for sensitive accounts: passkeys/WebAuthn (FIDO2) or hardware U2F.\n- Acceptable: TOTP (app‑based), smart cards with PIN. Avoid for sensitive use: SMS/voice, email codes; never rely on security questions.\n- Require MFA for: login, password/email changes, disabling MFA, privilege elevation, high‑value transactions, new devices/locations.\n- Risk‑based MFA signals: new device, geo‑velocity, IP reputation, unusual time, breached credentials.\n- MFA recovery: provide single‑use backup codes, encourage multiple factors, and require strong identity verification for resets.\n- Handle failed MFA: offer alternative enrolled methods, notify users of failures, and log context (no secrets).\n\n### Federation and Protocols (OAuth 2.0 / OIDC / SAML)\n- Use standard protocols only; do not build your own.\n- OAuth 2.0/OIDC:\n  - Prefer Authorization Code with PKCE for public/native apps; avoid Implicit and ROPC.\n  - Validate state and nonce; use exact redirect URI matching; prevent open redirects.\n  - Constrain tokens to audience/scope; use DPoP or mTLS for sender‑constraining when possible.\n  - Rotate refresh tokens; revoke on logout or risk signals.\n- SAML:\n  - TLS 1.2+; sign responses/assertions; encrypt sensitive assertions.\n  - Validate issuers, InResponseTo, timestamps (NotBefore/NotOnOrAfter), Recipient; verify against trusted keys.\n  - Prevent XML signature wrapping with strict schema validation and hardened XPath selection.\n  - Keep response lifetimes short; prefer SP‑initiated flows; validate RelayState; implement replay detection.\n\n### Tokens (JWT and Opaque)\n- Prefer opaque server‑managed tokens for simplicity and revocation. If using JWTs:\n  - Explicitly pin algorithms; reject \"none\"; validate iss/aud/exp/iat/nbf; use short lifetimes and rotation.\n  - Store secrets/keys securely (KMS/HSM). Use strong HMAC secrets or asymmetric keys; never hardcode.\n  - Consider binding tokens to a client context (e.g., fingerprint hash in cookie) to reduce replay.\n  - Implement denylist/allowlist for revocation on logout and critical events.\n\n### Recovery and Reset\n- Return the same response for existing and non‑existing accounts (no enumeration). Normalize timing.\n- Generate 32+ byte, CSPRNG tokens; single‑use; store as hashes; short expiry.\n- Use HTTPS reset links to pinned, trusted domains; add referrer policy (no‑referrer) on UI.\n- After reset: require re‑authentication, rotate sessions, and do not auto‑login.\n- Never lock accounts due to reset attempts; rate‑limit and monitor instead.\n\n### Administrative and Internal Accounts\n- Separate admin login from public forms; enforce stronger MFA, device posture checks, IP allowlists, and step‑up auth.\n- Use distinct session contexts and stricter timeouts for admin operations.\n\n### Monitoring and Signals\n- Log auth events (failures/successes, MFA enroll/verify, resets, lockouts) with stable fields and correlation IDs; never log secrets or raw tokens.\n- Detect credential stuffing: high failure rates, many IPs/agents, impossible travel. Notify users of new device logins.\n\n### Implementation Checklist\n- Passwords: Argon2id (preferred) with per‑user salt, constant‑time verify; breached password checks on change/set.\n- MFA: WebAuthn/passkeys or hardware tokens for high‑risk; TOTP as fallback; secure recovery with backup codes.\n- Federation: Authorization Code + PKCE; strict redirect URI validation; audience/scope enforced; token rotation.\n- Tokens: short‑lived, sender‑constrained where possible; revocation implemented; secrets in KMS/HSM.\n- Recovery: single‑use, hashed, time‑boxed tokens; consistent responses; re‑auth required after reset; sessions rotated.\n- Abuse: rate limits, throttling, and anomaly detection on auth endpoints; uniform error handling.\n- Admin: isolated flows with stricter policies and device checks.\n\n### Test Plan\n- Unit/integration tests for login, MFA enroll/verify, resets, and lockouts with uniform errors.\n- Protocol tests: PKCE, state/nonce, redirect URI validation, token audience/scope.\n- Dynamic tests for credential stuffing resistance and token replay; validate revocation after logout and role change.\n"
  },
  {
    "path": "sources/core/codeguard-0-authorization-access-control.md",
    "content": "---\ndescription: Authorization and access control (RBAC/ABAC/ReBAC, IDOR, mass assignment,\n  transaction auth)\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\n- yaml\nalwaysApply: false\n---\n\n## Authorization & Access Control\n\nEnforce least privilege and precise access decisions for every request and resource, prevent IDOR and mass assignment, and provide strong transaction authorization where necessary.\n\n### Core Principles\n1.  Deny by Default: The default for any access request should be 'deny'. Explicitly grant permissions to roles or users rather than explicitly denying them. When no allow rule matches, return HTTP 403 Forbidden.\n2.  Principle of Least Privilege: Grant users the minimum level of access required to perform their job functions. Regularly audit permissions to ensure they are not excessive.\n3.  Validate Permissions on Every Request: Check authorization for every single request, regardless of source (AJAX, API, direct). Use middleware/filters to ensure consistent enforcement.\n4.  Prefer ABAC/ReBAC over RBAC: Use Attribute-Based Access Control (ABAC) or Relationship-Based Access Control (ReBAC) for fine-grained permissions instead of simple role-based access control.\n\n### Systemic Controls\n- Centralize authorization at service boundaries via middleware/policies/filters.\n- Model permissions at the resource level (ownership/tenancy) and enforce scoping in data queries.\n- Return generic 403/404 responses to avoid leaking resource existence.\n- Log all denials with user, action, resource identifier (non-PII), and rationale code.\n\n### Preventing IDOR\n- Never trust user-supplied identifiers alone. Always verify access to each object instance.\n- Resolve resources through user-scoped queries or server-side lookups. Example: `currentUser.projects.find(id)` instead of `Project.find(id)`.\n- Use non-enumerable identifiers (UUIDs/random) as defense-in-depth. Do not rely on obscurity alone.\n\n### Preventing Mass Assignment\n- Do not bind request bodies directly to domain objects containing sensitive fields.\n- Expose only safe, editable fields via DTOs. Maintain explicit allow-lists for patch/update.\n- Use framework features to block-list sensitive fields if allow-listing is infeasible.\n\n### Transaction Authorization (Step-Up)\n- Require a second factor for sensitive actions (wire transfers, privilege elevation, data export). Apply What‑You‑See‑Is‑What‑You‑Sign: show critical fields for user confirmation.\n- Use unique, time‑limited authorization credentials per transaction; reject on data changes mid‑flow.\n- Enforce the chosen authorization method server-side; prevent client‑side downgrades.\n- Protect against brute-force with throttling and complete flow restarts after failures.\n\n### Testing and Automation\n- Maintain an authorization matrix (YAML/JSON) listing endpoints/resources, roles/attributes, and expected outcomes.\n- Automate integration tests that iterate the matrix, mint role tokens, and assert allow/deny results—including token expiry/revocation cases.\n- Exercise negative tests: swapped IDs, downgraded roles, missing scopes, and bypass attempts.\n\n### Implementation Checklist\n- Middleware/policies enforce deny-by-default and resource checks on every endpoint.\n- Query scoping ensures users only access permitted rows/objects.\n- DTOs and allow-lists prevent mass assignment; sensitive fields never bindable.\n- Step-up authorization in place for sensitive operations with unique, short-lived credentials.\n- Authorization matrix drives CI tests; failures block merges.\n"
  },
  {
    "path": "sources/core/codeguard-0-client-side-web-security.md",
    "content": "---\ndescription: Client-side web security (XSS/DOM XSS, CSP, CSRF, clickjacking, XS-Leaks,\n  third-party JS)\nlanguages:\n- c\n- html\n- javascript\n- php\n- typescript\n- vlang\ntags:\n- web\nalwaysApply: false\n---\n\n## Client‑side Web Security\n\nProtect browser clients against code injection, request forgery, UI redress, cross‑site leaks, and unsafe third‑party scripts with layered, context‑aware controls.\n\n### XSS Prevention (Context‑Aware)\n- HTML context: prefer `textContent`. If HTML is required, sanitize with a vetted library (e.g., DOMPurify) and strict allow‑lists.\n- Attribute context: always quote attributes and encode values.\n- JavaScript context: do not build JS from untrusted strings; avoid inline event handlers; use `addEventListener`.\n- URL context: validate protocol/domain and encode; block `javascript:` and data URLs where inappropriate.\n- Redirects/forwards: never use user input directly for destinations; use server-side mapping (ID→URL) or validate against trusted domain allowlists.\n- CSS context: allow‑list values; never inject raw style text from users.\n\nExample sanitization:\n```javascript\nconst clean = DOMPurify.sanitize(userHtml, {\n  ALLOWED_TAGS: ['b','i','p','a','ul','li'],\n  ALLOWED_ATTR: ['href','target','rel'],\n  ALLOW_DATA_ATTR: false\n});\n```\n\n### DOM‑based XSS and Dangerous Sinks\n- Prohibit `innerHTML`, `outerHTML`, `document.write` with untrusted data.\n- Prohibit `eval`, `new Function`, string‑based `setTimeout/Interval`.\n- Validate and encode data before assigning to `location` or event handler properties.\n- Use strict mode and explicit variable declarations to prevent global namespace pollution from DOM clobbering.\n- Adopt Trusted Types and enforce strict CSP to prevent DOM sinks exploitation.\n\nTrusted Types + CSP:\n```http\nContent-Security-Policy: script-src 'self' 'nonce-{random}'; object-src 'none'; base-uri 'self'; require-trusted-types-for 'script'\n```\n\n### Content Security Policy (CSP)\n- Prefer nonce‑based or hash‑based CSP over domain allow‑lists.\n- Start with Report‑Only mode; collect violations; then enforce.\n- Baseline to aim for: `default-src 'self'; style-src 'self' 'unsafe-inline'; frame-ancestors 'self'; form-action 'self'; object-src 'none'; base-uri 'none'; upgrade-insecure-requests`.\n\n### CSRF Defense\n- Fix XSS first; then layer CSRF defenses.\n- Use framework‑native CSRF protections and synchronizer tokens on all state‑changing requests.\n- Cookie settings: `SameSite=Lax` or `Strict`; sessions `Secure` and `HttpOnly`; use `__Host-` prefix when possible.\n- Validate Origin/Referer; require custom headers for API mutations in SPA token models.\n- Never use GET for state changes; validate tokens on POST/PUT/DELETE/PATCH only. Enforce HTTPS for all token transmission.\n\n### Clickjacking Defense\n- Primary: `Content-Security-Policy: frame-ancestors 'none'` or a specific allow‑list.\n- Fallback for legacy browsers: `X-Frame-Options: DENY` or `SAMEORIGIN`.\n- Consider UX confirmations for sensitive actions when framing is required.\n\n### Cross‑Site Leaks (XS‑Leaks) Controls\n- Use `SameSite` cookies appropriately; prefer `Strict` for sensitive actions.\n- Adopt Fetch Metadata protections to block suspicious cross‑site requests.\n- Isolate browsing contexts: COOP/COEP and CORP where applicable.\n- Disable caching and add user‑unique tokens for sensitive responses to prevent cache probing.\n\n### Third‑Party JavaScript\n- Minimize and isolate: prefer sandboxed iframes with `sandbox` and postMessage origin checks.\n- Use Subresource Integrity (SRI) for external scripts and monitor for changes.\n- Provide a first‑party, sanitized data layer; deny direct DOM access from tags where possible.\n- Govern via tag manager controls and vendor contracts; keep libraries updated.\n\nSRI example:\n```html\n<script src=\"https://cdn.vendor.com/app.js\"\n  integrity=\"sha384-...\" crossorigin=\"anonymous\"></script>\n```\n\n### HTML5, CORS, WebSockets, Storage\n- postMessage: always specify exact target origin; verify `event.origin` on receive.\n- CORS: avoid `*`; allow‑list origins; validate preflights; do not rely on CORS for authz.\n- WebSockets: require `wss://`, origin checks, auth, message size limits, and safe JSON parsing.\n- Client storage: never store secrets in `localStorage`/`sessionStorage`; prefer HttpOnly cookies; if unavoidable, isolate via Web Workers.\n- Links: add `rel=\"noopener noreferrer\"` to external `target=_blank` links.\n\n### HTTP Security Headers (Client Impact)\n- HSTS: enforce HTTPS everywhere.\n- X‑Content‑Type‑Options: `nosniff`.\n- Referrer‑Policy and Permissions‑Policy: restrict sensitive signals and capabilities.\n\n### AJAX and Safe DOM APIs\n- Avoid dynamic code execution; use function callbacks, not strings.\n- Build JSON with `JSON.stringify`; never via string concatenation.\n- Prefer creating elements and setting `textContent`/safe attributes over raw HTML insertion.\n\n### Implementation Checklist\n- Contextual encoding/sanitization for every sink; no dangerous APIs without guards.\n- Strict CSP with nonces and Trusted Types; violations monitored.\n- CSRF tokens on all state‑changing requests; secure cookie attributes.\n- Frame protections set; XS‑Leak mitigations enabled (Fetch Metadata, COOP/COEP/CORP).\n- Third‑party JS isolated with SRI and sandbox; vetted data layer only.\n- HTML5/CORS/WebSocket usage hardened; no secrets in web storage.\n- Security headers enabled and validated.\n\n### Test Plan\n- Automated checks for dangerous DOM/API patterns.\n- E2E tests for CSRF and clickjacking; CSP report monitoring.\n- Manual probes for XS‑Leaks (frame count, timing, cache) and open redirect behavior.\n"
  },
  {
    "path": "sources/core/codeguard-0-cloud-orchestration-kubernetes.md",
    "content": "---\ndescription: Kubernetes hardening (RBAC, admission policies, network policies, secrets,\n  supply chain)\nlanguages:\n- javascript\n- yaml\ntags:\n- infrastructure\nalwaysApply: false\n---\n\n## Cloud & Orchestration (Kubernetes)\n\nKubernetes cluster and workload hardening: identity, policy, networking, secrets, and supply chain controls.\n\n### Controls\n- Identity & RBAC: least privilege for users and service accounts; separate namespaces; bind only needed roles.\n- Policy: admission controls (OPA/Gatekeeper/Kyverno) for image sources, capabilities, root, network policies, and required labels/annotations.\n- Networking: default‑deny with network policies; explicit egress allow‑lists; service identity/mTLS within mesh where applicable.\n- Secrets: use KMS providers; avoid plaintext in manifests; rotate regularly; restrict secret mount paths.\n- Nodes: hardened OS, auto‑updates, minimal attack surface; isolate sensitive workloads with taints/tolerations and dedicated nodes.\n- Supply chain: verify image signatures; enforce provenance (SLSA/Sigstore) in admission.\n\n### Checklist\n- Namespaces per team/app; RBAC roles scoped; audit logging enabled.\n- Admission policies enforce image provenance, non‑root, dropped capabilities, read‑only root FS, and network policy presence.\n- Network policies in place for ingress/egress; service accounts scoped per deployment.\n\n### Verification\n- Cluster conformance and CIS benchmark scans.\n- Policy tests in CI for manifests (OPA unit tests); periodic admission dry‑run.\n\n### Incident Readiness\n- Enable audit logs and centralize; restrict access to etcd; backup/restore tested.\n- Define break‑glass roles with MFA and time‑bound approvals.\n"
  },
  {
    "path": "sources/core/codeguard-0-data-storage.md",
    "content": "---\ndescription: Data & storage security (DB isolation, TLS, least privilege, RLS/CLS,\n  backups, auditing)\nlanguages:\n- c\n- javascript\n- sql\n- yaml\ntags:\n- data-security\n- infrastructure\nalwaysApply: false\n---\n\n## Database Security Guidelines\n\nThis rule advises on securely configuring SQL and NoSQL databases to protect against data breaches and unauthorized access:\n\n- Backend Database Protection\n  - Isolate database servers from other systems and limit host connections.\n  - Disable network (TCP) access when possible; use local socket files or named pipes.\n  - Configure database to bind only on localhost when appropriate.\n  - Restrict network port access to specific hosts with firewall rules.\n  - Place database server in separate DMZ isolated from application server.\n  - Never allow direct connections from thick clients to backend database.\n\n- Transport Layer Security\n  - Configure database to only allow encrypted connections.\n  - Install trusted digital certificates on database servers.\n  - Use TLSv1.2+ with modern ciphers (AES-GCM, ChaCha20) for client connections.\n  - Verify digital certificate validity in client applications.\n  - Ensure all database traffic is encrypted, not just initial authentication.\n\n- Secure Authentication Configuration\n  - Always require authentication, including from local server connections.\n  - Protect accounts with strong, unique passwords.\n  - Use dedicated accounts per application or service.\n  - Configure minimum required permissions only.\n  - Regularly review accounts and permissions.\n  - Remove accounts when applications are decommissioned.\n  - Change passwords when staff leave or compromise is suspected.\n\n- Database Credential Storage\n  - Never store credentials in application source code.\n  - Store credentials in configuration files outside web root.\n  - Set appropriate file permissions for credential access.\n  - Never check credential files into source code repositories.\n  - Encrypt credential storage using built-in functionality when available.\n  - Use environment variables or secrets management solutions.\n\n- Secure Permission Management\n  - Apply principle of least privilege to all database accounts.\n  - Do not use built-in root, sa, or SYS accounts.\n  - Do not grant administrative rights to application accounts.\n  - Restrict account connections to allowed hosts only.\n  - Use separate databases and accounts for Development, UAT, and Production.\n  - Grant only required permissions (SELECT, UPDATE, DELETE as needed).\n  - Avoid making accounts database owners to prevent privilege escalation.\n  - Implement table-level, column-level, and row-level permissions when needed.\n\n- Database Configuration and Hardening\n  - Install required security updates and patches regularly.\n  - Run database services under low-privileged user accounts.\n  - Remove default accounts and sample databases.\n  - Store transaction logs on separate disk from main database files.\n  - Configure regular encrypted database backups with proper permissions.\n  - Disable unnecessary stored procedures and dangerous features.\n  - Implement database activity monitoring and alerting.\n\n- Platform-Specific Hardening\n  - SQL Server: Disable xp_cmdshell, CLR execution, SQL Browser service, Mixed Mode Authentication (unless required).\n  - MySQL/MariaDB: Run mysql_secure_installation, disable FILE privilege for users.\n  - PostgreSQL: Follow PostgreSQL security documentation guidelines.\n  - MongoDB: Implement MongoDB security checklist requirements.\n  - Redis: Follow Redis security guide recommendations.\n\nSummary:  \nIsolate database systems, enforce encrypted connections, implement strong authentication, store credentials securely using secrets management, apply least privilege permissions, harden database configurations, and maintain regular security updates and monitoring.\n"
  },
  {
    "path": "sources/core/codeguard-0-devops-ci-cd-containers.md",
    "content": "---\ndescription: DevOps, CI/CD, and containers (pipeline hardening, artifacts, Docker/K8s\n  images, virtual patching, toolchain)\nlanguages:\n- docker\n- javascript\n- powershell\n- shell\n- xml\n- yaml\ntags:\n- infrastructure\nalwaysApply: false\n---\n\n## DevOps, CI/CD, and Containers\n\nSecure the build, packaging, and deployment supply chain: protect pipelines and artifacts, harden containers, and use virtual patching and toolchain flags when necessary.\n\n### CI/CD Pipeline Security\n- Repos: protected branches; mandatory reviews; signed commits.\n- Secrets: never hardcode; fetch at runtime from vault/KMS; mask in logs.\n- Least privilege: ephemeral, isolated runners with minimal permissions.\n- Security gates in CI: SAST, SCA, DAST, IaC scanning; block on criticals.\n- Dependencies: pin via lockfiles; verify integrity; use private registries.\n- Sign everything: commits and artifacts (containers/jars) and verify prior to deploy; adopt SLSA provenance.\n\n### Docker and Container Hardening\n- User: run as non‑root; set `USER` in Dockerfile\n- Use `--security-opt=no-new-privileges` to prevent privilege escalation.\n- Capabilities: `--cap-drop all` and add only what you need; never `--privileged`.\n- Daemon socket: never mount `/var/run/docker.sock`\n- DO NOT enable TCP Docker daemon socket (`-H tcp://0.0.0.0:XXX`) without TLS.\n- Avoid `- \"/var/run/docker.sock:/var/run/docker.sock\"` in docker-compose files.\n- Filesystems: read‑only root, tmpfs for temp write; resource limits (CPU/mem).\n- Networks: avoid host network; define custom networks; limit exposed ports.\n- Images: minimal base (distroless/alpine), pin tags and digests; remove package managers and tools from final image; add `HEALTHCHECK`.\n- Secrets: Docker/Kubernetes secrets; never in layers/env; mount via runtime secrets.\n- Scanning: scan images on build and admission; block high‑severity vulns.\n\n### Node.js in Containers\n- Deterministic builds: `npm ci --omit=dev`; pin base image with digest.\n- Production env: `ENV NODE_ENV=production`.\n- Non‑root: copy with correct ownership and drop to `USER node`.\n- Signals: use an init (e.g., `dumb-init`) and implement graceful shutdown handlers.\n- Multi‑stage builds: separate build and runtime; mount secrets via BuildKit; use `.dockerignore`.\n\n### Virtual Patching (Temporary Mitigation)\n- Use WAF/IPS/ModSecurity for immediate protection when code fixes are not yet possible.\n- Prefer positive security rules (allow‑list) for accuracy; avoid exploit‑specific signatures.\n- Process: prepare tooling in advance; analyze CVEs; implement patches in log‑only first, then enforce; track and retire after code fix.\n\n### C/C++ Toolchain Hardening (when applicable)\n- Compiler: `-Wall -Wextra -Wconversion`, `-fstack-protector-all`, PIE (`-fPIE`/`-pie`), `_FORTIFY_SOURCE=2`, CFI (`-fsanitize=cfi` with LTO).\n- Linker: RELRO/now, noexecstack, NX/DEP and ASLR.\n- Debug vs Release: enable sanitizers in debug; enable hardening flags in release; assert in debug only.\n- CI checks: verify flags (`checksec`) and fail builds if protections missing.\n\n### Implementation Checklist\n- Pipeline: secrets in vault; ephemeral runners; security scans; signed artifacts with provenance.\n- Containers: non‑root, least privilege, read‑only FS, resource limits; no daemon socket mounts.\n- Images: minimal, pinned, scanned; healthchecks; `.dockerignore` maintained.\n- Node images: `npm ci`, `NODE_ENV=production`, proper init and shutdown.\n- Virtual patching: defined process; accurate rules; logs; retirement after fix.\n- Native builds: hardening flags enabled and verified in CI.\n"
  },
  {
    "path": "sources/core/codeguard-0-file-handling-and-uploads.md",
    "content": "---\ndescription: Secure file handling & uploads (validation, storage isolation, scanning,\n  safe delivery)\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\nalwaysApply: false\n---\n\n## File Upload Security Guidelines\n\nThis rule advises on secure file upload practices to prevent malicious file attacks and protect system integrity:\n\n- Extension Validation\n  - List allowed extensions only for business-critical functionality.\n  - Ensure input validation is applied before validating extensions.\n  - Avoid double extensions (e.g., `.jpg.php`) and null byte injection (e.g., `.php%00.jpg`).\n  - Use allowlist approach rather than denylist for file extensions.\n  - Validate extensions after decoding filename to prevent bypass attempts.\n\n- Content Type and File Signature Validation\n  - Never trust client-supplied Content-Type headers as they can be spoofed.\n  - Validate file signatures (magic numbers) in conjunction with Content-Type checking.\n  - Implement allowlist approach for MIME types as a quick protection layer.\n  - Use file signature validation but not as a standalone security measure.\n\n- Filename Security\n  - Generate random filenames (UUID/GUID) instead of using user-supplied names.\n  - If user filenames required, implement maximum length limits.\n  - Restrict characters to alphanumeric, hyphens, spaces, and periods only.\n  - Prevent leading periods (hidden files) and sequential periods (directory traversal).\n  - Avoid leading hyphens or spaces for safer shell script processing.\n\n- File Content Validation\n  - For images, apply image rewriting techniques to destroy malicious content.\n  - For Microsoft documents, use Apache POI for validation.\n  - Avoid ZIP files due to numerous attack vectors.\n  - Implement manual file review in sandboxed environments when resources allow.\n  - Integrate antivirus scanning and Content Disarm & Reconstruct (CDR) for applicable file types.\n\n- Storage Security\n  - Store files on different servers for complete segregation when possible.\n  - Store files outside webroot with administrative access only.\n  - If storing in webroot, set write-only permissions with proper access controls.\n  - Use application handlers that map IDs to filenames for public access.\n  - Consider database storage for specific use cases with DBA expertise.\n\n- Access Control and Authentication\n  - Require user authentication before allowing file uploads.\n  - Implement proper authorization levels for file access and modification.\n  - Set filesystem permissions on principle of least privilege.\n  - Scan files before execution if execution permission is required.\n\n- Upload and Download Limits\n  - Set proper file size limits for upload protection.\n  - Consider post-decompression size limits for compressed files.\n  - Implement request limits for download services to prevent DoS attacks.\n  - Use secure methods to calculate ZIP file sizes safely.\n\n- Additional Security Measures\n  - Protect file upload endpoints from CSRF attacks.\n  - Keep all file processing libraries securely configured and updated.\n  - Implement logging and monitoring for upload activities.\n  - Provide user reporting mechanisms for illegal content.\n  - Use secure extraction methods for compressed files.\n\nSummary:  \nImplement defense-in-depth for file uploads through multi-layered validation, secure storage practices, proper access controls, and comprehensive monitoring. Never rely on single validation methods and always generate safe filenames to prevent attacks.\n"
  },
  {
    "path": "sources/core/codeguard-0-framework-and-languages.md",
    "content": "---\ndescription: Framework & language security guides (Django/DRF, Laravel/Symfony/Rails,\n  .NET, Java/JAAS, Node.js, PHP config)\nlanguages:\n- c\n- java\n- javascript\n- kotlin\n- php\n- python\n- ruby\n- typescript\n- xml\n- yaml\nalwaysApply: false\n---\n\n## Framework & Language Guides\n\nApply secure‑by‑default patterns per platform. Harden configurations, use built‑in protections, and avoid common pitfalls.\n\n### Django\n- Disable DEBUG in production; keep Django and deps updated.\n- Enable `SecurityMiddleware`, clickjacking middleware, MIME sniffing protection.\n- Force HTTPS (`SECURE_SSL_REDIRECT`); configure HSTS; set secure cookie flags (`SESSION_COOKIE_SECURE`, `CSRF_COOKIE_SECURE`).\n- CSRF: ensure `CsrfViewMiddleware` and `{% csrf_token %}` in forms; proper AJAX token handling.\n- XSS: rely on template auto‑escaping; avoid `mark_safe` unless trusted; use `json_script` for JS.\n- Auth: use `django.contrib.auth`; validators in `AUTH_PASSWORD_VALIDATORS`.\n- Secrets: generate via `get_random_secret_key`; store in env/secrets manager.\n\n### Django REST Framework (DRF)\n- Set `DEFAULT_AUTHENTICATION_CLASSES` and restrictive `DEFAULT_PERMISSION_CLASSES`; never leave `AllowAny` for protected endpoints.\n- Always call `self.check_object_permissions(request, obj)` for object‑level authz.\n- Serializers: explicit `fields=[...]`; avoid `exclude` and `\"__all__\"`.\n- Throttling: enable rate limits (and/or at gateway/WAF).\n- Disable unsafe HTTP methods where not needed. Avoid raw SQL; use ORM/parameters.\n\n### Laravel\n- Production: `APP_DEBUG=false`; generate app key; secure file perms.\n- Cookies/sessions: enable encryption middleware; set `http_only`, `same_site`, `secure`, short lifetimes.\n- Mass assignment: use `$request->only()` / `$request->validated()`; avoid `$request->all()`.\n- SQLi: use Eloquent parameterization; validate dynamic identifiers.\n- XSS: rely on Blade escaping; avoid `{!! ... !!}` for untrusted data.\n- File uploads: validate `file`, size, and `mimes`; sanitize filenames with `basename`.\n- CSRF: ensure middleware and form tokens enabled.\n\n### Symfony\n- XSS: Twig auto‑escaping; avoid `|raw` unless trusted.\n- CSRF: use `csrf_token()` and `isCsrfTokenValid()` for manual flows; Forms include tokens by default.\n- SQLi: Doctrine parameterized queries; never concatenate inputs.\n- Command execution: avoid `exec/shell_exec`; use Filesystem component.\n- Uploads: validate with `#[File(...)]`; store outside public; unique names.\n- Directory traversal: validate `realpath`/`basename` and enforce allowed roots.\n- Sessions/security: configure secure cookies and authentication providers/firewalls.\n\n### Ruby on Rails\n- Avoid dangerous functions:\n\n```ruby\neval(\"ruby code here\")\nsystem(\"os command here\")\n`ls -al /` # (backticks contain os command)\nexec(\"os command here\")\nspawn(\"os command here\")\nopen(\"| os command here\")\nProcess.exec(\"os command here\")\nProcess.spawn(\"os command here\")\nIO.binread(\"| os command here\")\nIO.binwrite(\"| os command here\", \"foo\")\nIO.foreach(\"| os command here\") {}\nIO.popen(\"os command here\")\nIO.read(\"| os command here\")\nIO.readlines(\"| os command here\")\nIO.write(\"| os command here\", \"foo\")\n```\n\n- SQLi: always parameterize; use `sanitize_sql_like` for LIKE patterns.\n- XSS: default auto‑escape; avoid `raw`, `html_safe` on untrusted data; use `sanitize` allow‑lists.\n- Sessions: database‑backed store for sensitive apps; force HTTPS (`config.force_ssl = true`).\n- Auth: use Devise or proven libraries; configure routes and protected areas.\n- CSRF: `protect_from_forgery` for state‑changing actions.\n- Secure redirects: validate/allow‑list targets.\n- Headers/CORS: set secure defaults; configure `rack-cors` carefully.\n\n### .NET (ASP.NET Core)\n- Keep runtime and NuGet packages updated; enable SCA in CI.\n- Authz: use `[Authorize]` attributes; perform server‑side checks; prevent IDOR.\n- Authn/sessions: ASP.NET Identity; lockouts; cookies `HttpOnly`/`Secure`; short timeouts.\n- Crypto: use PBKDF2 for passwords, AES‑GCM for encryption; DPAPI for local secrets; TLS 1.2+.\n- Injection: parameterize SQL/LDAP; validate with allow‑lists.\n- Config: enforce HTTPS redirects; remove version headers; set CSP/HSTS/X‑Content‑Type‑Options.\n- CSRF: anti‑forgery tokens on state‑changing actions; validate on server.\n\n### Java and JAAS\n- SQL/JPA: use `PreparedStatement`/named parameters; never concatenate input.\n- XSS: allow‑list validation; sanitize output with reputable libs; encode for context.\n- Logging: parameterized logging to prevent log injection.\n- Crypto: AES‑GCM; secure random nonces; never hardcode keys; use KMS/HSM.\n- JAAS: configure `LoginModule` stanzas; implement `initialize/login/commit/abort/logout`; avoid exposing credentials; segregate public/private credentials; manage subject principals properly.\n\n### Node.js\n- Limit request sizes; validate and sanitize input; escape output.\n- Avoid `eval`, `child_process.exec` with user input; use `helmet` for headers; `hpp` for parameter pollution.\n- Rate limit auth endpoints; monitor event loop health; handle uncaught exceptions cleanly.\n- Cookies: set `secure`, `httpOnly`, `sameSite`; set `NODE_ENV=production`.\n- Keep packages updated; run `npm audit`; use security linters and ReDoS testing.\n\n### PHP Configuration\n- Production php.ini: `expose_php=Off`, log errors not display; restrict `allow_url_fopen/include`; set `open_basedir`.\n- Disable dangerous functions; set session cookie flags (`Secure`, `HttpOnly`, `SameSite=Strict`); enable strict session mode.\n- Constrain upload size/number; set resource limits (memory, post size, execution time).\n- Use Snuffleupagus or similar for additional hardening.\n\n### Implementation Checklist\n- Use each framework’s built‑in CSRF/XSS/session protections and secure cookie flags.\n- Parameterize all data access; avoid dangerous OS/exec functions with untrusted input.\n- Enforce HTTPS/HSTS; set secure headers.\n- Centralize secret management; never hardcode secrets; lock down debug in production.\n- Validate/allow‑list redirects and dynamic identifiers.\n- Keep dependencies and frameworks updated; run SCA and static analysis regularly.\n"
  },
  {
    "path": "sources/core/codeguard-0-iac-security.md",
    "content": "---\ndescription: Infrastructure as Code Security\nlanguages:\n- c\n- d\n- javascript\n- powershell\n- ruby\n- shell\n- yaml\ntags:\n- infrastructure\nalwaysApply: false\n---\n\n# Infrastructure as Code (IaC) Security\n\nWhen designing cloud infrastructure and writing Infrastructure as Code (IaC) in languages like Terraform and CloudFormation, always use secure practices and defaults such as preventing public exposure and follow the principle of least privilege. Actively identify security misconfigurations and provide secure alternatives.\n\n## Critical Security Patterns In Infrastructure as Code\n\n### Network security\n- **ALWAYS** restrict the access to remote administrative services, databases, LDAP, TACACS+, or other sensitive services. No service should be accessible from the entire Internet if it does not need to be. Instead, restrict access to a specific set of IP addresses or CIDR blocks which require access.\n    - Security Group and ACL inbound rules should **NEVER** allow `0.0.0.0/0` to remote administration ports (such as SSH 22, RDP 3389).\n    - Security Group and ACL inbound rules should **NEVER** allow `0.0.0.0/0` to database ports (such as 3306, 5432, 1433, 1521, 27017).\n    - Kubernetes API endpoints allow lists should **NEVER** allow `0.0.0.0/0`. EKS, AKS, GKE, and any other Kubernetes API endpoint should be restricted to an allowed list of CIDR addresses which require administrative access.\n    - **NEVER** expose cloud platform database services (RDS, Azure SQL, Cloud SQL) to all IP addresses `0.0.0.0/0`.\n- Generally prefer private networking, such as internal VPC, VNET, VPN, or other internal transit unless public network access is required.\n- **ALWAYS** enable VPC/VNET flow logs for network monitoring and security analysis.\n- **ALWAYS** implement default deny rules and explicit allow rules for required traffic only.\n- Generally prefer blocking egress traffic to the Internet by default. If egress is required appropriate traffic control solutions might include:\n    - Egress firewall or proxy with rules allowing access to specific required services.\n    - Egress security group (SG) or access control list (ACL) with rules allowing access to specific required IPs or CIDR blocks.\n    - DNS filtering to prevent access to malicious domains.\n\n### Data protection\n- **ALWAYS** configure data encryption at rest for all storage services including databases, file systems, object storage, and block storage.\n    - Enable encryption for cloud storage services (S3, Azure Blob, GCS buckets).\n    - Configure database encryption at rest for all database engines (RDS, Azure SQL, Cloud SQL, DocumentDB, etc.).\n    - Enable EBS/disk encryption for virtual machine storage volumes.\n- **ALWAYS** configure encryption in transit for all data communications.\n    - Use TLS 1.2 or higher for all HTTPS/API communications.\n    - Configure SSL/TLS for database connections with certificate validation.\n    - Enable encryption for inter-service communication within VPCs/VNETs.\n    - Use encrypted protocols for remote access (SSH, HTTPS, secure RDP).\n- **ALWAYS** implement data classification and protection controls based on sensitivity levels.\n    - Apply stricter encryption and access controls for PII, PHI, financial data, and intellectual property.\n    - Use separate encryption keys for different data classification levels.\n- **ALWAYS** configure secure data retention and disposal policies.\n    - Define data retention periods based on regulatory and business requirements.\n    - Implement automated data lifecycle management with secure deletion.\n- **ALWAYS** enable comprehensive data access monitoring and auditing.\n    - Log all data access, modification, and deletion operations.\n    - Monitor for unusual data access patterns and potential data exfiltration.\n    - Implement real-time alerting for sensitive data access violations.\n- **ALWAYS** encrypt data backups.\n    - Encrypt all backup data using separate encryption keys from production data.\n    - Store backups in geographically distributed locations with appropriate access controls.\n    - Test backup restoration procedures regularly and verify backup integrity.\n\n### Access control\n- **NEVER** leave critical administration or data services with anonymous access (backups, storage, container registries, file shares) unless otherwise labeled as public classification or intended to be public.\n- **NEVER** use wildcard permissions in IAM policies or cloud RBAC (`\"Action\": \"*\"`, `\"Resource\": \"*\"`)\n- **NEVER** overprivilege service accounts with Owner/Admin roles when it is not necessary.\n- **NEVER** use service API Keys and client secrets and instead use workload identity with role-based access control to eliminate the need for long-lived credentials.\n- **NEVER** enable or use the legacy Instance Metadata Service version 1 (IMDSv1) in AWS.\n- **NEVER** use legacy or outdated authentication methods (such as local users) when there is a more secure alternative such as OAuth.\n\n### Container and VM images\n- **NEVER** use non-hardened VM and container images.\n- **ALWAYS** choose distroless or minimal container images.\n- **RECOMMEND** using secure baseline virtual machine images from trusted sources.\n- **RECOMMEND** using minimal distroless container images from trusted sources.\n\n### Logging and administrative access\n- **NEVER** disable administrative activity logging for sensitive services.\n- **ALWAYS** enable audit logging for privileged operations.\n\n### Secrets management\n- **NEVER** hardcode secrets, passwords, API keys, or certificates directly in IaC source code.\n- **ALWAYS** in Terraform mark secrets with \"sensitive = true\", in other IaC code use appropriate annotations or metadata to indicate sensitive values.\n\n### Backup and data recovery\n- **NEVER** create backups without encryption at rest and in transit.\n- **ALWAYS** configure multi-region data storage for backups with cross-region replication.\n- **NEVER** configure backups without retention policies and lifecycle management.\n"
  },
  {
    "path": "sources/core/codeguard-0-input-validation-injection.md",
    "content": "---\ndescription: Input validation and injection defense (SQL/SOQL/LDAP/OS), parameterization, prototype pollution\nlanguages:\n- apex\n- c\n- go\n- html\n- java\n- javascript\n- php\n- powershell\n- python\n- ruby\n- shell\n- sql\n- typescript\ntags:\n- web\nalwaysApply: false\n---\n\n## Input Validation & Injection Defense\n\nEnsure untrusted input is validated and never interpreted as code. Prevent injection across SQL, LDAP, OS commands, templating, and JavaScript runtime object graphs.\n\n### Core Strategy\n- Validate early at trust boundaries with positive (allow‑list) validation and canonicalization.\n- Treat all untrusted input as data, never as code. Use safe APIs that separate code from data.\n- Parameterize queries/commands; escape only as last resort and context‑specific.\n\n### Validation Playbook\n- Syntactic validation: enforce format, type, ranges, and lengths for each field.\n- Semantic validation: enforce business rules (e.g., start ≤ end date, enum allow‑lists).\n- Normalization: canonicalize encodings before validation; validate complete strings (regex anchors ^$); beware ReDoS.\n- Free‑form text: define character class allow‑lists; normalize Unicode; set length bounds.\n- Files: validate by content type (magic), size caps, and safe extensions; server‑generate filenames; scan; store outside web root.\n\n### SQL Injection Prevention\n- Use prepared statements and parameterized queries for 100% of data access.\n- Use bind variables for any dynamic SQL construction within stored procedures and never concatenate user input into SQL.\n- Prefer least‑privilege DB users and views; never grant admin to app accounts.\n- Escaping is fragile and discouraged; parameterization is the primary defense.\n\nExample (Java PreparedStatement):\n```java\nString custname = request.getParameter(\"customerName\");\nString query = \"SELECT account_balance FROM user_data WHERE user_name = ? \";  \nPreparedStatement pstmt = connection.prepareStatement( query );\npstmt.setString( 1, custname);\nResultSet results = pstmt.executeQuery( );\n```\n\n### SOQL/SOSL Injection (Salesforce)\n\nSOQL and SOSL are query/search languages (no SQL-style DDL/DML). Data changes are performed via Apex DML or Database methods. Note: SOQL can lock rows via `FOR UPDATE`.\n\n- Primary risk: data exfiltration by bypassing intended query filters/business logic; impact is amplified when Apex runs with elevated access (system mode) or when CRUD/FLS aren't enforced.\n- Second-order risk (conditional): if queried records are passed to DML, injection can broaden the record set and cause unintended mass updates/deletes.\n- Prefer static SOQL/SOSL with bind variables: `[SELECT Id FROM Account WHERE Name = :userInput]` or `FIND :term`.\n- For dynamic SOQL, use `Database.queryWithBinds()`; for dynamic SOSL, use `Search.query()`. Allow‑list any dynamic identifiers. If concatenation is unavoidable, escape string values with `String.escapeSingleQuotes()`.\n- Enforce CRUD/FLS with `WITH USER_MODE` or `WITH SECURITY_ENFORCED` (don't combine both). Enforce record sharing with `with sharing` or user-mode operations. Use `Security.stripInaccessible()` before DML.\n\n### LDAP Injection Prevention\n- Always apply context‑appropriate escaping:\n  - DN escaping for `\\ # + < > , ; \" =` and leading/trailing spaces\n  - Filter escaping for `* ( ) \\ NUL`\n- Validate inputs with allow‑lists before constructing queries; use libraries that provide DN/filter encoders.\n- Use least‑privilege LDAP connections with bind authentication; avoid anonymous binds for application queries.\n\n### OS Command Injection Defense\n- Prefer built‑in APIs instead of shelling out (e.g., library calls over `exec`).\n- If unavoidable, use structured execution that separates command and arguments (e.g., ProcessBuilder). Do not invoke shells.\n- Strictly allow‑list commands and validate arguments with allow‑list regex; exclude metacharacters (& | ; $ > < ` \\ ! ' \" ( ) and whitespace as needed).\n- Use `--` to delimit arguments where supported to prevent option injection.\n\nExample (Java ProcessBuilder):\n```java\nProcessBuilder pb = new ProcessBuilder(\"TrustedCmd\", \"Arg1\", \"Arg2\");\nMap<String,String> env = pb.environment();\npb.directory(new File(\"TrustedDir\"));\nProcess p = pb.start();\n```\n\n### Query Parameterization Guidance\n- Use the platform’s parameterization features (JDBC PreparedStatement, .NET SqlCommand, Ruby ActiveRecord bind params, PHP PDO, SQLx bind, etc.).\n- For stored procedures, ensure parameters are bound; never build dynamic SQL via string concatenation inside procedures.\n\n### Prototype Pollution (JavaScript)\n- Developers should use `new Set()` or `new Map()` instead of using object literals\n- When objects are required, create with `Object.create(null)` or `{ __proto__: null }` to avoid inherited prototypes.\n- Freeze or seal objects that should be immutable; consider Node `--disable-proto=delete` as defense‑in‑depth.\n- Avoid unsafe deep merge utilities; validate keys against allow‑lists and block `__proto__`, `constructor`, `prototype`.\n\n### Caching and Transport\n- Apply `Cache-Control: no-store` on responses containing sensitive data; enforce HTTPS across data flows.\n\n### Implementation Checklist\n- Central validators: types, ranges, lengths, enums; canonicalization before checks.\n- 100% parameterization coverage for SQL; dynamic identifiers via allow‑lists only.\n- LDAP DN/filter escaping in use; inputs validated prior to query.\n- No shell invocation for untrusted input; if unavoidable, structured exec + allow‑list + regex validation.\n- JS object graph hardened: safe constructors, blocked prototype paths, safe merge utilities.\n- File uploads validated by content, size, and extension; stored outside web root and scanned.\n\n### Test Plan\n- Static checks for string concatenation in queries/commands and dangerous DOM/merge sinks.\n- Fuzzing for SQL/LDAP/OS injection vectors; unit tests for validator edge cases.\n- Negative tests exercising blocked prototype keys and deep merge behavior.\n"
  },
  {
    "path": "sources/core/codeguard-0-logging.md",
    "content": "---\ndescription: Logging & monitoring (structured telemetry, redaction, integrity, detection\n  & alerting)\nlanguages:\n- c\n- javascript\n- yaml\ntags:\n- privacy\nalwaysApply: false\n---\n\n## Logging & Monitoring\n\nProduce structured, privacy‑aware telemetry that supports detection, response, and forensics without exposing secrets.\n\n### What to Log\n- Authn/authz events; admin actions; config changes; sensitive data access; input validation failures; security errors.\n- Include correlation/request IDs, user/session IDs (non‑PII), source IP, user agent, timestamps (UTC, RFC3339).\n\n### How to Log\n- Structured logs (JSON) with stable field names; avoid free‑form text for critical signals.\n- Sanitize all log inputs to prevent log injection (strip CR/LF/delimiters); validate data from other trust zones.\n- Redact/tokenize secrets and sensitive fields; never log credentials, tokens, recovery codes, or raw session IDs.\n- Ensure integrity: append‑only or WORM storage; tamper detection; centralized aggregation; access controls and retention policies.\n\n### Detection & Alerting\n- Build alerts for auth anomalies (credential stuffing patterns, impossible travel), privilege changes, excessive failures, SSRF indicators, and data exfil patterns.\n- Tune thresholds; provide runbooks; ensure on‑call coverage; test alert flows.\n\n### Storage & Protection\n- Isolate log storage (separate partition/database); strict file/directory permissions; store outside web‑accessible locations.\n- Synchronize time across systems; use secure protocols for transmission; implement tamper detection and monitoring.\n\n### Privacy & Compliance\n- Maintain data inventory and classification; minimize personal data in logs; honor retention and deletion policies.\n- Provide mechanisms to trace and delete user‑linked log data where required by policy.\n\n### Implementation Checklist\n- JSON logging enabled; log injection sanitization active; redaction filters active; correlation IDs on all requests.\n- Isolated log storage with tamper detection; centralized log pipeline with integrity protections; retention configured.\n- Security alerts defined and tested; dashboards and reports in place.\n\n### Validation\n- Unit/integration tests assert presence/absence of key fields; redaction unit tests.\n- Periodic audits for secret/PII leakage; tabletop exercises for incident workflows.\n"
  },
  {
    "path": "sources/core/codeguard-0-mcp-security.md",
    "content": "---\ndescription: MCP (Model Context Protocol) Security based on CoSAI MCP Security guidelines\nlanguages:\n- python\n- javascript\n- typescript\n- go\n- rust\n- java\nalwaysApply: false\n---\n\n# MCP (Model Context Protocol) Security Guidelines\n\nNEVER deploy MCP servers or clients without implementing proper security controls.\n\n### Workload Identity and Authentication\n- Use SPIFFE/SPIRE for cryptographic workload identities\n  - SPIFFE (Secure Production Identity Framework For Everyone) provides a standard for service identity\n  - SPIRE (SPIFFE Runtime Environment) issues and rotates short-lived cryptographic identities (SVIDs)\n\n### Input and Data Sanitization\n- Validate ALL inputs using allowlists at every trust boundary\n- Sanitize file paths through canonicalization\n- Use parameterized queries for database operations\n- Apply context-aware output encoding (SQL, shell, HTML)\n- Sanitize tool outputs: return only minimum fields, redact all PII and sensitive data\n- Treat ALL inputs, tool schemas, metadata, prompts, and resource content as untrusted input\n- Deploy prompt injection detection systems\n- Use strict JSON schemas to maintain boundaries between instructions and data\n\n### Sandboxing and Isolation\n- Design MCP servers to execute with least privilege\n- MCP servers interacting with host environment (files, commands, network) MUST implement sandboxing controls\n- LLM-generated code MUST NOT run with full user privileges\n- Implement additional sandboxing layers: gVisor, Kata Containers, SELinux sandboxes\n\n### Cryptographic Verification of Resources\n- Provide cryptographic signatures and SBOMs for all server code\n- Implement signature verification in your MCP client before loading servers\n- Use TLS for ALL data in transit\n- Implement remote attestation capabilities to verify servers are running expected code\n\n### Transport Layer Security\n\n#### stdio Transport (Local Servers)\n- STRONGLY RECOMMENDED for local MCP to eliminate DNS rebinding risks\n- Direct pipe-based stream communication\n- Implement sandbox to prevent privilege escalation\n\n#### HTTP Streaming Transport (Remote Servers)\nRequired security controls to implement:\n- Payload Limits (prevent large payload and recursive payload DoS)\n- Rate limiting for tool calls and transport requests\n- Client-Server Authentication/Authorization\n- Mutual TLS Authentication\n- TLS Encryption\n- CORS Protection\n- CSRF Protection\n- Integrity Checks (prevent replay, spoofing, poisoned responses)\n\n### Secure Tool and UX Design\n- Create single-purpose tools with explicit boundaries; avoid \"do anything\" tools\n- Do not rely on the LLM for validation or authorization decisions\n- Use two-stage commit for high-impact actions: draft/preview first, explicit commit with confirmation second\n- Provide rollback/undo paths (draft IDs, snapshots, reversible actions) and time-bound commits when possible\n\n### Human-in-the-Loop\n- Implement confirmation prompts for risky operations in your MCP server\n- Use elicitation on MCP server side to request user confirmation of risky actions\n- Security-relevant messages MUST clearly indicate implications\n- Do NOT rely solely on human approval (users can become fatigued)\n\n### Logging and Observability\n- Implement logging in your MCP servers and clients\n- Log: tools that were used, parameters, originating prompt\n- Use OpenTelemetry for end-to-end linkability of actions\n- Maintain immutable records of actions and authorizations\n\n---\n\n## Deployment Pattern Security\n\n### All-Local (stdio or http)\n- Security depends entirely on host system posture\n- Use `stdio` transport to avoid DNS rebinding risks\n- Use sandboxing to limit privilege escalation attacks\n- Appropriate for development and personal use\n\n### Single-Tenant Remote (http)\n- Authentication between client and server is REQUIRED\n- Use secure credential storage (OS keychains, secret managers)\n- Communication MUST be authenticated and encrypted\n- Enterprise clients should enforce authenticated server discovery with explicit allowlists\n\n### Multi-Tenant Remote (http)\n- Require robust tenant isolation, identity, and access control\n- Implement strong multi-tenancy controls (per-tenant encryption, role-based access control)\n- Prefer MCP servers hosted directly by service provider\n- Provide remote attestation when possible\n"
  },
  {
    "path": "sources/core/codeguard-0-mobile-apps.md",
    "content": "---\ndescription: 'Mobile app security (iOS/Android): storage, transport, code integrity,\n  biometrics, permissions'\nlanguages:\n- java\n- javascript\n- kotlin\n- matlab\n- perl\n- swift\n- xml\nalwaysApply: false\n---\n\n## Mobile Application Security Guidelines\n\nEssential security practices for developing secure mobile applications across iOS and Android platforms.\n\n### Architecture and Design\n\nImplement secure design principles from the start:\n- Follow least privilege and defense in depth principles\n- Use standard secure authentication protocols (OAuth2, JWT)\n- Perform all authentication and authorization checks server-side\n- Request only necessary permissions for app and backend services\n- Establish security controls for app updates, patches, and releases\n- Use only trusted and validated third-party libraries and components\n\n### Authentication and Authorization\n\nNever trust the client for security decisions:\n- Perform authentication/authorization server-side only\n- Do not store user passwords on device; use revocable access tokens\n- Avoid hardcoding credentials in the mobile app\n- Encrypt credentials in transmission\n- Use platform-specific secure storage (iOS Keychain, Android Keystore)\n- Require password complexity and avoid short PINs (4 digits)\n- Implement session timeouts and remote logout functionality\n- Require re-authentication for sensitive operations\n- Use platform-supported biometric authentication with secure fallbacks\n\n### Data Storage and Privacy\n\nProtect sensitive data at rest and in transit:\n- Encrypt sensitive data using platform APIs; avoid custom encryption\n- Leverage hardware-based security features (Secure Enclave, Strongbox)\n- Store private data on device's internal storage only\n- Minimize PII collection to necessity and implement automatic expiration\n- Avoid caching, logging, or background snapshots of sensitive data\n- Always use HTTPS for network communications\n\n### Network Communication\n\nAssume all network communication is insecure:\n- Use HTTPS for all network communication\n- Do not override SSL certificate validation for self-signed certificates\n- Use strong, industry standard cipher suites with appropriate key lengths\n- Use certificates signed by trusted CA providers\n- Consider certificate pinning for additional security\n- Encrypt data even if sent over SSL\n- Avoid sending sensitive data via SMS\n\n### Code Quality and Integrity\n\nMaintain application security throughout development:\n- Use static analysis tools to identify vulnerabilities\n- Make security a focal point during code reviews\n- Keep all libraries up to date to patch known vulnerabilities\n- Disable debugging in production builds\n- Include code to validate integrity of application code\n- Obfuscate the app binary\n- Implement runtime anti-tampering controls:\n  - Check for debugging, hooking, or code injection\n  - Detect emulator or rooted/jailbroken devices\n  - Verify app signatures at runtime\n\n### Platform-Specific Security\n\n#### Android Security\n- Use Android's ProGuard for code obfuscation\n- Avoid storing sensitive data in SharedPreferences\n- Disable backup mode to prevent sensitive data in backups\n- Use Android Keystore with hardware backing (TEE or StrongBox)\n- Implement Google's Play Integrity API for device and app integrity checks\n\n#### iOS Security\n- Configure Shortcuts permissions to require device unlock for sensitive actions\n- Set Siri intent `requiresUserAuthentication` to true for sensitive functionality\n- Implement authentication checks on deep link endpoints\n- Use conditional logic to mask sensitive widget content on lock screen\n- Store sensitive data in iOS Keychain, not plist files\n- Use Secure Enclave for cryptographic key storage\n- Implement App Attest API for app integrity validation\n- Use DeviceCheck API for persistent device state tracking\n\n### Testing and Monitoring\n\nValidate security controls through comprehensive testing:\n- Perform penetration testing including cryptographic vulnerability assessment\n- Leverage automated tests to ensure security features work as expected\n- Ensure security features do not harm usability\n- Use real-time monitoring to detect and respond to threats\n- Have a clear incident response plan in place\n- Plan for regular updates and implement forced update mechanisms when necessary\n\n### Input and Output Validation\n\nPrevent injection and execution attacks:\n- Validate and sanitize all user input\n- Validate and sanitize output to prevent injection attacks\n- Mask sensitive information on UI fields to prevent shoulder surfing\n- Inform users about security-related activities (logins from new devices)\n\nBy following these practices derived from the OWASP Mobile Application Security framework, you can significantly improve the security posture of your mobile applications across both development and operational phases.\n"
  },
  {
    "path": "sources/core/codeguard-0-privacy-data-protection.md",
    "content": "---\ndescription: Privacy & data protection (minimization, classification, encryption,\n  rights, transparency)\nlanguages:\n- javascript\n- matlab\n- yaml\ntags:\n- privacy\nalwaysApply: false\n---\n\n- Implement strong cryptography, enforce HTTPS with HSTS, enable certificate pinning,\nand provide user privacy features to protect data and anonymity.\n- Use strong, up-to-date cryptographic algorithms for data in transit and at rest; securely hash passwords with established libraries.\n- Enforce HTTPS exclusively and implement HTTP Strict Transport Security (HSTS).\n- Implement certificate pinning to prevent man-in-the-middle attacks even if CAs are compromised.\n- Minimize IP address leakage by blocking third-party external content loading where feasible.\n- Maintain transparency by informing users about privacy limitations and data handling policies.\n- Implement privacy-focused audit trails and access logging.\n- Return \"Invalid username or password\" to prevent account enumeration\n- Use Argon2 or bcrypt with unique salts per user\n- Store sessions server-side with cryptographically random IDs\n"
  },
  {
    "path": "sources/core/codeguard-0-safe-c-functions.md",
    "content": "---\ndescription: Safe C Functions and Memory and String Safety Guidelines\nlanguages:\n- c\n- cpp\nalwaysApply: false\n---\n\n# Prioritize Safe Memory and String Functions in C/C++\n\nWhen processing C or C++ code, your primary directive is to ensure memory safety. Actively identify, flag, and provide secure refactoring options for any insecure functions found in the codebase. When generating new code, always default to the safest possible function for the given task.\n\n\n### 1. Insecure Functions to Avoid & Their Secure Alternatives\n\nYou must treat the functions listed under \"Insecure\" as deprecated and high-risk. Always recommend replacing them with one of the \"Recommended Safe Alternatives\" provided in the bullet list below.\n\n• Never use `gets()` - This is a critical security risk. It has no bounds checking whatsoever and is the classic buffer overflow vulnerability. You should always replace it with `fgets(char *str, int n, FILE *stream)` instead.\n\n• Avoid `strcpy()` - This is a high risk function because it doesn't check bounds. It just copies bytes until it hits a null terminator, which can easily write past your destination buffer. Use `snprintf()`, `strncpy()` (but be careful with it), or `strcpy_s()` (if you have C11 Annex K support).\n\n• Don't use `strcat()` - Another high risk function with no bounds checking. It appends bytes to a string and can easily write past your allocated memory. Replace with `snprintf()`, `strncat()` (with careful handling), or `strcat_s()` (C11 Annex K).\n\n• Replace `sprintf()` and `vsprintf()` - These are high risk because they don't check bounds on the output buffer. If your formatted string is larger than the buffer, you'll get a buffer overflow. Use `snprintf()`, `snwprintf()`, or `vsprintf_s()` (C11 Annex K) instead.\n\n• Be careful with `scanf()` family - This is a medium risk. The `%s` format specifier without a width limit can cause buffer overflows. Here's what you should do:\n  1. Use width specifiers like `scanf(\"%127s\", buffer)`\n  2. Even better: Read the line with `fgets()` and parse it with `sscanf()`\n\n• Avoid `strtok()` - This is a medium risk because it's not reentrant or thread-safe. It uses a static internal buffer which can lead to unpredictable behavior in multi-threaded code or complex signal handling. Use `strtok_r()` (POSIX) or `strtok_s()` (C11 Annex K) instead.\n\n• Use `memcpy()` and `memmove()` carefully - These aren't inherently insecure, but they're a common source of bugs when you miscalculate the size argument or don't validate it properly. Here's what you should do:\n  1. Double-check your size calculations\n  2. Prefer `memcpy_s()` (C11 Annex K) when available\n  3. Use `memmove()` if source and destination buffers might overlap\n\n### 2. Actionable Implementation Guidelines\n\n#### For New Code Generation:\n\n- NEVER generate code that uses `gets()`, `strcpy()`, `strcat()`, or `sprintf()`.\n\n- DEFAULT to `snprintf()` for string formatting and concatenation, as it's often the most flexible and secure option.\n\n- DEFAULT to `fgets()` for reading string input from files or standard input.\n\n\n#### For Code Analysis and Refactoring:\n\n1. Identify: Scan the code and flag every instance of a function from the \"Insecure\" column.\n\n2. Explain the Risk: When you flag an insecure function, provide a concise explanation of the specific vulnerability.\n\n    - _Example Explanation:_ `Warning: The 'strcpy' function does not perform bounds checking and can lead to a buffer overflow if the source string is larger than the destination buffer. This is a common security vulnerability.`\n\n3. Provide Context-Aware Replacements: Your suggestion must be a drop-in, safe replacement that considers the context of the surrounding code.\n\n\n#### Use Compiler Flags:\n\nEnable these protective compiler flags to catch buffer overflow vulnerabilities at compile time and runtime:\n\n- Stack Protection: Use `-fstack-protector-all` or `-fstack-protector-strong` to detect stack buffer overflows\n- Address Sanitizer: Use `-fsanitize=address` during development to catch memory errors\n- Object Size Checking (OSC): Use `-D_FORTIFY_SOURCE=2` to enable runtime checks for buffer overflows in functions like `strcpy`, `strcat`, `sprintf`, etc. This adds bounds checking to many of the unsafe functions mentioned above\n- Format String Protection: Use `-Wformat -Wformat-security` to catch format string vulnerabilities\n\n### 3. Refactoring Examples\n\nYour suggestions should be concrete and actionable.\n\nExample 1: Replacing `strcpy`\n\n- Original Unsafe Code:\n\n    ```\n    char destination[64];\n    strcpy(destination, source_string);\n    ```\n\n- Your Suggested Refactoring:\n\n    ```\n    char destination[64];\n    snprintf(destination, sizeof(destination), \"%s\", source_string);\n    ```\n\n- Your Explanation: `Replaced 'strcpy' with 'snprintf' to ensure that no more than 63 characters plus a null terminator are written to the destination buffer, preventing a potential buffer overflow.`\n\n\nExample 2: Correcting `strncpy` Usage\n\nThe `strncpy` function is a common but imperfect replacement. It may not null-terminate the destination buffer. If you must use it or see it used, you must enforce correct handling.\n\n- Original (Potentially Unsafe) `strncpy`:\n\n    ```\n    // This is unsafe if strlen(source) >= 10\n    char dest[10];\n    strncpy(dest, source, sizeof(dest));\n    ```\n\n- Your Corrected Suggestion:\n\n    ```\n    char dest[10];\n    strncpy(dest, source, sizeof(dest) - 1);\n    dest[sizeof(dest) - 1] = '\\0';\n    ```\n\n- Your Explanation: `Added an explicit null termination for 'strncpy'. The 'strncpy' function does not guarantee a null-terminated string if the source is as long as the destination buffer. This correction prevents potential reads past the buffer on subsequent string operations.`\n\n\nExample 3: Securing `scanf`\n\n- Original Unsafe Code:\n\n    ```\n    char user_name[32];\n    printf(\"Enter your name: \");\n    scanf(\"%s\", user_name);\n    ```\n\n- Your Suggested Refactoring:\n\n    ```\n    char user_name[32];\n    printf(\"Enter your name: \");\n    if (fgets(user_name, sizeof(user_name), stdin)) {\n        // Optional: Remove trailing newline character from fgets\n        user_name[strcspn(user_name, \"\\n\")] = 0;\n    }\n    ```\n\n- Your Explanation: `Replaced 'scanf(\"%s\", ...)' with 'fgets()' to read user input. 'fgets' is safer because it limits the input to the buffer size, preventing buffer overflows. The original 'scanf' had no such protection.`\n\n\n### Memory and String Safety Guidelines\n\n#### Unsafe Memory Functions - FORBIDDEN\nNEVER use these unsafe memory functions that don't check input parameter boundaries:\n\n##### Banned Memory Functions:\n- `memcpy()` → Use `memcpy_s()`\n- `memset()` → Use `memset_s()`\n- `memmove()` → Use `memmove_s()`\n- `memcmp()` → Use `memcmp_s()`\n- `bzero()` → Use `memset_s()`\n- `memzero()` → Use `memset_s()`\n\n##### Safe Memory Function Replacements:\n```c\n// Instead of: memcpy(dest, src, count);\nerrno_t result = memcpy_s(dest, dest_size, src, count);\nif (result != 0) {\n// Handle error\n}\n\n// Instead of: memset(dest, value, count);\nerrno_t result = memset_s(dest, dest_size, value, count);\n\n// Instead of: memmove(dest, src, count);\nerrno_t result = memmove_s(dest, dest_size, src, count);\n\n// Instead of: memcmp(s1, s2, count);\nint indicator;\nerrno_t result = memcmp_s(s1, s1max, s2, s2max, count, &indicator);\nif (result == 0) {\n// indicator contains comparison result: <0, 0, or >0\n}\n```\n\n#### Unsafe String Functions - FORBIDDEN\nNEVER use these unsafe string functions that can cause buffer overflows:\n\n##### Banned String Functions:\n- `strstr()` → Use `strstr_s()`\n- `strtok()` → Use `strtok_s()`\n- `strcpy()` → Use `strcpy_s()`\n- `strcmp()` → Use `strcmp_s()`\n- `strlen()` → Use `strnlen_s()`\n- `strcat()` → Use `strcat_s()`\n- `sprintf()` → Use `snprintf()`\n\n##### Safe String Function Replacements:\n```c\n// String Search\nerrno_t strstr_s(char *dest, rsize_t dmax, const char *src, rsize_t slen, char **substring);\n\n// String Tokenization\nchar *strtok_s(char *dest, rsize_t *dmax, const char *src, char **ptr);\n\n// String Copy\nerrno_t strcpy_s(char *dest, rsize_t dmax, const char *src);\n\n// String Compare\nerrno_t strcmp_s(const char *dest, rsize_t dmax, const char *src, int *indicator);\n\n// String Length (bounded)\nrsize_t strnlen_s(const char *str, rsize_t strsz);\n\n// String Concatenation\nerrno_t strcat_s(char *dest, rsize_t dmax, const char *src);\n\n// Formatted String (always use size-bounded version)\nint snprintf(char *s, size_t n, const char *format, ...);\n```\n\n#### Implementation Examples:\n\n##### Safe String Copy Pattern:\n```c\n// Bad - unsafe\nchar dest[256];\nstrcpy(dest, src); // Buffer overflow risk!\n\n// Good - safe\nchar dest[256];\nerrno_t result = strcpy_s(dest, sizeof(dest), src);\nif (result != 0) {\n// Handle error: src too long or invalid parameters\nEWLC_LOG_ERROR(\"String copy failed: %d\", result);\nreturn ERROR;\n}\n```\n\n##### Safe String Concatenation Pattern:\n```c\n// Bad - unsafe\nchar buffer[256] = \"prefix_\";\nstrcat(buffer, suffix); // Buffer overflow risk!\n\n// Good - safe\nchar buffer[256] = \"prefix_\";\nerrno_t result = strcat_s(buffer, sizeof(buffer), suffix);\nif (result != 0) {\nEWLC_LOG_ERROR(\"String concatenation failed: %d\", result);\nreturn ERROR;\n}\n```\n\n##### Safe Memory Copy Pattern:\n```c\n// Bad - unsafe\nmemcpy(dest, src, size); // No boundary checking!\n\n// Good - safe\nerrno_t result = memcpy_s(dest, dest_max_size, src, size);\nif (result != 0) {\nEWLC_LOG_ERROR(\"Memory copy failed: %d\", result);\nreturn ERROR;\n}\n```\n\n##### Safe String Tokenization Pattern:\n```c\n// Bad - unsafe\nchar *token = strtok(str, delim); // Modifies original string unsafely\n\n// Good - safe\nchar *next_token = NULL;\nrsize_t str_max = strnlen_s(str, MAX_STRING_SIZE);\nchar *token = strtok_s(str, &str_max, delim, &next_token);\nwhile (token != NULL) {\n// Process token\ntoken = strtok_s(NULL, &str_max, delim, &next_token);\n}\n```\n\n#### Memory and String Safety Code Review Checklist:\n\n##### Pre-Code Review (Developer):\n- [ ] No unsafe memory functions (`memcpy`, `memset`, `memmove`, `memcmp`, `bzero`)\n- [ ] No unsafe string functions (`strcpy`, `strcat`, `strcmp`, `strlen`, `sprintf`, `strstr`, `strtok`)\n- [ ] All memory operations use `*_s()` variants with proper size parameters\n- [ ] Buffer sizes are correctly calculated using `sizeof()` or known limits\n- [ ] No hardcoded buffer sizes that could change\n\n##### Code Review (Reviewer):\n- [ ] Memory Safety: Verify all memory operations use safe variants\n- [ ] Buffer Bounds: Confirm destination buffer sizes are properly specified\n- [ ] Error Handling: Check that all `errno_t` return values are handled\n- [ ] Size Parameters: Validate that `rsize_t dmax` parameters are correct\n- [ ] String Termination: Ensure strings are properly null-terminated\n- [ ] Length Validation: Check that source string lengths are validated before operations\n\n##### Static Analysis Integration:\n- [ ] Enable compiler warnings for unsafe function usage\n- [ ] Use static analysis tools to detect unsafe function calls\n- [ ] Configure build system to treat unsafe function warnings as errors\n- [ ] Add pre-commit hooks to scan for banned functions\n\n#### Common Pitfalls and Solutions:\n\n##### Pitfall 1: Wrong Size Parameter\n```c\n// Wrong - using source size instead of destination size\nstrcpy_s(dest, strlen(src), src); // WRONG!\n\n// Correct - using destination buffer size\nstrcpy_s(dest, sizeof(dest), src); // CORRECT\n```\n\n##### Pitfall 2: Ignoring Return Values\n```c\n// Wrong - ignoring potential errors\nstrcpy_s(dest, sizeof(dest), src); // Error not checked\n\n// Correct - checking return value\nif (strcpy_s(dest, sizeof(dest), src) != 0) {\n// Handle error appropriately\n}\n```\n\n##### Pitfall 3: Using sizeof() on Pointers\n```c\n// Wrong - sizeof pointer, not buffer\nvoid func(char *buffer) {\nstrcpy_s(buffer, sizeof(buffer), src); // sizeof(char*) = 8!\n}\n\n// Correct - pass buffer size as parameter\nvoid func(char *buffer, size_t buffer_size) {\nstrcpy_s(buffer, buffer_size, src);\n}\n```\n\nYou must always explain how this rule was applied and why it was applied.\n"
  },
  {
    "path": "sources/core/codeguard-0-session-management-and-cookies.md",
    "content": "---\ndescription: Session management and secure cookies (rotation, fixation, timeouts,\n  theft detection)\nlanguages:\n- c\n- go\n- html\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\ntags:\n- authentication\n- web\nalwaysApply: false\n---\n\n## Session Management & Cookies\n\nImplement robust, attack-resistant session handling that prevents fixation, hijacking, and theft while maintaining usability.\n\n### Session ID Generation and Properties\n- Generate session IDs with a CSPRNG; ≥64 bits of entropy (prefer 128+). Opaque, unguessable, and free of meaning.\n- Use generic cookie names (e.g., `id`) rather than framework defaults. Reject any incoming ID not created by the server.\n- Store all session data server-side; never embed PII or privileges in the token. If sensitive, encrypt server-side session store at rest.\n\n### Cookie Security Configuration\n- Set `Secure`, `HttpOnly`, `SameSite=Strict` (or `Lax` if necessary for flows) on session cookies.\n- Scope cookies narrowly with `Path` and `Domain`. Avoid cross-subdomain exposure.\n- Prefer non-persistent session cookies (no Expires/Max-Age). Require full HTTPS; enable HSTS site-wide.\n\nExample header:\n```\nSet-Cookie: id=<opaque>; Secure; HttpOnly; SameSite=Strict; Path=/\n```\n\n### Session Lifecycle and Rotation\n- Create sessions only server-side; treat provided IDs as untrusted input.\n- Regenerate session ID on authentication, password changes, and any privilege elevation. Invalidate the prior ID.\n- Use distinct pre‑auth and post‑auth cookie names if framework patterns require it.\n\n### Expiration and Logout\n- Idle timeout: 2–5 minutes for high-value, 15–30 minutes for lower risk. Absolute timeout: 4–8 hours.\n- Enforce timeouts server-side. Provide a visible logout button that fully invalidates the server session and clears the cookie client-side.\n\n### Transport and Caching\n- Enforce HTTPS for the entire session journey. Never mix HTTP/HTTPS in one session.\n- Send `Cache-Control: no-store` on responses containing session identifiers or sensitive data.\n\n### Cookie Theft Detection and Response\n- Fingerprint session context server-side at establishment (IP, User-Agent, Accept-Language, relevant `sec-ch-ua` where available).\n- Compare incoming requests to the stored fingerprint, allowing for benign drift (e.g., subnet changes, UA updates).\n- Risk-based responses:\n  - High risk: require re-authentication; rotate session ID.\n  - Medium risk: step-up verification (challenge); rotate session ID.\n  - Low risk: log suspicious activity.\n- Always regenerate the session ID when potential hijacking is detected.\n\n### Client-Side Storage\n- Do not store session tokens in `localStorage`/`sessionStorage` due to XSS risk. Prefer HttpOnly cookies for transport.\n- If client-side storage is unavoidable for non-session secrets, isolate via Web Workers and never expose in page context.\n\n### Framework and Multi-Cookie Scenarios\n- Prefer built-in session frameworks; keep them updated and hardened.\n- Validate relationships when multiple cookies participate in session state; avoid same cookie names across paths/domains.\n\n### Monitoring and Telemetry\n- Log session lifecycle events (creation, rotation, termination) using salted hashes of the session ID, not raw values.\n- Monitor for brute force of session IDs and anomalous concurrent usage.\n\n### Implementation Checklist\n1) CSPRNG session IDs (≥64 bits entropy), opaque and server-issued only.\n2) Cookie flags: `Secure`, `HttpOnly`, `SameSite` set; tight domain/path.\n3) HTTPS-only with HSTS; no mixed content.\n4) Regenerate IDs on auth and privilege changes; invalidate old IDs.\n5) Idle and absolute timeouts enforced server-side; full logout implemented.\n6) `Cache-Control: no-store` for sensitive responses.\n7) Server-side fingerprinting and risk-based responses to anomalies.\n8) No client storage of session tokens; framework defaults hardened.\n"
  },
  {
    "path": "sources/core/codeguard-0-supply-chain-security.md",
    "content": "---\ndescription: Dependency & supply chain security (pinning, SBOM, provenance, integrity,\n  private registries)\nlanguages:\n- docker\n- javascript\n- yaml\nalwaysApply: false\n---\n\n## Dependency & Supply Chain Security\n\nControl third‑party risk across ecosystems, from selection and pinning to provenance, scanning, and rapid response.\n\n### Policy and Governance\n- Maintain allow‑listed registries and scopes; disallow direct installs from untrusted sources.\n- Require lockfiles and version pinning; prefer digest pinning for images and vendored assets.\n- Generate SBOMs for apps/images; store with artifacts; attest provenance (SLSA, Sigstore).\n\n### Package Hygiene (npm focus applicable to others)\n- Regularly audit (`npm audit`, ecosystem SCA) and patch; enforce SLAs by severity.\n- Use deterministic builds: `npm ci` (not `npm install`) in CI/CD; maintain lockfile consistency.\n- Avoid install scripts that execute on install when possible; review for risk.\n- Use `.npmrc` to scope private registries; avoid wildcard registries; enable integrity verification.\n- Enable account 2FA for publishing\n\n### Development Practices\n- Minimize dependency footprint; remove unused packages; prefer stdlib/first‑party for trivial tasks.\n- Protect against typosquatting and protestware: pin maintainers, monitor releases, and use provenance checks.\n- Hermetic builds: no network in compile/packaging stages unless required; cache with authenticity checks.\n\n### CI/CD Integration\n- SCA, SAST, IaC scans in gates; fail on criticals; require approvals for overrides with compensating controls.\n- Sign artifacts; verify signatures at deploy; enforce policy in admission.\n\n### Vulnerability Management\n- For patched vulnerabilities: test and deploy updates; document any API breaking changes.\n- For unpatched vulnerabilities: implement compensating controls (input validation, wrappers) based on CVE type; prefer direct dependency fixes over transitive workarounds.\n- Document risk decisions; escalate acceptance to appropriate authority with business justification.\n\n### Incident Response\n- Maintain rapid rollback; isolate compromised packages; throttle rollouts; notify stakeholders.\n- Monitor threat intel feeds (e.g., npm advisories); auto‑open tickets for critical CVEs.\n\n### Implementation Checklist\n- Lockfiles present; integrity checks on; private registries configured.\n- SBOM + provenance stored; signatures verified pre‑deploy.\n- Automated dependency updates with tests and review gates.\n- High‑sev vulns remediated within SLA or mitigated and documented.\n"
  },
  {
    "path": "sources/core/codeguard-0-xml-and-serialization.md",
    "content": "---\ndescription: XML security and safe deserialization (DTD/XXE hardening, schema validation,\n  no unsafe native deserialization)\nlanguages:\n- c\n- go\n- java\n- php\n- python\n- ruby\n- xml\nalwaysApply: false\n---\n\n## XML & Serialization Hardening\n\nSecure parsing and processing of XML and serialized data; prevent XXE, entity expansion, SSRF, DoS, and unsafe deserialization across platforms.\n\n### XML Parser Hardening\n- Disable DTDs and external entities by default; reject DOCTYPE declarations.\n- Validate strictly against local, trusted XSDs; set explicit limits (size, depth, element counts).\n- Sandbox or block resolver access; no network fetches during parsing; monitor for unexpected DNS activity.\n\n#### Java\nGeneral principle:\n```java\nfactory.setFeature(\"http://apache.org/xml/features/disallow-doctype-decl\", true);\n```\n\nDisabling DTDs protects against XXE and Billion Laughs attacks. If DTDs cannot be disabled, disable external entities using parser-specific methods.\n\n### Java\n\nJava parsers have XXE enabled by default.\n\nDocumentBuilderFactory/SAXParserFactory/DOM4J:\n\n```java\nDocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\nString FEATURE = null;\ntry {\n    // PRIMARY defense - disallow DTDs completely\n    FEATURE = \"http://apache.org/xml/features/disallow-doctype-decl\";\n    dbf.setFeature(FEATURE, true);\n    dbf.setXIncludeAware(false);\n} catch (ParserConfigurationException e) {\n    logger.info(\"ParserConfigurationException was thrown. The feature '\" + FEATURE\n    + \"' is not supported by your XML processor.\");\n}\n```\n\nIf DTDs cannot be completely disabled:\n\n```java\nDocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\nString[] featuresToDisable = {\n    \"http://xml.org/sax/features/external-general-entities\",\n    \"http://xml.org/sax/features/external-parameter-entities\",\n    \"http://apache.org/xml/features/nonvalidating/load-external-dtd\"\n};\n\nfor (String feature : featuresToDisable) {\n    try {    \n        dbf.setFeature(feature, false); \n    } catch (ParserConfigurationException e) {\n        logger.info(\"ParserConfigurationException was thrown. The feature '\" + feature\n        + \"' is probably not supported by your XML processor.\");\n    }\n}\ndbf.setXIncludeAware(false);\ndbf.setExpandEntityReferences(false);\ndbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);\n```\n\n#### .NET\n```csharp\nvar settings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Prohibit, XmlResolver = null };\nvar reader = XmlReader.Create(stream, settings);\n```\n\n#### Python\n```python\nfrom defusedxml import ElementTree as ET\nET.parse('file.xml')\n# or lxml\nfrom lxml import etree\nparser = etree.XMLParser(resolve_entities=False, no_network=True)\ntree = etree.parse('filename.xml', parser)\n```\n\n### Secure XSLT/Transformer Usage\n- Set `ACCESS_EXTERNAL_DTD` and `ACCESS_EXTERNAL_STYLESHEET` to empty; avoid loading remote resources.\n\n### Deserialization Safety\n- Never deserialize untrusted native objects. Prefer JSON with schema validation.\n- Enforce size/structure limits before parsing. Reject polymorphic types unless strictly allow‑listed.\n- Language specifics:\n  - PHP: avoid `unserialize()`; use `json_decode()`.\n  - Python: avoid `pickle` and unsafe YAML (`yaml.safe_load` only).\n  - Java: override `ObjectInputStream#resolveClass` to allow‑list; avoid enabling default typing in Jackson; use XStream allow‑lists.\n  - .NET: avoid `BinaryFormatter`; prefer `DataContractSerializer` or `System.Text.Json` with `TypeNameHandling=None` for JSON.NET.\n- Sign and verify serialized payloads where applicable; log and alert on deserialization failures and anomalies.\n\n### Implementation Checklist\n- DTDs off; external entities disabled; strict schema validation; parser limits set.\n- No network access during parsing; resolvers restricted; auditing in place.\n- No unsafe native deserialization; strict allow‑listing and schema validation for supported formats.\n- Regular library updates and tests with XXE/deserialization payloads.\n"
  },
  {
    "path": "sources/core/codeguard-1-crypto-algorithms.md",
    "content": "---\ndescription: Cryptographic Security Guidelines & Post-Quantum Readiness\nlanguages: []\nalwaysApply: true\n---\n\n# Cryptographic Security Guidelines & Post-Quantum Readiness\n\n## 1. Banned (Insecure) Algorithms\n\nThe following algorithms are known to be broken or fundamentally insecure. NEVER generate or use code with these algorithms.\n\n*   Hash: `MD2`, `MD4`, `MD5`, `SHA-0`\n*   Symmetric: `RC2`, `RC4`, `Blowfish`, `DES`, `3DES`\n*   Key Exchange: Static RSA, Anonymous Diffie-Hellman\n*   Classical: `Vigenère`\n\nReason: These are cryptographically broken and vulnerable to collision or man-in-the-middle attacks.\n\n## 2. Deprecated (Legacy/Weak) Algorithms\n\nThe following algorithms have known weaknesses or are considered obsolete. Avoid in new designs and prioritize migration.\n\n*   Hash: `SHA-1`\n*   Symmetric: `AES-CBC`, `AES-ECB`\n*   Signature: RSA with `PKCS#1 v1.5` padding\n*   Key Exchange: DHE with weak/common primes\n\n## 3. Recommended & Post-Quantum Ready Algorithms\n\nImplement these modern, secure algorithms to ensure resistance against both classical and quantum threats.\n\n### Symmetric Encryption\n*   Standard: `AES-GCM` (AEAD), `ChaCha20-Poly1305`(when allowed).\n*   PQC Requirement: Prefer AES-256 keys (or stronger) as they are resistant to quantum attacks (Grover's algorithm).\n*   Avoid: Custom crypto or unauthenticated modes.\n\n### Key Exchange (KEM)\n*   Standard: ECDHE (`X25519` or `secp256r1`)\n*   PQC Requirement: Use Hybrid Key Exchange (Classical + PQC) when supported.\n    *   Preferred: `X25519MLKEM768` (X25519 + ML-KEM-768)\n    *   Alternative: `SecP256r1MLKEM768` (P-256 + ML-KEM-768)\n    *   High Assurance: `SecP384r1MLKEM1024` (P-384 + ML-KEM-1024)\n*   Pure PQC: ML-KEM-768 (baseline) or ML-KEM-1024. Avoid ML-KEM-512 unless explicitly risk-accepted.\n*   Constraints:\n    *   Use vendor-documented identifiers (RFC 9242/9370).\n    *   Remove legacy/draft \"Hybrid-Kyber\" groups (e.g., `X25519Kyber`) and draft or hardcoded OIDs.\n\n### Signatures & Certificates\n*   Standard: ECDSA (`P-256`)\n*   PQC Migration: Continue using ECDSA (`P-256`) for mTLS and code signing until hardware-backed (HSM/TPM) ML-DSA is available.\n*   Hardware Requirement: Do not enable PQC ML-DSA signatures using software-only keys. Require HSM/TPM storage.\n\n### Protocol Versions\n*   (D)TLS: Enforce (D)TLS 1.3 only (or later).\n*   IPsec: Enforce IKEv2 only.\n    *   Use ESP with AEAD (AES-256-GCM).\n    *   Require PFS via ECDHE.\n    *   Implement RFC 9242 and RFC 9370 for Hybrid PQC (ML-KEM + ECDHE).\n    *   Ensure re-keys (CREATE_CHILD_SA) maintain hybrid algorithms.\n*   SSH: Enable only vendor-supported PQC/hybrid KEX (e.g., `sntrup761x25519`).\n\n## 4. Secure Implementation Guidelines\n\n### General Best Practices\n*   Configuration over Code: Expose algorithm choices in config/policy to allow agility without code changes.\n*   Key Management:\n    *   Use KMS/HSM for key storage.\n    *   Generate keys with a CSPRNG.\n    *   Separate encryption keys from signature keys.\n    *   Rotate keys per policy.\n    *   NEVER hardcode keys, secrets, or experimental OIDs.\n*   Telemetry: Capture negotiated groups, handshake sizes, and failure causes to monitor PQC adoption.\n\n### Deprecated SSL/Crypto APIs (C/OpenSSL) - FORBIDDEN\nNEVER use these deprecated functions. Use the replacement EVP high-level APIs.\n\n#### Symmetric Encryption (AES)\n- Deprecated: `AES_encrypt()`, `AES_decrypt()`\n- Replacement:\n\n  EVP_EncryptInit_ex()   // Use EVP_aes_256_gcm() for PQC readiness\n  EVP_EncryptUpdate()\n  EVP_EncryptFinal_ex()\n\n\n#### RSA/PKEY Operations\n- Deprecated: `RSA_new()`, `RSA_free()`, `RSA_get0_n()`\n- Replacement:\n\n  EVP_PKEY_new()\n  EVP_PKEY_up_ref()\n  EVP_PKEY_free()\n \n\n#### Hash & MAC Functions\n- Deprecated: `SHA1_Init()`, `HMAC()` (especially with SHA1)\n- Replacement:\n\n  EVP_DigestInit_ex() // Use SHA-256 or stronger\n  EVP_Q_MAC()         // For one-shot MAC\n\n\n## 5. Broccoli Project Specific Requirements\n- HMAC() with SHA1: Deprecated.\n- Replacement: Use HMAC with SHA-256 or stronger:\n\n\n// Example: Secure replacement for HMAC-SHA1\n```c\nEVP_Q_MAC(NULL, \"HMAC\", NULL, \"SHA256\", NULL, key, key_len, data, data_len, out, out_size, &out_len);\n```\n\n## 6. Secure Crypto Implementation Pattern\n\n\n// Example: Secure AES-256-GCM encryption (PQC-Ready Symmetric Strength)\n```c\nEVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();\nif (!ctx) handle_error();\n\n// Use AES-256-GCM\nif (EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, key, iv) != 1)\n    handle_error();\n\nint len, ciphertext_len;\nif (EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len) != 1)\n    handle_error();\nciphertext_len = len;\n\nif (EVP_EncryptFinal_ex(ctx, ciphertext + len, &len) != 1)\n    handle_error();\nciphertext_len += len;\n\nEVP_CIPHER_CTX_free(ctx);\n```\n"
  },
  {
    "path": "sources/core/codeguard-1-digital-certificates.md",
    "content": "---\ndescription: Certificate Best Practices\nlanguages: []\ntags:\n- secrets\nalwaysApply: true\n---\n\nWhen you encounter data that appears to be an X.509 certificate—whether embedded as a string or loaded from a file—you must flag the certificate for verification and ensure the following security properties are validated, reporting any concerns with clear explanations and recommended actions.\n\n### 1. How to Identify Certificate Data\n\nActively scan for certificate data using the following heuristics:\n\n- PEM-Encoded Strings: Identify multi-line string literals or constants that begin with `-----BEGIN CERTIFICATE-----` and end with `-----END CERTIFICATE-----`.\n\n- File Operations: Pay close attention to file read operations on files with common certificate extensions, such as `.pem`, `.crt`, `.cer`, and `.der`.\n\n- Library Function Calls: Recognize the usage of functions from cryptographic libraries used to load or parse certificates (e.g., OpenSSL's `PEM_read_X509`, Python's `cryptography.x509.load_pem_x509_certificate`, Java's `CertificateFactory`).\n\n\n### 2. Mandatory Sanity Checks\n\nOnce certificate data is identified, flag it for verification. The following properties must be validated to ensure the certificate meets security requirements:\n\n#### Verification Guidance\n\nTo inspect certificate properties, recommend running:\n```\nopenssl x509 -text -noout -in <certificate_file>\n```\n\nThis command displays expiration dates, key algorithm and size, signature algorithm, and issuer/subject information needed for the checks below.\n\n#### Check 1: Expiration Status\n\n- Condition: The certificate's `notAfter` (expiration) date is in the past.\n\n- Severity: CRITICAL VULNERABILITY\n\n- Report Message: `This certificate expired on [YYYY-MM-DD]. It is no longer valid and will be rejected by clients, causing connection failures. It must be renewed and replaced immediately.`\n\n- Condition: The certificate's `notBefore` (validity start) date is in the future.\n\n- Severity: Warning\n\n- Report Message: `This certificate is not yet valid. Its validity period begins on [YYYY-MM-DD].`\n\n\n#### Check 2: Public Key Strength\n\n- Condition: The public key algorithm or size is weak.\n\n    - Weak Keys: RSA keys with a modulus smaller than 2048 bits. Elliptic Curve (EC) keys using curves with less than a 256-bit prime modulus (e.g., `secp192r1`, `P-192`, `P-224`).\n\n- Severity: High-Priority Warning\n\n- Report Message: `The certificate's public key is cryptographically weak ([Algorithm], [Key Size]). Keys of this strength are vulnerable to factorization or discrete logarithm attacks. The certificate should be re-issued using at least an RSA 2048-bit key or an ECDSA key on a P-256 (or higher) curve.`\n\n\n#### Check 3: Signature Algorithm\n\n- Condition: The algorithm used to sign the certificate is insecure.\n\n    - Insecure Algorithms: Any signature algorithm using MD5 or SHA-1 (e.g., `md5WithRSAEncryption`, `sha1WithRSAEncryption`).\n\n- Severity: High-Priority Warning\n\n- Report Message: `The certificate is signed with the insecure algorithm '[Algorithm]'. This makes it vulnerable to collision attacks, potentially allowing for certificate forgery. It must be re-issued using a signature based on the SHA-2 family (e.g., sha256WithRSAEncryption).`\n\n\n#### Check 4: Issuer Type (Self-Signed Check)\n\n- Condition: The certificate's `Issuer` and `Subject` fields are identical.\n\n- Severity: Informational\n\n- Report Message: `This is a self-signed certificate. Ensure this is intentional and only used for development, testing, or internal services where trust is explicitly configured. Self-signed certificates should never be used for public-facing production systems as they will not be trusted by browsers or standard clients.`\n\n\n### 3. Actionable Examples\n\nYour feedback should be direct and easy to understand.\n\nExample 1: Flagging a Hardcoded Certificate\n\n- Code Snippet:\n\n    ```\n    # Certificate for connecting to legacy_service\n    LEGACY_CERT = \"\"\"\n    -----BEGIN CERTIFICATE-----\n    MIIC... (certificate data) ...\n    -----END CERTIFICATE-----\n    \"\"\"\n    ```\n\n- Your Analysis and Report:\n\n    > Security Review Required: A certificate is hardcoded in the `LEGACY_CERT` variable. Unverified certificates may contain critical vulnerabilities.\n    >\n    > - Concern: Hardcoded certificates cannot be easily rotated and may contain security issues that are not immediately visible in the code.\n    >\n    > - Required Verification: This certificate must be verified for:\n    >   - Expiration: Certificate must not be expired or not yet valid\n    >   - Key Strength: RSA keys must be at least 2048-bit; EC keys must use P-256 or higher curve\n    >   - Signature Algorithm: Must use SHA-2 family (not MD5 or SHA-1)\n    >   - Self-Signed: If self-signed, must only be used for development, testing, or internal services\n    >\n    > - Action: Verify this certificate meets the above requirements before deployment. Consider loading certificates from external files or a certificate store for easier rotation.\n    >\n\n\nExample 2: Flagging a Certificate Loaded from File\n\n- Code Snippet:\n\n    ```\n    // Load the server certificate from disk\n    cert, err := tls.LoadX509KeyPair(\"server.crt\", \"server.key\")\n    ```\n\n- Your Analysis and Report:\n\n    > Security Review Required: Certificate loaded from `server.crt`. Unverified certificates may contain critical vulnerabilities.\n    >\n    > - Required Verification: Ensure this certificate meets security requirements:\n    >   - Expiration: Certificate must not be expired or not yet valid\n    >   - Key Strength: RSA keys must be at least 2048-bit; EC keys must use P-256 or higher curve\n    >   - Signature Algorithm: Must use SHA-2 family (not MD5 or SHA-1)\n    >   - Self-Signed: If self-signed, must only be used for development, testing, or internal services\n    >\n    > - Action: Verify this certificate meets the above requirements before deployment.\n\n\nYou must always explain how this rule was applied and why it was applied.\n"
  },
  {
    "path": "sources/core/codeguard-1-hardcoded-credentials.md",
    "content": "---\ndescription: No Hardcoded Credentials\nlanguages: []\ntags:\n- secrets\nalwaysApply: true\n---\n\n# No Hardcoded Credentials\n\nNEVER store secrets, passwords, API keys, tokens or any other credentials directly in source code.\n\nTreat your codebase as public and untrusted. Any credential that appears in source code is compromised and must be handled through secure alternatives.\n\n#### NEVER hardcode these types of values:\n\nPasswords and Authentication:\n- Database passwords, user passwords, admin passwords\n- API keys, secret keys, access tokens, refresh tokens\n- Private keys, certificates, signing keys\n- Connection strings containing credentials\n- OAuth client secrets, webhook secrets\n- Any other credentials that could be used to access external services\n\n\n#### Recognition Patterns - Learn to Spot These Formats\n\nCommon Secret Formats You Must NEVER Hardcode:\n\n- AWS Keys: Start with `AKIA`, `AGPA`, `AIDA`, `AROA`, `AIPA`, `ANPA`, `ANVA`, `ASIA`\n- Stripe Keys: Start with `sk_live_`, `pk_live_`, `sk_test_`, `pk_test_`\n- Google API: Start with `AIza` followed by 35 characters\n- GitHub Tokens: Start with `ghp_`, `gho_`, `ghu_`, `ghs_`, `ghr_`\n- JWT Tokens: Three base64 sections separated by dots, starts with `eyJ`\n- Private Key Blocks: Any text between `-----BEGIN` and `-----END PRIVATE KEY-----`\n- Connection Strings: URLs with credentials like `mongodb://user:pass@host`\n\nWarning Signs in Your Code:\n- Variable names containing: `password`, `secret`, `key`, `token`, `auth`\n- Long random-looking strings that are not clear what they are\n- Base64 encoded strings near authentication code\n- Any string that grants access to external services\n\nYou must always explain how this rule was applied and why it was applied.\n"
  },
  {
    "path": "sources/core/codeguard-SKILLS.md.template",
    "content": "---\nname: software-security\ndescription: 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.\ncodeguard-version: \"x.x.x\"\nframework: \"Project CodeGuard\"\npurpose: \"Embed secure-by-default practices into AI coding workflows\"\n---\n\n# Software Security Skill (Project CodeGuard)\nThis 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.\n\n## When to Use This Skill\nThis skill should be activated when:\n- Writing new code in any language\n- Reviewing or modifying existing code\n- Implementing security-sensitive features (authentication, cryptography, data handling, etc.)\n- Working with user input, databases, APIs, or external services\n- Configuring cloud infrastructure, CI/CD pipelines, or containers\n- Handling sensitive data, credentials, or cryptographic operations\n\n## How to Use This Skill\nWhen writing or reviewing code:\n1. Always-Apply Rules: Some rules MUST be checked on every code operation:\n- `codeguard-1-hardcoded-credentials.md` - Never hardcode secrets, passwords, API keys, or tokens\n- `codeguard-1-crypto-algorithms.md` - Use only modern, secure cryptographic algorithms\n- `codeguard-1-digital-certificates.md` - Validate and manage digital certificates securely\n2. Context-Specific Rules: Apply rules from /rules directory based on the language of the feature being implemented using the table given below:\n<!-- LANGUAGE_MAPPINGS_START -->\n<!-- LANGUAGE_MAPPINGS_END -->\n3. Proactive Security: Don't just avoid vulnerabilities-actively implement secure patterns:\n- Use parameterized queries for database access\n- Validate and sanitize all user input\n- Apply least-privilege principles\n- Use modern cryptographic algorithms and libraries\n- Implement defense-in-depth strategies\n\n## CodeGuard Security Rules\nThe security rules are available in the `rules/` directory.\n\n### Usage Workflow\nWhen generating or reviewing code, follow this workflow:\n\n### 1. Initial Security Check\nBefore writing any code:\n- Check: Will this handle credentials? → Apply codeguard-1-hardcoded-credentials\n- Check: What language am I using? → Identify applicable language-specific rules\n- Check: What security domains are involved? → Load relevant rule files\n\n### 2. Code Generation\nWhile writing code:\n- Apply secure-by-default patterns from relevant Project CodeGuard rules\n- Add security-relevant comments explaining choices\n\n### 3. Security Review\nAfter writing code:\n- Review against implementation checklists in each rule\n- Verify no hardcoded credentials or secrets\n- Validate that all the rules have been successfully followed when applicable.\n- Explain which security rules were applied\n- Highlight security features implemented"
  },
  {
    "path": "sources/owasp/codeguard-0-ajax-security.md",
    "content": "---\ndescription: AJAX Security Best Practices for Client-Side Scripts\nlanguages:\n- c\n- html\n- javascript\n- php\n- typescript\nalwaysApply: false\n---\n\nIt is crucial to follow AJAX security best practices to prevent common web vulnerabilities. Here are some guidelines to keep in mind:\n\n### 1. Avoid Dangerous JavaScript Functions\n\nDynamically evaluating code with functions like `eval()`, `new Function()`, `setTimeout()`, and `setInterval()` with dynamic, unvalidated string arguments is a significant security risk. User input or untrusted data can lead to code injection vulnerabilities.\n\nBest Practice:\n\nInstead of using these functions with dynamic strings, use safer alternatives. For example, use anonymous functions for `setTimeout` and `setInterval`.\n\nExample of what to avoid:\n\n```javascript\n// Unsafe: susceptible to injection\nsetTimeout(\"alert('hello')\", 1000);\n\n// Safe:\nsetTimeout(() => alert('hello'), 1000);\n```\n\n### 2. Prevent Cross-Site Scripting (XSS) with `innerHTML`\n\nUsing `.innerHTML` to insert content into your web page can lead to XSS if the content is not properly sanitized.\n\nBest Practice:\n\nUse `.innerText` or `.textContent` when you only need to insert text. If you must insert HTML, use context-aware sanitization libraries before using `.innerHTML`.\n\nExample:\n\n```javascript\n// Unsafe:\ndocument.getElementById(\"div1\").innerHTML = untrusted_user_input;\n\n// Safe for text content:\ndocument.getElementById(\"div1\").textContent = untrusted_user_input;\n```\n\n### 3. Secure Client-Side Encryption and Secret Handling\n\nNever perform encryption or handle secrets on the client side. Client-side code is visible and can be manipulated by attackers.\n\nBest Practice:\n\nAll sensitive operations, including encryption and secret management, must be performed on the server.\n\n### 4. Safe JSON and XML Creation\n\nBuilding JSON or XML using string concatenation is error-prone and can lead to injection vulnerabilities.\n\nBest Practice:\n\nUse built-in browser APIs like `JSON.stringify()` or trusted third-party libraries to serialize data.\n\nExample:\n\n```javascript\n// Unsafe:\nlet jsonString = '{\"name\":\"' + name + '\",\"email\":\"' + email + '\"}';\n\n// Safe:\nlet jsonString = JSON.stringify({ name: name, email: email });\n```\n\n### 5. Server-Side Enforcement of Security and Business Logic\n\nClient-side validation is for user experience, not security. An attacker can easily bypass it.\n\nBest Practice:\n\nAlways enforce all security checks and business logic on the server. The server should never trust any data coming from the client.\n\n### Additional Recommendations:\n\n*   Input Validation: Rigorously validate all inputs on the server, as AJAX services can be called directly by attackers.\n*   CSRF Protection: Use anti-CSRF tokens for any state-changing AJAX requests. Store authentication tokens securely with HttpOnly and Secure flags for cookies.\n*   JSON Hijacking: Return JSON responses with an object as the outermost element to prevent JSON hijacking in older browsers.\n*   Schema Validation: Use JSON or XML schemas to validate the structure and types of data in AJAX requests and responses.\n*   OWASP Resources: The [OWASP Java Encoder Project](https://owasp.org/www-project-java-encoder/) is an excellent resource for server-side encoding."
  },
  {
    "path": "sources/owasp/codeguard-0-attack-surface-analysis.md",
    "content": "---\ndescription: Attack Surface Analysis Best Practices\nlanguages:\n- c\n- go\n- java\n- javascript\n- matlab\n- php\n- python\n- ruby\n- typescript\n- yaml\nalwaysApply: false\n---\n\nUnderstanding and managing your application's attack surface is a fundamental part of building secure software. Your application's attack surface is the sum of all points where an unauthorized user (an attacker) can try to enter data, extract data, or invoke execution. Here’s how to approach it:\n\n### 1. Identify and Document Your Attack Surface\n\nYou can't protect what you don't know about. The first step is to map out all the entry and exit points of your application.\n\n**Best Practices:**\n\n*   **Catalog Entry Points:** Identify all user interfaces (UIs), APIs (REST, GraphQL, etc.), file uploads, database connections, message queue consumers, and webhooks. Examples include `/api/users`, file upload endpoints, database connection strings, and Redis/RabbitMQ consumers.\n*   **Map Data Flows:** Document how data enters, is processed, and exits your system. This includes data formats being used (e.g., JSON, XML, serialized objects).\n*   **Integrate with CI/CD:** Add automated checks in your CI/CD pipeline to detect new endpoints or changes to existing ones. Use tools like OpenAPI spec validation or endpoint discovery to flag changes for security review.\n*   **Keep Documentation Current:** Maintain a living document, such as a `ATTACK_SURFACE.md` file in your repository, that details these components. Assign responsibility for updates to the development team lead and review quarterly.\n\n### 2. Analyze and Prioritize\n\nOnce you have a map, you need to assess the risk associated with each part of the attack surface.\n\n**Best Practices:**\n\n*   **Categorize Components:** Classify each entry point based on its exposure (e.g., public-facing vs. internal) and the sensitivity of the data it handles.\n*   **Secure APIs and Integrations:** For each API endpoint, ensure proper input validation, rate limiting (e.g., 100 requests/minute per user), authentication (OAuth2, JWT), and authorization checks. Regularly review third-party integration credentials and permissions.\n*   **Handle Authentication and Authorization:** Implement proper access controls for all endpoints. Document which user roles can access which resources. Regularly audit deprecated endpoints and remove or secure them properly.\n*   **Prioritize Reviews:** Focus your security efforts, such as code reviews and penetration testing, on the most exposed and highest-risk components. An unauthenticated public API handling user data is a higher priority than an internal admin dashboard.\n*   **Threat Model:** Use threat modeling to think like an attacker and identify potential vulnerabilities in your design.\n\n### 3. Reduce and Monitor\n\nA smaller attack surface is easier to defend.\n\n**Best Practices:**\n\n*   **Principle of Least Privilege:** If a feature, endpoint, or dependency is not needed, remove it. Disable unused features and interfaces.\n*   **Minimize Dependencies:** Every third-party library you add can potentially increase your attack surface. Regularly audit and remove unused dependencies.\n*   **Continuously Reassess:** Your application is always evolving. Make attack surface analysis a regular part of your development lifecycle, not a one-time activity.\n\n### 4. Implement Runtime Defenses\n\nSecure coding is essential, but it should be part of a defense-in-depth strategy with operational controls.\n\n**Best Practices:**\n\n*   **Deploy Web Application Firewalls (WAFs):** Configure WAF rules to block common attack patterns (SQL injection, XSS) and implement custom rules for your application's specific attack vectors.\n*   **Implement Rate Limiting:** Set up application-level rate limiting for critical endpoints (e.g., login, API calls) and infrastructure-level DDoS protection.\n*   **Enable Anomaly Detection:** Monitor for unusual traffic patterns, failed authentication attempts, and unexpected data access patterns. Set up alerts for suspicious activity.\n*   **Network Segmentation:** Use firewalls and network controls to limit communication between components. These are often defined in Infrastructure-as-Code (IaC) files (e.g., Terraform, CloudFormation), which should be reviewed for security.\n*   **Regular Security Monitoring:** Implement logging and monitoring for all entry points, with weekly reviews of security logs and monthly assessments of attack surface changes."
  },
  {
    "path": "sources/owasp/codeguard-0-authentication.md",
    "content": "---\ndescription: Authentication Security Best Practices\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\nalwaysApply: false\n---\n\nSecure authentication is one of the most critical aspects of application development.\n\n- Use non-public, random, and unique identifiers for users internally. For login, allow users to use their verified email address or a username, but ensure error messages are generic (e.g., \"Invalid username or password\") to prevent attackers from guessing valid accounts.\n- Implement proper password strength controls: minimum 8 characters, maximum 64+ characters to support passphrases, allow all characters including unicode and whitespace, and avoid composition rules (no requirements for uppercase/lowercase/numbers/special characters).\n- Block common and previously breached passwords using services like HaveIBeenPwned's Pwned Passwords API, and include a password strength meter to help users create stronger passwords.\n- Store user passwords using a modern, strong, and slow hashing algorithm like Argon2 (preferred) or bcrypt with recommended parameters. Use a unique salt for each user.\n- Support Password Managers: Use standard `<input type=\"password\">` fields and allow pasting to ensure compatibility with password managers.\n- Use TLS Everywhere: All communication transmitting credentials, session tokens, or any sensitive data must be over HTTPS.\n- When comparing password hashes, use a secure, constant-time comparison function to prevent timing attacks.\n- Protect against automated attacks: implement account lockout after failed login attempts, use CAPTCHA to prevent brute force attacks, and consider rate limiting on authentication endpoints.\n- Require multi-factor authentication (MFA) using TOTP (authenticator apps) or WebAuthn (hardware keys) for sensitive accounts. Consider making MFA mandatory for all users.\n- Implement secure session management: use HttpOnly and Secure flags for session cookies, rotate session IDs after login, and set appropriate session timeouts.\n- Before a user can change their password, require them to re-enter their current password. Require re-authentication for all sensitive features and transactions (password changes, email updates, financial transactions).\n- Implement secure password recovery mechanisms that don't reveal whether an account exists (\"If that email address is in our database, we will send you an email to reset your password\").\n- Enable comprehensive logging and monitoring: log all authentication failures, successful logins, account lockouts, and password changes for security monitoring and incident response.\n- Internal or administrative accounts should not be accessible from public login forms. Use a separate, more secure authentication system for internal users.\n- For delegated or federated authentication, use established protocols like OAuth 2.0, OpenID Connect (OIDC), or SAML. DO NOT build your own."
  },
  {
    "path": "sources/owasp/codeguard-0-authorization-testing-automation.md",
    "content": "---\ndescription: Authorization Testing Automation Best Practices\nlanguages:\n- go\n- java\n- javascript\n- python\n- ruby\n- typescript\n- xml\n- yaml\nalwaysApply: false\n---\n\nEnforce Automated Authorization Testing Using a Formal Authorization Matrix\n\nAutomate authorization tests driven by a formal authorization matrix to detect regressions early and ensure reliable access control enforcement.\n\n  1. **Define a formal authorization matrix file** (preferably XML or YAML) that lists:\n    - All features/services/endpoints.\n    - Logical roles and data filters if applicable.\n    - Expected HTTP response codes for allowed and denied accesses.\n    - Optional test payloads per service for richer testing.\n\n  2. **Secure the authorization matrix**: Store matrix files encrypted at rest with appropriate access controls. Version control the matrix and restrict write access to authorized personnel only.\n\n  3. **Automate integration tests** to:\n    - Dynamically load and iterate over the matrix entries.\n    - Generate role-based access tokens (e.g., JWT) matching each logical role.\n    - Handle token lifecycle: refresh, expiration, and revocation scenarios.\n    - Call each service endpoint as each role.\n    - Validate actual HTTP response codes against expected allowed/denied codes from the matrix.\n    - Report any discrepancies immediately, specifying involved role, service, and unexpected response.\n\n  4. **Environment-specific configuration**: Support different authorization matrices and token configurations for development, testing, and production environments.\n\n  5. **Secure the test environment**: Isolate test environments, use dedicated test accounts, and ensure test tokens cannot access production resources.\n\n  6. Factorize test logic to centralize token creation, service calls, and response validation while isolating role-based POVs for clear error profiling.\n\n  7. Maintain the authorization matrix as a living document alongside code; update it whenever authorization changes occur.\n\n  8. Provide a readable, auditable view of the authorization matrix (HTML via XSLT or similar) accessible to developers and auditors for verification."
  },
  {
    "path": "sources/owasp/codeguard-0-authorization.md",
    "content": "---\ndescription: Authorization Security Best Practices\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\n- yaml\nalwaysApply: false\n---\n\nImplementing robust authorization is critical to ensure users can only access the data and features they are permitted to. While authentication confirms who a user is, authorization determines what they can do.\n\n### Core Principles of Secure Authorization\n\n1.  **Deny by Default:** The default for any access request should be 'deny'. Explicitly grant permissions to roles or users rather than explicitly denying them. When no allow rule matches, return HTTP 403 Forbidden.\n2.  **Principle of Least Privilege:** Grant users the minimum level of access required to perform their job functions. Regularly audit permissions to ensure they are not excessive.\n3.  **Validate Permissions on Every Request:** Check authorization for every single request, regardless of source (AJAX, API, direct). Use middleware/filters to ensure consistent enforcement.\n4.  **Prefer ABAC/ReBAC over RBAC:** Use Attribute-Based Access Control (ABAC) or Relationship-Based Access Control (ReBAC) for fine-grained permissions instead of simple role-based access control.\n\n### Server-Side Enforcement is Non-Negotiable\n\nAll authorization decisions must be enforced on the server-side for every request. Client-side checks are for user experience only and can be easily bypassed.\n\n**What to Avoid (Anti-Pattern):**\n\n```javascript\n// Insecure: Client-side check that can be bypassed by an attacker.\nif (currentUser.isAdmin) {\n  showAdminDashboard();\n} // The server must also check if the user is an admin before returning data.\n```\n\n**Best Practice:**\n\nUse centralized middleware or decorators in your backend framework to enforce authorization checks consistently across all relevant endpoints.\n\n**Example (Express.js middleware showing deny-by-default):**\n\n```javascript\nfunction canViewProject(req, res, next) {\n  const project = await db.getProject(req.params.id);\n\n  // Explicit allow conditions\n  if (project.ownerId === req.user.id) {\n    return next(); // Owner can view\n  }\n  if (req.user.isAdmin) {\n    return next(); // Admin can view\n  }\n  if (project.isPublic && req.user.isVerified) {\n    return next(); // Verified users can view public projects\n  }\n\n  // Deny by default - no allow rule matched\n  return res.status(403).json({\n    error: 'Access denied',\n    message: 'You do not have permission to view this resource'\n  });\n}\n\napp.get('/projects/:id', isAuthenticated, canViewProject, (req, res) => {\n  // return project data\n});\n```\n\n### Prevent Insecure Direct Object References (IDOR)\n\nAn IDOR vulnerability occurs when an application uses a user-supplied identifier (like a database ID) to access an object directly, without verifying the user has permission to access *that specific object*.\n\n**What to Avoid (Anti-Pattern):**\n\n```javascript\n// Insecure: The code checks if the user is authenticated, but not if they\n// are authorized to view the invoice with the given ID.\napp.get('/invoices/:id', isAuthenticated, (req, res) => {\n  const invoice = await db.getInvoice(req.params.id); // Attacker can cycle through IDs\n  res.json(invoice);\n});\n```\n\n**Best Practice:**\n\nAlways verify that the authenticated user has the necessary permissions for the specific object they are requesting.\n\n### Additional Best Practices\n\n*   **Token Lifecycle Management:** Implement token revocation for logout/role changes and session invalidation when permissions change.\n*   **Centralized Error Handling:** Return generic error messages (403 Forbidden or 404 Not Found) when authorization fails to avoid information leakage.\n*   **Comprehensive Logging:** Log all authorization failures with user ID, resource, action, and timestamp for security monitoring.\n*   **Testing:** Write unit and integration tests for authorization logic. Test both positive (should have access) and negative (should be denied) cases.\n*   **Static Resources:** Apply authorization checks to static files, cloud storage, and other resources, not just API endpoints."
  },
  {
    "path": "sources/owasp/codeguard-0-bean-validation.md",
    "content": "---\ndescription: Bean Validation Security Best Practices\nlanguages:\n- java\n- xml\nalwaysApply: false\n---\n\nUsing a declarative, centralized approach to validation is crucial for security and maintainability. The Java Bean Validation standard (now Jakarta Validation) and its primary implementation, Hibernate Validator, provide a powerful way to handle this.\n\n### Why Use Bean Validation?\n\nInstead of scattering validation logic throughout your business layer, you define validation rules directly on your domain models (your \"beans\"). This keeps your validation logic in one place, making it consistent and easy to manage.\n\n### 1. Setting Up Your Project\n\nAdd Hibernate Validator to your `pom.xml`:\n\n```xml\n<dependency>\n    <groupId>org.hibernate.validator</groupId>\n    <artifactId>hibernate-validator</artifactId>\n    <version>8.0.0.Final</version>\n</dependency>\n```\n\nIf you're using Spring Boot, the `spring-boot-starter-web` dependency includes Hibernate Validator automatically.\n\n### 2. Annotating Your Beans\n\nApply standard validation annotations directly to the fields of your model classes. **Always combine @NotNull/@NotBlank with @Size constraints for sensitive fields.**\n\n**Example (`UserForm.java`):**\n```java\npublic class UserForm {\n\n    @NotBlank @Size(min = 2, max = 50)\n    private String name;\n\n    @NotBlank @Email @Size(max = 254)\n    private String email;\n\n    @NotBlank @Size(min = 8, max = 128)\n    @Pattern(regexp = \"[A-Za-z0-9@#$%^&+=]+\")\n    private String password;\n\n    // ... getters and setters\n}\n```\n\n### 3. Triggering Validation\n\nIn a web context (like a Spring MVC controller), use the `@Valid` annotation on your model attribute to trigger the validation process automatically.\n\n**Example (Spring Controller):**\n```java\n@RestController\npublic class UserController {\n\n    @PostMapping(\"/register\")\n    public ResponseEntity<?> register(@Valid @RequestBody UserForm form, BindingResult result) {\n        if (result.hasErrors()) {\n            logger.warn(\"Validation failed: {}\", result.getFieldErrors());\n            return ResponseEntity.badRequest().body(result.getFieldErrors());\n        }\n\n        userService.create(form);\n        return ResponseEntity.ok(\"Success\");\n    }\n}\n```\n\n### 4. Validating Nested Objects\n\nIf your model contains other objects that also need validation, just annotate them with `@Valid`.\n\n**Example:**\n```java\npublic class Order {\n    @Valid @NotNull\n    private Address shippingAddress;\n}\n```\n\n### Best Practices Summary\n\n*   **Centralize Rules:** Define validation constraints on your domain models.\n*   **Use Standard Annotations:** Leverage the rich set of built-in annotations (`@NotNull`, `@Size`, `@Pattern`, `@Min`, `@Max`, `@Email`, etc.).\n*   **Automate with `@Valid`:** Let your framework trigger validation automatically in your controllers.\n*   **Handle Errors Gracefully:** Use `BindingResult` to capture validation errors and return a meaningful `400 Bad Request` response. Never expose sensitive system information in error messages.\n*   **Create Custom Constraints:** For complex business rules that aren't covered by standard annotations, create your own custom validation constraints."
  },
  {
    "path": "sources/owasp/codeguard-0-browser-extension-vulnerabilities.md",
    "content": "---\ndescription: Browser Extension Security Best Practices\nlanguages:\n- c\n- javascript\n- typescript\nalwaysApply: false\n---\n\nA browser extension can access sensitive user data and modify web page content, making security a top priority. Here are the key areas to focus on when developing extensions.\n\n### 1. The Manifest: Your Security Foundation\n\nYour `manifest.json` file is the heart of your extension's security model. Configure it with the principle of least privilege.\n\n*   **Permissions:** Only request the permissions your extension absolutely needs to function. Avoid broad host permissions like `<all_urls>` or `http://*/*`. If you only need to access a few sites, specify them explicitly.\n*   **Content Security Policy (CSP):** Define a strict CSP to mitigate XSS and other injection attacks. A good starting point is:\n\n    ```json\n    \"content_security_policy\": {\n      \"extension_pages\": \"script-src 'self'; object-src 'self'\"\n    }\n    ```\n\n    This policy disallows inline scripts and `eval()`, and restricts script and object sources to your extension's own package.\n\n### 2. Secure Coding Practices\n\n*   **Avoid Dynamic Code Execution:** Never use `eval()`, `new Function()`, `setTimeout()` with strings, or dynamic `import()` of remote URLs. These are major security risks.\n\n*   **Sanitize DOM Inputs:** To prevent XSS from content scripts, never use `.innerHTML` with data that isn't fully sanitized. Prefer safer APIs like `.textContent`, or use a trusted library like DOMPurify.\n\n    **Example:**\n    ```javascript\n    // Insecure:\n    document.getElementById('user-greeting').innerHTML = `Welcome, ${userInput}!`;\n\n    // Secure:\n    document.getElementById('user-greeting').textContent = `Welcome, ${userInput}!`;\n    ```\n\n### 3. Data Storage and Communication\n\n*   **Use `chrome.storage`:** Avoid `localStorage` for storing any sensitive information. `localStorage` is accessible by any script on the same origin, including potentially malicious scripts injected into the page. Use the `chrome.storage` API instead, which is isolated to your extension.\n*   **Encrypt Sensitive Data:** Before storing any sensitive user data, encrypt it.\n*   **Use HTTPS:** All network communication must use HTTPS (`wss://` for WebSockets) to protect data in transit. Monitor network requests to prevent unauthorized data exfiltration.\n\n### 4. Interacting with Web Pages\n\n*   **Isolate Sensitive UI:** Do not inject sensitive information directly into a web page's DOM. A malicious script on the page could scrape this data. Instead, display sensitive information in extension-owned UI elements like popups, sidebars, or options pages.\n*   **Use Message Passing:** Use the standard message passing APIs (`chrome.runtime.sendMessage`, `chrome.tabs.sendMessage`) for communication between your content scripts and background scripts. Do not use the DOM as a communication channel.\n\n### 5. Supply Chain Security\n\n*   **Audit Dependencies:** Regularly audit your third-party libraries using tools like `npm audit`. A malicious or vulnerable dependency can compromise your entire extension.\n*   **No Remote Code:** Do not fetch and execute remote code. All of your extension's logic should be included in its initial package. This is a requirement for most browser extension marketplaces."
  },
  {
    "path": "sources/owasp/codeguard-0-c-based-toolchain-hardening.md",
    "content": "---\ndescription: C/C++ Toolchain Hardening Best Practices\nlanguages:\n- c\n- matlab\nalwaysApply: false\n---\n\nThe security of your compiled application depends heavily on the options you pass to your C/C++ compiler and linker. Modern toolchains provide a powerful set of features to harden your binaries against common exploitation techniques.\n\n### 1. Compiler Flags for Hardening\n\nThese flags should be a standard part of your release build configuration. **Note**: Some flags have platform-specific support.\n\n*   **Enable All Warnings:** Warnings often point to latent bugs. Start with a strong baseline.\n    *   **GCC/Clang:** `-Wall -Wextra -Wconversion`\n*   **Stack Smashing Protection:** This adds a \"canary\" to the stack to detect buffer overflows before they can be exploited.\n    *   **GCC/Clang:** `-fstack-protector-all`\n*   **Position-Independent Executables (PIE):** This allows the operating system to load the application at a random memory address (ASLR), making it much harder for attackers to predict memory layouts.\n    *   **Compiler:** `-fPIE` (Linux/Windows), `-fpie` (macOS)\n    *   **Linker:** `-pie`\n*   **Fortify Source:** This adds checks to common library functions (like `strcpy`, `printf`) to prevent buffer overflows.\n    *   **GCC/Clang:** `-D_FORTIFY_SOURCE=2` (Note: requires optimization `-O1` or higher).\n*   **Control Flow Integrity (CFI):** Guards against ROP/JOP attacks (Clang 3.5+).\n    *   **Clang:** `-fsanitize=cfi` (requires `-flto`)\n\n### 2. Linker Flags for Hardening\n\nThese flags control how your final executable is constructed.\n\n*   **Relocation Read-Only (RELRO):** This makes parts of the binary read-only after the dynamic linker has done its work, preventing certain exploitation techniques like GOT overwrites.\n    *   **GCC/Clang Linker:** `-Wl,-z,relro,-z,now`\n*   **Non-Executable Stack (NX):** This prevents code from being executed from the stack, a hallmark of many exploits.\n    *   **GCC/Clang Linker:** `-Wl,-z,noexecstack`\n*   **Additional Runtime Protections:**\n    *   **Linux:** `-Wl,-z,noexecheap` (prevent heap execution)\n    *   **Windows:** `/NXCOMPAT /DYNAMICBASE` (DEP and ASLR support)\n\n### 3. Build Configurations: Debug vs. Release\n\nMaintain separate, distinct build configurations for development and production.\n\n*   **Debug Builds:**\n    *   Disable optimizations (`-O0`) and enable full debugging information (`-g3`).\n    *   Define the `DEBUG` macro (`-DDEBUG`) and do **not** define `NDEBUG`.\n    *   Use sanitizers to detect memory errors at runtime (e.g., `-fsanitize=address,undefined,leak`).\n    *\t**Linux only:** Create a separate build with `fsanitize=memory` added to the compiler and linker flags. Do **not** add other sanitizers to this build.\n\n*   **Release Builds:**\n    *   Enable optimizations (e.g., `-O2`).\n    *   Define the `NDEBUG` macro (`-DNDEBUG`) to disable assertions and debugging code. Do **not** define `DEBUG`.\n    *   Include all the hardening flags mentioned above.\n\n### 4. Using Assertions Effectively\n\nAssertions are a powerful tool for catching bugs early.\n\n*   **Best Practice:** Use `assert()` liberally in your code to check for pre-conditions, post-conditions, and invariants. Assertions are automatically disabled in release builds (when `NDEBUG` is defined), so they have no performance impact on your production code.\n\n    ```c\n    void process_data(char *data, size_t len) {\n        assert(data != NULL && \"Data pointer cannot be null!\");\n        // ...\n    }\n    ```\n\n### 5. CI/CD Integration\n\nEnforce security flags in your build pipeline:\n\n**CMakeLists.txt example:**\n```cmake\nif(CMAKE_BUILD_TYPE STREQUAL \"Release\")\n    target_compile_options(${PROJECT_NAME} PRIVATE\n        -fstack-protector-all -fPIE -D_FORTIFY_SOURCE=2)\n    target_link_options(${PROJECT_NAME} PRIVATE\n        -pie -Wl,-z,relro,-z,now -Wl,-z,noexecstack)\nendif()\n```\n\n**Verification:** Add security checks to CI pipeline:\n```bash\n# Linux: Verify hardening flags were applied\nchecksec --file=./your_binary || exit 1\n```\n\n### 6. Verifying Your Binary\n\nDon't just trust that the flags worked. Use a tool to check the security properties of your final executable.\n\n*   **Linux:** Use the `checksec` tool.\n*   **Windows:** Use Microsoft's BinScope.\n*   **Dependency Security:** Regularly audit third-party libraries with tools like `npm audit` or OWASP Dependency-Check.\n\nBy integrating these toolchain hardening practices into your CI/CD pipeline, you can significantly raise the bar for attackers and build more resilient and secure C/C++ applications."
  },
  {
    "path": "sources/owasp/codeguard-0-choosing-and-using-security-questions.md",
    "content": "---\ndescription: Security Questions Implementation Guidelines\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\nalwaysApply: false\n---\n\nSecurity questions are no longer considered a strong authentication factor in authentication systems. Modern best practices strongly recommend against using them as a standalone method for authentication or account recovery.\n\n### Preferred Alternatives to Security Questions\n\n*   **Multi-Factor Authentication (MFA):** Implement time-based one-time passwords (TOTP), push notifications, or hardware security keys as your primary account recovery mechanism.\n*   **One-Time Recovery Codes:** Generate and provide users with a set of single-use recovery codes when they set up their account.\n*   **Email-Based Recovery:** Send a time-limited, single-use recovery link to a verified email address.\n\n### If You Must Use Security Questions (Legacy Systems)\n\nIf your organization requires security questions for legacy or compliance reasons, implement these critical safeguards:\n\n#### 1. Question Selection\n\n*   **Provide a Curated List:** Offer a predefined set of strong questions rather than allowing free-form user-created ones.\n*   **Question Quality:** Choose questions that are:\n    *   **Memorable:** Users can consistently recall the answer over time.\n    *   **Stable:** The answer doesn't change over the user's lifetime.\n    *   **Confidential:** Not easily discoverable through social media or public records.\n    *   **Specific:** Has a single, precise answer rather than multiple possible answers.\n\n#### 2. Answer Handling\n\n*   **Secure Storage:** Hash all answers using Argon2id or bcrypt with per-answer unique salt and system-wide pepper. Store data encrypted at rest with AES-256.\n*   **Normalization:** Before comparing answers, normalize them by removing extra spaces, converting to lowercase, and removing punctuation.\n*   **Input Validation:**\n    *   Enforce a minimum length but allow legitimate short answers.\n    *   Implement a denylist for weak answers like \"password\", \"123456\", or the user's own username/email.\n\n#### 3. Implementation Best Practices\n\n*   **Multi-Layered Security:** Use multiple questions together for increased security.\n*   **Consistent Question:** When a user fails to answer correctly, don't rotate to a different question—this helps prevent attackers from learning all the answers through multiple attempts.\n*   **Brute-Force Protection:** Implement rate limiting (max 5 attempts per hour), progressive delays, CAPTCHA after 3 failed attempts, and account lockout to prevent automated attacks.\n*   **Verify Email First:** Always verify ownership of the recovery email address before presenting security questions.\n*   **Require Authentication:** Require re-authentication (password or MFA) before allowing users to change their security questions or answers.\n*   **Secure Transport:** Use HTTPS/TLS 1.3+ for all recovery endpoints and implement certificate pinning where possible to prevent man-in-the-middle attacks.\n\nBy following these guidelines, you can minimize the risks associated with security questions while working toward implementing more secure authentication methods."
  },
  {
    "path": "sources/owasp/codeguard-0-ci-cd-security.md",
    "content": "---\ndescription: CI/CD Security Best Practices\nlanguages:\n- javascript\n- powershell\n- shell\n- xml\n- yaml\nalwaysApply: false\n---\n\nThe CI/CD pipeline is the backbone of a software development process. It's also a high-value target for attackers. Securing the pipeline is essential for protecting code, infrastructure, and users.\n\n### 1. Secure Your Source Code\n\nThe pipeline starts with your source code repository.\n\n*   **Protected Branches:** Configure protected branches (e.g., `main`, `develop`) in your SCM (GitHub, GitLab, etc.). Require code reviews and passing status checks before any code can be merged.\n*   **Commit Signing:** Enforce commit signing (e.g., with GPG keys) to verify the author of every commit and prevent spoofing.\n\n### 2. Don't Hardcode Secrets\n\nNever, ever hardcode secrets (API keys, passwords, tokens) in your pipeline configuration files or source code.\n\n*   **Best Practice:** Use a dedicated secrets management solution (like HashiCorp Vault, AWS Secrets Manager, or your CI platform's built-in secret store). Your pipeline should fetch secrets at runtime. Ensure that secrets are masked and never appear in logs.\n\n    **Example (GitHub Actions):**\n\n    steps:\n      - name: Deploy to production\n        run: ./deploy.sh\n        env:\n          API_KEY: ${{ secrets.PROD_API_KEY }} # Fetches from GitHub's encrypted secrets\n    ```\n\n### 3. Harden Your Pipeline Configuration\n\n*   **Least Privilege:** Grant the minimum level of permissions necessary to your pipeline's identities and build agents. If a job only needs to build code, it shouldn't have deployment credentials.\n*   **Isolated Build Environments:** Use ephemeral, isolated build environments (containers or VMs) that are destroyed after each job to prevent cross-contamination and lateral movement.\n*   **Automated Security Scanning:** Integrate security scanning directly into your pipeline. This creates a security gate that prevents vulnerabilities from moving down the line.\n    *   **SAST (Static Analysis):** Scans your source code for vulnerabilities.\n    *   **SCA (Software Composition Analysis):** Scans your dependencies for known vulnerabilities.\n    *   **DAST (Dynamic Analysis):** Scans your running application in a test environment.\n    *   **IaC Scanning:** Scans your Infrastructure-as-Code files (Terraform, CloudFormation) for misconfigurations.\n\n### 4. Secure Your Dependencies\n\nYour software is only as secure as its dependencies.\n\n*   **Pin Versions:** Use a lock file (`package-lock.json`, `yarn.lock`, `Gemfile.lock`) to pin your dependencies to specific, trusted versions. This prevents a malicious package from being automatically pulled in.\n*   **Validate Integrity:** Use package manager features that validate package integrity using hashes or checksums.\n*   **Use Private Registries:** For sensitive internal packages, use a private registry to avoid dependency confusion attacks.\n\n### 5. Sign Everything\n\nTo ensure the integrity of your supply chain, cryptographically sign your important assets.\n\n*   **Best Practice:**\n    *   Sign your Git commits.\n    *   Sign your build artifacts (e.g., Docker images, JAR files).\n    *   **Verify signatures** before deployment: ensure all artifacts are signed by trusted keys and reject unsigned or invalidly signed artifacts.\n    *   Consider frameworks like **SLSA (Supply-chain Levels for Software Artifacts)** to create a verifiable chain of custody for your software.\n\nBy embedding these practices into your daily workflow, you can build a CI/CD pipeline that is not only fast and efficient but also secure and resilient."
  },
  {
    "path": "sources/owasp/codeguard-0-clickjacking-defense.md",
    "content": "---\ndescription: Clickjacking Defense Best Practices\nlanguages:\n- c\n- html\n- javascript\n- php\n- typescript\nalwaysApply: false\n---\n\n## Protecting Your Web Application Against Clickjacking\n\nClickjacking (UI redress attacks) can trick users into performing unintended actions by hiding or disguising interactive elements. These attacks embed your site in an invisible iframe, allowing attackers to capture clicks meant for their site and redirect them to your application.\n\n### Defense Strategy: Multiple Layers of Protection\n\n#### 1. HTTP Headers: Your First Line of Defense\n\n**Content Security Policy (CSP)** is the modern, recommended approach that supersedes X-Frame-Options in modern browsers:\n\n```http\nContent-Security-Policy: frame-ancestors 'none';\n```\n\nThis directive prevents any site from framing your content. If you need to allow specific sites to frame your content, use:\n\n```http\nContent-Security-Policy: frame-ancestors 'self' https://trusted-site.com;\n```\n\n**Important:** CSP frame-ancestors takes precedence over X-Frame-Options when both are present. For older browsers that don't support CSP, implement the **X-Frame-Options** header as a fallback:\n\n```http\nX-Frame-Options: DENY\n```\n\nor\n\n```http\nX-Frame-Options: SAMEORIGIN\n```\n\n**Important:** Never use `X-Frame-Options: ALLOW-FROM` as it's obsolete and not supported by modern browsers.\n\n#### 2. Cookie Protection\n\nProtect your session cookies from being included in cross-origin requests:\n\n```http\nSet-Cookie: sessionid=abc123; SameSite=Lax; Secure; HttpOnly\n```\n\nOptions for the SameSite attribute:\n- `Strict`: Cookies are only sent in first-party context (most secure)\n- `Lax`: Cookies are sent when navigating to your site from another site (good balance)\n\n#### 3. JavaScript Frame-Buster\n\nFor legacy browsers or as an additional layer, implement this defensive code in your page's `<head>` section:\n\n```html\n<style id=\"antiClickjack\">body{display:none !important;}</style>\n<script type=\"text/javascript\">\n  if (self === top) {\n    // Not framed, remove the style that hides content\n    var antiClickjack = document.getElementById(\"antiClickjack\");\n    antiClickjack.parentNode.removeChild(antiClickjack);\n  } else {\n    // Framed, break out of the frame\n    top.location = self.location;\n  }\n</script>\n```\n\nThis approach first hides the page content, then only reveals it if the page is not framed. If framed, it attempts to break out of the frame. **Note:** Frame-busters can be defeated by advanced attackers and should not be your only defense.\n\n#### 4. Special Cases: When Your Site Must Be Framed\n\nIf your application legitimately needs to be framed (e.g., it's designed to be embedded):\n\n1. Use CSP to whitelist only specific domains:\n   ```http\n   Content-Security-Policy: frame-ancestors 'self' https://trusted-partner.com;\n   ```\n\n2. Implement additional confirmation for sensitive actions:\n   ```javascript\n   if (sensitiveAction && window !== window.top) {\n     if (!window.confirm('Confirm this action?')) {\n       return false; // Cancel the action if not confirmed\n     }\n   }\n   ```\n\n### Implementation Best Practices\n\n1. **Apply Globally:** Add these protections to all pages, not just sensitive ones.\n2. **Automate Header Injection:** Configure your web server, CDN, or application framework to automatically inject headers rather than manually adding them to each page.\n3. **Use CSP Report-Only for Testing:** Deploy `Content-Security-Policy-Report-Only: frame-ancestors 'none';` first to monitor violations before enforcing.\n4. **Test Thoroughly:** Verify your defenses work across different browsers and with proxies.\n5. **Defense in Depth:** Combine all three protection methods for maximum security.\n6. **Monitor and Verify:** Use tools like the OWASP ZAP scanner to confirm your headers are properly set and monitor CSP reports for violations.\n\nBy implementing these defenses, you significantly reduce the risk of clickjacking attacks against your web application."
  },
  {
    "path": "sources/owasp/codeguard-0-content-security-policy.md",
    "content": "---\ndescription: Content Security Policy (CSP) Best Practices\nlanguages:\n- c\n- html\n- javascript\n- php\n- typescript\nalwaysApply: false\n---\n\n## Content Security Policy (CSP): A Defense-in-Depth Strategy\n\nImplementing a strong Content Security Policy (CSP) is one of the most effective ways to mitigate cross-site scripting (XSS), clickjacking, and other injection attacks. CSP works by declaring which dynamic resources are allowed to load, effectively creating an allowlist that the browser enforces.\n\n### Implementation\n\n#### 1. Deliver CSP via HTTP Headers\n\nThe most effective way to implement CSP is through HTTP response headers:\n\n```http\nContent-Security-Policy: default-src 'self'; script-src 'self' https://trusted-cdn.com;\n```\n\nWhen testing a new policy, use the report-only mode to monitor without blocking:\n\n```http\nContent-Security-Policy-Report-Only: default-src 'self'; script-src 'self';\n```\n\n**Note:** Avoid using the meta tag approach (`<meta http-equiv=\"Content-Security-Policy\"...>`) except when you cannot modify HTTP headers, as it provides less protection and doesn't support all directives.\n\n#### 2. Adopt a Strict CSP Strategy\n\nModern CSP best practices favor nonce-based or hash-based approaches over domain whitelisting:\n\n**Nonce-based approach:**\n\n```http\nContent-Security-Policy: script-src 'nonce-random123' 'strict-dynamic';\n```\n\nWith corresponding HTML:\n\n```html\n<script nonce=\"random123\">alert('Hello');</script>\n```\n\n**Important:** Generate a unique, cryptographically strong nonce for each page load. The nonce should be at least 128 bits of entropy encoded in base64.\n\n**Server-side nonce generation examples:**\n\n```javascript\n// Node.js: crypto.randomBytes(16).toString('base64')\n// Python: base64.b64encode(secrets.token_bytes(16)).decode('utf-8')\n```\n\n**Hash-based alternative:**\n```http\nContent-Security-Policy: script-src 'sha256-hashOfYourScriptContent' 'strict-dynamic';\n```\n\n#### 3. Baseline CSP for Getting Started\n\n```http\nContent-Security-Policy: default-src 'self'; style-src 'self' 'unsafe-inline'; frame-ancestors 'self'; form-action 'self'; object-src 'none'; base-uri 'none'; upgrade-insecure-requests;\n```\n\nThis policy:\n- Restricts resources to the same origin\n- Allows inline styles (necessary for many applications initially)\n- Prevents clickjacking by controlling framing\n- Limits form submissions to the same origin\n- Blocks plugin content (Flash, Java applets)\n- Prevents base tag injection attacks\n- Automatically upgrades HTTP requests to HTTPS (when `upgrade-insecure-requests` is used)\n\n#### 4. Refactor Your Code for CSP Compatibility\n\nTo make CSP implementation easier:\n\n1. **Move inline code to external files:**\n   ```html\n   <!-- Instead of this -->\n   <button onclick=\"doSomething()\">\n\n   <!-- Do this -->\n   <button id=\"myButton\">\n   <script src=\"buttons.js\"></script> <!-- With event listeners -->\n   ```\n\n2. **Eliminate inline styles:**\n   ```html\n   <!-- Instead of this -->\n   <div style=\"color: red\">\n\n   <!-- Do this -->\n   <div class=\"red-text\">\n   ```\n\n#### 5. Key CSP Directives You Should Know\n\n- **`default-src`**: The fallback for other fetch directives\n- **`script-src`**: Controls JavaScript sources\n- **`style-src`**: Controls CSS sources - use `'self'` for external stylesheets, add `'unsafe-inline'` only if needed for inline styles\n- **`img-src`**: Controls image sources\n- **`connect-src`**: Controls fetch, XHR, WebSocket connections\n- **`object-src`**: Controls `<object>`, `<embed>`, and `<applet>` elements - set to `'none'` to block Flash/plugins\n- **`frame-ancestors`**: Controls which sites can embed your pages (replaces X-Frame-Options) - use `'none'` to prevent all framing\n- **`form-action`**: Controls where forms can be submitted\n- **`upgrade-insecure-requests`**: Automatically upgrades HTTP requests to HTTPS\n\n#### 6. Enable Violation Reporting\n\nSet up a reporting endpoint to collect CSP violations:\n\n```http\nContent-Security-Policy: default-src 'self'; report-uri https://your-domain.com/csp-reports;\n```\n\n#### 7. Implementation Steps\n\n1. Start with `Content-Security-Policy-Report-Only`\n2. Analyze violation reports\n3. Gradually tighten policy\n4. Switch to enforcing mode\n5. Continue monitoring\n\nRemember that CSP is a defense-in-depth measure. It complements, but does not replace, proper input validation, output encoding, and other secure coding practices."
  },
  {
    "path": "sources/owasp/codeguard-0-cookie-theft-mitigation.md",
    "content": "---\ndescription: Cookie Theft Mitigation Best Practices\nlanguages:\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\nalwaysApply: false\n---\n\n## Cookie Theft Mitigation Guidelines\n\nThis rule advises on detecting and mitigating session cookie theft through server-side monitoring:\n\n- Session Fingerprinting\n  - Store session environment information when sessions are established: IP address, User-Agent, Accept-Language, Date.\n  - Save additional headers for enhanced detection: Accept, Accept-Encoding, sec-ch-ua headers (when available).\n  - Associate fingerprint data with session ID for comparison on subsequent requests.\n\n- Cookie Theft Detection\n  - Monitor for significant changes in session environment information across requests.\n  - Compare current request headers against stored session fingerprint data.\n  - Account for legitimate variations (IP subnet changes, browser updates) vs. suspicious changes.\n  - Use multiple detection vectors rather than relying on single header changes.\n\n- Risk-Based Response to Anomalies\n  - For high-risk operations: require re-authentication when suspicious changes detected.\n  - For medium-risk operations: implement CAPTCHA challenges or additional verification.\n  - For low-risk operations: log suspicious activity and continue monitoring.\n  - Always regenerate session ID when potential hijacking is detected.\n\n- Session Validation Implementation\n  - Implement detection as middleware that runs before sensitive operations.\n  - Apply validation selectively to high-value endpoints to manage performance impact.\n  - Log all suspicious session activities with sufficient context for investigation.\n  - Handle false positives gracefully to maintain user experience.\n\n- Secure Session Storage\n  - Store session fingerprint data securely on server-side (never client-side).\n  - Use secure session storage mechanisms provided by your framework.\n  - Ensure session data is properly encrypted and protected.\n\nCode Example (from OWASP):\n```js\nconst session = SessionStorage.create()\nsession.save({\n  ip: req.clientIP,\n  user_agent: req.headers.userAgent,\n  date: req.headers.date,\n  accept_language: req.headers.acceptLanguage,\n  // ...\n})\n\nfunction cookieTheftDetectionMiddleware(req, res) {\n  const currentIP = req.clientIP\n  const expectedIP = req.session.ip\n  if (checkGeoIPRange(currentIP, expected) === false) {\n     // Validation\n  }\n  const currentUA = req.userAgent\n  const expectedUA = req.session.ua\n  if (checkUserAgent(currentUA, expectedUA)) {\n    // Validation\n  }\n  // ...\n}\n```\n\nSummary:  \nImplement server-side session fingerprinting to detect cookie theft, monitor environment changes across requests, apply risk-based responses to suspicious activity, and maintain secure session storage. Consider future standards like Device Bound Session Credentials for enhanced protection."
  },
  {
    "path": "sources/owasp/codeguard-0-credential-stuffing-prevention.md",
    "content": "---\ndescription: Credential Stuffing Prevention Best Practices\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\nalwaysApply: false\n---\n\n## Defending Against Credential Stuffing and Password Spraying Attacks\n\nWhen implementing authentication systems, defend against automated attacks that leverage stolen credentials. These attacks are increasingly sophisticated, and a layered defense strategy is essential.\n\n| Attack Type | Description |\n|-------------|-------------|\n| Brute Force | Testing multiple passwords from dictionary or other source against a single account |\n| Credential Stuffing | Testing username/password pairs obtained from the breach of another site |\n| Password Spraying | Testing a single weak password against a large number of different accounts |\n\nMFA is your most effective defense against credential stuffing attacks. Even if attackers have valid credentials, MFA creates a significant barrier.\n\n### 1. Multi-Factor Authentication (MFA): Primary Defense\n\nMFA stops 99.9% of credential-based attacks. Implement risk-based MFA that triggers on:\n- New devices or locations\n- Proxy/VPN usage\n- Recent failed login attempts\n- High-risk actions (admin changes, financial transactions)\n\nAlways require MFA for administrative functions and sensitive operations.\n\n### 2. Layered Controls: Defense in Depth\n\n* **CAPTCHA:** Trigger based on failed attempts (≥3), proxy/VPN IPs, bot user agents, or high-velocity traffic.\n\n* **Rate Limiting:** Implement multiple layers:\n  - Per-IP limits (3-15 attempts based on reputation)\n  - Per-user limits (5 attempts per account)\n  - Global limits to detect distributed attacks\n  - Temporary blocks with automatic expiration\n\n* **Device Fingerprinting:** Track user-agent, language, screen resolution, and TLS fingerprints to detect anomalous patterns.\n\n* **Unpredictable Usernames:** Avoid email addresses as usernames; use generated, non-sequential identifiers.\n\n### 3. Additional Defenses\n\n* **Multi-Step Login:** Require JavaScript token generation or CSRF tokens to complicate automated tools.\n\n* **Breached Password Detection:** Check new passwords against breach databases using [HaveIBeenPwned API](https://haveibeenpwned.com/API/v3) and reject passwords found in data breaches.\n\n* **User Notifications:** Alert users about successful logins from new devices and provide session management dashboards.\n\n### 4. Monitoring and Alerting\n\n* **Attack Detection:** Monitor for:\n  - High failed login volumes (>100/min)\n  - Many unique attacking IPs (>50/min)\n  - Abnormal CAPTCHA solve rates (>95%)\n  - Distributed attacks from hosting providers\n\n* **Impossible Travel Detection:** Alert when users appear to login from distant locations within impossible timeframes (faster than commercial flight speed).\n\n* **Breach Intelligence:** Integrate with [HaveIBeenPwned API](https://haveibeenpwned.com/API/v3) to detect newly compromised credentials.\n\nThese layered defenses significantly increase attack cost while maintaining good user experience."
  },
  {
    "path": "sources/owasp/codeguard-0-cross-site-request-forgery-prevention.md",
    "content": "---\ndescription: Cross-Site Request Forgery (CSRF) Prevention Best Practices\nlanguages:\n- c\n- go\n- html\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\nalwaysApply: false\n---\n\n## Introduction\n\nA Cross-Site Request Forgery (CSRF) attack occurs when a malicious web site, email, blog, instant message, or program tricks an authenticated user's web browser into performing an unwanted action on a trusted site. If a target user is authenticated to the site, unprotected target sites cannot distinguish between legitimate authorized requests and forged authenticated requests.\n\n**IMPORTANT: Remember that Cross-Site Scripting (XSS) can defeat all CSRF mitigation techniques!** Consider the client and authentication method to determine the best approach for CSRF protection in your application.\n\n## Preventing Cross-Site Request Forgery (CSRF) Attacks\n\n### Implementation Best Practices\n\n#### 1. Fix XSS Vulnerabilities First\n\nCross-Site Scripting (XSS) vulnerabilities can bypass CSRF protections. Always address XSS issues alongside CSRF mitigations.\n\n#### 2. Use Framework-Native CSRF Protection\n\nUse framework built-in CSRF protection with correct implementation:\n\n* **Angular**: Configure HttpClient with XSRF protection:\n  ```typescript\n  // app.config.ts\n  provideHttpClient(withXsrfConfiguration({\n    cookieName: 'XSRF-TOKEN',\n    headerName: 'X-XSRF-TOKEN'\n  }))\n  ```\n\n* **Next.js**: Use csrf middleware in API routes:\n  ```javascript\n  // pages/api/protected.js\n  import { csrf } from 'csrf';\n  export default csrf(async (req, res) => {\n    // Your protected endpoint logic\n  });\n  ```\n\n* **Spring Security**: Enable CSRF protection properly:\n  ```java\n  @Configuration\n  @EnableWebSecurity\n  public class SecurityConfig {\n    @Bean\n    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {\n      return http.csrf(Customizer.withDefaults()).build(); // CSRF enabled by default\n    }\n  }\n  ```\n\n* **Django**: Use CSRF middleware and template tags:\n  ```python\n  # settings.py - ensure CsrfViewMiddleware is enabled\n  MIDDLEWARE = ['django.middleware.csrf.CsrfViewMiddleware', ...]\n  ```\n\n#### 3. Implement the Synchronizer Token Pattern\n\nGenerate unique, unpredictable tokens per session:\n\n```javascript\n// Token generation: use HMAC with session ID + secret\nconst csrfToken = crypto.createHmac('sha256', process.env.CSRF_SECRET)\n  .update(req.session.id).digest('hex');\n```\n\n**Form submissions**: Include token as hidden field:\n```html\n<input type=\"hidden\" name=\"_csrf\" value=\"{{csrfToken}}\">\n```\n\n**AJAX requests**: Send token in custom header:\n```javascript\nheaders: { 'X-CSRF-Token': document.querySelector('meta[name=\"csrf-token\"]').content }\n```\n\n#### 4. Protect All State-Changing Requests\n\n* **Never use GET for state changes**: All operations that change state should use POST, PUT, DELETE, or PATCH.\n* **Validate tokens on all unsafe methods**: Verify CSRF tokens on every state-changing request.\n\n#### 5. Secure Token Transmission and Storage\n\n**Mandatory HTTPS**: Always enforce HTTPS for CSRF token transmission:\n```javascript\n// Redirect HTTP to HTTPS\napp.use((req, res, next) => {\n  if (!req.secure && process.env.NODE_ENV === 'production') {\n    return res.redirect(`https://${req.headers.host}${req.url}`);\n  }\n  next();\n});\n```\n\n**Secure Cookie Configuration**: Use proper cookie attributes for CSRF tokens and sessions:\n```http\nSet-Cookie: __Host-XSRF-TOKEN=abc123; Path=/; Secure; SameSite=Lax\nSet-Cookie: __Host-sessionid=xyz789; Path=/; Secure; HttpOnly; SameSite=Lax\n```\n\nCookie attribute requirements:\n* **Secure**: Mandatory - prevents transmission over HTTP\n* **SameSite=Lax**: Balances security and usability; use `Strict` for high-security applications\n* **__Host- prefix**: Prevents subdomain cookie injection attacks\n* **HttpOnly**: For session cookies only (CSRF tokens need JavaScript access)\n\n#### 6. Defense-in-Depth Strategies\n\n**Combine Multiple Protections**: Layer CSRF tokens with origin validation and rate limiting:\n\n```javascript\n// Comprehensive CSRF protection middleware\nfunction csrfProtection(req, res, next) {\n  // 1. Validate Origin/Referer headers\n  const origin = req.headers.origin || req.headers.referer;\n  if (!origin || !isValidOrigin(origin)) {\n    return res.status(403).json({error: 'Invalid origin'});\n  }\n  \n  // 2. Validate CSRF token\n  const token = req.headers['x-csrf-token'] || req.body._csrf;\n  if (!isValidCsrfToken(token, req.session.id)) {\n    return res.status(403).json({error: 'Invalid CSRF token'});\n  }\n  \n  // 3. Rate limiting per session\n  if (exceedsRateLimit(req.session.id)) {\n    return res.status(429).json({error: 'Rate limit exceeded'});\n  }\n  \n  next();\n}\n\nfunction isValidOrigin(origin) {\n  const allowedOrigins = ['https://yourdomain.com', 'https://app.yourdomain.com'];\n  return allowedOrigins.includes(new URL(origin).origin);\n}\n```\n\n**Token-Based Authentication (SPAs)**: For SPAs using JWT/bearer tokens:\n```javascript\n// Custom header approach for token-based auth\nfunction apiCsrfProtection(req, res, next) {\n  // Require custom header for state-changing operations\n  if (['POST', 'PUT', 'DELETE', 'PATCH'].includes(req.method)) {\n    if (!req.headers['x-requested-with']) {\n      return res.status(403).json({error: 'Missing required header'});\n    }\n  }\n  next();\n}\n```\n\n#### 7. Special Cases\n\n**Login CSRF Protection**: Use pre-session tokens for login forms:\n```javascript\n// Generate token before authentication, destroy session after login\nconst loginToken = crypto.randomBytes(32).toString('hex');\nreq.session.loginCsrfToken = loginToken;\n```\n\n**Client-Side CSRF Prevention**: Validate input sources in JavaScript:\n```javascript\n// Avoid using URL parameters/fragments for generating requests\n// Validate endpoint URLs against allow-lists before making requests\nconst allowedEndpoints = ['/api/profile', '/api/settings'];\nif (!allowedEndpoints.includes(requestEndpoint)) {\n  throw new Error('Invalid endpoint');\n}\n```\n\n#### 8. Testing and Validation\n\nEssential CSRF defense tests:\n* Cross-origin form submissions should be blocked\n* CSRF tokens must be validated on all state-changing requests  \n* Test with various SameSite cookie settings\n* Verify Origin/Referer header validation works correctly\n\nThese layered defenses provide robust CSRF protection while maintaining usability."
  },
  {
    "path": "sources/owasp/codeguard-0-cross-site-scripting-prevention.md",
    "content": "---\ndescription: Cross Site Scripting (XSS) Prevention Best Practices\nlanguages:\n- c\n- html\n- javascript\n- php\n- typescript\n- vlang\nalwaysApply: false\n---\n\n## Introduction\n\nThis cheat sheet helps developers prevent XSS vulnerabilities.\n\nCross-Site Scripting (XSS) attacks are serious and can lead to account impersonation, observing user behaviour, loading external content, stealing sensitive data, and more. XSS occurs when an attacker injects malicious content into a webpage that executes in users' browsers.\n\n**This cheatsheet contains techniques to prevent or limit the impact of XSS. Since no single technique will solve XSS, using the right combination of defensive techniques will be necessary to prevent XSS.**\n\n## Preventing Cross-Site Scripting (XSS) Vulnerabilities\n\n### Core Defensive Strategies\n\n#### 1. Context-Aware Output Encoding\n\nThe most important defense against XSS is proper output encoding based on the context where data will be inserted:\n\n* **HTML Body Context:** Use `element.textContent` instead of `innerHTML`, or sanitize with DOMPurify for rich content\n* **HTML Attribute Context:** Always quote attributes and HTML encode values: `<div data-user=\"{{encodedInput}}\">` \n* **JavaScript Context:** Avoid dynamic JavaScript generation; use data attributes and event listeners instead\n* **CSS Context:** Validate CSS values against strict allow-lists before injection\n* **URL Context:** Encode URLs and validate protocols to prevent `javascript:` URLs\n\n* **ARIA Attributes & SVG Context:** Validate ARIA values against allow-lists; sanitize SVG with DOMPurify SVG profiles\n* **JavaScript Event Handlers:** Never inject user data into `onclick` attributes; use `addEventListener()` instead\n\n#### 2. Leverage Framework Protections\n\nModern frameworks provide built-in XSS protections:\n\n* **React:** Auto-escapes values in JSX, but be careful with `dangerouslySetInnerHTML`\n* **Angular:** Uses contextual auto-escaping, but be cautious with `[innerHTML]` binding\n* **Vue:** Auto-escapes mustache interpolations, but watch out for `v-html`\n\nWhen using escape hatches like `dangerouslySetInnerHTML`, `[innerHTML]`, or `v-html`, always sanitize with DOMPurify first.\n\n#### 3. Server-Side Input Validation and Sanitization\n\n* **Server-Side Validation is Mandatory:** All input validation must happen on the server. Client-side validation is for user experience only.\n* **Validate Against Allow-Lists:** Use strict regex patterns for expected input types (email, username, etc.) with length limits.\n* **HTML Sanitization with Trusted Libraries:** Use [DOMPurify](https://github.com/cure53/DOMPurify) with strict configurations:\n  ```javascript\n  const cleanHtml = DOMPurify.sanitize(userHtml, {\n    ALLOWED_TAGS: ['b', 'i', 'p', 'a', 'ul', 'li'],\n    ALLOWED_ATTR: ['href', 'target', 'rel'],\n    ALLOW_DATA_ATTR: false\n  });\n  ```\n\n#### 4. Defense-in-Depth Controls\n\n* **Content Security Policy (CSP):** Implement strict CSP headers as additional protection:\n  ```http\n  Content-Security-Policy: default-src 'self'; \n    script-src 'self' 'nonce-{random}'; \n    style-src 'self' 'unsafe-inline'; \n    object-src 'none'; \n    base-uri 'self';\n    require-trusted-types-for 'script';\n  ```\n\n* **Trusted Types API:** Use Trusted Types to prevent DOM XSS:\n  ```javascript\n  // Define trusted types policy\n  const policy = trustedTypes.createPolicy('myPolicy', {\n    createHTML: (string) => DOMPurify.sanitize(string),\n    createScript: () => { throw new Error('Script creation not allowed'); }\n  });\n  \n  // Use with trusted types\n  element.innerHTML = policy.createHTML(userInput);\n  ```\n\n* **Safe DOM APIs:** Always prefer safe DOM manipulation methods:\n  ```javascript\n  // Safe approaches\n  element.textContent = userInput;          // Always safe for text\n  element.setAttribute('data-user', userInput); // Safe for most attributes\n  element.classList.add(validatedClassName);     // Safe for CSS classes\n  ```\n\n* **Secure Cookie Configuration:** Prevent cookie theft:\n  ```http\n  Set-Cookie: sessionId=abc123; HttpOnly; Secure; SameSite=Strict\n  ```\n\n#### 5. Common Pitfalls to Avoid\n\n* **Don't trust any data source:** Even internal APIs or databases can contain malicious data.\n* **Beware of indirect inputs:** User data can enter your application through URLs, form fields, HTTP headers, and JSON/XML payloads.\n* **Don't rely on client-side sanitization alone:** Always re-validate and sanitize on the server.\n* **Keep dependencies updated:** Regularly update your frameworks and libraries to benefit from security patches.\n\nBy applying these context-specific encoding strategies and defense-in-depth approaches, you can significantly reduce the risk of XSS vulnerabilities in your web applications."
  },
  {
    "path": "sources/owasp/codeguard-0-cryptographic-storage.md",
    "content": "---\ndescription: Cryptographic Storage Best Practices\nlanguages:\n- c\n- go\n- java\n- javascript\n- kotlin\n- matlab\n- python\n- ruby\n- swift\n- typescript\nalwaysApply: false\n---\n\n## Introduction\n\nThis rule provides a simple model to follow when implementing solutions to protect data at rest.\n\nPasswords should not be stored using reversible encryption - secure password hashing algorithms should be used instead. The [Password Storage Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html) contains further guidance on storing passwords.\n\n## Rule 1: Architectural Design Requirements\n\n**YOU MUST consider the overall architecture** of the system, as this will have a huge impact on the technical implementation.\n\n* **Application level** - REQUIRED for database compromise protection\n* **Database level** (SQL Server TDE) - Additional data-at-rest protection  \n* **Filesystem level** (BitLocker, LUKS) - Physical theft protection\n* **Hardware level** (encrypted RAID/SSDs) - Hardware-based protection\n\n**YOU MUST minimize sensitive data storage** - avoid storing credit card details and implement data minimization policies. Use tokenization when storage is unavoidable.\n\n## Rule 2: Algorithm Requirements\n\n**YOU MUST use approved algorithms:**\n* **Symmetric:** AES with ≥128-bit keys (256-bit preferred)\n* **Asymmetric:** Curve25519 (ECC preferred) or RSA ≥2048 bits\n* **Custom algorithms:** PROHIBITED\n\n**YOU MUST use authenticated cipher modes:**\n1. **GCM** or **CCM** (preferred)\n2. **CTR/CBC** only with separate authentication (Encrypt-then-MAC)\n3. **ECB:** PROHIBITED\n\n**For RSA: YOU MUST enable Random Padding** (OAEP/PKCS#1).\n\n**YOU MUST use cryptographically secure random generators:**\n\n| Platform | PROHIBITED | REQUIRED |\n|----------|------------|----------|\n| PHP | `rand()`, `mt_rand()` | `random_bytes()`, `random_int()` |\n| Java | `java.util.Random` | `java.security.SecureRandom` |\n| .NET | `System.Random` | `System.Security.Cryptography.RandomNumberGenerator` |\n| Python | `random` module | `secrets` module |\n| JavaScript | `Math.random()` | `window.crypto.getRandomValues()` |\n| Go | `math/rand` | `crypto/rand` |\n| Node.js | `Math.random()` | `crypto.randomBytes()`, `crypto.randomInt()` |\n\n**UUIDs:** Version 1 UUIDs are NOT random. Only trust Version 4 UUIDs if implementation uses CSPRNG.\n\n## Rule 3: Key Management Requirements\n\n**YOU MUST implement formal processes for:**\n* Key generation using cryptographically secure functions\n* Secure key distribution and deployment\n* Regular key rotation and secure decommissioning\n\n**Key Generation:** YOU ARE PROHIBITED from using passwords, phrases, or predictable patterns. Multiple keys MUST be fully independent.\n\n**Key Rotation Requirements - YOU MUST rotate keys when:**\n* Key compromise is suspected\n* Cryptoperiod expires (see NIST SP 800-57)\n* Usage limits reached (2^35 bytes for 64-bit keys, 2^68 bytes for 128-bit)\n* Algorithm security changes\n\n**YOU MUST have rotation processes ready BEFORE compromise.**\n\n## Rule 4: Key Storage Requirements\n\n**YOU MUST use secure storage mechanisms where available:**\n* Physical/Virtual HSMs\n* Cloud key vaults (AWS KMS, Azure Key Vault, Google Cloud KMS)\n* External secrets management (HashiCorp Vault, Conjur)\n* Framework secure APIs (ProtectedData, Keychain)\n\n**Basic Storage Rules (when secure mechanisms unavailable):**\n* PROHIBITED: Hard-coding keys in source code or version control\n* REQUIRED: Restrictive permissions on config files\n* AVOID: Environment variables (exposure risk)\n\n**Key Separation:** YOU MUST store keys separately from encrypted data where possible.\n\n**Key Encryption:** YOU MUST encrypt stored keys using separate Key Encryption Keys (KEK):\n* Data Encryption Key (DEK) encrypts data\n* Key Encryption Key (KEK) encrypts DEK\n* KEK MUST be stored separately and be ≥ as strong as DEK\n\n## Rule 5: Defense in Depth Requirements\n\n**YOU MUST design applications to be secure even if cryptographic controls fail:**\n* Additional security layers for encrypted information\n* Strong access control (not relying on encrypted URL parameters alone)\n* Logging and monitoring of encrypted data access\n\n## Critical Security Requirements\n\n**COMPLIANCE IS MANDATORY** for all systems handling sensitive data.\n\n**YOU ARE ABSOLUTELY PROHIBITED FROM:**\n* Implementing custom cryptographic algorithms\n* Using insecure random generators for security purposes\n* Hard-coding encryption keys in source code\n* Using deprecated algorithms (MD5, SHA-1, DES, RC4)\n* Storing keys with encrypted data without proper separation\n\n**YOU MUST ALWAYS:**\n* Use authenticated encryption modes where available\n* Generate unique, random keys for each operation\n* Implement proper key lifecycle management\n* Use vetted cryptographic libraries only\n* Test key rotation procedures before needed"
  },
  {
    "path": "sources/owasp/codeguard-0-cw-cryptographic-security-guidelines.md",
    "content": "---\ndescription: Cryptographic Security Guidelines\nlanguages:\n- c\nalwaysApply: false\n---\n\n### Cryptographic Security Guidelines\n\n#### Deprecated SSL/Crypto APIs - FORBIDDEN\n**NEVER use these deprecated functions. Use the replacement APIs listed below:**\n\n##### Symmetric Encryption (AES)\n- **Deprecated**: `AES_encrypt()`, `AES_decrypt()`\n- **Replacement**: Use EVP high-level APIs:\n  ```c\n  EVP_EncryptInit_ex()\n  EVP_EncryptUpdate()\n  EVP_EncryptFinal_ex()\n  EVP_DecryptInit_ex()\n  EVP_DecryptUpdate()\n  EVP_DecryptFinal_ex()\n  ```\n\n##### RSA Operations\n- **Deprecated**: `RSA_new()`, `RSA_up_ref()`, `RSA_free()`, `RSA_set0_crt_params()`, `RSA_get0_n()`\n- **Replacement**: Use EVP key management APIs:\n  ```c\n  EVP_PKEY_new()\n  EVP_PKEY_up_ref()\n  EVP_PKEY_free()\n  ```\n\n##### Hash Functions\n- **Deprecated**: `SHA1_Init()`, `SHA1_Update()`, `SHA1_Final()`\n- **Replacement**: Use EVP digest APIs:\n  ```c\n  EVP_DigestInit_ex()\n  EVP_DigestUpdate()\n  EVP_DigestFinal_ex()\n  EVP_Q_digest()  // For simple one-shot hashing\n  ```\n\n##### MAC Operations\n- **Deprecated**: `CMAC_Init()`, `HMAC()` (especially with SHA1)\n- **Replacement**: Use EVP MAC APIs:\n  ```c\n  EVP_Q_MAC()  // For simple MAC operations\n  ```\n\n##### Key Wrapping\n- **Deprecated**: `AES_wrap_key()`, `AES_unwrap_key()`\n- **Replacement**: Use EVP key wrapping APIs or implement using EVP encryption\n\n##### Other Deprecated Functions\n- **Deprecated**: `DSA_sign()`, `DH_check()`\n- **Replacement**: Use corresponding EVP APIs for DSA and DH operations\n\n#### Banned Insecure Algorithms - STRICTLY FORBIDDEN\n**These algorithms MUST NOT be used in any form:**\n\n##### Hash Algorithms (Banned)\n- MD2, MD4, MD5, SHA-0\n- **Reason**: Cryptographically broken, vulnerable to collision attacks\n- **Use Instead**: SHA-256, SHA-384, SHA-512\n\n##### Symmetric Ciphers (Banned)\n- RC2, RC4, Blowfish, DES, 3DES\n- **Reason**: Weak key sizes, known vulnerabilities\n- **Use Instead**: AES-128, AES-256, ChaCha20\n\n##### Key Exchange (Banned)\n- Static RSA key exchange\n- Anonymous Diffie-Hellman\n- **Reason**: No forward secrecy, vulnerable to man-in-the-middle attacks\n- **Use Instead**: ECDHE, DHE with proper validation\n\n#### Broccoli Project Specific Requirements\n- **HMAC() with SHA1**: Deprecated per Broccoli project requirements\n- **Replacement**: Use HMAC with SHA-256 or stronger:\n  ```c\n  // Instead of HMAC() with SHA1\n  EVP_Q_MAC(NULL, \"HMAC\", NULL, \"SHA256\", NULL, key, key_len, data, data_len, out, out_size, &out_len);\n  ```\n\n#### Secure Crypto Implementation Pattern\n```c\n// Example: Secure AES encryption\nEVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();\nif (!ctx) handle_error();\n\nif (EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, key, iv) != 1)\n    handle_error();\n\nint len, ciphertext_len;\nif (EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len) != 1)\n    handle_error();\nciphertext_len = len;\n\nif (EVP_EncryptFinal_ex(ctx, ciphertext + len, &len) != 1)\n    handle_error();\nciphertext_len += len;\n\nEVP_CIPHER_CTX_free(ctx);\n```\n\n#### Code Review Checklist\n- [ ] No deprecated SSL/crypto APIs used\n- [ ] No banned algorithms (MD5, DES, RC4, etc.)\n- [ ] HMAC uses SHA-256 or stronger (not SHA1)\n- [ ] All crypto operations use EVP high-level APIs\n- [ ] Proper error handling for all crypto operations\n- [ ] Key material properly zeroed after use"
  },
  {
    "path": "sources/owasp/codeguard-0-cw-memory-string-usage-guidelines.md",
    "content": "---\ndescription: Memory and String Safety Guidelines\nlanguages:\n- c\nalwaysApply: false\n---\n\n### Memory and String Safety Guidelines\n\n#### Unsafe Memory Functions - FORBIDDEN\n**NEVER use these unsafe memory functions that don't check input parameter boundaries:**\n\n##### Banned Memory Functions:\n- `memcpy()` → Use `memcpy_s()`\n- `memset()` → Use `memset_s()`\n- `memmove()` → Use `memmove_s()`\n- `memcmp()` → Use `memcmp_s()`\n- `bzero()` → Use `memset_s()`\n- `memzero()` → Use `memset_s()`\n\n##### Safe Memory Function Replacements:\n```c\n// Instead of: memcpy(dest, src, count);\nerrno_t result = memcpy_s(dest, dest_size, src, count);\nif (result != 0) {\n// Handle error\n}\n\n// Instead of: memset(dest, value, count);\nerrno_t result = memset_s(dest, dest_size, value, count);\n\n// Instead of: memmove(dest, src, count);\nerrno_t result = memmove_s(dest, dest_size, src, count);\n\n// Instead of: memcmp(s1, s2, count);\nint indicator;\nerrno_t result = memcmp_s(s1, s1max, s2, s2max, count, &indicator);\nif (result == 0) {\n// indicator contains comparison result: <0, 0, or >0\n}\n```\n\n#### Unsafe String Functions - FORBIDDEN\n**NEVER use these unsafe string functions that can cause buffer overflows:**\n\n##### Banned String Functions:\n- `strstr()` → Use `strstr_s()`\n- `strtok()` → Use `strtok_s()`\n- `strcpy()` → Use `strcpy_s()`\n- `strcmp()` → Use `strcmp_s()`\n- `strlen()` → Use `strnlen_s()`\n- `strcat()` → Use `strcat_s()`\n- `sprintf()` → Use `snprintf()`\n\n##### Safe String Function Replacements:\n```c\n// String Search\nerrno_t strstr_s(char *dest, rsize_t dmax, const char *src, rsize_t slen, char **substring);\n\n// String Tokenization\nchar *strtok_s(char *dest, rsize_t *dmax, const char *src, char **ptr);\n\n// String Copy\nerrno_t strcpy_s(char *dest, rsize_t dmax, const char *src);\n\n// String Compare\nerrno_t strcmp_s(const char *dest, rsize_t dmax, const char *src, int *indicator);\n\n// String Length (bounded)\nrsize_t strnlen_s(const char *str, rsize_t strsz);\n\n// String Concatenation\nerrno_t strcat_s(char *dest, rsize_t dmax, const char *src);\n\n// Formatted String (always use size-bounded version)\nint snprintf(char *s, size_t n, const char *format, ...);\n```\n\n#### Implementation Examples:\n\n##### Safe String Copy Pattern:\n```c\n// Bad - unsafe\nchar dest[256];\nstrcpy(dest, src); // Buffer overflow risk!\n\n// Good - safe\nchar dest[256];\nerrno_t result = strcpy_s(dest, sizeof(dest), src);\nif (result != 0) {\n// Handle error: src too long or invalid parameters\nEWLC_LOG_ERROR(\"String copy failed: %d\", result);\nreturn ERROR;\n}\n```\n\n##### Safe String Concatenation Pattern:\n```c\n// Bad - unsafe\nchar buffer[256] = \"prefix_\";\nstrcat(buffer, suffix); // Buffer overflow risk!\n\n// Good - safe\nchar buffer[256] = \"prefix_\";\nerrno_t result = strcat_s(buffer, sizeof(buffer), suffix);\nif (result != 0) {\nEWLC_LOG_ERROR(\"String concatenation failed: %d\", result);\nreturn ERROR;\n}\n```\n\n##### Safe Memory Copy Pattern:\n```c\n// Bad - unsafe\nmemcpy(dest, src, size); // No boundary checking!\n\n// Good - safe\nerrno_t result = memcpy_s(dest, dest_max_size, src, size);\nif (result != 0) {\nEWLC_LOG_ERROR(\"Memory copy failed: %d\", result);\nreturn ERROR;\n}\n```\n\n##### Safe String Tokenization Pattern:\n```c\n// Bad - unsafe\nchar *token = strtok(str, delim); // Modifies original string unsafely\n\n// Good - safe\nchar *next_token = NULL;\nrsize_t str_max = strnlen_s(str, MAX_STRING_SIZE);\nchar *token = strtok_s(str, &str_max, delim, &next_token);\nwhile (token != NULL) {\n// Process token\ntoken = strtok_s(NULL, &str_max, delim, &next_token);\n}\n```\n\n#### Memory and String Safety Code Review Checklist:\n\n##### Pre-Code Review (Developer):\n- [ ] No unsafe memory functions (`memcpy`, `memset`, `memmove`, `memcmp`, `bzero`)\n- [ ] No unsafe string functions (`strcpy`, `strcat`, `strcmp`, `strlen`, `sprintf`, `strstr`, `strtok`)\n- [ ] All memory operations use `*_s()` variants with proper size parameters\n- [ ] Buffer sizes are correctly calculated using `sizeof()` or known limits\n- [ ] No hardcoded buffer sizes that could change\n\n##### Code Review (Reviewer):\n- [ ] **Memory Safety**: Verify all memory operations use safe variants\n- [ ] **Buffer Bounds**: Confirm destination buffer sizes are properly specified\n- [ ] **Error Handling**: Check that all `errno_t` return values are handled\n- [ ] **Size Parameters**: Validate that `rsize_t dmax` parameters are correct\n- [ ] **String Termination**: Ensure strings are properly null-terminated\n- [ ] **Length Validation**: Check that source string lengths are validated before operations\n\n##### Static Analysis Integration:\n- [ ] Enable compiler warnings for unsafe function usage\n- [ ] Use static analysis tools to detect unsafe function calls\n- [ ] Configure build system to treat unsafe function warnings as errors\n- [ ] Add pre-commit hooks to scan for banned functions\n\n#### Common Pitfalls and Solutions:\n\n##### Pitfall 1: Wrong Size Parameter\n```c\n// Wrong - using source size instead of destination size\nstrcpy_s(dest, strlen(src), src); // WRONG!\n\n// Correct - using destination buffer size\nstrcpy_s(dest, sizeof(dest), src); // CORRECT\n```\n\n##### Pitfall 2: Ignoring Return Values\n```c\n// Wrong - ignoring potential errors\nstrcpy_s(dest, sizeof(dest), src); // Error not checked\n\n// Correct - checking return value\nif (strcpy_s(dest, sizeof(dest), src) != 0) {\n// Handle error appropriately\n}\n```\n\n##### Pitfall 3: Using sizeof() on Pointers\n```c\n// Wrong - sizeof pointer, not buffer\nvoid func(char *buffer) {\nstrcpy_s(buffer, sizeof(buffer), src); // sizeof(char*) = 8!\n}\n\n// Correct - pass buffer size as parameter\nvoid func(char *buffer, size_t buffer_size) {\nstrcpy_s(buffer, buffer_size, src);\n}\n```"
  },
  {
    "path": "sources/owasp/codeguard-0-database-security.md",
    "content": "---\ndescription: Database Security Best Practices\nlanguages:\n- c\n- java\n- javascript\n- python\n- sql\n- yaml\nalwaysApply: false\n---\n\n## Database Security Guidelines\n\nThis rule advises on securely configuring SQL and NoSQL databases to protect against data breaches and unauthorized access:\n\n- Backend Database Protection\n  - Isolate database servers from other systems and limit host connections.\n  - Disable network (TCP) access when possible; use local socket files or named pipes.\n  - Configure database to bind only on localhost when appropriate.\n  - Restrict network port access to specific hosts with firewall rules.\n  - Place database server in separate DMZ isolated from application server.\n  - Never allow direct connections from thick clients to backend database.\n\n- Transport Layer Security\n  - Configure database to only allow encrypted connections.\n  - Install trusted digital certificates on database servers.\n  - Use TLSv1.2+ with modern ciphers (AES-GCM, ChaCha20) for client connections.\n  - Verify digital certificate validity in client applications.\n  - Ensure all database traffic is encrypted, not just initial authentication.\n\n- Secure Authentication Configuration\n  - Always require authentication, including from local server connections.\n  - Protect accounts with strong, unique passwords.\n  - Use dedicated accounts per application or service.\n  - Configure minimum required permissions only.\n  - Regularly review accounts and permissions.\n  - Remove accounts when applications are decommissioned.\n  - Change passwords when staff leave or compromise is suspected.\n\n- Database Credential Storage\n  - Never store credentials in application source code.\n  - Store credentials in configuration files outside web root.\n  - Set appropriate file permissions for credential access.\n  - Never check credential files into source code repositories.\n  - Encrypt credential storage using built-in functionality when available.\n  - Use environment variables or secrets management solutions.\n\n- Secure Permission Management\n  - Apply principle of least privilege to all database accounts.\n  - Do not use built-in root, sa, or SYS accounts.\n  - Do not grant administrative rights to application accounts.\n  - Restrict account connections to allowed hosts only.\n  - Use separate databases and accounts for Development, UAT, and Production.\n  - Grant only required permissions (SELECT, UPDATE, DELETE as needed).\n  - Avoid making accounts database owners to prevent privilege escalation.\n  - Implement table-level, column-level, and row-level permissions when needed.\n\n- Database Configuration and Hardening\n  - Install required security updates and patches regularly.\n  - Run database services under low-privileged user accounts.\n  - Remove default accounts and sample databases.\n  - Store transaction logs on separate disk from main database files.\n  - Configure regular encrypted database backups with proper permissions.\n  - Disable unnecessary stored procedures and dangerous features.\n  - Implement database activity monitoring and alerting.\n\n- Platform-Specific Hardening\n  - SQL Server: Disable xp_cmdshell, CLR execution, SQL Browser service, Mixed Mode Authentication (unless required).\n  - MySQL/MariaDB: Run mysql_secure_installation, disable FILE privilege for users.\n  - PostgreSQL: Follow PostgreSQL security documentation guidelines.\n  - MongoDB: Implement MongoDB security checklist requirements.\n  - Redis: Follow Redis security guide recommendations.\n\nSummary:  \nIsolate database systems, enforce encrypted connections, implement strong authentication, store credentials securely using secrets management, apply least privilege permissions, harden database configurations, and maintain regular security updates and monitoring."
  },
  {
    "path": "sources/owasp/codeguard-0-deserialization.md",
    "content": "---\ndescription: Deserialization Security Best Practices\nlanguages:\n- c\n- java\n- javascript\n- php\n- python\n- xml\n- yaml\nalwaysApply: false\n---\n\n## Avoid Unsafe Deserialization of Untrusted Data\n\nDeserialization of untrusted input can lead to critical vulnerabilities such as remote code execution, denial of service, and privilege escalation. This rule ensures developers follow best practices to safely handle serialization and deserialization operations.\n\nRequirements:\n\n- Always treat incoming serialized data from untrusted sources as hostile.\n- Validate input size, structure, and content before deserialization.\n- Prefer standardized, safe data formats like JSON or XML without type metadata over native serialization formats.\n- For XML: disable DTDs and external entities to prevent XXE attacks.\n- Avoid using unsafe native serialization APIs on untrusted input, such as:\n  - PHP: avoid `unserialize()`, use `json_decode()`/`json_encode()` instead.\n  - Python: avoid `pickle.loads`, `yaml.load` (use `safe_load`), and `jsonpickle` on untrusted data.\n  - Java: override `ObjectInputStream#resolveClass()` to allowlist classes; mark sensitive fields `transient`; avoid polymorphic deserialization unless strictly allowlisted.\n  - .NET: avoid `BinaryFormatter`; use `DataContractSerializer` or `XmlSerializer`; set `TypeNameHandling = None` in JSON.Net; never trust deserialized types blindly.\n- Sign serialized data and verify signatures to ensure integrity before deserialization.\n- Configure serialization libraries securely:\n  - Jackson: `mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL)` is dangerous\n  - fastjson: Enable safemode, disable autotype\n  - XStream: Use allowlists with `XStream.allowTypes()`\n  - SnakeYAML: Use `yaml.safe_load()` instead of `yaml.load()`\n  - Keep dependencies updated to fixed versions.\n- Reject or safely handle polymorphic or complex objects deserialization from untrusted sources.\n- Use hardened deserialization agents/tools (e.g., SerialKiller, hardened ObjectInputStream subclasses, JVM agents).\n- Log deserialization attempts and monitor for suspicious activity.\n- Regularly scan code and dependencies for unsafe deserialization patterns using static and dynamic analysis tools.\n\nSecurity Impact:\n\nDeserialization attacks are a frequently exploited vector leading to severe security impacts. Following these practices prevents attackers from injecting malicious objects or payloads that the application may execute.\n\nExamples:\n\nAvoid:\n- PHP: calling `unserialize($data)` on external input.\n- Java: deserializing classes without strict allowlisting or type validation.\n- Python: loading YAML with `yaml.load()` on untrusted data.\n- .NET: using `BinaryFormatter.Deserialize()` on untrusted input.\n- XML: processing XML with DTDs enabled or external entity resolution.\n\nRecommended:\n- PHP: use `json_decode()` and validate JSON schema.\n- Java: Override `resolveClass()` to allowlist safe classes:\n  ```java\n  @Override\n  protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {\n      if (!allowedClasses.contains(desc.getName())) {\n          throw new InvalidClassException(\"Unauthorized class\", desc.getName());\n      }\n      return super.resolveClass(desc);\n  }\n  ```\n- Python: Use safe YAML loading:\n  ```python\n  import yaml\n  data = yaml.safe_load(input)  # Safe\n  # Never: yaml.load(input)     # Dangerous\n  ```\n- .NET: Use DataContractSerializer with type control:\n  ```csharp\n  // Safe approach\n  var serializer = new DataContractSerializer(typeof(SafeType));\n  var obj = serializer.ReadObject(stream);\n  \n  // JSON.NET safety\n  JsonConvert.DeserializeObject<SafeType>(json, new JsonSerializerSettings {\n      TypeNameHandling = TypeNameHandling.None\n  });\n  ```\n- XML: Configure parsers safely:\n  ```java\n  // Java: Disable DTDs and external entities\n  factory.setFeature(\"http://apache.org/xml/features/disallow-doctype-decl\", true);\n  factory.setFeature(\"http://xml.org/sax/features/external-general-entities\", false);\n  ```\n  ```csharp\n  // .NET: Disable DTD processing\n  XmlReaderSettings settings = new XmlReaderSettings();\n  settings.DtdProcessing = DtdProcessing.Prohibit;\n  ```\n\nMonitoring Requirements:\n- Log all deserialization attempts with data size and type information\n- Alert on deserialization failures or unexpected data patterns\n- Monitor for known malicious payloads (e.g., `AC ED 00 05`, `rO0`, `AAEAAAD`)\n- Track deserialization performance to detect \"billion laughs\" attacks"
  },
  {
    "path": "sources/owasp/codeguard-0-django-rest-framework.md",
    "content": "---\ndescription: Django REST Framework Security Best Practices\nlanguages:\n- python\n- yaml\nalwaysApply: false\n---\n\n## Django REST Framework Security Guidelines\n\nThis rule advises on critical security practices when developing Django REST Framework APIs to protect against common risks:\n\n- Authentication & Authorization  \n  - Always set `DEFAULT_AUTHENTICATION_CLASSES` with appropriate authentication schemes for all non-public endpoints.  \n  - When using SessionAuthentication, ensure CSRF protection is enabled and properly configured.\n  - Never leave `DEFAULT_PERMISSION_CLASSES` as `AllowAny`; explicitly restrict access using proper permission classes.  \n  - When overriding `get_object()`, always call `self.check_object_permissions(request, obj)` to enforce object-level access control.  \n  - Avoid per-view overrides of authentication, permission, or throttle classes unless fully confident in their implications.\n\n- Serializer Security  \n  - In DRF Serializers, specify explicit `fields = [...]` allowlists; do not use `exclude`.\n  - For Django ModelForms (when used outside DRF), always use `Meta.fields` allowlist instead of `Meta.exclude` or `\"__all__\"`.\n  - DO NOT use `Meta.exclude` (denylist approach) or `ModelForms.Meta.fields = \"__all__\"`.\n\n- Rate Limiting & Throttling  \n  - Configure `DEFAULT_THROTTLE_CLASSES` to enable API rate limiting as a DoS defense layer.  \n  - Prefer to enforce rate limiting at the gateway or WAF level; DRF throttling is a last-resort safeguard.\n\n- Security Configuration  \n  - Ensure `DEBUG` and `DEBUG_PROPAGATE_EXCEPTIONS` are set to `False` in production environments.  \n  - Never hardcode secrets such as `SECRET_KEY`; inject them via environment variables or secrets managers.  \n  - Disable all unused or dangerous HTTP methods (e.g., PUT, DELETE) at the API level.  \n  - Validate, sanitize, and filter all incoming data rigorously.\n  - Set secure HTTP headers: `SECURE_CONTENT_TYPE_NOSNIFF = True`, `X_FRAME_OPTIONS = 'DENY'`, `SECURE_BROWSER_XSS_FILTER = True`.\n  - Implement Content Security Policy (CSP) using django-csp middleware to prevent XSS and clickjacking attacks.\n\n- Prevent Injection Attacks  \n  - Avoid raw SQL queries with user input; use ORM or parameterized queries exclusively.\n  - DO NOT add user input to dangerous methods (`raw()`, `extra()`, `cursor.execute()`).\n  - For YAML parsing, use safe loaders (`yaml.SafeLoader`); never parse YAML or pickle data from untrusted sources.\n  - DO NOT use `eval()`, `exec()`, or similar dynamic code execution functions on user input.\n\n- Secret Management  \n  - Never hardcode secrets such as `SECRET_KEY`; inject them via environment variables or secrets managers.\n  - DO NOT hardcode API keys, database passwords, or other sensitive credentials in source code.\n\n- Input Validation  \n  - Validate, sanitize, and filter all client-provided data rigorously.\n  - Use Django's built-in form validation or DRF serializers with proper validation methods.\n\n- Logging Security  \n  - Log authentication failures, authorization denials, and validation errors with sufficient context.\n  - DO NOT log sensitive data (passwords, tokens, PII) in application logs.\n  - Include stack traces, error messages, and user context in security-relevant log entries.\n\nSummary:  \nAlways enforce object-level permissions with `check_object_permissions()`, use explicit field allowlists in serializers, avoid dangerous functions with user input, never hardcode secrets, implement proper input validation, and ensure security-aware logging practices."
  },
  {
    "path": "sources/owasp/codeguard-0-django-security.md",
    "content": "---\ndescription: Django Security Best Practices\nlanguages:\n- c\n- python\nalwaysApply: false\n---\n\n## Django Security Guidelines\n\nThis rule advises on critical Django security practices to prevent common web vulnerabilities:\n\n- General Security Configuration\n  - Never set `DEBUG = True` in production environments.\n  - Keep Django and dependencies up-to-date regularly.\n  - Use rate limiting packages like `django_ratelimit` or `django-axes` for brute-force protection.\n\n- Authentication System\n  - Include `django.contrib.auth`, `django.contrib.contenttypes`, and `django.contrib.sessions` in `INSTALLED_APPS`.\n  - Use `@login_required` decorator to protect views requiring authentication.\n  - Configure `AUTH_PASSWORD_VALIDATORS` with appropriate validators for password policies.\n  - Use `make_password()` and `check_password()` functions for password hashing and verification.\n\n- Secret Key Management\n  - Generate `SECRET_KEY` with at least 50 characters containing letters, digits, and symbols.\n  - Use `get_random_secret_key()` function for key generation.\n  - Store `SECRET_KEY` in environment variables, never hardcode in source.\n  - Rotate keys regularly and immediately upon exposure.\n\n- Security Middleware Configuration\n  - Include `django.middleware.security.SecurityMiddleware` in `MIDDLEWARE` settings.\n  - Include `django.middleware.clickjacking.XFrameOptionsMiddleware` in `MIDDLEWARE` settings.\n  - Set `SECURE_CONTENT_TYPE_NOSNIFF = True` for MIME type protection.\n  - Configure `SECURE_HSTS_SECONDS` with positive integer for HTTPS enforcement.\n  - Set `X_FRAME_OPTIONS = 'DENY'` or `'SAMEORIGIN'` for clickjacking protection.\n\n- Cookie Security\n  - Set `SESSION_COOKIE_SECURE = True` to send session cookies over HTTPS only.\n  - Set `CSRF_COOKIE_SECURE = True` to send CSRF cookies over HTTPS only.\n  - Use `secure=True` parameter when setting custom cookies with `HttpResponse.set_cookie()`.\n\n- CSRF Protection\n  - Include `django.middleware.csrf.CsrfViewMiddleware` in `MIDDLEWARE` settings.\n  - Use `{% csrf_token %}` template tag in all forms.\n  - Extract CSRF token properly for AJAX calls.\n\n- XSS Protection\n  - Use Django's built-in template system with automatic HTML escaping.\n  - Avoid `safe` filter and `mark_safe` function unless input is from trusted sources.\n  - Use `json_script` template filter for passing data to JavaScript.\n\n- HTTPS Configuration\n  - Set `SECURE_SSL_REDIRECT = True` to redirect HTTP requests to HTTPS.\n  - Configure `SECURE_PROXY_SSL_HEADER` when behind proxy or load balancer.\n\n- Admin Panel Security\n  - Change default admin URL from `/admin/` to custom path in `urls.py`.\n\nCode Examples (from OWASP):\n\n```python\n# Authentication setup\nINSTALLED_APPS = [\n    'django.contrib.auth',\n    'django.contrib.contenttypes',\n    'django.contrib.sessions',\n]\n\n@login_required\ndef my_view(request):\n    # Your view logic\n\n# Password validation\nAUTH_PASSWORD_VALIDATORS = [\n    {\n        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',\n    },\n    {\n        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',\n        'OPTIONS': {\n            'min_length': 8,\n        }\n    },\n]\n\n# Secret key from environment\nSECRET_KEY = os.environ.get('DJANGO_SECRET_KEY')\n\n# Cookie security\nresponse.set_cookie('my_cookie', 'cookie_value', secure=True)\n```\n\n```html\n<!-- CSRF in forms -->\n<form method=\"post\">\n    {% csrf_token %}\n    <!-- Your form fields here -->\n</form>\n```\n\nSummary:  \nConfigure Django securely by disabling debug mode in production, using proper authentication settings, securing secret keys, enabling security middleware, setting secure cookie attributes, implementing CSRF protection, preventing XSS, enforcing HTTPS, and securing admin access."
  },
  {
    "path": "sources/owasp/codeguard-0-docker-security.md",
    "content": "---\ndescription: Docker Security Best Practices\nlanguages:\n- docker\n- yaml\nalwaysApply: false\n---\n\n## Docker Security Guidelines\n\nThis rule advises on critical Docker container security practices to protect against common risks:\n\n- Container User Security\n  - Always specify a non-root user in Dockerfiles using `USER` directive.\n  - Never run containers as root; use `docker run -u <user>` or Kubernetes `securityContext.runAsUser`.\n  - Avoid `USER root` or missing `USER` directives in Dockerfiles.\n\n- Docker Daemon Socket Protection\n  - DO NOT expose `/var/run/docker.sock` to containers via volume mounts.\n  - DO NOT enable TCP Docker daemon socket (`-H tcp://0.0.0.0:XXX`) without TLS.\n  - Avoid `- \"/var/run/docker.sock:/var/run/docker.sock\"` in docker-compose files.\n\n- Capability and Privilege Management\n  - Drop all capabilities (`--cap-drop all`) and add only required ones (`--cap-add`).\n  - DO NOT use `--privileged` flag in container configurations.\n  - Set `allowPrivilegeEscalation: false` in Kubernetes security contexts.\n  - Use `--security-opt=no-new-privileges` to prevent privilege escalation.\n\n- Dockerfile Security Practices\n  - Pin base image versions (avoid `latest` tags in production).\n  - Use `COPY` instead of `ADD` when not extracting archives.\n  - DO NOT include secrets, passwords, or API keys in Dockerfiles.\n  - Avoid curl bashing in `RUN` directives; use package managers when possible.\n  - Include `HEALTHCHECK` instructions for container health monitoring.\n\n- Resource and Filesystem Security\n  - Limit container resources (memory, CPU) in docker-compose or Kubernetes specs.\n  - Use read-only root filesystems (`--read-only` or `readOnlyRootFilesystem: true`).\n  - Mount volumes as read-only (`:ro`) when write access is not needed.\n  - Use `--tmpfs` for temporary writable storage instead of persistent volumes.\n\n- Network and Runtime Security\n  - Avoid default bridge networking; define custom Docker networks.\n  - DO NOT share host network namespace (`--net=host`).\n  - DO NOT expose unnecessary ports in Dockerfiles or container configs.\n  - Enable default security profiles (seccomp, AppArmor, SELinux); do not disable them.\n\n- Secret Management\n  - Use Docker Secrets or Kubernetes encrypted secrets for sensitive data.\n  - DO NOT embed secrets in environment variables or Dockerfile layers.\n  - Avoid hardcoded credentials in container configurations.\n\n- Container Image Security\n  - Scan images for vulnerabilities before deployment.\n  - Use minimal base images (alpine, distroless) to reduce attack surface.\n  - Remove package managers and unnecessary tools from production images.\n\nSummary:  \nAlways run containers as non-root users, never expose Docker daemon socket, drop unnecessary capabilities, use secure Dockerfile practices, implement resource limits and read-only filesystems, configure proper networking, manage secrets securely, and scan images for vulnerabilities."
  },
  {
    "path": "sources/owasp/codeguard-0-dom-based-xss-prevention.md",
    "content": "---\ndescription: DOM-based XSS Prevention Best Practices\nlanguages:\n- c\n- html\n- javascript\n- php\n- typescript\n- vlang\nalwaysApply: false\n---\n\n## DOM-based XSS Prevention Security Rule\n\n**RULE ENFORCEMENT:** This rule prevents DOM-based XSS vulnerabilities where untrusted data from user-controllable sources is inserted into DOM sinks without proper encoding or sanitization.\n\n## Rule 1: DOM Sink Restrictions by Context\n\n**YOU MUST secure DOM sinks** based on risk level:\n\n### High-Risk HTML Sinks\n\n```javascript\n// VIOLATION: Direct assignment to HTML sinks\nelement.innerHTML = userInput;           // DANGEROUS\nelement.outerHTML = userInput;           // DANGEROUS\ndocument.write(userInput);               // DANGEROUS\n\n// REQUIRED: Use safe alternatives\nelement.textContent = userInput;         // SAFE\nelement.innerHTML = DOMPurify.sanitize(userInput); // SAFE\n```\n\n### Critical JavaScript Execution Sinks\n\n```javascript\n// VIOLATION: Code execution sinks\neval(userInput);                         // CRITICAL VULNERABILITY\nnew Function(userInput);                 // CRITICAL VULNERABILITY\nsetTimeout(userInput, 100);              // CRITICAL VULNERABILITY\n\n// REQUIRED: Safe alternatives\nsetTimeout(() => processData(userInput), 100);  // SAFE\nJSON.parse(userInput);                   // SAFE for JSON\n```\n\n### Medium-Risk URL/Event Sinks\n\n```javascript\n// VIOLATION: Unvalidated assignments\nlocation.href = userInput;               // DANGEROUS\nelement.onclick = userInput;             // DANGEROUS\n\n// REQUIRED: Validate and use safe patterns\nif (/^https?:\\/\\/trusted-domain\\.com/.test(userInput)) {\n  location.href = encodeURI(userInput);\n}\nelement.addEventListener('click', () => handleClick(userInput));\n```\n\n## Rule 2: Mandatory CSP and Trusted Types\n\n**YOU MUST implement strict CSP:**\n\n```http\nContent-Security-Policy: \n  script-src 'self' 'nonce-{random}';\n  object-src 'none';\n  base-uri 'self';\n  require-trusted-types-for 'script';\n```\n\n**YOU MUST use Trusted Types:**\n\n```javascript\nconst policy = trustedTypes.createPolicy('dom-xss-prevention', {\n  createHTML: (string) => DOMPurify.sanitize(string, {\n    ALLOWED_TAGS: ['b', 'i', 'em', 'strong', 'p', 'br'],\n    ALLOWED_ATTR: []\n  })\n});\nelement.innerHTML = policy.createHTML(userInput);\n```\n\n## Rule 3: Server-Side Validation Requirements\n\n**YOU MUST validate all user input server-side:**\n\n```javascript\nfunction validateInput(input, context) {\n  if (input.length > 1000) throw new Error('Input too long');\n  \n  const patterns = {\n    html: /<script|javascript:|on\\w+\\s*=/i,\n    url: /^https?:\\/\\/[a-zA-Z0-9.-]+/,\n    text: /<[^>]*>/g\n  };\n  \n  if (patterns[context] && patterns[context].test(input)) {\n    throw new Error('Invalid input detected');\n  }\n  return input;\n}\n```\n\n## Rule 4: Context-Aware Encoding\n\n**YOU MUST apply proper encoding by context:**\n\n```javascript\n// HTML Context\nfunction encodeHTML(str) {\n  return str.replace(/[&<>\"'\\/]/g, char => ({\n    '&': '&amp;', '<': '&lt;', '>': '&gt;', '\"': '&quot;', \"'\": '&#39;', '/': '&#x2F;'\n  })[char]);\n}\n\n// JavaScript Context  \nfunction encodeJS(str) {\n  return str.replace(/[\\\\'\"<>/\\n\\r\\t]/g, char => ({\n    '\\\\': '\\\\\\\\', \"'\": \"\\\\'\", '\"': '\\\\\"', '<': '\\\\u003C', \n    '>': '\\\\u003E', '/': '\\\\u002F', '\\n': '\\\\n', '\\r': '\\\\r', '\\t': '\\\\t'\n  })[char]);\n}\n\n// URL Context\nfunction encodeURL(str) {\n  return encodeURIComponent(str).replace(/['\"<>]/g, char => \n    '%' + char.charCodeAt(0).toString(16).toUpperCase());\n}\n```\n\n## Rule 5: Safe DOM API Usage\n\n**YOU MUST use safe DOM construction:**\n\n```javascript\nfunction createSafeElement(tagName, textContent, attributes = {}) {\n  const element = document.createElement(tagName);\n  if (textContent) element.textContent = textContent;\n  \n  const safeAttrs = ['class', 'id', 'title', 'alt', 'src', 'href', 'role'];\n  for (const [key, value] of Object.entries(attributes)) {\n    if (safeAttrs.includes(key.toLowerCase())) {\n      element.setAttribute(key, value);\n    }\n  }\n  return element;\n}\n```\n\n## Rule 6: Source Validation\n\n**YOU MUST validate untrusted data sources:**\n\n### URL Parameters & PostMessage\n\n```javascript\n// URL parameter validation\nfunction getURLParam(name) {\n  const value = new URLSearchParams(location.search).get(name);\n  if (!value || value.length > 100 || /<script|javascript:/i.test(value)) {\n    throw new Error('Invalid parameter');\n  }\n  return value;\n}\n\n// PostMessage validation\nwindow.addEventListener('message', (event) => {\n  const allowedOrigins = ['https://trusted-domain.com'];\n  if (!allowedOrigins.includes(event.origin)) return;\n  processMessage(event.data);\n});\n```\n\n## Rule 7: Framework Integration\n\n**Framework-specific safe patterns:**\n\n```javascript\n// React: Safe HTML rendering\nfunction SafeComponent({ htmlContent }) {\n  const clean = DOMPurify.sanitize(htmlContent, {\n    ALLOWED_TAGS: ['b', 'i', 'em', 'strong', 'p', 'br']\n  });\n  return <div dangerouslySetInnerHTML={{ __html: clean }} />;\n}\n\n// Vue: Safe directive\nVue.directive('safe-html', {\n  update: (el, binding) => {\n    el.innerHTML = DOMPurify.sanitize(binding.value, {\n      ALLOWED_TAGS: ['b', 'i', 'em', 'strong', 'p', 'br']\n    });\n  }\n});\n```\n\n## Violation Detection Patterns\n\n**These patterns will trigger security violations:**\n\n```javascript\n// VIOLATION: Direct DOM manipulation\nelement.innerHTML = userInput;\neval(userData);\nsetTimeout(userData, 100);\nlocation.href = userUrl;\nelement.onclick = userHandler;\n\n// VIOLATION: Unescaped template literals\nconst html = `<div>${userInput}</div>`;\n\n// VIOLATION: Missing validation\nwindow.addEventListener('message', (e) => processMessage(e.data));\n```\n\n## Mandatory Compliance Checks\n\n\n✅ **DOMPurify imported and used for HTML insertion**\n✅ **No innerHTML/outerHTML without sanitization**\n✅ **No eval(), new Function(), setTimeout/setInterval with strings**\n✅ **CSP implemented with script-src 'self'**\n✅ **Trusted Types policy implemented**\n✅ **URL validation before location assignments**\n✅ **Origin validation for postMessage**\n\n**VIOLATION CONSEQUENCES:** DOM-based XSS vulnerabilities allow arbitrary JavaScript execution, leading to account compromise, data theft, and malicious user actions.\n\n**TEST PAYLOADS:** Code must safely handle:\n- `<script>alert('XSS')</script>`\n- `javascript:alert('XSS')`\n- `\"><script>alert('XSS')</script>`\n- `'; eval('alert(1)'); //`"
  },
  {
    "path": "sources/owasp/codeguard-0-dom-clobbering-prevention.md",
    "content": "---\ndescription: DOM Clobbering Prevention Best Practices\nlanguages:\n- c\n- html\n- javascript\n- php\n- typescript\n- vlang\nalwaysApply: false\n---\n\n## DOM Clobbering Prevention Security Rule\n\n**RULE ENFORCEMENT:** This rule prevents DOM clobbering attacks where malicious HTML elements with `id` or `name` attributes override JavaScript variables and browser APIs, potentially leading to XSS and security bypasses.\n\n## Rule 1: HTML Sanitization Requirements\n\n**YOU MUST sanitize all user-provided HTML** using DOMPurify or Sanitizer API:\n\n```javascript\n// REQUIRED: DOMPurify configuration\nconst clean = DOMPurify.sanitize(userInput, {\n  SANITIZE_DOM: true,           // Protects built-in APIs\n  SANITIZE_NAMED_PROPS: true,   // MANDATORY: Protects custom variables  \n  FORBID_ATTR: ['id', 'name']   // REQUIRED: Remove clobbering attributes\n});\n\n// ALTERNATIVE: Sanitizer API\nconst sanitizer = new Sanitizer({\n  blockAttributes: [{'name': 'id', elements: '*'}, {'name': 'name', elements: '*'}]\n});\nelement.setHTML(userInput, {sanitizer});\n```\n\n**YOU ARE PROHIBITED FROM:**\n* Using `innerHTML` with unsanitized user input\n* Allowing `id` or `name` attributes in user-generated content\n* Disabling `SANITIZE_NAMED_PROPS` in DOMPurify configuration\n\n## Rule 2: Content Security Policy Requirements\n\n**YOU MUST implement strict CSP** to prevent DOM clobbering exploitation:\n\n```http\nContent-Security-Policy: \n  script-src 'self' 'nonce-{random}';\n  object-src 'none';\n  base-uri 'self';\n  require-trusted-types-for 'script';\n```\n\n**YOU MUST use Trusted Types** for DOM manipulation:\n\n```javascript\nconst policy = trustedTypes.createPolicy('dompurify', {\n  createHTML: (string) => DOMPurify.sanitize(string, {SANITIZE_NAMED_PROPS: true})\n});\nelement.innerHTML = policy.createHTML(userInput);\n```\n\n## Rule 3: Variable Declaration Requirements\n\n**YOU MUST use explicit variable declarations** to prevent global namespace pollution:\n\n```javascript\n\"use strict\";                       // REQUIRED in all JavaScript files\nconst config = { isAdmin: false };  // REQUIRED: Explicit declarations\nlet userState = {};                 // REQUIRED: Block-scoped variables\n\n// PROHIBITED: Creates vulnerable globals\nconfig = { isAdmin: false };        // WILL BE FLAGGED AS VIOLATION\n```\n\n**YOU ARE PROHIBITED FROM:**\n* Storing sensitive data on `window` or `document` objects\n* Using implicit global variables without declaration keywords\n* Accessing user input on the left side of assignment expressions\n\n## Rule 4: Object Validation Requirements\n\n**YOU MUST validate object types** before accessing potentially clobbered properties:\n\n```javascript\n// REQUIRED: Type validation before use\nfunction safePropertyAccess(obj, property) {\n  if (obj && typeof obj === 'object' && !(obj instanceof Element)) {\n    return obj[property];\n  }\n  throw new Error('Potential DOM clobbering detected');\n}\n\n// REQUIRED: Validate built-in APIs\nif (typeof document.getElementById === 'function') {\n  const element = document.getElementById('myId');\n}\n```\n\n## Rule 5: Dynamic Attribute Restrictions\n\n**YOU MUST validate all dynamic HTML attributes:**\n\n```javascript\nfunction setElementAttribute(element, name, value) {\n  const forbidden = ['id', 'name', 'onclick', 'onload', 'onerror'];\n  if (forbidden.includes(name.toLowerCase())) {\n    throw new Error(`Attribute ${name} is prohibited for security`);\n  }\n  element.setAttribute(name, DOMPurify.sanitize(value, {ALLOWED_TAGS: []}));\n}\n```\n\n**YOU ARE PROHIBITED FROM:**\n* Setting `id` or `name` attributes from user input\n* Using dynamic attribute assignment without validation\n* Bypassing sanitization for `data-` or `aria-` attributes\n\n## Rule 6: Framework Security Requirements\n\n**React/Vue applications MUST use sanitized HTML rendering:**\n\n```javascript\n// REACT: Required safe HTML component\nfunction SafeHtmlComponent({ userContent }) {\n  const clean = DOMPurify.sanitize(userContent, {\n    SANITIZE_NAMED_PROPS: true,\n    FORBID_ATTR: ['id', 'name']\n  });\n  return <div dangerouslySetInnerHTML={{__html: clean}} />;\n}\n\n// VUE: Required directive\nVue.directive('safe-html', {\n  update(el, binding) {\n    el.innerHTML = DOMPurify.sanitize(binding.value, {\n      SANITIZE_NAMED_PROPS: true,\n      FORBID_ATTR: ['id', 'name']\n    });\n  }\n});\n```\n\n## Rule 7: Runtime Monitoring Requirements\n\n**YOU MUST implement DOM clobbering detection:**\n\n```javascript\n// REQUIRED: Runtime monitoring\nfunction detectClobbering() {\n  ['config', 'api', 'user', 'admin'].forEach(global => {\n    if (window[global] instanceof Element) {\n      console.error(`SECURITY VIOLATION: DOM Clobbering detected on ${global}`);\n      securityLogger.warn('DOM_CLOBBERING_DETECTED', { variable: global });\n    }\n  });\n}\nsetInterval(detectClobbering, 5000);\n```\n\n## Rule Violation Detection\n\n**The following patterns will trigger security violations:**\n\n```javascript\n// VIOLATION: Implicit global creation\nconfig = { sensitive: true };\n\n// VIOLATION: Storing sensitive data on window\nwindow.userRole = 'admin';\n\n// VIOLATION: Unvalidated innerHTML usage\nelement.innerHTML = userInput;\n\n// VIOLATION: Dynamic property access without validation\nobj[userControlledProperty] = value;\n\n// VIOLATION: Missing sanitization\ndocument.body.appendChild(htmlFromUser);\n```\n\n## Mandatory Compliance Checks\n\nThe following checks MUST be performed:\n\n✅ **DOMPurify imported and configured with `SANITIZE_NAMED_PROPS: true`**\n✅ **No direct `innerHTML` usage without sanitization**\n✅ **Strict mode enabled in all JavaScript files**\n✅ **CSP headers implemented with `script-src 'self'`**\n✅ **No `id` or `name` attributes in user content**\n✅ **Runtime clobbering detection implemented**\n✅ **Type validation before property access**\n\n**NON-COMPLIANCE CONSEQUENCES:** Code that violates these rules creates DOM clobbering vulnerabilities that can lead to XSS attacks and privilege escalation.\n\n**TEST PAYLOAD:** `<a id=config><a id=config name=isAdmin href=true>` must be properly sanitized or blocked."
  },
  {
    "path": "sources/owasp/codeguard-0-dotnet-security.md",
    "content": "---\ndescription: DotNet Security Best Practices\nlanguages:\n- c\n- javascript\n- xml\nalwaysApply: false\n---\n\n## .NET Security Guidelines\n\nThis rule advises on critical .NET security practices to prevent common web vulnerabilities:\n\n- General Security Configuration\n  - Keep .NET Framework, .NET Core, and all NuGet packages up-to-date.\n  - Use Software Composition Analysis (SCA) tools in CI/CD pipelines.\n  - Subscribe to .NET Core and ASP.NET Core security announcements on GitHub.\n\n- Access Control and Authorization\n  - Use `[Authorize]` attributes at controller or method level for all externally facing endpoints.\n  - Always validate user permissions server-side before resource access.\n  - Check roles using `User.Identity.IsInRole` in code when needed.\n  - Implement proper direct object reference validation to prevent IDOR attacks.\n\n- Authentication and Session Management\n  - Use ASP.NET Core Identity with secure password policies and account lockout.\n  - Set cookies with `HttpOnly = true` and `requireSSL = true` in production.\n  - Reduce session timeout and disable sliding expiration for better security.\n  - Throttle login, registration, and password reset methods against brute force attacks.\n\n- Cryptographic Security\n  - Never write custom cryptographic functions; use .NET's proven implementations.\n  - Use strong hashing algorithms: SHA512 for general hashing, PBKDF2 for passwords.\n  - Use AES-GCM for encryption with proper key management.\n  - Use Windows Data Protection API (DPAPI) for secure local storage.\n  - Enforce TLS 1.2+ for all network communications.\n\n- Injection Prevention\n  - Use parameterized SQL queries or Entity Framework exclusively.\n  - Never concatenate user input into SQL commands or OS commands.\n  - Use allowlist validation on all user input with methods like `IPAddress.TryParse`.\n  - Escape LDAP special characters with backslash when needed.\n\n- Security Misconfiguration\n  - Disable debug and tracing in production using web.config transforms.\n  - Force HTTPS redirects using `app.UseHttpsRedirection()` or Global.asax.\n  - Remove server version headers and implement secure HTTP headers.\n  - Never use default passwords or credentials.\n\n- CSRF Protection\n  - Use anti-forgery tokens with `@Html.AntiForgeryToken()` in forms.\n  - Validate tokens with `[ValidateAntiForgeryToken]` on POST/PUT actions.\n  - Set `ViewStateUserKey = Session.SessionID` for Web Forms CSRF protection.\n  - Remove anti-forgery cookies on logout.\n\n- Secure Headers Configuration\n  - Set `X-Frame-Options`, `X-Content-Type-Options`, `Content-Security-Policy` headers.\n  - Configure HSTS with `Strict-Transport-Security` header.\n  - Remove `X-Powered-By` and version disclosure headers.\n\n- Logging and Monitoring\n  - Log authentication failures, access control violations with user context.\n  - Use ILogger framework for centralized logging.\n  - Never log sensitive data like passwords or tokens.\n  - Include stack traces and error context in security logs.\n\n- Serialization Security\n  - Avoid `BinaryFormatter` which is dangerous for untrusted data.\n  - Use `System.Text.Json`, `XmlSerializer`, or `DataContractSerializer` safely.\n  - Never deserialize untrusted data without validation.\n  - Implement digital signature validation for serialized objects.\n\nCSRF Protection Example (from OWASP):\n\n```csharp\nprotected override OnInit(EventArgs e) {\n    base.OnInit(e);\n    ViewStateUserKey = Session.SessionID;\n}\n```\n\nSecure Headers Configuration (from OWASP):\n\n```xml\n<system.webServer>\n  <httpProtocol>\n    <customHeaders>\n      <add name=\"Content-Security-Policy\"\n         value=\"default-src 'none'; style-src 'self'; img-src 'self'; font-src 'self'\" />\n      <add name=\"X-Content-Type-Options\" value=\"NOSNIFF\" />\n      <add name=\"X-Frame-Options\" value=\"DENY\" />\n      <add name=\"X-Permitted-Cross-Domain-Policies\" value=\"master-only\"/>\n      <add name=\"X-XSS-Protection\" value=\"0\"/>\n      <remove name=\"X-Powered-By\"/>\n    </customHeaders>\n  </httpProtocol>\n</system.webServer>\n```\n\nSummary:  \nSecure .NET applications by implementing proper authorization controls, using secure authentication and session management, applying strong cryptography, preventing injection attacks, configuring security headers, implementing CSRF protection, maintaining secure configurations, logging security events properly, and handling serialization safely."
  },
  {
    "path": "sources/owasp/codeguard-0-error-handling.md",
    "content": "---\ndescription: Error Handling Security Best Practices\nlanguages:\n- c\n- java\n- javascript\n- python\n- typescript\n- xml\nalwaysApply: false\n---\n\n## Error Handling Security Guidelines\n\nThis rule advises on secure error handling practices to prevent information leakage and ensure proper logging:\n\n- General Error Handling Security\n  - Implement global error handlers to catch all unhandled exceptions.\n  - Return generic error messages without exposing stack traces, file paths, or version information.\n  - Log detailed error information securely on the server side for investigation.\n  - Use appropriate HTTP status codes: 4xx for client errors, 5xx for server errors.\n\n- Production Environment Configuration\n  - Disable detailed error pages and debug information in production.\n  - Set `customErrors mode=\"RemoteOnly\"` in web.config for ASP.NET applications.\n  - Disable development exception pages in ASP.NET Core production environments.\n  - Configure proper error page redirects to generic error handlers.\n\n- Secure Error Logging\n  - Log exceptions with sufficient context (user ID, IP address, timestamp) for forensics.\n  - Never log sensitive data like passwords, tokens, or personal information in error logs.\n  - Use structured logging for better analysis and monitoring.\n  - Implement log rotation and secure storage for error logs.\n\n- Error Response Security\n  - Return consistent error response formats using standards like RFC 7807.\n  - Add security headers to error responses to prevent XSS and information disclosure.\n  - Ensure error response content is properly escaped to prevent injection attacks.\n  - Remove server version headers and technology stack information from error responses.\n\nCode Examples (from OWASP):\n\nStandard Java Web Application:\n```xml\n<!-- web.xml configuration -->\n<error-page>\n    <exception-type>java.lang.Exception</exception-type>\n    <location>/error.jsp</location>\n</error-page>\n```\n\n```java\n<%@ page language=\"java\" isErrorPage=\"true\" contentType=\"application/json; charset=UTF-8\"\n    pageEncoding=\"UTF-8\"%>\n<%\nString errorMessage = exception.getMessage();\n//Log the exception via the content of the implicit variable named \"exception\"\n//...\n//We build a generic response with a JSON format because we are in a REST API app context\n//We also add an HTTP response header to indicate to the client app that the response is an error\nresponse.setHeader(\"X-ERROR\", \"true\");\n//Note that we're using an internal server error response\n//In some cases it may be prudent to return 4xx error codes, when we have misbehaving clients\nresponse.setStatus(500);\n%>\n{\"message\":\"An error occur, please retry\"}\n```\n\nSpring Boot Global Error Handler:\n```java\n\n@RestControllerAdvice\npublic class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {\n\n    @ExceptionHandler(value = {Exception.class})\n    public ProblemDetail handleGlobalError(RuntimeException exception, WebRequest request) {\n        //Log the exception via the content of the parameter named \"exception\"\n        //...\n        //Note that we're using an internal server error response\n        //In some cases it may be prudent to return 4xx error codes, if we have misbehaving clients\n        //By specification, the content-type can be \"application/problem+json\" or \"application/problem+xml\"\n        return ProblemDetail.forStatusAndDetail(HttpStatus.INTERNAL_SERVER_ERROR, \"An error occur, please retry\");\n    }\n}\n```\n\nASP.NET Core Error Controller:\n```csharp\n[Route(\"api/[controller]\")]\n[ApiController]\n[AllowAnonymous]\npublic class ErrorController : ControllerBase\n{\n    [HttpGet]\n    [HttpPost]\n    [HttpHead]\n    [HttpDelete]\n    [HttpPut]\n    [HttpOptions]\n    [HttpPatch]\n    public JsonResult Handle()\n    {\n        //Get the exception that has implied the call to this controller\n        Exception exception = HttpContext.Features.Get<IExceptionHandlerFeature>()?.Error;\n        //Log the exception via the content of the variable named \"exception\" if it is not NULL\n        //...\n        //We build a generic response with a JSON format because we are in a REST API app context\n        //We also add an HTTP response header to indicate to the client app that the response is an error\n        var responseBody = new Dictionary<String, String>{ {\n            \"message\", \"An error occur, please retry\"\n        } };\n        JsonResult response = new JsonResult(responseBody);\n        //Note that we're using an internal server error response\n        //In some cases it may be prudent to return 4xx error codes, if we have misbehaving clients\n        response.StatusCode = (int)HttpStatusCode.InternalServerError;\n        Request.HttpContext.Response.Headers.Remove(\"X-ERROR\");\n        Request.HttpContext.Response.Headers.Add(\"X-ERROR\", \"true\");\n        return response;\n    }\n}\n```\n\nASP.NET Web.config Security Configuration:\n```xml\n<configuration>\n    <system.web>\n        <customErrors mode=\"RemoteOnly\"\n                      defaultRedirect=\"~/ErrorPages/Oops.aspx\" />\n    </system.web>\n</configuration>\n```\n\nSummary:  \nImplement centralized error handling with generic user messages while logging detailed error information securely. Disable debug information in production and ensure error responses don't leak sensitive system details."
  },
  {
    "path": "sources/owasp/codeguard-0-file-upload.md",
    "content": "---\ndescription: File Upload Security Best Practices\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\nalwaysApply: false\n---\n\n## File Upload Security Guidelines\n\nThis rule advises on secure file upload practices to prevent malicious file attacks and protect system integrity:\n\n- Extension Validation\n  - List allowed extensions only for business-critical functionality.\n  - Ensure input validation is applied before validating extensions.\n  - Avoid double extensions (e.g., `.jpg.php`) and null byte injection (e.g., `.php%00.jpg`).\n  - Use allowlist approach rather than denylist for file extensions.\n  - Validate extensions after decoding filename to prevent bypass attempts.\n\n- Content Type and File Signature Validation\n  - Never trust client-supplied Content-Type headers as they can be spoofed.\n  - Validate file signatures (magic numbers) in conjunction with Content-Type checking.\n  - Implement allowlist approach for MIME types as a quick protection layer.\n  - Use file signature validation but not as a standalone security measure.\n\n- Filename Security\n  - Generate random filenames (UUID/GUID) instead of using user-supplied names.\n  - If user filenames required, implement maximum length limits.\n  - Restrict characters to alphanumeric, hyphens, spaces, and periods only.\n  - Prevent leading periods (hidden files) and sequential periods (directory traversal).\n  - Avoid leading hyphens or spaces for safer shell script processing.\n\n- File Content Validation\n  - For images, apply image rewriting techniques to destroy malicious content.\n  - For Microsoft documents, use Apache POI for validation.\n  - Avoid ZIP files due to numerous attack vectors.\n  - Implement manual file review in sandboxed environments when resources allow.\n  - Integrate antivirus scanning and Content Disarm & Reconstruct (CDR) for applicable file types.\n\n- Storage Security\n  - Store files on different servers for complete segregation when possible.\n  - Store files outside webroot with administrative access only.\n  - If storing in webroot, set write-only permissions with proper access controls.\n  - Use application handlers that map IDs to filenames for public access.\n  - Consider database storage for specific use cases with DBA expertise.\n\n- Access Control and Authentication\n  - Require user authentication before allowing file uploads.\n  - Implement proper authorization levels for file access and modification.\n  - Set filesystem permissions on principle of least privilege.\n  - Scan files before execution if execution permission is required.\n\n- Upload and Download Limits\n  - Set proper file size limits for upload protection.\n  - Consider post-decompression size limits for compressed files.\n  - Implement request limits for download services to prevent DoS attacks.\n  - Use secure methods to calculate ZIP file sizes safely.\n\n- Additional Security Measures\n  - Protect file upload endpoints from CSRF attacks.\n  - Keep all file processing libraries securely configured and updated.\n  - Implement logging and monitoring for upload activities.\n  - Provide user reporting mechanisms for illegal content.\n  - Use secure extraction methods for compressed files.\n\nSummary:  \nImplement defense-in-depth for file uploads through multi-layered validation, secure storage practices, proper access controls, and comprehensive monitoring. Never rely on single validation methods and always generate safe filenames to prevent attacks."
  },
  {
    "path": "sources/owasp/codeguard-0-forgot-password.md",
    "content": "---\ndescription: Forgot Password Security Best Practices\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\nalwaysApply: false\n---\n\n## Forgot Password Security Guidelines\n\nThis rule advises on secure password reset implementation to prevent user enumeration, token abuse, and unauthorized access:\n\n- Password Reset Request Security\n  - Return consistent messages for both existent and non-existent accounts to prevent user enumeration.\n  - Ensure consistent response times by using asynchronous processing or identical code paths.\n  - Implement rate limiting per account and IP address to prevent flooding attacks.\n  - Apply input validation and SQL injection prevention to reset request forms.\n  - Use CSRF tokens to protect password reset forms and endpoints.\n\n- Token Generation and Storage\n  - Generate tokens using cryptographically secure random number generators.\n  - Make tokens sufficiently long to protect against brute-force attacks (minimum 32 bytes).\n  - Store password reset tokens securely using hashing (same practices as password storage).\n  - Link tokens to individual users in the database with expiration times.\n  - Make tokens single-use and invalidate immediately after successful use.\n\n- Password Reset Process Security\n  - Require users to confirm new passwords by entering twice.\n  - Enforce consistent password policy throughout the application.\n  - Store new passwords following secure password storage practices.\n  - Send confirmation emails without including the actual password.\n  - Never automatically log users in after password reset.\n  - Invalidate all existing user sessions after successful password reset.\n\n- URL Token Implementation\n  - Generate secure tokens and attach to URL query strings for email delivery.\n  - Use hardcoded or validated trusted domains for reset URLs (avoid Host header injection).\n  - Enforce HTTPS for all password reset URLs.\n  - Add referrer policy with 'noreferrer' value to prevent referrer leakage.\n  - Implement rate limiting to prevent token brute-forcing attempts.\n\n- PIN Implementation\n  - Generate PINs between 6-12 digits using cryptographically secure methods.\n  - Create limited sessions from PINs that only permit password reset operations.\n  - Format PINs with spaces for better user readability.\n  - Apply same security practices as tokens (single-use, expiration, secure storage).\n\n- Security Questions Integration\n  - Use security questions only as additional layer, never as sole mechanism.\n  - Follow secure question selection practices from OWASP guidance.\n  - Validate security question answers using secure comparison methods.\n\n- Logging and Monitoring (Code-Level)\n  - Log password reset attempts with user context but never log tokens or passwords.\n  - Implement structured logging for security monitoring integration.\n  - Include sufficient context for security analysis (IP, user agent, timestamp).\n  - Never log sensitive information in application logs.\n\n- Account Lockout Prevention\n  - Never lock accounts in response to password reset requests.\n  - Implement alternative abuse prevention through rate limiting and monitoring.\n  - Separate password reset abuse protection from authentication lockout mechanisms.\n\nSummary:  \nImplement secure password reset functionality through consistent response handling, cryptographically secure token generation and storage, CSRF protection, proper session management, and comprehensive logging while preventing user enumeration and account lockout attacks."
  },
  {
    "path": "sources/owasp/codeguard-0-graphql.md",
    "content": "---\ndescription: GraphQL Security Best Practices\nlanguages:\n- javascript\n- typescript\nalwaysApply: false\n---\n\n## GraphQL Security Guidelines\n\nThis rule advises on secure GraphQL API development to prevent injection, DoS, unauthorized access, and information leakage:\n\n- Input Validation and Injection Prevention\n  - Use specific GraphQL data types (scalars, enums) for all input validation.\n  - Define custom GraphQL validators for complex validations and use custom scalars.\n  - Define schemas for mutation inputs with strict validation rules.\n  - Use allowlist approach for character validation (avoid denylists).\n  - Apply parameterized statements and safe APIs for database queries in resolvers.\n  - Use ORMs/ODMs properly to avoid ORM injection vulnerabilities.\n  - Gracefully reject invalid input without revealing internal API details.\n\n- DoS Prevention and Query Limiting\n  - Implement query depth limiting using libraries like graphql-depth-limit (JavaScript) or MaxQueryDepthInstrumentation (Java).\n  - Add query complexity analysis using graphql-cost-analysis or MaxQueryComplexityInstrumentation.\n  - Enforce query amount limiting with libraries like graphql-input-number.\n  - Implement pagination to limit data returned in single responses.\n  - Add query timeouts at application level using custom instrumentation.\n  - Apply rate limiting per IP or user to prevent basic DoS attacks.\n  - Use server-side batching and caching (like Facebook's DataLoader) for efficiency.\n\n- Access Control and Authorization\n  - Validate requester authorization for all data viewing and mutation operations.\n  - Implement proper object-level authorization to prevent IDOR/BOLA vulnerabilities.\n  - Enforce authorization checks on both edges and nodes in GraphQL schema.\n  - Use Interfaces and Unions to return different object properties based on permissions.\n  - Add access control validation in Query and Mutation resolvers with RBAC middleware.\n  - Check for unintended node/nodes fields that allow direct object access by ID.\n  - Implement field-level access controls for sensitive data.\n\n- Batching Attack Prevention\n  - Limit the number of queries that can be batched and run simultaneously.\n  - Add object request rate limiting at code level to track instance requests.\n  - Prevent batching for sensitive objects (usernames, passwords, tokens, OTPs).\n  - Implement custom solutions to disable batching for critical operations.\n  - Monitor and log batching attempts for security analysis.\n\n- Secure Configuration Management\n  - Disable GraphQL introspection in production environments using NoIntrospectionGraphqlFieldVisibility (Java) or validation rules (JavaScript).\n  - Disable GraphiQL and similar exploration tools in production.\n  - Configure error masking to prevent stack traces and debug information exposure.\n  - Set NODE_ENV to 'production' or use debug: false in Apollo Server configuration.\n  - Disable field suggestion hints when introspection is disabled.\n\n- Authentication and Session Management\n  - Require authentication for all GraphQL endpoints (unless explicitly public).\n  - Implement proper session management with secure token validation.\n  - Use JWT or session-based authentication with proper validation in resolvers.\n  - Apply CSRF protection for GraphQL mutations when using cookie-based authentication.\n  - Validate authentication state before processing any queries or mutations.\n\nCode Examples (from OWASP):\n\nDisable Introspection - Java:\n```java\nGraphQLSchema schema = GraphQLSchema.newSchema()\n    .query(StarWarsSchema.queryType)\n    .fieldVisibility( NoIntrospectionGraphqlFieldVisibility.NO_INTROSPECTION_FIELD_VISIBILITY )\n    .build();\n```\n\nDisable Introspection & GraphiQL - JavaScript:\n```javascript\napp.use('/graphql', graphqlHTTP({\n  schema: MySessionAwareGraphQLSchema,\n+ validationRules: [NoIntrospection]\n  graphiql: process.env.NODE_ENV === 'development',\n}));\n```\n\nQuery Depth Example:\n```javascript\nquery evil {            # Depth: 0\n  album(id: 42) {       # Depth: 1\n    songs {             # Depth: 2\n      album {           # Depth: 3\n        songs {         # Depth: 4\n          album {id: N} # Depth: N\n        }\n      }\n    }\n  }\n}\n```\n\nExcessive Amount Request Example:\n```javascript\nquery {\n  author(id: \"abc\") {\n    posts(first: 99999999) {\n      title\n    }\n  }\n}\n```\n\nBatching Attack Example:\n```javascript\n[\n  {\n    query: < query 0 >,\n    variables: < variables for query 0 >,\n  },\n  {\n    query: < query 1 >,\n    variables: < variables for query 1 >,\n  },\n  {\n    query: < query n >\n    variables: < variables for query n >,\n  }\n]\n```\n\nSummary:  \nSecure GraphQL APIs through comprehensive input validation, query limiting, proper access controls, batching attack prevention, secure configuration management, and robust authentication mechanisms while preventing common attack vectors like injection, DoS, and unauthorized data access."
  },
  {
    "path": "sources/owasp/codeguard-0-html5-security.md",
    "content": "---\ndescription: HTML5 Security Best Practices\nlanguages:\n- c\n- go\n- html\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\n- xml\nalwaysApply: false\n---\n\n## HTML5 Security Guidelines\n\nThis rule advises on secure HTML5 development practices to prevent vulnerabilities in modern web applications:\n\n- Web Messaging Security\n  - Always specify exact target origin in postMessage calls, never use \"*\" wildcard.\n  - Verify event.origin strictly against trusted full domain names in message handlers.\n  - Avoid partial domain matching that could allow subdomain takeover attacks.\n  - Validate and sanitize all data received through postMessage before processing.\n  - Never use eval() or Function() constructor on message data.\n  - Use textContent instead of innerHTML when setting received message content.\n  - Treat all cross-origin message data as untrusted input requiring validation.\n\n- Cross-Origin Resource Sharing (CORS) Security\n  - Restrict Access-Control-Allow-Origin to specific trusted origins, avoid \"*\" wildcard.\n  - Validate URLs passed to XMLHttpRequest.open to prevent open redirects.\n  - Implement proper CSRF protection even when using CORS (CORS doesn't prevent CSRF).\n  - Reject mixed content requests (HTTP requests from HTTPS origins).\n  - Don't rely solely on Origin header for access control as it can be spoofed outside browsers.\n  - Use preflight requests for non-simple requests and validate Access-Control headers.\n\n- WebSocket Security\n  - Use only secure wss:// protocol, never unencrypted ws:// in production.\n  - Implement application-level authentication and authorization for WebSocket connections.\n  - Verify Origin header against allowlist during WebSocket handshake.\n  - Validate and parse all WebSocket messages safely using JSON.parse() with error handling.\n  - Implement connection limits and message size restrictions to prevent DoS attacks.\n  - Use JWT or similar tokens for WebSocket authentication with proper validation.\n  - Implement token invalidation and denylist mechanisms for revoked access.\n\n- Client-Side Storage Security\n  - Never store sensitive data (passwords, tokens, API keys, PII) in localStorage or sessionStorage.\n  - Use sessionStorage instead of localStorage when persistence across browser sessions is not required.\n  - Validate and sanitize all data retrieved from client-side storage before use.\n  - Implement data encryption for sensitive information stored client-side when absolutely necessary.\n  - Avoid hosting multiple applications on same origin to prevent storage access conflicts.\n\n- DOM Manipulation and Link Security\n  - Add rel=\"noopener noreferrer\" attribute to all external links with target=\"_blank\".\n  - Set window.opener = null when using window.open() for external URLs.\n  - Use iframe sandbox attribute with minimal permissions for untrusted content.\n  - Implement X-Frame-Options header alongside iframe sandbox for defense-in-depth.\n  - Avoid innerHTML with user-supplied content; use textContent, createElement, or trusted templating.\n  - When innerHTML must be used, sanitize content with DOMPurify or similar libraries.\n\n- Input Field Security and CSRF Protection\n  - Use autocomplete=\"off\" on sensitive input fields (passwords, credit cards, SSN).\n  - Add spellcheck=\"false\", autocorrect=\"off\", autocapitalize=\"off\" on credential inputs.\n  - Implement CSRF tokens on all state-changing forms and AJAX requests.\n  - Use secure cookie attributes: HttpOnly, Secure, SameSite=Strict for session cookies.\n  - Validate CSRF tokens on server-side for all POST, PUT, DELETE requests.\n\n- Secure Cookie Configuration\n  - Set HttpOnly flag on session cookies to prevent JavaScript access.\n  - Use Secure flag to ensure cookies are only sent over HTTPS connections.\n  - Implement SameSite=Strict or SameSite=Lax to prevent CSRF attacks.\n  - Use __Secure- or __Host- cookie prefixes for additional security.\n  - Set appropriate cookie expiration and path restrictions.\n\n- Web Workers and Service Workers Security\n  - Validate all messages sent to and received from Web Workers.\n  - Never create Web Workers from user-supplied URLs or content.\n  - Implement proper error handling and timeout mechanisms for Worker communications.\n  - Use Content Security Policy to restrict Worker script sources.\n  - Validate Service Worker registration and update mechanisms.\n\n- Geolocation and Device API Security\n  - Require explicit user permission before accessing Geolocation API.\n  - Validate geolocation data before sending to servers.\n  - Implement proper error handling for geolocation failures.\n  - Use HTTPS when transmitting location data to prevent interception.\n\n- Offline Application Security\n  - Require user consent before caching application data offline.\n  - Validate integrity of cached resources to prevent cache poisoning.\n  - Use HTTPS for all manifest files and cached resources.\n  - Implement proper cache invalidation mechanisms for security updates.\n\nSummary:  \nImplement comprehensive HTML5 security controls through proper origin validation, secure storage practices, CSRF protection, secure cookie configuration, safe DOM manipulation, and robust authentication mechanisms to prevent XSS, CSRF, clickjacking, and data leakage vulnerabilities."
  },
  {
    "path": "sources/owasp/codeguard-0-http-headers.md",
    "content": "---\ndescription: HTTP Security Headers Best Practices\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\n- xml\nalwaysApply: false\n---\n\n## HTTP Security Headers Guidelines\n\nThis rule enforces secure configuration of HTTP response headers to protect against common web vulnerabilities including XSS, Clickjacking, Information Disclosure, and MIME-type attacks.\n\n### Required Security Headers\n\n1. Content Security Policy (CSP)\n   - Must include default-src directive\n   - Must include script-src directive with appropriate restrictions\n   - Must include frame-ancestors directive for clickjacking protection\n   - Example: `Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; frame-ancestors 'none'`\n\n2. Cookie Security\n   - All session/sensitive cookies must have Secure flag\n   - All session/sensitive cookies must have HttpOnly flag\n   - All cookies must have SameSite attribute (Strict or Lax)\n   - Example: `Set-Cookie: session=123; Secure; HttpOnly; SameSite=Strict`\n\n3. Cross-Origin Isolation\n   - Must set Cross-Origin-Embedder-Policy (COEP)\n   - Must set Cross-Origin-Resource-Policy (CORP)\n   - Must set Cross-Origin-Opener-Policy (COOP)\n   - Examples:\n     ```\n     Cross-Origin-Embedder-Policy: require-corp\n     Cross-Origin-Resource-Policy: same-origin\n     Cross-Origin-Opener-Policy: same-origin\n     ```\n\n4. Transport Security\n   - Must set Strict-Transport-Security (HSTS)\n   - Must include appropriate max-age (minimum 1 year)\n   - Example: `Strict-Transport-Security: max-age=31536000; includeSubDomains`\n\n5. Cache Control\n   - Must set appropriate Cache-Control for sensitive data\n   - Example: `Cache-Control: no-store, max-age=0`\n\n6. Content Type Protection\n   - Must set X-Content-Type-Options\n   - Example: `X-Content-Type-Options: nosniff`\n\n### Prohibited Headers\n\nThe following headers must not be present or must be removed:\n- X-Powered-By\n- Server (or must contain non-revealing value)\n- X-AspNet-Version\n- X-AspNetMvc-Version\n\n### Required Header Combinations\n\nCertain security features require multiple headers to work effectively:\n\n1. Clickjacking Protection:\n   - Must use CSP frame-ancestors OR\n   - Must use X-Frame-Options: DENY\n\n2. XSS Protection:\n   - Must use CSP with appropriate script-src\n   - Must not rely solely on X-XSS-Protection\n\n3. CORS Security:\n   - Must not use Access-Control-Allow-Origin: *\n   - Must explicitly list allowed origins\n\n### Implementation Examples\n\nPHP:\n```php\nheader(\"X-Frame-Options: DENY\");\n```\n\nApache (.htaccess):\n```apache\n<IfModule mod_headers.c>\nHeader always set X-Frame-Options \"DENY\"\n</IfModule>\n```\n\nIIS (Web.config):\n```xml\n<system.webServer>\n...\n <httpProtocol>\n   <customHeaders>\n     <add name=\"X-Frame-Options\" value=\"DENY\" />\n   </customHeaders>\n </httpProtocol>\n...\n</system.webServer>\n```\n\nHAProxy:\n```\nhttp-response set-header X-Frame-Options DENY\n```\n\nNginx:\n```nginx\nadd_header \"X-Frame-Options\" \"DENY\" always;\n```\n\nExpress.js:\n```javascript\nconst helmet = require('helmet');\nconst app = express();\n// Sets \"X-Frame-Options: SAMEORIGIN\"\napp.use(\n helmet.frameguard({\n   action: \"sameorigin\",\n })\n);\n```\n\n### Testing Tools\n\nMozilla Observatory is an online tool which helps you to check your website's header status.\n\nSmartScanner has a dedicated test profile for testing security of HTTP headers. Online tools usually test the homepage of the given address. But SmartScanner scans the whole website, ensuring all web pages have the right HTTP Headers in place."
  },
  {
    "path": "sources/owasp/codeguard-0-http-strict-transport-security.md",
    "content": "---\ndescription: HTTP Strict Transport Security Best Practices\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\nalwaysApply: false\n---\n\n## HTTP Strict Transport Security Guidelines\n\nThis rule enforces secure configuration of HTTP Strict Transport Security (HSTS) headers to protect users by ensuring all communications occur over HTTPS.\n\n### Introduction\n\nHTTP Strict Transport Security (HSTS) is an opt-in security enhancement specified by a web application through a special response header. Once a supported browser receives this header, it prevents any communications from being sent over HTTP to the specified domain and instead sends all communications over HTTPS. It also prevents HTTPS click through prompts on browsers.\n\nCritical Requirement: The Strict-Transport-Security header is only honored over HTTPS connections and is completely ignored when sent over HTTP, per RFC 6797.\n\n### Threats Addressed\n\nHSTS protects against:\n- Man-in-the-middle attacks when users bookmark or manually type `http://example.com`\n- Web applications inadvertently containing HTTP links or serving content over HTTP\n- Man-in-the-middle attackers using invalid certificates (HSTS prevents users from accepting bad certificates)\n\n### Required Configuration\n\n1. Basic HSTS Header (testing phase):\n   ```\n   Strict-Transport-Security: max-age=86400; includeSubDomains\n   ```\n\n2. Production HSTS Header (1 year minimum):\n   ```\n   Strict-Transport-Security: max-age=31536000; includeSubDomains\n   ```\n\n3. Preload-Ready HSTS Header:\n   ```\n   Strict-Transport-Security: max-age=31536000; includeSubDomains; preload\n   ```\n\n### Phased Rollout Requirements\n\n1. Phase 1 - Testing (recommended first step):\n   - Use short max-age (600-86400 seconds) for initial testing\n   - Monitor for any HTTP content or functionality issues\n\n2. Phase 2 - Production:\n   - Increase max-age to minimum 1 year (31536000 seconds)\n   - Add `includeSubDomains` only if all subdomains support HTTPS\n\n3. Phase 3 - Preload (optional):\n   - Add `preload` directive only after thorough testing\n   - Submit domain to HSTS preload list at hstspreload.org\n   - Warning: Preload is effectively permanent and affects all subdomains\n\n### Implementation Examples\n\nSimple example using 1 year max-age (dangerous without includeSubDomains):\n```\nStrict-Transport-Security: max-age=31536000\n```\n\nSecure example with subdomain protection:\n```\nStrict-Transport-Security: max-age=31536000; includeSubDomains\n```\n\nShort max-age for initial rollout testing:\n```\nStrict-Transport-Security: max-age=86400; includeSubDomains\n```\n\n### Monitoring and Validation\n\nRequired post-deployment actions:\n- Verify HSTS header presence in all HTTPS responses\n- Monitor browser console for mixed content warnings\n- Audit all internal links and redirects for HTTP references\n- Test subdomain HTTPS availability before enabling includeSubDomains\n- Use tools like Mozilla Observatory or Security Headers to validate configuration\n\n### Browser Support\n\nHSTS is supported by all modern browsers. The only notable exception is Opera Mini."
  },
  {
    "path": "sources/owasp/codeguard-0-injection-prevention.md",
    "content": "---\ndescription: Injection Prevention Best Practices\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- powershell\n- python\n- ruby\n- shell\n- sql\n- typescript\nalwaysApply: false\n---\n\n## Injection Prevention Guidelines\n\nThis rule provides clear, actionable guidance for preventing injection flaws across multiple languages and injection types. Injection flaws occur when untrusted data is sent to an interpreter as part of a command or query.\n\n### Introduction\n\nInjection attacks, especially SQL Injection, are unfortunately very common. Injection flaws occur when an application sends untrusted data to an interpreter. Injection flaws are very prevalent, particularly in legacy code, often found in SQL queries, LDAP queries, XPath queries, OS commands, program arguments, etc.\n\n### SQL Injection Prevention\n\nDefense Option 1: Prepared Statements (with Parameterized Queries)\n\nSafe Java Prepared Statement Example:\n```java\n// This should REALLY be validated too\nString custname = request.getParameter(\"customerName\"); \nString query = \"SELECT account_balance FROM user_data WHERE user_name = ?\";\nPreparedStatement pstmt = connection.prepareStatement( query );\npstmt.setString( 1, custname); \nResultSet results = pstmt.executeQuery( );\n```\n\nDefense Option 2: Stored Procedures\n\nSafe Java Stored Procedure Example:\n```java\n// This should REALLY be validated\nString custname = request.getParameter(\"customerName\");\ntry {\n CallableStatement cs = connection.prepareCall(\"{call sp_getAccountBalance(?)}\");\n cs.setString(1, custname);\n ResultSet results = cs.executeQuery();\n // Result set handling...\n} catch (SQLException se) {\n // Logging and error handling...\n}\n```\n\nDefense Option 3: Allow-List Input Validation\n\nDefense Option 4: Escaping All User-Supplied Input\n\n### LDAP Injection Prevention\n\nEscape all variables using the right LDAP encoding function\n\nSafe Java for LDAP escaping Example:\n```java\npublic String escapeDN (String name) {\n //From RFC 2253 and the / character for JNDI\n final char[] META_CHARS = {'+', '\"', '<', '>', ';', '/'};\n String escapedStr = new String(name);\n //Backslash is both a Java and an LDAP escape character,\n //so escape it first\n escapedStr = escapedStr.replaceAll(\"\\\\\\\\\\\\\\\\\",\"\\\\\\\\\\\\\\\\\");\n //Positional characters - see RFC 2253\n escapedStr = escapedStr.replaceAll(\"\\^#\",\"\\\\\\\\\\\\\\\\#\");\n escapedStr = escapedStr.replaceAll(\"\\^ | $\",\"\\\\\\\\\\\\\\\\ \");\n for (int i=0 ; i < META_CHARS.length ; i++) {\n        escapedStr = escapedStr.replaceAll(\"\\\\\\\\\" +\n                     META_CHARS[i],\"\\\\\\\\\\\\\\\\\" + META_CHARS[i]);\n }\n return escapedStr;\n}\n```\n\n```java\npublic String escapeSearchFilter (String filter) {\n //From RFC 2254\n String escapedStr = new String(filter);\n escapedStr = escapedStr.replaceAll(\"\\\\\\\\\\\\\\\\\",\"\\\\\\\\\\\\\\\\5c\");\n escapedStr = escapedStr.replaceAll(\"\\\\\\\\\\*\",\"\\\\\\\\\\\\\\\\2a\");\n escapedStr = escapedStr.replaceAll(\"\\\\\\\\(\",\"\\\\\\\\\\\\\\\\28\");\n escapedStr = escapedStr.replaceAll(\"\\\\\\\\)\",\"\\\\\\\\\\\\\\\\29\");\n escapedStr = escapedStr.replaceAll(\"\\\\\\\\\" +\n               Character.toString('\\u0000'), \"\\\\\\\\\\\\\\\\00\");\n return escapedStr;\n}\n```\n\n### Operating System Commands\n\nIf it is considered unavoidable the call to a system command incorporated with user-supplied input, the following two layers of defense should be used:\n\n1. Parameterization - If available, use structured mechanisms that automatically enforce the separation between data and command\n2. Input validation - the values for commands and the relevant arguments should be both validated:\n   - Commands must be validated against a list of allowed commands\n   - Arguments should be validated using positive or allowlist input validation\n   - Allow-list Regular Expression - explicitly define a list of good characters allowed and maximum length. Ensure that metacharacters like `& | ; $ > < \\` \\ !` and whitespaces are not part of the Regular Expression\n\nExample regular expression: `^[a-z0-9]{3,10}$`\n\nIncorrect Usage:\n```java\nProcessBuilder b = new ProcessBuilder(\"C:\\DoStuff.exe -arg1 -arg2\");\n```\n\nCorrect Usage:\n```java\nProcessBuilder pb = new ProcessBuilder(\"TrustedCmd\", \"TrustedArg1\", \"TrustedArg2\");\nMap<String, String> env = pb.environment();\npb.directory(new File(\"TrustedDir\"));\nProcess p = pb.start();\n```\n\n### Injection Prevention Rules\n\nRule #1 (Perform proper input validation)\n- Perform proper input validation. Positive or allowlist input validation with appropriate canonicalization is recommended, but is not a complete defense as many applications require special characters in their input.\n\nRule #2 (Use a safe API)\n- The preferred option is to use a safe API which avoids the use of the interpreter entirely or provides a parameterized interface. Be careful of APIs, such as stored procedures, that are parameterized, but can still introduce injection under the hood.\n\nRule #3 (Contextually escape user data)\n- If a parameterized API is not available, you should carefully escape special characters using the specific escape syntax for that interpreter."
  },
  {
    "path": "sources/owasp/codeguard-0-input-validation.md",
    "content": "---\ndescription: Input Validation Security Best Practices\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- sql\n- typescript\n- xml\nalwaysApply: false\n---\n\n## Input Validation Security Guidelines\n\nThis rule provides clear, actionable guidance for implementing robust input validation security functionality in applications.\n\n### Introduction\n\nInput validation ensures only properly formed data enters the workflow, preventing malformed data from persisting in the database and triggering malfunction of downstream components. Input validation should happen as early as possible in the data flow.\n\nData from all potentially untrusted sources should be subject to input validation, including Internet-facing web clients and backend feeds from suppliers, partners, vendors or regulators.\n\nInput Validation should not be used as the primary method of preventing XSS, SQL Injection and other attacks but can significantly contribute to reducing their impact if implemented properly.\n\n### Input Validation Strategies\n\n- Syntactic validation: enforce correct syntax of structured fields (e.g. SSN, date, currency symbol)\n- Semantic validation: enforce correctness of values in specific business context (e.g. start date before end date, price within expected range)\n\n### Implementing Input Validation\n\n- Data type validators available in web application frameworks (Django Validators, Apache Commons Validators)\n- Validation against JSON Schema and XML Schema (XSD)\n- Type conversion with strict exception handling (Integer.parseInt() in Java, int() in Python)\n- Range checks for numerical parameters and length checks for strings\n- Array of allowed values for small sets of string parameters\n- Regular expressions covering the whole input string (^...$) avoiding \"any character\" wildcards\n\n### Allowlist vs Denylist\n\nAllowlist validation defines exactly what IS authorized; everything else is not authorized. For structured data (dates, SSNs, zip codes, emails), define strong validation patterns using regular expressions. For fixed options (dropdowns, radio buttons), input must match exactly one offered value.\n\n### Validating Free-form Unicode Text\n\n- Normalization: Ensure canonical encoding across all text\n- Character category allowlisting: Use Unicode categories like \"decimal digits\" or \"letters\"\n- Individual character allowlisting: Allow specific characters like apostrophe for names\n\n### Regular Expressions (Regex)\n\nBe aware of RegEx Denial of Service (ReDoS) attacks. Input validation should:\n- Be applied to all input data\n- Define allowed character sets\n- Define minimum and maximum length (e.g. {1,25})\n\n### Allow List Regular Expression Examples\n\nU.S. Zip Code: `^\\d{5}(-\\d{4})?$`\n\nJava Example:\n```java\nprivate static final Pattern zipPattern = Pattern.compile(\"^\\d{5}(-\\d{4})?$\");\n\npublic void doPost(HttpServletRequest request, HttpServletResponse response) {\n  try {\n      String zipCode = request.getParameter(\"zip\");\n      if (!zipPattern.matcher(zipCode).matches()) {\n          throw new YourValidationException(\"Improper zipcode format.\");\n      }\n  } catch(YourValidationException e) {\n      response.sendError(response.SC_BAD_REQUEST, e.getMessage());\n  }\n}\n```\n\n### Client-side vs Server-side Validation\n\nInput validation must be implemented on the server-side before data processing, as client-side validation can be circumvented by attackers.\n\n### File Upload Validation\n\nUpload Verification:\n- Validate filename uses expected extension type\n- Enforce maximum file size limits\n- Check ZIP files before extraction (target path, compression level, estimated size)\n\nUpload Storage:\n- Use server-generated random filenames\n- Analyze uploaded files for malicious content\n- Server defines file paths, not client\n\nBeware of dangerous file types:\n- crossdomain.xml / clientaccesspolicy.xml\n- .htaccess and .htpasswd\n- Web executable scripts: aspx, asp, css, swf, jsp, js, php, cgi\n\nImage Upload:\n- Use image rewriting libraries to verify and strip content\n- Set extension based on detected content type\n- Ensure content type is within defined image types\n\n### Email Address Validation\n\nSyntactic Validation:\n- Contains two parts separated by @\n- No dangerous characters (backticks, quotes, null bytes)\n- Domain contains only letters, numbers, hyphens, periods\n- Length limits: local part ≤ 63 chars, total ≤ 254 chars\n\nSemantic Validation:\n- Send verification email with secure token\n- Token requirements: ≥32 chars, cryptographically random, single-use, time-limited"
  },
  {
    "path": "sources/owasp/codeguard-0-insecure-direct-object-reference-prevention.md",
    "content": "---\ndescription: Insecure Direct Object Reference Prevention\nlanguages:\n- c\n- go\n- java\n- javascript\n- kotlin\n- php\n- python\n- ruby\n- swift\n- typescript\nalwaysApply: false\n---\n\n## Insecure Direct Object Reference Prevention Guidelines\n\nThis rule provides actionable guidance for preventing IDOR vulnerabilities by implementing proper access control checks around direct object references.\n\n### Introduction\n\nInsecure Direct Object Reference (IDOR) is a vulnerability that arises when attackers can access or modify objects by manipulating identifiers used in a web application's URLs or parameters. It occurs due to missing access control checks, which fail to verify whether a user should be allowed to access specific data.\n\n### Examples\n\nFor instance, when a user accesses their profile, the application might generate a URL like this:\n\n```\nhttps://example.org/users/123\n```\n\nThe 123 in the URL is a direct reference to the user's record in the database, often represented by the primary key. If an attacker changes this number to 124 and gains access to another user's information, the application is vulnerable to Insecure Direct Object Reference.\n\nIn some cases, the identifier may not be in the URL, but rather in the POST body:\n\n```html\n<form action=\"/update_profile\" method=\"post\">\n  <input type=\"hidden\" name=\"user_id\" value=\"12345\">\n  <button type=\"submit\">Update Profile</button>\n</form>\n```\n\nIn this example, attackers can manipulate the \"user_id\" field to modify profiles of other users without authorization.\n\n### Identifier Complexity\n\nIn some cases, using more complex identifiers like GUIDs can make it practically impossible for attackers to guess valid values. However, even with complex identifiers, access control checks are essential. If attackers obtain URLs for unauthorized objects, the application should still block their access attempts.\n\n### Mitigation\n\nTo mitigate IDOR, implement access control checks for each object that users try to access. Web frameworks often provide ways to facilitate this. Additionally, use complex identifiers as a defense-in-depth measure, but remember that access control is crucial even with these identifiers.\n\nAvoid exposing identifiers in URLs and POST bodies if possible. Instead, determine the currently authenticated user from session information. When using multi-step flows, pass identifiers in the session to prevent tampering.\n\nWhen looking up objects based on primary keys, use datasets that users have access to. For example, in Ruby on Rails:\n\n```ruby\n// vulnerable, searches all projects\n@project = Project.find(params[:id])\n// secure, searches projects related to the current user\n@project = @current_user.projects.find(params[:id])\n```\n\nVerify the user's permission every time an access attempt is made. Implement this structurally using the recommended approach for your web framework.\n\nAs an additional defense-in-depth measure, replace enumerable numeric identifiers with more complex, random identifiers. You can achieve this by adding a column with random strings in the database table and using those strings in the URLs instead of numeric primary keys. Another option is to use UUIDs or other long random values as primary keys. Avoid encrypting identifiers as it can be challenging to do so securely."
  },
  {
    "path": "sources/owasp/codeguard-0-jaas.md",
    "content": "---\ndescription: JAAS Security Best Practices\nlanguages:\n- c\n- java\n- xml\nalwaysApply: false\n---\n\n## Introduction - What is JAAS authentication\n\nThe process of verifying the identity of a user or another system is authentication.\n\nJAAS, as an authentication framework manages the authenticated user's identity and credentials from login to logout.\n\nThe JAAS authentication lifecycle:\n\n1. Create `LoginContext`.\n2. Read the configuration file for one or more `LoginModules` to initialize.\n3. Call `LoginContext.initialize()` for each LoginModule to initialize.\n4. Call `LoginContext.login()` for each LoginModule.\n5. If login successful then call `LoginContext.commit()` else call `LoginContext.abort()`\n\n## Configuration file\n\nThe JAAS configuration file contains a `LoginModule` stanza for each `LoginModule` available for logging on to the application.\n\nA stanza from a JAAS configuration file:\n\n```text\nBranches\n{\n    USNavy.AppLoginModule required\n    debug=true\n    succeeded=true;\n}\n```\n\nNote the placement of the semicolons, terminating both `LoginModule` entries and stanzas.\n\nThe word required indicates the `LoginContext`'s `login()` method must be successful when logging in the user. The `LoginModule`-specific values `debug` and `succeeded` are passed to the `LoginModule`.\n\nThey are defined by the `LoginModule` and their usage is managed inside the `LoginModule`. Note, Options are Configured using key-value pairing such as `debug=\"true\"` and the key and value should be separated by a `=` sign.\n\n## Main.java (The client)\n\n- Execution syntax:\n\n```text\nJava –Djava.security.auth.login.config==packageName/packageName.config\n        packageName.Main Stanza1\n\nWhere:\n    packageName is the directory containing the config file.\n    packageName.config specifies the config file in the Java package, packageName.\n    packageName.Main specifies Main.java in the Java package, packageName.\n    Stanza1 is the name of the stanza Main() should read from the config file.\n```\n\n- When executed, the 1st command-line argument is the stanza from the config file. The Stanza names the `LoginModule` to be used. The 2nd argument is the `CallbackHandler`.\n- Create a new `LoginContext` with the arguments passed to `Main.java`.\n    - `loginContext = new LoginContext (args[0], new AppCallbackHandler());`\n- Call the LoginContext.Login Module:\n    - `loginContext.login();`\n- The value in succeeded Option is returned from `loginContext.login()`.\n- If the login was successful, a subject was created.\n\n## LoginModule.java\n\nA `LoginModule` must have the following authentication methods:\n\n- `initialize()`\n- `login()`\n- `commit()`\n- `abort()`\n- `logout()`\n\n### initialize()\n\nIn `Main()`, after the `LoginContext` reads the correct stanza from the config file, the `LoginContext` instantiates the `LoginModule` specified in the stanza.\n\n- `initialize()` methods signature:\n    - `Public void initialize (Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options)`\n- The arguments above should be saved as follows:\n    - `this.subject = subject;`\n    - `this.callbackHandler = callbackHandler;`\n    - `this.sharedState = sharedState;`\n    - `this.options = options;`\n- What the `initialize()` method does:\n    - Builds a subject object of the `Subject` class contingent on a successful `login()`.\n    - Sets the `CallbackHandler` which interacts with the user to gather login information.\n    - If a `LoginContext` specifies 2 or more LoginModules, which is legal, they can share information via a `sharedState` map.\n    - Saves state information such as debug and succeeded in an options Map.\n\n### login()\n\nCaptures user supplied login information. The code snippet below declares an array of two callback objects which, when passed to the `callbackHandler.handle` method in the `callbackHandler.java` program, will be loaded with a username and password provided interactively by the user:\n\n```java\nNameCallback nameCB = new NameCallback(\"Username\");\nPasswordCallback passwordCB = new PasswordCallback (\"Password\", false);\nCallback[] callbacks = new Callback[] { nameCB, passwordCB };\ncallbackHandler.handle (callbacks);\n```\n\n- Authenticates the user\n- Retrieves the user supplied information from the callback objects:\n    - `String ID = nameCallback.getName ();`\n    - `char[] tempPW = passwordCallback.getPassword ();`\n- Compare `name` and `tempPW` to values stored in a repository such as LDAP.\n- Set the value of the variable succeeded and return to `Main()`.\n\n### commit()\n\nOnce the users credentials are successfully verified during `login()`, the JAAS authentication framework associates the credentials, as needed, with the subject.\n\nThere are two types of credentials, **Public** and **Private**:\n\n- Public credentials include public keys.\n- Private credentials include passwords and public keys.\n\nPrincipals (i.e. Identities the subject has other than their login name) such as employee number or membership ID in a user group are added to the subject.\n\nBelow, is an example `commit()` method where first, for each group the authenticated user has membership in, the group name is added as a principal to the subject. The subject's username is then added to their public credentials.\n\nCode snippet setting then adding any principals and a public credentials to a subject:\n\n```java\npublic boolean commit() {\n    If (userAuthenticated) {\n        Set groups = UserService.findGroups (username);\n        for (Iterator itr = groups.iterator (); itr.hasNext (); {\n            String groupName = (String) itr.next ();\n            UserGroupPrincipal group = new UserGroupPrincipal (GroupName);\n            subject.getPrincipals ().add (group);\n        }\n        UsernameCredential cred = new UsernameCredential (username);\n        subject.getPublicCredentials().add (cred);\n    }\n}\n```\n\n### abort()\n\nThe `abort()` method is called when authentication doesn't succeed. Before the `abort()` method exits the `LoginModule`, care should be taken to reset state including the username and password input fields.\n\n### logout()\n\nThe release of the users principals and credentials when `LoginContext.logout` is called:\n\n```java\npublic boolean logout() {\n    if (!subject.isReadOnly()) {\n        Set principals = subject.getPrincipals(UserGroupPrincipal.class);\n        subject.getPrincipals().removeAll(principals);\n        Set creds = subject.getPublicCredentials(UsernameCredential.class);\n        subject.getPublicCredentials().removeAll(creds);\n        return true;\n    } else {\n        return false;\n    }\n}\n```\n\n## CallbackHandler.java\n\nThe `callbackHandler` is in a source (`.java`) file separate from any single `LoginModule` so that it can service a multitude of LoginModules with differing callback objects:\n\n- Creates instance of the `CallbackHandler` class and has only one method, `handle()`.\n- A `CallbackHandler` servicing a LoginModule requiring username & password to login:\n\n```java\npublic void handle(Callback[] callbacks) {\n    for (int i = 0; i < callbacks.length; i++) {\n        Callback callback = callbacks[i];\n        if (callback instanceof NameCallback) {\n            NameCallback nameCallBack = (NameCallback) callback;\n            nameCallBack.setName(username);\n    }  else if (callback instanceof PasswordCallback) {\n            PasswordCallback passwordCallBack = (PasswordCallback) callback;\n            passwordCallBack.setPassword(password.toCharArray());\n        }\n    }\n}\n```"
  },
  {
    "path": "sources/owasp/codeguard-0-java-security.md",
    "content": "---\ndescription: Java Security Best Practices\nlanguages:\n- c\n- java\n- javascript\n- typescript\n- xml\n- yaml\nalwaysApply: false\n---\n\n## Java Security Guidelines\n\nKey security practices for secure Java application development.\n\n### SQL Injection Prevention\n\nUse parameterized queries to prevent SQL injection:\n\n```java\n// Safe - PreparedStatement with parameters\nString query = \"select * from color where friendly_name = ?\";\ntry (PreparedStatement pStatement = con.prepareStatement(query)) {\n    pStatement.setString(1, userInput);\n    try (ResultSet rSet = pStatement.executeQuery()) {\n        // Process results\n    }\n}\n```\n\n### JPA Query Security\n\nUse parameterized JPA queries:\n\n```java\n// Safe - Named parameters\nString queryPrototype = \"select c from Color c where c.friendlyName = :colorName\";\nQuery queryObject = entityManager.createQuery(queryPrototype);\nColor c = (Color) queryObject.setParameter(\"colorName\", userInput).getSingleResult();\n```\n\n### XSS Prevention\n\nApply input validation and output encoding:\n\n```java\n// Input validation with allowlist\nif (!Pattern.matches(\"[a-zA-Z0-9\\\\s\\\\-]{1,50}\", userInput)) {\n    return false;\n}\n\n// Output sanitization\nPolicyFactory policy = new HtmlPolicyBuilder().allowElements(\"p\", \"strong\").toFactory();\nString safeOutput = policy.sanitize(outputToUser);\nsafeOutput = Encode.forHtml(safeOutput);\n```\n\n### Secure Logging\n\nUse parameterized logging to prevent log injection:\n\n```java\n// Safe - parameterized logging\nlogger.warn(\"Login failed for user {}.\", username);\n\n// Avoid - string concatenation\n// logger.warn(\"Login failed for user \" + username);\n```\n\n### Cryptography Best Practices\n\nUse trusted cryptographic libraries and secure algorithms:\n\n```java\n// Use AES-GCM with proper nonce management\nCipher cipher = Cipher.getInstance(\"AES/GCM/NoPadding\");\nGCMParameterSpec gcmParameterSpec = new GCMParameterSpec(128, nonce);\ncipher.init(Cipher.ENCRYPT_MODE, secretKey, gcmParameterSpec);\n\n// Generate secure random nonces\nbyte[] nonce = new byte[12];\nSecureRandom.getInstanceStrong().nextBytes(nonce);\n```\n\n### Key Security Requirements\n\n- Never hardcode cryptographic keys in source code\n- Use Google Tink or similar trusted crypto libraries when possible\n- Avoid writing custom cryptographic implementations\n- Implement proper key rotation and management\n- Keep all dependencies updated with security patches"
  },
  {
    "path": "sources/owasp/codeguard-0-json-web-token-for-java.md",
    "content": "---\ndescription: JSON Web Token Security for Java\nlanguages:\n- c\n- java\n- javascript\n- typescript\n- xml\n- yaml\nalwaysApply: false\n---\n\n## JSON Web Token Security for Java\n\nKey security practices for implementing JWT in Java applications.\n\n### Algorithm Security\n\nAlways specify the expected algorithm explicitly:\n\n```java\n// Prevent 'none' algorithm attack\nJWTVerifier verifier = JWT.require(Algorithm.HMAC256(keyHMAC)).build();\nDecodedJWT decodedToken = verifier.verify(token);\n```\n\n### Token Sidejacking Prevention\n\nCode to create the token after successful authentication.\n\n``` java\n// HMAC key - Block serialization and storage as String in JVM memory\nprivate transient byte[] keyHMAC = ...;\n// Random data generator\nprivate SecureRandom secureRandom = new SecureRandom();\n\n...\n\n//Generate a random string that will constitute the fingerprint for this user\nbyte[] randomFgp = new byte[50];\nsecureRandom.nextBytes(randomFgp);\nString userFingerprint = DatatypeConverter.printHexBinary(randomFgp);\n\n//Add the fingerprint in a hardened cookie - Add cookie manually because\n//SameSite attribute is not supported by javax.servlet.http.Cookie class\nString fingerprintCookie = \"__Secure-Fgp=\" + userFingerprint\n                           + \"; SameSite=Strict; HttpOnly; Secure\";\nresponse.addHeader(\"Set-Cookie\", fingerprintCookie);\n\n//Compute a SHA256 hash of the fingerprint in order to store the\n//fingerprint hash (instead of the raw value) in the token\n//to prevent an XSS to be able to read the fingerprint and\n//set the expected cookie itself\nMessageDigest digest = MessageDigest.getInstance(\"SHA-256\");\nbyte[] userFingerprintDigest = digest.digest(userFingerprint.getBytes(\"utf-8\"));\nString userFingerprintHash = DatatypeConverter.printHexBinary(userFingerprintDigest);\n\n//Create the token with a validity of 15 minutes and client context (fingerprint) information\nCalendar c = Calendar.getInstance();\nDate now = c.getTime();\nc.add(Calendar.MINUTE, 15);\nDate expirationDate = c.getTime();\nMap<String, Object> headerClaims = new HashMap<>();\nheaderClaims.put(\"typ\", \"JWT\");\nString token = JWT.create().withSubject(login)\n   .withExpiresAt(expirationDate)\n   .withIssuer(this.issuerID)\n   .withIssuedAt(now)\n   .withNotBefore(now)\n   .withClaim(\"userFingerprint\", userFingerprintHash)\n   .withHeader(headerClaims)\n   .sign(Algorithm.HMAC256(this.keyHMAC));\n```\n\nCode to validate the token.\n\n``` java\n// HMAC key - Block serialization and storage as String in JVM memory\nprivate transient byte[] keyHMAC = ...;\n\n...\n\n//Retrieve the user fingerprint from the dedicated cookie\nString userFingerprint = null;\nif (request.getCookies() != null && request.getCookies().length > 0) {\n List<Cookie> cookies = Arrays.stream(request.getCookies()).collect(Collectors.toList());\n Optional<Cookie> cookie = cookies.stream().filter(c -> \"__Secure-Fgp\"\n                                            .equals(c.getName())).findFirst();\n if (cookie.isPresent()) {\n   userFingerprint = cookie.get().getValue();\n }\n}\n\n//Compute a SHA256 hash of the received fingerprint in cookie in order to compare\n//it to the fingerprint hash stored in the token\nMessageDigest digest = MessageDigest.getInstance(\"SHA-256\");\nbyte[] userFingerprintDigest = digest.digest(userFingerprint.getBytes(\"utf-8\"));\nString userFingerprintHash = DatatypeConverter.printHexBinary(userFingerprintDigest);\n\n//Create a verification context for the token\nJWTVerifier verifier = JWT.require(Algorithm.HMAC256(keyHMAC))\n                              .withIssuer(issuerID)\n                              .withClaim(\"userFingerprint\", userFingerprintHash)\n                              .build();\n\n//Verify the token, if the verification fail then an exception is thrown\nDecodedJWT decodedToken = verifier.verify(token);\n```\n\n\n### Token Revocation\n\nImplement token blacklist for logout functionality:\n\n\nCode in charge of adding a token to the denylist and checking if a token is revoked.\n\n``` java\n/**\n* Handle the revocation of the token (logout).\n* Use a DB in order to allow multiple instances to check for revoked token\n* and allow cleanup at centralized DB level.\n*/\npublic class TokenRevoker {\n\n /** DB Connection */\n @Resource(\"jdbc/storeDS\")\n private DataSource storeDS;\n\n /**\n  * Verify if a digest encoded in HEX of the ciphered token is present\n  * in the revocation table\n  *\n  * @param jwtInHex Token encoded in HEX\n  * @return Presence flag\n  * @throws Exception If any issue occur during communication with DB\n  */\n public boolean isTokenRevoked(String jwtInHex) throws Exception {\n     boolean tokenIsPresent = false;\n     if (jwtInHex != null && !jwtInHex.trim().isEmpty()) {\n         //Decode the ciphered token\n         byte[] cipheredToken = DatatypeConverter.parseHexBinary(jwtInHex);\n\n         //Compute a SHA256 of the ciphered token\n         MessageDigest digest = MessageDigest.getInstance(\"SHA-256\");\n         byte[] cipheredTokenDigest = digest.digest(cipheredToken);\n         String jwtTokenDigestInHex = DatatypeConverter.printHexBinary(cipheredTokenDigest);\n\n         //Search token digest in HEX in DB\n         try (Connection con = this.storeDS.getConnection()) {\n             String query = \"select jwt_token_digest from revoked_token where jwt_token_digest = ?\";\n             try (PreparedStatement pStatement = con.prepareStatement(query)) {\n                 pStatement.setString(1, jwtTokenDigestInHex);\n                 try (ResultSet rSet = pStatement.executeQuery()) {\n                     tokenIsPresent = rSet.next();\n                 }\n             }\n         }\n     }\n\n     return tokenIsPresent;\n }\n\n\n### Secure Client Storage\n\nStore tokens in sessionStorage with fingerprint binding:\n\n```javascript\n// Store in sessionStorage, not localStorage\nsessionStorage.setItem(\"token\", data.token);\n\n// Send as Bearer token\nxhr.setRequestHeader(\"Authorization\", \"bearer \" + token);\n```\n\n### Strong Secrets\n\nUse strong HMAC secrets or RSA keys:\n- HMAC secrets: minimum 64 characters, cryptographically random\n- Prefer RSA or ECDSA over HMAC for better security\n- Never hardcode secrets in source code"
  },
  {
    "path": "sources/owasp/codeguard-0-key-management.md",
    "content": "---\ndescription: Cryptographic Key Management Security\nlanguages:\n- c\n- go\n- java\n- javascript\n- kotlin\n- python\n- ruby\n- swift\n- typescript\nalwaysApply: false\n---\n\n## Key Management Security Guidelines\n\nEssential practices for secure cryptographic key management in applications.\n\n### Key Generation\n\nCryptographic keys shall be generated within cryptographic module with at least FIPS 140-2 compliance. Any random value required by the key-generating module shall be generated within that module using a Random Bit Generator implemented within the cryptographic module.\n\nHardware cryptographic modules are preferred over software cryptographic modules for protection.\n\n### Key Storage\n\n- Keys must be protected on both volatile and persistent memory, ideally processed within secure cryptographic modules\n- Keys should never be stored in plaintext format\n- Ensure all keys are stored in a cryptographic vault, such as a hardware security module (HSM) or isolated cryptographic service\n- When storing keys in offline devices/databases, encrypt the keys using Key Encryption Keys (KEKs) prior to export\n- KEK length and algorithm should be equivalent to or greater in strength than the keys being protected\n- Ensure that standard application level code never reads or uses cryptographic keys directly\n\n### Key Usage and Separation\n\nAccording to NIST, in general, a single key should be used for only one purpose (e.g., encryption, authentication, key wrapping, random number generation, or digital signatures).\n\nReasons for key separation:\n- The use of the same key for two different cryptographic processes may weaken the security provided\n- Limiting the use of a key limits the damage that could be done if the key is compromised\n- Some uses of keys interfere with each other\n\n### Memory Management\n\nKeys stored in memory for a long time can become \"burned in\". This can be mitigated by splitting the key into components that are frequently updated.\n\nPlan for the recovery from possible corruption of the memory media necessary for key or certificate generation, registration, and distribution systems.\n\n### Key Backup and Recovery\n\nData that has been encrypted with lost cryptographic keys will never be recovered. Therefore, it is essential that applications incorporate a secure key backup capability.\n\nWhen backing up keys, ensure that the database used to store the keys is encrypted using at least a FIPS 140-2 validated module.\n\nNever escrow keys used for performing digital signatures, but consider the need to escrow keys that support encryption.\n\n### Trust Store Security\n\n- Design controls to secure the trust store against injection of third-party root certificates\n- Implement integrity controls on objects stored in the trust store\n- Do not allow for export of keys held within the trust store without authentication and authorization\n- Setup strict policies and procedures for exporting key material\n- Implement a secure process for updating the trust store\n\n### Key Security Requirements\n\n- Generate keys only in FIPS 140-2 validated modules or HSMs\n- Use NIST-approved algorithms with appropriate key lengths\n- Never reuse keys for different cryptographic purposes\n- Implement key rotation and lifecycle management\n- Use ephemeral keys for Perfect Forward Secrecy\n- Monitor and audit all key access operations\n- Use only reputable crypto libraries that are well maintained and validated by third-party organizations"
  },
  {
    "path": "sources/owasp/codeguard-0-kubernetes-security.md",
    "content": "---\ndescription: Kubernetes Security Best Practices\nlanguages:\n- javascript\n- shell\n- yaml\nalwaysApply: false\n---\n\n## Kubernetes Security Guidelines\n\nEssential security practices for secure Kubernetes cluster deployment and management.\n\n### Host and Component Security\n\nKeep Kubernetes components updated to the latest stable version. The Kubernetes project maintains release branches for the most recent three minor releases with security fixes.\n\nSecure critical components:\n- Restrict access to etcd with mutual TLS authentication and firewall isolation\n- Use strong credentials between API servers and etcd\n- Control network access to sensitive ports (6443 for API server, 2379-2380 for etcd)\n- Enable Kubelet authentication and authorization to prevent unauthenticated access\n\n### Build Phase Security\n\nUse approved, scanned container images from trusted registries:\n- Store approved images in private registries\n- Integrate vulnerability scanning into CI pipeline to block vulnerable images\n- Use minimal base images (distroless when possible) to reduce attack surface\n- Remove shells and package managers from runtime containers\n\n### Deploy Phase Security\n\n#### Pod Security Configuration\n\nApply security context to control pod security parameters:\n\n```yaml\napiVersion: v1\nkind: Pod\nmetadata:\n  name: hello-world\nspec:\n  containers:\n  # specification of the pod's containers\n  # ...\n  # Security Context\n  securityContext:\n    readOnlyRootFilesystem: true\n    runAsNonRoot: true\n```\n\n#### Pod Security Standards\n\nUse Pod Security Admission Controller with namespace-level enforcement:\n\n```yaml\napiVersion: v1\nkind: Namespace\nmetadata:\n  name: policy-test\n  labels:    \n    pod-security.kubernetes.io/enforce: restricted\n    pod-security.kubernetes.io/audit: restricted\n    pod-security.kubernetes.io/warn: restricted\n```\n\nThree security profiles available:\n- **Privileged**: Unrestricted (system workloads only)\n- **Baseline**: Minimally restrictive, prevents known privilege escalations\n- **Restricted**: Most restrictive, enforces current pod hardening practices\n\n#### Network Security\n\nImplement network policies to control pod-to-pod communication:\n\n```json\nPOST /apis/net.alpha.kubernetes.io/v1alpha1/namespaces/tenant-a/networkpolicys\n{\n  \"kind\": \"NetworkPolicy\",\n  \"metadata\": {\n    \"name\": \"pol1\"\n  },\n  \"spec\": {\n    \"allowIncoming\": {\n      \"from\": [{\n        \"pods\": { \"segment\": \"frontend\" }\n      }],\n      \"toPorts\": [{\n        \"port\": 80,\n        \"protocol\": \"TCP\"\n      }]\n    },\n    \"podSelector\": {\n      \"segment\": \"backend\"\n    }\n  }\n}\n```\n\n#### Resource Management\n\nDefine resource quotas to prevent DoS attacks:\n\n```yaml\napiVersion: v1\nkind: ResourceQuota\nmetadata:\n  name: compute-resources\nspec:\n  hard:\n    pods: \"4\"\n    requests.cpu: \"1\"\n    requests.memory: 1Gi\n    limits.cpu: \"2\"\n    limits.memory: 2Gi\n```\n\n### Secrets Management\n\n- Mount secrets as read-only volumes rather than environment variables\n- Store secrets separately from images and pods\n- Enable encryption at rest for Secret resources in etcd\n- Consider external secrets managers for multi-cluster environments\n\n### Runtime Phase Security\n\n#### Monitoring and Detection\n\nMonitor container behavior for security anomalies:\n- Shell execution inside containers\n- Sensitive file access (e.g., /etc/shadow)\n- Unexpected network connections\n- Process activity deviations between replicas\n\n#### Audit Logging\n\nEnable Kubernetes audit logging to track API requests:\n\n```json\n{\n  \"kind\":\"Event\",\n  \"apiVersion\":\"audit.k8s.io/v1beta1\",\n  \"metadata\":{ \"creationTimestamp\":\"2019-08-22T12:00:00Z\" },\n  \"level\":\"Metadata\",\n  \"timestamp\":\"2019-08-22T12:00:00Z\",\n  \"auditID\":\"23bc44ds-2452-242g-fsf2-4242fe3ggfes\",\n  \"stage\":\"RequestReceived\",\n  \"requestURI\":\"/api/v1/namespaces/default/persistentvolumeclaims\",\n  \"verb\":\"list\",\n  \"user\": {\n    \"username\":\"user@example.org\",\n    \"groups\":[ \"system:authenticated\" ]\n  },\n  \"sourceIPs\":[ \"172.12.56.1\" ]\n}\n```\n\n### Access Control\n\n- Implement RBAC with least privilege principles\n- Use external authentication (OIDC) with multi-factor authentication\n- Avoid built-in authentication methods for production clusters\n- Secure the Kubernetes Dashboard with authenticating reverse proxy\n\n### Key Security Requirements\n\n- Always run the latest stable Kubernetes version\n- Use namespaces to isolate workloads\n- Apply security contexts to prevent privileged container execution\n- Implement network policies for traffic segmentation\n- Enable comprehensive audit logging and monitoring\n- Rotate credentials frequently and revoke bootstrap tokens promptly\n- Use admission controllers to enforce security policies"
  },
  {
    "path": "sources/owasp/codeguard-0-laravel.md",
    "content": "---\ndescription: Laravel Security Best Practices\nlanguages:\n- c\n- javascript\n- php\n- typescript\n- yaml\nalwaysApply: false\n---\n\n## Laravel Security Guidelines\n\nEssential security practices for building secure Laravel applications.\n\n### Basic Configuration\n\nDisable debug mode in production and generate application key:\n\n```ini\nAPP_DEBUG=false\n```\n\n```bash\nphp artisan key:generate\n```\n\nSet secure file permissions: directories `775`, files `664`, executables `775`.\n\n### Cookie Security and Session Management\n\nEnable cookie encryption middleware in `App\\Http\\Kernel`:\n\n```php\nprotected $middlewareGroups = [\n    'web' => [\n        \\App\\Http\\Middleware\\EncryptCookies::class,\n        ...\n    ],\n    ...\n];\n```\n\nConfigure secure session settings in `config/session.php`:\n\n```php\n'http_only' => true,\n'domain' => null,\n'same_site' => 'lax',\n'secure' => null,\n'lifetime' => 15,\n```\n\n### Mass Assignment Protection\n\nProtect against mass assignment vulnerabilities:\n\n```php\n// VULNERABLE: Allows modification of any field\nRoute::any('/profile', function (Request $request) {\n    $request->user()->forceFill($request->all())->save();\n    return response()->json(compact('user'));\n})->middleware('auth');\n```\n\nUse `$request->only()` or `$request->validated()` instead of `$request->all()`.\n\n### SQL Injection Prevention\n\nUse Eloquent ORM parameterized queries by default. For raw queries, always use bindings:\n\n```php\n// VULNERABLE: No SQL bindings\nUser::whereRaw('email = \"'.$request->input('email').'\"')->get();\n\n// SAFE: Using SQL bindings\nUser::whereRaw('email = ?', [$request->input('email')])->get();\n\n// SAFE: Named bindings\nUser::whereRaw('email = :email', ['email' => $request->input('email')])->get();\n```\n\nValidate column names to prevent column name injection:\n\n```php\n$request->validate(['sortBy' => 'in:price,updated_at']);\nUser::query()->orderBy($request->validated()['sortBy'])->get();\n```\n\n### XSS Prevention\n\nUse Blade's automatic escaping for all untrusted data:\n\n```blade\n{{-- SAFE: Automatically escaped --}}\n{{ request()->input('somedata') }}\n\n{{-- VULNERABLE: Unescaped data --}}\n{!! request()->input('somedata') !!}\n```\n\n### File Upload Security\n\nAlways validate file type and size:\n\n```php\n$request->validate([\n    'photo' => 'file|size:100|mimes:jpg,bmp,png'\n]);\n```\n\nSanitize filenames to prevent path traversal:\n\n```php\nRoute::post('/upload', function (Request $request) {\n    $request->file('file')->storeAs(auth()->id(), basename($request->input('filename')));\n    return back();\n});\n```\n\n### Path Traversal Prevention\n\nUse `basename()` to strip directory information:\n\n```php\nRoute::get('/download', function(Request $request) {\n    return response()->download(storage_path('content/').basename($request->input('filename')));\n});\n```\n\n### CSRF Protection\n\nEnable CSRF middleware and use tokens in forms:\n\n```php\nprotected $middlewareGroups = [\n    'web' => [\n        ...\n         \\App\\Http\\Middleware\\VerifyCsrfToken::class,\n         ...\n    ],\n];\n```\n\n```html\n<form method=\"POST\" action=\"/profile\">\n    @csrf\n    <input type=\"hidden\" name=\"_token\" value=\"{{ csrf_token() }}\" />\n</form>\n```\n\n### Command Injection Prevention\n\nEscape shell commands with user input:\n\n```php\npublic function verifyDomain(Request $request)\n{\n    exec('whois '.$request->input('domain'));\n}\n```\n\nUse `escapeshellcmd` and `escapeshellarg` for proper escaping.\n\n### Rate Limiting\n\nApply throttling to protect against abuse:\n\n```php\nRoute::get('/profile', function () {\n    return 'User profile';\n})->middleware('throttle:10,1'); // 10 requests per minute\n\n// Custom rate limiter\nRateLimiter::for('custom-limit', function ($request) {\n    return Limit::perMinute(5)->by($request->user()?->id ?: $request->ip());\n});\n```\n\n### Other Injection Prevention\n\nAvoid dangerous functions with untrusted input:\n\n```php\nunserialize($request->input('data'));\neval($request->input('data'));\nextract($request->all());\n```\n\n### Security Auditing\n\nUse Enlightn Security Checker to scan for vulnerabilities and keep dependencies updated."
  },
  {
    "path": "sources/owasp/codeguard-0-ldap-injection-prevention.md",
    "content": "---\ndescription: LDAP Injection Prevention\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\n- xml\n- yaml\nalwaysApply: false\n---\n\n## LDAP Injection Prevention Guidelines\n\nEssential practices for preventing LDAP injection vulnerabilities in applications that use directory services.\n\n### Understanding LDAP Injection\n\nLDAP injection occurs when untrusted user input is improperly incorporated into LDAP queries, potentially allowing attackers to bypass authentication, access unauthorized data, or modify directory information.\n\nTwo main components vulnerable to injection:\n- **Distinguished Names (DNs)**: Unique identifiers like `cn=Richard Feynman, ou=Physics Department, dc=Caltech, dc=edu`\n- **Search Filters**: Query criteria using boolean logic in Polish notation\n\n### Primary Defense: Proper Escaping\n\n#### Distinguished Name Escaping\n\nCharacters that must be escaped in DNs: `\\ # + < > , ; \" =` and leading or trailing spaces.\n\nCharacters allowed in DNs (no escaping needed): `* ( ) . & - _ [ ] ` ~ | @ $ % ^ ? : { } ! '`\n\n#### Search Filter Escaping\n\nCharacters that must be escaped in search filters: `* ( ) \\ NUL`\n\n### Safe Java Example\n\nThe original OWASP document provides this allowlist validation approach:\n\n```java\n// String userSN = \"Sherlock Holmes\"; // Valid\n// String userPassword = \"secret2\"; // Valid\n// ... beginning of LDAPInjection.searchRecord()...\nsc.setSearchScope(SearchControls.SUBTREE_SCOPE);\nString base = \"dc=example,dc=com\";\n\nif (!userSN.matches(\"[\\\\w\\\\s]*\") || !userPassword.matches(\"[\\\\w]*\")) {\n throw new IllegalArgumentException(\"Invalid input\");\n}\n\nString filter = \"(&(sn = \" + userSN + \")(userPassword=\" + userPassword + \"))\";\n// ... remainder of LDAPInjection.searchRecord()... \n```\n\n### Safe .NET Encoding\n\nUse .NET AntiXSS (now the Encoder class) LDAP encoding functions:\n- `Encoder.LdapFilterEncode(string)` - encodes according to RFC4515\n- `Encoder.LdapDistinguishedNameEncode(string)` - encodes according to RFC2253\n- `LdapDistinguishedNameEncode(string, bool, bool)` - with optional initial/final character escaping\n\n### Framework-Based Protection\n\nUse frameworks that automatically protect from LDAP injection:\n- **Java**: OWASP ESAPI with `encodeForLDAP(String)` and `encodeForDN(String)`\n- **.NET**: LINQ to LDAP (for .NET Framework 4.5 or lower) provides automatic LDAP encoding\n\n### Additional Defenses\n\n#### Least Privilege\n- Minimize privileges assigned to LDAP binding accounts\n- Use read-only accounts where possible\n- Avoid administrative accounts for application connections\n\n#### Bind Authentication\n- Configure LDAP with bind authentication to add verification and authorization checks\n- Prevent anonymous connections and unauthenticated binds\n\n#### Allow-List Input Validation\n- Validate input against known-safe characters before LDAP query construction\n- Normalize user input before validation\n- Store sensitive data in sanitized form\n\n### Key Security Requirements\n\n- Always escape untrusted data before incorporating into LDAP queries\n- Use context-appropriate escaping (DN vs search filter)\n- Implement comprehensive input validation with allowlists\n- Use established security libraries rather than custom escaping\n- Apply principle of least privilege to LDAP connections\n- Enable proper authentication mechanisms to prevent bypass attacks"
  },
  {
    "path": "sources/owasp/codeguard-0-legacy-application-management.md",
    "content": "---\ndescription: Legacy Application Management Security\nlanguages:\n- c\n- java\n- javascript\n- perl\n- php\n- python\n- ruby\n- yaml\nalwaysApply: false\n---\n\n## Legacy Application Management Guidelines\n\nEssential security practices for managing legacy applications that remain in active use despite being outdated.\n\n### Understanding Legacy Application Risks\n\nLegacy applications introduce significant security risks because they:\n- May have reached End-of-Life (EoL) with no vendor support or patches\n- Use outdated technologies with limited expertise available\n- Produce data in custom formats incompatible with modern security tools\n- Often have accumulated security vulnerabilities over time\n\n### Inventory and Asset Management\n\n**Inventory Management**: Compile comprehensive documentation identifying legacy applications including:\n- Version numbers, production dates, and configuration settings\n- Network hosts and infrastructure dependencies\n- Services running on hosting infrastructure\n- Physical location and access permissions for servers\n- Software Bill of Materials (SBOM) for applications with third-party dependencies\n\n**Risk Assessment**: Use industry standard frameworks like NIST Risk Management Framework for formal assessment. Consider these key questions:\n- What information is handled/stored and what would be the impact if compromised?\n- Do the application/dependencies/infrastructure have known vulnerabilities?\n- How critical is application availability to business continuity?\n- Could an attacker use this application to access other critical systems?\n\n### Authentication and Authorization\n\nApply the principle of least privilege with enhanced restrictions for legacy systems:\n\n**Network-Level Controls**:\n- Host applications within restricted subnets\n- Apply IP allow-listing to prevent arbitrary access\n- Consider air-gapped environments for high-risk applications\n- Close unnecessary ports on application hosts\n- Use firewall rules to restrict port access\n\n**Access Controls**:\n- Reduce feature sets available to end users\n- Disable high-risk administrative functionalities\n- Require authentication via Identity Provider (IdP) services\n- Implement VPN access requirements for network environments\n- Develop intermediary services/APIs to avoid direct user access to legacy applications\n\n### Vulnerability Management\n\n**Vulnerability Scanning**: Conduct regular automated scanning using industry standard tools:\n- Use tools like Nessus and Qualys on scheduled intervals\n- Apply Static Application Security Testing (SAST) for code vulnerabilities\n- Use Software Composition Analysis (SCA) for dependency vulnerabilities\n- Perform manual assessment when automated tools aren't viable\n\n**Patch Management**: \n- Prioritize patches based on vulnerability severity and CVE status\n- Focus on vulnerabilities with publicly listed exploits\n- Apply additional access restrictions when patching isn't possible\n\n### Data Storage Security\n\nEnsure data protection through encryption:\n- Encrypt data at rest (database storage)\n- Encrypt data in transit with secure protocols\n- Apply most restrictive network access controls for applications limited to plain text protocols\n- Consider temporary or permanent air-gapping when necessary\n\n### Ensuring Maintainability\n\nMaintain institutional expertise for business continuity:\n- Train multiple staff members on legacy application troubleshooting\n- Document processes and troubleshooting guides for common failures\n- Develop expertise in legacy programming languages within the organization\n- Create knowledge transfer programs for new team members\n\n### Change Management\n\nPlan staged migration to modern solutions considering:\n- Budget allocation and timeline for upgrading solutions\n- Required expertise for migration (internal development or acquisition)\n- Migration urgency based on risk profile and organizational risk appetite\n\nInclude in change management plans:\n- Granular steps toward migration with explicit completion dates\n- Clear business and security case for change\n- Extensive consultation with existing solution stakeholders\n\n### Continuous Monitoring and Incident Response\n\nImplement enhanced security monitoring with rapid response capabilities:\n\n**Monitoring Solutions**:\n- Develop custom APIs to convert legacy application data for modern security tools\n- Use automation scripts for compromise indicator reports when APIs aren't possible\n- Monitor for anomalous network traffic and activity surges\n\n**Incident Response**:\n- Prioritize incident response for critical legacy systems\n- Document incident response playbooks with emergency procedures\n- Include escalation contacts and incident response leader details\n- Integrate incident response with broader business continuity planning\n\nLegacy applications require heightened security measures due to their inherent risks, but with proper controls and planning, organizations can manage these risks while working toward modernization."
  },
  {
    "path": "sources/owasp/codeguard-0-logging-vocabulary.md",
    "content": "---\ndescription: Standardized Security Event Logging Vocabulary\nlanguages:\n- c\n- go\n- java\n- javascript\n- kotlin\n- php\n- python\n- ruby\n- swift\n- typescript\nalwaysApply: false\n---\n\n## Security Event Logging Vocabulary Guidelines\n\nStandardized vocabulary for logging security events to improve monitoring, alerting, and incident response.\n\n### Standard Event Format\n\nNOTE: All dates should be logged in ISO 8601 format WITH UTC offset to ensure maximum portability\n\n```json\n{\n    \"datetime\": \"2021-01-01T01:01:01-0700\",\n    \"appid\": \"foobar.netportal_auth\",\n    \"event\": \"AUTHN_login_success:joebob1\",\n    \"level\": \"INFO\",\n    \"description\": \"User joebob1 login successfully\",\n    \"useragent\": \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36\",\n    \"source_ip\": \"165.225.50.94\",\n    \"host_ip\": \"10.12.7.9\",\n    \"hostname\": \"portalauth.foobar.com\",\n    \"protocol\": \"https\",\n    \"port\": \"440\",\n    \"request_uri\": \"/api/v2/auth/\",\n    \"request_method\": \"POST\",\n    \"region\": \"AWS-US-WEST-2\",\n    \"geo\": \"USA\"\n}\n```\n\n### Authentication Events [AUTHN]\n\nauthn_login_success[:userid] - Successful login (Level: INFO)\nauthn_login_successafterfail[:userid,retries] - Successful login after previous failures (Level: INFO)\nauthn_login_fail[:userid] - Failed login attempt (Level: WARN)\nauthn_login_fail_max[:userid,maxlimit] - Maximum failures reached (Level: WARN)\nauthn_login_lock[:userid,reason] - Account locked (reasons: maxretries, suspicious, customer, other) (Level: WARN)\nauthn_password_change[:userid] - Password successfully changed (Level: INFO)\nauthn_password_change_fail[:userid] - Password change failed (Level: CRITICAL)\nauthn_impossible_travel[:userid,region1,region2] - User in distant locations simultaneously (Level: CRITICAL)\nauthn_token_created[:userid,entitlements] - Service token created (Level: INFO)\nauthn_token_revoked[:userid,tokenid] - Token revoked (Level: INFO)\nauthn_token_reuse[:userid,tokenid] - Revoked token reuse attempt (Level: CRITICAL)\nauthn_token_delete[:appid] - Token deleted (Level: WARN)\n\n### Authorization Events [AUTHZ]\n\nauthz_fail[:userid,resource] - Unauthorized access attempt (Level: CRITICAL)\nauthz_change[:userid,from,to] - User entitlements changed (Level: WARN)\nauthz_admin[:userid,event] - All privileged user activity (Level: WARN)\n\n### Encryption/Decryption Events [CRYPT]\n\ncrypt_decrypt_fail[userid] - Decryption failure (Level: WARN)\ncrypt_encrypt_fail[userid] - Encryption failure (Level: WARN)\n\n### Excessive Use Events [EXCESS]\n\nexcess_rate_limit_exceeded[userid,max] - Rate limit exceeded (Level: WARN)\n\n### File Upload Events [UPLOAD]\n\nupload_complete[userid,filename,type] - File upload completed (Level: INFO)\nupload_stored[filename,from,to] - File stored with new name/location (Level: INFO)\nupload_validation[filename,(virusscan|imagemagick|...):(FAILED|incomplete|passed)] - File validation results (Level: INFO|CRITICAL)\nupload_delete[userid,fileid] - File deleted (Level: INFO)\n\n### Input Validation Events [INPUT]\n\ninput_validation_fail:[(fieldone,fieldtwo...),userid] - Server-side validation failure (Level: WARN)\n\n### Malicious Behavior Events [MALICIOUS]\n\nmalicious_excess_404:[userid|IP,useragent] - Excessive 404s indicating force-browsing (Level: WARN)\nmalicious_extraneous:[userid|IP,inputname,useragent] - Unexpected input data submitted (Level: CRITICAL)\nmalicious_attack_tool:[userid|IP,toolname,useragent] - Known attack tools detected (Level: CRITICAL)\nmalicious_cors:[userid|IP,useragent,referer] - Illegal cross-origin request (Level: CRITICAL)\nmalicious_direct_reference:[userid|IP,useragent] - Direct object reference attempt (Level: CRITICAL)\n\n### Privilege Changes Events [PRIVILEGE]\n\nprivilege_permissions_changed:[userid,file|object,fromlevel,tolevel] - Object permissions changed (Level: WARN)\n\n### Sensitive Data Events [DATA]\n\nsensitive_create:[userid,file|object] - Sensitive data created (Level: WARN)\nsensitive_read:[userid,file|object] - Sensitive data accessed (Level: WARN)\nsensitive_update:[userid,file|object] - Sensitive data modified (Level: WARN)\nsensitive_delete:[userid,file|object] - Sensitive data marked for deletion (Level: WARN)\n\n### Sequence Errors Events [SEQUENCE]\n\nsequence_fail:[userid] - Business logic flow bypassed (Level: CRITICAL)\n\n### Session Management Events [SESSION]\n\nsession_created:[userid] - New authenticated session (Level: INFO)\nsession_renewed:[userid] - Session extended after expiry warning (Level: INFO)\nsession_expired:[userid,reason] - Session expired (reasons: logout, timeout, revoked) (Level: INFO)\nsession_use_after_expire:[userid] - Expired session use attempt (Level: CRITICAL)\n\n### System Events [SYS]\n\nsys_startup:[userid] - System started (Level: WARN)\nsys_shutdown:[userid] - System shut down (Level: WARN)\nsys_restart:[userid] - System restarted (Level: WARN)\nsys_crash[:reason] - System crash (Level: WARN)\nsys_monitor_disabled:[userid,monitor] - Security monitoring disabled (Level: WARN)\nsys_monitor_enabled:[userid,monitor] - Security monitoring enabled (Level: WARN)\n\n### User Management Events [USER]\n\nuser_created:[userid,newuserid,attributes[one,two,three]] - New user created (Level: WARN)\nuser_updated:[userid,onuserid,attributes[one,two,three]] - User account updated (Level: WARN)\nuser_archived:[userid,onuserid] - User account archived (Level: WARN)\nuser_deleted:[userid,onuserid] - User account deleted (Level: WARN)\n\n### Data Exclusions\n\nNever log sensitive information: private or secret information, source code, keys, certificates, authentication passwords, session identification values, access tokens, sensitive personal data, PII, database connection strings, encryption keys, bank account or payment card data, commercially-sensitive information.\n\n### Implementation Requirements\n\n- Use ISO 8601 format with UTC offset for all timestamps\n- Include application identifier (appid) for correlation  \n- Apply consistent severity levels (INFO, WARN, CRITICAL)\n- Include relevant context (IP addresses, user agents, request details)\n- Consider data privacy regulations when logging user information\n- Fields logged after event type should be considered optional based on business needs and data stewardship responsibilities"
  },
  {
    "path": "sources/owasp/codeguard-0-mass-assignment.md",
    "content": "---\ndescription: Mass Assignment Prevention\nlanguages:\n- c\n- java\n- javascript\n- php\n- ruby\n- scala\nalwaysApply: false\n---\n\n## Mass Assignment Prevention Guidelines\n\nEssential practices for preventing mass assignment vulnerabilities that allow attackers to modify unintended object properties.\n\n### Understanding Mass Assignment\n\nMass assignment occurs when frameworks automatically bind HTTP request parameters to program variables or objects. Attackers can exploit this by creating new parameters to overwrite sensitive fields like `isAdmin` or other privilege-related properties.\n\n**Alternative Names by Framework:**\n- Mass Assignment: Ruby on Rails, NodeJS\n- Autobinding: Spring MVC, ASP NET MVC  \n- Object injection: PHP\n\n### Vulnerable Example\n\nUser form with typical fields:\n```html\n<form>\n     <input name=\"userid\" type=\"text\">\n     <input name=\"password\" type=\"text\">\n     <input name=\"email\" text=\"text\">\n     <input type=\"submit\">\n</form>\n```\n\nUser object with sensitive field:\n```java\npublic class User {\n   private String userid;\n   private String password;\n   private String email;\n   private boolean isAdmin;\n   //Getters & Setters\n}\n```\n\nVulnerable controller with automatic binding:\n```java\n@RequestMapping(value = \"/addUser\", method = RequestMethod.POST)\npublic String submit(User user) {\n   userService.add(user);\n   return \"successPage\";\n}\n```\n\nAttack payload:\n```text\nPOST /addUser\nuserid=bobbytables&password=hashedpass&email=bobby@tables.com&isAdmin=true\n```\n\n### Primary Defense Strategies\n\n**1. Use Data Transfer Objects (DTOs)**\nCreate objects exposing only safe, editable fields:\n\n```java\npublic class UserRegistrationFormDTO {\n private String userid;\n private String password;\n private String email;\n //NOTE: isAdmin field is not present\n //Getters & Setters\n}\n```\n\n**2. Allow-list Approach**\nExplicitly define permitted fields for binding.\n\n**3. Block-list Approach**  \nExplicitly exclude sensitive fields from binding.\n\n### Framework-Specific Implementations\n\n#### Spring MVC\n\nAllow-listing permitted fields:\n```java\n@Controller\npublic class UserController {\n    @InitBinder\n    public void initBinder(WebDataBinder binder, WebRequest request) {\n        binder.setAllowedFields([\"userid\",\"password\",\"email\"]);\n    }\n}\n```\n\nBlock-listing sensitive fields:\n```java\n@Controller\npublic class UserController {\n   @InitBinder\n   public void initBinder(WebDataBinder binder, WebRequest request) {\n      binder.setDisallowedFields([\"isAdmin\"]);\n   }\n}\n```\n\n#### NodeJS + Mongoose\n\nAllow-listing with underscore.js:\n```javascript\nvar UserSchema = new mongoose.Schema({\n    userid: String,\n    password: String,\n    email : String,\n    isAdmin : Boolean,\n});\n\nUserSchema.statics = {\n    User.userCreateSafeFields: ['userid', 'password', 'email']\n};\n\nvar User = mongoose.model('User', UserSchema);\n\n_ = require('underscore');\nvar user = new User(_.pick(req.body, User.userCreateSafeFields));\n```\n\nBlock-listing with mongoose-mass-assign plugin:\n```javascript\nvar massAssign = require('mongoose-mass-assign');\n\nvar UserSchema = new mongoose.Schema({\n    userid: String,\n    password: String,\n    email : String,\n    isAdmin : { type: Boolean, protect: true, default: false }\n});\n\nUserSchema.plugin(massAssign);\nvar User = mongoose.model('User', UserSchema);\n\nvar user = User.massAssign(req.body);\n```\n\n#### PHP Laravel + Eloquent\n\nAllow-listing with $fillable:\n```php\n<?php\nnamespace App;\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass User extends Model {\n    private $userid;\n    private $password;\n    private $email;\n    private $isAdmin;\n\n    protected $fillable = array('userid','password','email');\n}\n```\n\nBlock-listing with $guarded:\n```php\n<?php\nnamespace App;\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass User extends Model {\n    private $userid;\n    private $password;\n    private $email;\n    private $isAdmin;\n\n    protected $guarded = array('isAdmin');\n}\n```\n\n### Exploitability Conditions\n\nMass assignment becomes exploitable when:\n- Attacker can guess common sensitive fields\n- Attacker has access to source code to review models\n- The target object has an empty constructor\n\n### Key Prevention Principles\n\n1. Never bind user input directly to domain objects with sensitive fields\n2. Use DTOs to expose only safe, editable fields\n3. Apply framework-specific allow-listing or block-listing mechanisms\n4. Regularly review models for sensitive attributes\n5. Prefer allow-listing over block-listing when possible\n\nMass assignment protection is critical to prevent unauthorized privilege escalation and data manipulation attacks."
  },
  {
    "path": "sources/owasp/codeguard-0-microservices-security.md",
    "content": "---\ndescription: Microservices Security Best Practices\nlanguages:\n- c\n- go\n- java\n- javascript\n- python\n- ruby\n- typescript\n- yaml\nalwaysApply: false\n---\n\n## Microservices Security Guidelines\n\nEssential security practices for implementing authentication, authorization, and logging in microservices-based systems.\n\n### Edge-Level Authorization\n\nAPI gateways can centralize authorization enforcement for downstream microservices, but have limitations:\n- Pushing all authorization decisions to the gateway becomes hard to manage in complex ecosystems\n- The API gateway may become a single point of decision violating \"defense in depth\"\n- Operation teams typically own gateways, slowing development velocity\n\n**Recommendation**: Implement authorization at both edge level (coarse-grained) and service level (fine-grained).\n\n### Service-Level Authorization Patterns\n\n**NIST Components**:\n- Policy Administration Point (PAP): User interface for creating and managing access control rules\n- Policy Decision Point (PDP): Computes access decisions by evaluating access control policy\n- Policy Enforcement Point (PEP): Enforces policy decisions for protected objects\n- Policy Information Point (PIP): Retrieval source of attributes for policy evaluation\n\n#### Centralized Pattern with Embedded Policy Decision Point\n\n**Recommended approach**: Access control rules defined centrally but stored and evaluated at microservice level.\n\n1. Access control rules defined using PAP and delivered to embedded PDP with required attributes\n2. When subject invokes microservice endpoint, microservice code invokes the PDP \n3. PDP generates access control policy decision by evaluating input against rules and attributes\n4. Microservice enforces authorization based on PDP decision\n\n**Implementation**: PDP implemented as microservice built-in library or sidecar in service mesh architecture.\n\n### Authorization Implementation Recommendations\n\n1. Use special language to express policy instead of hardcoding in source code\n2. Implement as platform-level solution managed by dedicated security team\n3. Use widely-adopted solutions rather than custom implementations\n4. Implement \"defense in depth\" principle:\n   - Gateway/proxy level: coarse-grained authorization\n   - Microservice level: fine-grained decisions using shared authorization components\n   - Business code level: business-specific access control rules\n5. Implement formal procedures for access control policy development, approval, and rollout\n\n### External Entity Identity Propagation\n\n**Problem**: Internal services need caller context for fine-grained authorization decisions.\n\n**Anti-pattern**: Reusing external access tokens internally (insecure due to token leakage risk).\n\n#### Recommended Pattern: Signed Data Structure by Trusted Issuer\n\nAfter edge authentication, generate data structure representing external entity identity (user ID, roles, permissions), signed or encrypted by trusted issuer and propagated to internal microservices.\n\n**Implementation Recommendations**:\n1. Decouple external access tokens from internal representation\n2. Use single data structure to represent and propagate external entity identity\n3. Sign internal entity representation structure (symmetric or asymmetric encryption)\n4. Make internal structure extensible for additional claims\n5. Never expose internal entity representation structure outside trusted boundary\n\n### Service-to-Service Authentication\n\n#### Mutual Transport Layer Security (mTLS)\n\nEach microservice carries public/private key pair for authentication via mTLS. Provides:\n- Legitimate service identification\n- Confidentiality and integrity of transmitted data\n\n**Challenges**: Key provisioning, trust bootstrap, certificate revocation, key rotation.\n\n#### Token-Based Authentication\n\nToken contains caller ID (microservice ID) and permissions (scopes). Process:\n1. Caller microservice obtains signed token from security token service using service ID and password\n2. Token attached to outgoing requests via HTTP headers\n3. Called microservice extracts and validates token online or offline\n\n**Online validation**: Network call to centralized service (detects revoked tokens, high latency, for critical requests)\n**Offline validation**: Uses downloaded public key (may not detect revoked tokens, low latency, for non-critical requests)\n\n### Logging Architecture\n\n**Principles**:\n- Each microservice writes log messages to local file using standard output\n- Logging agent periodically pulls log messages and publishes to message broker\n- Central logging service subscribes to message broker messages\n\n**Key Requirements**:\n\n1. **Local File Logging**: Microservices write to local files, not directly to central logging system\n2. **Dedicated Logging Agent**: Decoupled component deployed on same host as microservice\n3. **Asynchronous Pattern**: Message broker implements asynchronous connection between logging agent and central service\n4. **Mutual Authentication**: Logging agent and message broker use mutual authentication (TLS)\n5. **Access Control**: Message broker enforces access control policy with least privileges\n6. **Data Sanitization**: Filter/sanitize log messages to exclude sensitive data (PII, passwords, API keys)\n7. **Correlation ID**: Generate unique correlation ID for every call chain to group log messages\n8. **Health Monitoring**: Logging agent provides health and status data\n9. **Structured Format**: Publish log messages in structured format (JSON, CSV)\n10. **Context Data**: Append platform context (hostname, container name) and runtime context (class name, filename)\n\n### Security Architecture Documentation\n\nEssential documentation to support threat modeling, attack surface analysis, and least privilege enforcement:\n\n#### Service and Infrastructure Inventory\n- Document all application services and infrastructure components with unique IDs, business functions, API definitions including security schemes (scopes, API keys), source repositories, and team ownership\n- Include authentication, authorization, logging, monitoring, and discovery services\n- Document data storages (databases, caches) and message queues with software types\n\n#### Data Classification and Flow Mapping\n- Identify and classify all data assets by protection level (PII, confidential, public)\n- Map service-to-storage relationships with access types (read, read/write)\n- Document service-to-service communications (synchronous HTTP/gRPC, asynchronous messaging) with data exchanged\n- Track which assets are stored in which systems (golden source vs cache)\n\n#### Architecture Visualization\n- Create graphical representations using service call graphs or data flow diagrams\n- Use tools like DOT language to visualize component relationships and trust boundaries\n- Maintain current architecture diagrams showing all connections and data flows\n\n#### Security Applications\nThis documentation enables:\n- Attack surface enumeration from API definitions for focused security testing\n- Data leakage analysis by tracking sensitive data movement across service boundaries  \n- Least privilege implementation by defining minimal permissions based on documented interactions\n- Trust boundary validation and justification of all service communications\n- Centralized security control verification to avoid duplicate or missing protections\n\n### Security Best Practices Summary\n\n- Implement defense-in-depth authorization at multiple layers\n- Use centralized policy management with embedded decision points\n- Propagate signed internal identity tokens, never external tokens\n- Choose mTLS for service authentication when possible, token-based for flexibility\n- Implement resilient, secure logging with proper data sanitization\n- Maintain comprehensive architecture documentation for threat modeling and security analysis\n- Apply formal governance procedures for all security policy changes"
  },
  {
    "path": "sources/owasp/codeguard-0-mobile-application-security.md",
    "content": "---\ndescription: Mobile Application Security Best Practices\nlanguages:\n- java\n- javascript\n- kotlin\n- matlab\n- perl\n- swift\n- xml\nalwaysApply: false\n---\n\n## Mobile Application Security Guidelines\n\nEssential security practices for developing secure mobile applications across iOS and Android platforms.\n\n### Architecture and Design\n\nImplement secure design principles from the start:\n- Follow least privilege and defense in depth principles\n- Use standard secure authentication protocols (OAuth2, JWT)\n- Perform all authentication and authorization checks server-side\n- Request only necessary permissions for app and backend services\n- Establish security controls for app updates, patches, and releases\n- Use only trusted and validated third-party libraries and components\n\n### Authentication and Authorization\n\nNever trust the client for security decisions:\n- Perform authentication/authorization server-side only\n- Do not store user passwords on device; use revocable access tokens\n- Avoid hardcoding credentials in the mobile app\n- Encrypt credentials in transmission\n- Use platform-specific secure storage (iOS Keychain, Android Keystore)\n- Require password complexity and avoid short PINs (4 digits)\n- Implement session timeouts and remote logout functionality\n- Require re-authentication for sensitive operations\n- Use platform-supported biometric authentication with secure fallbacks\n\n### Data Storage and Privacy\n\nProtect sensitive data at rest and in transit:\n- Encrypt sensitive data using platform APIs; avoid custom encryption\n- Leverage hardware-based security features (Secure Enclave, Strongbox)\n- Store private data on device's internal storage only\n- Minimize PII collection to necessity and implement automatic expiration\n- Avoid caching, logging, or background snapshots of sensitive data\n- Always use HTTPS for network communications\n\n### Network Communication\n\nAssume all network communication is insecure:\n- Use HTTPS for all network communication\n- Do not override SSL certificate validation for self-signed certificates\n- Use strong, industry standard cipher suites with appropriate key lengths\n- Use certificates signed by trusted CA providers\n- Consider certificate pinning for additional security\n- Encrypt data even if sent over SSL\n- Avoid sending sensitive data via SMS\n\n### Code Quality and Integrity\n\nMaintain application security throughout development:\n- Use static analysis tools to identify vulnerabilities\n- Make security a focal point during code reviews\n- Keep all libraries up to date to patch known vulnerabilities\n- Disable debugging in production builds\n- Include code to validate integrity of application code\n- Obfuscate the app binary\n- Implement runtime anti-tampering controls:\n  - Check for debugging, hooking, or code injection\n  - Detect emulator or rooted/jailbroken devices\n  - Verify app signatures at runtime\n\n### Platform-Specific Security\n\n#### Android Security\n- Use Android's ProGuard for code obfuscation\n- Avoid storing sensitive data in SharedPreferences\n- Disable backup mode to prevent sensitive data in backups\n- Use Android Keystore with hardware backing (TEE or StrongBox)\n- Implement Google's Play Integrity API for device and app integrity checks\n\n#### iOS Security\n- Configure Shortcuts permissions to require device unlock for sensitive actions\n- Set Siri intent `requiresUserAuthentication` to true for sensitive functionality\n- Implement authentication checks on deep link endpoints\n- Use conditional logic to mask sensitive widget content on lock screen\n- Store sensitive data in iOS Keychain, not plist files\n- Use Secure Enclave for cryptographic key storage\n- Implement App Attest API for app integrity validation\n- Use DeviceCheck API for persistent device state tracking\n\n### Testing and Monitoring\n\nValidate security controls through comprehensive testing:\n- Perform penetration testing including cryptographic vulnerability assessment\n- Leverage automated tests to ensure security features work as expected\n- Ensure security features do not harm usability\n- Use real-time monitoring to detect and respond to threats\n- Have a clear incident response plan in place\n- Plan for regular updates and implement forced update mechanisms when necessary\n\n### Input and Output Validation\n\nPrevent injection and execution attacks:\n- Validate and sanitize all user input\n- Validate and sanitize output to prevent injection attacks\n- Mask sensitive information on UI fields to prevent shoulder surfing\n- Inform users about security-related activities (logins from new devices)\n\nBy following these practices derived from the OWASP Mobile Application Security framework, you can significantly improve the security posture of your mobile applications across both development and operational phases."
  },
  {
    "path": "sources/owasp/codeguard-0-multifactor-authentication.md",
    "content": "---\ndescription: Multifactor Authentication Implementation\nlanguages:\n- c\n- go\n- java\n- javascript\n- kotlin\n- php\n- python\n- ruby\n- swift\n- typescript\nalwaysApply: false\n---\n\n## Multifactor Authentication Guidelines\n\nEssential practices for implementing secure multifactor authentication to protect against account compromise.\n\n### Understanding MFA Factors\n\nTrue MFA requires at least two factors from different categories. Multiple instances of the same factor (password + PIN) does not constitute MFA:\n\n1. Something You Know: Passwords, PINs (avoid security questions - no longer acceptable per NIST SP 800-63)\n2. Something You Have: Hardware tokens, software OTP, U2F tokens, certificates, smart cards \n3. Something You Are: Biometrics (fingerprints, facial recognition, iris scans)\n4. Somewhere You Are: Source IP address, geolocation, geofencing\n5. Something You Do: Behavioral profiling, keystroke dynamics (largely theoretical)\n\n### When to Require MFA\n\nImplement MFA for all critical authentication points:\n- User login (primary requirement)\n- Password changes or security question updates\n- Email address changes associated with the account\n- Disabling MFA\n- Elevating user session to administrative session\n- All API endpoints and mobile application authentication flows\n\n### MFA Method Recommendations\n\n#### Recommended (Most Secure)\n- Passkeys/FIDO2: Combines possession and biometric/PIN factors, resistant to phishing\n- Hardware U2F Tokens: Challenge-response authentication, phishing-resistant\n- Hardware OTP Tokens: Separate physical devices, nearly impossible to compromise remotely\n- Digital Certificates: Resistant to phishing, centrally manageable\n\n#### Acceptable with Caution\n- Software TOTP: Widely supported, cost-effective but vulnerable if device is compromised\n- Smart Cards: Strong when combined with PIN but require PKI infrastructure\n\n#### Avoid for Sensitive Applications\n- SMS/Phone Calls: Susceptible to SIM swapping, should not protect PII or financial data\n- Email Verification: Often same password as application, provides minimal additional security\n- Security Questions: No longer acceptable per NIST SP 800-63\n\n### Implementation Requirements\n\n#### Risk-Based Authentication\nAdjust MFA requirements based on context to improve user experience:\n- Require MFA for new devices or unusual locations\n- Use corporate IP ranges or geolocation as risk signals\n- Consider time of access and device fingerprinting\n- Implement behavioral profiling for continuous authentication\n\nCommon risk signals:\n- Geolocation and IP reputation\n- Device fingerprinting\n- Unusual time of access\n- Known compromised credentials\n\n#### MFA Recovery Mechanisms\nImplement secure recovery without creating bypass opportunities:\n- Provide single-use recovery codes during MFA enrollment\n- Require users to set up multiple MFA types\n- Mail recovery codes to registered physical address\n- Implement rigorous identity verification through support team\n- Use trusted user vouching systems for corporate environments\n\n#### Failed Authentication Handling\nWhen password succeeds but MFA fails:\n- Prompt user to try alternative MFA methods\n- Allow secure MFA reset process\n- Notify user of failed attempt with time, location, and device information\n- Display notification on next successful login\n\n#### Security Controls\n- Rate limit authentication attempts to prevent brute force\n- Implement account lockout after multiple failed MFA attempts\n- Verify MFA state in all authenticated endpoints\n- Avoid \"remember this device\" for highly sensitive applications\n- Log and monitor all MFA-related events\n\n### Factor-Specific Considerations\n\n#### Hardware Tokens\nPros: Extremely difficult to compromise remotely, work without mobile devices\nCons: Expensive deployment, administrative overhead, can be stolen and used without PIN\n\n#### Software TOTP\nPros: Cost-effective, easy deployment, widely supported standards\nCons: Vulnerable if device compromised, may be on same device as authentication\n\n#### Biometrics\nPros: Hard to spoof, fast and convenient\nCons: Privacy concerns, expensive hardware, difficult to change if compromised\n\n#### Location-Based\nPros: Transparent to users, minimal administrative overhead\nCons: No protection against compromised systems or insider threats\n\n### Third-Party MFA Services\n\nConsider using established MFA providers to reduce implementation complexity:\n- Evaluate security practices and certifications of providers\n- Understand implications if third-party service is compromised\n- Ensure service meets compliance requirements for your industry\n\n### Regulatory Compliance\n\nMany industries require MFA implementation:\n- Finance and healthcare sectors commonly mandate MFA\n- GDPR compliance often requires MFA for sensitive data\n- NIST SP 800-63 provides authoritative guidance on authentication factors\n- Verify specific regulatory requirements for your application domain\n\n### Testing and Monitoring\n\n- Conduct penetration testing specifically targeting authentication flows\n- Test MFA bypass scenarios and recovery mechanisms\n- Monitor for unusual authentication patterns and failed attempts\n- Regularly audit MFA implementation against current threats\n- Ensure security features maintain usability to prevent user circumvention\n\nFollowing these guidelines based on the OWASP Multifactor Authentication framework will significantly reduce the risk of account compromise while maintaining reasonable usability for legitimate users."
  },
  {
    "path": "sources/owasp/codeguard-0-network-segmentation.md",
    "content": "---\ndescription: Network Segmentation Security Architecture\nlanguages:\n- c\n- javascript\n- shell\n- yaml\nalwaysApply: false\n---\n\n## Network Segmentation Guidelines\n\nEssential practices for implementing secure network architecture to limit attack surface and prevent lateral movement.\n\n### Purpose of Network Segmentation\n\nNetwork segmentation is the core of multi-layer defense in depth for modern services. Proper segmentation slows down attackers by preventing:\n- SQL injections from providing direct database access\n- Compromised workstations from accessing sensitive systems\n- Lateral movement between organizational servers\n- Access to command and control servers from compromised internal systems\n\n### Three-Layer Security Architecture\n\nImplement a mandatory three-tier architecture with distinct security zones:\n\n#### Frontend Layer\nContains internet-facing components:\n- Load balancers\n- Application layer firewalls (WAF)\n- Web servers\n- Web caches\n\nFrontend systems should only communicate with middleware layer, never directly with backend.\n\n#### Middleware Layer  \nHouses business logic and processing:\n- Web applications implementing system logic\n- Authorization services\n- Analytics services\n- Message queues\n- Stream processing platforms\n\nMiddleware mediates all communication between frontend and backend layers.\n\n#### Backend Layer\nStores sensitive data and critical systems:\n- SQL databases\n- LDAP directories and domain controllers\n- Cryptographic key storage\n- File servers\n\nBackend systems should only accept connections from middleware layer.\n\n### Segmentation Implementation\n\n#### Communication Flow Rules\nEnforce strict unidirectional communication patterns:\n- External users connect only to frontend\n- Frontend communicates only with middleware\n- Middleware communicates with backend when needed\n- No direct frontend-to-backend communication allowed\n\n#### DMZ Configuration\nFrontend layer should include two DMZ segments:\n- DMZ Inbound: Services accessible from internet, protected by WAF\n- DMZ Outbound: Services with external network access but no inbound internet access\n\n#### Firewall Policy Requirements\n- Define explicit allow rules rather than broad network access\n- Prohibit cross-system communication between different information systems at same layer\n- Prevent middleware from accessing foreign backend systems directly\n- Document all allowed network flows in security policy\n\n### Interservice Communication Security\n\n#### Between Different Applications\n- Frontend and middleware segments of different systems cannot communicate directly\n- Middleware cannot access backend segments of other services\n- Each application should use dedicated network segments when possible\n\n#### Load Balancer Approach\nFor organizations with fewer networks hosting multiple applications:\n- Deploy load balancers within each network segment\n- Open only one port to each network (to the load balancer)\n- Perform traffic routing based on application layer parameters\n- Note: This approach moves access control to OSI Layer 7 rather than network layer\n\n### Network Security Policy Documentation\n\nOrganizations must maintain written policies describing:\n- Firewall rules and network access permissions\n- Visual diagrams showing allowed communication flows\n- Specific provisions for different use cases\n\nPolicy should be accessible to:\n- Network administrators\n- Security representatives  \n- IT auditors\n- System architects and developers\n- IT administrators\n\n#### CI/CD Network Permissions\nDefine specific network access rules for software development systems, including:\n- Source code repository access\n- Build system connectivity\n- Deployment pipeline network requirements\n\n#### Secure Logging Architecture\nImplement tamper-resistant logging:\n- Copy logs to separate servers using syslog protocol\n- Syslog allows only adding new events, preventing log modification\n- Separate log storage from application systems\n- Include both security events and attack indicators\n\n#### Monitoring System Access\nDefine network policies for IT monitoring systems:\n- Specify which segments monitoring tools can access\n- Document data collection and alerting network flows\n- Ensure monitoring doesn't create security bypass opportunities\n\n### Implementation Best Practices\n\n- Use visual network diagrams to communicate segmentation design\n- Implement defense in depth with multiple firewall layers\n- Regularly audit network access rules and segmentation effectiveness\n- Test segmentation by attempting prohibited network connections\n- Monitor network traffic for violations of segmentation policies\n- Update segmentation rules as application architecture evolves\n\nNetwork segmentation based on these principles, following the [comprehensive guidance](https://github.com/sergiomarotco/OWASP-Network-segmentation-cheat-sheet), provides foundational security that significantly reduces attack surface and limits potential breach impact."
  },
  {
    "path": "sources/owasp/codeguard-0-nodejs-docker.md",
    "content": "---\ndescription: Node.js Docker Security Best Practices\nlanguages:\n- d\n- javascript\nalwaysApply: false\n---\n\n## Node.js Docker Security Guidelines\n\nEssential security practices for building optimized and secure Node.js Docker images for production deployment.\n\n### Use Explicit and Deterministic Base Images\n\nAlways use specific, pinned base image tags to ensure deterministic builds:\n- Avoid `FROM node` or `FROM node:latest` which introduces non-deterministic behavior\n- Use minimal base images to reduce attack surface and image size\n- Pin images with both tag and SHA256 digest for maximum security\n\nRecommended pattern:\n```dockerfile\nFROM node:lts-alpine@sha256:b2da3316acdc2bec442190a1fe10dc094e7ba4121d029cb32075ff59bb27390a\n```\n\n### Install Only Production Dependencies\n\nUse deterministic dependency installation that excludes development packages:\n```dockerfile\nRUN npm ci --omit=dev\n```\n\nThis approach:\n- Prevents surprises in CI by halting if lockfile deviations exist\n- Reduces security risk from development dependencies\n- Decreases image size by excluding unnecessary packages\n\n### Optimize for Production Environment\n\nSet the production environment variable to enable framework optimizations:\n```dockerfile\nENV NODE_ENV production\n```\n\nMany frameworks like Express only enable performance and security optimizations when this variable is set to \"production\".\n\n### Run as Non-Root User\n\nFollow the principle of least privilege to minimize security risks:\n```dockerfile\nCOPY --chown=node:node . /usr/src/app\nUSER node\n```\n\nThe official node images include a least-privileged `node` user. Ensure all copied files are owned by this user to prevent permission issues.\n\n### Handle Process Signals Properly\n\nUse a proper init system to handle process signals correctly:\n```dockerfile\nRUN apk add dumb-init\nCMD [\"dumb-init\", \"node\", \"server.js\"]\n```\n\nAvoid these problematic patterns:\n- `CMD \"npm\" \"start\"` - npm doesn't forward signals\n- `CMD \"node\" \"server.js\"` - Node.js as PID 1 doesn't handle signals properly\n\n### Implement Graceful Shutdown\n\nAdd signal handlers in your Node.js application code:\n```javascript\n    async function closeGracefully(signal) {\n       console.log(`*^!@4=> Received signal to terminate: ${signal}`)\n     \n       await fastify.close()\n       // await db.close() if we have a db connection in this app\n       // await other things we should cleanup nicely\n       process.exit()\n    }\n    process.on('SIGINT', closeGracefully)\n    process.on('SIGTERM', closeGracefully)\n```\n\n### Use Multi-Stage Builds\n\nSeparate build and production stages to minimize final image size and prevent secret leakage:\n\n```dockerfile\n# --------------> The build image\nFROM node:latest AS build\nWORKDIR /usr/src/app\nCOPY package*.json /usr/src/app/\nRUN --mount=type=secret,mode=0644,id=npmrc,target=/usr/src/app/.npmrc npm ci --omit=dev\n\n# --------------> The production image\nFROM node:lts-alpine@sha256:b2da3316acdc2bec442190a1fe10dc094e7ba4121d029cb32075ff59bb27390a\nRUN apk add dumb-init\nENV NODE_ENV production\nUSER node\nWORKDIR /usr/src/app\nCOPY --chown=node:node --from=build /usr/src/app/node_modules /usr/src/app/node_modules\nCOPY --chown=node:node . /usr/src/app\nCMD [\"dumb-init\", \"node\", \"server.js\"]\n```\n\n### Use .dockerignore File\n\nCreate a `.dockerignore` file to exclude unnecessary and sensitive files:\n```\nnode_modules\nnpm-debug.log\nDockerfile\n.git\n.gitignore\n.npmrc\n```\n\nThis prevents:\n- Copying modified local `node_modules/` over the container-built version\n- Including sensitive files like credentials or local configuration\n- Cache invalidation from log files or temporary files\n\n### Mount Secrets Securely\n\nUse Docker BuildKit secrets to handle sensitive files like `.npmrc`:\n```dockerfile\nRUN --mount=type=secret,mode=0644,id=npmrc,target=/usr/src/app/.npmrc npm ci --omit=dev\n```\n\nBuild command:\n```bash\ndocker build . -t nodejs-tutorial --secret id=npmrc,src=.npmrc\n```\n\nThis ensures secrets are never copied into the final Docker image layers.\n\n### Security Scanning\n\nRegularly scan your Docker images for vulnerabilities using static analysis tools and keep dependencies updated.\n\nBy following these practices, you'll create secure, optimized, and maintainable Node.js Docker images suitable for production deployment."
  },
  {
    "path": "sources/owasp/codeguard-0-nodejs-security.md",
    "content": "---\ndescription: Node.js Security Best Practices\nlanguages:\n- c\n- javascript\n- typescript\nalwaysApply: false\n---\n\n## Node.js Security Guidelines\n\nEssential security practices for developing secure Node.js applications to prevent common vulnerabilities and attacks.\n\n### Application Security\n\n#### Use Flat Promise Chains\n\nAvoid callback hell and improve error handling by using flat Promise chains or async/await:\n\n```javascript\n// Avoid callback hell\nfunc1(\"input1\")\n   .then(function (result){\n      return func2(\"input2\");\n   })\n   .then(function (result){\n      return func3(\"input3\");\n   })\n   .then(function (result){\n      return func4(\"input4\");\n   })\n   .catch(function (error) {\n      // error operations\n   });\n```\n\nUsing async/await:\n```javascript\n(async() => {\n  try {\n    let res1 = await func1(\"input1\");\n    let res2 = await func2(\"input2\");\n    let res3 = await func3(\"input2\");\n    let res4 = await func4(\"input2\");\n  } catch(err) {\n    // error operations\n  }\n})();\n```\n\n#### Set Request Size Limits\n\nPrevent resource exhaustion by limiting request body sizes:\n\n```javascript\napp.use(express.urlencoded({ extended: true, limit: \"1kb\" }));\napp.use(express.json({ limit: \"1kb\" }));\n```\n\nFor custom limits using raw-body:\n```JavaScript\nconst contentType = require('content-type')\nconst express = require('express')\nconst getRawBody = require('raw-body')\n\nconst app = express()\n\napp.use(function (req, res, next) {\n  if (!['POST', 'PUT', 'DELETE'].includes(req.method)) {\n    next()\n    return\n  }\n\n  getRawBody(req, {\n    length: req.headers['content-length'],\n    limit: '1kb',\n    encoding: contentType.parse(req).parameters.charset\n  }, function (err, string) {\n    if (err) return next(err)\n    req.text = string\n    next()\n  })\n})\n```\n\n#### Perform Input Validation\n\nUse allowlists and sanitize all inputs to prevent injection attacks. Consider modules like validator and express-mongo-sanitize for input validation.\n\n#### Perform Output Escaping\n\nEscape all HTML and JavaScript content to prevent XSS attacks using libraries like escape-html or node-esapi.\n\n#### Monitor Event Loop Health\n\nUse monitoring to detect when your server is overloaded:\n\n```javascript\nconst toobusy = require('toobusy-js');\napp.use(function(req, res, next) {\n    if (toobusy()) {\n        res.status(503).send(\"Server Too Busy\");\n    } else {\n    next();\n    }\n});\n```\n\n#### Prevent Brute Force Attacks\n\nImplement rate limiting and delays for authentication endpoints:\n\n```javascript\nconst bouncer = require('express-bouncer');\nbouncer.blocked = function (req, res, next, remaining) {\n    res.status(429).send(\"Too many requests have been made. Please wait \" + remaining/1000 + \" seconds.\");\n};\napp.post(\"/login\", bouncer.block, function(req, res) {\n    if (LoginFailed){  }\n    else {\n        bouncer.reset( req );\n    }\n});\n```\n\n#### Use Anti-CSRF Protection\n\nProtect state-changing requests against Cross-Site Request Forgery. Note: csurf package is deprecated; use alternative CSRF protection packages.\n\n#### Prevent HTTP Parameter Pollution\n\nUse the hpp module to handle multiple parameters with the same name:\n\n```javascript\nconst hpp = require('hpp');\napp.use(hpp());\n```\n\n#### Return Only Necessary Data\n\nLimit data exposure by returning only required fields:\n\n```javascript\nexports.sanitizeUser = function(user) {\n  return {\n    id: user.id,\n    username: user.username,\n    fullName: user.fullName\n  };\n};\n```\n\n### Error and Exception Handling\n\n#### Handle Uncaught Exceptions\n\nBind to uncaughtException events to clean up resources before shutdown:\n\n```javascript\nprocess.on(\"uncaughtException\", function(err) {\n    // clean up allocated resources\n    // log necessary error details to log files\n    process.exit(); // exit the process to avoid unknown state\n});\n```\n\n#### Handle EventEmitter Errors\n\nAlways listen to error events when using EventEmitter objects:\n\n```javascript\nconst events = require('events');\nconst emitter = new myEventEmitter();\nemitter.on('error', function(err){\n    //Perform necessary error handling here\n});\n```\n\n### Server Security\n\n#### Set Secure Cookie Flags\n\nConfigure cookies with appropriate security flags:\n\n```javascript\nconst session = require('express-session');\napp.use(session({\n    secret: 'your-secret-key',\n    name: 'cookieName',\n    cookie: { secure: true, httpOnly: true, path: '/user', sameSite: true}\n}));\n```\n\n#### Use Security Headers\n\nImplement security headers using helmet:\n\n```javascript\nconst helmet = require(\"helmet\");\napp.use(helmet()); // Add various HTTP headers\n```\n\nKey headers include:\n- HSTS: `app.use(helmet.hsts());`\n- Frame protection: `app.use(helmet.frameguard());`\n- XSS protection: `app.use(helmet.xssFilter());`\n- Content Security Policy: `app.use(helmet.contentSecurityPolicy({...}));`\n- Content type protection: `app.use(helmet.noSniff());`\n- Hide powered by: `app.use(helmet.hidePoweredBy());`\n\n### Platform Security\n\n#### Keep Packages Updated\n\nRegularly audit and update dependencies:\n\n```bash\nnpm audit\nnpm audit fix\n```\n\nUse tools like OWASP Dependency-Check and Retire.js to identify vulnerable packages.\n\n#### Avoid Dangerous Functions\n\nExercise caution with potentially dangerous functions:\n- Avoid `eval()` with user input (remote code execution risk)\n- Be careful with `child_process.exec` (command injection risk)\n- Sanitize inputs when using `fs` module (directory traversal risk)\n- Use `vm` module within proper sandboxes\n\n#### Prevent ReDoS Attacks\n\nTest regular expressions for denial of service vulnerabilities using tools like vuln-regex-detector.\n\n#### Use Security Linters\n\nImplement static analysis tools like ESLint and JSHint with security-focused rules in your development workflow.\n\n#### Enable Strict Mode\n\nAlways use strict mode to catch common JavaScript errors:\n\n```javascript\n\"use strict\";\n\nfunc();\nfunction func() {\n  y = 3.14;   // This will cause an error (y is not defined)\n}\n```\n\n### Application Activity Logging\n\nImplement comprehensive logging for security monitoring:\n\n```javascript\nconst logger = new (Winston.Logger) ({\n    transports: [\n        new (winston.transports.Console)(),\n        new (winston.transports.File)({ filename: 'application.log' })\n    ],\n    level: 'verbose'\n});\n```\n\nBy following these practices, you can significantly improve the security posture of your Node.js applications and protect against common web application vulnerabilities."
  },
  {
    "path": "sources/owasp/codeguard-0-npm-security.md",
    "content": "---\ndescription: NPM Security Best Practices\nlanguages:\n- javascript\nalwaysApply: false\n---\n\n## NPM Security Guidelines\n\nEssential security practices for managing NPM packages and dependencies in JavaScript projects.\n\n### Prevent Secret Leakage\n\nAvoid publishing sensitive data to the npm registry:\n- Use the `files` property in package.json as an allowlist to control what gets published\n- Be cautious with `.gitignore` and `.npmignore` - if both exist, `.npmignore` takes precedence\n- Run `npm publish --dry-run` to review the tarball contents before actual publishing\n- NPM automatically revokes tokens detected in published packages, but prevention is better\n\n### Enforce Deterministic Builds\n\nEnsure consistent dependency installation across environments:\n- Use `npm ci` instead of `npm install` in CI/CD and production builds\n- Use `yarn install --frozen-lockfile` if using Yarn\n- Never commit changes to package.json without updating the corresponding lockfile\n- Lockfile inconsistencies can pull unintended package versions and compromise security\n\n### Minimize Script Execution Risks\n\nReduce attack surface from package installation scripts:\n- Add `--ignore-scripts` when installing packages: `npm install --ignore-scripts`\n- Consider adding `ignore-scripts=true` to your `.npmrc` configuration\n- Always vet third-party packages for credibility before installation\n- Avoid immediate upgrades to new versions; allow time for community review\n- Review changelog and release notes before upgrading dependencies\n\n### Monitor Package Health\n\nRegularly assess the state of your dependencies:\n- Use `npm outdated` to identify packages that need updates\n- Run `npm doctor` to verify healthy npm installation and environment\n- Monitor for known vulnerabilities in dependencies using `npm audit`\n- Scan for security vulnerabilities in third-party open source projects\n- Set up monitoring for new CVEs that impact your project dependencies\n\n### Use Private Registry Solutions\n\nConsider using local npm proxies for enhanced control:\n- Verdaccio provides a lightweight private registry solution\n- Private registries offer package access control and authenticated users\n- Proxy capabilities reduce duplicate downloads and save bandwidth\n- Enable routing dependencies to different registries for security control\n- Useful for testing environments and mono-repo projects\n\n### Enable Account Security\n\nProtect your npm publishing capabilities:\n- Enable two-factor authentication with `npm profile enable-2fa auth-and-writes`\n- Use auth-and-writes mode for comprehensive protection of profile, login, and package management\n- Auth-only mode provides protection for login and profile changes only\n- Use authentication apps like Google Authenticator for 2FA tokens\n\n### Manage Access Tokens Securely\n\nControl programmatic access to npm registry:\n- Create tokens with minimal required permissions using `npm token create`\n- Use read-only tokens when write access is not needed\n- Restrict tokens to specific IP ranges with `--cidr` option\n- Regularly audit tokens with `npm token list`\n- Revoke unused or compromised tokens immediately with `npm token revoke`\n- Never expose tokens in source code, logs, or environment variables\n\n### Defend Against Typosquatting\n\nProtect against malicious package substitution:\n- Verify package names and metadata with `npm info <package>` before installation\n- Be extra careful when copy-pasting installation commands from untrusted sources\n- Check source code repositories and npm registry to confirm package legitimacy\n- Default to being logged out of npm during daily development work\n- Use `--ignore-scripts` when installing packages from unknown sources\n\n### Follow Responsible Disclosure\n\nHandle security vulnerabilities appropriately:\n- Follow responsible disclosure programs when reporting vulnerabilities\n- Coordinate with package maintainers before public disclosure\n- Allow time for fixes and upgrade paths before publicizing security issues\n- Use proper channels to report security concerns to package authors\n\n### Package Naming Best Practices\n\nUnderstand npm naming rules and security implications:\n- Package names limited to 214 characters, lowercase only\n- Cannot start with dot, underscore, or contain special characters like \"~\\'!()*\"\n- Be aware that typosquatting attacks target popular package names\n- NPM uses spam detection mechanisms for new package publications\n- Reserved names include node_modules and favicon.ico"
  },
  {
    "path": "sources/owasp/codeguard-0-oauth2.md",
    "content": "---\ndescription: OAuth 2.0 Security Best Practices\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\n- yaml\nalwaysApply: false\n---\n\n## OAuth 2.0 Security Guidelines\n\nEssential security practices for implementing secure OAuth 2.0 authorization flows and protecting against common attacks.\n\n### Essential Basics\n\nPrevent open redirectors that can enable token exfiltration:\n- Clients and Authorization Servers must not expose URLs that forward the user's browser to arbitrary URIs obtained from query parameters\n- Use exact string matching for redirect URI validation during client registration\n\nUse proper CSRF protection:\n- When Authorization Server supports PKCE, clients may rely on PKCE's CSRF protection\n- In OpenID Connect flows, the \"nonce\" parameter provides CSRF protection\n- Otherwise, use one-time CSRF tokens in the \"state\" parameter that are securely bound to the user agent\n\nPrevent mix-up attacks in multi-Authorization Server environments:\n- Use the issuer \"iss\" parameter as a countermeasure when interacting with multiple Authorization Servers\n- Alternatively, use distinct redirect URIs to identify different authorization and token endpoints\n- Authorization Servers should avoid accidentally forwarding requests containing user credentials\n\n### PKCE - Proof Key for Code Exchange\n\nPKCE mitigates authorization code interception attacks, especially for public clients:\n\n- Use PKCE flow to prevent injection (replay) of authorization codes into authorization responses\n- Use PKCE code challenge methods that do not expose the verifier in the authorization request\n- Use S256 as the code challenge method instead of plain text\n- Authorization servers must support PKCE and enforce correct \"code_verifier\" usage at the token endpoint\n- Prevent PKCE downgrade attacks by accepting \"code_verifier\" only when \"code_challenge\" was present in the authorization request\n\n### Authorization Code vs Implicit Grant\n\nPrefer Authorization Code Grant over Implicit Grant:\n- Use response type \"code\" (authorization code grant) or \"code id_token\" instead of implicit flows\n- This allows the Authorization Server to detect replay attempts and reduces attack surface\n- Access tokens are not exposed in URLs and can be sender-constrained\n\n### Token Replay Prevention\n\nImplement sender-constraining mechanisms:\n- Use Mutual TLS for OAuth 2.0 or OAuth Demonstration of Proof of Possession (DPoP) to prevent token replays\n- Implement refresh token rotation or ensure refresh tokens are sender-constrained\n\n### Access Token Privilege Restriction\n\nApply the principle of least privilege:\n- Restrict token privileges to the minimum required for the particular application or use case\n- Implement audience restriction by associating access tokens with specific Resource Servers\n- Resource Servers must verify that tokens were intended for their use\n- Restrict tokens to specific resources and actions using \"scope\" and \"authorization_details\" parameters\n- Use \"scope\" and \"resource\" parameters to determine the intended Resource Server\n\n### Avoid Insecure Grant Types\n\nNever use Resource Owner Password Credentials Grant:\n- This grant type insecurely exposes Resource Owner credentials to the client\n- Increases the attack surface of the application\n\n### Client Authentication\n\nUse strong authentication methods:\n- Implement client authentication whenever possible\n- Prefer asymmetric (public-key based) methods like mTLS or \"private_key_jwt\" (OpenID Connect)\n- Asymmetric methods eliminate the need to store sensitive symmetric keys on Authorization Servers\n- This approach is more robust against various attacks\n\n### Additional Security Controls\n\nProtect sensitive claims and enforce secure communications:\n- Authorization Servers must not allow clients to influence their \"client_id\" or \"sub\" values\n- Prevent clients from controlling any claims that could be confused with genuine Resource Owner data\n- Use end-to-end TLS for all communications\n- Never transmit authorization responses over unencrypted network connections\n- Prohibit redirect URIs using \"http\" scheme except for native clients using Loopback Interface Redirection\n\n### Implementation Summary\n\nSecure OAuth 2.0 implementations require:\n- PKCE implementation for all public clients\n- Proper CSRF protection through state parameters or nonce\n- Authorization Code Grant preference over Implicit Grant\n- Token sender-constraining mechanisms (mTLS or DPoP)\n- Strict privilege restriction and audience validation\n- Strong client authentication using asymmetric methods\n- Elimination of insecure grant types\n- Comprehensive TLS enforcement and redirect URI validation\n\nFollowing these practices ensures robust protection against authorization code interception, token replay, privilege escalation, and mix-up attacks while maintaining the flexibility and security benefits of OAuth 2.0."
  },
  {
    "path": "sources/owasp/codeguard-0-open-redirect.md",
    "content": "---\ndescription: Open Redirect Prevention - Secure handling of user-controlled redirects\n  to prevent phishing attacks\nlanguages:\n- c\n- javascript\n- php\n- typescript\n- vlang\nalwaysApply: false\n---\n\n# Avoid Open Redirects\n\n- Never use user input directly as a redirect target (e.g., `res.redirect(userInput)` in Node.js/Express).\n- If redirection is controlled by user input:\n    - Allow only local paths (must start with `/` and not contain protocols like `http:`).\n    - OR: Allow only destinations present in a strict allowlist of trusted domains.\n- URLs must be parsed and validated using robust libraries, not simple substring checks.\n- DO NOT allow wildcard domains like `*.example.com` in any allowlist.\n- Add explicit code comments when using redirects to document how the rule is being enforced."
  },
  {
    "path": "sources/owasp/codeguard-0-os-command-injection-defense.md",
    "content": "---\ndescription: OS Command Injection Defense\nlanguages:\n- c\n- go\n- java\n- javascript\n- perl\n- php\n- python\n- ruby\n- shell\nalwaysApply: false\n---\n\n## OS Command Injection Defense Guidelines\n\nEssential practices for preventing OS command injection vulnerabilities when executing system commands in applications.\n\n### Understanding Command Injection\n\nCommand injection occurs when software constructs a system command using externally influenced input without properly neutralizing special elements that can modify the intended command.\n\nExample of vulnerable input:\n```\ncalc & echo \"test\"\n```\n\nThis changes the meaning from executing just `calc` to executing both `calc` and `echo \"test\"`.\n\n#### Argument Injection\n\nEvery OS Command Injection is also an Argument Injection, where user input can be passed as arguments while executing a specific command. For example:\n\n```php\nsystem(\"curl \" . escape(\"--help\"))\n```\n\nEven with escaping, this shows the output of `curl --help` instead of the intended behavior.\n\n### Primary Defenses\n\n#### Defense Option 1: Avoid Calling OS Commands Directly\n\nThe primary defense is to avoid calling OS commands directly. Built-in library functions are preferred as they cannot be manipulated to perform unintended tasks.\n\nExample: Use `mkdir()` instead of `system(\"mkdir /dir_name\")`.\n\n#### Defense Option 2: Escape Values Added to OS Commands\n\nUse language-specific escaping functions when OS commands cannot be avoided.\n\nPHP example using escapeshellarg():\nThe `escapeshellarg()` function surrounds user input in single quotes, so malformed input like `& echo \"hello\"` becomes `calc '& echo \"hello\"'` which is parsed as a single argument.\n\nNote: Even with `escapeshellarg()`, an attacker can still pass a single argument to the command.\n\n#### Defense Option 3: Parameterization with Input Validation\n\nIf system commands incorporating user input cannot be avoided, use two layers of defense:\n\nLayer 1 - Parameterization:\nUse structured mechanisms that automatically enforce separation between data and command, providing proper quoting and encoding.\n\nLayer 2 - Input Validation:\n- Commands: Validate against a list of allowed commands\n- Arguments: Use positive/allowlist input validation where arguments are explicitly defined\n- Allowlist Regular Expression: Define allowed characters and maximum length, excluding metacharacters\n\nExample regex allowing only lowercase letters and numbers (3-10 characters): `^[a-z0-9]{3,10}$`\n\nPOSIX Guideline: Use `--` delimiter to prevent argument injection:\n```\ncurl -- $url\n```\nThis prevents argument injection even if `$url` contains additional arguments.\n\nDangerous metacharacters to avoid:\n```\n& |  ; $ > < ` \\ ! ' \" ( )\n```\n\n### Code Examples\n\n#### Java\n\nUse ProcessBuilder with separated command and arguments:\n\nIncorrect usage:\n```java\nProcessBuilder b = new ProcessBuilder(\"C:\\DoStuff.exe -arg1 -arg2\");\n```\n\nCorrect usage:\n```java\nProcessBuilder pb = new ProcessBuilder(\"TrustedCmd\", \"TrustedArg1\", \"TrustedArg2\");\n\nMap<String, String> env = pb.environment();\n\npb.directory(new File(\"TrustedDir\"));\n\nProcess p = pb.start();\n```\n\nNote about Runtime.exec:\nJava's `Runtime.exec` does NOT invoke the shell and does not support shell metacharacters. It splits strings into arrays and executes the first word with the rest as parameters, making shell-based attacks ineffective.\n\n\n#### PHP\n\nUse escapeshellarg() or escapeshellcmd() rather than exec(), system(), or passthru().\n\n### Additional Defenses\n\nImplement defense in depth:\n- Applications should run with the lowest privileges required for necessary tasks\n- Create isolated accounts with limited privileges for single tasks\n\n### Implementation Summary\n\nSecure command execution requires:\n- Avoiding OS commands when possible (use built-in libraries)\n- Using parameterized execution with separated commands and arguments\n- Implementing strict input validation with allowlists\n- Applying proper escaping functions when available\n- Running applications with minimal privileges\n- Using structured mechanisms that enforce data/command separation\n\nFollowing these practices significantly reduces the risk of OS command injection vulnerabilities in applications."
  },
  {
    "path": "sources/owasp/codeguard-0-password-storage.md",
    "content": "---\ndescription: Password Storage Security\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\nalwaysApply: false\n---\n\n## Password Storage Security Guidelines\n\nEssential practices for securely storing passwords using modern hashing algorithms to protect against offline attacks.\n\n### Core Principles\n\nPasswords must be hashed, NOT encrypted. Hashing is a one-way function that prevents retrieval of the original password even if the database is compromised. Encryption is reversible and should only be used in rare edge cases where the original password must be recovered.\n\nStrong passwords stored with modern hashing algorithms and proper configuration should be effectively impossible for attackers to crack.\n\nTo sum up our recommendations:\n\n- **Use [Argon2id](#argon2id) with a minimum configuration of 19 MiB of memory, an iteration count of 2, and 1 degree of parallelism.**\n- **If [Argon2id](#argon2id) is not available, use [scrypt](#scrypt) with a minimum CPU/memory cost parameter of (2^17), a minimum block size of 8 (1024 bytes), and a parallelization parameter of 1.**\n- **For legacy systems using [bcrypt](#bcrypt), use a work factor of 10 or more and with a password limit of 72 bytes.**\n- **If FIPS-140 compliance is required, use [PBKDF2](#pbkdf2) with a work factor of 600,000 or more and set with an internal hash function of HMAC-SHA-256.**\n- **Consider using a [pepper](#peppering) to provide additional defense in depth (though alone, it provides no additional secure characteristics).**\n\n### Recommended Algorithms and Parameters\n\nUse these algorithms in order of preference:\n\n#### Argon2id (Preferred)\nArgon2id was the winner of the 2015 Password Hashing Competition and provides balanced resistance to side-channel and GPU-based attacks.\n\nConfiguration options (choose one):\n- m=47104 (46 MiB), t=1, p=1\n- m=19456 (19 MiB), t=2, p=1  \n- m=12288 (12 MiB), t=3, p=1\n- m=9216 (9 MiB), t=4, p=1\n- m=7168 (7 MiB), t=5, p=1\n\nAll configurations provide equal security with different CPU/RAM trade-offs.\n\n#### scrypt (If Argon2id unavailable)\nConfiguration options (choose one):\n- N=2^17 (128 MiB), r=8 (1024 bytes), p=1\n- N=2^16 (64 MiB), r=8 (1024 bytes), p=2\n- N=2^15 (32 MiB), r=8 (1024 bytes), p=3\n- N=2^14 (16 MiB), r=8 (1024 bytes), p=5\n- N=2^13 (8 MiB), r=8 (1024 bytes), p=10\n\n#### bcrypt (Legacy systems only)\nUse work factor of 10 or more. Maximum password length is 72 bytes for most implementations.\n\n#### PBKDF2 (FIPS-140 compliance required)\n- PBKDF2-HMAC-SHA1: 1,300,000 iterations\n- PBKDF2-HMAC-SHA256: 600,000 iterations\n- PBKDF2-HMAC-SHA512: 210,000 iterations\n\n### Salting\n\nModern hashing algorithms (Argon2id, bcrypt, scrypt, PBKDF2) automatically handle salting. Salts must be:\n- Unique for every password\n- Generated using a cryptographically secure random number generator\n- Stored alongside the hash\n\nNever manually implement salting when using modern algorithms.\n\n### Peppering (Optional Defense in Depth)\n\nA pepper is a secret value shared between stored passwords, unlike salts which are unique per password. Peppers provide additional protection if the database is compromised but the application server remains secure.\n\nRequirements for peppering:\n- Store pepper separately from the password database\n- Use secure storage (secrets vaults, HSMs)\n- Generate securely using cryptographically strong methods\n- Changing pepper requires all users to reset passwords\n\nImplementation strategies:\n- Pre-hashing: Add pepper to password before hashing\n- Post-hashing: HMAC the password hash with pepper as key\n\n### Work Factor Tuning\n\nBalance security and performance by adjusting work factors:\n- Target less than one second for hash calculation\n- Increase work factors over time as hardware improves\n- Test on your specific server hardware\n- Higher work factors slow down both legitimate authentication and attacker cracking\n\n### Upgrading Legacy Hashes\n\nFor applications using weak algorithms (MD5, SHA-1):\n\nMethod 1: Force password reset\n- Expire old hashes for inactive users\n- Require password reset on next login\n- More secure but less user-friendly\n\nMethod 2: Layer hashing\n- Use existing hash as input to secure algorithm\n- Example: `bcrypt(md5($password))`\n- Upgrade to direct hashing when user next authenticates\n- Store algorithm and parameters using PHC string format\n\n### bcrypt Pre-Hashing Considerations\n\nIf pre-hashing with bcrypt is necessary:\n- Use `bcrypt(base64(hmac-sha384(data:$password, key:$pepper)), $salt, $cost)`\n- Store pepper outside database\n- Avoid simple pre-hashing like `bcrypt(sha512($password))` due to password shucking vulnerability\n\n### International Character Support\n\nPassword hashing libraries must:\n- Accept full Unicode character range\n- Support null bytes in passwords\n- Preserve entropy without reduction\n- Handle characters from various languages and pictograms\n\n### Performance Guidelines\n\n- Hash calculation should take less than one second\n- Benchmark on production hardware\n- Monitor authentication performance\n- Adjust parameters based on server capacity and user load\n- Consider denial of service risks from overly high work factors\n\n### Implementation Summary\n\nSecure password storage requires:\n- Modern slow hashing algorithms (Argon2id preferred)\n- Proper algorithm configuration with adequate work factors\n- Automatic salt handling by the hashing library\n- Optional pepper for defense in depth\n- Upgrade path for legacy hashes\n- Performance tuning for your environment\n- Support for international characters\n\nFollowing these practices protects user passwords against offline attacks even if the database is compromised."
  },
  {
    "path": "sources/owasp/codeguard-0-php-configuration.md",
    "content": "---\ndescription: PHP Secure Configuration\nlanguages:\n- php\nalwaysApply: false\n---\n\n## PHP Secure Configuration Guidelines\n\nEssential security settings for PHP configuration to harden PHP applications against common vulnerabilities.\n\n### PHP Version Management\n\nRun a supported version of PHP. As of this writing, 8.1 is the oldest version receiving security support from PHP, though distribution vendors often provide extended support.\n\n### Error Handling Configuration\n\nConfigure proper error handling to prevent information disclosure while ensuring errors are logged:\n\n```ini\nexpose_php              = Off\nerror_reporting         = E_ALL\ndisplay_errors          = Off\ndisplay_startup_errors  = Off\nlog_errors              = On\nerror_log               = /valid_path/PHP-logs/php_error.log\nignore_repeated_errors  = Off\n```\n\nKeep `display_errors` to `Off` on production servers and monitor the logs frequently.\n\n### General Security Settings\n\n```ini\ndoc_root                = /path/DocumentRoot/PHP-scripts/\nopen_basedir            = /path/DocumentRoot/PHP-scripts/\ninclude_path            = /path/PHP-pear/\nextension_dir           = /path/PHP-extensions/\nmime_magic.magicfile    = /path/PHP-magic.mime\nallow_url_fopen         = Off\nallow_url_include       = Off\nvariables_order         = \"GPCS\"\nallow_webdav_methods    = Off\nsession.gc_maxlifetime  = 600\n```\n\n`allow_url_*` prevents LFIs from being easily escalated to RFIs.\n\n### File Upload Handling\n\n```ini\nfile_uploads            = On\nupload_tmp_dir          = /path/PHP-uploads/\nupload_max_filesize     = 2M\nmax_file_uploads        = 2\n```\n\nIf your application is not using file uploads, `file_uploads` should be turned `Off`.\n\n### Executable Handling\n\n```ini\nenable_dl               = Off\ndisable_functions       = system, exec, shell_exec, passthru, phpinfo, show_source, highlight_file, popen, proc_open, fopen_with_path, dbmopen, dbase_open, putenv, move_uploaded_file, chdir, mkdir, rmdir, chmod, rename, filepro, filepro_rowcount, filepro_retrieve, posix_mkfifo\ndisable_classes         =\n```\n\nThese are dangerous PHP functions. Disable all functions that you don't use.\n\n### Session Handling\n\nSession settings are some of the most important values to concentrate on in configuring. It is a good practice to change `session.name` to something new.\n\n```ini\nsession.save_path                = /path/PHP-session/\nsession.name                     = myPHPSESSID\nsession.auto_start               = Off\nsession.use_trans_sid            = 0\nsession.cookie_domain            = full.qualified.domain.name\n#session.cookie_path             = /application/path/\nsession.use_strict_mode          = 1\nsession.use_cookies              = 1\nsession.use_only_cookies         = 1\nsession.cookie_lifetime          = 14400 # 4 hours\nsession.cookie_secure            = 1\nsession.cookie_httponly          = 1\nsession.cookie_samesite          = Strict\nsession.cache_expire             = 30\nsession.sid_length               = 256\nsession.sid_bits_per_character   = 6\n```\n\n### Additional Security Settings\n\n```ini\nsession.referer_check   = /application/path\nmemory_limit            = 50M\npost_max_size           = 20M\nmax_execution_time      = 60\nreport_memleaks         = On\nhtml_errors             = Off\nzend.exception_ignore_args = On\n```\n\n### Advanced Protection with Snuffleupagus\n\nSnuffleupagus is the spiritual descendant of Suhosin for PHP 7 and onwards, with modern features. It's considered stable and is usable in production.\n\n### Implementation Summary\n\nSecure PHP configuration requires:\n- Hiding PHP version information (expose_php = Off)\n- Proper error handling with logging enabled but display disabled in production\n- Disabling remote file access (allow_url_fopen/include = Off)\n- Restricting dangerous functions based on application needs\n- Hardening session management with secure cookie settings\n- Setting appropriate resource limits to prevent DoS\n- Using modern security extensions like Snuffleupagus\n\nFollowing these configuration practices significantly reduces the attack surface of PHP applications and protects against common vulnerabilities."
  },
  {
    "path": "sources/owasp/codeguard-0-pinning.md",
    "content": "---\ndescription: Certificate and Public Key Pinning Security\nlanguages:\n- c\n- java\n- javascript\n- kotlin\n- matlab\n- swift\n- typescript\n- xml\nalwaysApply: false\n---\n\n## Certificate and Public Key Pinning Guidelines\n\nEssential practices for implementing certificate and public key pinning to prevent Man-in-the-Middle attacks in hostile environments.\n\n### Understanding the Problem\n\nTLS channels can be vulnerable to MITM attacks when certificate-based trust is compromised through:\n1. Attackers acquiring rogue certificates from trusted CAs for victim sites\n2. Attackers injecting dangerous CAs into client trust stores\n\nPinning associates a host with its expected X509 certificate or public key, creating a pinset that advertised credentials must match.\n\n### When NOT to Pin (Critical Decision Criteria)\n\nAvoid pinning in these situations:\n- You don't control both client and server sides of the connection\n- You cannot update the pinset securely without app redeployment\n- Certificate key pairs cannot be predicted before being put into service\n- Application is not a native mobile application\n- Updating pinset is disruptive to operations\n\nThe risk of outages almost always outweighs security benefits given modern certificate authority security advancements.\n\n### When Pinning May Be Appropriate\n\nConsider pinning only when:\n- You control both endpoints and can manage certificate lifecycles\n- You can implement secure pin update mechanisms\n- Your threat model specifically requires protection against CA compromise\n- You have tested thoroughly and planned for certificate rotation\n\n### Implementation Approaches by Platform\n\n#### Android\nUse Android's Network Security Configuration feature with `<pin-set>` configuration settings. Alternatively, use established libraries like OkHTTP for programmatic pinning. Avoid implementing custom SSL validation from scratch.\n\n#### iOS\nApple suggests pinning CA public keys via `Info.plist` under App Transport Security Settings. Use TrustKit library for easier implementation. Custom implementation requires SecTrustEvaluate logic following HTTPS Server Trust Evaluation guidelines.\n\n#### .Net\nImplement using ServicePointManager callbacks for certificate validation.\n\n#### OpenSSL\nUse verify_callback or post-connection validation via SSL_get_peer_certificate. Must call SSL_get_verify_result (verify X509_V_OK) and SSL_get_peer_certificate (verify non-NULL). Fail connection and tear down socket on validation errors.\n\n#### Electron\nUse electron-ssl-pinning library or ses.setCertificateVerifyProc for custom certificate validation.\n\n### What to Pin\n\nPin selection strategy:\n1. Leaf certificate pinning (recommended): Provides 100% certainty but requires backup pins for intermediate CAs to prevent app breakage during certificate rotation\n2. Intermediate CA pinning: Reduces risk but trusts all certificates issued by that CA\n3. Root CA pinning: Not recommended due to high risk from trusting all intermediate CAs\n\nPin type options:\n- Whole certificate: Easiest to implement but requires frequent updates for certificate rotation\n- Public key (subjectPublicKeyInfo): More flexible, allows certificate renewal with same key, provides access to key parameters and algorithm context\n- Hash: Convenient and fixed-length but lacks contextual information\n\nPrefer subjectPublicKeyInfo pinning for balance of flexibility and security context.\n\n### Pin Management Best Practices\n\n#### Pin Addition Timing\nAdd pins at development time (preloading) rather than Trust On First Use (TOFU). Preloading out-of-band prevents attackers from tainting pins.\n\n#### Backup Strategy\nAlways include backup pins (intermediate CA or alternate certificates) to prevent application outages during certificate updates.\n\n#### Update Mechanisms\nPlan secure pin update methods that don't require app redeployment. Consider remote configuration with authenticated channels.\n\n#### Failure Handling\nNever allow users to bypass pin validation failures. Log failures client-side but terminate connections on pin mismatches.\n\n### Corporate Environment Considerations\n\nFor organizations using interception proxies as part of Data Loss Prevention:\n- Do not automatically allow-list interception proxies\n- Add proxy public keys to pinset only after explicit risk acceptance approval\n- Treat corporate proxies as \"good bad actors\" that still break end-to-end security\n\n### Testing and Validation\n\nThoroughly test pinning implementations using OWASP Mobile Security Testing Guide network communication guidelines:\n- Verify pin validation occurs correctly\n- Test certificate rotation scenarios\n- Validate failure handling paths\n- Ensure backup pins function properly\n\n### Operational Considerations\n\n#### Certificate Lifecycle Management\n- Coordinate with backend teams on certificate rotation schedules\n- Plan pin updates in advance of certificate expiration\n- Monitor certificate validity periods\n- Implement alerts for approaching pin expiration\n\n#### Risk Assessment\nUnderstand that pinning creates operational risk of application outages if not managed properly. The security benefit must outweigh availability risks for your specific threat model.\n\n### Common Implementation Errors to Avoid\n\n- Custom TLS or pinning implementations instead of vetted libraries\n- Pinning without backup strategies\n- Allowing user bypass of pin failures\n- Inadequate testing of certificate rotation scenarios\n- Pinning root CAs without understanding the expanded trust implications\n\n### Summary\n\nCertificate and public key pinning can provide additional protection against sophisticated MITM attacks but introduces significant operational complexity and availability risks. Most applications should rely on standard certificate validation rather than implementing pinning. When pinning is necessary, use platform-native solutions or well-established libraries, implement comprehensive backup strategies, and thoroughly test all scenarios including certificate rotation."
  },
  {
    "path": "sources/owasp/codeguard-0-prototype-pollution-prevention.md",
    "content": "---\ndescription: Prototype Pollution Prevention\nlanguages:\n- javascript\n- typescript\nalwaysApply: false\n---\n\n# Prototype Pollution Prevention Guideline\n\n## Explanation\n\nPrototype Pollution is a critical vulnerability that can allow attackers to manipulate an application's JavaScript objects and properties, leading to serious security issues such as unauthorized access to data, privilege escalation, and even remote code execution.\n\n## Suggested protection mechanisms\n\n### Use \"new Set()\" or \"new Map()\"\n\nDevelopers should use `new Set()` or `new Map()` instead of using object literals:\n\n```javascript\nlet allowedTags = new Set();\nallowedTags.add('b');\nif(allowedTags.has('b')){\n  //...\n}\n\nlet options = new Map();\noptions.set('spaces', 1);\nlet spaces = options.get('spaces')\n```\n\n### If objects or object literals are required\n\nIf objects have to be used then they should be created using the `Object.create(null)` API to ensure they don't inherit from the Object prototype:\n\n```javascript\nlet obj = Object.create(null);\n```\n\nIf object literals are required then as a last resort you could use the `__proto__` property:\n\n```javascript\nlet obj = {__proto__:null};\n```\n\n### Use object \"freeze\" and \"seal\" mechanisms\n\nYou can also use the `Object.freeze()` and `Object.seal()` APIs to prevent built-in prototypes from being modified however this can break the application if the libraries they use modify the built-in prototypes.\n\n### Node.js configuration flag\n\nNode.js also offers the ability to remove the `__proto__` property completely using the `--disable-proto=delete` flag. Note this is a defense in depth measure.\n\nPrototype pollution is still possible using `constructor.prototype` properties but removing `__proto__` helps reduce attack surface and prevent certain attacks.\n\n### Other resources\n\n- [What is prototype pollution? (Portswigger Web Security Academy)](https://portswigger.net/web-security/prototype-pollution)\n- [Prototype pollution (Snyk Learn)](https://learn.snyk.io/lessons/prototype-pollution/javascript/)\n\n### Credits\n\nCredit to [Gareth Hayes](https://garethheyes.co.uk/) for providing the original protection guidance [in this comment](https://github.com/OWASP/ASVS/issues/1563#issuecomment-1470027723)."
  },
  {
    "path": "sources/owasp/codeguard-0-query-parameterization.md",
    "content": "---\ndescription: SQL Injection Prevention via Query Parameterization\nlanguages:\n- c\n- java\n- perl\n- php\n- ruby\n- rust\n- sql\nalwaysApply: false\n---\n\n## Query Parameterization Guidelines\n\nEssential practices for preventing SQL injection attacks by using parameterized queries instead of string concatenation when building database queries.\n\n### Core Principle\n\nSQL injection is prevented through parameterized queries that separate SQL structure from data. User input is treated as data parameters, not executable SQL code, preventing attackers from altering query structure.\n\n### Implementation Requirements\n\nAlways use language-specific parameterized queries or prepared statements:\n\n#### Java with PreparedStatement\n```java\nString custname = request.getParameter(\"customerName\");\nString query = \"SELECT account_balance FROM user_data WHERE user_name = ? \";  \nPreparedStatement pstmt = connection.prepareStatement( query );\npstmt.setString( 1, custname);\nResultSet results = pstmt.executeQuery( );\n```\n\n#### Java with Hibernate\n```java\n// HQL\n@Entity // declare as entity;\n@NamedQuery(\n name=\"findByDescription\",\n query=\"FROM Inventory i WHERE i.productDescription = :productDescription\"\n)\npublic class Inventory implements Serializable {\n @Id\n private long id;\n private String productDescription;\n}\n\n// Use case\n// This should REALLY be validated too\nString userSuppliedParameter = request.getParameter(\"Product-Description\");\n// Perform input validation to detect attacks\nList<Inventory> list =\n session.getNamedQuery(\"findByDescription\")\n .setParameter(\"productDescription\", userSuppliedParameter).list();\n\n// Criteria API\n// This should REALLY be validated too\nString userSuppliedParameter = request.getParameter(\"Product-Description\");\n// Perform input validation to detect attacks\nInventory inv = (Inventory) session.createCriteria(Inventory.class).add\n(Restrictions.eq(\"productDescription\", userSuppliedParameter)).uniqueResult();\n```\n\n#### .NET with OleDbCommand\n```csharp\nString query = \"SELECT account_balance FROM user_data WHERE user_name = ?\";\ntry {\n   OleDbCommand command = new OleDbCommand(query, connection);\n   command.Parameters.Add(new OleDbParameter(\"customerName\", CustomerName Name.Text));\n   OleDbDataReader reader = command.ExecuteReader();\n   // …\n} catch (OleDbException se) {\n   // error handling\n}\n```\n\n#### ASP.NET with SqlCommand\n```csharp\nstring sql = \"SELECT * FROM Customers WHERE CustomerId = @CustomerId\";\nSqlCommand command = new SqlCommand(sql);\ncommand.Parameters.Add(new SqlParameter(\"@CustomerId\", System.Data.SqlDbType.Int));\ncommand.Parameters[\"@CustomerId\"].Value = 1;\n```\n\n#### Ruby with ActiveRecord\n```ruby\n## Create\nProject.create!(:name => 'owasp')\n## Read\nProject.all(:conditions => \"name = ?\", name)\nProject.all(:conditions => { :name => name })\nProject.where(\"name = :name\", :name => name)\n## Update\nproject.update_attributes(:name => 'owasp')\n## Delete\nProject.delete(:name => 'name')\n```\n\n#### Ruby Built-in\n```ruby\ninsert_new_user = db.prepare \"INSERT INTO users (name, age, gender) VALUES (?, ? ,?)\"\ninsert_new_user.execute 'aizatto', '20', 'male'\n```\n\n#### PHP with PDO\n```php\n$stmt = $dbh->prepare(\"INSERT INTO REGISTRY (name, value) VALUES (:name, :value)\");\n$stmt->bindParam(':name', $name);\n$stmt->bindParam(':value', $value);\n```\n\n#### Cold Fusion\n```coldfusion\n<cfquery name = \"getFirst\" dataSource = \"cfsnippets\">\n    SELECT * FROM #strDatabasePrefix#_courses WHERE intCourseID =\n    <cfqueryparam value = #intCourseID# CFSQLType = \"CF_SQL_INTEGER\">\n</cfquery>\n```\n\n#### Perl with DBI\n```perl\nmy $sql = \"INSERT INTO foo (bar, baz) VALUES ( ?, ? )\";\nmy $sth = $dbh->prepare( $sql );\n$sth->execute( $bar, $baz );\n```\n\n#### Rust with SQLx\n```rust\n// Input from CLI args but could be anything\nlet username = std::env::args().last().unwrap();\n\n// Using build-in macros (compile time checks)\nlet users = sqlx::query_as!(\n        User,\n        \"SELECT * FROM users WHERE name = ?\",\n        username\n    )\n    .fetch_all(&pool)\n    .await \n    .unwrap();\n\n// Using built-in functions\nlet users: Vec<User> = sqlx::query_as::<_, User>(\n        \"SELECT * FROM users WHERE name = ?\"\n    )\n    .bind(&username)\n    .fetch_all(&pool)\n    .await\n    .unwrap();\n```\n\n### Stored Procedure Security\n\n#### Normal Stored Procedures\nParameters are naturally bound without special requirements:\n\n##### Oracle PL/SQL\n```sql\nPROCEDURE SafeGetBalanceQuery(UserID varchar, Dept varchar) AS BEGIN\n   SELECT balance FROM accounts_table WHERE user_ID = UserID AND department = Dept;\nEND;\n```\n\n##### SQL Server T-SQL\n```sql\nPROCEDURE SafeGetBalanceQuery(@UserID varchar(20), @Dept varchar(10)) AS BEGIN\n   SELECT balance FROM accounts_table WHERE user_ID = @UserID AND department = @Dept\nEND\n```\n\n#### Dynamic SQL in Stored Procedures\nUse bind variables to ensure dynamic SQL treats inputs as data, not code:\n\n##### Oracle with EXECUTE IMMEDIATE\n```sql\nPROCEDURE AnotherSafeGetBalanceQuery(UserID varchar, Dept varchar)\n          AS stmt VARCHAR(400); result NUMBER;\nBEGIN\n   stmt := 'SELECT balance FROM accounts_table WHERE user_ID = :1\n            AND department = :2';\n   EXECUTE IMMEDIATE stmt INTO result USING UserID, Dept;\n   RETURN result;\nEND;\n```\n\n##### SQL Server with sp_executesql\n```sql\nPROCEDURE SafeGetBalanceQuery(@UserID varchar(20), @Dept varchar(10)) AS BEGIN\n   DECLARE @sql VARCHAR(200)\n   SELECT @sql = 'SELECT balance FROM accounts_table WHERE '\n                 + 'user_ID = @UID AND department = @DPT'\n   EXEC sp_executesql @sql,\n                      '@UID VARCHAR(20), @DPT VARCHAR(10)',\n                      @UID=@UserID, @DPT=@Dept\nEND\n```\n\n### Critical Security Notes\n\n- Ensure parameterization occurs server-side; client-side parameterization libraries may still build unsafe queries through string concatenation\n- Parameterized queries are the primary defense against SQL injection\n- Input validation should focus on business logic requirements, not SQL injection prevention\n- Never concatenate user input directly into SQL query strings\n- Use bind variables for any dynamic SQL construction within stored procedures"
  },
  {
    "path": "sources/owasp/codeguard-0-rest-assessment.md",
    "content": "---\ndescription: RESTful Web Service Security Assessment Guidelines\nlanguages:\n- c\n- go\n- java\n- javascript\n- python\n- ruby\n- typescript\n- xml\n- yaml\nalwaysApply: false\n---\n\n## RESTful Web Service Security Assessment Guidelines\n\nEssential practices for security testing and assessment of RESTful web services, focusing on identifying attack surfaces and testing methodologies.\n\n### Understanding REST Security Challenges\n\nRESTful web services present unique security testing challenges:\n\n- Attack surface is not visible through application inspection since client applications often don't utilize all available service functions\n- Parameters may be embedded in URL paths, custom headers, or structured data rather than standard query strings\n- Large parameter sets in JSON/XML structures significantly increase testing complexity\n- Custom authentication mechanisms require reverse engineering and may not work with standard testing tools\n- Lack of formal documentation makes comprehensive testing difficult\n\n### REST Service Characteristics\n\nKey properties that impact security assessment:\n\n- Primary operations use HTTP methods (GET, POST, PUT, DELETE)\n- Non-standard parameter locations including URL segments and custom headers\n- Structured parameters and responses using JSON or XML formats\n- Custom authentication and session management with security tokens\n- Machine-to-machine communication without traditional login sequences\n\n### Attack Surface Discovery\n\n#### Documentation-Based Discovery\n\nObtain service information for comprehensive coverage:\n\n- Formal service descriptions (WSDL 2.0, WADL) when available\n- Developer guides and API documentation\n- Application source code or configuration files\n- Framework configuration files (especially .NET) that may reveal REST service definitions\n\n#### Proxy-Based Request Collection\n\nUse capable proxy tools to collect complete HTTP interactions:\n\n- Capture full requests including headers and body content, not just URLs\n- REST services utilize more than GET parameters requiring complete request analysis\n- Dynamic client-side activation may not provide visible links for inspection\n\n#### Parameter Identification Techniques\n\nAnalyze collected requests to identify non-standard parameters:\n\n- Abnormal HTTP headers often indicate header-based parameters\n- URL segments with repeating patterns (dates, numbers, ID-like strings) suggest URL-embedded parameters\n- URLs without extensions in the final segment, especially when other segments have extensions\n- Highly varying URL segments with many different values indicate parameters rather than physical directories\n- Structured parameter values in JSON, XML, or custom formats\n\n### Parameter Verification Methods\n\nDistinguish between path elements and parameters:\n\n- Set suspected parameter values to invalid inputs\n- Web server returns 404 for invalid path elements\n- Application returns application-level error messages for invalid parameter values\n- This technique helps confirm parameter identification but doesn't work in all cases\n\n### Fuzzing Optimization Strategies\n\nAnalyze collected parameter values to optimize testing:\n\n- Identify valid versus invalid value patterns\n- Focus fuzzing on marginal invalid values (e.g., zero for positive integers)\n- Identify sequences to test beyond current user's allocated range\n- Understand parameter relationships and dependencies\n\n### Authentication Mechanism Handling\n\nAddress custom authentication challenges:\n\n- Reverse engineer custom token-based authentication\n- Ensure fuzzing tools properly emulate authentication mechanisms\n- Account for session management differences in machine-to-machine communication\n- Test authentication bypass and privilege escalation scenarios\n\n### Testing Methodology Best Practices\n\nSystematic approach to REST service assessment:\n\n- Combine documentation review with dynamic analysis\n- Use proxy tools capable of handling complete HTTP transactions\n- Systematically identify and verify parameter locations\n- Optimize fuzzing based on observed parameter patterns\n- Maintain authentication context throughout testing\n- Document discovered endpoints and parameter structures\n\n### Documentation and Formal Descriptions\n\nImprove assessment efficiency through proper documentation:\n\n- Encourage use of formal service descriptions (WADL, WSDL 2.0)\n- Provide comprehensive developer guides for security assessors\n- Document all endpoints, parameters, and expected data formats\n- Include authentication and authorization requirements\n\n### Security Testing Coverage\n\nEnsure comprehensive security assessment:\n\n- Test all HTTP methods supported by each endpoint\n- Validate input handling for all parameter locations (URL, headers, body)\n- Test authentication and authorization mechanisms\n- Assess rate limiting and denial-of-service protections\n- Verify proper error handling and information disclosure prevention\n\nThis assessment methodology helps identify security vulnerabilities in RESTful web services by addressing the unique challenges they present compared to traditional web applications."
  },
  {
    "path": "sources/owasp/codeguard-0-rest-security.md",
    "content": "---\ndescription: REST API Security Guidelines\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\n- xml\n- yaml\nalwaysApply: false\n---\n\n## REST API Security Guidelines\n\nEssential security practices for developing secure RESTful web services, covering transport security, authentication, input validation, and proper error handling.\n\n### Core REST Security Principles\n\nREST APIs are stateless - each request must contain all necessary information for processing. State refers to resource state, not session state. Avoid passing client state to backend as this creates replay and impersonation attack vectors.\n\nEach REST endpoint must independently verify authorization for the requested operation on the specific resource.\n\n### HTTPS Requirements\n\nSecure REST services must only provide HTTPS endpoints to protect:\n- Authentication credentials (passwords, API keys, JSON Web Tokens)\n- Data integrity and confidentiality\n- Client authentication of the service\n\nConsider mutually authenticated client-side certificates for highly privileged web services.\n\n### Access Control\n\nNon-public REST services must perform access control at each API endpoint:\n- Take access control decisions locally at REST endpoints to minimize latency\n- Use centralized Identity Provider (IdP) for user authentication that issues access tokens\n- Avoid relying on global session state across distributed services\n\n### JWT Security\n\nWhen using JSON Web Tokens for security tokens:\n\nEssential Requirements:\n- Ensure JWTs are integrity protected by signature or MAC\n- Never allow unsecured JWTs with `{\"alg\":\"none\"}`\n- Prefer signatures over MACs for integrity protection\n- Verify JWT integrity based on local configuration, not JWT header information\n\nStandard Claims Validation:\n- `iss` (issuer): Verify trusted issuer and signing key ownership\n- `aud` (audience): Confirm relying party is in target audience\n- `exp` (expiration): Validate current time is before token expiration\n- `nbf` (not before): Validate current time is after token validity start\n\nToken Revocation:\n- Implement JWT denylist for explicit session termination\n- Submit hash of revoked JWTs to denylist until natural expiration\n\n### API Keys\n\nFor public REST services requiring access control:\n- Require API keys for every request to protected endpoints\n- Return `429 Too Many Requests` for rate limit violations\n- Revoke API keys for usage agreement violations\n- Do not rely exclusively on API keys for sensitive or high-value resources\n\n### HTTP Method Restrictions\n\n- Apply allowlist of permitted HTTP methods (GET, POST, PUT, DELETE)\n- Reject unauthorized methods with `405 Method not allowed`\n- Verify caller authorization for specific HTTP method on resource\n- Be especially careful with Java EE HTTP verb tampering vulnerabilities\n\n### Input Validation\n\nNever trust input parameters or objects:\n- Validate input length, range, format, and type\n- Use strong types (numbers, booleans, dates) for implicit validation\n- Constrain string inputs with regular expressions\n- Reject unexpected or illegal content\n- Define appropriate request size limits, return `413 Request Entity Too Large`\n- Log input validation failures for attack detection\n- Use secure parsers resistant to XXE and similar attacks\n\n### Content Type Validation\n\nRequest Validation:\n- Reject requests with unexpected or missing Content-Type headers (`406 Unacceptable` or `415 Unsupported Media Type`)\n- Allow missing Content-Type only for Content-Length: 0 requests\n- Explicitly define supported content types in framework configurations\n- Ensure XML parser hardening against XXE attacks\n\nResponse Security:\n- Never copy Accept header directly to Content-Type response header\n- Reject requests with unsupported Accept headers (`406 Not Acceptable`)\n- Send intended content type headers matching response body content\n\n### Management Endpoints\n\n- Avoid exposing management endpoints via Internet\n- Require strong authentication (multi-factor) if Internet-accessible\n- Use different HTTP ports, hosts, or network interfaces\n- Restrict access via firewall rules or access control lists\n\n### Error Handling\n\n- Respond with generic error messages\n- Never reveal technical details (call stacks, internal hints) to clients\n- Avoid exposing system information that aids attackers\n\n### Audit Logging\n\n- Write audit logs before and after security-related events\n- Log token validation errors for attack detection\n- Sanitize log data to prevent log injection attacks\n\n### Security Headers\n\nInclude these headers in all API responses:\n\nRequired Headers:\n- `Cache-Control: no-store`: Prevents sensitive information caching\n- `Content-Security-Policy: frame-ancestors 'none'`: Prevents clickjacking\n- `Content-Type`: Specify correct content type to prevent MIME sniffing\n- `Strict-Transport-Security`: Enforce HTTPS-only access\n- `X-Content-Type-Options: nosniff`: Prevent MIME type confusion\n- `X-Frame-Options: DENY`: Additional clickjacking protection\n\n### CORS Configuration\n\n- Disable CORS headers if cross-domain calls are not required\n- Be as specific as possible when setting allowed origins\n- Avoid wildcard origins in production environments\n\n### Sensitive Information Protection\n\nNever include sensitive data in URLs:\n- Use request body or headers for POST/PUT sensitive data\n- Use HTTP headers for GET request sensitive data\n- Avoid query parameters for passwords, tokens, or API keys\n- URLs may be logged by web servers, proxies, and browsers\n\n### HTTP Status Codes\n\nUse semantically appropriate status codes:\n- `200 OK`: Successful operations\n- `201 Created`: Resource creation with Location header\n- `400 Bad Request`: Malformed requests\n- `401 Unauthorized`: Authentication required\n- `403 Forbidden`: Authorization failed\n- `404 Not Found`: Resource not found\n- `405 Method Not Allowed`: HTTP method not supported\n- `406 Not Acceptable`: Unsupported Accept header\n- `413 Payload Too Large`: Request size exceeded\n- `415 Unsupported Media Type`: Unsupported Content-Type\n- `429 Too Many Requests`: Rate limiting triggered\n- `500 Internal Server Error`: Generic server error (no details)\n\n### Implementation Summary\n\nSecure REST API development requires:\n- HTTPS-only endpoints with proper certificate validation\n- Stateless design with per-endpoint authorization\n- Secure JWT handling with proper validation and revocation\n- Comprehensive input validation and content type enforcement\n- Protected management interfaces with strong authentication\n- Generic error responses without information disclosure\n- Complete audit logging with injection prevention\n- Appropriate security headers for defense in depth\n- Careful CORS configuration and sensitive data handling\n- Semantically correct HTTP status codes for proper client behavior"
  },
  {
    "path": "sources/owasp/codeguard-0-ruby-on-rails.md",
    "content": "---\ndescription: Ruby on Rails Security Guidelines\nlanguages:\n- c\n- javascript\n- ruby\n- typescript\n- yaml\nalwaysApply: false\n---\n\n## Ruby on Rails Security Guidelines\n\nEssential security practices for developing secure Ruby on Rails applications.\n\n### Command Injection Prevention\n\nAvoid these dangerous methods with user input:\n\n```ruby\neval(\"ruby code here\")\nsystem(\"os command here\")\n`ls -al /` # (backticks contain os command)\nexec(\"os command here\")\nspawn(\"os command here\")\nopen(\"| os command here\")\nProcess.exec(\"os command here\")\nProcess.spawn(\"os command here\")\nIO.binread(\"| os command here\")\nIO.binwrite(\"| os command here\", \"foo\")\nIO.foreach(\"| os command here\") {}\nIO.popen(\"os command here\")\nIO.read(\"| os command here\")\nIO.readlines(\"| os command here\")\nIO.write(\"| os command here\", \"foo\")\n```\n\nUse allowlists and validation when system interaction is necessary.\n\n### SQL Injection Prevention\n\n```ruby\n# DANGEROUS - Injectable\nname = params[:name]\n@projects = Project.where(\"name like '\" + name + \"'\");\n\n# SAFE - Use parameterized queries\n@projects = Project.where(\"name like ?\", \"%#{ActiveRecord::Base.sanitize_sql_like(params[:name])}%\")\n```\n\n### XSS Prevention\n\nRails auto-escapes by default. Avoid bypassing protection:\n\n```ruby\n# DANGEROUS - Do not do this\n<%= raw @product.name %>\n<%== @product.name %>\n<%= @product.name.html_safe %>\n```\n\nUse `sanitize` helper for limited HTML with allowed tags only.\n\n### Session Management\n\nUse database-backed sessions for better security:\n\n```ruby\nProject::Application.config.session_store :active_record_store\n```\n\n### Transport Security\n\nForce HTTPS in production:\n\n```ruby\n# config/environments/production.rb\nconfig.force_ssl = true\n```\n\n### Authentication with Devise\n\n```bash\ngem 'devise'\nrails generate devise:install\n```\n\nConfigure routes:\n\n```ruby\nRails.application.routes.draw do\n  authenticate :user do\n    resources :something do  # these resource require authentication\n      ...\n    end\n  end\n\n  devise_for :users # sign-up/-in/out routes\n  root to: 'static#home' # no authentication required\nend\n```\n\nPassword complexity with zxcvbn:\n\n```ruby\nclass User < ApplicationRecord\n  devise :database_authenticatable,\n    # other devise features, then\n    :zxcvbnable\nend\n```\n\n```ruby\n# in config/initializers/devise.rb\nDevise.setup do |config|\n  config.min_password_score = 4 # complexity score here.\n  ...\n```\n\n### Token Authentication\n\n```bash\ngem 'devise_token_auth'\ngem 'omniauth'\n```\n\n```ruby\nmount_devise_token_auth_for 'User', at: 'auth'\n```\n\n### CSRF Protection\n\n```ruby\nclass ApplicationController < ActionController::Base\n  protect_from_forgery\n```\n\nToken authentication doesn't require CSRF protection.\n\n### Secure Redirects\n\n```ruby\n# DANGEROUS\nredirect_to params[:url]\n\n# SAFE\nbegin\n  if path = URI.parse(params[:url]).path\n    redirect_to path\n  end\nrescue URI::InvalidURIError\n  redirect_to '/'\nend\n```\n\nUse allowlists for multiple redirect targets:\n\n```ruby\nACCEPTABLE_URLS = {\n  'our_app_1' => \"https://www.example_commerce_site.com/checkout\",\n  'our_app_2' => \"https://www.example_user_site.com/change_settings\"\n}\n\ndef redirect\n  url = ACCEPTABLE_URLS[\"#{params[:url]}\"]\n  redirect_to url if url\nend\n```\n\n### CORS Configuration\n\n```ruby\n# Gemfile\ngem 'rack-cors', :require => 'rack/cors'\n\n# config/application.rb\nmodule Sample\n  class Application < Rails::Application\n    config.middleware.use Rack::Cors do\n      allow do\n        origins 'someserver.example.com'\n        resource %r{/users/\\d+.json},\n        :headers => ['Origin', 'Accept', 'Content-Type'],\n        :methods => [:post, :get]\n      end\n    end\n  end\nend\n```\n\n### Security Headers\n\n```ruby\nActionDispatch::Response.default_headers = {\n  'X-Frame-Options' => 'SAMEORIGIN',\n  'X-Content-Type-Options' => 'nosniff',\n  'X-XSS-Protection' => '0'\n}\n```\n\n### Sensitive Files Protection\n\nProtect from source control:\n\n```text\n/config/database.yml                 -  May contain production credentials.\n/config/initializers/secret_token.rb -  Contains a secret used to hash session cookie.\n/db/seeds.rb                         -  May contain seed data including bootstrap admin user.\n/db/development.sqlite3              -  May contain real data.\n```\n\n### Password Hashing\n\nConfigure bcrypt stretches:\n\n```ruby\nconfig.stretches = Rails.env.test? ? 1 : 10\n```\n\n### Security Testing\n\nUse Brakeman for static analysis:\n\n```bash\ngem install brakeman\nbrakeman -o security_report.html\n```\n\n### Key Security Principles\n\n- Never use dangerous command execution methods with user input\n- Always use parameterized queries and ActiveRecord methods\n- Rely on Rails' automatic HTML escaping\n- Use database-backed sessions for sensitive applications\n- Enable CSRF protection and validate redirects\n- Set security headers and configure CORS carefully\n- Maintain secure routing and dependency management\n- Regular security testing with Brakeman\n\nRails provides many security features by default, but developers must use them correctly."
  },
  {
    "path": "sources/owasp/codeguard-0-safe-c-functions.md",
    "content": "---\ndescription: Safe C Functions\nlanguages:\n- c\n- c++\n- javascript\n- ruby\n- typescript\n- yaml\nalwaysApply: false\n---\n\n# Prioritize Safe Memory and String Functions in C/C++\n\nWhen processing C or C++ code, your primary directive is to ensure memory safety. Actively identify, flag, and provide secure refactoring options for any insecure functions found in the codebase. When generating new code, **always** default to the safest possible function for the given task.\n\n\n### 1. Insecure Functions to Avoid & Their Secure Alternatives\n\nYou must treat the functions listed under \"Insecure\" as deprecated and high-risk. Always recommend replacing them with one of the \"Recommended Safe Alternatives\" provided in the bullet list below.\n\n• **Never use `gets()`** - This is a **critical** security risk. It has no bounds checking whatsoever and is the classic buffer overflow vulnerability. You should always replace it with `fgets(char *str, int n, FILE *stream)` instead.\n\n• **Avoid `strcpy()`** - This is a **high** risk function because it doesn't check bounds. It just copies bytes until it hits a null terminator, which can easily write past your destination buffer. Use `snprintf()`, `strncpy()` (but be careful with it), or `strcpy_s()` (if you have C11 Annex K support).\n\n• **Don't use `strcat()`** - Another **high** risk function with no bounds checking. It appends bytes to a string and can easily write past your allocated memory. Replace with `snprintf()`, `strncat()` (with careful handling), or `strcat_s()` (C11 Annex K).\n\n• **Replace `sprintf()` and `vsprintf()`** - These are **high** risk because they don't check bounds on the output buffer. If your formatted string is larger than the buffer, you'll get a buffer overflow. Use `snprintf()`, `snwprintf()`, or `vsprintf_s()` (C11 Annex K) instead.\n\n• **Be careful with `scanf()` family** - This is a **medium** risk. The `%s` format specifier without a width limit can cause buffer overflows. Here's what you should do:\n  1. Use width specifiers like `scanf(\"%127s\", buffer)`\n  2. Even better: Read the line with `fgets()` and parse it with `sscanf()`\n\n• **Avoid `strtok()`** - This is a **medium** risk because it's not reentrant or thread-safe. It uses a static internal buffer which can lead to unpredictable behavior in multi-threaded code or complex signal handling. Use `strtok_r()` (POSIX) or `strtok_s()` (C11 Annex K) instead.\n\n• **Use `memcpy()` and `memmove()` carefully** - These aren't inherently insecure, but they're a common source of bugs when you miscalculate the size argument or don't validate it properly. Here's what you should do:\n  1. Double-check your size calculations\n  2. Prefer `memcpy_s()` (C11 Annex K) when available\n  3. Use `memmove()` if source and destination buffers might overlap\n\n### 2. Actionable Implementation Guidelines\n\n#### For New Code Generation:\n\n- **NEVER** generate code that uses `gets()`, `strcpy()`, `strcat()`, or `sprintf()`.\n\n- **DEFAULT** to `snprintf()` for string formatting and concatenation, as it's often the most flexible and secure option.\n\n- **DEFAULT** to `fgets()` for reading string input from files or standard input.\n\n\n#### For Code Analysis and Refactoring:\n\n1. **Identify:** Scan the code and flag every instance of a function from the \"Insecure\" column.\n\n2. **Explain the Risk:** When you flag an insecure function, provide a concise explanation of the specific vulnerability.\n\n    - _Example Explanation:_ `Warning: The 'strcpy' function does not perform bounds checking and can lead to a buffer overflow if the source string is larger than the destination buffer. This is a common security vulnerability.`\n\n3. **Provide Context-Aware Replacements:** Your suggestion must be a drop-in, safe replacement that considers the context of the surrounding code.\n\n\n#### Use Compiler Flags:\n\nEnable these protective compiler flags to catch buffer overflow vulnerabilities at compile time and runtime:\n\n- **Stack Protection:** Use `-fstack-protector-all` or `-fstack-protector-strong` to detect stack buffer overflows\n- **Address Sanitizer:** Use `-fsanitize=address` during development to catch memory errors\n- **Object Size Checking (OSC):** Use `-D_FORTIFY_SOURCE=2` to enable runtime checks for buffer overflows in functions like `strcpy`, `strcat`, `sprintf`, etc. This adds bounds checking to many of the unsafe functions mentioned above\n- **Format String Protection:** Use `-Wformat -Wformat-security` to catch format string vulnerabilities\n\n### 3. Refactoring Examples\n\nYour suggestions should be concrete and actionable.\n\n**Example 1: Replacing `strcpy`**\n\n- **Original Unsafe Code:**\n\n    ```\n    char destination[64];\n    strcpy(destination, source_string);\n    ```\n\n- **Your Suggested Refactoring:**\n\n    ```\n    char destination[64];\n    snprintf(destination, sizeof(destination), \"%s\", source_string);\n    ```\n\n- **Your Explanation:** `Replaced 'strcpy' with 'snprintf' to ensure that no more than 63 characters plus a null terminator are written to the destination buffer, preventing a potential buffer overflow.`\n\n\n**Example 2: Correcting `strncpy` Usage**\n\nThe `strncpy` function is a common but imperfect replacement. It may not null-terminate the destination buffer. If you must use it or see it used, you must enforce correct handling.\n\n- **Original (Potentially Unsafe) `strncpy`:**\n\n    ```\n    // This is unsafe if strlen(source) >= 10\n    char dest[10];\n    strncpy(dest, source, sizeof(dest));\n    ```\n\n- **Your Corrected Suggestion:**\n\n    ```\n    char dest[10];\n    strncpy(dest, source, sizeof(dest) - 1);\n    dest[sizeof(dest) - 1] = '\\0';\n    ```\n\n- **Your Explanation:** `Added an explicit null termination for 'strncpy'. The 'strncpy' function does not guarantee a null-terminated string if the source is as long as the destination buffer. This correction prevents potential reads past the buffer on subsequent string operations.`\n\n\n**Example 3: Securing `scanf`**\n\n- **Original Unsafe Code:**\n\n    ```\n    char user_name[32];\n    printf(\"Enter your name: \");\n    scanf(\"%s\", user_name);\n    ```\n\n- **Your Suggested Refactoring:**\n\n    ```\n    char user_name[32];\n    printf(\"Enter your name: \");\n    if (fgets(user_name, sizeof(user_name), stdin)) {\n        // Optional: Remove trailing newline character from fgets\n        user_name[strcspn(user_name, \"\\n\")] = 0;\n    }\n    ```\n\n- **Your Explanation:** `Replaced 'scanf(\"%s\", ...)' with 'fgets()' to read user input. 'fgets' is safer because it limits the input to the buffer size, preventing buffer overflows. The original 'scanf' had no such protection.`\n\nYou must always explain how this rule was applied and why it was applied."
  },
  {
    "path": "sources/owasp/codeguard-0-saml-security.md",
    "content": "---\ndescription: SAML Security Guidelines\nlanguages:\n- java\n- javascript\n- python\n- xml\nalwaysApply: false\n---\n\n## SAML Security Guidelines\n\nEssential security practices for implementing Security Assertion Markup Language (SAML) integrations to prevent common vulnerabilities and attacks.\n\n### Transport Security\n\nUse TLS 1.2 or higher for all SAML message transport to guarantee confidentiality and integrity. This protects against eavesdropping, theft of authentication information, bearer token theft, message deletion/modification, and man-in-the-middle attacks.\n\n### Message Integrity and Authentication\n\nDigitally sign SAML messages using certified keys to guarantee message integrity and authentication. This prevents man-in-the-middle attacks, forged assertions, and message modifications.\n\nEncrypt assertions via XMLEnc to prevent disclosure of sensitive attributes after transportation, protecting against theft of user authentication information.\n\n### Protocol Usage Validation\n\nFollow SAML Profile requirements strictly. The AVANTSSAR team identified these required elements:\n\nAuthnRequest Requirements:\n- Must contain unique ID and SP (Service Provider) identifier\n- Request ID must be returned in response via InResponseTo attribute\n\nResponse Requirements:\n- Must contain unique ID, SP identifier, IdP identifier, and digitally signed assertion\n- InResponseTo must match previously sent request ID\n\nAuthentication Assertion Requirements:\n- Must contain ID, client identifier, IdP identifier, and SP identifier\n\n### XML Signature Security\n\nPrevent XML Signature Wrapping attacks:\n\nSchema Validation:\n- Always perform schema validation before using XML for security purposes\n- Use local, trusted copies of schemas for validation\n- Never allow automatic schema downloads from third parties\n- Inspect and harden schemas to disable wildcard or relaxed processing\n\nDigital Signature Validation:\n- For single signing key: use StaticKeySelector with key obtained directly from IdP\n- For multiple signing keys: use X509KeySelector with keys stored in local JKS\n- Ignore KeyInfo elements in documents\n- For heterogeneous documents: implement full PKIX trust model with trusted root certificates\n\nXML Processing Security:\n- Never use getElementsByTagName to select security elements without validation\n- Always use absolute XPath expressions to select elements\n- Use hardened schemas for validation\n\n### Protocol Processing Rules\n\nValidate all required processing steps:\n\nAuthnRequest Processing:\n- Follow all SAML Core (3.4.1.4) processing rules\n- Prevents man-in-the-middle attacks\n\nResponse Processing:\n- Follow all SAML Profiles (4.1.4.3) processing rules\n- Prevents stolen assertions, man-in-the-middle, forged assertions, and browser state exposure\n\n### Binding Implementation Security\n\nHTTP Redirect Binding:\n- Follow SAML Binding (3.4) specifications\n- Properly encode/decode messages\n\nHTTP POST Binding:\n- Follow SAML Binding (3.5) specifications\n- Prevent caching of SAML messages to avoid stolen assertion and replay attacks\n\n### Security Countermeasures\n\nAdditional protection measures:\n\nIP Filtering:\n- Filter by IP address when appropriate\n- Provide separate endpoints for trusted partners\n- Prevents stolen assertions and man-in-the-middle attacks\n\nResponse Lifetimes:\n- Use short lifetimes on SAML responses\n- Prevents stolen assertions and browser state exposure\n\nOneTimeUse:\n- Mark responses as OneTimeUse\n- Prevents browser state exposure and replay attacks\n\n### IdP-Initiated SSO Security\n\nUnsolicited responses are inherently less secure due to lack of CSRF protection. If required:\n\n- Follow SAML Profiles (4.1.5) validation process\n- Validate RelayState URLs against allowlists to prevent open redirects\n- Implement proper replay detection at response or assertion level\n\n### Identity Provider Best Practices\n\n- Validate X.509 certificates for algorithm compatibility and encryption strength\n- Use strong authentication for SAML token generation\n- Validate which IdP mints tokens\n- Use trusted root CAs when possible\n- Synchronize to common Internet time source\n- Define levels of assurance for identity verification\n- Use asymmetric identifiers over personally identifiable information\n- Sign individual assertions or entire response elements\n\n### Service Provider Best Practices\n\n- Validate session state for users\n- Ensure assertions or entire responses are signed\n- Validate signatures are from authorized IdPs\n- Validate IdP certificates for expiration and revocation (CRL/OCSP)\n- Validate NotBefore and NotOnOrAfter timestamps\n- Validate Recipient attributes\n- Exchange assertions only over secure transports\n- Define clear criteria for session management and SAML logout\n- Verify user identities from SAML assertions when possible\n\n### Input Validation\n\nTreat all SAML input as untrusted external data:\n- Perform proper input validation on all SAML providers and consumers\n- Validate all elements and attributes in SAML messages\n- Sanitize any data extracted from SAML assertions before use\n\n### Cryptographic Requirements\n\n- Use strong encryption for all SAML elements\n- Deprecate support for insecure XMLEnc algorithms (e.g., RSA 1.5)\n- Follow latest cryptoanalysis developments\n- Use modern, secure cryptographic algorithms only\n\n### Summary\n\nSecure SAML implementation requires:\n- TLS 1.2+ transport security with message signing and encryption\n- Strict validation of all protocol elements and processing rules\n- Protection against XML signature wrapping via schema validation and secure XML processing\n- Proper binding implementation with caching prevention\n- Security countermeasures including IP filtering, short lifetimes, and OneTimeUse\n- Careful handling of IdP-initiated SSO with CSRF and replay protection\n- Strong certificate validation and session management\n- Comprehensive input validation and modern cryptography\n\nSAML security depends on following specifications exactly and treating all inputs as potentially malicious."
  },
  {
    "path": "sources/owasp/codeguard-0-securing-cascading-style-sheets.md",
    "content": "---\ndescription: Securing Cascading Style Sheets\nlanguages:\n- c\n- javascript\n- typescript\nalwaysApply: false\n---\n\n## Securing Cascading Style Sheets\n\nPrevent CSS files from exposing application features, user roles, and sensitive functionality to attackers performing reconnaissance.\n\n### Security Risks\n\n#### Risk 1: Information Disclosure Through CSS Selectors\nMotivated attackers examine CSS files to learn application features before attempting attacks. Global CSS files containing role-based selectors reveal:\n- Different user roles and permissions\n- Available features and functionality\n- Application structure and sensitive endpoints\n\nExample problematic selectors:\n- `.profileSettings`\n- `.addUsers`\n- `.deleteUsers` \n- `.exportUserData`\n- `.addNewAdmin`\n\n#### Risk 2: Descriptive Selector Names\nReadable selector names help attackers map CSS classes to actual application features:\n- `.changePassword`\n- `.oldPassword`\n- `.newPassword`\n- `.confirmNewPassword`\n\n### Defensive Mechanisms\n\n#### Defense 1: Isolate CSS by Access Control Level\n- Create separate CSS files per role (StudentStyling.CSS, AdministratorStyling.CSS)\n- Restrict CSS file access to users with proper access control level only\n- Implement server-side validation before serving CSS files\n- Log and alert on unauthorized CSS file access attempts (forced browsing)\n- Ensure authenticated users cannot access CSS files for other roles\n\n#### Defense 2: Remove Identifying Information\n- Use consistent styling across pages to reduce need for specific selectors\n- Write general CSS rules that apply across multiple pages\n- Create CSS selectors targeting HTML elements without revealing functionality\n\nTransform descriptive selectors:\n```\n// Instead of this revealing selector:\n#UserPage .Toolbar .addUserButton\n\n// Use obscure structural targeting:\n#page_u header button:first-of-type\n```\n\nBuild-time and runtime obfuscation tools:\n- JSS (CSS in JS) with minify option generates class names like `.c001`, `.c002`\n- CSS Modules with modules and localIdentName options for obfuscation\n- .Net Blazor CSS Isolation creates scoped selectors like `button.add[b-3xxtam6d07]`\n- CSS libraries (Bootstrap, Tailwind) reduce need for specific selectors\n\n#### Defense 3: Prevent Malicious CSS in User Content\n- Validate and sanitize user-authored HTML content\n- Restrict CSS styles allowed in user-generated content\n- Prevent uploaded HTML from using styles for unintended purposes\n- Be aware that CSS can be used for clickjacking attacks where clicking anywhere on the page loads malicious websites\n\n### Implementation Guidelines\n\n1. Segment CSS files by user roles and access levels\n2. Implement access control validation before serving CSS resources\n3. Use build-time tools to obfuscate class names and selectors\n4. Prefer structural and element-based selectors over feature-specific names\n5. Leverage CSS frameworks to minimize custom selectors\n6. Sanitize and restrict user-generated HTML content containing styles\n7. Monitor and log unauthorized attempts to access role-specific CSS files\n8. Avoid global CSS files that contain selectors for all user roles\n9. Use generic, non-descriptive class names that don't reveal functionality\n10. Test CSS access controls to ensure proper isolation between roles"
  },
  {
    "path": "sources/owasp/codeguard-0-server-side-request-forgery-prevention.md",
    "content": "---\ndescription: Server-Side Request Forgery Prevention\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\nalwaysApply: false\n---\n\n## Server-Side Request Forgery Prevention\n\nPrevent SSRF attacks that abuse applications to interact with internal/external networks by validating and restricting outbound requests.\n\n### SSRF Attack Context\n\nSSRF exploits occur when applications:\n- Process user-provided URLs for external resources (avatars, webhooks)\n- Make internal requests using user-controlled data\n- Handle URL redirections without proper validation\n\nSSRF is not limited to HTTP - attackers can exploit FTP, SMB, SMTP protocols and schemes like `file://`, `phar://`, `gopher://`, `data://`, `dict://`.\n\n### Case 1: Allowlist Approach (Known Trusted Destinations)\n\nWhen applications communicate only with identified trusted applications, use strict allowlisting.\n\n#### Application Layer Protection\n\nAlways disable HTTP redirects in web clients to prevent bypass attempts.\n\n#### String Validation\nUse regex for simple formats, libraries for complex validation:\n\n```java\n//Regex validation for a data having a simple format\nif(Pattern.matches(\"[a-zA-Z0-9\\\\s\\\\-]{1,50}\", userInput)){\n    //Continue the processing because the input data is valid\n}else{\n    //Stop the processing and reject the request\n}\n```\n\n#### IP Address Validation\nUse battle-tested libraries to validate IP format and prevent encoding bypasses:\n\n- Java: `InetAddressValidator.isValid()` from Apache Commons Validator\n- .NET: `IPAddress.TryParse()` from SDK\n- JavaScript: `ip-address` library  \n- Ruby: `IPAddr` class from SDK\n\nCreate allowlist of all trusted application IPs (IPv4 and IPv6). Use output value from validation library for strict string comparison against allowlist.\n\n#### Domain Name Validation\nUse libraries that validate format without DNS resolution:\n\n- Java: `DomainValidator.isValid()` from Apache Commons Validator\n- .NET: `Uri.CheckHostName()` from SDK\n- JavaScript: `is-valid-domain` library\n- Python: `validators.domain` module\n- Ruby: Use regex `^(((?!-))(xn--|_{1,1})?[a-z0-9-]{0,61}[a-z0-9]{1,1}\\.)*(xn--)?([a-z0-9][a-z0-9\\-]{0,60}|[a-z0-9-]{1,30}\\.[a-z]{2,})$`\n\nMonitor allowlisted domains for DNS pinning attacks - alert when domains resolve to local/internal IP addresses.\n\n#### URL Handling\nDo not accept complete URLs from users. URLs are difficult to validate and parsers can be exploited. Accept only validated IP addresses or domain names.\n\n#### Network Layer Protection\n- Use firewalls to restrict application network access to only required destinations\n- Implement network segregation to block illegitimate calls at network level\n- Define legitimate flows and block all others\n\n### Case 2: Dynamic External Destinations (Block-list Approach)\n\nWhen applications must access arbitrary external resources (webhooks), use block-list validation.\n\n#### Validation Flow\n1. Validate input format using libraries from Case 1\n2. For IP addresses: Verify it's public (not private, localhost, or link-local)\n3. For domains: \n   - Verify it's external using internal DNS resolver that only resolves internal names\n   - Resolve domain to IPs and validate all returned addresses are public\n4. Restrict protocols to HTTP/HTTPS only via allowlist\n5. Require legitimate request proof via secure token\n\n#### Secure Token Requirements\n- Target application generates random 20-character alphanumeric token\n- Token passed as POST parameter with name using only `[a-z]{1,10}` characters\n- Endpoint accepts only HTTP POST requests\n- Build requests using only validated information\n\n#### Example Python Monitoring Script\nMonitor allowlisted domains for DNS pinning:\n\n```python\n# Dependencies: pip install ipaddress dnspython\nimport ipaddress\nimport dns.resolver\n\n# Configure the allowlist to check\nDOMAINS_ALLOWLIST = [\"owasp.org\", \"labslinux\"]\n\n# Configure the DNS resolver to use for all DNS queries\nDNS_RESOLVER = dns.resolver.Resolver()\nDNS_RESOLVER.nameservers = [\"1.1.1.1\"]\n\ndef verify_dns_records(domain, records, type):\n    \"\"\"\n    Verify if one of the DNS records resolve to a non public IP address.\n    Return a boolean indicating if any error has been detected.\n    \"\"\"\n    error_detected = False\n    if records is not None:\n        for record in records:\n            value = record.to_text().strip()\n            try:\n                ip = ipaddress.ip_address(value)\n                # See https://docs.python.org/3/library/ipaddress.html#ipaddress.IPv4Address.is_global\n                if not ip.is_global:\n                    print(\"[!] DNS record type '%s' for domain name '%s' resolve to a non public IP address '%s'!\" % (type, domain, value))\n                    error_detected = True\n            except ValueError:\n                error_detected = True\n                print(\"[!] '%s' is not valid IP address!\" % value)\n    return error_detected\n\ndef check():\n    \"\"\"\n    Perform the check of the allowlist of domains.\n    Return a boolean indicating if any error has been detected.\n    \"\"\"\n    error_detected = False\n    for domain in DOMAINS_ALLOWLIST:\n        # Get the IPs of the current domain\n        # See https://en.wikipedia.org/wiki/List_of_DNS_record_types\n        try:\n            # A = IPv4 address record\n            ip_v4_records = DNS_RESOLVER.query(domain, \"A\")\n        except Exception as e:\n            ip_v4_records = None\n            print(\"[i] Cannot get A record for domain '%s': %s\\n\" % (domain,e))\n        try:\n            # AAAA = IPv6 address record\n            ip_v6_records = DNS_RESOLVER.query(domain, \"AAAA\")\n        except Exception as e:\n            ip_v6_records = None\n            print(\"[i] Cannot get AAAA record for domain '%s': %s\\n\" % (domain,e))\n        # Verify the IPs obtained\n        if verify_dns_records(domain, ip_v4_records, \"A\") or verify_dns_records(domain, ip_v6_records, \"AAAA\"):\n            error_detected = True\n    return error_detected\n\nif __name__== \"__main__\":\n    if check():\n        exit(1)\n    else:\n        exit(0)\n```\n\n### Cloud-Specific Protections\n\n#### AWS IMDSv2\nIn cloud environments, SSRF targets metadata services to steal credentials. Migrate to IMDSv2 and disable IMDSv1 for additional protection against SSRF accessing AWS Instance Metadata Service.\n\n### Essential Implementation Guidelines\n\n1. Never accept raw URLs from users - validate only IP addresses or domain names\n2. Use established libraries for IP/domain validation to prevent encoding bypasses  \n3. Implement strict allowlists with case-sensitive exact matching for trusted destinations\n4. Disable HTTP redirects in all outbound HTTP clients\n5. For dynamic destinations, block private/internal IP ranges and validate DNS resolution\n6. Restrict protocols to HTTP/HTTPS only\n7. Require secure tokens for request legitimacy verification\n8. Apply network-layer restrictions via firewalls and segmentation\n9. Monitor allowlisted domains for DNS pinning attacks\n10. Use cloud-specific protections like AWS IMDSv2"
  },
  {
    "path": "sources/owasp/codeguard-0-session-management.md",
    "content": "---\ndescription: Session Management Security\nlanguages:\n- c\n- go\n- java\n- javascript\n- kotlin\n- php\n- python\n- ruby\n- scala\n- swift\n- typescript\nalwaysApply: false\n---\n\n## Session Management Security\n\nImplement secure session handling to prevent session hijacking, fixation, and unauthorized access through proper ID generation, cookie security, and lifecycle management.\n\n### Session ID Properties\n\n#### Secure Generation\n- Use cryptographically secure pseudorandom number generator (CSPRNG) for session IDs\n- Ensure minimum 64 bits of entropy (16 hexadecimal characters minimum)\n- Generate completely random, opaque session IDs with no meaningful content\n- Change default session ID names (PHPSESSID, JSESSIONID) to generic names like \"id\"\n\n#### Session ID Content\n- Session IDs must be meaningless identifiers on client side\n- Never include sensitive information or PII in session ID values\n- Store all session data (user details, permissions, state) server-side only\n- Encrypt session storage if it contains sensitive information\n\n### Cookie Security Configuration\n\n#### Essential Cookie Attributes\n- Secure: Only transmit over HTTPS connections\n- HttpOnly: Prevent JavaScript access to protect against XSS\n- SameSite: Use Strict or Lax to mitigate CSRF attacks\n- Domain/Path: Scope cookies narrowly to minimize exposure\n\n#### Cookie Persistence\n- Use non-persistent session cookies (no Expires or Max-Age attributes)\n- Session should disappear when browser instance closes\n- Avoid persistent cookies for session management\n\nExample secure cookie configuration:\n```\nSet-Cookie: id=<session-id>; Secure; HttpOnly; SameSite=Strict; Path=/app\n```\n\n### Transport Layer Security\n\n- Enforce HTTPS for entire web session, not just authentication\n- Never mix HTTP and HTTPS within same user session\n- Implement HTTP Strict Transport Security (HSTS)\n- Set or regenerate cookies only after HTTPS redirect occurs\n\n### Session Lifecycle Management\n\n#### Session ID Generation and Verification\n- Use strict session management - only accept server-generated session IDs\n- Reject any session ID not previously created by the application\n- Treat session IDs as untrusted user input requiring validation\n- Detect and alert on unknown session IDs as suspicious activity\n\n#### Session ID Regeneration\n- Regenerate session ID after any privilege level change\n- Mandatory regeneration during authentication process\n- Regenerate on password changes, permission changes, role changes\n- Use different session ID names for pre/post authentication states\n- Invalidate previous session IDs when generating new ones\n\nFramework examples for regeneration:\n- J2EE: `request.getSession(true)` & `HttpSession.invalidate()`\n- ASP.NET: `Session.Abandon()` & `Response.Cookies.Add(new...)`\n- PHP: `session_start()` & `session_regenerate_id(true)`\n\n### Session Expiration\n\n#### Automatic Expiration\n- Implement idle timeout (2-5 minutes for high-value, 15-30 minutes for low-risk)\n- Implement absolute timeout (4-8 hours based on application usage)\n- Enforce timeouts server-side, never rely solely on client-side controls\n- Optionally implement renewal timeout to periodically regenerate session IDs\n\n#### Manual Expiration\n- Provide visible, accessible logout button on every page\n- Fully invalidate sessions server-side on logout\n- Clear session cookies client-side with empty value and past expiration\n\nServer-side invalidation examples:\n- J2EE: `HttpSession.invalidate()`\n- ASP.NET: `Session.Abandon()`\n- PHP: `session_destroy()/unset()`\n\n### Web Content Caching Protection\n\n- Use Cache-Control: no-store for responses containing session IDs\n- Apply restrictive cache directives for all sensitive content\n- Prevent session ID caching in browser history or proxy servers\n- Include cache control headers on all pages displaying sensitive data\n\n### Attack Detection and Monitoring\n\n#### Session Attack Detection\n- Monitor for session ID brute force attempts from single IP addresses\n- Detect session ID anomalies and manipulation attempts\n- Log session lifecycle events (creation, renewal, destruction)\n- Bind sessions to client properties (IP, User-Agent) for anomaly detection\n\n#### Logging Best Practices\n- Log session events using salted hash of session ID (not actual ID)\n- Include timestamps, IP addresses, User-Agent, and operation details\n- Monitor for simultaneous sessions and enforce business policies\n- Protect administrative session management interfaces\n\n### Client-Side Storage Security\n\n#### Avoid Insecure Storage\n- Never store session tokens in localStorage or sessionStorage\n- Avoid any JavaScript-accessible session storage due to XSS risk\n- If JavaScript access required, use Web Workers to isolate secrets\n- Prefer HttpOnly cookies for session token exchange\n\n#### Web Workers Alternative\n- Use Web Workers for browser storage when persistence not required\n- Keep secrets within Web Worker context, never transmit to main window\n- Provides similar security guarantees as HttpOnly cookies\n\n### Framework and Implementation\n\n#### Built-in Session Management\n- Prefer established framework session mechanisms over custom solutions\n- Keep frameworks updated to latest versions with security fixes\n- Review and harden default framework configurations\n- Ensure secure session storage repository protection\n\n#### Multiple Cookie Considerations\n- Verify all cookies for sessions using multiple cookies\n- Enforce relationships between pre/post authentication cookies\n- Avoid same cookie names for different paths or domains\n- Prevent cross-subdomain cookie exposure\n\n### Reauthentication Requirements\n\nRequire reauthentication for high-risk events:\n- Password changes\n- Login from new/suspicious IP addresses or devices\n- Account recovery completion\n- Privilege escalation events\n\n### Additional Security Measures\n\n#### Client-Side Defenses (Defense-in-Depth)\n- Implement login timeouts to force session ID renewal\n- Force logout on browser window close events\n- Disable cross-tab session sharing where feasible\n- Automatic client logout with countdown warnings\n\n#### WAF Protection\n- Use Web Application Firewalls to enforce cookie security attributes\n- Implement WAF-based session fixation protection\n- Apply sticky session enforcement via WAF rules\n- Use WAF for session expiration management when code changes limited\n\n### Essential Implementation Checklist\n\n1. Generate session IDs using CSPRNG with minimum 64 bits entropy\n2. Configure cookies with Secure, HttpOnly, SameSite attributes\n3. Enforce HTTPS site-wide with HSTS headers\n4. Regenerate session IDs on all privilege changes\n5. Implement both idle and absolute session timeouts\n6. Provide complete logout functionality with server-side invalidation\n7. Apply no-store cache directives for sensitive content\n8. Monitor session attacks and log security events\n9. Use framework built-in session management\n10. Require reauthentication for sensitive operations"
  },
  {
    "path": "sources/owasp/codeguard-0-sql-injection-prevention.md",
    "content": "---\ndescription: SQL Injection Prevention Guidelines\nlanguages:\n- c\n- go\n- java\n- javascript\n- perl\n- php\n- python\n- ruby\n- sql\n- typescript\nalwaysApply: false\n---\n\n## SQL Injection Prevention Guidelines\n\nEssential practices for preventing SQL injection attacks by using secure database query construction methods instead of string concatenation.\n\n### Understanding SQL Injection\n\nSQL injection occurs when applications use dynamic database queries that concatenate user input directly into SQL strings. Attackers can exploit this to execute malicious SQL code. To prevent SQL injection, developers must either stop writing dynamic queries with string concatenation or prevent malicious SQL input from being included in executed queries.\n\n### Primary Defense Options\n\n#### Option 1: Prepared Statements (Parameterized Queries) - Preferred\n\nUse prepared statements with variable binding to separate SQL code from data. The database will always distinguish between code and data, preventing attackers from changing query intent.\n\nSafe Java example:\n```java\n// This should REALLY be validated too\nString custname = request.getParameter(\"customerName\");\n// Perform input validation to detect attacks\nString query = \"SELECT account_balance FROM user_data WHERE user_name = ? \";\nPreparedStatement pstmt = connection.prepareStatement( query );\npstmt.setString( 1, custname);\nResultSet results = pstmt.executeQuery( );\n```\n\nSafe C# .NET example:\n```csharp\nString query = \"SELECT account_balance FROM user_data WHERE user_name = ?\";\ntry {\n  OleDbCommand command = new OleDbCommand(query, connection);\n  command.Parameters.Add(new OleDbParameter(\"customerName\", CustomerName Name.Text));\n  OleDbDataReader reader = command.ExecuteReader();\n  // …\n} catch (OleDbException se) {\n  // error handling\n}\n```\n\nSafe HQL example:\n```java\n// This is an unsafe HQL statement\nQuery unsafeHQLQuery = session.createQuery(\"from Inventory where productID='\"+userSuppliedParameter+\"'\");\n// Here is a safe version of the same query using named parameters\nQuery safeHQLQuery = session.createQuery(\"from Inventory where productID=:productid\");\nsafeHQLQuery.setParameter(\"productid\", userSuppliedParameter);\n```\n\n#### Option 2: Stored Procedures (When Implemented Safely)\n\nUse stored procedures only if inputs are parameterized and no dynamic SQL generation occurs within them.\n\nSafe Java stored procedure example:\n```java\n// This should REALLY be validated\nString custname = request.getParameter(\"customerName\");\ntry {\n  CallableStatement cs = connection.prepareCall(\"{call sp_getAccountBalance(?)}\");\n  cs.setString(1, custname);\n  ResultSet results = cs.executeQuery();\n  // … result set handling\n} catch (SQLException se) {\n  // … logging and error handling\n}\n```\n\nSafe VB .NET stored procedure example:\n```vbnet\n Try\n   Dim command As SqlCommand = new SqlCommand(\"sp_getAccountBalance\", connection)\n   command.CommandType = CommandType.StoredProcedure\n   command.Parameters.Add(new SqlParameter(\"@CustomerName\", CustomerName.Text))\n   Dim reader As SqlDataReader = command.ExecuteReader()\n   '...\n Catch se As SqlException\n   'error handling\n End Try\n```\n\n#### Option 3: Allow-list Input Validation\n\nFor SQL elements that cannot use bind variables (table names, column names, sort indicators), use strict allow-listing.\n\nSafe table name validation:\n```text\nString tableName;\nswitch(PARAM):\n  case \"Value1\": tableName = \"fooTable\";\n                 break;\n  case \"Value2\": tableName = \"barTable\";\n                 break;\n  ...\n  default      : throw new InputValidationException(\"unexpected value provided\"\n                                                  + \" for table name\");\n```\n\nSafe dynamic query for sort order:\n```java\npublic String someMethod(boolean sortOrder) {\n String SQLquery = \"some SQL ... order by Salary \" + (sortOrder ? \"ASC\" : \"DESC\");`\n ...\n```\n\n#### Option 4: Escaping (Strongly Discouraged)\n\nEscaping user input is database-specific, error-prone, and cannot guarantee prevention of all SQL injections. Use parameterized queries instead.\n\n### Additional Defenses\n\n#### Least Privilege\n\nMinimize privileges for all database accounts:\n- Grant only necessary access rights (read vs. write)\n- Avoid DBA or admin access for application accounts\n- Use separate database users for different applications\n- Consider using views to limit data access further\n\n#### Input Validation\n\nUse input validation as a secondary defense to detect unauthorized input before SQL execution. Validated data is not necessarily safe for string concatenation - always use parameterized queries.\n\n### Key Principles\n\n- Define all SQL code first, then pass parameters separately\n- Never concatenate user input directly into SQL strings\n- Treat user input as data, never as executable SQL code\n- Use least privilege database accounts\n- Implement input validation as defense in depth, not primary protection"
  },
  {
    "path": "sources/owasp/codeguard-0-symfony.md",
    "content": "---\ndescription: Symfony Security Best Practices\nlanguages:\n- php\n- yaml\nalwaysApply: false\n---\n\n## Symfony Security Best Practices\n\nEssential security practices for developing secure Symfony applications, covering common vulnerabilities and framework-specific protections.\n\n### Cross-Site Scripting (XSS) Prevention\n\nUse Twig's default `{{ }}` output escaping for all variables. Only use `|raw` filter for trusted content requiring HTML rendering.\n\n```twig\n<p>Hello {{name}}</p>\n{# if 'name' is '<script>alert('hello!')</script>', Twig will output this:\n'<p>Hello &lt;script&gt;alert(&#39;hello!&#39;)&lt;/script&gt;</p>' #}\n\n<p>{{ product.title|raw }}</p>\n{# if 'product.title' is 'Lorem <strong>Ipsum</strong>', Twig will output\nexactly that instead of 'Lorem &lt;strong&gt;Ipsum&lt;/strong&gt;' #}\n```\n\n### CSRF Protection\n\nSymfony Forms include CSRF tokens automatically. For manual handling, use `csrf_token()` and `isCsrfTokenValid()`.\n\n```php\nclass PostForm extends AbstractType\n{\n    public function configureOptions(OptionsResolver $resolver): void\n    {\n        $resolver->setDefaults([\n            // ... \n            'csrf_protection' => true,  // enable/disable csrf protection for this form\n            'csrf_field_name' => '_csrf_token',\n            'csrf_token_id'   => 'post_item', // change arbitrary string used to generate\n        ]);\n    }\n}\n```\n\nManual CSRF token handling:\n```twig\n<form action=\"{{ url('delete_post', { id: post.id }) }}\" method=\"post\">\n    <input type=\"hidden\" name=\"token\" value=\"{{ csrf_token('delete-post') }}\">\n    <button type=\"submit\">Delete post</button>\n</form>\n```\n\n```php\nclass ExampleController extends AbstractController\n{\n    #[Route('/posts/{id}', methods: ['DELETE'], name: 'delete_post')]\n    public function delete(Post $post, Request $request): Response \n    { \n        $token = $request->request->get('token');\n        if($this->isCsrfTokenValid($token)) {\n            // ...\n        }\n        // ...\n    }\n}\n```\n\n### SQL Injection Prevention\n\nUse parameterized queries with Doctrine ORM. Never concatenate user input in SQL strings.\n\n```php\n// Repository method\n$post = $em->getRepository(Post::class)->findOneBy(['id' => $id]);\n\n// DQL with parameters\n$query = $em->createQuery(\"SELECT p FROM App\\Entity\\Post p WHERE p.id = :id\");\n$query->setParameter('id', $id);\n$post = $query->getSingleResult();\n\n// DBAL Query Builder\n$qb = $em->createQueryBuilder();\n$post = $qb->select('p')\n            ->from('posts','p')\n            ->where('id = :id')\n            ->setParameter('id', $id)\n            ->getQuery()\n            ->getSingleResult();\n```\n\n### Command Injection Prevention\n\nAvoid `exec()`, `shell_exec()`, `system()` with user input. Use Symfony Filesystem component or native PHP functions.\n\n```php\n// Vulnerable example\n$filename = $request->request->get('filename');\nexec(sprintf('rm %s', $filename));\n\n// Secure alternatives - use native PHP or Symfony Filesystem\nunlink($filename);\n\n// Or Symfony Filesystem component\nuse Symfony\\Component\\Filesystem\\Filesystem;\n$filesystem = new Filesystem();\n$filesystem->remove($filename);\n```\n\n### File Upload Security\n\nValidate file uploads with Symfony Validator constraints. Store files outside public directory with unique names.\n\n```php\nclass UploadDto\n{\n    public function __construct(\n        #[File(\n            maxSize: '1024k',\n            mimeTypes: ['application/pdf', 'application/x-pdf'],\n        )]\n        public readonly UploadedFile $file,\n    ){}\n}\n```\n\n### Directory Traversal Prevention\n\nUse `realpath()` and `basename()` to validate and sanitize file paths.\n\n```php\n$storagePath = $this->getParameter('kernel.project_dir') . '/storage';\n$filePath = $storagePath . '/' . $filename;\n\n$realBase = realpath($storagePath);\n$realPath = realpath($filePath);\n\nif ($realPath === false || !str_starts_with($realPath, $realBase)) {\n    //Directory Traversal!\n}\n\n// Alternative: strip directory information\n$filePath = $storagePath . '/' . basename($filename);\n```\n\n### Security Configuration\n\nConfigure session security, authentication, and access controls properly.\n\n```yaml\n# Session configuration\nframework:\n    session:\n        cookie_httponly: true\n        cookie_lifetime: 5\n        cookie_samesite: lax\n        cookie_secure: auto\n\n# Authentication providers, firewalls, and access control\nsecurity:\n    providers:\n        app_user_provider:\n            entity:\n                class: App\\Entity\\User\n                property: email\n    firewalls:\n        dev:\n            pattern: ^/(_(profiler|wdt)|css|images|js)/\n            security: false\n        admin:\n            lazy: true\n            provider: app_user_provider\n            pattern: ^/admin\n            custom_authenticator: App\\Security\\AdminAuthenticator\n            logout:\n                path: app_logout\n                target: app_login\n        main:\n            lazy: true\n            provider: app_user_provider\n    access_control:\n        - { path: ^/admin, roles: ROLE_ADMIN }\n        - { path: ^/login, roles: PUBLIC_ACCESS }\n```\n\n### Production Security\n\n- Set `APP_ENV=prod` and disable debug mode\n- Run regular security checks: `composer update` and `symfony check:security`\n- Use Symfony secrets for sensitive data\n- Configure CORS with `nelmio/cors-bundle` (avoid wildcard origins)\n- Implement security headers (HSTS, CSP, X-Frame-Options)\n- Enforce HTTPS and set proper file permissions"
  },
  {
    "path": "sources/owasp/codeguard-0-third-party-javascript-management.md",
    "content": "---\ndescription: Third Party JavaScript Management Security\nlanguages:\n- c\n- javascript\n- typescript\nalwaysApply: false\n---\n\n## Third Party JavaScript Management Security\n\nSecure third-party JavaScript tags to prevent arbitrary code execution, data leakage, and loss of application control.\n\n### Major Risks\n\nThird-party JavaScript poses three critical risks:\n1. Loss of control over client application changes\n2. Execution of arbitrary code on client systems\n3. Disclosure of sensitive information to third parties\n\n### Security Strategies\n\n#### Server Direct Data Layer (Recommended)\nCreate controlled data layer that third-party scripts can access instead of direct DOM access.\n\nKey principles:\n- Only first-party code populates the data layer\n- Third-party scripts read exclusively from sanitized data layer\n- Tag JavaScript can only access host data layer values, never URL parameters\n\nBenefits:\n- Only your JavaScript executes on user browsers\n- Only validated data sent to vendors\n- Scalable for large sites with multiple vendor tags\n\n#### Subresource Integrity (SRI)\nEnsure only reviewed code executes by adding integrity metadata.\n\n```html\n<script src=\"https://analytics.vendor.com/v1.1/script.js\"\n   integrity=\"sha384-MBO5IDfYaE6c6Aao94oZrIOiC7CGiSNE64QUbHNPhzk8Xhm0djE6QqTpL0HzTUxk\"\n   crossorigin=\"anonymous\">\n</script>\n```\n\nRequirements:\n- Vendor host must have CORS enabled\n- Monitor vendor JavaScript for changes regularly\n- Update integrity hashes when vendor updates scripts\n\n#### Sandboxing with iframe\nIsolate vendor JavaScript to prevent direct DOM and cookie access.\n\n```html\n<!-- Host page with sandboxed iframe -->\n<html>\n   <head></head>\n     <body>\n       ...\n       <iframe\n       src=\"https://somehost-static.net/analytics.html\"\n       sandbox=\"allow-same-origin allow-scripts\">\n       </iframe>\n   </body>\n</html>\n\n<!-- somehost-static.net/analytics.html -->\n<html>\n   <head></head>\n     <body>\n       ...\n       <script>\n       window.addEventListener(\"message\", receiveMessage, false);\n       function receiveMessage(event) {\n         if (event.origin !== \"https://somehost.com:443\") {\n           return;\n         } else {\n         // Make some DOM here and initialize other\n        //data required for 3rd party code\n         }\n       }\n       </script>\n       <!-- 3rd party vendor javascript -->\n       <script src=\"https://analytics.vendor.com/v1.1/script.js\"></script>\n       <!-- /3rd party vendor javascript -->\n   </body>\n </html>\n```\n\nCommunication requirements:\n- Use postMessage mechanism for secure data exchange\n- Validate event origins before processing messages\n- Consider Content Security Policy (CSP) for additional protection\n\n#### Content Sanitization\nClean DOM data before sending to third parties using:\n- DOMPurify: XSS sanitizer for HTML, MathML and SVG\n- MentalJS: JavaScript parser and sandbox\n\n#### Tag Manager Controls\nFor tag management systems:\n- Restrict JavaScript access to data layer values only\n- Disable custom HTML tags and JavaScript code where possible\n- Verify tag manager security practices and access controls\n- Implement two-factor authentication for tag configuration\n\n### Operational Security\n\n#### Keep Libraries Updated\n- Regularly update JavaScript libraries to address vulnerabilities\n- Use tools like RetireJS to identify vulnerable libraries\n\n#### Vendor Agreements\nContractual controls:\n- Require evidence of secure coding practices and code integrity monitoring\n- Include penalties for serving malicious JavaScript\n- Mandate source code monitoring and change detection\n\n#### Complete Prevention Strategy\nMost effective controls:\n1. Data layer architecture with API calls to marketing servers\n2. Subresource Integrity implementation\n3. Virtual frame containment deployment\n\n### Implementation Guidelines\n\n1. Implement server-direct data layer architecture for analytics tags\n2. Use Subresource Integrity for all external script requests\n3. Deploy iframe sandboxing for high-risk third-party scripts\n4. Sanitize all dynamic data before including in tag payloads\n5. Maintain updated JavaScript libraries and monitor for vulnerabilities\n6. Establish vendor agreements with security requirements\n7. Regularly audit and monitor third-party script changes"
  },
  {
    "path": "sources/owasp/codeguard-0-threat-modeling.md",
    "content": "---\ndescription: Threat Modeling Security\nlanguages:\n- c\n- go\n- java\n- javascript\n- kotlin\n- php\n- python\n- ruby\n- scala\n- swift\n- typescript\nalwaysApply: false\n---\n\n## Threat Modeling Security\n\nStructured process to identify, analyze, and mitigate security threats early in the development lifecycle through systematic security analysis.\n\n### Threat Modeling Process\n\nThe threat modeling process answers four key questions:\n1. What are we working on?\n2. What can go wrong?\n3. What are we going to do about it?\n4. Did we do a good enough job?\n\n### System Modeling (What are we working on?)\n\nCreate visual representations of your system to understand attack surfaces and security boundaries.\n\nData Flow Diagrams (DFDs):\n- Model system interactions with data and external entities\n- Identify trust boundaries, data flows, data stores, and processes\n- Use tools like OWASP Threat Dragon, Microsoft Threat Modeling Tool, or draw.io\n- Create multiple DFDs for complex systems (high-level overview plus detailed sub-systems)\n- Store DFDs in accessible format for updates and reference\n\nKey elements to capture:\n- External entities interacting with the system\n- Data flows between components\n- Data storage locations\n- Process boundaries and trust zones\n- Authentication and authorization points\n\nAlternative approaches:\n- Brainstorming sessions for domain discovery\n- Collaborative definition of key terms and concepts\n- Quick identification of business processes and dependencies\n\n### Threat Identification (What can go wrong?)\n\nUse STRIDE methodology to systematically identify threats within your system context.\n\nSTRIDE Categories:\n\n| Threat Category | Violates | Examples |\n|-----------------|----------|----------|\n| Spoofing | Authenticity | Attacker steals authentication token to impersonate user |\n| Tampering | Integrity | Attacker abuses application to perform unintended database updates |\n| Repudiation | Non-repudiability | Attacker manipulates logs to cover their actions |\n| Information Disclosure | Confidentiality | Attacker extracts data from database containing user account info |\n| Denial of Service | Availability | Attacker locks legitimate user out by failed authentication attempts |\n| Elevation of Privileges | Authorization | Attacker tampers with JWT to change their role |\n\nThreat identification techniques:\n- Systematic review of each STRIDE category for system components\n- Brainstorming sessions with development and security teams\n- Integration with tactical approaches like kill chains or MITRE ATT&CK\n- Use of threat modeling tools for structured analysis\n\nThreat prioritization:\n- Assess likelihood and impact of identified threats\n- Consider cost and effort required for mitigation\n- Focus on threats with highest risk-to-effort ratio\n\n### Response and Mitigations (What are we going to do about it?)\n\nDefine responses for each identified threat using these strategies:\n\nResponse Options:\n- Mitigate: Reduce likelihood that threat will materialize\n- Eliminate: Remove feature or component causing the threat\n- Transfer: Shift responsibility to another entity\n- Accept: Acknowledge risk but take no action due to business constraints\n\nMitigation requirements:\n- Develop actionable mitigation strategies that can be implemented\n- Document mitigations as testable requirements\n- Reference standards like OWASP ASVS and MITRE CWE for guidance\n- Apply mitigations at category or individual threat level\n- Ensure mitigations are built into the system, not theoretical\n\n### Review and Validation (Did we do a good enough job?)\n\nValidate threat model completeness and effectiveness through stakeholder review.\n\nReview criteria:\n- Does the model accurately reflect the actual system?\n- Have all applicable threats been identified?\n- Has a response strategy been defined for each threat?\n- Do mitigation strategies reduce risk to acceptable levels?\n- Is the threat model formally documented and accessible?\n- Can mitigations be tested and measured for effectiveness?\n\nReview participants:\n- Development teams\n- Security teams\n- Architecture teams\n- Product stakeholders\n- Operations teams\n\n### Integration with Development Process\n\nIntegrate threat modeling seamlessly into SDLC:\n- Perform initial threat modeling during design phase\n- Update threat models when system architecture changes\n- Include threat modeling in feature development workflows\n- Treat as standard development step, not optional add-on\n- Maintain and refine models alongside system evolution\n\nDevelopment team considerations:\n- Provide security training for developers\n- Include security experts in threat modeling sessions\n- Use tools that simplify and automate threat identification\n- Promote security culture within development organization\n- Establish cross-team collaboration and communication\n\nTools and techniques:\n- OWASP Threat Dragon for collaborative threat modeling\n- Microsoft Threat Modeling Tool for structured analysis\n- OWASP pytm for threat-modeling-as-code approach\n- draw.io with threat modeling libraries for diagramming\n- STRIDE, PASTA, LINDDUN, OCTAVE, and VAST methodologies\n\n### Implementation Guidelines\n\n1. Start threat modeling early in design phase and integrate into SDLC\n2. Create comprehensive Data Flow Diagrams showing trust boundaries and data flows\n3. Apply STRIDE methodology systematically to identify threats\n4. Prioritize threats based on likelihood, impact, and mitigation cost\n5. Develop actionable, testable mitigation requirements\n6. Involve security experts and cross-functional stakeholders in reviews\n7. Document threat models and store artifacts accessibly\n8. Update threat models when system architecture or features change\n9. Use appropriate tools to support and streamline the process\n10. Foster security awareness and collaboration across development teams"
  },
  {
    "path": "sources/owasp/codeguard-0-transaction-authorization.md",
    "content": "---\ndescription: Transaction Authorization Security\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\nalwaysApply: false\n---\n\n## Transaction Authorization Security\n\nSecure implementation of transaction authorization to prevent bypassing of sensitive operations like wire transfers through robust user confirmation and server-side enforcement.\n\n### Transaction Authorization Concept\n\nTransaction authorization requires users to submit a second factor to verify authorization for sensitive operations. This applies beyond financial systems to any operation requiring explicit user consent (account unlocks, privilege changes, data modifications).\n\nAuthorization methods include:\n- Time-based one-time password (OTP) tokens (OATH TOTP)\n- OTP sent by SMS or phone\n- Digital signatures from smart cards or smartphones\n- Challenge-response tokens including unconnected card readers\n- Transaction authorization cards with unique numbers\n\n### Functional Requirements\n\n#### User Identification and Acknowledgment (What You See Is What You Sign)\nTransaction authorization must allow users to identify and acknowledge significant transaction data.\n\nKey principles:\n- User must verify all significant transaction data during authorization process\n- Display critical information: target account, amount, transaction type\n- Balance security requirements with user experience and technical constraints\n- For external devices with limited displays, show minimum significant data (partial account number, amount)\n- Ensure meaningful prompts to prevent social engineering and malware abuse\n\n#### Authorization Token and Method Changes\n- Changes to authorization tokens require authorization using current authorization credentials\n- Changes to authorization methods require authorization using current authorization method\n- Prevent malware from downgrading to least secure authorization method\n- Inform users about potential dangers of different authorization methods\n\n#### Authentication vs Authorization Separation\nDistinguish authentication process from transaction authorization process to prevent credential reuse attacks.\n\nPrevention strategies:\n- Use different methods for authentication and transaction authorization\n- Employ different actions in external security components\n- Present clear messages about what users are signing\n- Prevent malware from using authentication credentials for transaction authorization\n\n#### Unique Authorization per Transaction\nEach transaction requires unique authorization credentials to prevent replay attacks and credential reuse during sessions.\n\n### Non-Functional Requirements\n\n#### Server-Side Enforcement\nAll authorization checks must be performed and enforced server-side.\n\nImplementation requirements:\n- Never allow client-side influence on authorization results\n- Prevent tampering with transaction data parameters\n- Prevent adding/removing parameters that disable authorization checks\n- Apply security programming best practices (default deny, no debug code in production)\n- Encrypt data for confidentiality and integrity, verify server-side\n\n#### Authorization Method Enforcement\nServer-side must enforce chosen authorization method or application policies.\n\nSecurity considerations:\n- Prevent client-side manipulation of authorization method parameters\n- Avoid building new authorization methods on old insecure codebases\n- Ensure attackers cannot downgrade to older, less secure methods\n\n#### Server-Side Transaction Verification\nGenerate and store all significant transaction data server-side.\n\nRequirements:\n- All transaction data must be verified by user and generated server-side\n- Pass data to authorization component without client tampering possibility\n- Prevent malware manipulation of transaction data display\n\n#### Brute-Force Protection\nImplement protections against authorization credential brute-force attacks.\n\nControls:\n- Restart entire transaction authorization process after failed attempts\n- Apply throttling and retry limits\n- Use additional automation prevention techniques\n\n#### Transaction State Control\nEnforce sequential transaction state transitions.\n\nStandard transaction flow:\n1. User enters transaction data\n2. User requests authorization from application\n3. Application initializes authorization mechanism\n4. User verifies/confirms transaction data\n5. User responds with authorization credentials\n6. Application validates authorization and executes transaction\n\nProtection measures:\n- Prevent out-of-order step execution\n- Prevent step skipping\n- Protect against transaction data overwriting before authorization\n- Prevent skipping transaction authorization entirely\n\n#### Transaction Data Protection\nProtect transaction data against modification during authorization process.\n\nImplementation strategies:\n- Invalidate authorization data if transaction data is modified\n- Reset authorization process on transaction data modifications\n- Log, monitor, and investigate modification attempts\n- Prevent Time of Check to Time of Use vulnerabilities\n\n#### Data Confidentiality\nProtect transaction data privacy during all client-server communications throughout the authorization process.\n\n#### Transaction Execution Verification\nImplement final control gate before transaction execution.\n\nVerification requirements:\n- Verify transaction was properly authorized by user\n- Prevent Time of Check to Time of Use attacks\n- Prevent authorization check bypass in transaction entry process\n\n#### Time-Limited Authorization\nLimit authorization credential validity to prevent delayed abuse by malware.\n\nControls:\n- Restrict time window between challenge/OTP generation and authorization completion\n- Balance security with normal user behavior\n- Help prevent resource exhaustion attacks\n\n#### Unique Authorization Credentials\nGenerate unique authorization credentials for every operation to prevent replay attacks.\n\nGeneration methods:\n- Use timestamps in signed transaction data\n- Include sequence numbers in challenges\n- Use random values in authorization components\n\n### Implementation Considerations\n\n#### Risk-Based Authorization\nDetermine which transactions require authorization based on:\n- Risk analysis of specific application\n- Risk exposure assessment\n- Other implemented safeguards\n\n#### Cryptographic Protection\nImplement cryptographic operations to ensure:\n- Transaction integrity\n- Data confidentiality\n- Non-repudiation of transactions\n\n#### Secure Key Management\nProtect device signing keys during device pairing and signing protocol.\n\nSecurity measures:\n- Prevent malware injection/replacement of signing keys\n- Use second factors for key protection (passwords, biometrics)\n- Leverage secure elements (TEE, TPM, smart cards)\n\n#### User Awareness\nTrain users on secure practices:\n- Verify transaction data from trusted sources, not computer screens\n- Understand risks of different authorization mechanisms\n- Recognize social engineering attempts\n\n#### Anti-Malware Integration\nDeploy anti-malware solutions as additional protection layer while recognizing they cannot provide 100% effectiveness.\n\n### Security Controls Summary\n\n1. Implement What You See Is What You Sign principle for transaction data verification\n2. Separate authentication from transaction authorization processes\n3. Use unique, time-limited authorization credentials per transaction\n4. Enforce all authorization logic server-side without client trust\n5. Protect authorization token and method changes through re-authorization\n6. Prevent authorization method downgrade attacks\n7. Implement brute-force protection with transaction resets\n8. Enforce sequential transaction state transitions\n9. Protect transaction data against modification during authorization\n10. Secure all client-server communications with encryption\n11. Verify proper authorization before transaction execution\n12. Use cryptographic signing and secure key storage for integrity\n13. Monitor and log suspicious authorization activities\n14. Provide user education on authorization security practices"
  },
  {
    "path": "sources/owasp/codeguard-0-transport-layer-security.md",
    "content": "---\ndescription: Transport Layer Security\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\n- yaml\nalwaysApply: false\n---\n\n## Transport Layer Security\n\nSecure implementation of TLS to protect client-server communications with confidentiality, integrity, and authentication through proper protocol, cipher, and certificate configuration.\n\n\n### TLS Security Benefits\n\nWhen correctly implemented, TLS provides:\n- Confidentiality: Protection against attackers reading traffic contents\n- Integrity: Protection against traffic modification and replay attacks\n- Authentication: Client confirmation of legitimate server connection\n\n### Protocol Security\n\n#### Use Strong TLS Protocols Only\nGeneral purpose web applications should default to TLS 1.3 with TLS 1.2 support if necessary.\n\nProtocol requirements:\n- Enable TLS 1.3 by default\n- Support TLS 1.2 only if legacy client compatibility required\n- Disable TLS 1.0, TLS 1.1, SSL v2, and SSL v3 completely\n- Enable TLS_FALLBACK_SCSV extension to prevent downgrade attacks when fallback necessary\n- Note: PCI DSS forbids legacy protocols such as TLS 1.0\n\n#### Configure Strong Cipher Suites\nUse only strong ciphers that provide adequate security levels.\n\nCipher requirements:\n- Prefer GCM ciphers where possible\n- Always disable null ciphers, anonymous ciphers, and EXPORT ciphers\n- Use Mozilla Foundation secure configuration generator for balanced security and compatibility\n\n#### Set Appropriate Diffie-Hellman Groups\nConfigure secure Diffie-Hellman parameters for key exchange.\n\nTLS 1.3 groups: ffdhe2048, ffdhe3072, ffdhe4096, ffdhe6144, ffdhe8192\n\nConfiguration examples:\n\n```text\n# OpenSSL configuration\nopenssl_conf = openssl_init\n[openssl_init]\nssl_conf = ssl_module\n[ssl_module]\nsystem_default = tls_system_default\n[tls_system_default]\nGroups = x25519:prime256v1:x448:ffdhe2048:ffdhe3072\n```\n\n```text\n# Apache configuration\nSSLOpenSSLConfCmd Groups x25519:secp256r1:ffdhe3072\n```\n\n```text\n# NGINX configuration\nssl_ecdh_curve x25519:secp256r1:ffdhe3072;\n```\n\n#### Disable TLS Compression\nDisable TLS compression to protect against CRIME attacks that could recover sensitive information like session cookies.\n\n#### Keep Cryptographic Libraries Updated\nMaintain current versions of SSL/TLS libraries to protect against vulnerabilities like Heartbleed.\n\n### Certificate Management\n\n#### Use Strong Keys and Protection\nGenerate certificates with minimum 2048-bit key size and protect private keys from unauthorized access using filesystem permissions and access controls.\n\n#### Use Strong Cryptographic Hashing\nCertificates should use SHA-256 for hashing algorithm rather than deprecated MD5 and SHA-1 algorithms.\n\n#### Use Correct Domain Names\nCertificate domain names must match server's FQDN in both commonName (CN) and subjectAlternativeName (SAN) attributes.\n\n#### Consider Wildcard Certificate Risks\nWildcard certificates violate principle of least privilege. Use only when genuine need exists and never for systems at different trust levels.\n\n#### Use Appropriate Certificate Authority\nChoose trusted CAs for Internet-facing applications. Consider LetsEncrypt for free domain validated certificates.\n\n### Application Implementation\n\n#### Use TLS for All Pages\nImplement TLS for entire application with HTTP 301 redirects and HSTS header support.\n\n#### Prevent Mixed Content\nLoad all JavaScript, CSS, and resources over HTTPS to prevent session cookie sniffing and malicious code injection.\n\n#### Use Secure Cookie Flag\nMark all cookies with \"Secure\" attribute to restrict transmission to encrypted HTTPS connections only.\n\n#### Prevent Sensitive Data Caching\nUse cache prevention headers:\n\n```text\nCache-Control: no-cache, no-store, must-revalidate\nPragma: no-cache\nExpires: 0\n```\n\n#### Implement HTTP Strict Transport Security\nHSTS instructs browsers to always request site over HTTPS and prevents bypassing certificate warnings.\n\n#### Consider Client Certificates and Mutual TLS\nmTLS provides mutual authentication but involves significant administrative overhead. Recommended for high-value applications with technically sophisticated users.\n\n#### Avoid Public Key Pinning in Browsers\nHPKP deprecated and no longer supported by modern browsers. Consider pinning only in controlled environments.\n\n### Testing and Validation\n\nTest TLS configuration using tools like SSL Labs Server Test, testssl.sh, SSLyze, and other recommended online and offline testing tools.\n\n### Implementation Guidelines\n\n1. Default to TLS 1.3 with TLS 1.2 fallback only if necessary\n2. Disable all legacy protocols (TLS 1.0/1.1, SSL v2/v3)\n3. Configure strong cipher suites with GCM preference\n4. Set appropriate Diffie-Hellman groups for key exchange\n5. Disable TLS compression to prevent CRIME attacks\n6. Use minimum 2048-bit certificate keys with SHA-256 hashing\n7. Ensure correct domain names in certificate CN and SAN fields\n8. Implement HTTPS for all pages with 301 redirects from HTTP\n9. Prevent mixed content by loading all resources over HTTPS\n10. Set Secure flag on all cookies\n11. Prevent sensitive data caching with appropriate HTTP headers\n12. Implement HSTS for browser enforcement\n13. Consider mTLS for high-value applications\n14. Avoid public key pinning in browsers\n15. Regular testing of TLS configuration with recommended tools"
  },
  {
    "path": "sources/owasp/codeguard-0-unvalidated-redirects-and-forwards.md",
    "content": "---\ndescription: Unvalidated Redirects and Forwards Prevention\nlanguages:\n- c\n- java\n- javascript\n- php\n- ruby\n- rust\n- typescript\nalwaysApply: false\n---\n\n## Unvalidated Redirects and Forwards Prevention\n\nPrevent open redirect and forward vulnerabilities by validating all user-controlled redirect destinations to stop phishing attacks and access control bypass.\n\n### Security Risks\n\nUnvalidated redirects and forwards occur when applications accept untrusted input for redirect destinations, enabling:\n- Phishing attacks by redirecting users to malicious sites while maintaining trusted domain appearance\n- Access control bypass by forwarding to privileged functions normally restricted\n- User credential theft through convincing redirect to attacker-controlled sites\n\n### Safe Redirect Examples\n\nSecure redirects use hardcoded URLs that cannot be manipulated by attackers:\n\nJava:\n```java\nresponse.sendRedirect(\"http://www.mysite.com\");\n```\n\nPHP:\n```php\n<?php\n/* Redirect browser */\nheader(\"Location: http://www.mysite.com\");\n/* Exit to prevent the rest of the code from executing */\nexit;\n?>\n```\n\nASP .NET:\n```csharp\nResponse.Redirect(\"~/folder/Login.aspx\")\n```\n\nRails:\n```ruby\nredirect_to login_path\n```\n\nRust actix web:\n```rust\n  Ok(HttpResponse::Found()\n        .insert_header((header::LOCATION, \"https://mysite.com/\"))\n        .finish())\n```\n\n### Dangerous Redirect Examples\n\nVulnerable code accepts user input directly for redirect destinations:\n\nJava:\n```java\nresponse.sendRedirect(request.getParameter(\"url\"));\n```\n\nPHP:\n```php\n$redirect_url = $_GET['url'];\nheader(\"Location: \" . $redirect_url);\n```\n\nC# .NET:\n```csharp\nstring url = request.QueryString[\"url\"];\nResponse.Redirect(url);\n```\n\nRails:\n```ruby\nredirect_to params[:url]\n```\n\nRust actix web:\n```rust\n  Ok(HttpResponse::Found()\n        .insert_header((header::LOCATION, query_string.path.as_str()))\n        .finish())\n```\n\nASP.NET MVC vulnerable example:\n```csharp\n[HttpPost]\n public ActionResult LogOn(LogOnModel model, string returnUrl)\n {\n   if (ModelState.IsValid)\n   {\n     if (MembershipService.ValidateUser(model.UserName, model.Password))\n     {\n       FormsService.SignIn(model.UserName, model.RememberMe);\n       if (!String.IsNullOrEmpty(returnUrl))\n       {\n         return Redirect(returnUrl);\n       }\n       else\n       {\n         return RedirectToAction(\"Index\", \"Home\");\n       }\n     }\n     else\n     {\n       ModelState.AddModelError(\"\", \"The user name or password provided is incorrect.\");\n     }\n   }\n\n   // If we got this far, something failed, redisplay form\n   return View(model);\n }\n```\n\nAttack example:\n```text\n http://example.com/example.php?url=http://malicious.example.com\n```\n\n### Dangerous Forward Examples\n\nServer-side forwards can bypass access controls when user input determines the forward destination:\n\nJava servlet vulnerable forward:\n```java\npublic class ForwardServlet extends HttpServlet\n{\n  protected void doGet(HttpServletRequest request, HttpServletResponse response)\n                    throws ServletException, IOException {\n    String query = request.getQueryString();\n    if (query.contains(\"fwd\"))\n    {\n      String fwd = request.getParameter(\"fwd\");\n      try\n      {\n        request.getRequestDispatcher(fwd).forward(request, response);\n      }\n      catch (ServletException e)\n      {\n        e.printStackTrace();\n      }\n    }\n  }\n}\n```\n\nAttack example:\n```text\nhttp://www.example.com/function.jsp?fwd=admin.jsp\n```\n\n### Prevention Strategies\n\n#### Avoid User Input for Destinations\nSimply avoid using redirects and forwards, or do not allow URLs as user input for destinations.\n\n#### Use Server-Side Mapping\nHave users provide short name, ID, or token mapped server-side to full target URL.\n\nBenefits:\n- Highest degree of protection against URL tampering\n- Prevents direct URL manipulation\n\nConsiderations:\n- Prevent enumeration vulnerabilities where users cycle through IDs\n- Use sufficiently large or complex token spaces\n\n#### Input Validation and Authorization\nIf user input cannot be avoided:\n- Ensure supplied value is valid and appropriate for the application\n- Verify user is authorized for the redirect/forward target\n- Validate before performing redirect or forward\n\n#### Allow-List Approach\nCreate and enforce list of trusted URLs or hosts:\n- Use allow-list approach rather than deny-list\n- Sanitize input against trusted URL list\n- Consider regex patterns for flexible matching\n\n#### External Redirect Warning\nForce external redirects through interstitial warning page:\n- Clearly display destination to users\n- Require user confirmation before proceeding\n- Helps users identify potential phishing attempts\n\n#### Framework-Specific Considerations\n\nPHP:\n- Always follow header(\"Location: ...\") with exit; to stop execution\n- Prevents continued code execution after redirect\n\nASP.NET MVC:\n- MVC 1 & 2 particularly vulnerable to open redirection attacks\n- Use MVC 3 or later to avoid vulnerabilities\n\n### Implementation Guidelines\n\n1. Use hardcoded URLs for redirects whenever possible\n2. Implement server-side mapping for user-controlled redirects\n3. Validate all user input against allow-list of permitted destinations\n4. Verify user authorization before performing redirects or forwards\n5. Use interstitial warnings for external redirects\n6. Follow framework-specific security practices\n7. Prevent enumeration through complex token spaces\n8. Keep frameworks updated with security patches\n9. Never trust user input for determining redirect destinations\n10. Test redirect functionality for bypass attempts"
  },
  {
    "path": "sources/owasp/codeguard-0-user-privacy-protection.md",
    "content": "---\ndescription: User Privacy Protection Rule\nlanguages:\n- c\n- go\n- java\n- javascript\n- kotlin\n- python\n- ruby\n- swift\n- typescript\nalwaysApply: false\n---\n\n- Implement strong cryptography, enforce HTTPS with HSTS, enable certificate pinning,\nand provide user privacy features to protect data and anonymity.\n- Use strong, up-to-date cryptographic algorithms for data in transit and at rest; securely hash passwords with established libraries.\n- Enforce HTTPS exclusively and implement HTTP Strict Transport Security (HSTS).\n- Implement certificate pinning to prevent man-in-the-middle attacks even if CAs are compromised.\n- Minimize IP address leakage by blocking third-party external content loading where feasible.\n- Maintain transparency by informing users about privacy limitations and data handling policies.\n- Implement privacy-focused audit trails and access logging.\n- Return \"Invalid username or password\" to prevent account enumeration\n- Use Argon2 or bcrypt with unique salts per user\n- Store sessions server-side with cryptographically random IDs"
  },
  {
    "path": "sources/owasp/codeguard-0-virtual-patching.md",
    "content": "---\ndescription: Virtual Patching Security\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\n- xml\nalwaysApply: false\n---\n\n## Virtual Patching Security\n\nImplement temporary security controls to protect against known vulnerabilities while developing permanent code fixes through security policy enforcement layers.\n\n### Virtual Patching Definition\n\nA security policy enforcement layer which prevents and reports exploitation attempts of known vulnerabilities. Virtual patches analyze transactions and intercept attacks in transit so malicious traffic never reaches the web application, providing protection while actual source code remains unmodified.\n\n### When Virtual Patching is Needed\n\nVirtual patching addresses real-world scenarios where immediate code fixes are not feasible:\n\n- Lack of resources: Developers allocated to other projects\n- Third-party software: Code cannot be modified by users\n- Outsourced development: Changes require new project authorization\n\nImportant: Code level fixes and virtual patching are NOT mutually exclusive. They are executed by different teams (developers vs. security operations) and can run in tandem.\n\n### Virtual Patching Goals\n\n- Minimize Time-to-Fix: Implement mitigation as soon as possible while code fixes are developed\n- Attack Surface Reduction: Focus on minimizing attack vectors, even partial reduction (50% in 10 minutes vs 100% in 48 hours)\n\n### Virtual Patching Tools\n\nAvailable tools for implementing virtual patches:\n- Intermediary devices such as WAF or IPS appliances\n- Web server plugins such as ModSecurity\n- Application layer filters such as ESAPI WAF\n\n### Virtual Patching Methodology\n\nFollow structured workflow for consistent, repeatable virtual patching:\n\n1. Preparation\n2. Identification  \n3. Analysis\n4. Virtual Patch Creation\n5. Implementation/Testing\n6. Recovery/Follow-Up\n\n### Preparation Phase\n\nCritical preparation items before incidents occur:\n\n- Public/Vendor Vulnerability Monitoring: Subscribe to vendor alert mailing lists for commercial software\n- Virtual Patching Pre-Authorization: Expedite governance processes since virtual patches don't modify source code\n- Deploy Virtual Patching Tools in Advance: Install ModSecurity WAF or similar tools ready for activation\n- Increase HTTP Audit Logging: Capture request URI, full headers, request/response bodies for incident analysis\n\n### Analysis Phase\n\nRecommended analysis steps:\n\n1. Determine virtual patching applicability for vulnerability type\n2. Utilize bug tracking system for vulnerability information management\n3. Verify vulnerability identifier (CVE name/number)\n4. Designate impact level for proper prioritization\n5. Specify affected software versions\n6. List configuration requirements to trigger vulnerability\n7. Collect Proof of Concept (PoC) exploit code for analysis and testing\n\n### Virtual Patch Creation Principles\n\nTwo main requirements for accurate virtual patches:\n- No false positives: Never block legitimate traffic\n- No false negatives: Never miss attacks, even with evasion attempts\n\n### Positive Security (Allow List) Virtual Patches (Recommended)\n\nPositive security model provides comprehensive input validation by specifying valid input characteristics and denying anything non-conformant.\n\nExample ModSecurity virtual patch for SQL injection protection:\n\n```text\n##\n## Verify we only receive 1 parameter called \"reqID\"\n##\nSecRule REQUEST_URI \"@contains /wp-content/plugins/levelfourstorefront/scripts/administration/exportsubscribers.php\" \"chain,id:1,phase:2,t:none,t:Utf8toUnicode,t:urlDecodeUni,t:normalizePathWin,t:lowercase,block,msg:'Input Validation Error for \\'reqID\\' parameter - Duplicate Parameters Names Seen.',logdata:'%{matched_var}'\"\n  SecRule &ARGS:/reqID/ \"!@eq 1\"\n\n##\n## Verify reqID's payload only contains integers\n##\nSecRule REQUEST_URI \"@contains /wp-content/plugins/levelfourstorefront/scripts/administration/exportsubscribers.php\" \"chain,id:2,phase:2,t:none,t:Utf8toUnicode,t:urlDecodeUni,t:normalizePathWin,t:lowercase,block,msg:'Input Validation Error for \\'reqID\\' parameter.',logdata:'%{args.reqid}'\"\n  SecRule ARGS:/reqID/ \"!@rx ^[0-9]+$\"\n```\n\n### Negative Security (Block List) Virtual Patches\n\nNegative security model detects specific known attacks rather than allowing only valid traffic.\n\nExample PoC attack payload:\n```text\nhttp://localhost/wordpress/wp-content/plugins/levelfourstorefront/scripts/administration/exportsubscribers.php?reqID=1' or 1='1\n```\n\nExample ModSecurity block list virtual patch:\n```text\nSecRule REQUEST_URI \"@contains /wp-content/plugins/levelfourstorefront/scripts/administration/exportsubscribers.php\" \"chain,id:1,phase:2,t:none,t:Utf8toUnicode,t:urlDecodeUni,t:normalizePathWin,t:lowercase,block,msg:'Input Validation Error for \\'reqID\\' parameter.',logdata:'%{args.reqid}'\"\n  SecRule ARGS:/reqID/ \"@pm '\"\n```\n\n### Security Model Comparison\n\nPositive vs Negative Security considerations:\n- Negative security: Faster implementation but more evasion possibilities\n- Positive security: Better protection but manual process, less scalable for large/dynamic sites\n- Positive security recommended for specific vulnerability locations identified by alerts\n\n### Avoid Exploit-Specific Patches\n\nResist creating patches that only block exact exploit payloads. Example poor approach for XSS:\n\n```html\n<script>\n  alert('XSS Test')\n</script>\n```\n\nBlocking only this exact payload provides minimal long-term protection value.\n\n### Automated Virtual Patch Creation\n\nTools for automated patch creation from vulnerability reports:\n- OWASP ModSecurity Core Rule Set (CRS) Scripts: Auto-convert XML output from tools like ZAP\n- ThreadFix Virtual Patching: Convert vulnerability XML data into ModSecurity patches\n- Direct WAF Importing: Commercial WAF products import DAST tool XML reports\n\n### Implementation and Testing\n\nTesting tools for virtual patch validation:\n- Web browsers\n- Command-line clients (Curl, Wget)\n- Local proxy servers (ZAP)\n- ModSecurity AuditViewer for log manipulation and re-injection\n\nTesting steps:\n- Implement patches initially in \"Log Only\" mode to prevent false positives\n- Request retest from vulnerability identification team\n- Return to analysis phase if evasions occur during retesting\n\n### Recovery and Follow-Up\n\nPost-implementation activities:\n- Update ticket system with virtual patch details and rule IDs\n- Conduct periodic re-assessments to determine when virtual patches can be removed\n- Run virtual patch alert reports to demonstrate protection value\n- Track time-to-fix metrics for different vulnerability types\n\n### Developer Integration Guidelines\n\n1. Prioritize permanent code fixes over virtual patches\n2. Collaborate with security teams on virtual patch requirements and testing\n3. Understand virtual patch limitations and temporary nature\n4. Provide input on normal application behavior for positive security rules\n5. Review virtual patch logs for attack patterns that inform secure coding practices\n6. Plan code fixes to address root causes protected by virtual patches\n7. Participate in virtual patch removal process once code fixes are deployed\n8. Document application-specific requirements for virtual patch creation"
  },
  {
    "path": "sources/owasp/codeguard-0-vulnerable-dependency-management.md",
    "content": "---\ndescription: Vulnerable Dependency Management\nlanguages:\n- c\n- go\n- javascript\n- r\n- xml\nalwaysApply: false\n---\n\n## Vulnerable Dependency Management\n\nDetect and mitigate security vulnerabilities in third-party dependencies through automated scanning, proper testing, and systematic remediation approaches.\n\n### Automated Detection\n\nIntegrate vulnerability scanning from project inception using tools that cover multiple vulnerability sources:\n- CVE databases (NIST National Vulnerability Database)\n- Full disclosure sources (mailing lists, Exploit-DB)\n- Provider-specific vulnerability feeds\n\nRecommended tools:\n- OWASP Dependency Check (Java, .NET, experimental support for Python, Ruby, PHP, Node.js, C/C++)\n- NPM Audit (Node.js, JavaScript)\n- OWASP Dependency Track (organization-wide management)\n\n### Remediation Cases\n\nCase 1 - Patched version available:\n1. Update dependency version in testing environment\n2. Run automated tests to verify functionality\n3. If tests pass: deploy to production\n4. If tests fail: update application code for API changes or report incompatibility to provider\n\nCase 2 - Patch delayed, provider provides workaround:\n1. Apply provider workaround if available\n2. If provider lists impacted functions, add protective wrappers\n3. Validate workaround in testing environment\n\nExample protective wrapper for RCE vulnerability:\n```java\npublic void callFunctionWithRCEIssue(String externalInput){\n    //Apply input validation on the external input using regex\n    if(Pattern.matches(\"[a-zA-Z0-9]{1,50}\", externalInput)){\n        //Call the flawed function using safe input\n        functionWithRCEIssue(externalInput);\n    }else{\n        //Log the detection of exploitation\n        SecurityLogger.warn(\"Exploitation of the RCE issue XXXXX detected !\");\n        //Raise an exception leading to a generic error send to the client...\n    }\n}\n```\n\nCase 3 - No patch available:\n1. Analyze CVE description to understand vulnerability type (SQL injection, XSS, XXE, etc.)\n2. Identify all application code calling the vulnerable dependency\n3. Implement compensating controls based on vulnerability type\n4. Create unit tests to verify protection effectiveness\n5. For open source dependencies: consider creating and contributing patches\n\nCase 4 - Previously unknown vulnerability discovered:\n1. Notify provider with vulnerability details\n2. If provider cooperates: follow Case 2 approach\n3. If provider unresponsive: follow Case 3 approach\n\n### Dependencies Analysis\n\nTransitive dependencies: Act on direct dependencies when possible, as modifying transitive dependencies requires understanding complex dependency chains and can impact application stability.\n\nUse dependency management tools to identify:\n- Direct vs transitive dependency relationships\n- All code paths using vulnerable components\n- Impact scope of potential vulnerabilities\n\n### Testing and Validation\n\nMaintain comprehensive automated tests covering:\n- Features using impacted dependencies\n- Security controls added as mitigations\n- Regression detection during updates\n\nRun tests before and after dependency updates to ensure:\n- Application functionality remains intact\n- Security mitigations are effective\n- No new vulnerabilities are introduced\n\n### Risk Management\n\nDocument all vulnerability decisions including:\n- Technical analysis and CVSS scoring\n- Chosen mitigation approach and rationale\n- Testing results and validation steps\n- Risk acceptance decisions with business justification\n\nEscalate risk acceptance decisions to Chief Risk Officer after thorough technical analysis.\n\n### Continuous Monitoring\n\nImplement continuous dependency scanning in CI/CD pipelines:\n- Scan on every build\n- Fail builds for high-severity vulnerabilities\n- Generate reports for security team review\n- Track remediation progress and compliance\n\nChoose tools supporting false-positive flagging and multiple reliable input sources to handle different vulnerability disclosure methods.\n\n### Prevention Guidelines\n\n- Start dependency scanning from project inception\n- Prefer fixing vulnerabilities at source (application or dependency) over external controls\n- Keep dependencies updated regularly\n- Monitor dependencies for maintenance status and community activity\n- Consider dependency complexity and transitive dependency count when selecting libraries"
  },
  {
    "path": "sources/owasp/codeguard-0-web-service-security.md",
    "content": "---\ndescription: Web Service Security\nlanguages:\n- c\n- go\n- java\n- javascript\n- php\n- python\n- ruby\n- typescript\n- xml\nalwaysApply: false\n---\n\n## Web Service Security\n\nSecure web services through transport protection, authentication, input validation, and XML attack prevention.\n\n### Transport Security\n\nAll web service communications with sensitive features or data must use well-configured TLS. TLS provides confidentiality, integrity protection, replay defenses, and server authentication.\n\nServer certificate validation requirements:\n- Issued by trusted provider\n- Not expired or revoked\n- Matches service domain name\n- Proven private key ownership\n\n### Authentication\n\n- Avoid Basic Authentication; use Client Certificate Authentication with Mutual-TLS when appropriate\n- Enforce consistent encoding styles between client and server\n\n### Message Protection\n\nFor XML data requiring integrity beyond TLS:\n- Use XML digital signatures with sender's private key\n- Encrypt sensitive data with strong ciphers for both transport and at-rest protection when required\n\n### Authorization\n\n- Authorize clients for every web service method and request\n- Check privileges for requested resources on every request\n- Separate administrative functions from regular service endpoints\n- Add challenge-response mechanisms for sensitive operations (password changes, contact details, payment instructions)\n\n### Input Validation\n\nSchema validation:\n- Validate SOAP payloads against XML schema definition (XSD)\n- Define maximum length and character sets for all parameters\n- Use strong allow-list patterns for fixed format parameters (zip codes, phone numbers)\n\nContent validation for XML input:\n- Validate against malformed XML entities\n- Validate against XML Bomb attacks\n- Use strong allowlists for input validation\n- Validate against external entity attacks\n\n### XML Attack Protection\n\nConfigure XML parsers to protect against:\n- Recursive payloads\n- Oversized payloads\n- XML entity expansion\n- Overlong element names (SOAP Actions)\n\nBuild test cases to verify parser resistance to these attacks.\n\n### Output Protection\n\nApply proper output encoding to prevent XSS when web service data is consumed by web clients.\n\n### Resource Protection\n\n- Limit SOAP message sizes to prevent DoS attacks\n- Limit CPU cycles, memory usage, and simultaneous connections\n- Optimize configuration for maximum message throughput\n- Implement virus scanning for SOAP attachments before storage\n\n### Compliance\n\nEnsure compliance with Web Services-Interoperability (WS-I) Basic Profile for security baseline."
  },
  {
    "path": "sources/owasp/codeguard-0-xml-external-entity-prevention.md",
    "content": "---\ndescription: XML External Entity Prevention\nlanguages:\n- c\n- java\n- matlab\n- php\n- python\n- swift\n- xml\nalwaysApply: false\n---\n\n## XML External Entity Prevention\n\nPrevent XXE attacks by disabling DTDs and external entities in XML parsers. Safest approach: disable DTDs completely.\n\n### General Principle\n\n```java\nfactory.setFeature(\"http://apache.org/xml/features/disallow-doctype-decl\", true);\n```\n\nDisabling DTDs protects against XXE and Billion Laughs attacks. If DTDs cannot be disabled, disable external entities using parser-specific methods.\n\n### Java\n\nJava parsers have XXE enabled by default.\n\nDocumentBuilderFactory/SAXParserFactory/DOM4J:\n\n```java\nDocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\nString FEATURE = null;\ntry {\n    // PRIMARY defense - disallow DTDs completely\n    FEATURE = \"http://apache.org/xml/features/disallow-doctype-decl\";\n    dbf.setFeature(FEATURE, true);\n    dbf.setXIncludeAware(false);\n} catch (ParserConfigurationException e) {\n    logger.info(\"ParserConfigurationException was thrown. The feature '\" + FEATURE\n    + \"' is not supported by your XML processor.\");\n}\n```\n\nIf DTDs cannot be completely disabled:\n\n```java\nDocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\nString[] featuresToDisable = {\n    \"http://xml.org/sax/features/external-general-entities\",\n    \"http://xml.org/sax/features/external-parameter-entities\",\n    \"http://apache.org/xml/features/nonvalidating/load-external-dtd\"\n};\n\nfor (String feature : featuresToDisable) {\n    try {    \n        dbf.setFeature(feature, false); \n    } catch (ParserConfigurationException e) {\n        logger.info(\"ParserConfigurationException was thrown. The feature '\" + feature\n        + \"' is probably not supported by your XML processor.\");\n    }\n}\ndbf.setXIncludeAware(false);\ndbf.setExpandEntityReferences(false);\ndbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);\n```\n\nXMLInputFactory (StAX):\n```java\nxmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);\n// Or if DTDs needed:\nxmlInputFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, \"\");\nxmlInputFactory.setProperty(\"javax.xml.stream.isSupportingExternalEntities\", false);\n```\n\nTransformerFactory:\n```java\nTransformerFactory tf = TransformerFactory.newInstance();\ntf.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, \"\");\ntf.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, \"\");\n```\n\nXMLReader:\n```java\nXMLReader reader = XMLReaderFactory.createXMLReader();\nreader.setFeature(\"http://apache.org/xml/features/disallow-doctype-decl\", true);\nreader.setFeature(\"http://apache.org/xml/features/nonvalidating/load-external-dtd\", false);\nreader.setFeature(\"http://xml.org/sax/features/external-general-entities\", false);\nreader.setFeature(\"http://xml.org/sax/features/external-parameter-entities\", false);\n```\n\nSAXBuilder:\n```java\nSAXBuilder builder = new SAXBuilder();\nbuilder.setFeature(\"http://apache.org/xml/features/disallow-doctype-decl\",true);\nDocument doc = builder.build(new File(fileName));\n```\n\nNo-op EntityResolver:\n```java\npublic final class NoOpEntityResolver implements EntityResolver {\n    public InputSource resolveEntity(String publicId, String systemId) {\n        return new InputSource(new StringReader(\"\"));\n    }\n}\nxmlReader.setEntityResolver(new NoOpEntityResolver());\ndocumentBuilder.setEntityResolver(new NoOpEntityResolver());\n```\n\nNever use java.beans.XMLDecoder on untrusted content - it can execute arbitrary code.\n\n### .NET\n\nXmlReader (.NET 4.5.2+ safe by default):\n```csharp\nXmlReaderSettings settings = new XmlReaderSettings();\nsettings.DtdProcessing = DtdProcessing.Prohibit;\nsettings.XmlResolver = null;\nXmlReader reader = XmlReader.Create(stream, settings);\n```\n\nXmlTextReader (prior to .NET 4.0):\n```csharp\nXmlTextReader reader = new XmlTextReader(stream);\nreader.ProhibitDtd = true;  \n```\n\nXmlTextReader (.NET 4.0 - 4.5.2):\n```csharp\nXmlTextReader reader = new XmlTextReader(stream);\nreader.DtdProcessing = DtdProcessing.Prohibit;  \n```\n\nXmlDocument (prior to 4.5.2):\n```csharp\nXmlDocument xmlDoc = new XmlDocument();\nxmlDoc.XmlResolver = null;\nxmlDoc.LoadXml(xml);\n```\n\nXPathNavigator (prior to 4.5.2):\n```csharp\nXmlReader reader = XmlReader.Create(\"example.xml\");\nXPathDocument doc = new XPathDocument(reader);\nXPathNavigator nav = doc.CreateNavigator();\nstring xml = nav.InnerXml.ToString();\n```\n\n### C/C++\n\nlibxml2: Avoid XML_PARSE_NOENT and XML_PARSE_DTDLOAD options.\n\nlibxerces-c:\n```cpp\nXercesDOMParser *parser = new XercesDOMParser;\nparser->setCreateEntityReferenceNodes(true);\nparser->setDisableDefaultEntityResolution(true);\n\nSAXParser* parser = new SAXParser;\nparser->setDisableDefaultEntityResolution(true);\n\nSAX2XMLReader* reader = XMLReaderFactory::createXMLReader();\nparser->setFeature(XMLUni::fgXercesDisableDefaultEntityResolution, true);\n```\n\n### PHP\n\nPHP 8.0+ prevents XXE by default. Earlier versions:\n```php\nlibxml_set_external_entity_loader(null);\n```\n\n### Python\n\n```python\nfrom defusedxml import ElementTree as ET\ntree = ET.parse('filename.xml')\n\n# Or with lxml:\nfrom lxml import etree\nparser = etree.XMLParser(resolve_entities=False, no_network=True)\ntree = etree.parse('filename.xml', parser)\n```\n\n### iOS/macOS\n\n```swift\nlet options: NSXMLNodeOptions = .documentTidyXML\nlet xmlDoc = try NSXMLDocument(data: data, options: options.union(.nodeLoadExternalEntitiesNever))\n```\n\n### ColdFusion\n\nAdobe ColdFusion:\n```\n<cfset parseroptions = structnew()>\n<cfset parseroptions.ALLOWEXTERNALENTITIES = false>\n<cfscript>\na = XmlParse(\"xml.xml\", false, parseroptions);\n</cfscript>\n```\n\nLucee (Application.cfc):\n```\nthis.xmlFeatures = {\n     externalGeneralEntities: false,\n     secure: true,\n     disallowDoctypeDecl: true\n};\n```\n\n### Additional Measures\n\n- Update XML libraries regularly\n- Validate XML input before parsing\n- Use static analysis tools for XXE detection\n- Test with XXE payloads in safe environments\n\n### When DTDs Required\n\nIf DTDs absolutely necessary:\n- Use custom EntityResolver with restricted entities\n- Implement strict entity allowlisting\n- Preprocess XML to remove dangerous DOCTYPE declarations"
  },
  {
    "path": "sources/owasp/codeguard-0-xml-security.md",
    "content": "---\ndescription: XML Security Rule\nlanguages:\n- xml\nalwaysApply: false\n---\n\nEnforce robust XML security practices to prevent XXE, SSRF, DoS, schema poisoning, and data integrity issues in XML parsing and validation.\n\n- Always reject unexpected elements, attributes, or data outside the schema definitions.\n- Perform business logic validation on XML data after schema validation (e.g., numeric ranges on payment amounts).\n- Keep XML processing libraries up to date and use secure configurations by default.\n\nUse a standards-compliant XML parser configured to disable DTD processing and external entity resolution to prevent XXE and entity expansion attacks.\n- Ensure your XML parser rejects malformed XML documents and halts processing on fatal errors.\n- Configure parser features such as `disallow-doctype-decl`, `external-general-entities`, and `external-parameter-entities` set to false or disabled.\n\nValidate all XML documents strictly against local, trusted XML Schemas (XSDs) with narrow data type restrictions and clearly defined element occurrence limits.\n- Avoid or limit the use of DTDs; prefer comprehensive XSD validation.\n- Use schemas with explicit types, length limits, regex patterns, enumerations, and xs:assertion where appropriate.\n- Set maxOccurs explicitly to control element multiplicity.\n- Store schemas locally with strict filesystem permissions and never load schemas over unencrypted HTTP.\n\nPrevent resource exhaustion by rejecting XML documents with excessive depth, nested unclosed tags, or large size to avoid DoS.\n- Enforce limits on XML element nesting depth and document size.\n- Test parser CPU usage differences between valid and malformed XML inputs.\n- Reject or timeout processing on unexpectedly complex documents.\n\nBlock or sandbox XML processing from making remote network calls to mitigate SSRF and information disclosure risks.\n- Disable external entity resolution or restrict it to local, whitelisted resources only.\n      - Validate and sanitize all external URI references in XML entities.\n- Monitor for unexpected DNS lookups or network activity during XML parsing.\n\nLog and monitor XML parsing errors and rejections to detect injection attempts, malformed XML attacks, or schema poisoning.\n- Capture detailed error information without exposing sensitive data.\n - Alert on repeated or suspicious XML parse failures.\n- Audit local schema files regularly for unauthorized changes."
  },
  {
    "path": "sources/owasp/codeguard-0-xs-leaks.md",
    "content": "---\ndescription: Preventing Cross-Site Leaks (XS-Leaks)\nlanguages:\n- c\n- javascript\n- typescript\nalwaysApply: false\n---\n\nProtecting your applications from Cross-Site Leaks is crucial for safeguarding user privacy. XS-Leaks are a class of vulnerabilities that exploit subtle browser behaviors to extract sensitive user information across origins. \n\nXS-Leaks occur when an attacker's website can infer information about a user's state on another website through side-channels like:\n\n- Error messages\n- Frame counting\n- Resource timing\n- Cache probing\n- Response size detection\n\nThese attacks can reveal sensitive information such as whether a user is logged in, specific account details, or even extract data from cross-origin resources.\n\nProperly configured cookies are your first line of defense against XS-Leaks. For example:\n\n```javascript\n// Setting cookies in JavaScript with secure attributes\ndocument.cookie = \"sessionId=abc123; SameSite=Strict; Secure; HttpOnly; Path=/\";\n```\n\nFor server-side cookie setting (example in Express.js):\n\n```javascript\napp.use(session({\n  secret: 'your-secret-key',\n  cookie: {\n    sameSite: 'strict',  // Options: strict, lax, none\n    secure: true,         // Requires HTTPS\n    httpOnly: true        // Prevents JavaScript access\n  }\n}));\n```\n\nIn your HTTP response headers:\n\n```http\nSet-Cookie: sessionId=abc123; SameSite=Strict; Secure; HttpOnly; Path=/\n```\n\n\n\n* Always specify a `SameSite` attribute:\n  * Use `SameSite=Strict` for cookies related to sensitive actions\n  * Use `SameSite=Lax` for cookies needed on normal navigation to your site\n  * Use `SameSite=None; Secure` only when third-party usage is absolutely required\n\n* Never rely on browser defaults as they may vary across browsers and versions\n\n### Framing Protection\n\nPrevent your site from being framed by potentially malicious sites:\n\n```javascript\n// In your Express.js application\napp.use((req, res, next) => {\n  // CSP frame-ancestors directive (modern approach)\n  res.setHeader(\n    'Content-Security-Policy',\n    \"frame-ancestors 'self' https://trusted-parent.com\"\n  );\n  \n  // X-Frame-Options (legacy fallback)\n  res.setHeader('X-Frame-Options', 'SAMEORIGIN');\n  \n  next();\n});\n```\n\nUse Fetch Metadata headers to detect and block suspicious cross-origin requests:\n\n```javascript\n// Express.js middleware for protecting sensitive endpoints\nfunction secureEndpoint(req, res, next) {\n  // Get Fetch Metadata headers\n  const fetchSite = req.get('Sec-Fetch-Site') || 'unknown';\n  const fetchMode = req.get('Sec-Fetch-Mode') || 'unknown';\n  const fetchDest = req.get('Sec-Fetch-Dest') || 'unknown';\n  \n  // Block cross-site requests to sensitive endpoints\n  if (fetchSite === 'cross-site' && req.path.startsWith('/api/sensitive')) {\n    return res.status(403).send('Cross-site requests not allowed');\n  }\n  \n  // Block embedding in iframes from untrusted sites\n  if (fetchDest === 'iframe' && fetchSite === 'cross-site') {\n    return res.status(403).send('Embedding not allowed');\n  }\n  \n  next();\n}\n\napp.use(secureEndpoint);\n```\n\n### Secure Cross-Origin Communication\n\nWhen using `postMessage` for cross-origin communication:\n\n```javascript\n// UNSAFE - Never do this\nwindow.postMessage(sensitiveData, '*');\n\n// SAFE - Always specify the exact target origin\nwindow.postMessage(sensitiveData, 'https://trusted-receiver.com');\n\n// When receiving messages, always verify the origin\nwindow.addEventListener('message', (event) => {\n  // Always verify message origin\n  if (event.origin !== 'https://trusted-sender.com') {\n    console.error('Received message from untrusted origin:', event.origin);\n    return;\n  }\n  \n  // Process the message\n  processMessage(event.data);\n});\n```\n\n### Isolating Browsing Contexts\n\nUse Cross-Origin-Opener-Policy (COOP) to isolate your site from potential attackers:\n\n```http\nCross-Origin-Opener-Policy: same-origin\n```\n\nIn Express.js:\n\n```javascript\napp.use((req, res, next) => {\n  res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');\n  next();\n});\n```\n\nFor maximum isolation, combine with Cross-Origin-Embedder-Policy (COEP):\n\n```javascript\napp.use((req, res, next) => {\n  res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');\n  res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp');\n  next();\n});\n```\n\n### Preventing Cache-Based Leaks\n\nProtect sensitive resources from cache probing attacks:\n\n```javascript\n// Express.js middleware for sensitive endpoints\napp.get('/api/sensitive-data', (req, res) => {\n  // Add user-specific token to prevent cache probing\n  const userToken = req.user.securityToken;\n  \n  // Disable caching for sensitive resources\n  res.setHeader('Cache-Control', 'no-store');\n  res.setHeader('Pragma', 'no-cache');\n  \n  // Add user token to response to ensure uniqueness\n  const data = { userToken, sensitiveData: 'secret information' };\n  res.json(data);\n});\n```\n\nFor static resources that might reveal user state:\n\n```javascript\n// Add user-specific tokens to URLs of sensitive resources\nfunction getUserSpecificUrl(baseUrl) {\n  const userToken = generateUserToken();\n  return `${baseUrl}?token=${userToken}`;\n}\n\nconst profileImageUrl = getUserSpecificUrl('/images/profile.jpg');\n```\n\n### Comprehensive Defense Strategy\n\nImplement these headers for a robust defense against XS-Leaks:\n\n```javascript\napp.use((req, res, next) => {\n  // Framing protection\n  res.setHeader('Content-Security-Policy', \"frame-ancestors 'self'\");\n  res.setHeader('X-Frame-Options', 'SAMEORIGIN');\n  \n  // Resource isolation\n  res.setHeader('Cross-Origin-Resource-Policy', 'same-origin');\n  res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');\n  \n  // Cache control for dynamic content\n  if (req.path.startsWith('/api/')) {\n    res.setHeader('Cache-Control', 'no-store');\n  }\n  \n  next();\n});\n```"
  },
  {
    "path": "sources/owasp/codeguard-0-xss-filter-evasion.md",
    "content": "---\ndescription: XSS Filter Evasion Prevention - Advanced techniques attackers use to\n  bypass input filtering and blacklists\nlanguages:\n- c\n- javascript\n- typescript\nalwaysApply: false\n---\n\nRelying solely on input filtering or blacklists is insufficient because attackers use numerous techniques to bypass these defenses:\n\n- Mixed encoding schemes: Combining HTML, URL, and Unicode encodings\n- Whitespace manipulation: Using tabs, newlines, and other whitespace characters to confuse parsers\n- Malformed tags: Creating deliberately broken HTML that browsers will \"fix\" during rendering\n- Obfuscation: Using JavaScript encoding functions like `String.fromCharCode()` to hide malicious code\n\n### Context-Aware Output Encoding\n\nThe most effective defense is to apply the appropriate encoding based on where the data will be used:\n\n#### HTML Context (Content between tags)\n\n```javascript\n// VULNERABLE\nconst userName = request.getParameter(\"user\");\ndocument.getElementById(\"welcome\").innerHTML = \"Hello, \" + userName;\n\n// SECURE\nimport { encodeForHTML } from 'your-encoding-library';\nconst userName = request.getParameter(\"user\");\ndocument.getElementById(\"welcome\").innerHTML = \"Hello, \" + encodeForHTML(userName);\n```\n\n#### HTML Attribute Context\n\n```javascript\n// VULNERABLE\nconst userColor = request.getParameter(\"color\");\ndocument.getElementById(\"profile\").innerHTML = \n  `<div class=\"profile\" style=\"background-color:${userColor}\">Profile</div>`;\n\n// SECURE\nimport { encodeForHTMLAttribute } from 'your-encoding-library';\nconst userColor = request.getParameter(\"color\");\ndocument.getElementById(\"profile\").innerHTML = \n  `<div class=\"profile\" style=\"background-color:${encodeForHTMLAttribute(userColor)}\">Profile</div>`;\n```\n\n#### JavaScript Context\n\n```javascript\n// VULNERABLE\nconst userInput = request.getParameter(\"input\");\nconst script = document.createElement(\"script\");\nscript.textContent = `const userValue = \"${userInput}\";`;\n\n// SECURE\nimport { encodeForJavaScript } from 'your-encoding-library';\nconst userInput = request.getParameter(\"input\");\nconst script = document.createElement(\"script\");\nscript.textContent = `const userValue = \"${encodeForJavaScript(userInput)}\";`;\n```\n\n#### URL Context\n\n```javascript\n// VULNERABLE\nconst redirectUrl = request.getParameter(\"url\");\nlocation.href = redirectUrl;\n\n// SECURE\nimport { encodeForURL } from 'your-encoding-library';\nconst redirectUrl = request.getParameter(\"url\");\n// Validate URL pattern first\nif (isValidRedirectURL(redirectUrl)) {\n  location.href = encodeForURL(redirectUrl);\n}\n```\n\n#### CSS Context\n\n```javascript\n// VULNERABLE\nconst userTheme = request.getParameter(\"theme\");\ndocument.getElementById(\"custom\").style = userTheme;\n\n// SECURE\nimport { encodeForCSS } from 'your-encoding-library';\nconst userTheme = request.getParameter(\"theme\");\ndocument.getElementById(\"custom\").style = encodeForCSS(userTheme);\n```\n\n### Using Established Sanitization Libraries\n\nAvoid creating your own sanitization logic. Use well-maintained libraries instead:\n\n#### JavaScript/DOM\n\n```javascript\n// Using DOMPurify\nimport DOMPurify from 'dompurify';\n\nfunction displayUserContent(content) {\n  // Configure DOMPurify to only allow specific tags and attributes\n  const config = {\n    ALLOWED_TAGS: ['b', 'i', 'em', 'strong', 'a', 'p', 'ul', 'ol', 'li'],\n    ALLOWED_ATTR: ['href', 'target']\n  };\n  \n  const sanitized = DOMPurify.sanitize(content, config);\n  document.getElementById('user-content').innerHTML = sanitized;\n}\n```\n\n#### Java\n\n```java\n// Using OWASP Java Encoder\nimport org.owasp.encoder.Encode;\n\n@Controller\npublic class UserController {\n    @GetMapping(\"/profile\")\n    public String showProfile(Model model, @RequestParam String username) {\n        model.addAttribute(\"encodedUsername\", Encode.forHtml(username));\n        return \"profile\";\n    }\n}\n\n// In your template (e.g., Thymeleaf)\n// <div th:text=\"${encodedUsername}\">Username</div>\n```\n\n#### PHP\n\n```php\n// Using HTMLPurifier\nrequire_once 'HTMLPurifier.auto.php';\n\n$config = HTMLPurifier_Config::createDefault();\n$purifier = new HTMLPurifier($config);\n\n$userBio = $_POST['bio'];\n$cleanBio = $purifier->purify($userBio);\n\necho '<div class=\"bio\">' . $cleanBio . '</div>';\n```\n\n### Avoiding Dangerous Patterns\n\nCertain coding patterns are particularly vulnerable to XSS attacks:\n\n#### Avoid Unsafe JavaScript APIs\n\n```javascript\n// DANGEROUS - Never do this with user input\neval(userInput);\ndocument.write(userInput);\nnew Function(userInput);\nsetTimeout(userInput, 100);\nsetInterval(userInput, 100);\nelement.innerHTML = userInput;\n\n// SAFER ALTERNATIVES\n// Instead of eval, parse JSON safely\nconst data = JSON.parse(userInput);\n\n// Instead of innerHTML, use textContent\nelement.textContent = userInput;\n\n// Or create elements properly\nconst div = document.createElement('div');\ndiv.textContent = userInput;\nparentElement.appendChild(div);\n```\n\n#### Avoid Inline Scripts and Event Handlers\n\n```html\n<!-- DANGEROUS - Inline event handlers are vulnerable to XSS -->\n<button onclick=\"doSomething('<?php echo $userInput; ?>')\">Click me</button>\n\n<!-- SAFER - Use addEventListener instead -->\n<button id=\"safeButton\">Click me</button>\n<script>\n  document.getElementById('safeButton').addEventListener('click', function() {\n    doSomething(sanitizedUserInput);\n  });\n</script>\n```\n\n### Defense in Depth Strategy\n\nImplement multiple layers of protection:\n\n#### Content Security Policy (CSP)\n\n```http\n# Strong CSP header that blocks inline scripts and restricts sources\nContent-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none'; style-src 'self'; img-src 'self' data:;\n```\n\n```javascript\n// Setting CSP in Express.js\nconst helmet = require('helmet');\napp.use(helmet.contentSecurityPolicy({\n  directives: {\n    defaultSrc: [\"'self'\"],\n    scriptSrc: [\"'self'\", 'trusted-cdn.com'],\n    objectSrc: [\"'none'\"],\n    styleSrc: [\"'self'\"],\n    imgSrc: [\"'self'\", 'data:']\n  }\n}));\n```\n\n#### Secure Cookie Configuration\n\n```http\nSet-Cookie: sessionId=abc123; HttpOnly; Secure; SameSite=Strict\n```\n\n#### Input Validation\n\n```javascript\n// Validate input format before processing\nfunction validateUsername(username) {\n  // Only allow alphanumeric characters and limited symbols\n  const usernameRegex = /^[a-zA-Z0-9_.-]{3,30}$/;\n  if (!usernameRegex.test(username)) {\n    throw new Error('Invalid username format');\n  }\n  return username;\n}\n```"
  },
  {
    "path": "sources/owasp/codeguard-0-zero-trust-architecture.md",
    "content": "---\ndescription: Zero Trust Architecture Implementation - Security principles for designing\n  systems with no implicit trust\nlanguages:\n- c\n- go\n- java\n- javascript\n- kotlin\n- php\n- python\n- ruby\n- scala\n- shell\n- swift\n- typescript\n- yaml\nalwaysApply: false\n---\n\n## Implementing Zero Trust Architecture\n\nImplementing Zero Trust Architecture (ZTA) principles in your applications is essential for modern security.\n\nZero Trust is built on the principle of \"never trust, always verify\" and assumes that threats exist both outside and inside the network. Key concepts include:\n\n- No implicit trust based on network location or asset ownership\n- Continuous verification of identity and device health\n- Least privilege access to resources and data\n- Microsegmentation of networks and applications\n- Continuous monitoring and analytics for threat detection\n\n### Authentication & Authorization\n\n- Implement Strong Authentication using FIDO2/WebAuthn\n\n- Implement Context-Aware Authorization\nImplement authorization that considers multiple factors:\n\n```java\n// Java example of context-aware authorization\npublic class ZeroTrustAuthorizationService {\n    public boolean authorizeAccess(User user, Resource resource, AccessContext context) {\n        // 1. Verify user identity\n        if (!identityService.verifyIdentity(user)) {\n            logFailedAttempt(\"Identity verification failed\", user, resource, context);\n            return false;\n        }\n\n        // 2. Check device health and compliance\n        if (!deviceService.isCompliant(context.getDeviceId())) {\n            logFailedAttempt(\"Device not compliant\", user, resource, context);\n            return false;\n        }\n\n        // 3. Evaluate risk score based on multiple factors\n        int riskScore = riskEngine.calculateScore(user, resource, context);\n        if (riskScore > ACCEPTABLE_THRESHOLD) {\n            logFailedAttempt(\"Risk score too high\", user, resource, context);\n            return false;\n        }\n\n        // 4. Check if user has required permissions\n        if (!permissionService.hasPermission(user, resource, context.getRequestedAction())) {\n            logFailedAttempt(\"Insufficient permissions\", user, resource, context);\n            return false;\n        }\n\n        // 5. Log successful access\n        auditLogger.logAccess(user, resource, context);\n        return true;\n    }\n}\n```\n\n- Implement Short-Lived Access Tokens\n\nImplement token-based authentication with short lifetimes:\n\n```python\n# Python example using JWT with short expiration\nimport jwt\nfrom datetime import datetime, timedelta\n\ndef generate_access_token(user_id, device_id, permissions):\n    # Set token to expire in 15 minutes\n    expiration = datetime.utcnow() + timedelta(minutes=15)\n\n    payload = {\n        'sub': user_id,\n        'device_id': device_id,\n        'permissions': permissions,\n        'exp': expiration,\n        'iat': datetime.utcnow(),\n        'jti': str(uuid.uuid4())  # Unique token ID\n    }\n\n    # Sign with appropriate algorithm and key\n    token = jwt.encode(payload, SECRET_KEY, algorithm='ES256')\n\n    # Store token metadata for potential revocation\n    store_token_metadata(user_id, payload['jti'], device_id, expiration)\n\n    return token\n```\n\n### Secure Communication\n\n- Implement TLS 1.3 for all communications\n\n- Implement API security measures:\n\n```typescript\n// TypeScript example of API security middleware\nimport express from 'express';\nimport rateLimit from 'express-rate-limit';\nimport helmet from 'helmet';\n\nconst app = express();\n\n// Set security headers\napp.use(helmet());\n\n// Rate limiting\nconst apiLimiter = rateLimit({\n  windowMs: 15 * 60 * 1000, // 15 minutes\n  max: 100, // limit each IP to 100 requests per windowMs\n  standardHeaders: true,\n  legacyHeaders: false,\n});\napp.use('/api/', apiLimiter);\n\n// API authentication middleware\napp.use('/api/', (req, res, next) => {\n  const token = req.headers.authorization?.split(' ')[1];\n\n  if (!token) {\n    return res.status(401).json({ error: 'Authentication required' });\n  }\n\n  try {\n    // Verify token and extract user info\n    const user = verifyAndDecodeToken(token);\n\n    // Check if token has been revoked\n    if (isTokenRevoked(token)) {\n      return res.status(401).json({ error: 'Token revoked' });\n    }\n\n    // Add user info to request for downstream handlers\n    req.user = user;\n    next();\n  } catch (error) {\n    return res.status(401).json({ error: 'Invalid token' });\n  }\n});\n\n// Payload validation middleware\napp.use(express.json({\n  verify: (req, res, buf) => {\n    try {\n      // Check if JSON is valid and meets schema requirements\n      validateSchema(buf.toString(), req.path);\n    } catch (e) {\n      throw new Error('Invalid JSON payload');\n    }\n  },\n  limit: '100kb' // Limit payload size\n}));\n```\n\n### Monitoring and Logging\n\n- Implement Comprehensive Logging\n\n```csharp\n// C# example of detailed security logging\npublic class SecurityLogger\n{\n    private readonly ILogger _logger;\n\n    public SecurityLogger(ILogger logger)\n    {\n        _logger = logger;\n    }\n\n    public void LogAccessAttempt(string userId, string resourceId, bool success, AccessContext context)\n    {\n        var logEvent = new SecurityEvent\n        {\n            EventType = success ? \"access_granted\" : \"access_denied\",\n            Timestamp = DateTime.UtcNow,\n            UserId = userId,\n            ResourceId = resourceId,\n            IpAddress = context.IpAddress,\n            DeviceId = context.DeviceId,\n            DeviceHealth = context.DeviceHealthStatus,\n            Location = context.GeoLocation,\n            RequestedPermissions = context.RequestedPermissions,\n            RiskScore = context.RiskScore\n        };\n\n        // Log with appropriate level\n        if (success)\n        {\n            _logger.LogInformation(\"Access granted: {Event}\", JsonSerializer.Serialize(logEvent));\n        }\n        else\n        {\n            _logger.LogWarning(\"Access denied: {Event}\", JsonSerializer.Serialize(logEvent));\n        }\n    }\n}\n```\n\n### Implement fine-grained network and application segmentation\n\n\n```yaml\n# Kubernetes Network Policy example for microsegmentation\napiVersion: networking.k8s.io/v1\nkind: NetworkPolicy\nmetadata:\n  name: api-backend-policy\n  namespace: production\nspec:\n  podSelector:\n    matchLabels:\n      app: api-backend\n  policyTypes:\n  - Ingress\n  - Egress\n  ingress:\n  - from:\n    - podSelector:\n        matchLabels:\n          app: frontend\n    ports:\n    - protocol: TCP\n      port: 443\n  egress:\n  - to:\n    - podSelector:\n        matchLabels:\n          app: database\n    ports:\n    - protocol: TCP\n      port: 5432\n  - to:\n    - namespaceSelector:\n        matchLabels:\n          name: monitoring\n      podSelector:\n        matchLabels:\n          app: telemetry\n    ports:\n    - protocol: TCP\n      port: 9090\n```"
  },
  {
    "path": "sources/templates/custom-rule-template.md.example",
    "content": "---\ndescription: Brief description of the rule\nlanguages:\n- python\n- javascript\ntags:\n- data-security\nalwaysApply: false\n---\n\n<!-- The content below is flexible - structure it to fit your use case -->\n\n## Rule Title\n\nBrief overview of what this rule enforces and why it matters.\n\n### Section Name\n- Guidance point with specific recommendation\n- Another specific requirement or best practice\n\n### Another Section\n- Additional guidance organized by topic\n- Keep points concise and actionable\n\n### Implementation Checklist\n- Verify requirement one is met\n- Confirm requirement two is implemented\n- Check that patterns are followed\n\n### Test Plan\n- Describe how to verify the rule is followed\n- Include specific test scenarios\n"
  },
  {
    "path": "src/LICENSE.md",
    "content": "                                 Apache License\n                           Version 2.0, January 2004\n                        http://www.apache.org/licenses/\n\n   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION\n\n   1. Definitions.\n\n      \"License\" shall mean the terms and conditions for use, reproduction,\n      and distribution as defined by Sections 1 through 9 of this document.\n\n      \"Licensor\" shall mean the copyright owner or entity authorized by\n      the copyright owner that is granting the License.\n\n      \"Legal Entity\" shall mean the union of the acting entity and all\n      other entities that control, are controlled by, or are under common\n      control with that entity. For the purposes of this definition,\n      \"control\" means (i) the power, direct or indirect, to cause the\n      direction or management of such entity, whether by contract or\n      otherwise, or (ii) ownership of fifty percent (50%) or more of the\n      outstanding shares, or (iii) beneficial ownership of such entity.\n\n      \"You\" (or \"Your\") shall mean an individual or Legal Entity\n      exercising permissions granted by this License.\n\n      \"Source\" form shall mean the preferred form for making modifications,\n      including but not limited to software source code, documentation\n      source, and configuration files.\n\n      \"Object\" form shall mean any form resulting from mechanical\n      transformation or translation of a Source form, including but\n      not limited to compiled object code, generated documentation,\n      and conversions to other media types.\n\n      \"Work\" shall mean the work of authorship, whether in Source or\n      Object form, made available under the License, as indicated by a\n      copyright notice that is included in or attached to the work\n      (an example is provided in the Appendix below).\n\n      \"Derivative Works\" shall mean any work, whether in Source or Object\n      form, that is based on (or derived from) the Work and for which the\n      editorial revisions, annotations, elaborations, or other modifications\n      represent, as a whole, an original work of authorship. For the purposes\n      of this License, Derivative Works shall not include works that remain\n      separable from, or merely link (or bind by name) to the interfaces of,\n      the Work and Derivative Works thereof.\n\n      \"Contribution\" shall mean any work of authorship, including\n      the original version of the Work and any modifications or additions\n      to that Work or Derivative Works thereof, that is intentionally\n      submitted to Licensor for inclusion in the Work by the copyright owner\n      or by an individual or Legal Entity authorized to submit on behalf of\n      the copyright owner. For the purposes of this definition, \"submitted\"\n      means any form of electronic, verbal, or written communication sent\n      to the Licensor or its representatives, including but not limited to\n      communication on electronic mailing lists, source code control systems,\n      and issue tracking systems that are managed by, or on behalf of, the\n      Licensor for the purpose of discussing and improving the Work, but\n      excluding communication that is conspicuously marked or otherwise\n      designated in writing by the copyright owner as \"Not a Contribution.\"\n\n      \"Contributor\" shall mean Licensor and any individual or Legal Entity\n      on behalf of whom a Contribution has been received by Licensor and\n      subsequently incorporated within the Work.\n\n   2. Grant of Copyright License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      copyright license to reproduce, prepare Derivative Works of,\n      publicly display, publicly perform, sublicense, and distribute the\n      Work and such Derivative Works in Source or Object form.\n\n   3. Grant of Patent License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      (except as stated in this section) patent license to make, have made,\n      use, offer to sell, sell, import, and otherwise transfer the Work,\n      where such license applies only to those patent claims licensable\n      by such Contributor that are necessarily infringed by their\n      Contribution(s) alone or by combination of their Contribution(s)\n      with the Work to which such Contribution(s) was submitted. If You\n      institute patent litigation against any entity (including a\n      cross-claim or counterclaim in a lawsuit) alleging that the Work\n      or a Contribution incorporated within the Work constitutes direct\n      or contributory patent infringement, then any patent licenses\n      granted to You under this License for that Work shall terminate\n      as of the date such litigation is filed.\n\n   4. Redistribution. You may reproduce and distribute copies of the\n      Work or Derivative Works thereof in any medium, with or without\n      modifications, and in Source or Object form, provided that You\n      meet the following conditions:\n\n      (a) You must give any other recipients of the Work or\n          Derivative Works a copy of this License; and\n\n      (b) You must cause any modified files to carry prominent notices\n          stating that You changed the files; and\n\n      (c) You must retain, in the Source form of any Derivative Works\n          that You distribute, all copyright, patent, trademark, and\n          attribution notices from the Source form of the Work,\n          excluding those notices that do not pertain to any part of\n          the Derivative Works; and\n\n      (d) If the Work includes a \"NOTICE\" text file as part of its\n          distribution, then any Derivative Works that You distribute must\n          include a readable copy of the attribution notices contained\n          within such NOTICE file, excluding those notices that do not\n          pertain to any part of the Derivative Works, in at least one\n          of the following places: within a NOTICE text file distributed\n          as part of the Derivative Works; within the Source form or\n          documentation, if provided along with the Derivative Works; or,\n          within a display generated by the Derivative Works, if and\n          wherever such third-party notices normally appear. The contents\n          of the NOTICE file are for informational purposes only and\n          do not modify the License. You may add Your own attribution\n          notices within Derivative Works that You distribute, alongside\n          or as an addendum to the NOTICE text from the Work, provided\n          that such additional attribution notices cannot be construed\n          as modifying the License.\n\n      You may add Your own copyright statement to Your modifications and\n      may provide additional or different license terms and conditions\n      for use, reproduction, or distribution of Your modifications, or\n      for any such Derivative Works as a whole, provided Your use,\n      reproduction, and distribution of the Work otherwise complies with\n      the conditions stated in this License.\n\n   5. Submission of Contributions. Unless You explicitly state otherwise,\n      any Contribution intentionally submitted for inclusion in the Work\n      by You to the Licensor shall be under the terms and conditions of\n      this License, without any additional terms or conditions.\n      Notwithstanding the above, nothing herein shall supersede or modify\n      the terms of any separate license agreement you may have executed\n      with Licensor regarding such Contributions.\n\n   6. Trademarks. This License does not grant permission to use the trade\n      names, trademarks, service marks, or product names of the Licensor,\n      except as required for reasonable and customary use in describing the\n      origin of the Work and reproducing the content of the NOTICE file.\n\n   7. Disclaimer of Warranty. Unless required by applicable law or\n      agreed to in writing, Licensor provides the Work (and each\n      Contributor provides its Contributions) on an \"AS IS\" BASIS,\n      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n      implied, including, without limitation, any warranties or conditions\n      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A\n      PARTICULAR PURPOSE. You are solely responsible for determining the\n      appropriateness of using or redistributing the Work and assume any\n      risks associated with Your exercise of permissions under this License.\n\n   8. Limitation of Liability. In no event and under no legal theory,\n      whether in tort (including negligence), contract, or otherwise,\n      unless required by applicable law (such as deliberate and grossly\n      negligent acts) or agreed to in writing, shall any Contributor be\n      liable to You for damages, including any direct, indirect, special,\n      incidental, or consequential damages of any character arising as a\n      result of this License or out of the use or inability to use the\n      Work (including but not limited to damages for loss of goodwill,\n      work stoppage, computer failure or malfunction, or any and all\n      other commercial damages or losses), even if such Contributor\n      has been advised of the possibility of such damages.\n\n   9. Accepting Warranty or Additional Liability. While redistributing\n      the Work or Derivative Works thereof, You may choose to offer,\n      and charge a fee for, acceptance of support, warranty, indemnity,\n      or other liability obligations and/or rights consistent with this\n      License. However, in accepting such obligations, You may act only\n      on Your own behalf and on Your sole responsibility, not on behalf\n      of any other Contributor, and only if You agree to indemnify,\n      defend, and hold each Contributor harmless for any liability\n      incurred by, or claims asserted against, such Contributor by reason\n      of your accepting any such warranty or additional liability.\n\n   END OF TERMS AND CONDITIONS\n"
  },
  {
    "path": "src/convert_to_ide_formats.py",
    "content": "# Copyright 2025 Cisco Systems, Inc. and its affiliates\n#\n# SPDX-License-Identifier: Apache-2.0\n\n\"\"\"\nConvert Unified Rules to IDE Formats\n\nTransforms the unified markdown sources into IDE-specific bundles (Cursor,\nWindsurf, Copilot, Agent Skills, Antigravity). This script is the main entry point\nfor producing distributable rule packs from the sources/ directory.\n\"\"\"\n\nimport re\nimport shutil\nfrom pathlib import Path\nfrom collections import defaultdict\n\nfrom converter import RuleConverter\nfrom formats import (\n    CursorFormat,\n    WindsurfFormat,\n    CopilotFormat,\n    AgentSkillsFormat,\n    AntigravityFormat,\n)\nfrom utils import get_version_from_pyproject\nfrom validate_versions import set_plugin_version, set_marketplace_version\n\n# Project root is always one level up from src/\nPROJECT_ROOT = Path(__file__).parent.parent\n\n\ndef sync_plugin_metadata(version: str) -> None:\n    \"\"\"\n    Sync version from pyproject.toml to Agent Skills metadata files.\n\n    Args:\n        version: Version string from pyproject.toml\n    \"\"\"\n    set_plugin_version(version, PROJECT_ROOT)\n    set_marketplace_version(version, PROJECT_ROOT)\n    print(f\"✅ Synced plugin metadata to {version}\")\n\n\ndef matches_tag_filter(rule_tags: list[str], filter_tags: list[str]) -> bool:\n    \"\"\"\n    Check if rule has all required tags (AND logic).\n\n    Args:\n        rule_tags: List of tags from the rule (already normalized to lowercase)\n        filter_tags: List of tags to filter by (already normalized to lowercase)\n\n    Returns:\n        True if rule has all filter tags (or no filter), False otherwise\n    \"\"\"\n    if not filter_tags:\n        return True  # No filter means all pass\n\n    return all(tag in rule_tags for tag in filter_tags)\n\n\ndef update_skill_md(language_to_rules: dict[str, list[str]], skill_path: str) -> None:\n    \"\"\"\n    Update SKILL.md with language-to-rules mapping table.\n\n    Args:\n        language_to_rules: Dictionary mapping languages to rule files\n        skill_path: Path to SKILL.md file\n    \"\"\"\n    # Generate markdown table\n    table_lines = [\n        \"| Language | Rule Files to Apply |\",\n        \"|----------|---------------------|\",\n    ]\n\n    for language in sorted(language_to_rules.keys()):\n        rules = sorted(language_to_rules[language])\n        rules_str = \", \".join(rules)\n        table_lines.append(f\"| {language} | {rules_str} |\")\n\n    table = \"\\n\".join(table_lines)\n\n    # Markers for the language mappings section\n    start_marker = \"<!-- LANGUAGE_MAPPINGS_START -->\"\n    end_marker = \"<!-- LANGUAGE_MAPPINGS_END -->\"\n\n    # Read SKILL.md\n    skill_file = Path(skill_path)\n    content = skill_file.read_text(encoding=\"utf-8\")\n\n    if start_marker not in content or end_marker not in content:\n        raise RuntimeError(\n            \"Invalid template: Language mappings section not found in codeguard-SKILLS.md.template\"\n        )\n\n    # Replace entire section including markers with just the table\n    start_idx = content.index(start_marker)\n    end_idx = content.index(end_marker) + len(end_marker)\n    new_section = f\"\\n\\n{table}\\n\\n\"\n    updated_content = content[:start_idx] + new_section + content[end_idx:]\n\n    # Write back to SKILL.md\n    skill_file.write_text(updated_content, encoding=\"utf-8\")\n    print(f\"Updated SKILL.md with language mappings\")\n\n\ndef convert_rules(\n    input_path: str,\n    output_dir: str = \"dist\",\n    include_agentskills: bool = True,\n    version: str = None,\n    filter_tags: list[str] = None,\n) -> dict[str, list[str]]:\n    \"\"\"\n    Convert rule file(s) to all supported IDE formats using RuleConverter.\n\n    Args:\n        input_path: Path to a single .md file or folder containing .md files\n        output_dir: Output directory (default: 'dist/')\n        include_agentskills: Whether to generate Agent Skills format (default: True, only for core rules)\n        version: Version string to use (default: read from pyproject.toml)\n        filter_tags: Optional list of tags to filter by (AND logic, case-insensitive)\n\n    Returns:\n        Dictionary with 'success' and 'errors' lists:\n        {\n            \"success\": [\"rule1.md\", \"rule2.md\"],\n            \"errors\": [\"rule3.md: error message\"]\n        }\n\n    Example:\n        results = convert_rules(\"sources/core\", \"dist\", include_agentskills=True)\n        print(f\"Converted {len(results['success'])} rules\")\n    \"\"\"\n    if version is None:\n        version = get_version_from_pyproject()\n\n    # Specify formats to generate\n    all_formats = [\n        CursorFormat(version),\n        WindsurfFormat(version),\n        CopilotFormat(version),\n        AntigravityFormat(version),\n    ]\n\n    # Only include Agent Skills format for core rules (committed as skills)\n    if include_agentskills:\n        all_formats.append(AgentSkillsFormat(version))\n\n    converter = RuleConverter(formats=all_formats)\n    path = Path(input_path)\n\n    if not path.exists():\n        raise FileNotFoundError(f\"{input_path} does not exist\")\n\n    # Determine files to process\n    if path.is_file():\n        if path.suffix != \".md\":\n            raise ValueError(f\"{input_path} is not a .md file\")\n        md_files = [path]\n    else:\n        # Use rglob to recursively find all .md files in subdirectories\n        md_files = sorted(list(path.rglob(\"*.md\")))\n        if not md_files:\n            raise ValueError(f\"No .md files found in {input_path}\")\n\n    print(f\"Converting {len(md_files)} files from: {path}\")\n\n    # Setup output directory\n    output_base = Path(output_dir)\n\n    results = {\"success\": [], \"errors\": [], \"skipped\": []}\n    language_to_rules = defaultdict(list)\n\n    # Process each file\n    for md_file in md_files:\n        try:\n            # Convert the file (raises exceptions on error)\n            result = converter.convert(md_file)\n\n            # Apply tag filter if specified\n            if filter_tags and not matches_tag_filter(result.tags, filter_tags):\n                results[\"skipped\"].append(result.filename)\n                continue\n\n            # Write each format\n            output_files = []\n            for format_name, output in result.outputs.items():\n                # Construct output path\n                # Agent Skills goes to project root ./skills/\n                # Other formats go to dist/ (or specified output_dir)\n                if format_name == \"agentskills\":\n                    base_dir = PROJECT_ROOT\n                else:\n                    base_dir = output_base\n\n                output_file = (\n                    base_dir / output.subpath / f\"{result.basename}{output.extension}\"\n                )\n\n                # Create directory if it doesn't exist and write file\n                output_file.parent.mkdir(parents=True, exist_ok=True)\n                output_file.write_text(output.content, encoding=\"utf-8\")\n                output_files.append(output_file.name)\n\n            print(f\"Success: {result.filename} → {', '.join(output_files)}\")\n            results[\"success\"].append(result.filename)\n\n            # Update language mappings for SKILL.md\n            for language in result.languages:\n                language_to_rules[language].append(result.filename)\n\n        except FileNotFoundError as e:\n            error_msg = f\"{md_file.name}: File not found - {e}\"\n            print(f\"Error: {error_msg}\")\n            results[\"errors\"].append(error_msg)\n\n        except ValueError as e:\n            error_msg = f\"{md_file.name}: Validation error - {e}\"\n            print(f\"Error: {error_msg}\")\n            results[\"errors\"].append(error_msg)\n\n        except Exception as e:\n            error_msg = f\"{md_file.name}: Unexpected error - {e}\"\n            print(f\"Error: {error_msg}\")\n            results[\"errors\"].append(error_msg)\n\n    # Summary\n    if filter_tags:\n        print(\n            f\"\\nResults: {len(results['success'])} success, {len(results['skipped'])} skipped (tag filter), {len(results['errors'])} errors\"\n        )\n    else:\n        print(\n            f\"\\nResults: {len(results['success'])} success, {len(results['errors'])} errors\"\n        )\n\n    # Generate SKILL.md with language mappings (only if Agent Skills is included)\n    if include_agentskills and language_to_rules:\n        template_path = (\n            PROJECT_ROOT / \"sources\" / \"core\" / \"codeguard-SKILLS.md.template\"\n        )\n\n        if not template_path.exists():\n            raise FileNotFoundError(\n                f\"SKILL.md template not found at {template_path}. \"\n                \"This file is required for Agent Skills generation.\"\n            )\n\n        output_skill_dir = PROJECT_ROOT / \"skills\" / \"software-security\"\n        output_skill_dir.mkdir(parents=True, exist_ok=True)\n        output_skill_path = output_skill_dir / \"SKILL.md\"\n\n        # Read template and inject current version from pyproject.toml\n        template_content = template_path.read_text(encoding=\"utf-8\")\n        # Replace the hardcoded version with actual version\n        template_content = re.sub(\n            r'codeguard-version:\\s*\"[^\"]*\"',\n            f'codeguard-version: \"{version}\"',\n            template_content,\n        )\n        output_skill_path.write_text(template_content, encoding=\"utf-8\")\n\n        update_skill_md(language_to_rules, str(output_skill_path))\n\n    return results\n\n\ndef _resolve_source_paths(args) -> list[Path]:\n    \"\"\"\n    Resolve source paths from CLI arguments.\n    Priority: --source flags > default (core)\n    \"\"\"\n    # If --source flags provided, resolve under sources/\n    if args.source:\n        return [Path(\"sources\") / src for src in args.source]\n\n    # Default: core rules only\n    return [Path(\"sources/core\")]\n\n\nif __name__ == \"__main__\":\n    import sys\n    from argparse import ArgumentParser\n\n    parser = ArgumentParser(\n        description=\"Convert unified rule markdown into IDE-specific bundles.\"\n    )\n    parser.add_argument(\n        \"--source\",\n        nargs=\"+\",\n        help=\"Named sources under ./sources to convert (e.g., --source core owasp). Default: core\",\n    )\n    parser.add_argument(\n        \"--output-dir\",\n        \"-o\",\n        default=\"dist\",\n        help=\"Output directory for generated bundles (default: dist).\",\n    )\n    parser.add_argument(\n        \"--tag\",\n        \"--tags\",\n        dest=\"tags\",\n        help=\"Filter rules by tags (comma-separated, case-insensitive, AND logic). Example: --tag api,web-security\",\n    )\n\n    cli_args = parser.parse_args()\n    source_paths = _resolve_source_paths(cli_args)\n\n    # Validate all source paths exist\n    missing = [p for p in source_paths if not p.exists()]\n    if missing:\n        print(f\"❌ Source path(s) not found: {', '.join(str(p) for p in missing)}\")\n        sys.exit(1)\n\n    # Check for duplicate filenames across sources if multiple sources\n    if len(source_paths) > 1:\n        filename_to_sources = defaultdict(list)\n        for source_path in source_paths:\n            for md_file in source_path.rglob(\"*.md\"):\n                filename_to_sources[md_file.name].append(source_path.name)\n\n        duplicates = {\n            name: srcs for name, srcs in filename_to_sources.items() if len(srcs) > 1\n        }\n        if duplicates:\n            print(f\"❌ Found {len(duplicates)} duplicate filename(s) across sources:\")\n            for filename, sources in duplicates.items():\n                print(f\"   - {filename} in: {', '.join(sources)}\")\n            print(\"\\nPlease rename files to have unique names across all sources.\")\n            sys.exit(1)\n\n    # Get version once and sync to metadata files\n    version = get_version_from_pyproject()\n    sync_plugin_metadata(version)\n\n    # Check if core is in the sources for Agent Skills generation\n    has_core = Path(\"sources/core\") in source_paths\n    if has_core:\n        # Validate template exists early\n        template_path = (\n            PROJECT_ROOT / \"sources\" / \"core\" / \"codeguard-SKILLS.md.template\"\n        )\n        if not template_path.exists():\n            print(f\"❌ SKILL.md template not found at {template_path}\")\n            print(\"This file is required for Agent Skills generation.\")\n            sys.exit(1)\n\n    # Clean output directories once before processing\n    output_path = Path(cli_args.output_dir)\n    if output_path.exists():\n        shutil.rmtree(output_path)\n        print(f\"✅ Cleaned {cli_args.output_dir}/ directory\")\n\n    if has_core:\n        skills_rules_dir = PROJECT_ROOT / \"skills\" / \"software-security\" / \"rules\"\n        if skills_rules_dir.exists():\n            shutil.rmtree(skills_rules_dir)\n            print(f\"✅ Cleaned skills/ directory\")\n\n    # Print processing summary\n    if len(source_paths) > 1:\n        sources_list = \", \".join(p.name for p in source_paths)\n        print(f\"\\nConverting {len(source_paths)} sources: {sources_list}\")\n        if has_core:\n            print(\"(Agent Skills will include only core rules)\")\n        print()\n\n    # Convert all sources\n    aggregated = {\"success\": [], \"errors\": [], \"skipped\": []}\n    # Parse comma-separated tags and normalize to lowercase\n    filter_tags = None\n    if cli_args.tags:\n        filter_tags = [\n            tag.strip().lower() for tag in cli_args.tags.split(\",\") if tag.strip()\n        ]\n\n    # Print tag filter info if active\n    if filter_tags:\n        print(\n            f\"Tag filter active: {', '.join(filter_tags)} (AND logic - rules must have all tags)\\n\"\n        )\n\n    for source_path in source_paths:\n        is_core = source_path == Path(\"sources/core\")\n\n        print(f\"Processing: {source_path}\")\n        results = convert_rules(\n            str(source_path),\n            cli_args.output_dir,\n            include_agentskills=is_core,\n            version=version,\n            filter_tags=filter_tags,\n        )\n\n        aggregated[\"success\"].extend(results[\"success\"])\n        aggregated[\"errors\"].extend(results[\"errors\"])\n        if \"skipped\" in results:\n            aggregated[\"skipped\"].extend(results[\"skipped\"])\n        print(\"\")\n\n    if aggregated[\"errors\"]:\n        print(\"❌ Some conversions failed\")\n        sys.exit(1)\n\n    print(\"✅ All conversions successful\")\n"
  },
  {
    "path": "src/converter.py",
    "content": "# Copyright 2025 Cisco Systems, Inc. and its affiliates\n#\n# SPDX-License-Identifier: Apache-2.0\n\n\"\"\"\nRule Converter\n\nConverts unified markdown rules to multiple IDE formats.\nHandles parsing, validation, and format generation.\n\"\"\"\nfrom dataclasses import dataclass\nfrom pathlib import Path\n\nfrom language_mappings import languages_to_globs\nfrom utils import parse_frontmatter_and_content, validate_tags\nfrom formats import (\n    BaseFormat,\n    ProcessedRule,\n)\n\n\n@dataclass\nclass FormatOutput:\n    \"\"\"\n    Represents the output for a single format.\n\n    Attributes:\n        content: The fully formatted content with frontmatter\n        extension: File extension including dot (e.g., '.mdc')\n        subpath: Subdirectory path (e.g., '.cursor/rules', 'skills/software-security/rules')\n    \"\"\"\n\n    content: str\n    extension: str\n    subpath: str\n\n\n@dataclass\nclass ConversionResult:\n    \"\"\"\n    Represents the complete result of converting a rule file.\n\n    Attributes:\n        filename: Original filename (e.g., 'my-rule.md')\n        basename: Filename without extension (e.g., 'my-rule')\n        outputs: Dictionary mapping format names to their outputs\n        languages: List of programming languages the rule applies to, empty list if always applies\n        tags: List of tags for categorizing and filtering rules\n    Example:\n        result = ConversionResult(\n            filename=\"my-rule.md\",\n            basename=\"my-rule\",\n            outputs={\n                \"cursor\": FormatOutput(\n                    content=\"---\\\\n...\\\\n---\\\\n\\\\nContent\",\n                    extension=\".mdc\",\n                    subpath=\".cursor/rules\"\n                )\n            },\n            languages=[\"python\", \"javascript\"],\n            tags=[\"authentication\", \"web-security\"]\n        )\n    \"\"\"\n\n    filename: str\n    basename: str\n    outputs: dict[str, FormatOutput]\n    languages: list[str]\n    tags: list[str]\n\n\nclass RuleConverter:\n    \"\"\"\n    Converts markdown rules to multiple IDE formats.\n\n    Uses the BaseFormat abstraction to support multiple IDE formats in an extensible way.\n    New formats can be added by creating a new BaseFormat subclass and passing it to the converter.\n\n    Main Methods:\n        - parse_rule(): Parse markdown file with YAML frontmatter\n        - generate_globs(): Convert languages to glob patterns\n        - convert(): Convert a rule file to all registered formats (returns ConversionResult)\n\n    Example:\n        # Create converter\n        from converter import RuleConverter, ConversionResult, FormatOutput\n        from formats import CursorFormat, WindsurfFormat\n        from utils import get_version_from_pyproject\n\n        version = get_version_from_pyproject()\n        converter = RuleConverter(formats=[\n            CursorFormat(version),\n            WindsurfFormat(version)\n        ])\n\n        # Convert a file\n        try:\n            result = converter.convert(\"rule.md\")\n            # result is ConversionResult dataclass\n\n            for format_name, output in result.outputs.items():\n                # output is FormatOutput dataclass\n                print(f\"{format_name}: {output.extension}\")\n                save_file(output.content, output.subpath)\n        except ValueError as e:\n            print(f\"Invalid rule: {e}\")\n    \"\"\"\n\n    def __init__(self, formats: list[BaseFormat]):\n        \"\"\"\n        Initialize the converter with version info and supported formats.\n\n        Args:\n            formats: List of BaseFormat instances to use for conversion.\n        \"\"\"\n        self.formats = formats\n\n    def parse_rule(self, content: str, filename: str) -> ProcessedRule:\n        \"\"\"\n        Parse a markdown file with required frontmatter.\n\n        Args:\n            content: Full file content with YAML frontmatter\n            filename: Name of the file being parsed\n\n        Returns:\n            ProcessedRule with validated frontmatter and content\n\n        Raises:\n            ValueError: If frontmatter is missing or invalid\n        \"\"\"\n        # Parse frontmatter and content using shared utility\n        frontmatter, markdown_content = parse_frontmatter_and_content(content)\n\n        if not frontmatter:\n            raise ValueError(f\"Missing or invalid frontmatter in {filename}\")\n\n        # Validate required description field\n        if \"description\" not in frontmatter or not frontmatter[\"description\"].strip():\n            raise ValueError(f\"Missing required 'description' field in {filename}\")\n\n        always_apply = frontmatter.get(\"alwaysApply\", False)\n\n        # Validate languages field based on alwaysApply setting\n        if always_apply:\n            # If alwaysApply is true, languages should not be specified or should be empty\n            if \"languages\" in frontmatter and frontmatter[\"languages\"]:\n                raise ValueError(\n                    f\"When 'alwaysApply' is true, 'languages' should not be specified or should be empty in {filename}\"\n                )\n            languages = []  # No languages when always applying\n        else:\n            # If alwaysApply is false, languages is required\n            if \"languages\" not in frontmatter:\n                raise ValueError(\n                    f\"Missing required 'languages' field in {filename} (required when alwaysApply is false)\"\n                )\n\n            languages = frontmatter[\"languages\"]\n            if not isinstance(languages, list) or not languages:\n                raise ValueError(\n                    f\"'languages' must be a non-empty list in {filename} when alwaysApply is false\"\n                )\n\n        # Parse and validate tags (optional field)\n        tags = []\n        if \"tags\" in frontmatter:\n            tags = validate_tags(frontmatter[\"tags\"], filename)\n\n        # Adding rule_id to the beginning of the content\n        rule_id = Path(filename).stem\n        markdown_content = f\"rule_id: {rule_id}\\n\\n{markdown_content}\"\n\n        return ProcessedRule(\n            description=frontmatter[\"description\"],\n            languages=[lang.lower() for lang in languages],\n            always_apply=always_apply,\n            content=markdown_content,\n            filename=filename,\n            tags=tags,\n        )\n\n    def generate_globs(self, languages: list[str]) -> str:\n        \"\"\"\n        Generate comma-separated glob patterns for languages.\n\n        Args:\n            languages: List of programming languages\n\n        Returns:\n            Comma-separated glob patterns, or \"**/*\" for all files\n        \"\"\"\n        # Use shared function from language_mappings\n        globs = languages_to_globs(languages)\n        return globs if globs else \"**/*\"\n\n    def convert(self, filepath: str) -> ConversionResult:\n        \"\"\"\n        Convert a rule file to all registered formats.\n\n        This method handles the entire conversion pipeline:\n        - Reading the file\n        - Parsing and validating\n        - Generating all format outputs\n\n        Args:\n            filepath: Path to the rule file to convert (str or Path)\n\n        Returns:\n            ConversionResult with filename, basename, and format outputs\n\n        Raises:\n            FileNotFoundError: If the rule file doesn't exist\n            ValueError: If the rule has invalid frontmatter or structure\n            Exception: For other unexpected errors during conversion\n\n        Example:\n            try:\n                result = converter.convert(\"rules/my-rule.md\")\n                for format_name, output in result.outputs.items():\n                    path = f\"{output.subpath}/{result.basename}{output.extension}\"\n                    write_file(path, output.content)\n            except (FileNotFoundError, ValueError) as e:\n                print(f\"Error: {e}\")\n        \"\"\"\n        filepath = Path(filepath)\n        filename = filepath.name\n        basename = filepath.stem\n\n        # Read the rule file (may raise FileNotFoundError)\n        content = filepath.read_text(encoding=\"utf-8\")\n\n        # Parse and validate (may raise ValueError)\n        rule = self.parse_rule(content, filename)\n\n        # Generate globs once for all formats\n        globs = self.generate_globs(rule.languages)\n\n        # Generate output for each format\n        outputs = {}\n        for format_handler in self.formats:\n            format_name = format_handler.get_format_name()\n            outputs[format_name] = FormatOutput(\n                content=format_handler.generate(rule, globs),\n                extension=format_handler.get_file_extension(),\n                subpath=format_handler.get_output_subpath(),\n            )\n\n        return ConversionResult(\n            filename=filename,\n            basename=basename,\n            outputs=outputs,\n            languages=rule.languages,\n            tags=rule.tags,\n        )\n"
  },
  {
    "path": "src/formats/__init__.py",
    "content": "# Copyright 2025 Cisco Systems, Inc. and its affiliates\n#\n# SPDX-License-Identifier: Apache-2.0\n\n\"\"\"\nFormats Package\n\nThis package contains all IDE format implementations for rule conversion.\n\nAvailable Formats:\n- CursorFormat: Generates .mdc files for Cursor IDE\n- WindsurfFormat: Generates .md files for Windsurf IDE\n- CopilotFormat: Generates .instructions.md files for GitHub Copilot\n- AgentSkillsFormat: Generates .md files for Agent Skills (OpenAI Codex, Claude Code, other AI coding tools)\n- AntigravityFormat: Generates .md files for Google Antigravity\n\nUsage:\n    from formats import BaseFormat, ProcessedRule, CursorFormat, WindsurfFormat, CopilotFormat, AgentSkillsFormat, AntigravityFormat\n\n    version = \"1.0.0\"\n    formats = [\n        CursorFormat(version),\n        WindsurfFormat(version),\n        CopilotFormat(version),\n        AgentSkillsFormat(version),\n        AntigravityFormat(version),\n    ]\n\"\"\"\n\nfrom formats.base import BaseFormat, ProcessedRule\nfrom formats.cursor import CursorFormat\nfrom formats.windsurf import WindsurfFormat\nfrom formats.copilot import CopilotFormat\nfrom formats.agentskills import AgentSkillsFormat\nfrom formats.antigravity import AntigravityFormat\n\n__all__ = [\n    \"BaseFormat\",\n    \"ProcessedRule\",\n    \"CursorFormat\",\n    \"WindsurfFormat\",\n    \"CopilotFormat\",\n    \"AgentSkillsFormat\",\n    \"AntigravityFormat\",\n]\n"
  },
  {
    "path": "src/formats/agentskills.py",
    "content": "# Copyright 2025 Cisco Systems, Inc. and its affiliates\n#\n# SPDX-License-Identifier: Apache-2.0\n\n\"\"\"\nAgent Skills Format Implementation\n\nGenerates .md files for the Agent Skills standard (agentskills.io).\nThis format is used by OpenAI Codex, Claude Code, and other AI coding tools.\n\"\"\"\n\nfrom formats.base import BaseFormat, ProcessedRule\n\n\nclass AgentSkillsFormat(BaseFormat):\n    \"\"\"\n    Agent Skills format implementation (.md files).\n\n    Agent Skills (https://agentskills.io/) is an open standard for extending\n    AI coding agents with task-specific capabilities. It uses standard markdown\n    files with YAML frontmatter to define rules and instructions.\n\n    This format is adopted by:\n    - OpenAI Codex (skills)\n    - Claude Code (plugins)\n    - Other AI coding tools\n\n    The original rule content is preserved and placed in the\n    skills/software-security/rules/ directory for distribution.\n    \"\"\"\n\n    def get_format_name(self) -> str:\n        \"\"\"Return Agent Skills format identifier.\"\"\"\n        return \"agentskills\"\n\n    def get_file_extension(self) -> str:\n        \"\"\"Return Agent Skills format file extension.\"\"\"\n        return \".md\"\n\n    def get_output_subpath(self) -> str:\n        \"\"\"Return Agent Skills output subdirectory.\"\"\"\n        return \"skills/software-security/rules\"\n\n    def generate(self, rule: ProcessedRule, globs: str) -> str:\n        \"\"\"\n        Generate Agent Skills .md format.\n\n        Agent Skills should preserve the original YAML frontmatter\n        (description, languages, alwaysApply) so the rules remain complete\n        and can be referenced properly by AI coding agents.\n\n        Args:\n            rule: The processed rule to format\n            globs: Glob patterns (not used for Agent Skills format)\n\n        Returns:\n            Complete markdown with original YAML frontmatter preserved\n        \"\"\"\n        # Build YAML frontmatter\n        yaml_lines = []\n\n        # Add description\n        desc = self._format_yaml_field(\"description\", rule.description)\n        if desc:\n            yaml_lines.append(desc)\n\n        # Add languages if present\n        if rule.languages:\n            # Format as YAML list\n            yaml_lines.append(\"languages:\")\n            for lang in rule.languages:\n                yaml_lines.append(f\"- {lang}\")\n\n        # Add alwaysApply\n        yaml_lines.append(f\"alwaysApply: {str(rule.always_apply).lower()}\")\n\n        return self._build_yaml_frontmatter(yaml_lines, rule.content)\n"
  },
  {
    "path": "src/formats/antigravity.py",
    "content": "# Copyright 2025 Cisco Systems, Inc. and its affiliates\n#\n# SPDX-License-Identifier: Apache-2.0\n\n\"\"\"\nAntigravity Format Implementation\n\nGenerates .md rule files for Antigravity with YAML frontmatter.\n\"\"\"\n\nfrom formats.base import BaseFormat, ProcessedRule\n\n\nclass AntigravityFormat(BaseFormat):\n    \"\"\"\n    Antigravity format implementation (.md rule files).\n\n    Antigravity uses .md files with YAML frontmatter containing:\n    - trigger: 'always_on' or 'glob' (activation type)\n    - globs: (if trigger is 'glob') File matching patterns\n    - description: Rule description\n    - version: Rule version\n    \n    Rules use activation types (Always On or Glob) to determine when\n    they apply, similar to Windsurf's implementation.\n    See: https://antigravity.google/docs/rules-workflows\n    \"\"\"\n\n    def get_format_name(self) -> str:\n        \"\"\"Return Antigravity format identifier.\"\"\"\n        return \"antigravity\"\n\n    def get_file_extension(self) -> str:\n        \"\"\"Return Antigravity format file extension.\"\"\"\n        return \".md\"\n\n    def get_output_subpath(self) -> str:\n        \"\"\"Return Antigravity output subdirectory.\"\"\"\n        return \".agent/rules\"\n\n    def generate(self, rule: ProcessedRule, globs: str) -> str:\n        \"\"\"\n        Generate Antigravity .md format with YAML frontmatter.\n\n        Args:\n            rule: The processed rule to format\n            globs: Glob patterns for file matching\n\n        Returns:\n            Formatted .md content with trigger, globs, description, and version\n        \n        Note:\n            Antigravity rules use activation types:\n            - 'always_on': Rule applies to all files (when alwaysApply is true)\n            - 'glob': Rule applies to files matching glob patterns (language-specific)\n        \"\"\"\n        yaml_lines = []\n\n        # Use trigger: always_on for rules that should always apply\n        if rule.always_apply:\n            yaml_lines.append(\"trigger: always_on\")\n        else:\n            yaml_lines.append(\"trigger: glob\")\n            yaml_lines.append(f\"globs: {globs}\")\n\n        # Add description (required by Antigravity spec)\n        desc = self._format_yaml_field(\"description\", rule.description)\n        if desc:\n            yaml_lines.append(desc)\n\n        # Add version\n        yaml_lines.append(f\"version: {self.version}\")\n\n        return self._build_yaml_frontmatter(yaml_lines, rule.content)\n"
  },
  {
    "path": "src/formats/base.py",
    "content": "# Copyright 2025 Cisco Systems, Inc. and its affiliates\n#\n# SPDX-License-Identifier: Apache-2.0\n\n\"\"\"\nBase Format Class\n\nAbstract base class for all IDE rule formats.\nDefines the interface that all format implementations must follow.\n\"\"\"\n\nimport yaml\nfrom abc import ABC, abstractmethod\nfrom dataclasses import dataclass\n\n\n@dataclass\nclass ProcessedRule:\n    \"\"\"\n    Represents a processed rule with required frontmatter.\n\n    Attributes:\n        description: Human-readable description of the rule\n        languages: List of programming languages this rule applies to\n        always_apply: Whether this rule should apply to all files\n        content: The actual rule content in markdown format\n        filename: Original filename of the rule\n        tags: List of tags for categorizing and filtering rules\n    \"\"\"\n\n    description: str\n    languages: list[str]\n    always_apply: bool\n    content: str\n    filename: str\n    tags: list[str]\n\n\nclass BaseFormat(ABC):\n    \"\"\"\n    Abstract base class for IDE rule formats.\n\n    This class defines the interface that all format implementations must follow.\n    Each format is responsible for:\n    - Providing its file extension (e.g., '.mdc', '.md')\n    - Providing its output subdirectory path (e.g., '.cursor/rules')\n    - Generating formatted content with proper frontmatter\n    \"\"\"\n\n    def __init__(self, version: str):\n        \"\"\"\n        Initialize format with version information.\n\n        Args:\n            version: Version string to include in generated files\n        \"\"\"\n        self.version = version\n\n    @abstractmethod\n    def get_format_name(self) -> str:\n        \"\"\"\n        Return the unique identifier for this format.\n\n        Returns:\n            Format name (e.g., 'cursor', 'windsurf', 'copilot')\n        \"\"\"\n        pass\n\n    @abstractmethod\n    def get_file_extension(self) -> str:\n        \"\"\"\n        Return the file extension for this format.\n\n        Returns:\n            File extension including the dot (e.g., '.mdc', '.md')\n        \"\"\"\n        pass\n\n    @abstractmethod\n    def get_output_subpath(self) -> str:\n        \"\"\"\n        Return the subdirectory path for this format.\n\n        Returns:\n            Subdirectory path (e.g., '.cursor/rules', 'skills/software-security/rules')\n        \"\"\"\n        pass\n\n    @abstractmethod\n    def generate(self, rule: ProcessedRule, globs: str) -> str:\n        \"\"\"\n        Generate the formatted content for this IDE format.\n\n        Args:\n            rule: The processed rule to format\n            globs: Glob patterns for file matching\n\n        Returns:\n            Fully formatted content with frontmatter and rule content\n        \"\"\"\n        pass\n\n    def _build_yaml_frontmatter(self, lines: list[str], content: str) -> str:\n        \"\"\"\n        Helper to build complete file with YAML frontmatter.\n\n        Args:\n            lines: List of YAML lines to include in frontmatter\n            content: The markdown content to append after frontmatter\n\n        Returns:\n            Complete formatted string with frontmatter and content\n        \"\"\"\n        yaml_str = \"\\n\".join(lines)\n        return f\"---\\n{yaml_str}\\n---\\n\\n{content}\\n\"\n\n    def _format_yaml_field(self, field_name: str, value: str) -> str:\n        \"\"\"\n        Format a field with proper YAML escaping for special characters.\n\n        Args:\n            field_name: Name of the YAML field\n            value: Value to format\n\n        Returns:\n            Properly formatted YAML string, or empty string if value is empty\n        \"\"\"\n        if value and value.strip():\n            yaml_dump = yaml.safe_dump(\n                {field_name: value},\n                default_flow_style=False,\n                allow_unicode=True,\n                width=float(\"inf\")\n            )\n            return yaml_dump.strip()\n        return \"\"\n"
  },
  {
    "path": "src/formats/copilot.py",
    "content": "# Copyright 2025 Cisco Systems, Inc. and its affiliates\n#\n# SPDX-License-Identifier: Apache-2.0\n\n\"\"\"\nCopilot Format Implementation\n\nGenerates .instructions.md files for GitHub Copilot with YAML frontmatter.\n\"\"\"\n\nfrom formats.base import BaseFormat, ProcessedRule\n\n\nclass CopilotFormat(BaseFormat):\n    \"\"\"\n    GitHub Copilot format implementation (.instructions.md files).\n\n    Copilot uses .instructions.md files with YAML frontmatter containing:\n    - applyTo: File matching patterns\n    - title: Rule title/description\n    - version: Rule version\n    \"\"\"\n\n    def get_format_name(self) -> str:\n        \"\"\"Return Copilot format identifier.\"\"\"\n        return \"copilot\"\n\n    def get_file_extension(self) -> str:\n        \"\"\"Return Copilot format file extension.\"\"\"\n        return \".instructions.md\"\n\n    def get_output_subpath(self) -> str:\n        \"\"\"Return Copilot output subdirectory.\"\"\"\n        return \".github/instructions\"\n\n    def generate(self, rule: ProcessedRule, globs: str) -> str:\n        \"\"\"\n        Generate Copilot .instructions.md format with YAML frontmatter.\n\n        Args:\n            rule: The processed rule to format\n            globs: Glob patterns for file matching\n\n        Returns:\n            Formatted .instructions.md content\n        \"\"\"\n        yaml_lines = []\n\n        # Add applyTo (Copilot's equivalent of globs)\n        yaml_lines.append(f\"applyTo: '{globs}'\")\n\n        # Add description\n        description = self._format_yaml_field(\"description\", rule.description)\n        if description:\n            yaml_lines.append(description)\n\n        # Add version\n        yaml_lines.append(f\"version: {self.version}\")\n\n        return self._build_yaml_frontmatter(yaml_lines, rule.content)\n"
  },
  {
    "path": "src/formats/cursor.py",
    "content": "# Copyright 2025 Cisco Systems, Inc. and its affiliates\n#\n# SPDX-License-Identifier: Apache-2.0\n\n\"\"\"\nCursor Format Implementation\n\nGenerates .mdc files for Cursor IDE with YAML frontmatter.\n\"\"\"\n\nfrom formats.base import BaseFormat, ProcessedRule\n\n\nclass CursorFormat(BaseFormat):\n    \"\"\"\n    Cursor IDE format implementation (.mdc files).\n\n    Cursor uses .mdc files with YAML frontmatter containing:\n    - description: Rule description\n    - globs: File matching patterns\n    - version: Rule version\n    - alwaysApply: (optional) Whether to apply to all files\n    \"\"\"\n\n    def get_format_name(self) -> str:\n        \"\"\"Return Cursor format identifier.\"\"\"\n        return \"cursor\"\n\n    def get_file_extension(self) -> str:\n        \"\"\"Return Cursor format file extension.\"\"\"\n        return \".mdc\"\n\n    def get_output_subpath(self) -> str:\n        \"\"\"Return Cursor output subdirectory.\"\"\"\n        return \".cursor/rules\"\n\n    def generate(self, rule: ProcessedRule, globs: str) -> str:\n        \"\"\"\n        Generate Cursor .mdc format with YAML frontmatter.\n\n        Args:\n            rule: The processed rule to format\n            globs: Glob patterns for file matching\n\n        Returns:\n            Formatted .mdc content\n        \"\"\"\n        yaml_lines = []\n\n        # Add description if present\n        desc = self._format_yaml_field(\"description\", rule.description)\n        if desc:\n            yaml_lines.append(desc)\n\n        # Add globs and version\n        yaml_lines.append(f\"globs: {globs}\")\n        yaml_lines.append(f\"version: {self.version}\")\n\n        # Add alwaysApply if needed\n        if rule.always_apply:\n            yaml_lines.append(\"alwaysApply: true\")\n\n        return self._build_yaml_frontmatter(yaml_lines, rule.content)\n"
  },
  {
    "path": "src/formats/windsurf.py",
    "content": "# Copyright 2025 Cisco Systems, Inc. and its affiliates\n#\n# SPDX-License-Identifier: Apache-2.0\n\n\"\"\"\nWindsurf Format Implementation\n\nGenerates .md files for Windsurf IDE with YAML frontmatter.\n\"\"\"\n\nfrom formats.base import BaseFormat, ProcessedRule\n\n\nclass WindsurfFormat(BaseFormat):\n    \"\"\"\n    Windsurf IDE format implementation (.md files).\n\n    Windsurf uses .md files with YAML frontmatter containing:\n    - trigger: 'always_on' or 'glob'\n    - globs: (if trigger is 'glob') File matching patterns\n    - title: Rule title/description\n    - version: Rule version\n    \"\"\"\n\n    def get_format_name(self) -> str:\n        \"\"\"Return Windsurf format identifier.\"\"\"\n        return \"windsurf\"\n\n    def get_file_extension(self) -> str:\n        \"\"\"Return Windsurf format file extension.\"\"\"\n        return \".md\"\n\n    def get_output_subpath(self) -> str:\n        \"\"\"Return Windsurf output subdirectory.\"\"\"\n        return \".windsurf/rules\"\n\n    def generate(self, rule: ProcessedRule, globs: str) -> str:\n        \"\"\"\n        Generate Windsurf .md format with YAML frontmatter.\n\n        Args:\n            rule: The processed rule to format\n            globs: Glob patterns for file matching\n\n        Returns:\n            Formatted .md content\n        \"\"\"\n        yaml_lines = []\n\n        # Use trigger: always_on for rules that should always apply\n        if rule.always_apply:\n            yaml_lines.append(\"trigger: always_on\")\n        else:\n            yaml_lines.append(\"trigger: glob\")\n            yaml_lines.append(f\"globs: {globs}\")\n\n        # Add title (Windsurf uses 'title' instead of 'description')\n        title = self._format_yaml_field(\"title\", rule.description)\n        if title:\n            yaml_lines.append(title)\n\n        # Add version\n        yaml_lines.append(f\"version: {self.version}\")\n\n        return self._build_yaml_frontmatter(yaml_lines, rule.content)\n"
  },
  {
    "path": "src/language_mappings.py",
    "content": "# Copyright 2025 Cisco Systems, Inc. and its affiliates\n#\n# SPDX-License-Identifier: Apache-2.0\n\n\"\"\"\nShared language mappings for all rule tools.\nSingle source of truth for language-to-extension mappings.\n\"\"\"\n\n# Master mapping of languages to file extensions\nLANGUAGE_TO_EXTENSIONS = {\n    \"apex\": [\".cls\", \".trigger\"],\n    \"python\": [\".py\", \".pyx\", \".pyi\"],\n    \"javascript\": [\".js\", \".jsx\", \".mjs\"],\n    \"typescript\": [\".ts\", \".tsx\"],\n    \"java\": [\".java\"],\n    \"c\": [\".c\", \".h\"],\n    \"cpp\": [\".cpp\", \".hpp\", \".cc\", \".cxx\"],\n    \"c++\": [\".cpp\", \".hpp\", \".cc\", \".cxx\"],  # Alias\n    \"go\": [\".go\"],\n    \"rust\": [\".rs\"],\n    \"ruby\": [\".rb\"],\n    \"php\": [\".php\"],\n    \"swift\": [\".swift\"],\n    \"kotlin\": [\".kt\", \".kts\"],\n    \"scala\": [\".scala\"],\n    \"r\": [\".r\", \".R\"],\n    \"matlab\": [\".m\"],\n    \"julia\": [\".jl\"],\n    \"dart\": [\".dart\"],\n    \"lua\": [\".lua\"],\n    \"perl\": [\".pl\", \".pm\"],\n    \"shell\": [\".sh\", \".bash\"],\n    \"powershell\": [\".ps1\"],\n    \"fsharp\": [\".fs\", \".fsx\"],\n    \"clojure\": [\".clj\", \".cljs\"],\n    \"elixir\": [\".ex\", \".exs\"],\n    \"erlang\": [\".erl\", \".hrl\"],\n    \"ocaml\": [\".ml\", \".mli\"],\n    \"nim\": [\".nim\"],\n    \"vlang\": [\".v\"],\n    \"zig\": [\".zig\"],\n    \"d\": [\".d\"],\n    \"solidity\": [\".sol\"],\n    \"vyper\": [\".vy\"],\n    \"cairo\": [\".cairo\"],\n    \"sway\": [\".sway\"],\n    \"handlebars\": [\".hbs\"],\n    \"liquid\": [\".liquid\"],\n    \"markdown\": [\".md\"],\n    \"mdx\": [\".mdx\"],\n    \"latex\": [\".tex\"],\n    \"yaml\": [\".yml\", \".yaml\"],\n    \"docker\": [\"Dockerfile*\", \"docker-compose*\", \".dockerfile\"],\n    \"xml\": [\".xml\", \".xsd\", \".xslt\", \".wsdl\"],\n    \"vue\": [\".vue\"],\n    \"svelte\": [\".svelte\"],\n    \"astro\": [\".astro\"],\n    \"elm\": [\".elm\"],\n    \"purescript\": [\".purs\"],\n    \"idris\": [\".idr\"],\n    \"agda\": [\".agda\"],\n    \"lean\": [\".lean\"],\n    \"coq\": [\".coq\"],\n    \"verilog\": [\".v\"],\n    \"vhdl\": [\".vhd\", \".vhdl\"],\n    \"cuda\": [\".cu\", \".cuh\"],\n    \"opencl\": [\".cl\"],\n    \"glsl\": [\".glsl\", \".vert\", \".frag\"],\n    \"hlsl\": [\".hlsl\"],\n    \"wgsl\": [\".wgsl\"],\n    \"html\": [\".html\", \".htm\"],\n    \"sql\": [\".sql\", \".ddl\", \".dml\"],\n}\n\n# Reverse mapping: extension to language (for conversion from globs)\nEXTENSION_TO_LANGUAGE = {}\nfor lang, exts in LANGUAGE_TO_EXTENSIONS.items():\n    for ext in exts:\n        # First language wins for duplicate extensions\n        if ext not in EXTENSION_TO_LANGUAGE:\n            EXTENSION_TO_LANGUAGE[ext] = lang\n\n\ndef languages_to_globs(languages: list[str]) -> str:\n    \"\"\"\n    Convert list of languages to glob patterns.\n    \n    Args:\n        languages: List of programming language names (e.g., ['python', 'javascript'])\n    \n    Returns:\n        Comma-separated glob patterns (e.g., '**/*.py,**/*.js')\n        Empty string if no languages provided\n    \"\"\"\n    if not languages:\n        return \"\"\n\n    extensions = []\n    for lang in languages:\n        if lang in LANGUAGE_TO_EXTENSIONS:\n            for ext in LANGUAGE_TO_EXTENSIONS[lang]:\n                if \"*\" in ext:\n                    extensions.append(ext)\n                else:\n                    extensions.append(f\"**/*{ext}\")\n\n    return \",\".join(sorted(set(extensions)))\n\n\ndef globs_to_languages(globs: str) -> list[str]:\n    \"\"\"\n    Convert glob patterns to list of languages.\n    \n    Args:\n        globs: Comma-separated glob patterns (e.g., '**/*.py,**/*.js')\n    \n    Returns:\n        Sorted list of language names that match the glob patterns\n        Empty list if no patterns or universal glob provided\n    \"\"\"\n    if not globs or globs in [\"**\", \"*\", \"**/*\"]:\n        return []\n\n    languages = set()\n    patterns = globs.split(\",\")\n\n    for pattern in patterns:\n        pattern = pattern.strip().lower()\n\n        # Check for file extensions and patterns\n        for ext, lang in EXTENSION_TO_LANGUAGE.items():\n            if ext.lower() in pattern:\n                languages.add(lang)\n                break  # One match per pattern is enough\n\n    return sorted(languages)\n"
  },
  {
    "path": "src/tag_mappings.py",
    "content": "# Copyright 2025 Cisco Systems, Inc. and its affiliates\n#\n# SPDX-License-Identifier: Apache-2.0\n\n\"\"\"\nTag Mappings\n\nCentralized list of known tags for categorizing security rules.\n\"\"\"\n\n# Known tags used in rules\n# Add new tags here as they are introduced in rules\nKNOWN_TAGS = {\n    \"authentication\",\n    \"data-security\",\n    \"infrastructure\",\n    \"privacy\",\n    \"secrets\",\n    \"web\",\n}\n\n"
  },
  {
    "path": "src/utils.py",
    "content": "# Copyright 2025 Cisco Systems, Inc. and its affiliates\n#\n# SPDX-License-Identifier: Apache-2.0\n\n\"\"\"\nUtilities for rule processing.\n\nCommon utilities used across the rule conversion tools.\n\"\"\"\n\nimport re\nimport tomllib\nfrom pathlib import Path\nimport yaml\n\n\ndef parse_frontmatter_and_content(content: str) -> tuple[dict | None, str]:\n    \"\"\"\n    Parse YAML frontmatter and content from markdown.\n    \n    Frontmatter must be in the format:\n        ---\n        yaml content\n        ---\n        markdown content\n    \n    The closing --- must be on its own line (not part of a comment or text).\n\n    Args:\n        content: Full file content\n\n    Returns:\n        Tuple of (frontmatter dict, markdown content)\n        Returns (None, content) if no valid frontmatter found\n    \"\"\"\n    if not content.startswith(\"---\\n\"):\n        return None, content\n\n    # Look for closing --- on its own line\n    # Use regex to ensure --- is at start of line (after newline)\n    closing_pattern = re.compile(r'\\n---\\n')\n    match = closing_pattern.search(content)\n    \n    if not match:\n        # No proper closing ---, treat as no frontmatter\n        return None, content\n    \n    # Extract frontmatter between opening and closing ---\n    frontmatter_text = content[4:match.start()]  # Skip opening \"---\\n\"\n    markdown_content = content[match.end():]  # Skip closing \"---\\n\"\n\n    try:\n        frontmatter = yaml.safe_load(frontmatter_text)\n    except yaml.YAMLError:\n        return None, content\n\n    return frontmatter, markdown_content.strip()\n\n\ndef validate_tags(tags, filename=None) -> list[str]:\n    \"\"\"\n    Validate tags list and return normalized (lowercase) tags.\n    \n    Args:\n        tags: The tags value to validate (should be a non-empty list)\n        filename: Optional filename for better error messages\n    \n    Returns:\n        List of normalized (lowercase) tags with duplicates removed.\n        Original order is preserved.\n    \n    Raises:\n        ValueError: If tags are invalid (wrong type, empty list, contain whitespace, etc.)\n    \n    Note:\n        - An empty tags list (tags: []) is considered invalid. If you have no tags,\n          omit the 'tags' field entirely from the frontmatter.\n        - Duplicate tags (after normalization) are automatically removed while\n          preserving the order of first occurrence.\n    \"\"\"\n    context = f\" in {filename}\" if filename else \"\"\n    \n    if not isinstance(tags, list):\n        raise ValueError(f\"'tags' must be a list{context}\")\n    \n    if not tags:\n        raise ValueError(f\"'tags' list cannot be empty{context}. Omit the field if you have no tags.\")\n    \n    normalized = []\n    for tag in tags:\n        if not isinstance(tag, str):\n            raise ValueError(f\"All tags must be strings{context}, found: {type(tag).__name__}\")\n        \n        if any(c.isspace() for c in tag):\n            raise ValueError(f\"Tags cannot contain whitespace: '{tag}'{context}\")\n        \n        if not tag:\n            raise ValueError(f\"Empty tag found{context}\")\n        \n        normalized.append(tag.lower())\n    \n    return list(dict.fromkeys(normalized))\n\n\ndef get_version_from_pyproject() -> str:\n    \"\"\"\n    Read version from pyproject.toml using Python's built-in TOML parser.\n\n    Requires Python 3.11+ for tomllib support.\n\n    Returns:\n        Version string from pyproject.toml\n\n    Raises:\n        FileNotFoundError: If pyproject.toml is not found\n        ValueError: If version field is missing or invalid\n    \"\"\"\n    pyproject_path = Path(\"pyproject.toml\")\n\n    if not pyproject_path.exists():\n        raise FileNotFoundError(\"pyproject.toml not found\")\n\n    try:\n        with open(pyproject_path, \"rb\") as f:\n            data = tomllib.load(f)\n\n        if \"project\" in data and \"version\" in data[\"project\"]:\n            version = data[\"project\"][\"version\"]\n            if isinstance(version, str) and version.strip():\n                return version.strip()\n        raise ValueError(\"Version field not found in pyproject.toml [project] section\")\n    except tomllib.TOMLDecodeError as e:\n        raise ValueError(f\"Invalid TOML syntax in pyproject.toml: {str(e)}\")\n    except (FileNotFoundError, ValueError):\n        raise\n    except Exception as e:\n        raise ValueError(f\"Unexpected error reading pyproject.toml: {str(e)}\")\n"
  },
  {
    "path": "src/validate_unified_rules.py",
    "content": "# Copyright 2025 Cisco Systems, Inc. and its affiliates\n#\n# SPDX-License-Identifier: Apache-2.0\n\n\"\"\"\nValidate Unified Rules Format\n\nValidates that unified rules have correct YAML frontmatter and structure.\n\"\"\"\n\nimport sys\nfrom pathlib import Path\n\nfrom language_mappings import LANGUAGE_TO_EXTENSIONS\nfrom tag_mappings import KNOWN_TAGS\nfrom utils import parse_frontmatter_and_content, validate_tags\n\n\ndef validate_rule(file_path: Path) -> dict[str, list[str]]:\n    \"\"\"Validate a single unified rule file.\"\"\"\n    errors = []\n    warnings = []\n\n    try:\n        # Read and parse file\n        content = file_path.read_text(encoding=\"utf-8\")\n        frontmatter, markdown_content = parse_frontmatter_and_content(content)\n\n        if frontmatter is None:\n            errors.append(\"Missing or invalid YAML frontmatter\")\n            return {\"errors\": errors, \"warnings\": warnings}\n\n        # Check required fields\n        if \"description\" not in frontmatter:\n            errors.append(\"Missing required field: description\")\n        elif not str(frontmatter[\"description\"]).strip():\n            errors.append(\"description cannot be empty\")\n\n        # Validate languages and alwaysApply logic\n        has_languages = \"languages\" in frontmatter and frontmatter[\"languages\"]\n        always_apply = frontmatter.get(\"alwaysApply\", False)\n\n        if always_apply and has_languages:\n            errors.append(\"Rules with alwaysApply=true should not have languages\")\n        elif not always_apply and not has_languages:\n            errors.append(\"Rules must have either languages or alwaysApply=true\")\n\n        # Validate language names if present\n        if has_languages and isinstance(frontmatter[\"languages\"], list):\n            unknown = [\n                lang\n                for lang in frontmatter[\"languages\"]\n                if lang.lower() not in LANGUAGE_TO_EXTENSIONS\n            ]\n            if unknown:\n                warnings.append(f\"Unknown languages: {', '.join(unknown)}\")\n\n        # Validate tags if present\n        if \"tags\" in frontmatter:\n            try:\n                normalized_tags = validate_tags(frontmatter[\"tags\"], file_path.name)\n                # Error on tags not in known list\n                unknown_tags = [tag for tag in normalized_tags if tag not in KNOWN_TAGS]\n                if unknown_tags:\n                    errors.append(f\"Unknown tags (add to KNOWN_TAGS): {', '.join(sorted(unknown_tags))}\")\n            except ValueError as e:\n                errors.append(str(e))\n\n        # Check content exists\n        if not markdown_content.strip():\n            errors.append(\"Rule content cannot be empty\")\n\n    except Exception as e:\n        errors.append(f\"Error reading file: {str(e)}\")\n\n    return {\"errors\": errors, \"warnings\": warnings}\n\n\ndef main():\n    \"\"\"Validate all rules in the sources directory.\"\"\"\n    rules_dir = Path(sys.argv[1] if len(sys.argv) > 1 else \"sources\")\n\n    if not rules_dir.exists():\n        print(f\"❌ Directory {rules_dir} does not exist\")\n        sys.exit(1)\n\n    # Find all .md files recursively (excluding README and templates)\n    md_files = [\n        f for f in rules_dir.rglob(\"*.md\") \n        if f.name.lower() != \"readme.md\" and not f.name.endswith(\".template\")\n    ]\n\n    if not md_files:\n        print(f\"❌ No rule files found in {rules_dir}\")\n        sys.exit(1)\n\n    print(f\"🔍 Validating {len(md_files)} rules in {rules_dir} (recursive)\\n\")\n\n    passed = 0\n    failed = 0\n    total_warnings = 0\n\n    for md_file in sorted(md_files):\n        result = validate_rule(md_file)\n        errors = result[\"errors\"]\n        warnings = result[\"warnings\"]\n\n        if errors:\n            failed += 1\n            print(f\"❌ {md_file.name}\")\n            for error in errors:\n                print(f\"   - {error}\")\n        else:\n            passed += 1\n            if warnings:\n                print(f\"✅ {md_file.name}\")\n                for warning in warnings:\n                    print(f\"   ⚠️  {warning}\")\n                    total_warnings += 1\n            else:\n                print(f\"✅ {md_file.name}\")\n\n    # Summary\n    print(f\"\\n📊 Results: {passed} passed, {failed} failed\")\n    if total_warnings:\n        print(f\"   Warnings: {total_warnings}\")\n\n    if failed > 0:\n        print(\"\\n❌ Validation failed\")\n        sys.exit(1)\n    else:\n        print(\"\\n✅ All rules valid!\")\n\n\nif __name__ == \"__main__\":\n    main()\n"
  },
  {
    "path": "src/validate_versions.py",
    "content": "#!/usr/bin/env python3\n# Copyright 2025 Cisco Systems, Inc. and its affiliates\n#\n# SPDX-License-Identifier: Apache-2.0\n\n\"\"\"\nVersion Validation Script\n\nValidates that all version strings match across:\n- pyproject.toml\n- .claude-plugin/plugin.json\n- .claude-plugin/marketplace.json\n\"\"\"\n\nimport json\nimport re\nimport sys\nimport tomllib\nfrom pathlib import Path\nfrom typing import NamedTuple\n\n\nclass VersionCheck(NamedTuple):\n    \"\"\"Result of a version check.\"\"\"\n\n    file: str\n    expected: str\n    found: str\n    matches: bool\n\n\ndef get_pyproject_version(root: Path) -> str:\n    \"\"\"Get version from pyproject.toml.\"\"\"\n    pyproject_path = root / \"pyproject.toml\"\n    with open(pyproject_path, \"rb\") as f:\n        data = tomllib.load(f)\n    return data[\"project\"][\"version\"]\n\n\ndef get_plugin_version(root: Path) -> str:\n    \"\"\"Get version from plugin.json.\"\"\"\n    plugin_path = root / \".claude-plugin\" / \"plugin.json\"\n    with open(plugin_path, encoding=\"utf-8\") as f:\n        data = json.load(f)\n    return data[\"version\"]\n\n\ndef set_plugin_version(version: str, root: Path) -> None:\n    \"\"\"Set version in plugin.json.\"\"\"\n    plugin_path = root / \".claude-plugin\" / \"plugin.json\"\n    with open(plugin_path, encoding=\"utf-8\") as f:\n        data = json.load(f)\n    data[\"version\"] = version\n    with open(plugin_path, \"w\", encoding=\"utf-8\") as f:\n        json.dump(data, f, indent=2)\n        f.write(\"\\n\")\n\n\ndef get_marketplace_version(root: Path) -> str:\n    \"\"\"Get version from marketplace.json.\"\"\"\n    marketplace_path = root / \".claude-plugin\" / \"marketplace.json\"\n    with open(marketplace_path, encoding=\"utf-8\") as f:\n        data = json.load(f)\n    return data[\"plugins\"][0][\"version\"]\n\n\ndef set_marketplace_version(version: str, root: Path) -> None:\n    \"\"\"Set version in marketplace.json.\"\"\"\n    marketplace_path = root / \".claude-plugin\" / \"marketplace.json\"\n    with open(marketplace_path, encoding=\"utf-8\") as f:\n        data = json.load(f)\n    for plugin in data.get(\"plugins\", []):\n        plugin[\"version\"] = version\n    with open(marketplace_path, \"w\", encoding=\"utf-8\") as f:\n        json.dump(data, f, indent=2)\n        f.write(\"\\n\")\n\n\ndef _read_front_matter_value(path: Path, key: str) -> str:\n    \"\"\"Read a YAML front-matter value from a markdown file.\"\"\"\n    content = path.read_text(encoding=\"utf-8\")\n    front_matter_match = re.match(r\"^---\\s*\\n(.*?)\\n---\\s*\\n\", content, re.DOTALL)\n    if not front_matter_match:\n        raise ValueError(f\"Missing front matter in {path}\")\n    front_matter = front_matter_match.group(1)\n    value_match = re.search(\n        rf'^{re.escape(key)}:\\s*\"([^\"]+)\"\\s*$',\n        front_matter,\n        re.MULTILINE,\n    )\n    if not value_match:\n        raise ValueError(f\"Missing {key} in front matter for {path}\")\n    return value_match.group(1)\n\n\ndef get_skill_codeguard_version(root: Path) -> str:\n    \"\"\"Get codeguard-version from skills/software-security/SKILL.md.\"\"\"\n    skill_path = root / \"skills\" / \"software-security\" / \"SKILL.md\"\n    return _read_front_matter_value(skill_path, \"codeguard-version\")\n\n\ndef validate_versions(expected_version: str, root: Path = None) -> list[VersionCheck]:\n    \"\"\"\n    Validate all versions match the expected version.\n\n    Args:\n        expected_version: The version to validate against (e.g., from git tag)\n        root: Project root directory (defaults to parent of this script)\n\n    Returns:\n        List of VersionCheck results\n    \"\"\"\n    if root is None:\n        root = Path(__file__).parent.parent\n\n    checks = [\n        VersionCheck(\n            \"pyproject.toml\", expected_version, get_pyproject_version(root), False\n        ),\n        VersionCheck(\"plugin.json\", expected_version, get_plugin_version(root), False),\n        VersionCheck(\n            \"marketplace.json\", expected_version, get_marketplace_version(root), False\n        ),\n        VersionCheck(\n            \"SKILL.md\",\n            expected_version,\n            get_skill_codeguard_version(root),\n            False,\n        ),\n    ]\n\n    # Update matches field\n    return [\n        VersionCheck(c.file, c.expected, c.found, c.expected == c.found) for c in checks\n    ]\n\n\ndef main() -> int:\n    \"\"\"Main entry point for CLI.\"\"\"\n    if len(sys.argv) != 2:\n        print(\"Usage: validate_versions.py <expected_version>\")\n        print(\"Example: validate_versions.py 1.0.0\")\n        return 1\n\n    expected_version = sys.argv[1]\n    results = validate_versions(expected_version)\n\n    # Print results\n    all_match = True\n    for check in results:\n        if check.matches:\n            print(f\"✅ {check.file}: {check.found}\")\n        else:\n            print(f\"❌ {check.file}: expected {check.expected}, found {check.found}\")\n            all_match = False\n\n    if all_match:\n        print(f\"\\n✅ All versions match: {expected_version}\")\n        return 0\n    else:\n        print(f\"\\n❌ Version mismatch detected!\")\n        return 1\n\n\nif __name__ == \"__main__\":\n    sys.exit(main())\n"
  }
]