feat: add policy YAML validation command to CLI#665
feat: add policy YAML validation command to CLI#665kanish5 wants to merge 2 commits intomicrosoft:mainfrom
Conversation
|
Welcome to the Agent Governance Toolkit! Thanks for your first pull request. |
🤖 AI Agent: breaking-change-detector — Summary🔍 API Compatibility ReportSummaryThis pull request introduces new CLI commands and enhances existing functionality for policy validation and governance. It includes additive changes, such as new public APIs, helper functions, and CLI subcommands. No breaking changes were detected in the diff. Findings
Migration GuideNo migration steps are necessary as no breaking changes were identified. Conclusion✅ No breaking changes detected. This pull request is safe to merge from an API compatibility perspective. |
🤖 AI Agent: docs-sync-checker — Issues Found📝 Documentation Sync ReportIssues Found
Suggestions
Additional Notes
Please address the above issues and suggestions to ensure the documentation is fully in sync with the code changes. |
There was a problem hiding this comment.
🤖 AI Agent: code-reviewer
Review Summary
This pull request introduces a new CLI command, agentos policy validate, which validates YAML policy files against a JSON schema and performs Pydantic validation. It also includes new interactive Jupyter notebooks for demonstrating policy enforcement, MCP security, and multi-agent governance. While the PR introduces valuable features, there are some critical security concerns, potential breaking changes, and areas for improvement.
🔴 CRITICAL
-
Insufficient Validation of YAML Input
- Issue: The
_validate_yaml_with_line_numbers()function does not sanitize or validate the YAML input for potential malicious payloads. YAML files can include arbitrary Python objects, which could lead to code execution vulnerabilities. - Impact: This could allow an attacker to execute arbitrary code by injecting malicious payloads into the YAML file.
- Recommendation: Use
yaml.safe_load()instead ofyaml.load()to prevent the execution of arbitrary code. Ifsafe_load()is already being used, ensure that it is explicitly mentioned in the code or documentation.
- Issue: The
-
Potential Bypass of JSON Schema Validation
- Issue: The JSON schema validation is described as "best-effort," which implies that it might not catch all invalid inputs. This could lead to security vulnerabilities if the policy engine relies on the validity of the schema for enforcement.
- Impact: A malicious or malformed policy file could bypass validation and introduce vulnerabilities into the system.
- Recommendation: Ensure that the JSON schema is comprehensive and rigorously tested. Consider adding unit tests to validate edge cases and ensure the schema is robust.
-
Regex Injection in MCP Security Scanner
- Issue: The
scan_tool_definitionfunction uses user-provided input (description) directly in regex operations. If the regex patterns are not properly sanitized, this could lead to regex injection vulnerabilities. - Impact: An attacker could craft a malicious tool description to exploit the regex engine, potentially causing denial of service (ReDoS) or other unexpected behavior.
- Recommendation: Use a library like
re.escape()to sanitize user-provided input before using it in regex operations.
- Issue: The
-
Lack of Thread Safety in Circuit Breaker Implementation
- Issue: The
simulate_callandcheck_circuit_breakerfunctions modify sharedAgentMetricsobjects without any synchronization mechanisms. This could lead to race conditions in a multi-threaded environment. - Impact: Inconsistent or incorrect metrics could lead to incorrect circuit breaker behavior, potentially allowing agents to operate in unsafe conditions.
- Recommendation: Use thread-safe data structures or synchronization primitives (e.g.,
threading.Lock) to ensure thread safety when modifying shared state.
- Issue: The
🟡 WARNING
- Backward Compatibility
- Issue: The addition of the
agentos policysubcommand introduces new functionality to the CLI. While this is not a breaking change, it is important to ensure that existing commands and workflows are not affected. - Recommendation: Verify that the new subcommand does not interfere with existing CLI commands. Add tests to ensure backward compatibility.
- Issue: The addition of the
💡 SUGGESTIONS
-
Error Reporting
- Observation: The error reporting for the
agentos policy validatecommand is functional but could be improved for user experience. - Suggestion: Consider adding color-coded output (e.g., using the
coloramalibrary) to make errors and warnings more visually distinct. Additionally, provide suggestions for fixing common errors when possible.
- Observation: The error reporting for the
-
Documentation
- Observation: The new CLI commands and Jupyter notebooks are valuable additions, but the documentation does not appear to have been updated to reflect these changes.
- Suggestion: Update the README and any relevant documentation to include details about the new CLI commands and how to use the Jupyter notebooks.
-
Testing
- Observation: While the PR mentions that the
agentos policy validatecommand is "CI-friendly," there is no evidence of automated tests for this functionality. - Suggestion: Add unit tests and integration tests for the new CLI commands, especially for edge cases in YAML parsing, JSON schema validation, and Pydantic validation.
- Observation: While the PR mentions that the
-
Notebook Security
- Observation: The Jupyter notebooks include code that simulates potentially dangerous operations (e.g.,
exec('/bin/sh')in the MCP Security Proxy notebook). - Suggestion: Add clear warnings in the notebook headers to indicate that the code is for educational purposes only and should not be used in production environments without proper safeguards.
- Observation: The Jupyter notebooks include code that simulates potentially dangerous operations (e.g.,
-
Code Quality
- Observation: The
_validate_yaml_with_line_numbers()and_load_json_schema()helper functions are not documented. - Suggestion: Add docstrings to these functions to improve code readability and maintainability.
- Observation: The
-
Error Handling
- Observation: The
agentos policy validatecommand exits with a non-zero code on failure, but the specific exit codes are not documented. - Suggestion: Document the possible exit codes and their meanings in the CLI help text or documentation.
- Observation: The
Conclusion
This PR introduces useful features and enhancements to the agentos CLI and the Jupyter notebooks. However, there are critical security issues that must be addressed before merging. Additionally, improvements to documentation, testing, and error reporting would enhance the overall quality of the contribution.
🤖 AI Agent: test-generator — `packages/agent-os/src/agent_os/cli/__init__.py`🧪 Test Coverage Analysis
|
🤖 AI Agent: security-scanner — Security Review of PR: `feat: add policy YAML validation command to CLI`Security Review of PR:
|
| Category | Severity | Description |
|---|---|---|
| Prompt Injection Defense Bypass | 🔴 CRITICAL | No sanitization of YAML content for prompt injection risks. |
| Policy Engine Circumvention | 🟠 HIGH | Validation bypass possible if jsonschema is not installed. |
| Trust Chain Weaknesses | 🟠 HIGH | No integrity verification for policy_schema.json. |
| Credential Exposure | 🟠 HIGH | Potential exposure of sensitive data in error logs. |
| Sandbox Escape | 🔴 CRITICAL | No isolation for YAML validation process. |
| Deserialization Attacks | 🟡 MEDIUM | Lack of explicit checks for unexpected data types or structures. |
| Race Conditions | 🟡 MEDIUM | TOCTOU vulnerability in _load_json_schema. |
| Supply Chain Risks | 🟡 MEDIUM | Dependency on jsonschema introduces potential supply chain risks. |
Actionable Recommendations
-
Prompt Injection Defense:
- Sanitize and validate YAML content for potentially malicious strings.
- Use a prompt sanitization library to detect injection risks.
-
Policy Engine Hardening:
- Make
jsonschemaa mandatory dependency or raise an error if unavailable. - Enforce strict validation against the JSON schema.
- Make
-
Trust Chain Improvements:
- Verify the integrity of
policy_schema.jsonusing a cryptographic hash. - Consider embedding the schema directly in the codebase.
- Verify the integrity of
-
Credential Protection:
- Mask sensitive fields in error messages and logs.
- Provide an option to suppress detailed error output in CI/CD environments.
-
Sandboxing:
- Ensure the YAML validation process is isolated from the rest of the system.
- Use secure YAML parsers and avoid deserialization of arbitrary objects.
-
Race Condition Mitigation:
- Cache the schema file in memory at application startup.
- Use file locking to prevent concurrent modifications.
-
Supply Chain Security:
- Pin dependencies to specific versions.
- Use dependency scanning tools to monitor for vulnerabilities.
Overall Assessment
This PR introduces a valuable feature for validating policy files but also introduces critical security risks that need to be addressed before merging. The most pressing issues are the potential for prompt injection, policy circumvention, and sandbox escape. These must be resolved to ensure the integrity and security of the agentos toolkit.
|
@microsoft-github-policy-service agree |
059e587 to
415c4af
Compare
There was a problem hiding this comment.
🤖 AI Agent: code-reviewer
Feedback on the Pull Request
🔴 CRITICAL Issues
-
Improper Handling of
jsonschemaImport Failure:- In
_validate_yaml_with_line_numbers, thejsonschemalibrary is imported inside the function, and if the import fails, the code silently skips JSON Schema validation. This could lead to undetected policy violations if the library is not installed. - Recommendation: Make
jsonschemaa required dependency in thesetup.pyorpyproject.tomlfile. If this is not feasible, explicitly log an error and exit with a non-zero status if the library is not installed.
- In
-
Insufficient Validation of Policy Schema:
- The
_validate_yaml_with_line_numbersfunction performs some basic checks (e.g., required fields, rule structure), but these checks are not comprehensive. For example:- The
rulesfield is only checked for being a list, but the individual rule objects are not fully validated against the schema. - The
actionfield is validated against a hardcoded list of valid actions, but this list may not stay in sync with the JSON Schema.
- The
- Recommendation: Use
jsonschemaexclusively for schema validation and avoid duplicating validation logic in_validate_yaml_with_line_numbers. This ensures consistency and reduces the risk of discrepancies between the schema and the code.
- The
-
Potential for Sandbox Escape in YAML Parsing:
- The
yaml.safe_loadfunction is used for parsing YAML files. While this is safer thanyaml.load, it is still vulnerable to certain attacks, such as those exploiting YAML tags. - Recommendation: Use a library like
ruamel.yamlor a stricter YAML parser to avoid potential sandbox escape vulnerabilities.
- The
-
Error Reporting for YAML Parsing:
- The error messages for YAML parsing issues are not sanitized. If an attacker provides a malicious YAML file, the error message could potentially leak sensitive information or be used for injection attacks.
- Recommendation: Sanitize error messages before displaying them to the user.
🟡 WARNING Issues
-
Backward Compatibility Risk:
- The addition of the
agentos policysubcommand introduces a new CLI structure. If users were relying on the existingvalidatecommand, this change could break their workflows. - Recommendation: Provide a deprecation warning for the old
validatecommand and ensure that it continues to work for a transitional period.
- The addition of the
-
Potential Breaking Changes in Policy Validation:
- The new validation logic introduces stricter checks (e.g., required fields, rule structure). Policies that previously passed validation may now fail.
- Recommendation: Clearly document these changes in the release notes and provide a migration guide for users to update their policies.
💡 Suggestions for Improvement
-
Improve Error Messages:
- The error messages in
_validate_yaml_with_line_numbersare functional but could be more user-friendly. For example, instead ofrules[2] -> action, consider using a more descriptive format likeRule 2: Invalid action field. - Recommendation: Use a consistent and user-friendly format for error messages.
- The error messages in
-
Add Unit Tests for CLI Commands:
- The new
agentos policysubcommands (validate,test,diff) should be thoroughly tested to ensure they work as expected. - Recommendation: Add unit tests for each subcommand, covering both success and failure scenarios.
- The new
-
Thread Safety in Policy Validation:
- The
_validate_yaml_with_line_numbersfunction and related logic do not appear to have any thread safety issues, but this should be explicitly verified if the CLI is expected to handle concurrent requests (e.g., in a CI/CD pipeline). - Recommendation: Add tests to verify thread safety in concurrent execution scenarios.
- The
-
Support for JSON Input:
- The
agentos policy validatecommand currently supports YAML files but does not explicitly mention support for JSON files. - Recommendation: Add explicit support for JSON files and update the documentation accordingly.
- The
-
Improve Documentation:
- The docstrings for the new functions and methods are detailed, but the overall documentation for the
agentos policysubcommands could be improved. - Recommendation: Add examples and usage instructions for the new subcommands in the CLI documentation.
- The docstrings for the new functions and methods are detailed, but the overall documentation for the
-
Use of
pathlibfor File Handling:- The code uses
Path.read_text()andPath.write_text()for file I/O, which is good. However, there are some instances whereopen()is still used (e.g., incmd_validate). - Recommendation: Use
pathlibconsistently for file handling.
- The code uses
-
Consider Adding a Dry-Run Mode:
- For the
agentos policy testandagentos policy diffsubcommands, a dry-run mode could be useful for users who want to preview the results without making changes. - Recommendation: Add a
--dry-runflag to these subcommands.
- For the
-
Logging Improvements:
- The CLI commands use
print()for output, which is fine for user-facing messages but not ideal for logging errors or debugging information. - Recommendation: Use the
loggingmodule for error and debug messages.
- The CLI commands use
Summary of Changes Needed
| Type | Description |
|---|---|
| 🔴 CRITICAL | Ensure jsonschema is a required dependency or handle its absence explicitly. |
| 🔴 CRITICAL | Use jsonschema exclusively for schema validation to avoid duplication. |
| 🔴 CRITICAL | Use a stricter YAML parser to prevent sandbox escape vulnerabilities. |
| 🔴 CRITICAL | Sanitize error messages to prevent information leakage or injection attacks. |
| 🟡 WARNING | Maintain backward compatibility for the validate command. |
| 🟡 WARNING | Document stricter validation rules and provide a migration guide. |
| 💡 SUGGESTION | Improve error messages, documentation, and add unit tests for new commands. |
| 💡 SUGGESTION | Add support for JSON input and a dry-run mode for certain subcommands. |
| 💡 SUGGESTION | Use pathlib consistently and improve logging practices. |
By addressing these issues and suggestions, the new feature will be more robust, secure, and user-friendly.
Closes #529
Summary
Added
agentos policy validate <file>CLI command that:jsonschema(best-effort)PolicyDocumentrules[2] -> action)New commands
agentos policy validate <file>— validate a single policy fileagentos policy test <policy> <scenarios>— run scenario testsagentos policy diff <file1> <file2>— compare two policiesChanges
packages/agent-os/src/agent_os/cli/__init__.py:cmd_validatewith JSON Schema + field location error reporting_validate_yaml_with_line_numbers()helper_load_json_schema()helpercmd_policy()dispatcheragentos policysubcommand in the CLI