feat(typescript): add standalone @microsoft/agentmesh-mcp-governance package#827
feat(typescript): add standalone @microsoft/agentmesh-mcp-governance package#827jackbatzner wants to merge 2 commits intomicrosoft:mainfrom
Conversation
|
Welcome to the Agent Governance Toolkit! Thanks for your first pull request. |
There was a problem hiding this comment.
🤖 AI Agent: code-reviewer
Pull Request Review: feat(typescript): add standalone @microsoft/agentmesh-mcp-governance package
Summary
This PR introduces a new standalone TypeScript package, @microsoft/agentmesh-mcp-governance, which provides MCP governance primitives without requiring the full Agent Mesh SDK. The package includes features such as credential redaction, rate limiting, session authentication, message signing, and gateway enforcement. The implementation appears well-structured, with a focus on modularity and security.
🔴 CRITICAL Issues
-
Regex Denial of Service (ReDoS) Vulnerability
- Files Affected:
credential-redactor.ts,gateway.ts - Issue: The use of unbounded regular expressions (e.g.,
\bsk-[A-Za-z0-9]{16,}\b,\bBearer\s+[A-Za-z0-9._\-+/=]{10,}\b) inCredentialRedactorandMcpGatewaycan lead to catastrophic backtracking, making the system vulnerable to ReDoS attacks. - Recommendation: Use more efficient regex patterns or limit the input size before applying regex. Consider using libraries like
safe-regexto validate regex patterns for safety.
- Files Affected:
-
Potential Credential Leakage
- Files Affected:
credential-redactor.ts,gateway.ts - Issue: The
CredentialRedactorandMcpGatewayrely on regex patterns to identify sensitive data. If a pattern does not match a specific credential format, sensitive data may be leaked. - Recommendation: Implement a fallback mechanism to flag any unrecognized patterns or enforce stricter input validation to minimize the risk of leakage.
- Files Affected:
-
Fail-Closed Behavior on Errors
- Files Affected:
gateway.ts - Issue: While the
McpGatewayimplementation attempts to fail closed on errors, there are multipletry-catchblocks (e.g., infinalizeandsafeRedactParams) that silently handle errors without logging or propagating them. - Recommendation: Ensure all errors are logged securely and consider propagating critical errors to higher layers for visibility.
- Files Affected:
🟡 WARNING: Potential Breaking Changes
-
Public API Stability
- Files Affected:
index.ts,README.md - Issue: The package is marked as "Public Preview," and the APIs are subject to change before GA. This could lead to breaking changes for early adopters.
- Recommendation: Clearly communicate API stability expectations in the documentation and consider versioning strategies to minimize disruption for users.
- Files Affected:
-
Custom Patterns in Credential Redactor
- Files Affected:
credential-redactor.ts - Issue: The
CredentialRedactorallows custom patterns, but if users provide unsafe or inefficient regex patterns, it could lead to performance issues or ReDoS vulnerabilities. - Recommendation: Validate custom regex patterns using a library like
safe-regexbefore accepting them.
- Files Affected:
💡 Suggestions for Improvement
-
Thread Safety
- Files Affected:
credential-redactor.ts,gateway.ts - Observation: The
CredentialRedactorandMcpGatewayclasses use shared state (e.g.,WeakMapforseenobjects). While JavaScript is single-threaded, Node.js environments may involve worker threads or asynchronous operations that could lead to race conditions. - Recommendation: Document the thread safety assumptions and consider using thread-safe data structures or synchronization mechanisms if multithreading is a possibility.
- Files Affected:
-
Enhanced Logging
- Files Affected:
gateway.ts - Observation: The
McpGatewayclass could benefit from more detailed logging, especially for decisions made at each stage (e.g., policy evaluation, rate limiting, approval). - Recommendation: Add structured logging for key decision points to improve observability and debugging.
- Files Affected:
-
Rate Limiter Configuration
- Files Affected:
gateway.ts - Observation: The
McpSlidingRateLimiteris used as the default rate limiter, but its configuration is not well-documented in the README. - Recommendation: Include detailed documentation on how to configure the rate limiter, including examples for different use cases.
- Files Affected:
-
Test Coverage
- Files Affected:
jest.config.js,tests/ - Observation: While the PR mentions that tests have been added, the test coverage for critical components like
McpGatewayandCredentialRedactoris not visible in the diff. - Recommendation: Ensure comprehensive test coverage for all critical paths, including edge cases, error handling, and performance under load.
- Files Affected:
-
Type Safety
- Files Affected:
credential-redactor.ts,gateway.ts - Observation: The use of
unknowntypes inredactNodeandsafeRedactParamscould lead to runtime errors if the input structure is not as expected. - Recommendation: Use stricter TypeScript types and runtime validation (e.g.,
zodorio-ts) to ensure input data conforms to expected schemas.
- Files Affected:
-
OWASP Agentic Top 10 Compliance
- Files Affected: General
- Observation: The package appears to address several OWASP Agentic Top 10 risks (e.g., input validation, rate limiting, and credential redaction). However, there is no explicit mention of compliance in the documentation.
- Recommendation: Map the package's features to OWASP Agentic Top 10 risks in the README to highlight its security posture.
-
Backward Compatibility
- Files Affected:
index.ts - Observation: The package re-exports several classes with both their original and aliased names (e.g.,
McpResponseScannerandMCPResponseScanner). This could lead to confusion and potential issues with backward compatibility in future versions. - Recommendation: Standardize on a single naming convention and deprecate one of the aliases to avoid confusion.
- Files Affected:
Conclusion
This PR introduces a valuable addition to the agent-governance-toolkit repository by providing a lightweight, standalone package for MCP governance. However, there are critical security issues related to regex patterns and error handling that must be addressed before merging. Additionally, attention should be given to API stability, thread safety, and documentation to ensure a robust and user-friendly release.
Let me know if you need further clarification or assistance!
🤖 AI Agent: security-scanner — Security Review of `@microsoft/agentmesh-mcp-governance`Security Review of
|
| Finding | Rating | Attack Vector | Recommendation |
|---|---|---|---|
| Prompt injection defense bypass | 🔴 CRITICAL | Malicious input bypasses blocked patterns or shell expansion checks. | Expand patterns, enhance hasShellExpansion, and consider a dedicated library. |
| Policy engine circumvention | 🟠 HIGH | Optional policyEvaluator allows insecure defaults or weak implementations. |
Make policyEvaluator mandatory and provide a secure default implementation. |
| Trust chain weaknesses | 🔵 LOW | Lack of secret management guidance and replay protection in message signing. | Add secret management guidance and implement timestamp/nonce validation. |
| Credential exposure | 🟠 HIGH | Incomplete redaction patterns may expose sensitive information. | Expand patterns, handle edge cases, and add comprehensive tests. |
| Sandbox escape | 🔵 LOW | Limited checks for sandbox escape techniques. | Add checks for file inclusion, environment variable injection, and other methods. |
| Deserialization attacks | 🔵 LOW | No deserialization currently, but future risks exist. | Use safe deserialization libraries if needed in the future. |
| Race conditions | 🟡 MEDIUM | Potential race conditions in rate-limiting and approval mechanisms. | Ensure thread safety and provide guidance for thread-safe callback implementations. |
| Supply chain risks | 🟡 MEDIUM | Dependency confusion or typosquatting risks in package.json. |
Use dependency auditing tools and pin dependency versions. |
Final Recommendation
This pull request introduces a critical security package, and the identified issues must be addressed before merging. Specifically:
- Address the Prompt Injection Defense Bypass issue as a priority.
- Ensure the Policy Engine Circumvention issue is resolved by requiring a secure
policyEvaluator. - Expand the
CredentialRedactorto cover more credential formats and edge cases. - Implement replay protection in
MCPMessageSigner.
Once these critical and high-priority issues are resolved, the pull request can be re-evaluated for merging.
There was a problem hiding this comment.
🤖 AI Agent: code-reviewer
Pull Request Review: feat(typescript): add standalone @microsoft/agentmesh-mcp-governance package
General Feedback
This PR introduces a new standalone TypeScript package, @microsoft/agentmesh-mcp-governance, which provides MCP governance primitives without requiring the full Agent Mesh SDK. The package appears well-structured, with a clear focus on modularity and reusability. The provided documentation is comprehensive, and the code adheres to modern TypeScript and JavaScript best practices.
However, there are several areas where improvements can be made, particularly in terms of security, type safety, and backward compatibility. Below is a detailed review of the PR.
🔴 CRITICAL: Security Issues
-
Regex Denial of Service (ReDoS) Vulnerabilities
-
The use of regular expressions in
CredentialRedactorandMcpGatewayintroduces potential ReDoS vulnerabilities. For example:const SENSITIVE_KEY_PATTERN = /(password|passwd|pwd|secret|token|api[_-]?key|connection.?string|accountkey|sharedaccesssignature|sas)/i; const BUILTIN_DANGEROUS_PATTERNS = [ /\b\d{3}-\d{2}-\d{4}\b/gi, // SSN /\b\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}\b/gi, // Credit card /;\s*(?:rm|del|format|mkfs)\b/gi, // Shell commands /\$\([^)\r\n]*\)/g, // Command substitution /`[^`]+`/g, // Backticks ];
These patterns are prone to catastrophic backtracking, especially when applied to large inputs. For example, the
SENSITIVE_KEY_PATTERNandBUILTIN_DANGEROUS_PATTERNScould be exploited to cause significant performance degradation.Actionable Recommendation:
- Use a library like safe-regex to validate the safety of regular expressions.
- Consider using more efficient, non-backtracking regex patterns or alternative parsing methods.
-
-
Fail-Closed Behavior in
McpGateway-
The
evaluateToolCallmethod inMcpGatewayhas a fail-closed mechanism for errors:return { allowed: false, reason: getSafeErrorMessage(error, 'Internal error - access denied (fail-closed)'), redactedParams: this.safeRedactParams(params), findings: [], };
While this is a good practice, it would be better to log the error details (e.g., stack trace) for debugging purposes. Currently, the error is only converted to a safe error message, which might obscure critical debugging information.
Actionable Recommendation:
- Log the full error details (e.g., stack trace) to a secure logging system for debugging purposes, while still returning a sanitized error message to the caller.
-
-
Credential Redaction
-
The
CredentialRedactorclass uses a set of predefined patterns to identify and redact sensitive information. However, the current implementation does not allow for dynamic updates to the list of patterns after the class is instantiated. -
Additionally, the
redactNodemethod does not handle circular references in objects, which could lead to infinite recursion.Actionable Recommendation:
- Allow dynamic updates to the list of patterns (e.g., via a
addPatternmethod). - Add a mechanism to detect and handle circular references in
redactNode.
- Allow dynamic updates to the list of patterns (e.g., via a
-
-
HMAC-SHA256 Key Management
-
The
MCPMessageSignerclass uses a secret key for HMAC-SHA256 signing. However, there is no mention of how this key is securely stored or rotated.Actionable Recommendation:
- Provide guidance in the documentation on securely storing and rotating the HMAC secret key.
- Consider integrating with a secure key management service (e.g., Azure Key Vault).
-
🟡 WARNING: Potential Breaking Changes
- Public API Stability
-
The package is marked as "Public Preview," and the README explicitly states that APIs may change before GA. While this is acceptable for a preview release, it is important to ensure that any breaking changes are clearly communicated to users.
Actionable Recommendation:
- Maintain a changelog that explicitly documents breaking changes.
- Consider versioning the API to allow for smoother transitions between versions.
-
💡 Suggestions for Improvement
-
Type Safety
-
The
redactmethod inCredentialRedactoruses a generic typeT, but the return type is cast toTwithout ensuring that the redacted object conforms to the original type. This could lead to runtime type errors.Actionable Recommendation:
- Use a stricter type-checking mechanism to ensure that the redacted object conforms to the original type.
-
-
Concurrency in
McpSlidingRateLimiter-
The
McpSlidingRateLimiterimplementation is not included in the diff, but rate limiters often involve shared state. Ensure that the implementation is thread-safe, especially in distributed environments.Actionable Recommendation:
- Use atomic operations or distributed locks to ensure thread safety in the rate limiter.
-
-
Documentation
-
The README provides a good overview of the package, but it could benefit from additional details on:
- Security best practices (e.g., key management, rate limiter configuration).
- Examples of integrating the package with other Agent Mesh components.
Actionable Recommendation:
- Expand the README to include these details.
-
-
Testing
-
While the PR mentions that tests have been added, the diff does not include the test files. Ensure that the following are covered:
- Edge cases for regex patterns (e.g., large inputs, malicious patterns).
- Concurrency tests for the rate limiter.
- End-to-end tests for the
McpGatewayclass.
Actionable Recommendation:
- Add tests for edge cases and concurrency scenarios.
- Include test coverage reports in the CI pipeline.
-
Summary of Feedback
- 🔴 CRITICAL: Address potential ReDoS vulnerabilities in regex patterns.
- 🔴 CRITICAL: Ensure proper error logging in fail-closed scenarios.
- 🔴 CRITICAL: Improve credential redaction to handle dynamic updates and circular references.
- 🔴 CRITICAL: Provide guidance on secure storage and rotation of HMAC secret keys.
- 🟡 WARNING: Clearly document potential breaking changes and maintain a changelog.
- 💡 SUGGESTION: Improve type safety in
CredentialRedactor. - 💡 SUGGESTION: Ensure thread safety in the rate limiter.
- 💡 SUGGESTION: Expand documentation to include security best practices and integration examples.
- 💡 SUGGESTION: Add comprehensive tests for edge cases, concurrency, and end-to-end scenarios.
This PR introduces a valuable addition to the project, but addressing the highlighted issues is critical to ensure the security, reliability, and usability of the package.
There was a problem hiding this comment.
🤖 AI Agent: code-reviewer
Pull Request Review: feat(typescript): add standalone @microsoft/agentmesh-mcp-governance package
🔴 CRITICAL
-
Credential Redaction Incompleteness:
- The
CredentialRedactorclass uses a set of predefined patterns (BUILTIN_PATTERNS) to detect and redact sensitive information. However, the list of patterns may not cover all possible credential formats, especially custom or less common ones. - Impact: False negatives in credential redaction could lead to sensitive information being logged or exposed.
- Action: Add a mechanism to allow users to dynamically add or update patterns at runtime. Additionally, provide a comprehensive list of patterns in the documentation and encourage users to review and extend them as needed.
- The
-
Fail-Closed Behavior in
McpGateway:- The
evaluateToolCallmethod inMcpGatewayhas a fail-closed mechanism for errors. While this is a good practice, the error message returned (Internal error - access denied (fail-closed)) is generic. - Impact: Lack of detailed error messages may hinder debugging and troubleshooting.
- Action: Log detailed error messages internally (e.g., to an audit sink) while keeping the external error message generic for security reasons.
- The
-
Regex Denial of Service (ReDoS) Risk:
- The use of regular expressions in both
CredentialRedactorandMcpGateway(e.g.,BUILTIN_PATTERNSandBUILTIN_DANGEROUS_PATTERNS) introduces a potential risk of ReDoS attacks if the input is maliciously crafted. - Action: Use libraries like
safe-regexto validate the safety of regular expressions. Additionally, consider using a library likere2for regex operations to mitigate ReDoS risks.
- The use of regular expressions in both
-
Session Secret Management:
- The
McpSessionAuthenticatorandMcpMessageSignerrely on secrets (MCP_SESSION_SECRETandMCP_SIGNING_SECRET) provided via environment variables. There is no validation or rotation mechanism for these secrets. - Impact: Weak or improperly managed secrets can lead to security vulnerabilities.
- Action: Add validation for the strength of the secrets and provide guidance on secure secret management. Consider integrating with a secret management solution (e.g., Azure Key Vault).
- The
🟡 WARNING
-
Potential Breaking Changes in API:
- The package introduces a new standalone module with re-exported MCP primitives. While the PR mentions that APIs may change before GA, any changes to the exported interfaces (e.g.,
McpGateway,CredentialRedactor) could break existing implementations. - Action: Clearly document the stability of each API and provide a migration guide for any future changes.
- The package introduces a new standalone module with re-exported MCP primitives. While the PR mentions that APIs may change before GA, any changes to the exported interfaces (e.g.,
-
Default Configuration Assumptions:
- The
McpGatewayclass uses default configurations fordeniedTools,allowedTools, andsensitiveTools. These defaults may not align with the security policies of all users. - Action: Explicitly document the default behavior and encourage users to review and customize these configurations during setup.
- The
💡 SUGGESTIONS
-
TypeScript Type Safety:
- The use of
Record<string, unknown>for parameters inMcpGatewayandCredentialRedactorcould be improved by using stricter types or generics to enforce type safety. - Action: Define specific interfaces or use generics to provide better type safety and developer experience.
- The use of
-
Concurrency and Thread Safety:
- The
McpSlidingRateLimiterandInMemoryMCPAuditSinkimplementations may face concurrency issues in multi-threaded or distributed environments. - Action: Document that these implementations are suitable for single-threaded or test environments only. Provide guidance or examples for integrating with distributed stores (e.g., Redis).
- The
-
OWASP MCP Compliance:
- The README mentions an "OWASP MCP Cheat Sheet mapping," but no details are provided.
- Action: Include a detailed mapping of how the package aligns with OWASP MCP guidelines in the documentation.
-
Testing Coverage:
- While the PR mentions that tests have been added, there is no mention of coverage metrics or specific test cases for critical components like
McpGatewayandCredentialRedactor. - Action: Ensure 100% test coverage for security-critical components and include tests for edge cases (e.g., malformed inputs, large payloads).
- While the PR mentions that tests have been added, there is no mention of coverage metrics or specific test cases for critical components like
-
Performance Optimization:
- The
CredentialRedactorandMcpGatewayclasses perform multiple regex scans on input data. For large payloads, this could lead to performance bottlenecks. - Action: Benchmark the performance of these operations and consider optimizing the regex patterns or using alternative approaches for large datasets.
- The
-
Documentation Improvements:
- The README provides a good overview but could benefit from additional details:
- Examples of how to implement custom
auditSinkandrateLimiter. - Guidance on securely managing secrets.
- Explanation of the
scanTimeoutMsparameter and its impact on performance and security.
- Examples of how to implement custom
- The README provides a good overview but could benefit from additional details:
-
Versioning Strategy:
- The package is introduced as version
3.0.2, which might be confusing for users expecting a new package to start at1.0.0. - Action: Consider starting the version at
1.0.0or provide a clear explanation for the versioning choice.
- The package is introduced as version
Summary
The PR introduces a useful and well-structured standalone package for MCP governance. However, there are critical security concerns related to credential redaction, regex safety, and secret management that must be addressed before merging. Additionally, there are potential breaking changes and areas for improvement in documentation, type safety, and performance optimization.
Action Items:
- 🔴 Address the critical issues related to credential redaction, ReDoS risks, and secret management.
- 🟡 Clearly document the API stability and provide a migration strategy for future changes.
- 💡 Improve type safety, concurrency handling, and documentation.
Once the critical issues are resolved and additional tests are added, this PR will be ready for merging.
Addressing AI Reviewer Findings"Credential Redaction Incompleteness" — Custom patterns are supported via the CodeQL ReDoS — Second round of regex hardening incoming to replace remaining polynomial patterns. |
There was a problem hiding this comment.
🤖 AI Agent: code-reviewer
Pull Request Review: feat(typescript): add standalone @microsoft/agentmesh-mcp-governance package
🔴 CRITICAL
-
Regex Denial-of-Service (ReDoS) Vulnerability in CredentialRedactor
- The
CredentialRedactorclass uses regular expressions to identify sensitive data. Some of the patterns, such aspem_blockandconnection_string, are prone to catastrophic backtracking, which can lead to a denial-of-service (DoS) attack if a maliciously crafted input is provided. - Actionable Fix: Use safe regex libraries like safe-regex to validate the regular expressions or refactor the regex patterns to avoid catastrophic backtracking.
- The
-
Fail-Closed Behavior in
McpGateway- While the
evaluateToolCallmethod attempts to fail-closed on errors, thefinalizemethod may still allow partial or incomplete audit entries to be recorded. This could lead to inconsistent audit logs in case of runtime errors. - Actionable Fix: Ensure that
finalizeexplicitly logs any errors encountered during the audit process and provides a fallback mechanism to ensure audit integrity.
- While the
-
Insufficient Replay Protection in
MCPMessageSigner- The
MCPMessageSignerclass is mentioned in the README but not included in the diff. If it uses HMAC-SHA256 for signing payloads, ensure that it includes robust nonce handling and timestamp validation to prevent replay attacks. - Actionable Fix: Review the implementation of
MCPMessageSignerto confirm that it enforces strict nonce uniqueness and timestamp expiration checks.
- The
-
Blocked Patterns in
McpGateway- The
checkSanitizationmethod inMcpGatewayuses both user-defined and built-in patterns to detect dangerous input. However, the built-in patterns include generic regexes like/\$\([^)]*\)/gand/[^]+/g`, which may not cover all potential injection vectors. - Actionable Fix: Expand the built-in dangerous patterns to include more comprehensive injection patterns (e.g., SQL injection, command injection). Consider integrating with a well-maintained library for detecting injection patterns.
- The
🟡 WARNING
-
Backward Compatibility
- The introduction of a new standalone package
@microsoft/agentmesh-mcp-governanceis a non-breaking change, but it introduces a new public API. Any future changes to this package will need to maintain backward compatibility. - Actionable Fix: Clearly document the API stability and versioning strategy for this package, especially since it is marked as a "Public Preview."
- The introduction of a new standalone package
-
Potential for Misconfiguration
- The
McpGatewayclass relies on user-provided configurations for critical security features likeapprovalHandler,rateLimiter, andpolicyEvaluator. Misconfiguration of these components could lead to security bypasses. - Actionable Fix: Add validation for the
McpGatewayConfigobject to ensure that all required components are properly configured. For example, throw an error ifapprovalHandleris missing whensensitiveToolsis defined.
- The
💡 SUGGESTIONS
-
Type Safety Improvements
- The
redactmethod inCredentialRedactorusesunknownas the type for input values. This reduces type safety when working with structured objects. - Suggestion: Use a generic type parameter for the
redactmethod to preserve type information. For example:redact<T>(value: T): CredentialRedactionResult<T> { // Implementation }
- The
-
Performance Optimization for Regex Scans
- The
createRegexScanBudgetmethod is used to enforce a timeout for regex scans. However, this approach may introduce performance overhead in high-throughput scenarios. - Suggestion: Consider using a streaming approach or pre-compiling regex patterns to improve performance.
- The
-
Enhanced Documentation
- The README provides a good overview of the package but lacks detailed examples for advanced use cases, such as integrating with custom audit sinks or implementing durable rate-limit storage.
- Suggestion: Add a section to the README with advanced usage examples and best practices for production deployments.
-
Thread Safety
- The
McpSlidingRateLimiterandInMemoryMCPAuditSinkclasses are mentioned but not included in the diff. If these classes use shared in-memory storage, they may not be thread-safe in concurrent environments. - Suggestion: Ensure that all shared in-memory components are thread-safe or explicitly document that they are intended for single-threaded use only.
- The
-
OWASP MCP Cheat Sheet Mapping
- The README mentions an OWASP MCP Cheat Sheet mapping but does not provide a direct link or detailed explanation.
- Suggestion: Include a link to the OWASP MCP Cheat Sheet and provide a table mapping the package's features to specific OWASP recommendations.
Summary
This pull request introduces a standalone @microsoft/agentmesh-mcp-governance package with a strong focus on modularity and security. While the implementation is generally robust, there are critical issues related to regex vulnerabilities, replay protection, and fail-closed behavior that must be addressed before merging. Additionally, there are opportunities to improve type safety, documentation, and performance.
Action Items:
- Address the critical issues flagged above.
- Add validation for user-provided configurations in
McpGateway. - Expand the README with advanced usage examples and OWASP mappings.
- Ensure thread safety for in-memory components.
Let me know if you need further clarification or assistance!
There was a problem hiding this comment.
🤖 AI Agent: code-reviewer
Pull Request Review: feat(typescript): add standalone @microsoft/agentmesh-mcp-governance package
Summary
This PR introduces a new standalone TypeScript package, @microsoft/agentmesh-mcp-governance, which provides MCP governance primitives without requiring the full Agent Mesh SDK. The package includes various security-related utilities such as credential redaction, rate limiting, payload signing, and gateway enforcement.
🔴 CRITICAL Issues
-
Regex Denial of Service (ReDoS) Vulnerability
- File:
credential-redactor.tsandgateway.ts - Issue: The use of regular expressions in
BUILTIN_PATTERNSandBUILTIN_DANGEROUS_PATTERNScould lead to ReDoS attacks if the input is crafted maliciously. For example, patterns like/\bBearer\s+[A-Za-z0-9._\-+/=]{10,}\b/giand/\b\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}\b/giare susceptible to catastrophic backtracking. - Recommendation: Use a library like safe-regex to validate the safety of regular expressions. Alternatively, consider using more efficient algorithms for pattern matching, such as DFA-based approaches.
- File:
-
Fail-Closed Behavior on Regex Timeout
- File:
credential-redactor.tsandgateway.ts - Issue: The
createRegexScanBudgetfunction enforces a timeout for regex scans, but the fallback behavior (content blockedoraccess denied) could lead to denial of service if legitimate inputs are incorrectly flagged. - Recommendation: Log and monitor regex timeouts to identify patterns causing excessive scan times. Consider optimizing or replacing problematic regex patterns.
- File:
-
Insecure Default Configuration
- File:
gateway.ts - Issue: The
McpGatewayclass defaults to an emptyallowedToolslist, which could lead to unintended tool calls being allowed if the configuration is incomplete. - Recommendation: Default to a deny-all policy (
allowedTools = []) and require explicit configuration for allowed tools.
- File:
-
Credential Redaction in Logs
- File:
credential-redactor.ts - Issue: The
CredentialRedactorclass redacts sensitive information but does not guarantee that all sensitive data is removed, especially if custom patterns are incomplete or misconfigured. - Recommendation: Add a warning or validation mechanism for custom patterns to ensure they are comprehensive. Provide a way to test patterns against sample inputs.
- File:
🟡 WARNING: Potential Breaking Changes
-
Public API Instability
- File:
README.md,package.json - Issue: The package is marked as "Public Preview," and APIs may change before GA. This could lead to breaking changes for early adopters.
- Recommendation: Clearly document the expected stability of each API and provide a migration guide for future changes.
- File:
-
Behavior of
McpGatewayPolicy Evaluator- File:
gateway.ts - Issue: The
policyEvaluatorfunction can returnundefined, which is treated as an implicit "allow." This behavior might lead to unexpected results if the evaluator fails to return a decision. - Recommendation: Treat
undefinedas a failure and default to a fail-closed behavior unless explicitly configured otherwise.
- File:
💡 Suggestions for Improvement
-
Type Safety Enhancements
- File:
credential-redactor.ts,gateway.ts - Issue: Some methods, such as
redactNodeandsafeRedactParams, useunknowntypes for input parameters, which can lead to runtime errors if the input is not validated. - Recommendation: Use stricter TypeScript types or runtime type-checking libraries (e.g.,
io-tsorzod) for input validation.
- File:
-
Thread Safety
- File:
gateway.ts - Issue: The
McpSlidingRateLimiterandInMemoryMCPAuditSinkimplementations may not be thread-safe, especially in multi-threaded environments. - Recommendation: Document the thread safety assumptions and provide thread-safe alternatives for production use cases.
- File:
-
OWASP Agentic Top 10 Compliance
- File:
gateway.ts - Issue: The
McpGatewayclass does not explicitly address all OWASP Agentic Top 10 risks, such as "Agent Impersonation" or "Exfiltration of Sensitive Data." - Recommendation: Include specific mitigations for these risks, such as stronger identity verification mechanisms and data exfiltration detection.
- File:
-
Test Coverage
- File:
jest.config.js - Issue: The test configuration does not enforce a minimum coverage threshold.
- Recommendation: Add a
coverageThresholdconfiguration to ensure critical components, such asMcpGatewayandCredentialRedactor, are thoroughly tested.
- File:
-
Documentation Improvements
- File:
README.md - Issue: The README does not provide detailed examples for all exported classes, such as
McpResponseScannerandMcpSecurityScanner. - Recommendation: Include usage examples and best practices for each class to help users adopt the package securely.
- File:
Summary of Recommendations
- 🔴 Address ReDoS vulnerabilities in regex patterns.
- 🔴 Ensure fail-closed behavior for regex timeouts and policy evaluation.
- 🟡 Document API stability and provide migration guidance for future changes.
- 💡 Enhance type safety and runtime validation.
- 💡 Improve thread safety for rate limiter and audit sink implementations.
- 💡 Add minimum test coverage thresholds and expand documentation.
Let me know if you need further clarification or assistance!
packages/agent-mesh/packages/mcp-governance/tests/credential-redactor.test.ts
Fixed
Show fixed
Hide fixed
There was a problem hiding this comment.
🤖 AI Agent: code-reviewer
Review Summary
This pull request introduces a new standalone TypeScript package, @microsoft/agentmesh-mcp-governance, which provides MCP governance primitives without requiring the full Agent Mesh SDK. The package includes several security-focused utilities such as credential redaction, rate limiting, HMAC signing, and policy enforcement. The implementation appears well-structured and adheres to best practices in many areas, but there are some critical security concerns, potential breaking changes, and areas for improvement.
🔴 CRITICAL
-
Regex Denial of Service (ReDoS) Vulnerability
- The
CredentialRedactorandMcpGatewayclasses use regular expressions for sensitive data redaction and pattern matching. Some of these regex patterns (e.g.,SENSITIVE_KEY_PATTERN,BUILTIN_PATTERNS, andBUILTIN_DANGEROUS_PATTERNS) are complex and may be vulnerable to ReDoS attacks, especially when processing large inputs. - Actionable Fix: Use a regex library that supports timeout or backtracking limits (e.g.,
safe-regexorre2), or pre-validate inputs to ensure they are within acceptable size limits before applying regex patterns.
- The
-
Fail-Closed Behavior on Errors
- While the
McpGatewayclass attempts to fail-closed on errors (e.g., inevaluateToolCallandfinalize), there are no guarantees that all exceptions are properly caught. For example, if an unexpected error occurs in theapprovalHandlerorauditSink, it could lead to undefined behavior. - Actionable Fix: Add a global error boundary or fallback mechanism to ensure that any uncaught exceptions result in a fail-closed decision.
- While the
-
Credential Redaction Incompleteness
- The
CredentialRedactorclass relies on predefined patterns (BUILTIN_PATTERNS) and sensitive key matching (SENSITIVE_KEY_PATTERN). However, this approach may miss custom or less common credential formats, leading to incomplete redaction. - Actionable Fix: Allow users to configure additional patterns dynamically at runtime and provide a mechanism to log unredacted sensitive data for debugging in a secure environment.
- The
-
Shell Expansion Detection
- The
hasShellExpansionfunction inMcpGatewayonly checks for$(...)syntax but does not account for other shell expansion patterns like${...}or backticks (`...`). - Actionable Fix: Expand the shell expansion detection logic to cover additional patterns and ensure comprehensive sanitization.
- The
🟡 WARNING
-
Potential Breaking Changes in Public API
- The
@microsoft/agentmesh-mcp-governancepackage introduces new APIs that may evolve before GA. While this is marked as a "Public Preview," any changes to these APIs in future releases could break consumers relying on the current implementation. - Actionable Fix: Clearly document the API stability guarantees (e.g., semantic versioning policy) and provide deprecation warnings for any future changes.
- The
-
Rate Limiter Behavior
- The
McpSlidingRateLimiterenforces rate limits but does not provide a mechanism for distributed rate limiting across multiple processes or nodes. This could lead to inconsistent enforcement in distributed deployments. - Actionable Fix: Add support for a distributed rate limiter (e.g., Redis-based) or document the limitations of the current implementation.
- The
💡 SUGGESTIONS
-
Type Safety Enhancements
- The
McpGatewayandCredentialRedactorclasses rely onRecord<string, unknown>for parameters and redacted outputs. This could lead to runtime type errors if the input structure is not as expected. - Suggestion: Use stricter TypeScript types or generics to enforce type safety for inputs and outputs.
- The
-
OWASP MCP Compliance
- While the package provides several security features, it would be beneficial to explicitly map these features to OWASP MCP (Machine Control Plane) compliance requirements in the documentation.
- Suggestion: Add a section in the README or documentation that maps each feature to specific OWASP MCP guidelines.
-
Performance Optimization
- The
CredentialRedactorandMcpGatewayclasses perform multiple regex scans on potentially large inputs. This could impact performance in high-throughput scenarios. - Suggestion: Benchmark the performance of these classes under realistic workloads and optimize regex patterns or introduce caching where applicable.
- The
-
Test Coverage
- The package includes unit tests, but there is no mention of integration or end-to-end tests to validate the behavior of the package in real-world scenarios.
- Suggestion: Add integration tests that simulate common use cases, such as evaluating tool calls with complex parameters or handling high request volumes.
-
Backward Compatibility
- The
@microsoft/agentmesh-mcp-governancepackage is designed to be standalone, but it re-exports primitives from the core TypeScript SDK. Any changes to the core SDK could inadvertently affect this package. - Suggestion: Use semantic versioning constraints in the
package.jsonfile to ensure compatibility with specific versions of the core SDK.
- The
Final Recommendation
- Address the critical security issues (ReDoS vulnerability, fail-closed behavior, credential redaction completeness, and shell expansion detection) before merging.
- Consider the potential breaking changes and document API stability guarantees.
- Implement the suggested improvements to enhance type safety, performance, and test coverage.
Once the critical issues are resolved, this package will be a valuable addition to the microsoft/agent-governance-toolkit repository.
There was a problem hiding this comment.
🤖 AI Agent: code-reviewer
Pull Request Review: feat(typescript): add standalone @microsoft/agentmesh-mcp-governance package
🔴 CRITICAL Issues
-
Regex Denial-of-Service (ReDoS) Risk in Credential Redactor:
- The
CredentialRedactorclass uses regular expressions for sensitive data redaction. WhilevalidateRegexis called, there is no explicit check for catastrophic backtracking in user-providedcustomPatterns. This could lead to a denial-of-service (DoS) attack if a maliciously crafted regex is passed. - Action: Add a stricter validation mechanism for user-provided regex patterns to ensure they are safe and do not exhibit catastrophic backtracking behavior.
- The
-
Fail-Closed Behavior in Gateway:
- The
McpGatewayclass has a fail-closed mechanism in case of errors (e.g., inevaluateToolCallandfinalize). However, the error handling does not log the full stack trace or provide sufficient context for debugging. - Action: Enhance error logging to include stack traces and additional context (e.g., agent ID, tool name, and stage) to aid in debugging while ensuring sensitive data is not exposed.
- The
-
HMAC Key Handling:
- The
MCPMessageSigneruses a secret key (MCP_SIGNING_SECRET) for HMAC-SHA256 signing. However, there is no validation to ensure the secret key meets minimum entropy requirements. - Action: Add validation to ensure the secret key is sufficiently long (e.g., at least 32 bytes) and random to prevent brute-force attacks.
- The
-
Sensitive Key Redaction:
- The
CredentialRedactorclass uses a hardcoded regex (SENSITIVE_KEY_PATTERN) to identify sensitive keys. This approach may miss certain key patterns or result in false positives. - Action: Allow users to extend or override the
SENSITIVE_KEY_PATTERNto better match their use cases.
- The
🟡 WARNING: Potential Breaking Changes
-
Public API Stability:
- The package is marked as "Public Preview," and the README explicitly states that APIs may change before GA. This could lead to breaking changes for early adopters.
- Action: Clearly document any planned breaking changes in future releases and provide migration guides.
-
Rate Limiter Behavior:
- The
McpSlidingRateLimiterenforces rate limits per agent. However, if the rate limit configuration changes (e.g.,maxRequestsorwindowMs), it could lead to unexpected behavior for existing deployments. - Action: Document the implications of changing rate limit configurations and provide guidelines for safely updating these settings.
- The
💡 Suggestions for Improvement
-
TypeScript Type Safety:
- The
evaluateToolCallmethod inMcpGatewayacceptsparamsasRecord<string, unknown>. This type is too generic and could lead to runtime errors if the structure ofparamsis not as expected. - Suggestion: Define a stricter TypeScript interface for
paramsand validate it at runtime using a library likezodorio-ts.
- The
-
OWASP MCP Cheat Sheet Mapping:
- The README mentions an OWASP MCP Cheat Sheet mapping but does not provide a detailed explanation of how the package aligns with OWASP guidelines.
- Suggestion: Include a dedicated section in the documentation that maps each feature (e.g.,
MCPResponseScanner,MCPSessionAuthenticator) to specific OWASP recommendations.
-
Thread Safety:
- The
CredentialRedactorandMcpGatewayclasses do not appear to use any thread-safe mechanisms. While JavaScript is single-threaded, Node.js environments with worker threads or clustered processes could encounter race conditions. - Suggestion: Document the thread safety assumptions and provide guidance for using these classes in multi-threaded environments.
- The
-
Enhanced Test Coverage:
- While the PR includes tests, there is no explicit mention of coverage for edge cases (e.g., malformed input, regex timeouts, or invalid configurations).
- Suggestion: Add tests for edge cases, including:
- Invalid or malicious regex patterns.
- Extremely large or deeply nested objects in
CredentialRedactor. - Rate limiter behavior under high concurrency.
-
Performance Optimization:
- The
CredentialRedactorprocesses strings and objects recursively. For deeply nested objects, this could lead to performance bottlenecks. - Suggestion: Implement a depth limit or optimize the recursion to handle large objects more efficiently.
- The
-
Durable Storage for Production:
- The README mentions that "production deployments should provide durable implementations for session, nonce, rate-limit, and audit storage." However, no examples or guidelines are provided.
- Suggestion: Include examples or references to durable storage implementations (e.g., Redis, DynamoDB) in the documentation.
-
Security Scanner Enhancements:
- The
MCPSecurityScanneris mentioned in the README but not included in the diff. If it uses regex patterns for scanning, it may also be susceptible to ReDoS attacks. - Suggestion: Apply the same regex validation improvements to the
MCPSecurityScanner.
- The
Summary
This PR introduces a standalone TypeScript package for MCP governance primitives, which is a valuable addition to the project. However, there are critical security issues related to regex validation, error handling, and key management that need to be addressed before merging. Additionally, there are potential breaking changes and areas for improvement in type safety, documentation, and performance.
Recommended Actions Before Merging:
- Address the critical issues related to regex validation, error handling, and key management.
- Document the implications of API changes and rate limiter configurations.
- Add tests for edge cases and performance bottlenecks.
Once these issues are resolved, the package will be a robust and secure addition to the project.
There was a problem hiding this comment.
🤖 AI Agent: code-reviewer
Pull Request Review: feat(typescript): add standalone @microsoft/agentmesh-mcp-governance package
Summary
This PR introduces a new standalone TypeScript package, @microsoft/agentmesh-mcp-governance, which provides MCP governance primitives without requiring the full Agent Mesh SDK. The package includes various security-related utilities such as credential redaction, rate limiting, payload signing, and policy enforcement. The PR also includes configuration files for ESLint, Jest, and TypeScript, as well as a README with usage instructions.
🔴 CRITICAL Issues
-
Regex Denial of Service (ReDoS) Vulnerability in CredentialRedactor
- Issue: The
CredentialRedactorclass uses regular expressions to identify sensitive information in strings. Some of the patterns, such aspem_blockandconnection_string, are vulnerable to ReDoS attacks due to their use of nested quantifiers and unbounded repetition. - Impact: An attacker could craft malicious input that causes these regex patterns to exhibit exponential backtracking, leading to high CPU usage and potential denial of service.
- Recommendation: Use safer regex patterns or implement a timeout mechanism for regex execution. Consider using libraries like
safe-regexto validate patterns for potential ReDoS vulnerabilities.
- Issue: The
-
Potential Credential Exposure in Audit Logs
- Issue: The
McpGatewayclass logs audit entries with redacted parameters. However, if theCredentialRedactorfails to redact sensitive information due to a missed pattern or a failure in the regex scan, credentials could be logged in plaintext. - Impact: This could lead to sensitive information being exposed in audit logs, violating security best practices.
- Recommendation: Add a fail-safe mechanism to ensure that no sensitive information is logged, even if redaction fails. For example, replace all unredacted parameters with a generic placeholder.
- Issue: The
-
Fail-Closed Behavior in
McpGateway- Issue: While the
McpGatewayclass attempts to fail closed in case of errors, there are scenarios where exceptions in thefinalizemethod could lead to incomplete audit logging or metrics recording. - Impact: This could result in incomplete security monitoring and auditing, reducing the effectiveness of the governance system.
- Recommendation: Ensure that all critical operations in the
finalizemethod are wrapped in try-catch blocks and that fallback mechanisms are in place to handle errors gracefully.
- Issue: While the
🟡 WARNING Issues
-
Backward Compatibility
- Issue: The introduction of a new standalone package does not directly affect existing APIs, but any future changes to the MCP primitives in the core SDK could break this package.
- Impact: Changes to the core SDK could inadvertently introduce breaking changes to this package, affecting users who rely on it.
- Recommendation: Clearly document the version compatibility between this package and the core SDK. Consider implementing integration tests to ensure compatibility.
-
Public Preview API Stability
- Issue: The README explicitly states that the package is in "Public Preview" and that APIs may change before GA.
- Impact: Users adopting this package in production environments may face breaking changes in future releases.
- Recommendation: Clearly communicate the stability level of each API and provide a migration guide for any breaking changes.
💡 Suggestions
-
Enhanced Documentation
- The README is well-written but could benefit from additional details:
- Provide a more detailed explanation of each exported primitive, including examples of real-world use cases.
- Include a section on how to implement custom durable storage for production deployments.
- Add a security considerations section to highlight best practices for using the package securely.
- The README is well-written but could benefit from additional details:
-
Test Coverage
- While the PR mentions that tests have been added, it is unclear whether all critical paths (e.g., regex redaction, rate limiting, and approval handling) are thoroughly tested.
- Recommendation: Ensure that the test suite includes edge cases, such as:
- Malicious inputs designed to exploit ReDoS vulnerabilities.
- Scenarios where the
CredentialRedactorfails to redact sensitive information. - Failures in the
finalizemethod ofMcpGateway.
-
TypeScript Typing Improvements
- The
CredentialRedactorclass uses a genericTtype for theredactmethod but does not enforce any constraints onT. - Recommendation: Consider adding constraints to the generic type to ensure type safety. For example:
redact<T extends object | string>(value: T): CredentialRedactionResult<T> { ... }
- The
-
Performance Optimization
- The
CredentialRedactorclass uses a naive approach to iterate over patterns and match them against the input. This could lead to performance bottlenecks for large inputs or a large number of patterns. - Recommendation: Explore more efficient algorithms or libraries for pattern matching and redaction, such as using a single combined regex or a trie-based approach.
- The
-
Thread Safety
- The
McpGatewayclass uses shared instances ofCredentialRedactorandMcpSlidingRateLimiter. If these classes are not thread-safe, concurrent access could lead to race conditions. - Recommendation: Ensure that all shared resources are thread-safe. If necessary, use synchronization mechanisms or create separate instances for each thread.
- The
-
OWASP Agentic Top 10 Compliance
- The package addresses several OWASP Agentic Top 10 risks, such as improper access control and insufficient logging. However, it is unclear if all risks are fully mitigated.
- Recommendation: Perform a comprehensive review of the package against the OWASP Agentic Top 10 to identify and address any gaps.
Summary of Actions
🔴 CRITICAL
- Address potential ReDoS vulnerabilities in regex patterns.
- Implement a fail-safe mechanism to prevent credential exposure in audit logs.
- Ensure robust fail-closed behavior in the
McpGatewayclass.
🟡 WARNING
- Document version compatibility and provide a migration guide for future changes.
- Clearly communicate the stability level of the API during the public preview phase.
💡 SUGGESTIONS
- Enhance the README with more detailed documentation and security considerations.
- Expand test coverage to include edge cases and failure scenarios.
- Improve TypeScript type safety for the
CredentialRedactorclass. - Optimize the performance of the
CredentialRedactorclass. - Ensure thread safety for shared resources in the
McpGatewayclass. - Conduct a full review for OWASP Agentic Top 10 compliance.
Let me know if you need further clarification or assistance!
c6d3766 to
f62798d
Compare
There was a problem hiding this comment.
🤖 AI Agent: code-reviewer
Pull Request Review: feat(typescript): add standalone @microsoft/agentmesh-mcp-governance package
Summary
This PR introduces a standalone @microsoft/agentmesh-mcp-governance npm package, which provides MCP governance primitives without requiring the full Agent Mesh SDK. The package includes features like credential redaction, rate limiting, HMAC signing, and gateway enforcement. Below is a detailed review of the changes.
🔴 CRITICAL
1. Potential Regex Denial of Service (ReDoS) in CredentialRedactor
- Issue: The
CredentialRedactorclass uses regex patterns for credential redaction, including user-defined patterns. WhilevalidateRegexis called to validate patterns, the implementation ofvalidateRegexis not provided in the diff. If this function does not adequately check for catastrophic backtracking or excessive backtracking, the regex patterns could be exploited for a ReDoS attack. - Impact: An attacker could craft malicious input that causes the regex engine to consume excessive CPU resources, leading to a denial-of-service condition.
- Recommendation: Ensure that
validateRegexincludes checks for catastrophic backtracking and enforces a reasonable complexity limit on user-provided regex patterns. Additionally, consider using a library likesafe-regexto validate regex patterns.
2. Fail-Closed Behavior in McpGateway
- Issue: The
McpGatewayclass has a fail-closed mechanism in case of errors during policy evaluation or finalization. While this is a good practice, the error messages returned in such cases (e.g., "Internal error - access denied (fail-closed)") may leak implementation details or provide attackers with hints about the system's behavior. - Impact: Leaking internal error details can aid attackers in crafting targeted attacks.
- Recommendation: Replace error messages with generic ones that do not reveal internal details. For example, "An error occurred. Access denied." Additionally, ensure that all sensitive information is redacted from error messages.
🟡 WARNING
1. Backward Compatibility
- Issue: The PR introduces a new package,
@microsoft/agentmesh-mcp-governance, which re-exports MCP primitives from the core TypeScript SDK. While this is a new package and does not directly modify existing ones, any future changes to the core SDK could inadvertently break this package if not carefully managed. - Impact: Changes to the core SDK could break the standalone package, leading to potential runtime errors for users.
- Recommendation: Implement a robust versioning strategy and add automated tests to ensure compatibility between the core SDK and the standalone package. Consider using semantic versioning to communicate breaking changes clearly.
💡 SUGGESTIONS
1. Improved Documentation
- Observation: The README for the new package is comprehensive, but it could benefit from additional details on the following:
- Examples of how to implement custom durable storage for production deployments.
- A more detailed explanation of the
approvalHandlerfunction and its expected behavior. - A section on security best practices for using the package, such as securely managing secrets and configuring rate limits.
- Recommendation: Expand the documentation to include the above details, as they are critical for secure and effective usage of the package.
2. Thread Safety in McpGateway
- Observation: The
McpGatewayclass uses shared instances ofCredentialRedactor,McpSlidingRateLimiter, and other components. While these components appear to be stateless or thread-safe, this assumption should be explicitly verified. - Recommendation: Review the implementation of shared components like
CredentialRedactorandMcpSlidingRateLimiterto ensure they are thread-safe. If they are not, consider adding synchronization mechanisms or creating separate instances for each thread.
3. Testing Coverage
- Observation: The PR includes a Jest configuration for testing, but the diff does not include any test cases for the new package.
- Recommendation: Add comprehensive test cases for the new package, covering:
- All public APIs.
- Edge cases for credential redaction (e.g., nested objects, large inputs, and user-defined regex patterns).
- Fail-closed behavior in
McpGateway. - Rate-limiting logic.
- Approval handler scenarios.
4. HMAC Key Management
- Observation: The
MCPMessageSigneruses a secret key for HMAC-SHA256 signing. However, there is no mention of how this key should be securely managed or rotated. - Recommendation: Provide guidance in the documentation on securely managing and rotating the HMAC key. Consider integrating with a secure key management solution (e.g., Azure Key Vault).
5. Default Configuration for McpGateway
- Observation: The
McpGatewayconstructor allows for extensive customization, but the default configuration may not be secure for production use (e.g., no rate limiter, no blocked patterns). - Recommendation: Provide a secure default configuration that includes reasonable rate limits, a set of blocked patterns, and a fail-closed policy evaluator.
6. Logging and Metrics
- Observation: The
McpGatewayclass uses aloggerandmetricsobject for logging and telemetry. However, the implementation of these components is not included in the diff. - Recommendation: Ensure that the
loggerandmetricsimplementations are secure and do not log sensitive information. Add tests to verify this behavior.
Summary of Review
This PR introduces a valuable addition to the project by providing a standalone package for MCP governance primitives. However, there are critical security concerns related to regex validation and error handling that must be addressed before merging. Additionally, there are opportunities to improve documentation, testing, and default configurations to enhance the package's usability and security.
Action Items
-
Address Critical Issues:
- Ensure regex patterns are safe from ReDoS attacks.
- Replace error messages with generic ones to avoid leaking internal details.
-
Resolve Warnings:
- Implement a robust versioning strategy to ensure backward compatibility.
-
Implement Suggestions:
- Expand documentation with security best practices and implementation details.
- Add comprehensive test cases for all new functionality.
- Review thread safety of shared components.
- Provide guidance on HMAC key management.
- Define a secure default configuration for
McpGateway.
Once these issues are addressed, the PR will be ready for merging.
|
CI Status Summary ✅ All CI checks pass except:
Standalone |
f62798d to
e797d1f
Compare
There was a problem hiding this comment.
🤖 AI Agent: code-reviewer
Code Review for @microsoft/agentmesh-mcp-governance Package
General Observations
This PR introduces a standalone TypeScript package, @microsoft/agentmesh-mcp-governance, which provides MCP governance primitives without requiring the full Agent Mesh SDK. The package includes utilities for authentication, signing, redaction, scanning, rate limiting, and gateway enforcement. The implementation appears well-structured, with a focus on modularity and extensibility.
🔴 CRITICAL
-
Regex Denial of Service (ReDoS) Vulnerability:
- The
CredentialRedactorandMcpGatewayclasses use regular expressions for pattern matching (e.g.,BUILTIN_PATTERNSandBUILTIN_DANGEROUS_PATTERNS). Some of these patterns (e.g.,pem_block,connection_string,dangerous_pattern) are complex and could be susceptible to ReDoS attacks if they are applied to large or maliciously crafted inputs.- Recommendation: Use libraries like safe-regex to validate regex patterns for safety. Additionally, consider limiting the size of inputs processed by these regexes to mitigate potential performance issues.
- The
-
Fail-Closed Behavior in
McpGateway:- While the
McpGatewayclass generally adheres to fail-closed principles, thesafeRedactParamsmethod catches errors during redaction and returns an empty object ({}) as a fallback. This could lead to a loss of critical information in the audit logs.- Recommendation: Instead of returning an empty object, log the error and include a sanitized version of the parameters (e.g., with sensitive fields redacted) to ensure audit completeness.
- While the
-
Credential Leakage in Audit Logs:
- The
auditSinkimplementation logs redacted parameters, but there is no guarantee that customauditSinkimplementations provided by users will handle sensitive data securely.- Recommendation: Document the importance of redacting sensitive data before passing it to the
auditSink. Consider providing a default secure implementation or validating user-providedauditSinkimplementations to ensure compliance.
- Recommendation: Document the importance of redacting sensitive data before passing it to the
- The
-
Insufficient Protection Against Replay Attacks:
- The
MCPMessageSignerclass (referenced in the README) mentions HMAC-SHA256 payload signing with timestamp and nonce replay protection. However, the implementation of nonce storage and validation is not included in this PR.- Recommendation: Ensure that nonce storage and validation are implemented securely, with a mechanism to prevent replay attacks. This is critical for maintaining the integrity of signed messages.
- The
-
Shell Expansion Detection:
- The
hasShellExpansionfunction only checks for the presence of$(and). This simplistic approach may result in false negatives or positives.- Recommendation: Use a more robust method to detect shell expansion patterns, such as leveraging a shell parser or a more comprehensive regex.
- The
🟡 WARNING
-
Potential Breaking Changes:
- The package is introduced as a standalone module with no dependencies on the full Agent Mesh SDK. While this is a non-breaking change for existing users of the SDK, it introduces a new package that may require separate maintenance and versioning. Ensure that this is clearly communicated to users, especially if there are plans to deprecate overlapping functionality in the main SDK.
-
API Stability:
- The README explicitly states that this package is in "Public Preview" and that APIs may change before GA. This could lead to breaking changes for early adopters.
- Recommendation: Consider versioning the package as
0.x.xto signal that it is not yet stable, or use a clear versioning strategy to indicate breaking changes.
- Recommendation: Consider versioning the package as
- The README explicitly states that this package is in "Public Preview" and that APIs may change before GA. This could lead to breaking changes for early adopters.
💡 SUGGESTIONS
-
Improved Documentation:
- The README provides a good overview of the package, but it could benefit from additional details:
- Add a section on "Security Best Practices" to guide users on securely configuring and using the package.
- Provide examples of how to implement custom
auditSinkandrateLimiterto ensure compliance with security best practices. - Include a section on how to handle errors and exceptions, especially in fail-closed scenarios.
- The README provides a good overview of the package, but it could benefit from additional details:
-
Rate Limiter Extensibility:
- The
McpSlidingRateLimiteris used as the default rate limiter. While this is a reasonable choice, the package could benefit from additional rate-limiting strategies (e.g., token bucket, fixed window).- Recommendation: Consider making the rate limiter interface more extensible to allow users to plug in custom implementations.
- The
-
Test Coverage:
- The PR includes a
jest.config.jsfile and mentions test coverage, but the actual test cases are not included in the diff.- Recommendation: Ensure comprehensive test coverage for all critical components, especially the
CredentialRedactorandMcpGatewayclasses. Include tests for edge cases, such as large inputs, malformed data, and failure scenarios.
- Recommendation: Ensure comprehensive test coverage for all critical components, especially the
- The PR includes a
-
TypeScript Typing:
- The TypeScript typings appear robust, but some areas could benefit from stricter type definitions:
- The
paramsargument inevaluateToolCallis typed asRecord<string, unknown>. Consider using a stricter type or a generic type parameter to enforce structure. - The
McpGatewayConfigtype could include stricter definitions forpolicyEvaluator,approvalHandler, andauditSinkto ensure type safety.
- The
- The TypeScript typings appear robust, but some areas could benefit from stricter type definitions:
-
Performance Optimization:
- The
redactNodemethod inCredentialRedactoruses recursion to traverse nested objects. This could lead to stack overflow errors for deeply nested structures.- Recommendation: Replace recursion with an iterative approach using a stack or queue to handle deeply nested structures more robustly.
- The
-
Default Configuration:
- The
McpGatewayclass uses default configurations for several components (e.g.,rateLimiter,metrics). While this is convenient, it may lead to unexpected behavior if users are unaware of the defaults.- Recommendation: Document the default configurations in the README and provide examples of how to override them.
- The
-
Error Handling:
- The
McpGatewayclass usesgetSafeErrorMessageto sanitize error messages. However, the implementation ofgetSafeErrorMessageis not included in the diff.- Recommendation: Review the implementation of
getSafeErrorMessageto ensure it does not inadvertently expose sensitive information.
- Recommendation: Review the implementation of
- The
-
Backward Compatibility:
- While this package is standalone, it re-exports MCP primitives from the core TypeScript SDK. Ensure that any changes to the core SDK do not inadvertently break this package.
- Recommendation: Add integration tests to verify compatibility between this package and the core SDK.
- While this package is standalone, it re-exports MCP primitives from the core TypeScript SDK. Ensure that any changes to the core SDK do not inadvertently break this package.
Summary
This PR introduces a valuable addition to the Agent Mesh ecosystem, providing standalone MCP governance primitives. While the implementation is generally robust, there are critical security concerns (e.g., ReDoS vulnerabilities, replay attack protection) and areas for improvement (e.g., documentation, test coverage, type safety). Addressing these issues will enhance the security, reliability, and usability of the package.
There was a problem hiding this comment.
🤖 AI Agent: code-reviewer
Pull Request Review for feat(typescript): add standalone @microsoft/agentmesh-mcp-governance package
🔴 CRITICAL
-
Regex Denial of Service (ReDoS) Vulnerability in CredentialRedactor
- The
CredentialRedactorclass uses regular expressions to match sensitive data patterns. Some of these patterns, such asSENSITIVE_KEY_PATTERNandBUILTIN_PATTERNS, may be vulnerable to ReDoS attacks if they are applied to large or maliciously crafted inputs. For example:const SENSITIVE_KEY_PATTERN = /(password|passwd|pwd|secret|token|api[_-]?key|connection.?string|accountkey|sharedaccesssignature|sas)/i;
- This pattern could potentially be exploited with inputs designed to cause catastrophic backtracking.
- Action: Use safe regex libraries like safe-regex to validate these patterns. Alternatively, consider using a library like re2 for regex matching, which avoids catastrophic backtracking.
- The
-
Fail-Closed Behavior in
McpGateway- The
evaluateToolCallmethod inMcpGatewayhas a fail-closed mechanism for errors during evaluation. While this is a good practice, the error handling infinalizedoes not log the error details or provide sufficient information for debugging:debugSecurityFailure(this.config.logger, 'gateway.finalize', error);
- If
debugSecurityFailuredoes not log the full stack trace or error details, it could make debugging difficult.
- If
- Action: Ensure that
debugSecurityFailurelogs the full error stack trace and relevant context for easier debugging.
- The
-
Potential Bypass of
McpGatewayPolicies- The
evaluateToolCallmethod relies on apolicyEvaluatorfunction to make policy decisions. However, the implementation does not validate thepolicyEvaluator's output beyond checking if it equals'deny'or'review'. If thepolicyEvaluatorreturns an unexpected value, it could lead to undefined behavior.const policyDecision = await this.config.policyEvaluator?.evaluate(toolName, { agentId, ...params, }); if (policyDecision === 'deny') { ... }
- Action: Validate the output of
policyEvaluatorto ensure it only returns expected values ('allow','deny', or'review'). Log a warning or error if an unexpected value is returned.
- The
-
Insufficient Validation of
blockedPatterns- The
blockedPatternsarray inMcpGatewayConfigaccepts both strings and regular expressions. However, thecheckSanitizationmethod does not validate string patterns, which could lead to unexpected behavior or security issues.if ((typeof pattern === 'string' && serialized.includes(pattern)) || (pattern instanceof RegExp && hasMatch(pattern, serialized))) {
- Action: Validate string patterns in
blockedPatternsto ensure they are safe and do not introduce vulnerabilities.
- The
🟡 WARNING
- Backward Compatibility
- This PR introduces a new package,
@microsoft/agentmesh-mcp-governance, which re-exports MCP primitives from the core TypeScript SDK. While this is a new package and does not directly modify existing functionality, any changes to the core SDK's MCP primitives could inadvertently break this package. - Action: Ensure that changes to the core SDK are thoroughly tested against this new package to maintain backward compatibility.
- This PR introduces a new package,
💡 SUGGESTIONS
-
Documentation Improvements
- The README provides a good overview of the package, but it could benefit from additional details:
- Add a section on how to configure the
McpGatewayclass, including examples ofMcpGatewayConfigoptions likepolicyEvaluator,rateLimiter, andapprovalHandler. - Provide more detailed examples of how to use each exported primitive (e.g.,
CredentialRedactor,MCPMessageSigner).
- Add a section on how to configure the
- The README provides a good overview of the package, but it could benefit from additional details:
-
Unit Test Coverage
- While the PR mentions that tests have been added, the provided diff does not include test files. It is crucial to ensure that all critical components, such as
CredentialRedactorandMcpGateway, are thoroughly tested for edge cases, especially for security-related functionality. - Action: Add unit tests for:
- Edge cases in regular expression matching (e.g., large inputs, malicious patterns).
- Fail-closed behavior in
evaluateToolCallandfinalize. - Validation of
policyEvaluatoroutput. - Handling of
blockedPatternswith both strings and regular expressions.
- While the PR mentions that tests have been added, the provided diff does not include test files. It is crucial to ensure that all critical components, such as
-
Thread Safety
- The
McpGatewayclass uses shared resources likerateLimiterandauditSink. If these resources are not thread-safe, it could lead to race conditions in concurrent environments. - Action: Ensure that shared resources like
rateLimiterandauditSinkare thread-safe. If they are not, consider using locks or other synchronization mechanisms.
- The
-
Type Safety
- The
safeRedactParamsmethod casts the result ofthis.redactor.redact(params).redactedtoRecord<string, unknown>without verifying its type.return this.redactor.redact(params).redacted as Record<string, unknown>;
- Action: Add runtime type checks to ensure that
redactedis a validRecord<string, unknown>before casting.
- The
-
Default Configuration Values
- The
McpGatewayconstructor sets default values for some configuration options (e.g.,deniedTools,allowedTools,sensitiveTools). However, these defaults may not align with the security requirements of all use cases. - Action: Consider providing more secure defaults, such as an empty
allowedToolslist and a non-emptydeniedToolslist, to encourage fail-closed behavior by default.
- The
-
Rate Limiter Configuration
- The
McpSlidingRateLimiteris instantiated with a default configuration ifrateLimitis provided. However, there is no validation of therateLimitconfiguration. - Action: Validate the
rateLimitconfiguration to ensure it has reasonable values (e.g.,maxRequests > 0,windowMs > 0).
- The
Summary
This PR introduces a standalone @microsoft/agentmesh-mcp-governance package with useful MCP governance primitives. While the implementation is generally solid, there are critical security issues related to regular expression usage, policy evaluation, and parameter sanitization. Addressing these issues and incorporating the suggested improvements will enhance the security, reliability, and usability of the package.
…package Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…alse positive Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
f203047 to
769df4a
Compare
There was a problem hiding this comment.
🤖 AI Agent: code-reviewer
Pull Request Review for @microsoft/agentmesh-mcp-governance
🔴 CRITICAL: Security Issues
-
Regex Denial of Service (ReDoS) Vulnerability
- File:
credential-redactor.ts,gateway.ts - Issue: The use of unbounded regular expressions (e.g.,
SENSITIVE_KEY_PATTERN,BUILTIN_PATTERNS, andBUILTIN_DANGEROUS_PATTERNS) introduces a potential ReDoS vulnerability. If an attacker crafts malicious input that causes excessive backtracking, it could lead to performance degradation or denial of service. - Recommendation: Use safe regex patterns or libraries like
safe-regexto validate patterns. Alternatively, consider limiting the input size or using non-backtracking regex engines.
- File:
-
Shell Injection Risk
- File:
gateway.ts - Issue: The
hasShellExpansionfunction checks for shell expansion patterns like$(...). However, this is insufficient to prevent command injection attacks. For example, backticks (`) are also used for shell commands in some contexts. - Recommendation: Extend the
hasShellExpansionfunction to detect additional shell injection patterns, such as backticks and other command execution syntax. Consider using a library likeshell-escapeto sanitize inputs.
- File:
-
Fail-Closed Behavior
- File:
gateway.ts - Issue: While the
evaluateToolCallmethod attempts to fail closed on errors, thefinalizemethod also contains error handling that defaults to denial. This redundancy could lead to inconsistent behavior if thefinalizemethod fails. - Recommendation: Ensure that the fail-closed behavior is consistent across all error handling paths. Consider centralizing error handling logic to avoid discrepancies.
- File:
-
HMAC-SHA256 Key Management
- File:
README.md,gateway.ts - Issue: The
MCPMessageSignerandMCPSessionAuthenticatorrely on secrets (MCP_SESSION_SECRETandMCP_SIGNING_SECRET) provided via environment variables. However, there is no mention of secure key rotation or storage mechanisms. - Recommendation: Document secure key management practices, including rotation, storage in a secure vault (e.g., Azure Key Vault), and periodic audits.
- File:
🟡 WARNING: Potential Breaking Changes
- API Stability
- File:
README.md,package.json - Issue: The package is marked as "Public Preview," and the APIs may change before GA. This could lead to breaking changes for early adopters.
- Recommendation: Clearly communicate the API stability status in the documentation and consider versioning strategies (e.g., semantic versioning) to minimize disruption.
- File:
💡 Suggestions for Improvement
-
Type Safety
- File:
credential-redactor.ts,gateway.ts - Observation: The use of
unknownandanytypes in methods likeredactNodeandsafeRedactParamsreduces type safety. - Recommendation: Use stricter TypeScript types or generics to enforce type safety. For example, replace
unknownwith a well-defined interface or type.
- File:
-
Performance Optimization
- File:
credential-redactor.ts - Observation: The
redactNodemethod uses recursive traversal for nested objects, which could lead to stack overflow for deeply nested structures. - Recommendation: Use an iterative approach or limit the recursion depth to prevent stack overflow.
- File:
-
Logging and Auditing
- File:
gateway.ts - Observation: The
finalizemethod logs audit entries but does not include a mechanism for log rotation or retention. - Recommendation: Implement log rotation and retention policies to comply with security best practices and avoid excessive log growth.
- File:
-
Documentation
- File:
README.md - Observation: The README provides a good overview but lacks detailed examples for advanced use cases like custom patterns or durable storage implementations.
- Recommendation: Add examples for advanced configurations and integration with production-grade storage systems.
- File:
-
Testing Coverage
- File:
jest.config.js - Observation: The
collectCoverageFromconfiguration only includessrc/**/*.ts. This may exclude utility files or edge cases. - Recommendation: Ensure comprehensive test coverage, including edge cases and error handling paths.
- File:
Summary
This pull request introduces a standalone @microsoft/agentmesh-mcp-governance package with a focus on MCP governance primitives. While the implementation is robust and adheres to many best practices, there are critical security issues (e.g., ReDoS, shell injection) and areas for improvement (e.g., type safety, documentation). Addressing these concerns will enhance the package's security, reliability, and usability.
- Critical Issues: 4
- Warnings: 1
- Suggestions: 5
Please address the 🔴 CRITICAL issues before merging.
|
Thank you for this work, @jackbatzner — the MCP governance implementation across multiple languages is impressive and clearly well-thought-out. After reviewing the full series, we've decided to take a hybrid approach (Decision C): keep MCP core logic in agent-os/agent-mesh and provide thin standalone re-export packages for enterprise repackaging — similar to what we shipped in PR #910 for Python. We're closing this PR because we'll implement the TypeScript standalone package in-house following this architecture. Your work directly informed our approach — the OWASP mapping methodology, the gateway/rate-limiter/session-auth pattern, and the trust proxy design all carry forward. We're keeping your documentation PRs open for review as they add independent value. Thank you for the sustained contribution effort across the entire MCP governance surface. |
Description
Introduces the standalone
@microsoft/agentmesh-mcp-governancenpm package with zero AGT dependency. This allows MCP server operators to adopt MCP governance primitives without pulling in the full Agent Mesh SDK.Package:
@microsoft/agentmesh-mcp-governancedependencies: {}— no AGT dependencyType of Change
Package(s) Affected
Checklist
Related Issues
Supersedes #791 (split for easier review). Merge after #791.