-
Notifications
You must be signed in to change notification settings - Fork 23
Add basic POC Generation Command to MCP Server t #109
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
base: main
Are you sure you want to change the base?
Changes from 11 commits
78bd06a
32ad411
8cbfd3c
da3ef99
bd6d4e5
1c87790
0ea0b48
bac4ab6
1723ce8
e0f60ea
2f533fd
7e5ea18
594760e
6bc9bf9
5b973bd
682488d
6b8fe2b
d52c8ca
847ec4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -40,6 +40,8 @@ For EVERY task, you MUST follow this procedure. This loop separates high-level s | |||||
| * **Action:** If it does not already exist, create a new folder named `.gemini_security` in the user's workspace. | ||||||
| * **Action:** Create a new file named `SECURITY_ANALYSIS_TODO.md` in `.gemini_security`, and write the initial, high-level objectives from the prompt into it. | ||||||
|
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. 🟢 Low There's a typo in the action description:
Suggested change
|
||||||
| * **Action:** Create a new, empty file named `DRAFT_SECURITY_REPORT.md` in `.gemini_security`. | ||||||
| * **Action"** Prep yourself using the following possible notes files under `.gemini_security/`. If they do not exist, skip them. | ||||||
|
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. 🟡 Medium Inconsistency in file naming: the documentation (
Suggested change
|
||||||
| * `vuln_allowlist.txt`: The allowlist file has vulnerabilities to ignore during your scan. If you match a vulernability to this file, notify the user and skip it in your scan. | ||||||
|
|
||||||
| 2. **Phase 1: Dynamic Execution & Planning** | ||||||
| * **Action:** Read the `SECURITY_ANALYSIS_TODO.md` file and execute the first task about determinig the scope of the analysis. | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { describe, it, vi, expect } from 'vitest'; | ||
| import { validatePocParams, runPoc } from './poc.js'; | ||
|
|
||
| describe('validatePocParams', () => { | ||
| it('should return valid message when parameters are provided', async () => { | ||
| const result = await validatePocParams({ | ||
| vulnerabilityType: 'SQL Injection', | ||
| sourceCode: 'SELECT * FROM users WHERE id = ' + 1, | ||
| }); | ||
|
|
||
| expect(result.isError).toBeUndefined(); | ||
| expect(result.content[0].text).toBe( | ||
| JSON.stringify({ message: 'Parameters are valid.' }) | ||
| ); | ||
| }); | ||
|
|
||
| it('should return error when vulnerabilityType is missing', async () => { | ||
| const result = await validatePocParams({ | ||
| vulnerabilityType: '', | ||
| sourceCode: 'code', | ||
| }); | ||
|
|
||
| expect(result.isError).toBe(true); | ||
| expect(result.content[0].text).toBe( | ||
| JSON.stringify({ error: 'Vulnerability type is required.' }) | ||
| ); | ||
| }); | ||
|
|
||
| it('should return error when sourceCode is missing', async () => { | ||
| const result = await validatePocParams({ | ||
| vulnerabilityType: 'type', | ||
| sourceCode: '', | ||
| }); | ||
|
|
||
| expect(result.isError).toBe(true); | ||
| expect(result.content[0].text).toBe( | ||
| JSON.stringify({ error: 'Source code is required.' }) | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('runPoc', () => { | ||
| it('should write file and execute it', async () => { | ||
| const mockFs = { | ||
| mkdir: vi.fn(async () => undefined), | ||
| writeFile: vi.fn(async () => undefined), | ||
| }; | ||
| const mockPath = { | ||
| join: (...args: string[]) => args.join('/'), | ||
| }; | ||
| const mockExecAsync = vi.fn(async () => ({ stdout: 'output', stderr: '' })); | ||
|
|
||
| const result = await runPoc( | ||
| { code: 'console.log("test")' }, | ||
| { fs: mockFs as any, path: mockPath as any, execAsync: mockExecAsync as any } | ||
| ); | ||
|
|
||
| expect(mockFs.mkdir).toHaveBeenCalledTimes(1); | ||
| expect(mockFs.writeFile).toHaveBeenCalledTimes(1); | ||
| expect(mockExecAsync).toHaveBeenCalledTimes(2); | ||
| expect(result.content[0].text).toBe( | ||
| JSON.stringify({ stdout: 'output', stderr: '' }) | ||
| ); | ||
| }); | ||
|
|
||
| it('should handle execution errors', async () => { | ||
| const mockFs = { | ||
| mkdir: vi.fn(async () => undefined), | ||
| writeFile: vi.fn(async () => undefined), | ||
| }; | ||
| const mockPath = { | ||
| join: (...args: string[]) => args.join('/'), | ||
| }; | ||
| const mockExecAsync = vi.fn(async () => { | ||
| throw new Error('Execution failed'); | ||
| }); | ||
|
|
||
| const result = await runPoc( | ||
| { code: 'error' }, | ||
| { fs: mockFs as any, path: mockPath as any, execAsync: mockExecAsync as any } | ||
| ); | ||
|
|
||
| expect(result.isError).toBe(true); | ||
| expect(result.content[0].text).toBe( | ||
| JSON.stringify({ error: 'Execution failed' }) | ||
| ); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { CallToolResult } from '@modelcontextprotocol/sdk/types.js'; | ||
| import { promises as fs } from 'fs'; | ||
| import path from 'path'; | ||
| import { exec } from 'child_process'; | ||
| import { promisify } from 'util'; | ||
|
|
||
| const execAsync = promisify(exec); | ||
|
|
||
| export async function validatePocParams( | ||
| { | ||
| vulnerabilityType, | ||
| sourceCode, | ||
| }: { | ||
| vulnerabilityType: string; | ||
| sourceCode: string; | ||
| } | ||
| ): Promise<CallToolResult> { | ||
| if (!vulnerabilityType || !vulnerabilityType.trim()) { | ||
| return { | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: JSON.stringify({ error: 'Vulnerability type is required.' }), | ||
| }, | ||
| ], | ||
| isError: true, | ||
| }; | ||
| } | ||
|
|
||
| if (!sourceCode || !sourceCode.trim()) { | ||
| return { | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: JSON.stringify({ error: 'Source code is required.' }), | ||
| }, | ||
| ], | ||
| isError: true, | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: JSON.stringify({ message: 'Parameters are valid.' }), | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
|
|
||
| export async function runPoc( | ||
| { | ||
| code, | ||
| }: { | ||
| code: string; | ||
| }, | ||
| dependencies: { fs: typeof fs; path: typeof path; execAsync: typeof execAsync } = { fs, path, execAsync } | ||
| ): Promise<CallToolResult> { | ||
| try { | ||
| const CWD = process.cwd(); | ||
| const securityDir = dependencies.path.join(CWD, '.gemini_security'); | ||
|
|
||
| // Ensure .gemini_security directory exists | ||
| try { | ||
| await dependencies.fs.mkdir(securityDir, { recursive: true }); | ||
| } catch (error) { | ||
QuinnDACollins marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
QuinnDACollins marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Ignore error if directory already exists | ||
| } | ||
|
|
||
| const pocFilePath = dependencies.path.join(securityDir, 'poc.js'); | ||
| await dependencies.fs.writeFile(pocFilePath, code, 'utf-8'); | ||
QuinnDACollins marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| try { | ||
| await dependencies.execAsync('npm install', { cwd: securityDir }); | ||
| } catch (error) { | ||
| // Ignore errors from npm install, as it might fail if no package.json exists, | ||
| // but we still want to attempt running the PoC. | ||
| } | ||
| const { stdout, stderr } = await dependencies.execAsync(`node ${pocFilePath}`); | ||
QuinnDACollins marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return { | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: JSON.stringify({ stdout, stderr }), | ||
| }, | ||
| ], | ||
| }; | ||
| } catch (error) { | ||
| let errorMessage = 'An unknown error occurred.'; | ||
| if (error instanceof Error) { | ||
| errorMessage = error.message; | ||
| } | ||
| return { | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: JSON.stringify({ error: errorMessage }), | ||
| }, | ||
| ], | ||
| isError: true, | ||
| }; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.