-
Notifications
You must be signed in to change notification settings - Fork 0
feat(policy-engine) : built policy engine #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import dotenv from "dotenv"; | |
| import { formatDate } from "@repo/shared"; | ||
| import { mcpDiscovery, mcpExecutor } from "../mcp/bootstrap.js"; | ||
| import { AppError } from "../types.js"; | ||
| import policiesRouter from "./policy/router.js"; | ||
|
|
||
| dotenv.config(); | ||
|
|
||
|
|
@@ -12,6 +13,7 @@ const port = process.env.PORT || 3001; | |
|
|
||
| app.use(cors()); | ||
| app.use(express.json({ limit: "1mb" })); | ||
| app.use(policiesRouter); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Policy management routes are exposed without authentication/authorization. This allows unauthorized clients to create/update/delete enforcement policies. Prompt for AI agents |
||
|
|
||
| // Health check endpoint | ||
| app.get("/health", (req, res) => { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { db, ApprovalStatus } from "@repo/db"; | ||
| import PolicyEngine from "./engine.js"; | ||
| import type { ApprovalRequest, ConversationRequest } from "../../types.js"; | ||
|
|
||
| export type Decision = "ALLOW" | "DENY" | "PENDING"; | ||
|
|
||
| export interface DecisionResult { | ||
| decision: Decision; | ||
| reason?: string; | ||
| } | ||
|
|
||
| export async function decide( | ||
| context: ApprovalRequest, | ||
| conversation: ConversationRequest, | ||
| ): Promise<DecisionResult> { | ||
| try { | ||
| // Step 1: Call PolicyEngine | ||
| const policy = await PolicyEngine(context, conversation); | ||
|
|
||
| // Step 2: Policy denied and does not require approval | ||
| if (!policy.allowed && !policy.requiresApproval) { | ||
| return { | ||
| decision: "DENY", | ||
| reason: policy.reason, | ||
| }; | ||
| } | ||
|
|
||
| // Step 3: Policy requires human approval | ||
| if (policy.requiresApproval) { | ||
| if (!context.approvalId) { | ||
| const created = await db.approval.create({ | ||
| data: { | ||
| tool_name: context.tool_name, | ||
| arguments: context.arguments as any, | ||
| status: ApprovalStatus.PENDING, | ||
| }, | ||
| }); | ||
|
|
||
| return { | ||
| decision: "PENDING", | ||
| reason: created.id, | ||
| }; | ||
| } | ||
|
|
||
| const approval = await db.approval.findUnique({ | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| where: { | ||
| id: context.approvalId, | ||
| }, | ||
| }); | ||
|
|
||
| if (!approval) { | ||
| return { | ||
| decision: "DENY", | ||
| reason: "Approval not found", | ||
| }; | ||
| } | ||
|
|
||
| if (approval.tool_name !== context.tool_name) { | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| return { | ||
| decision: "DENY", | ||
| reason: "Approval tool name mismatch", | ||
| }; | ||
| } | ||
|
|
||
| switch (approval.status) { | ||
| case ApprovalStatus.APPROVED: | ||
| await db.approval.delete({ | ||
| where: { id: approval.id }, | ||
| }); | ||
| return { | ||
| decision: "ALLOW", | ||
| }; | ||
|
greptile-apps[bot] marked this conversation as resolved.
greptile-apps[bot] marked this conversation as resolved.
|
||
| case ApprovalStatus.PENDING: | ||
| return { | ||
| decision: "PENDING", | ||
| }; | ||
| case ApprovalStatus.REJECTED: | ||
| return { | ||
| decision: "DENY", | ||
| reason: "Approval rejected", | ||
| }; | ||
| default: | ||
| return { | ||
| decision: "DENY", | ||
| reason: "Unrecognized approval status", | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| // Step 4: Policy allowed | ||
| if (policy.allowed) { | ||
| return { | ||
| decision: "ALLOW", | ||
| }; | ||
| } | ||
|
|
||
| // Fallback/Safety return | ||
| return { | ||
| decision: "DENY", | ||
| reason: "Unrecognized policy state", | ||
| }; | ||
| } catch (error) { | ||
| return { | ||
| decision: "DENY", | ||
| reason: "Decision engine failure", | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import needsApproval from "./rules/approval.js"; | ||
| import isblocked from "./rules/block.js"; | ||
| import budgetExceeded from "./rules/budget.js"; | ||
| import type { ApprovalRequest, ConversationRequest } from "../../types.js"; | ||
| import { db } from "@repo/db"; | ||
| import { logger } from "../../mcp/logger.js"; | ||
|
|
||
| export interface PolicyEngineResult { | ||
| allowed: boolean; | ||
| requiresApproval: boolean; | ||
| reason?: string; | ||
| } | ||
|
|
||
| export default async function PolicyEngine( | ||
| context: ApprovalRequest, | ||
| conversation: ConversationRequest, | ||
| ): Promise<PolicyEngineResult> { | ||
| let policy; | ||
| try { | ||
| const tool_name = context.tool_name; | ||
|
|
||
| // Fetch the policy record once to prevent double lookup and TOCTOU race conditions | ||
| policy = await db.policy.findUnique({ | ||
| where: { tool_name }, | ||
| }); | ||
| } catch (error: any) { | ||
| logger.error("Failed to query policy table in PolicyEngine pre-fetch", { | ||
| tool_name: context.tool_name, | ||
| error_message: error.message || String(error), | ||
| }); | ||
| return { | ||
| allowed: false, | ||
| requiresApproval: false, | ||
| reason: "Failed to query policy table", | ||
| }; | ||
| } | ||
|
|
||
| try { | ||
| const tool_name = context.tool_name; | ||
|
|
||
| // 1. Block Check | ||
| const blockedResult = await isblocked(tool_name, policy); | ||
| if (!blockedResult.success) { | ||
| return { | ||
| allowed: false, | ||
| requiresApproval: false, | ||
| reason: blockedResult.reason, | ||
| }; | ||
| } | ||
| if (blockedResult.result) { | ||
| return { | ||
| allowed: false, | ||
| requiresApproval: false, | ||
| reason: blockedResult.reason, | ||
| }; | ||
| } | ||
|
|
||
| // 2. Budget Check | ||
| const budgetResult = await budgetExceeded( | ||
| conversation.conversationId, | ||
| conversation.token, | ||
| ); | ||
| if (!budgetResult.success) { | ||
| return { | ||
| allowed: false, | ||
| requiresApproval: false, | ||
| reason: budgetResult.reason, | ||
| }; | ||
| } | ||
| if (budgetResult.result) { | ||
| return { | ||
| allowed: false, | ||
| requiresApproval: false, | ||
| reason: budgetResult.reason, | ||
| }; | ||
| } | ||
|
|
||
| // 3. Approval Check | ||
| const approvalResult = await needsApproval(tool_name, policy); | ||
| if (!approvalResult.success) { | ||
| return { | ||
| allowed: false, | ||
| requiresApproval: false, | ||
| reason: approvalResult.reason, | ||
| }; | ||
| } | ||
| if (approvalResult.result) { | ||
| return { | ||
| allowed: false, | ||
| requiresApproval: true, | ||
| reason: approvalResult.reason, | ||
| }; | ||
| } | ||
|
|
||
| // 4. Default Success (Allowed) | ||
| return { | ||
| allowed: true, | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| requiresApproval: false, | ||
| }; | ||
| } catch (error: any) { | ||
| return { | ||
| allowed: false, | ||
| requiresApproval: false, | ||
| reason: error instanceof Error ? error.message : String(error), | ||
| }; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.