Skip to content

Conversation

@github-actions
Copy link
Contributor

Security Fix: Allocation Size Overflow in Environment Key Collection

Alert Number: #20
Severity: High (security_severity_level: high)
Rule: go/allocation-size-overflow
File: pkg/workflow/mcp-config.go:314
CWE: CWE-190 (Integer Overflow or Wraparound)

Vulnerability Description

The renderSharedMCPConfig function was vulnerable to allocation size overflow when computing the capacity for the environment keys array. The vulnerable pattern was:

envKeys := make([]string, 0, len(mcpConfig.Env)+len(headerSecrets))

When computing the size of an allocation based on potentially large values, the result may overflow (for signed integer types) or wraparound (for unsigned types). An overflow causes the result to become negative, while a wraparound results in a small positive number.

If either mcpConfig.Env or headerSecrets maps were extremely large (close to max int), adding their lengths could:

  1. Overflow to negative: Cause a runtime panic when passed to make()
  2. Wraparound: Allocate an unexpectedly small buffer, potentially causing issues

While both maps come from MCP server configuration and are typically small, large configurations could theoretically trigger this vulnerability.

Fix Applied

The fix eliminates the overflow risk by removing pre-allocation entirely and relying on Go's append function to handle capacity growth automatically:

// CWE-190: Allocation Size Overflow Prevention
// Instead of pre-calculating capacity (len(mcpConfig.Env)+len(headerSecrets)),
// which could overflow if the maps are extremely large, we let Go's append
// handle capacity growth automatically. This is safe and efficient for
// environment variable maps which are typically small in practice.
var envKeys []string
for key := range mcpConfig.Env {
    envKeys = append(envKeys, key)
}
// Add header secrets for passthrough (copilot only)
for varName := range headerSecrets {
    // Only add if not already in env
    if _, exists := mcpConfig.Env[varName]; !exists {
        envKeys = append(envKeys, varName)
    }
}
sort.Strings(envKeys)

Approach

Rather than implementing complex overflow guards, the fix takes a simpler approach:

  1. No pre-allocation: Use var envKeys []string instead of make([]string, 0, capacity)
  2. Automatic capacity growth: Let Go's append function handle capacity management efficiently
  3. Simpler code: Eliminates all overflow calculation logic, making the code easier to understand and maintain

This approach is appropriate because environment variable maps are not expected to be large, and Go's append function is optimized for incremental growth.

Security Best Practices Applied

Simplicity over complexity: Removed unnecessary pre-allocation logic that introduced overflow risk
Language built-ins: Relied on Go's append function which handles capacity safely
Maintainability: Cleaner code that's easier to review and maintain
Defense in Depth: Eliminates the vulnerability at its source
Consistent pattern: Follows the same fix pattern used in alerts #6, #7, #28

Testing Considerations

To validate this fix, please test:

  • Normal workflow compilation with MCP server configurations
  • Workflows with custom MCP server environment variables
  • Workflows with HTTP MCP servers that have header secrets
  • Workflows with empty environment maps
  • Existing workflows continue to compile successfully
  • No runtime panics during configuration rendering
  • All unit tests pass (TestMCP* suite passing)

Impact Assessment

Risk Level: Low

  • This is a preventive fix for a potential denial-of-service vulnerability
  • No known exploits in production
  • Realistic environment variable maps are small (typically < 50 variables)
  • Fix maintains backward compatibility

Functionality: No Breaking Changes

  • Normal workflows (with reasonable MCP configurations) are unaffected
  • Code compiles successfully with no errors
  • All existing functionality continues to work as expected
  • Simpler implementation reduces maintenance burden

Related Security Alerts

This PR fixes CodeQL alert #20. There is also alert #21 in the same file with a similar pattern that should be addressed separately.

Previous similar fixes:

References


🤖 Generated with Claude Code

Co-Authored-By: Claude noreply@anthropic.com

AI generated by Security Fix PR

AI generated by Security Fix PR

…lection (Alert #20)

Fix CWE-190 allocation size overflow vulnerability in pkg/workflow/mcp-config.go
at line 314. The code was computing slice capacity by adding two map lengths
(len(mcpConfig.Env)+len(headerSecrets)), which could overflow with extremely
large maps.

Changed from pre-allocated slice to letting Go's append handle capacity
automatically, following the same pattern used in previous fixes (alerts #6, #7).
This eliminates the overflow risk while maintaining efficiency for typical
use cases where environment variable maps are small.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@pelikhan pelikhan marked this pull request as ready for review October 15, 2025 20:56
@pelikhan pelikhan merged commit 152b62e into main Oct 15, 2025
5 checks passed
@pelikhan pelikhan deleted the security-fix-alert-20-allocation-overflow-env-keys-1968296b7c7bcf87 branch October 15, 2025 20:57
@github-actions
Copy link
Contributor Author

Agentic Changeset Generator triggered by this pull request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants