Skip to content

Restrict PoC command to only run within it's directory, and on a spec…#142

Open
QuinnDACollins wants to merge 1 commit intomainfrom
deterministic_local_path_traversal
Open

Restrict PoC command to only run within it's directory, and on a spec…#142
QuinnDACollins wants to merge 1 commit intomainfrom
deterministic_local_path_traversal

Conversation

@QuinnDACollins
Copy link
Contributor

Introduce more Determinism in our PoC command, and specifically when generating concept for path traversal

  • Only runs PoC within our ./gemini_security/poc directory
  • When running for path traversal, generates a specific file right outside of the project directory

…ific file when encountering a Path Traversal vulnerability
@github-actions
Copy link

🤖 Hi @QuinnDACollins, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@gemini-code-assist
Copy link

Summary of Changes

Hello @QuinnDACollins, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the security and determinism of the PoC (Proof-of-Concept) command. It introduces a critical restriction that limits PoC script execution to a dedicated, secure directory, mitigating potential risks associated with running untrusted code. Additionally, it refines the process for generating Path Traversal PoCs, ensuring a consistent and verifiable method for demonstrating this specific vulnerability, which includes strict temporary file management.

Highlights

  • PoC Execution Restriction: Implemented a security measure to restrict the execution of Proof-of-Concept (PoC) scripts exclusively to the .gemini_security/poc directory, preventing arbitrary code execution from other locations.
  • Path Traversal PoC Determinism: Enhanced the generation of Path Traversal PoCs to be more deterministic, specifically by requiring the creation and subsequent cleanup of a temporary file (gcli_secext_temp.txt) directly outside the project directory to demonstrate the vulnerability.
  • Updated PoC Generation Instructions: The internal documentation for PoC generation has been updated to reflect the new requirements for Path Traversal vulnerabilities, emphasizing temporary file handling and cleanup.
  • Test Coverage: Added new test cases to verify that PoC execution fails when attempting to access files outside the designated secure directory, and updated existing tests to align with the new PoC file path structure.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • mcp-server/src/index.ts
    • Updated PoC generation instructions to include specific requirements for Path Traversal vulnerabilities, detailing temporary file creation and cleanup.
  • mcp-server/src/poc.test.ts
    • Enhanced mock path utilities (resolve, sep) for more robust testing of path-related logic.
    • Adjusted existing test cases to use the new .gemini_security/poc directory for PoC file paths and working directories.
    • Added a new test case to confirm that PoC execution is blocked when attempting to run a script from an unauthorized directory.
  • mcp-server/src/poc.ts
    • Implemented a security validation check within the runPoc function to ensure that the provided filePath resides strictly within the .gemini_security/poc directory.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a security enhancement by restricting the execution of Proof-of-Concept (PoC) scripts to a specific directory (.gemini_security/poc), which effectively prevents path traversal attacks. The changes include validation logic in poc.ts and updated tests to cover this new security measure.

My review identifies a critical command injection vulnerability that remains in the runPoc function, which should be addressed. I've also included a couple of medium-severity suggestions to improve code maintainability by reducing code duplication in tests and centralizing hardcoded paths.

Comment on lines +26 to +42
// Validate that the filePath is within the safe PoC directory
const resolvedFilePath = dependencies.path.resolve(filePath);
const safePocDir = dependencies.path.resolve(process.cwd(), '.gemini_security/poc');

