Skip to content

security(backend): Add comprehensive Git URL sanitization#89

Merged
DevanshuNEU merged 1 commit into
OpenCodeIntel:mainfrom
DevanshuNEU:fix/issue-57-git-url-sanitization
Dec 11, 2025
Merged

security(backend): Add comprehensive Git URL sanitization#89
DevanshuNEU merged 1 commit into
OpenCodeIntel:mainfrom
DevanshuNEU:fix/issue-57-git-url-sanitization

Conversation

@DevanshuNEU

Copy link
Copy Markdown
Collaborator

Summary

Adds comprehensive security validation to Git URL handling to prevent command injection and SSRF attacks.

Problem

The existing URL validation was insufficient:

  • No protection against shell metacharacters (; && || | \ $()`)
  • Allowed any host (potential data exfiltration to malicious git servers)
  • Incomplete private IP blocking (missed 10.x, 172.x, 192.168.x ranges)
  • No protection against AWS metadata endpoint (169.254.169.254)

Solution

Enhanced validate_git_url() with 6 security layers:

1. Command Injection Prevention

Blocks shell metacharacters before any URL parsing:

# Blocked patterns
; && || | ` $( ${ \n \r \x00

2. Host Allowlist

Only allows trusted Git hosting providers by default:

  • github.com
  • gitlab.com
  • bitbucket.org
  • codeberg.org
  • sr.ht (sourcehut)

Configurable via ALLOWED_GIT_HOSTS env var for self-hosted instances.

3. SSRF Prevention

Uses ipaddress module for proper CIDR checking:

  • 10.0.0.0/8 (Class A private)
  • 172.16.0.0/12 (Class B private)
  • 192.168.0.0/16 (Class C private)
  • 169.254.0.0/16 (Link-local / AWS metadata!)
  • Loopback, multicast, reserved ranges

4. URL Format Validation

Strict regex patterns for both HTTPS and SSH URLs.

5. DNS Resolution Check

Validates resolved IP isn't private (catches DNS rebinding attacks).

6. Improved SSH URL Handling

Proper parsing and validation of git@host:user/repo.git format.

Test Coverage

36 comprehensive test cases organized by attack vector:

  • TestCommandInjectionPrevention (9 tests)
  • TestSSRFPrevention (7 tests)
  • TestHostAllowlist (3 tests)
  • TestUrlFormatValidation (4 tests)
  • Plus existing path/query/name validation tests

Files Changed

  • backend/services/input_validator.py - Enhanced validation logic
  • backend/tests/test_validation.py - Comprehensive security tests

Breaking Changes

⚠️ URLs from non-allowlisted hosts will now be rejected.

If using self-hosted Git servers, set:

ALLOWED_GIT_HOSTS=git.mycompany.com,internal-git.corp.com

Testing

cd backend && python3 -m pytest tests/test_validation.py -v
# 36 passed ✅

Closes #57

…tel#57)

SECURITY FIXES:
- Add command injection prevention (blocks ; && || | ` $() ${} etc.)
- Add host allowlist (github.com, gitlab.com, bitbucket.org by default)
- Add SSRF prevention for private IP ranges (10.x, 172.16-31.x, 192.168.x)
- Block AWS metadata endpoint (169.254.169.254)
- Add configurable ALLOWED_GIT_HOSTS env var for self-hosted instances

IMPLEMENTATION:
- Enhanced validate_git_url() with 6 security layers
- Uses ipaddress module for proper CIDR checking
- Validates URL format with strict regex patterns
- SSH URL validation improved

TESTS:
- 36 comprehensive test cases covering:
  - Command injection (9 attack vectors)
  - SSRF prevention (7 private IP ranges)
  - Host allowlist enforcement
  - URL format validation
  - Custom env var configuration

Closes OpenCodeIntel#57
@vercel

vercel Bot commented Dec 11, 2025

Copy link
Copy Markdown

@DevanshuNEU is attempting to deploy a commit to the Dev's projects Team on Vercel.

A member of the Team first needs to authorize it.

@vercel

vercel Bot commented Dec 11, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
opencodeintel Ignored Ignored Preview Dec 11, 2025 2:39am

@DevanshuNEU DevanshuNEU merged commit bf9ce3b into OpenCodeIntel:main Dec 11, 2025
6 checks passed
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.

security(backend): Sanitize git URLs to prevent command injection

1 participant