Skip to main content

Overview

The Patching API provides intelligent vulnerability remediation capabilities. It analyzes security findings, generates appropriate code fixes, verifies the patches through testing, and prepares pull request metadata. Key Features:
  • Automated patch generation for security vulnerabilities
  • Code analysis and dataflow understanding
  • Lint, type-check, and test verification
  • Sandbox support for isolated patching
  • Pull request metadata generation
  • Multiple file change support

runPatchingAgent

Run the patching agent to fix a security vulnerability in a codebase. Patching Workflow:
  1. Analyze: Read and understand the vulnerability details
  2. Locate: Find the vulnerable code in the codebase
  3. Research: Understand the context and dataflow
  4. Patch: Generate and apply appropriate fixes
  5. Verify: Run lints, type-checks, and tests
  6. Document: Prepare PR metadata with changes
import { runPatchingAgent } from '@pensar/apex/api/patching';

const result = await runPatchingAgent({
  cwd: '/path/to/project',
  vulnerability: {
    name: 'SQL Injection in User Search',
    severity: 'HIGH',
    description: 'User input is directly concatenated into SQL query',
    location: 'src/controllers/userController.ts',
    startLineNumber: 42,
    endLineNumber: 45,
  },
  model: 'claude-sonnet-4-20250514',
  session: sessionInfo,
});

console.log(`Patched ${result.filesChanged.length} files`);
console.log(`PR Title: ${result.prTitle}`);

Parameters

input
RunPatchingAgentInput
required
Configuration for the patching agent

Response

filesChanged
FileChange[]
List of all files that were modified during patching
prTitle
string
Suggested title for the pull request
prDescription
string
Detailed description for the pull request, including:
  • Summary of the vulnerability
  • Changes made
  • Testing performed
  • Remediation approach

Usage Examples

import { runPatchingAgent } from '@pensar/apex/api/patching';
import { createSession } from '@pensar/apex/session';

const session = await createSession({
  name: 'Patch SQLi',
  targets: [],
});

const result = await runPatchingAgent({
  cwd: '/home/user/projects/my-app',
  vulnerability: {
    name: 'SQL Injection in User Search',
    severity: 'HIGH',
    description: 'User input from search parameter is directly ' +
                 'concatenated into SQL query without sanitization',
    location: 'src/controllers/userController.ts',
    startLineNumber: 42,
    endLineNumber: 45,
    cweMapping: ['CWE-89'],
  },
  model: 'claude-sonnet-4-20250514',
  session,
});

console.log(`Patched ${result.filesChanged.length} files:`);
result.filesChanged.forEach((file) => {
  console.log(`  ${file.filePath}: ${file.changesDescription}`);
});

console.log(`\nPR Title: ${result.prTitle}`);
console.log(`\nPR Description:\n${result.prDescription}`);

Patching Capabilities

Code Analysis

The patching agent can:
  • Read and understand complex codebases
  • Trace dataflow through functions and modules
  • Identify input validation gaps
  • Understand framework-specific patterns
  • Analyze dependencies and imports

Patch Generation

Supported remediation patterns:
  • Input validation: Add sanitization and validation
  • Parameterized queries: Convert to prepared statements
  • Output encoding: Add HTML/URL/SQL escaping
  • Access control: Add authorization checks
  • Cryptographic fixes: Upgrade weak algorithms
  • Configuration hardening: Fix insecure defaults

Verification

After patching, the agent:
  1. Runs linters to ensure code quality
  2. Executes type checkers (TypeScript, mypy, etc.)
  3. Runs existing test suites
  4. Verifies the vulnerability is resolved
  5. Ensures no regressions introduced

Best Practices

Provide Detailed Context

More context leads to better patches:
  • Include precise line numbers
  • Provide CWE mappings
  • Include PoC scripts when available
  • Add dataflow analysis if available

Review Generated Patches

Always review patches before merging:
  • Verify the fix addresses the root cause
  • Check for edge cases
  • Ensure coding standards are met
  • Validate test coverage

Use Sandboxes for Safety

For untrusted codebases:
  • Always use sandbox isolation
  • Never patch production code directly
  • Test patches in staging first

Blackbox Pentest

Discover vulnerabilities to patch

Targeted Pentest

Find specific vulnerabilities

Build docs developers (and LLMs) love