docs: add policy-as-code tutorial chapter 4 (conditional policies)#902
Conversation
Chapter 4 teaches two real-world problems: - Environment-aware rules: same agent, different behavior in dev vs prod - Conflict resolution: what happens when two teams write policies that disagree about the same tool (e.g. security blocks send_email, support team allows it) Covers four conflict resolution strategies (deny_overrides, allow_overrides, priority_first_match, most_specific_wins) and the scope hierarchy (GLOBAL → TENANT → ORGANIZATION → AGENT). Includes: - Tutorial doc (04-conditional-policies.md) - Three YAML policy files (env, global, support team) - Runnable Python example (04_conditional_policies.py) - Updated Ch3 "What's missing?" to link forward to Ch4 - Updated README table to link Ch4 and fix "coming soon" range Also normalizes README.md line endings from CRLF to LF to match the rest of the tutorial files. Ref microsoft#706
🤖 AI Agent: security-scanner — Security Analysis of the Pull RequestSecurity Analysis of the Pull RequestThis pull request primarily introduces a new tutorial chapter on conditional policies, conflict resolution strategies, and scope hierarchy for policy governance. While it is documentation-heavy, it includes runnable Python examples and YAML policy files, which must be reviewed for potential security vulnerabilities. Findings1. Prompt Injection Defense BypassRating: 🔵 LOW 2. Policy Engine CircumventionRating: 🟠 HIGH 3. Trust Chain WeaknessesRating: 🔵 LOW 4. Credential ExposureRating: 🔵 LOW 5. Sandbox EscapeRating: 🔵 LOW 6. Deserialization AttacksRating: 🟡 MEDIUM 7. Race ConditionsRating: 🔵 LOW 8. Supply Chain RisksRating: 🟡 MEDIUM Summary of Findings
Recommendations for the Pull Request
By addressing these issues, the security of the toolkit and its downstream users can be significantly improved. |
|
Welcome to the Agent Governance Toolkit! Thanks for your first pull request. |
There was a problem hiding this comment.
🤖 AI Agent: code-reviewer
Review Summary
The pull request introduces Chapter 4 of the "Policy-as-Code" tutorial, focusing on conditional policies and conflict resolution strategies. The tutorial is well-structured, provides clear examples, and includes runnable Python scripts and YAML policy files. The addition of conflict resolution strategies and scope hierarchy is a significant enhancement to the library's documentation. However, there are some areas that require attention to ensure correctness, security, and maintainability.
🔴 CRITICAL: Security Concerns
-
Conflict Resolution Strategy Default Behavior:
- The
PolicyConflictResolverclass allows multiple strategies for resolving conflicts, includingALLOW_OVERRIDES. While this is documented as a business decision, it is inherently risky in security-sensitive contexts. Allowing any policy to override a deny decision could lead to security bypasses, especially if a malicious actor gains control over a lower-scope policy. - Action: Add explicit warnings in the documentation and code comments about the risks of using
ALLOW_OVERRIDES. Consider implementing runtime checks or safeguards to ensure this strategy is not used in sensitive environments.
- The
-
Default Actions in Policies:
- The default action for the
04_env_policy.yamlisdeny, which is safe. However, the default action for the04_global_policy.yamlisallow. This could lead to unintended security bypasses if no rules match. - Action: Recommend changing the default action in
04_global_policy.yamltodenyfor safer defaults.
- The default action for the
-
Environment Field Validation:
- The
environmentfield in the example policies is used for conditional checks but lacks validation. If an agent sends an invalid or unexpected environment value, it could bypass rules. - Action: Ensure that the
environmentfield is validated against a predefined list of acceptable values (e.g.,["development", "staging", "production"]) in thePolicyEvaluator.
- The
🟡 WARNING: Potential Breaking Changes
-
Scope Hierarchy Behavior:
- The introduction of
MOST_SPECIFIC_WINSstrategy and scope hierarchy (GLOBAL → TENANT → ORGANIZATION → AGENT) changes how conflicts are resolved. This could lead to different outcomes for existing policies if users switch to this strategy. - Action: Clearly document this behavior and provide migration guidance for users transitioning from other strategies.
- The introduction of
-
Priority Tie Resolution:
- The
PRIORITY_FIRST_MATCHstrategy resolves ties based on input order. This behavior is fragile and could lead to unexpected results if policies are loaded in a different order. - Action: Consider adding a warning in the documentation about the risks of relying on input order for tie resolution.
- The
💡 Suggestions for Improvement
-
Thread Safety:
- The
PolicyEvaluatorandPolicyConflictResolverclasses should be reviewed for thread safety. If these classes are used in concurrent agent execution scenarios, ensure that they are stateless or implement locking mechanisms. - Action: Add unit tests to verify thread safety in concurrent execution scenarios.
- The
-
OWASP Agentic Top 10 Compliance:
- The tutorial does not explicitly address OWASP Agentic Top 10 risks, such as "Policy Injection" or "Improper Policy Enforcement."
- Action: Add a section to the tutorial that discusses how the library mitigates these risks.
-
Type Safety and Validation:
- The
CandidateDecisionandPolicyScopeclasses should be reviewed for type safety. Ensure that all fields are validated using Pydantic models or equivalent. - Action: Add Pydantic-based validation for these classes and include type hints in the documentation.
- The
-
Backward Compatibility:
- The tutorial introduces new concepts like
PolicyConflictResolverandConflictResolutionStrategy. Ensure that these additions do not break existing functionality. - Action: Add tests to verify backward compatibility with existing policies.
- The tutorial introduces new concepts like
-
Policy Testing Framework:
- The tutorial mentions policy testing in Chapter 6 but does not provide details. Consider adding a basic testing framework for users to validate their policies.
- Action: Provide a simple
pytest-based example for testing policies.
-
Documentation Enhancements:
- The tutorial is comprehensive but could benefit from visual aids (e.g., diagrams for scope hierarchy and conflict resolution strategies).
- Action: Add diagrams to illustrate the scope hierarchy and conflict resolution process.
Conclusion
The pull request is a valuable addition to the repository, enhancing the tutorial's depth and usability. However, addressing the critical security concerns and potential breaking changes is essential before merging. Implementing the suggested improvements will further strengthen the library's reliability and security posture.
There was a problem hiding this comment.
🤖 AI Agent: code-reviewer
Review Summary
This pull request introduces Chapter 4 of the "Policy-as-Code" tutorial, focusing on conditional policies and conflict resolution. The tutorial is well-structured, with clear examples, YAML configurations, and Python code snippets. The addition of conflict resolution strategies and scope hierarchy is a significant enhancement to the policy engine's capabilities. However, there are a few areas where improvements and clarifications are necessary.
🔴 CRITICAL
-
Policy Evaluation and Default Deny Behavior
- The
04_env_policy.yamlfile has adefaultssection withaction: deny. While this is a good security practice, the tutorial does not emphasize the importance of this default deny behavior. Failing to highlight this could lead to users creating policies with unintended permissive defaults, which is a security risk. - Action: Explicitly emphasize the importance of setting a default
denyaction in the tutorial text. Consider adding a warning about the risks of not doing so.
- The
-
Priority Tie-Breaking
- The
PRIORITY_FIRST_MATCHstrategy relies on input order to resolve ties. This behavior is fragile and could lead to unexpected results if policies are dynamically loaded or reordered. This could result in a security bypass if a lower-priority policy is inadvertently evaluated first. - Action: Add a warning in the tutorial about the risks of relying on input order for tie-breaking. Consider recommending that users avoid assigning the same priority to conflicting rules.
- The
-
Environment Field Validation
- The
environmentfield in the YAML policy is critical for conditional policies. However, there is no mention of validation for this field in the tutorial or the provided code snippets. If theenvironmentfield is misspelled or contains unexpected values, the policy may not work as intended, leading to potential security gaps. - Action: Ensure that the
PolicyEvaluatororPolicyDocumentclass validates theenvironmentfield against a predefined set of acceptable values (e.g.,development,staging,production). Update the tutorial to reflect this validation.
- The
🟡 WARNING
- Backward Compatibility
- The introduction of
PolicyConflictResolverandConflictResolutionStrategyis a significant addition to the library. If existing users have implemented custom conflict resolution logic, this change might lead to compatibility issues. - Action: Clearly document any changes to the public API in the release notes. Provide migration guidance for users who may have implemented custom conflict resolution logic.
- The introduction of
💡 SUGGESTIONS
-
Thread Safety
- The tutorial does not address whether the
PolicyEvaluatorandPolicyConflictResolverare thread-safe. This is critical for concurrent agent execution. - Action: Add a note in the tutorial and documentation about the thread safety of these components. If they are not thread-safe, provide guidance on how to use them safely in multi-threaded environments.
- The tutorial does not address whether the
-
Error Handling in Examples
- The Python examples in the tutorial do not include error handling for cases like missing or malformed YAML files.
- Action: Add error handling to the examples to catch and report issues like file not found, invalid YAML syntax, or missing required fields.
-
Conflict Resolution Strategy Defaults
- The tutorial does not specify a default conflict resolution strategy for the
PolicyConflictResolver. This could lead to confusion for users who do not explicitly set a strategy. - Action: Clarify the default conflict resolution strategy in the tutorial and documentation. If no default exists, consider setting one (e.g.,
DENY_OVERRIDESfor safety).
- The tutorial does not specify a default conflict resolution strategy for the
-
YAML Schema Validation
- The tutorial does not mention whether the YAML policy files are validated against a schema before being loaded. This could lead to runtime errors or unexpected behavior if the YAML files are malformed.
- Action: Ensure that the
PolicyDocument.from_yamlmethod includes schema validation. Update the tutorial to explain this validation process.
-
Conflict Resolution Logging
- The tutorial does not mention whether the
PolicyConflictResolverlogs its decision-making process. Logging this information would be valuable for debugging and auditing. - Action: Consider adding logging to the
PolicyConflictResolverto record how conflicts are resolved and which strategy was used.
- The tutorial does not mention whether the
-
Example Output Validation
- The tutorial includes example outputs for the Python scripts, but there is no automated test to verify that the actual output matches the documented output.
- Action: Add automated tests to validate that the example scripts produce the expected output. This will ensure the tutorial remains accurate as the library evolves.
-
Clarify Scope Hierarchy
- The tutorial introduces the scope hierarchy (GLOBAL → TENANT → ORGANIZATION → AGENT) but does not explain how these scopes are assigned in practice.
- Action: Add a section explaining how users can assign scopes to policies and what the default scope is if none is specified.
Final Notes
This is a well-written and valuable addition to the tutorial series. Addressing the critical and suggested issues will not only improve the tutorial's clarity but also enhance the security and usability of the library.
There was a problem hiding this comment.
Pull request overview
Adds Chapter 4 to the Policy-as-Code tutorial, introducing environment-aware conditional rules and explicit conflict resolution between policies authored by different teams.
Changes:
- Added Chapter 4 markdown tutorial covering conditional policies, conflict scenarios, four resolution strategies, and scope specificity.
- Added runnable Chapter 4 Python example plus three YAML policy files (environment, global security, and support team).
- Updated tutorial navigation (README chapter list and Chapter 3 “What’s missing?” forward link to Chapter 4).
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| docs/tutorials/policy-as-code/README.md | Adds Chapter 4 link and updates “coming soon” range. |
| docs/tutorials/policy-as-code/03-rate-limiting.md | Updates “What’s missing?” to point to Chapter 4 with revised framing. |
| docs/tutorials/policy-as-code/04-conditional-policies.md | New Chapter 4 tutorial content: conditional policies + conflict resolution + scopes. |
| docs/tutorials/policy-as-code/examples/04_conditional_policies.py | New runnable example demonstrating environment conditions and conflict resolution strategies. |
| docs/tutorials/policy-as-code/examples/04_env_policy.yaml | New environment-aware policy used by Chapter 4. |
| docs/tutorials/policy-as-code/examples/04_global_policy.yaml | New global security policy for conflict examples. |
| docs/tutorials/policy-as-code/examples/04_support_team_policy.yaml | New team policy that conflicts with global policy for demonstration. |
Use division VP → department manager instead of team lead → department head so the analogy correctly narrows with the GLOBAL → TENANT → ORGANIZATION → AGENT specificity order.
There was a problem hiding this comment.
🤖 AI Agent: code-reviewer
Pull Request Review: docs: add policy-as-code tutorial chapter 4 (conditional policies)
🔴 CRITICAL: Security Concerns
-
Conflict Resolution Strategies:
- The
ALLOW_OVERRIDESstrategy could lead to security bypasses if improperly configured. While this is a business decision, it should be accompanied by clear warnings in the documentation about its risks, especially in environments where security policies are critical (e.g., production). Consider adding a disclaimer in the tutorial emphasizing the potential for abuse or unintended consequences.
- The
-
Default Actions:
- The environment-aware policy (
04_env_policy.yaml) uses a default action ofdeny. While this is generally safe, the tutorial mentions thatstagingfalls through to the default. This could lead to confusion or misconfiguration in real-world scenarios. Ensure that users are explicitly warned about the importance of defining rules for all expected environments.
- The environment-aware policy (
-
Scope Hierarchy:
- The
MOST_SPECIFIC_WINSstrategy relies heavily on scope labels (GLOBAL,TENANT,ORGANIZATION,AGENT). If these labels are misused or misconfigured, it could lead to sandbox escape vectors where less secure policies override broader ones. Consider adding validation checks or warnings in thePolicyConflictResolverto ensure scopes are correctly applied.
- The
🟡 WARNING: Backward Compatibility
-
Conflict Resolution API:
- The introduction of
PolicyConflictResolverand its strategies (DENY_OVERRIDES,ALLOW_OVERRIDES, etc.) may change how existing users interact with the policy engine. If users were previously relying on implicit priority-based resolution, this could lead to unexpected behavior. Ensure backward compatibility by documenting how to migrate existing policies to use the new conflict resolution strategies.
- The introduction of
-
Normalized Line Endings:
- The change from CRLF to LF in
README.mdcould affect users on Windows systems who rely on specific line-ending formats. While this is minor, it may cause issues in environments where line endings are strictly enforced.
- The change from CRLF to LF in
💡 Suggestions for Improvement
-
Policy Testing:
- Add a section in the tutorial that demonstrates how to systematically test policies using pytest. For example, users could write tests to ensure that
DENY_OVERRIDESalways denies conflicting actions or thatMOST_SPECIFIC_WINSresolves conflicts correctly based on scope.
- Add a section in the tutorial that demonstrates how to systematically test policies using pytest. For example, users could write tests to ensure that
-
Environment Validation:
- Enhance the environment-aware policy example (
04_env_policy.yaml) by including a validation mechanism for theenvironmentfield. For instance, restrict valid environments to a predefined list (development,staging,production) using Pydantic models.
- Enhance the environment-aware policy example (
-
Conflict Resolution Examples:
- Expand the tutorial to include more complex conflict scenarios, such as three or more policies with overlapping rules. This would help users understand how the resolution strategies scale with complexity.
-
OWASP Agentic Top 10 Compliance:
- Include a section in the tutorial that maps the conflict resolution strategies to OWASP Agentic Top 10 principles. For example, highlight how
DENY_OVERRIDESaligns with "Fail Securely" and howMOST_SPECIFIC_WINScould be misused to bypass security controls.
- Include a section in the tutorial that maps the conflict resolution strategies to OWASP Agentic Top 10 principles. For example, highlight how
-
Interactive Examples:
- Provide an interactive Jupyter Notebook version of the tutorial. This would allow users to experiment with different policies and resolution strategies in real-time.
✅ Positive Observations
-
Documentation Quality:
- The tutorial is well-written and provides clear, step-by-step instructions. The inclusion of YAML examples and Python code makes it easy for users to follow along.
-
Conflict Resolution Strategies:
- The explanation of the four strategies (
DENY_OVERRIDES,ALLOW_OVERRIDES,PRIORITY_FIRST_MATCH,MOST_SPECIFIC_WINS) is thorough and provides valuable insights into their trade-offs.
- The explanation of the four strategies (
-
Runnable Examples:
- The inclusion of a runnable Python script (
04_conditional_policies.py) is excellent for hands-on learning.
- The inclusion of a runnable Python script (
-
Scope Hierarchy:
- The tutorial does a good job explaining the scope hierarchy and its impact on conflict resolution. This is crucial for understanding how policies interact in complex organizations.
Action Items
- Add security disclaimers for
ALLOW_OVERRIDESand default actions in environment-aware policies. - Document migration steps for users transitioning to the new conflict resolution API.
- Enhance the tutorial with testing examples and validation mechanisms.
- Map conflict resolution strategies to OWASP Agentic Top 10 principles.
- Consider providing an interactive Jupyter Notebook version of the tutorial.
Final Recommendation
Merge with the above changes to address critical security concerns and ensure backward compatibility.
|
Quick update on the decisions I made for Chapter 4: I expanded the scope beyond just environment-aware rules. I added conflict resolution in the same chapter since it naturally follows. I used the existing PolicyConflictResolver and PolicyScope hierarchy. Will gradually increase complexity to more production level policy enforcements. |
imran-siddique
left a comment
There was a problem hiding this comment.
Great continuation of the tutorial series @harinarayansrivatsan! Chapter 4 looks well-structured with environment-based conditional policies.
Summary
send_email, support team allows itTest plan
python docs/tutorials/policy-as-code/examples/04_conditional_policies.pyRef #706