Skip to content

Commit 7593306

Browse files
Merge pull request #38 from Contrast-Security-OSS/AIML-52_doc_updates_for_copilot_support
AIML-52 doc updates for copilot support
2 parents 149b00d + 129346d commit 7593306

File tree

3 files changed

+101
-9
lines changed

3 files changed

+101
-9
lines changed

README.md

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,26 @@ Welcome to Contrast AI SmartFix\! SmartFix is an AI-powered agent that automatic
2424
* **GitHub Token Permissions:** The GitHub token must have `contents: write` and `pull-requests: write` permissions. These permissions must be explicitly set in your workflow file. Note, SmartFix uses the internal GitHub token for Actions; you do not need to create a Personal Access Token (PAT).
2525
* **LLM Access:** Ensure that you have access to one of our recommended LLMs for use with SmartFix. If using an AWS Bedrock model, please see Amazon's User Guide on [model access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access-modify.html).
2626

27+
### Coding Agent
28+
29+
SmartFix supports two distinct coding agents for vulnerability remediation:
30+
31+
* **SmartFix Agent (Recommended):** Uses Contrast vulnerability data with a team of agentic AIs to analyze, fix, and validate vulnerability remediations. This agent creates a complete fix, ensures your project builds successfully, and verifies that existing tests continue to pass. Requires API keys for your preferred LLM provider.
32+
33+
* **GitHub Copilot Agent (Beta):** Leverages GitHub Copilot for vulnerability fixes through GitHub Issues. SmartFix creates a detailed GitHub Issue with vulnerability information and assigns it to GitHub Copilot for resolution. Copilot then attempts the fix and creates a Pull Request.
34+
35+
**GitHub Copilot Requirements:**
36+
* GitHub repository with **Issues** and **GitHub Copilot** enabled
37+
* GitHub Personal Access Token (PAT) with:
38+
* `meta` (read permissions)
39+
* `pulls` (read-write permissions)
40+
* `issues` (read-write permissions)
41+
2742
### Installation and Configuration
2843

29-
SmartFix is configured as a GitHub Action. Add a workflow file (e.g., `.github/workflows/smartfix.yml`) to your repository following the below example. A full workflow example is also available at [https://github.com/Contrast-Security-OSS/contrast-ai-smartfix-action/blob/main/contrast-ai-smartfix.yml.template](https://github.com/Contrast-Security-OSS/contrast-ai-smartfix-action/blob/main/contrast-ai-smartfix.yml.template):
44+
SmartFix is configured as a GitHub Action. Add a workflow file (e.g., `.github/workflows/smartfix.yml`) to your repository following the below example. A full workflow example is also available at [https://github.com/Contrast-Security-OSS/contrast-ai-smartfix-action/blob/main/contrast-ai-smartfix.yml.template](https://github.com/Contrast-Security-OSS/contrast-ai-smartfix-action/blob/main/contrast-ai-smartfix.yml.template).
45+
46+
The following is sample workflow file for using the SmartFix Coding Agent. (An example variation for the GitHub Coding Agent is next, below):
3047

3148
```
3249
name: Contrast AI SmartFix
@@ -104,7 +121,7 @@ jobs:
104121
handle_pr_merge:
105122
name: Handle PR Merge
106123
runs-on: ubuntu-latest
107-
if: github.event.pull_request.merged == true && contains(github.event.pull_request.head.ref, 'smartfix/remediation-')
124+
if: github.event.pull_request.merged == true && contains(join(github.event.pull_request.labels.*.name), 'contrast-vuln-id:VULN-')
108125
steps:
109126
- name: Checkout repository
110127
uses: actions/checkout@v4
@@ -129,7 +146,7 @@ jobs:
129146
handle_pr_closed:
130147
name: Handle PR Close
131148
runs-on: ubuntu-latest
132-
if: github.event.pull_request.merged == false && contains(github.event.pull_request.head.ref, 'smartfix/remediation-')
149+
if: github.event.pull_request.merged == false && contains(join(github.event.pull_request.labels.*.name), 'contrast-vuln-id:VULN-')
133150
steps:
134151
- name: Checkout repository
135152
uses: actions/checkout@v4
@@ -161,9 +178,53 @@ jobs:
161178
* The optional `formatting_command` will be run after SmartFix makes code changes to resolve the vulnerability and prior to any subsequent `build_command` invocations. We recommend supplying a `formatting_command` to fix code style issues in your project as it is an easy way to correct a common class of build-breaking problems.
162179
* **Suggestion:** Setup an API-only service user named “Contrast AI SmartFix” in your Organization Settings in your Contrast SaaS instance. At a minimum, it should have the “View Organization” permission and “Edit Application” permission for this application. This service user’s `contrast_authorization_key` value and the Organization’s `contrast_api_key` value should be used in the workflow.
163180

164-
### Supported LLMs (Bring Your Own LLM \- BYOLLM)
181+
### Installation and Configuration for GitHub Copilot Coding Agent
182+
183+
This is an example variation of the workflow file for use with the GitHub Copilot Coding Agent:
184+
```
185+
# The beginning of the workflow file is the same as the previous example.
186+
187+
# Use a variation of this 'generate_fixes' job in order to run with the GitHub Copilot Coding Agent
188+
generate_fixes:
189+
name: Generate Fixes
190+
runs-on: ubuntu-latest
191+
if: github.event_name == 'workflow_dispatch' || github.event_name == 'schedule'
192+
steps:
193+
# When using GitHub Copilot, it is unnecessary to authenticate with an LLM API
194+
195+
- name: Checkout repository
196+
uses: actions/checkout@v4
197+
with:
198+
fetch-depth: 0
199+
200+
- name: Run Contrast AI SmartFix - Generate Fixes Action
201+
uses: Contrast-Security-OSS/contrast-ai-smartfix-action@v1 # Replace with the latest version
202+
with:
203+
# Contrast Configuration
204+
contrast_host: ${{ vars.CONTRAST_HOST }} # The host name of your Contrast SaaS instance, e.g. 'app.contrastsecurity.com'
205+
contrast_org_id: ${{ vars.CONTRAST_ORG_ID }} # The UUID of your Contrast organization
206+
contrast_app_id: ${{ vars.CONTRAST_APP_ID }} # The UUID that is specific to the application in this repository.
207+
contrast_authorization_key: ${{ secrets.CONTRAST_AUTHORIZATION_KEY }}
208+
contrast_api_key: ${{ secrets.CONTRAST_API_KEY }}
209+
210+
# GitHub Configuration
211+
github_token: ${{ secrets.PAT_TOKEN }} # Necessary for creating Issues and assigning to Copilot. This token should have read permission for metadata and read-write permission for issues and pulls. A best practice is to have an GitHub Organization service account create the PAT (an Organization admin may need to approve it)
212+
base_branch: '${{ github.event.repository.default_branch }}' # This will default to your repo default branch (other common base branches are 'main', 'master' or 'develop')
213+
coding_agent: 'GITHUB_COPILOT' # Specify the use of GitHub Copilot instead of the default SmartFix internal coding agent
214+
215+
# Required Runtime Configuration
216+
build_command: 'mvn clean install' # Or the build command appropriate for your project. SmartFix will use this command to ensure that its changes work correctly with your project.
217+
218+
# Other Optional Inputs (see action.yml for defaults and more options)
219+
# formatting_command: 'mvn spotless:apply' # Or the command appropriate for your project to correct the formatting of SmartFix's changes. This ensures that SmartFix follows your coding standards.
220+
# max_open_prs: 5 # This is the maximum limit for the number of PRs that SmartFix will have open at single time
221+
222+
# The Closed and Merge Handler jobs remain the same as the previous example as well.
223+
```
224+
225+
### Supported LLMs (Bring Your Own LLM \- BYOLLM) for the SmartFix Coding Agent
165226

166-
For the Early Access release, SmartFix uses a "Bring Your Own LLM" (BYOLLM) model. You provide the credentials for your preferred LLM provider.
227+
SmartFix uses a "Bring Your Own LLM" (BYOLLM) model. You provide the credentials for your preferred LLM provider.
167228

168229
* **Recommended:** **Anthropic Claude Sonnet (e.g., Claude 3.7 Sonnet via AWS Bedrock or direct Anthropic API)**. This model has been extensively tested.
169230
* Option 1 - Direct Anthropic API:
@@ -180,7 +241,7 @@ For the Early Access release, SmartFix uses a "Bring Your Own LLM" (BYOLLM) mode
180241

181242
Refer to the `action.yml` file within the SmartFix GitHub Action repository and LiteLLM documentation for specific `agent_model` strings and required credentials for other models/providers. The LiteLLM documentation can be found at https://docs.litellm.ai/docs/providers/.
182243

183-
### Agent Model values
244+
### Agent Model Config Values
184245

185246
Here are several recommended `agent_model` values:
186247

@@ -190,7 +251,8 @@ Here are several recommended `agent_model` values:
190251

191252
### Supported Languages
192253

193-
* **Java, .NET, Go, Python, Node:** Java applications have received the most testing so far, but we have also had good results for .NET, Go, Python, and Node projects.
254+
* **Java, Python:** SmartFix regularly produces good fixes for a variety Java and Python testing projects (using a variety of project platforms such, as Maven, Gradle, Django, Flask, and WSGI). SmartFix officially supports Java and Python.
255+
* **.NET, Go, Node:** Java and Python applications have received the most testing so far, but we have also had good results for .NET, Go, and Node projects.
194256
* **Other Languages:** While it might work for other languages (such as Ruby, and PHP), comprehensive testing is in progress. Use with caution for non-Java projects.
195257

196258
### Supported GitHub Runners
@@ -239,6 +301,7 @@ SmartFix focuses on remediating:
239301

240302
## Key Features
241303

304+
* **Support for Multiple Coding Agents**: Choose to use either the internal SmartFix coding agent or GitHub Copilot to remediate your project's vulnerabilities
242305
* **Bring Your Own LLM (BYOLLM):** Flexibility to use your preferred LLM provider and model.
243306
* **Configurable PR Throttling:** Control the volume of automated PRs using `max_open_prs`.
244307
* **Build Command Integration:** You must provide a `build_command` to allow the agent to ensure changes can build. Ideally, this command will run the tests as well so the agent can ensure it doesn't break existing tests.

contrast-ai-smartfix.yml.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ jobs:
8989
handle_pr_merge:
9090
name: Handle PR Merge
9191
runs-on: ubuntu-latest
92-
if: github.event.pull_request.merged == true && contains(github.event.pull_request.head.ref, 'smartfix/remediation-')
92+
if: github.event.pull_request.merged == true && contains(join(github.event.pull_request.labels.*.name), 'contrast-vuln-id:VULN-')
9393
steps:
9494
- name: Checkout repository
9595
uses: actions/checkout@v4
@@ -114,7 +114,7 @@ jobs:
114114
handle_pr_closed:
115115
name: Handle PR Close
116116
runs-on: ubuntu-latest
117-
if: github.event.pull_request.merged == false && contains(github.event.pull_request.head.ref, 'smartfix/remediation-')
117+
if: github.event.pull_request.merged == false && contains(join(github.event.pull_request.labels.*.name), 'contrast-vuln-id:VULN-')
118118
steps:
119119
- name: Checkout repository
120120
uses: actions/checkout@v4

src/main.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,35 @@ def _patched_pipe_del(self):
9595
except (ImportError, AttributeError) as e:
9696
debug_log(f"Could not patch _ProactorBasePipeTransport: {str(e)}")
9797

98+
# Add a specific fix for BaseSubprocessTransport.__del__ on Windows
99+
try:
100+
from asyncio.base_subprocess import BaseSubprocessTransport
101+
102+
# Store the original __del__ method
103+
_original_subprocess_del = BaseSubprocessTransport.__del__
104+
105+
# Define a safe replacement for __del__
106+
def _patched_subprocess_del(self):
107+
try:
108+
# Check if the event loop is closed or finalizing
109+
if hasattr(self, '_loop') and self._loop is not None and (self._loop.is_closed() or sys.is_finalizing()):
110+
# Skip the original __del__ which would trigger the error
111+
return
112+
113+
# Otherwise use the original __del__ implementation
114+
_original_subprocess_del(self)
115+
except (AttributeError, RuntimeError, ImportError, TypeError, ValueError):
116+
# Catch and ignore all attribute, runtime, or value errors during shutdown
117+
# ValueError specifically handles "I/O operation on closed pipe"
118+
pass
119+
120+
# Apply the patch to the __del__ method
121+
BaseSubprocessTransport.__del__ = _patched_subprocess_del
122+
123+
debug_log("Successfully patched BaseSubprocessTransport.__del__ for Windows")
124+
except (ImportError, AttributeError) as e:
125+
debug_log(f"Could not patch BaseSubprocessTransport: {str(e)}")
126+
98127
def cleanup_asyncio():
99128
"""
100129
Cleanup function registered with atexit to properly handle asyncio resources during shutdown.

0 commit comments

Comments
 (0)