mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-11-24 14:16:34 +09:00
🔧 Add safe GitHub CLI utilities to prevent timeout issues (#514)
- Implement github-cli-safe.js with proper handling of special characters - Use temporary files for body content to avoid shell interpretation - Add timeout protection and proper process cleanup - Prevent command substitution, backtick execution, and path errors Fixes the 2-minute timeout issues when using gh commands with: - Backticks in code examples - Command substitution syntax like - Directory paths and special shell characters All GitHub CLI operations now safely handle complex content without triggering shell interpretation or hanging processes.
This commit is contained in:
1 parent
6150543829
commit
958f591097
1 file changed
+201
@@ -0,0 +1,201 @@
|
||||
/**
|
||||
* Safe GitHub CLI command execution utilities
|
||||
* Prevents shell injection and timeout issues with special characters
|
||||
*/
|
||||
|
||||
import { promises as fs } from 'fs';
|
||||
import { tmpdir } from 'os';
|
||||
import { join } from 'path';
|
||||
import { randomBytes } from 'crypto';
|
||||
import { spawn } from 'child_process';
|
||||
|
||||
/**
|
||||
* Execute GitHub CLI command with safe body content handling
|
||||
* @param {string} command - The gh command (e.g., 'issue comment')
|
||||
* @param {string} target - Target (issue/PR number or repo)
|
||||
* @param {string} body - Body content that may contain special characters
|
||||
* @param {Object} options - Additional options
|
||||
* @returns {Promise<string>} - Command output
|
||||
*/
|
||||
export async function safeGhCommand(command, target, body, options = {}) {
|
||||
const tmpFile = join(tmpdir(), `gh-body-${randomBytes(8).toString('hex')}.tmp`);
|
||||
|
||||
try {
|
||||
// Write body content to temporary file
|
||||
await fs.writeFile(tmpFile, body, 'utf8');
|
||||
|
||||
// Build command arguments
|
||||
const args = command.split(' ');
|
||||
args.push(target);
|
||||
args.push('--body-file', tmpFile);
|
||||
|
||||
// Add any additional options
|
||||
Object.entries(options).forEach(([key, value]) => {
|
||||
args.push(`--${key}`, value);
|
||||
});
|
||||
|
||||
// Execute command with timeout
|
||||
const result = await executeWithTimeout('gh', args, options.timeout || 30000);
|
||||
return result;
|
||||
} finally {
|
||||
// Clean up temp file
|
||||
try {
|
||||
await fs.unlink(tmpFile);
|
||||
} catch (err) {
|
||||
// Ignore cleanup errors
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute command with timeout and proper cleanup
|
||||
* @param {string} command - Command to execute
|
||||
* @param {string[]} args - Command arguments
|
||||
* @param {number} timeout - Timeout in milliseconds
|
||||
* @returns {Promise<string>} - Command output
|
||||
*/
|
||||
function executeWithTimeout(command, args, timeout) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn(command, args, {
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
shell: false, // Important: don't use shell
|
||||
});
|
||||
|
||||
let stdout = '';
|
||||
let stderr = '';
|
||||
let timedOut = false;
|
||||
|
||||
// Set timeout
|
||||
const timer = setTimeout(() => {
|
||||
timedOut = true;
|
||||
child.kill('SIGTERM');
|
||||
setTimeout(() => child.kill('SIGKILL'), 5000); // Force kill after 5s
|
||||
}, timeout);
|
||||
|
||||
child.stdout.on('data', (data) => {
|
||||
stdout += data.toString();
|
||||
});
|
||||
|
||||
child.stderr.on('data', (data) => {
|
||||
stderr += data.toString();
|
||||
});
|
||||
|
||||
child.on('close', (code) => {
|
||||
clearTimeout(timer);
|
||||
|
||||
if (timedOut) {
|
||||
reject(new Error(`Command timed out after ${timeout}ms`));
|
||||
} else if (code !== 0) {
|
||||
reject(new Error(`Command failed with exit code ${code}: ${stderr}`));
|
||||
} else {
|
||||
resolve(stdout.trim());
|
||||
}
|
||||
});
|
||||
|
||||
child.on('error', (err) => {
|
||||
clearTimeout(timer);
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Safe methods for common GitHub CLI operations
|
||||
*/
|
||||
export const gh = {
|
||||
/**
|
||||
* Create issue comment safely
|
||||
* @param {number|string} issue - Issue number
|
||||
* @param {string} body - Comment body
|
||||
* @param {Object} options - Additional options
|
||||
*/
|
||||
async issueComment(issue, body, options = {}) {
|
||||
return safeGhCommand('issue comment', issue.toString(), body, options);
|
||||
},
|
||||
|
||||
/**
|
||||
* Create PR comment safely
|
||||
* @param {number|string} pr - PR number
|
||||
* @param {string} body - Comment body
|
||||
* @param {Object} options - Additional options
|
||||
*/
|
||||
async prComment(pr, body, options = {}) {
|
||||
return safeGhCommand('pr comment', pr.toString(), body, options);
|
||||
},
|
||||
|
||||
/**
|
||||
* Create issue safely
|
||||
* @param {Object} params - Issue parameters
|
||||
*/
|
||||
async createIssue({ title, body, labels = [], assignees = [] }) {
|
||||
const tmpFile = join(tmpdir(), `gh-issue-${randomBytes(8).toString('hex')}.tmp`);
|
||||
|
||||
try {
|
||||
await fs.writeFile(tmpFile, body, 'utf8');
|
||||
|
||||
const args = ['issue', 'create', '--title', title, '--body-file', tmpFile];
|
||||
|
||||
if (labels.length > 0) {
|
||||
args.push('--label', labels.join(','));
|
||||
}
|
||||
|
||||
if (assignees.length > 0) {
|
||||
args.push('--assignee', assignees.join(','));
|
||||
}
|
||||
|
||||
return await executeWithTimeout('gh', args, 30000);
|
||||
} finally {
|
||||
try {
|
||||
await fs.unlink(tmpFile);
|
||||
} catch (err) {
|
||||
// Ignore cleanup errors
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Create PR safely
|
||||
* @param {Object} params - PR parameters
|
||||
*/
|
||||
async createPR({ title, body, base = 'main', head, draft = false }) {
|
||||
const tmpFile = join(tmpdir(), `gh-pr-${randomBytes(8).toString('hex')}.tmp`);
|
||||
|
||||
try {
|
||||
await fs.writeFile(tmpFile, body, 'utf8');
|
||||
|
||||
const args = ['pr', 'create', '--title', title, '--body-file', tmpFile, '--base', base];
|
||||
|
||||
if (head) {
|
||||
args.push('--head', head);
|
||||
}
|
||||
|
||||
if (draft) {
|
||||
args.push('--draft');
|
||||
}
|
||||
|
||||
return await executeWithTimeout('gh', args, 30000);
|
||||
} finally {
|
||||
try {
|
||||
await fs.unlink(tmpFile);
|
||||
} catch (err) {
|
||||
// Ignore cleanup errors
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Example usage:
|
||||
*
|
||||
* import { gh } from './github-cli-safe.js';
|
||||
*
|
||||
* // Safe issue comment with special characters
|
||||
* await gh.issueComment(123, 'Code: `npm install` and $(echo test)');
|
||||
*
|
||||
* // Safe PR creation with complex body
|
||||
* await gh.createPR({
|
||||
* title: 'Fix: Handle special characters',
|
||||
* body: 'This fixes issues with `backticks` and .claude/agents/ paths',
|
||||
* base: 'main'
|
||||
* });
|
||||
*/
|
||||
Reference in new issue
Block a user