if (!resolvedFilePath.startsWith(safePocDir + dependencies.path.sep)) {
return {
content: [
{
type: 'text',
text: JSON.stringify({
error: `Security Error: PoC execution is restricted to files within '${safePocDir}'. Attempted to access '${resolvedFilePath}'.`,
}),
},
],
isError: true,
};
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

While this path validation is an excellent security improvement against path traversal, a critical command injection vulnerability remains. The filePath variable is later used directly in a shell command via execAsync(node ${filePath}). If filePath contains shell metacharacters (e.g., ';', '&&', '|'), it can lead to arbitrary command execution.

To fix this, you should use a method that properly handles arguments and does not execute a shell, such as execFile from child_process.

Example:

import { execFile } from 'child_process';
import { promisify } from 'util';

const execFileAsync = promisify(execFile);

// In runPoc, instead of execAsync:
await execFileAsync('node', [filePath]);

This change is crucial to fully secure the PoC execution.

Comment on lines 12 to 19
const mockPath = {
dirname: (p: string) => p.substring(0, p.lastIndexOf('/')),
resolve: (p1: string, p2?: string) => {
if (p2) return p1 + '/' + p2;
return p1;
},
sep: '/',
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve maintainability and reduce code duplication, consider defining the mockPath object once at the describe block level and reusing it across all tests within this suite. This will make the test file cleaner and easier to update if the mock implementation needs to change.


// Validate that the filePath is within the safe PoC directory
const resolvedFilePath = dependencies.path.resolve(filePath);
const safePocDir = dependencies.path.resolve(process.cwd(), '.gemini_security/poc');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The path '.gemini_security/poc' is hardcoded here. This string is also referenced in mcp-server/src/index.ts and test files. To improve maintainability and avoid magic strings, consider defining this path as a constant in a shared location and reusing it across the application.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 Review Summary

This pull request introduces a security measure to restrict the execution of Proof-of-Concept (PoC) scripts to a specific directory, preventing potential path traversal vulnerabilities. The changes are well-implemented and include corresponding tests.

🔍 General Feedback

  • The addition of the security check in poc.ts is a welcome improvement.
  • The new test case in poc.test.ts effectively validates the security restriction.
  • The updated instructions in index.ts are clear and provide good guidance for generating PoCs for path traversal vulnerabilities.

Comment on lines +26 to +30
// Validate that the filePath is within the safe PoC directory
const resolvedFilePath = dependencies.path.resolve(filePath);
const safePocDir = dependencies.path.resolve(process.cwd(), '.gemini_security/poc');

if (!resolvedFilePath.startsWith(safePocDir + dependencies.path.sep)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 For better maintainability and readability, consider the following improvements: 1. Extract the magic string '.gemini_security/poc' into a constant, as it is used in multiple places (e.g., `poc.ts` and `poc.test.ts`). 2. Rename the variable `safePocDir` to something more descriptive like `allowedPocDir` or `restrictedPocDir` to better reflect its purpose.
Suggested change
// Validate that the filePath is within the safe PoC directory
const resolvedFilePath = dependencies.path.resolve(filePath);
const safePocDir = dependencies.path.resolve(process.cwd(), '.gemini_security/poc');
if (!resolvedFilePath.startsWith(safePocDir + dependencies.path.sep)) {
const POC_DIRECTORY = '.gemini_security/poc';
...
const allowedPocDir = dependencies.path.resolve(process.cwd(), POC_DIRECTORY);
if (!resolvedFilePath.startsWith(allowedPocDir + dependencies.path.sep)) {
...

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

## 📋 Security Analysis Summary

The pull request introduces a command injection vulnerability in the runPoc function. The filePath parameter is used in a shell command without proper sanitization, which could allow an attacker to execute arbitrary commands.

🔍 General Feedback

  • The path traversal check is a good security measure, but it's not sufficient to prevent command injection.
  • It's recommended to use spawn or fork instead of exec or execAsync when dealing with user input to prevent command injection vulnerabilities.

* Create a 'poc' directory in '.gemini_security' if it doesn't exist.
* Generate a Node.js script that demonstrates the vulnerability under the '.gemini_security/poc/' directory.
* Based on the vulnerability type certain criteria must be met in our script, otherwise generate the PoC to the best of your ability:
* If the vulnerability is a Path Traversal, the script should demonstrate the vulnerability by creating a temporary file 'gcli_secext_temp.txt' directly outside of the project directory. The script should then try to read the file using the vulnerable code. **IMPORTANT:** The script MUST clean up (delete) the 'gcli_secext_temp.txt' file after the verification step, regardless of whether the read was successful or not. Wrap file operations in try/finally blocks to ensure cleanup.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to rely on the script to do the cleanup? Can we strictly enforce the creation path and then have a deterministic cleanup function that attempts to delete the file always? This would avoid us relying on agent to do the cleanup.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking further - can we do that with file creation as well? i.e. use a separate function to create the file, then ask the PoC generator to generate exploit such that we are able to access that file, and then use a function to clean up the file. The only part that LLMs would do would be to write the exploit to look for the pre-defined file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments