Files
yoshiko-pg__difit/docs/security.md
yoshiko d590ab2c7a Add image diff display support (#30)
* feat: Add image diff display support for inline and side-by-side layouts

- Add image file detection utility in imageUtils.ts
- Implement ImageDiffChunk component with before/after image display
- Support both inline (vertical) and side-by-side (horizontal) layouts
- Add /api/blob/* endpoint for serving image files from git refs
- Update GitDiffParser to handle binary files and serve blob content
- Integrate image diff display in DiffViewer component
- Handle deleted, added, and modified image files appropriately

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* 画像の表示

* added/deleted対応

* 背景透過対応

* サイズの表記を追加

* 表示位置修正

* ファイルサイズ上限設定10MB

* staged/.対応

* create tests

* Fix command injection vulnerability in getBlobContent

依頼されたプロンプト内容:
execやexecSyncなど任意コード実行のところで、自由な入力を実行しているなど危険な実装がないかレビューしてほしい

実行したTODOリスト:
1. getBlobContent関数のセキュリティ問題を修正
2. 修正後のテストを実行
3. lint/typecheckを実行

Changes:
- Replace execSync with execFileSync to prevent command injection
- Update tests to use execFileSync mock instead of execSync
- Remove unused mockExecSync variable

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Update SECURITY_ANALYSIS.md to reflect execFileSync fix

- Mark command injection vulnerability as FIXED
- Update code examples to show execFileSync usage
- Update risk assessment from MEDIUM to LOW
- Document completed security improvements

* update document name

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-07-07 00:19:29 +09:00

5.0 KiB

Security Analysis Report - Code Execution Patterns

Summary

I've analyzed the codebase for potentially dangerous code execution patterns. Overall, the code appears to follow secure practices with proper input validation and sanitization. Here are the findings:

Code Execution Patterns Found

1. execSync Usage (src/cli/utils.ts)

Location: Lines 93-94, 138-145

// Getting GitHub token
const result = execSync('gh auth token', { encoding: 'utf8', stdio: 'pipe' });

// Verifying commit existence
execSync(`git cat-file -e ${sha}`, { stdio: 'ignore' });
execSync('git fetch origin', { stdio: 'ignore' });

Security Assessment: LOW RISK

  • The sha parameter is validated before use
  • Commands are using fixed patterns with minimal user input
  • The SHA is already verified to exist in the git repository

2. execFileSync Usage (src/server/git-diff.ts) - FIXED

Location: Lines 224-241

// Handle staged files
const buffer = execFileSync('git', ['show', `:${filepath}`], {
  maxBuffer: 10 * 1024 * 1024, // 10MB limit
});

// Get blob hash
const blobHash = execFileSync('git', ['rev-parse', `${ref}:${filepath}`], {
  encoding: 'utf8',
  maxBuffer: 10 * 1024 * 1024,
}).trim();

// Get raw binary content
const buffer = execFileSync('git', ['cat-file', 'blob', blobHash], {
  maxBuffer: 10 * 1024 * 1024, // 10MB limit
});

Security Assessment: LOW RISK (Previously MEDIUM RISK)

  • FIXED: Changed from execSync to execFileSync to prevent command injection
  • Arguments are now passed as an array, preventing shell interpretation
  • The filepath and ref parameters are safely passed without shell expansion
  • Resource limits (10MB) remain in place to prevent exhaustion attacks
  • No shell metacharacter interpretation occurs with execFileSync

3. spawn Usage (scripts/dev.js)

Location: Lines 10-14, 29-32

const cliProcess = spawn('pnpm', ['run', 'dev:cli', commitish, '--no-open'], {
  stdio: ['inherit', 'pipe', 'inherit'],
  shell: true,
});

viteProcess = spawn('vite', ['--open'], {
  stdio: 'inherit',
  shell: true,
});

Security Assessment: LOW RISK

  • This is a development script, not production code
  • The commitish parameter comes from command line but is passed as an array element, not interpolated

4. child_process Import (src/server/git-diff.ts)

Location: Line 224

const { execFileSync } = await import('child_process');

Security Assessment: EXPECTED

  • Dynamic import is used but the module name is hardcoded
  • This is the standard Node.js module for executing commands
  • Now imports execFileSync instead of execSync for better security

Input Validation Analysis

Positive Findings:

  1. Commit-ish Validation (src/cli/utils.ts)

    • Comprehensive validation function validateCommitish() that checks input against safe patterns
    • Rejects potentially dangerous inputs like empty strings or HEAD~
    • Uses regex patterns to validate SHA hashes, branch names, and special references
  2. GitHub PR URL Validation (src/cli/utils.ts)

    • parseGitHubPrUrl() properly validates URL format
    • Ensures hostname is github.com
    • Validates pull request number is numeric
  3. Diff Arguments Validation (src/cli/utils.ts)

    • validateDiffArguments() ensures proper argument combinations
    • Prevents comparing same values
    • Restricts special arguments to specific positions
  4. Express Route Parameters

    • File paths in /api/blob/ endpoint are captured but used within git commands that have their own validation

Recommendations

Completed:

  1. [FIXED] Command injection vulnerability in getBlobContent():
    • Changed from execSync to execFileSync
    • Arguments are now safely passed as an array
    • Shell metacharacter injection is no longer possible

Medium Priority:

  1. Consider using git library methods instead of direct command execution where possible
  2. Add explicit validation for the ref parameter in getBlobContent() as an additional defense layer

Low Priority:

  1. Document security considerations for developers about input validation requirements
  2. Consider adding rate limiting to prevent resource exhaustion attacks

Conclusion

The codebase demonstrates good security practices with proper input validation. The previously identified command injection vulnerability in getBlobContent() has been fixed by switching from execSync to execFileSync. The remaining risks are low because:

  1. The application is designed to run locally, not as a public web service
  2. Most user inputs go through validation functions
  3. The commands executed are limited to git operations within the repository context
  4. Resource limits are in place (10MB file size limit)
  5. [FIXED] Shell command injection is no longer possible with the use of execFileSync

The code does not use dangerous patterns like eval(), Function() constructor, or dynamic require() with user input.