-
Notifications
You must be signed in to change notification settings - Fork 901
feat(plugins): add Zscaler AI Guard plugin #1514
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
Open
z-anvesha
wants to merge
4
commits into
Portkey-AI:main
Choose a base branch
from
z-anvesha:feature/zscaler-portkey-plugin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+367
−37
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,59 @@ | ||
| import conf from '../conf.json'; | ||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import { fileURLToPath } from 'url'; | ||
|
|
||
| const pluginsEnabled = conf.plugins_enabled; | ||
|
|
||
| let importStrings: any = []; | ||
| let funcStrings: any = {}; | ||
| let funcs: any = {}; | ||
|
|
||
| for (const plugin of pluginsEnabled) { | ||
| const manifest = await import(`./${plugin}/manifest.json`); | ||
| const functions = manifest.functions.map((func: any) => func.id); | ||
| importStrings = [ | ||
| ...importStrings, | ||
| ...functions.map( | ||
| (func: any) => | ||
| `import { handler as ${manifest.id}${func} } from "./${plugin}/${func}"` | ||
| ), | ||
| ]; | ||
|
|
||
| funcs[plugin] = {}; | ||
| functions.forEach((func: any) => { | ||
| funcs[plugin][func] = func; | ||
| }); | ||
|
|
||
| funcStrings[plugin] = []; | ||
| for (let key in funcs[plugin]) { | ||
| funcStrings[plugin].push(`"${key}": ${manifest.id}${funcs[plugin][key]}`); | ||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = path.dirname(__filename); | ||
|
|
||
| async function buildPlugins() { | ||
| const conf = JSON.parse( | ||
| fs.readFileSync(path.join(__dirname, '../conf.json'), 'utf-8') | ||
| ); | ||
| const pluginsEnabled = conf.plugins_enabled; | ||
|
|
||
| let importStrings: any = []; | ||
| let funcStrings: any = {}; | ||
| let funcs: any = {}; | ||
|
|
||
| for (const plugin of pluginsEnabled) { | ||
| const manifestPath = path.join(__dirname, plugin, 'manifest.json'); | ||
| const manifestContent = fs.readFileSync(manifestPath, 'utf-8'); | ||
| const manifest = JSON.parse(manifestContent); | ||
| const safePluginId = manifest.id.replace(/-/g, ''); | ||
| const functions = manifest.functions.map((func: any) => func.id); | ||
| importStrings = [ | ||
| ...importStrings, | ||
| ...functions.map( | ||
| (func: any) => | ||
| `import { handler as ${safePluginId}${func.replace(/-/g, '')} } from "./${plugin}/${func}"` | ||
| ), | ||
| ]; | ||
|
|
||
| funcs[plugin] = {}; | ||
| functions.forEach((func: any) => { | ||
| funcs[plugin][func] = func; | ||
| }); | ||
|
|
||
| funcStrings[plugin] = []; | ||
| for (let key in funcs[plugin]) { | ||
| funcStrings[plugin].push( | ||
| `"${key}": ${safePluginId}${funcs[plugin][key].replace(/-/g, '')}` | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const indexFilePath = './plugins/index.ts'; | ||
| const indexFilePath = './plugins/index.ts'; | ||
|
|
||
| let finalFuncStrings: any = []; | ||
| for (let key in funcStrings) { | ||
| finalFuncStrings.push( | ||
| `\n "${key}": {\n ${funcStrings[key].join(',\n ')}\n }` | ||
| ); | ||
| } | ||
| let finalFuncStrings: any = []; | ||
| for (let key in funcStrings) { | ||
| finalFuncStrings.push( | ||
| `\n "${key}": {\n ${funcStrings[key].join(',\n ')}\n }` | ||
| ); | ||
| } | ||
|
|
||
| const content = `${importStrings.join('\n')}\n\nexport const plugins = {${finalFuncStrings}\n};\n`; | ||
| const content = `${importStrings.join('\n')}\n\nexport const plugins = {${finalFuncStrings}\n};\n`; | ||
|
|
||
| fs.writeFileSync(indexFilePath, content); | ||
| } | ||
|
|
||
| fs.writeFileSync(indexFilePath, content); | ||
| buildPlugins(); |
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,148 @@ | ||
| import { | ||
| HookEventType, | ||
| PluginContext, | ||
| type PluginHandler, | ||
| PluginParameters, | ||
| } from '../types'; | ||
| import { post, getText } from '../utils'; | ||
|
|
||
| const ZSCALER_EXECUTE_POLICY_URL = | ||
| 'https://api.zseclipse.net/v1/detection/execute-policy'; | ||
|
|
||
| interface ZscalerCredentials { | ||
| zscalerApiKey: string; | ||
| } | ||
|
|
||
| interface ZscalerPluginParameters { | ||
| policyId: string; | ||
| } | ||
|
|
||
| interface ZscalerExecutePolicyRequest { | ||
| policyId: string; | ||
| direction: 'IN' | 'OUT'; | ||
| content: any; | ||
| } | ||
|
|
||
| interface ZscalerExecutePolicyResponse { | ||
| action: 'ALLOW' | 'BLOCK'; | ||
| detectorResponses?: Record<string, any>; | ||
| } | ||
|
|
||
| export const handler: PluginHandler<ZscalerCredentials> = async ( | ||
| context: PluginContext, | ||
| parameters: PluginParameters<ZscalerCredentials>, | ||
| eventType: HookEventType | ||
| ) => { | ||
| let error: Error | null = null; | ||
| let verdict = true; | ||
| let data: Record<string, any> = {}; | ||
|
|
||
| const credentials = parameters.credentials as ZscalerCredentials | undefined; | ||
| const pluginParams = parameters.parameters as ZscalerPluginParameters; | ||
|
|
||
| // :white_check_mark: FAIL OPEN (aligned with other plugins) | ||
| if (!credentials?.zscalerApiKey) { | ||
| return { | ||
| error: new Error('Zscaler AI Guard API Key must be configured.'), | ||
| verdict: true, | ||
| data, | ||
| }; | ||
| } | ||
|
|
||
| if (!pluginParams?.policyId) { | ||
| return { | ||
| error: new Error('Zscaler AI Guard Policy ID must be configured.'), | ||
| verdict: true, | ||
| data, | ||
| }; | ||
| } | ||
|
|
||
| const contentToScan = getText(context, eventType); | ||
|
|
||
| if (!contentToScan) { | ||
| return { | ||
| error: new Error('No content found to scan.'), | ||
| verdict: true, | ||
| data, | ||
| }; | ||
| } | ||
|
|
||
| const direction = eventType === 'beforeRequestHook' ? 'IN' : 'OUT'; | ||
|
|
||
| const zscalerRequest: ZscalerExecutePolicyRequest = { | ||
| policyId: pluginParams.policyId, | ||
| direction, | ||
| content: contentToScan, | ||
| }; | ||
|
|
||
| try { | ||
| const headers = { | ||
| 'Content-Type': 'application/json', | ||
| Authorization: `Bearer ${credentials.zscalerApiKey}`, | ||
| }; | ||
|
|
||
| const response: ZscalerExecutePolicyResponse = await post( | ||
| ZSCALER_EXECUTE_POLICY_URL, | ||
| zscalerRequest, | ||
| { headers }, | ||
| 10000 | ||
| ); | ||
|
|
||
| data = { | ||
| zscalerAction: response.action, | ||
| detectorResponses: response.detectorResponses, | ||
| }; | ||
|
|
||
| // :white_check_mark: Check top-level action | ||
| let isBlocked = response.action === 'BLOCK'; | ||
|
|
||
| // :white_check_mark: Also check individual detectors (if present) | ||
| if (response.detectorResponses) { | ||
| const detectorBlocked = Object.values(response.detectorResponses).some( | ||
| (detector: any) => detector?.action === 'BLOCK' | ||
| ); | ||
|
|
||
| isBlocked = isBlocked || detectorBlocked; | ||
| } | ||
|
|
||
| verdict = !isBlocked; | ||
|
|
||
| if (!verdict) { | ||
| error = new Error( | ||
| 'Zscaler AI Guard blocked the content with action: BLOCK' | ||
| ); | ||
| } | ||
z-anvesha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (e: unknown) { | ||
| verdict = false; | ||
|
|
||
| const maybeError = e as any; | ||
| const status = maybeError?.response?.status; | ||
|
|
||
| // :white_check_mark: Proper 429 handling (your test will now pass) | ||
| if (status === 429) { | ||
| error = new Error('Zscaler AI Guard rate limit exceeded. Status: 429'); | ||
| } | ||
| // :white_check_mark: Proper 5xx handling | ||
| else if (status && status >= 500 && status < 600) { | ||
| error = new Error( | ||
| `Zscaler AI Guard API returned a server error. Status: ${status}` | ||
| ); | ||
| } | ||
| // :white_check_mark: Normal JS Error | ||
| else if (e instanceof Error) { | ||
| error = e; | ||
| } | ||
| // :white_check_mark: Fallback | ||
| else { | ||
| error = new Error('An unknown error occurred during Zscaler API call.'); | ||
| } | ||
|
|
||
| data = { originalError: error.message }; | ||
| } | ||
|
|
||
| return { | ||
| error, | ||
| verdict, | ||
| data, | ||
| }; | ||
| }; | ||
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,45 @@ | ||
| { | ||
| "id": "zscalerAiGuard", | ||
| "name": "Zscaler AI Guard", | ||
| "version": "1.0.0", | ||
| "description": "Integrates Zscaler AI Guard for advanced GenAI security, including DLP and prompt injection detection.", | ||
| "author": "Zscaler", | ||
| "credentials": { | ||
| "type": "object", | ||
| "properties": { | ||
| "zscalerApiKey": { | ||
| "type": "string", | ||
| "label": "Zscaler AI Guard API Key", | ||
| "description": "The API Key generated from your Zscaler AI Guard tenant.", | ||
| "encrypted": true | ||
| } | ||
| }, | ||
| "required": ["zscalerApiKey"] | ||
| }, | ||
| "functions": [ | ||
| { | ||
| "name": "Zscaler AI Guard Check", | ||
| "id": "zscalerAiGuardCheck", | ||
| "supportedHooks": ["beforeRequestHook", "afterRequestHook"], | ||
| "type": "guardrail", | ||
| "description": [ | ||
| { | ||
| "type": "subHeading", | ||
| "text": "Performs Zscaler AI Guard policy checks on prompts and LLM responses." | ||
| } | ||
| ], | ||
| "parameters": { | ||
| "type": "object", | ||
| "properties": { | ||
| "policyId": { | ||
| "type": "string", | ||
| "label": "Zscaler Policy ID", | ||
| "description": "The ID of the Zscaler Detections Policy to execute.", | ||
| "required": true | ||
| } | ||
| }, | ||
| "required": ["policyId"] | ||
| } | ||
| } | ||
| ] | ||
| } |
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.