Mcp gateway security

From Hack23/riksdagsmonitor · View interactive version

Mcp gateway security is an AI agent skill from the GitHub repository Hack23/riksdagsmonitor. MCP gateway security patterns, token management, request validation, and audit logging for MCP communications

How to use this skill

Skill file

MCP Gateway Security Skill

πŸ”΄ AI FIRST Quality Principle

Apply the AI FIRST principle: never accept first-pass quality. Minimum 2 iterations. Read all output, improve every section. No shortcuts.

Purpose

This skill provides security patterns and best practices for securing MCP (Model Context Protocol) gateway communications in the Riksdagsmonitor platform. It covers token management, request validation, audit logging, and threat mitigation for AI-assisted development workflows.

When to Use This Skill

Apply this skill when:

Do NOT use for:

Threat Model for MCP

Attack Surface

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                 Threat Vectors                β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                              β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚ Token     β”‚    β”‚ Prompt   β”‚    β”‚ Supply β”‚ β”‚
β”‚  β”‚ Theft     β”‚    β”‚ Injectionβ”‚    β”‚ Chain  β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β”‚
β”‚       β”‚               β”‚              β”‚       β”‚
β”‚       β–Ό               β–Ό              β–Ό       β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚         MCP Gateway                      β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚       β”‚            β”‚            β”‚            β”‚
β”‚  β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”  β”Œβ”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”     β”‚
β”‚  β”‚ Unauth  β”‚  β”‚ Data    β”‚  β”‚ Lateral β”‚     β”‚
β”‚  β”‚ Access  β”‚  β”‚ Exfil   β”‚  β”‚ Movementβ”‚     β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Threat Matrix

| Threat | Likelihood | Impact | Mitigation |

|---|---|---|---|

| Token theft from config | Medium | Critical | Environment variables, secret managers |

| Prompt injection via tools | Medium | High | Input validation, output sanitization |

| Supply chain attack on MCP packages | Low | Critical | Version pinning, integrity checks |

| Unauthorized file access | Medium | High | Directory restrictions, least privilege |

| Data exfiltration via MCP tools | Low | High | Output monitoring, allowed destinations |

| Privilege escalation | Low | Critical | Role-based access, capability limits |

Token Management

Token Security Requirements

| Requirement | Implementation | Priority |

|---|---|---|

| No hardcoded tokens | Environment variables only | Critical |

| Token rotation | Regular rotation schedule | High |

| Least privilege scopes | Minimal required permissions | Critical |

| Token encryption at rest | OS keychain or secret manager | High |

| Token audit trail | Log token usage, not values | Medium |

Secure Token Configuration

{
  "mcpServers": {
    "github": {
      "type": "stdio",
      "command": "github-mcp-server",
      "env": {
        "GITHUB_TOKEN": "${GITHUB_TOKEN}"
      }
    }
  }
}

Token Handling Rules:

βœ… DO: Use environment variable references (${VAR_NAME} in MCP JSON config files; $VAR_NAME in shell scripts; ${{ secrets.VAR_NAME }} in GitHub Actions)
βœ… DO: Use GitHub Actions secrets for CI/CD tokens
βœ… DO: Rotate tokens at least quarterly
βœ… DO: Use fine-grained PATs with minimal scopes
βœ… DO: Revoke tokens immediately when compromised

❌ DON'T: Hardcode tokens in configuration files
❌ DON'T: Commit tokens to version control
❌ DON'T: Share tokens between environments
❌ DON'T: Use classic PATs with broad scopes
❌ DON'T: Log token values in any log output

GitHub Token Scopes (Principle of Least Privilege)

| MCP Operation | Required Scope | Justification |

|---|---|---|

| Read code | contents:read | Code search and file reading |

| Create PRs | pull_requests:write | PR creation and updates |

| Manage issues | issues:write | Issue creation and updates |

| Read workflows | actions:read | CI/CD status checking |

| Security alerts | security_events:read | CodeQL and Dependabot |

Request Validation

Input Validation for MCP Tools

File Operations:

Validation Rules:
1. Path must be within allowed directories
2. Path must not contain traversal sequences (../)
3. File extension must be in allowed list
4. File size must not exceed limits
5. Content must not contain known malicious patterns

Code Operations:

Validation Rules:
1. Branch names must match allowed pattern
2. Commit messages must not contain secrets
3. File content must pass security scanning
4. PR descriptions must not leak sensitive data

Output Sanitization

Before Returning MCP Tool Output:
1. Strip any credential-like patterns
2. Remove internal IP addresses/hostnames
3. Truncate excessively large outputs
4. Validate JSON/structured output format
5. Log sanitization actions for audit

Dangerous Tool Patterns

| Tool | Risk | Mitigation |

|---|---|---|

| filesystem.write_file | Overwrite critical files | Restrict to project directories |

| filesystem.delete | Data loss | Require confirmation, backup |

| github.push_files | Inject malicious code | Code review before merge |

| playwright.evaluate | Execute arbitrary JS | Sandbox, restrict domains |

| bash.execute | System command execution | Allowlist commands, sandbox |

Audit Logging

What to Log

| Event | Log Level | Data to Capture |

|---|---|---|

| MCP server start/stop | INFO | Server name, timestamp |

| Tool invocation | INFO | Tool name, parameters (sanitized) |

| Authentication success | INFO | Server name, token type (not value) |

| Authentication failure | WARN | Server name, failure reason |

| Access denied | WARN | Tool, resource, reason |

| Configuration change | INFO | What changed, who changed it |

| Error/exception | ERROR | Error details, stack trace |

What NOT to Log

❌ Token values or API keys
❌ File contents containing secrets
❌ User passwords or credentials
❌ Full request/response bodies with PII
❌ Internal network topology details

Audit Log Format

{
  "timestamp": "2024-01-15T10:30:00Z",
  "event": "mcp.tool.invocation",
  "server": "github",
  "tool": "create_pull_request",
  "parameters": {
    "owner": "Hack23",
    "repo": "cia",
    "title": "[REDACTED]"
  },
  "result": "success",
  "duration_ms": 1250,
  "user": "copilot-agent"
}

Security Configuration Checklist

Pre-Deployment

β–‘ All tokens use environment variables (never hardcoded)
β–‘ Token scopes follow least privilege
β–‘ Filesystem access restricted to project directory only
β–‘ MCP package versions pinned to specific releases
β–‘ Configuration file committed (without secrets)
β–‘ No sensitive data in MCP server arguments
β–‘ SSE server URLs use HTTPS only
β–‘ Certificate validation enabled for remote servers

Periodic Review (Monthly)

β–‘ Review token scopes β€” remove unnecessary permissions
β–‘ Rotate tokens per schedule
β–‘ Check for new MCP package versions and CVEs
β–‘ Review audit logs for anomalies
β–‘ Verify directory restrictions still appropriate
β–‘ Test authentication failure handling
β–‘ Review and update threat model
β–‘ Check for deprecated MCP server versions

Incident Response

MCP Security Incident
    β”‚
    β”œβ”€β†’ Token Compromise
    β”‚   β”œβ”€β†’ Revoke token immediately
    β”‚   β”œβ”€β†’ Rotate all related tokens
    β”‚   β”œβ”€β†’ Review audit logs for unauthorized access
    β”‚   └─→ Update token storage mechanism
    β”‚
    β”œβ”€β†’ Unauthorized File Access
    β”‚   β”œβ”€β†’ Review filesystem server configuration
    β”‚   β”œβ”€β†’ Check for directory traversal attempts
    β”‚   β”œβ”€β†’ Restrict filesystem paths
    β”‚   └─→ Review accessed files for data exposure
    β”‚
    β”œβ”€β†’ Supply Chain Attack
    β”‚   β”œβ”€β†’ Pin to known-good version
    β”‚   β”œβ”€β†’ Verify package integrity
    β”‚   β”œβ”€β†’ Check for malicious tool behavior
    β”‚   └─→ Report to MCP package maintainers
    β”‚
    └─→ Prompt Injection
        β”œβ”€β†’ Review tool invocation logs
        β”œβ”€β†’ Identify injected content
        β”œβ”€β†’ Assess data exposure
        └─→ Update input validation rules

OWASP Agentic Security Alignment

| OWASP Agentic Risk | MCP Mitigation |

|---|---|

| Excessive Agency | Restrict tool capabilities, require confirmation |

| Tool Misuse | Input validation, output monitoring |

| Privilege Escalation | Least privilege tokens, capability limits |

| Data Leakage | Output sanitization, logging controls |

| Insecure Output | Validate all MCP tool responses |

| Supply Chain | Pin versions, verify integrity |

ISMS Alignment

| Security Area | ISO 27001 | NIST CSF | CIS Controls |

|---|---|---|---|

| Token Management | A.8.24, A.5.17 | PR.DS-1 | CIS 3.11 |

| Access Control | A.8.3, A.8.5 | PR.AC-4 | CIS 6.1 |

| Audit Logging | A.8.15 | DE.AE-3 | CIS 8.2 |

| Input Validation | A.8.28 | PR.IP-12 | CIS 16.1 |

| Configuration Mgmt | A.8.9 | PR.IP-1 | CIS 4.1 |

| Incident Response | A.5.24-A.5.27 | RS.MA-1 | CIS 17.1 |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.