# How code-review-graph Prevents DNS Rebinding Attacks Using HTTP Transport Security

> Discover how code-review-graph stops DNS rebinding attacks by validating HTTP transport security and enforcing strict origin headers for localhost requests only.

- Repository: [Tirth Kanani/code-review-graph](https://github.com/tirth8205/code-review-graph)
- Tags: how-to-guide
- Published: 2026-08-07

---

**code-review-graph prevents DNS rebinding attacks by enforcing strict Origin header validation in its HTTP transport middleware, allowing only requests from localhost, 127.0.0.1, or ::1 with matching ports while rejecting all external domain origins.**

The code-review-graph repository implements a hardened HTTP transport layer to protect local MCP servers from DNS rebinding attacks that could allow malicious websites to interact with privileged local endpoints. By validating every incoming request against a whitelist of loopback addresses and enforcing exact port matching, the system ensures that only legitimate same-origin traffic reaches the server.

## The HTTP Origin Guard Middleware

The core protection mechanism resides in [`code_review_graph/http_origin_guard.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/http_origin_guard.py), which exports the `build_http_middleware` function. When the server initializes with the streamable HTTP transport, this middleware wraps the application to intercept and validate every request carrying an **Origin** header.

### Strict Loopback Hostname Validation

The middleware maintains an explicit whitelist of allowed hostnames: **`localhost`**, **`127.0.0.1`**, and **`[::1]`** (IPv6 loopback). When parsing the Origin header, the implementation extracts the hostname and verifies it against this set. Any request originating from a non-loopback domain—such as `evil.example` resolved to 127.0.0.1 via DNS rebinding—immediately fails validation and receives a **403 Forbidden** response.

### Port Binding Verification

Beyond hostname checks, the middleware enforces that the port specified in the Origin header must match the server's configured listening port exactly. The `build_http_middleware` function accepts `host` and `port` parameters from the server initialization, storing the expected port for comparison. This prevents attackers from bypassing origin checks through port manipulation or cross-port request forgery.

### Scheme Enforcement

The validation logic restricts acceptable protocols to **`http`** and **`https`** only. Requests carrying non-HTTP schemes (such as `file://` or `chrome-extension://`) are rejected at the middleware layer, eliminating alternative attack vectors that bypass standard web security policies.

## Integration with the Streamable HTTP Transport

The protection activates when launching the server via `crg_main.main()` with the `transport="streamable-http"` parameter. The entrypoint in [`code_review_graph/crg_main.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/crg_main.py) instantiates the middleware using the same `host` and `port` arguments passed to the HTTP server, ensuring consistent security policy enforcement across the transport layer.

```python

# Starting the server with HTTP transport security enabled

from code_review_graph import crg_main

crg_main.main(
    transport="streamable-http",
    host="127.0.0.1",
    port=8080
)

```

The middleware implementation performs the following validation sequence for each request:

```python

# Simplified logic from code_review_graph/http_origin_guard.py

def build_http_middleware(host: str, port: int):
    allowed_hosts = {"localhost", "127.0.0.1", "[::1]"}
    
    def middleware(request, call_next):
        origin = request.headers.get("origin")
        if origin:
            scheme, hostname, origin_port = _parse_origin(origin)
            
            if scheme not in {"http", "https"}:
                raise HTTPException(status_code=403)
            
            if hostname not in allowed_hosts:
                raise HTTPException(status_code=403)
            
            if origin_port != port:
                raise HTTPException(status_code=403)
        
        return call_next(request)
    
    return middleware

```

## Security Verification Through Testing

The effectiveness of this DNS rebinding protection is verified by the test suite in [`tests/test_http_origin_guard.py`](https://github.com/tirth8205/code-review-graph/blob/main/tests/test_http_origin_guard.py). These tests confirm that:

- Foreign origins over both HTTP and HTTPS are rejected with 403 status codes
- Same-origin requests from `http://localhost:<port>` are accepted
- Requests from loopback addresses with mismatched ports are blocked
- Non-HTTP schemes trigger immediate rejection

## Summary

- **code-review-graph** prevents DNS rebinding through a whitelist-based Origin header validation system implemented in `build_http_middleware`.
- The middleware explicitly permits only **localhost**, **127.0.0.1**, and **[::1]** hostnames, rejecting any external domain regardless of DNS resolution.
- **Port matching** ensures requests target the specific server instance, blocking cross-port attack vectors.
- The protection activates automatically when using `transport="streamable-http"` in `crg_main.main()`.
- Comprehensive tests in [`tests/test_http_origin_guard.py`](https://github.com/tirth8205/code-review-graph/blob/main/tests/test_http_origin_guard.py) verify that malicious origins receive 403 Forbidden responses while legitimate loopback traffic proceeds.

## Frequently Asked Questions

### What is a DNS rebinding attack and why does code-review-graph need protection?

A DNS rebinding attack occurs when a malicious website manipulates DNS records to resolve a domain to a victim's local IP address (such as 127.0.0.1), allowing the attacker's JavaScript to bypass same-origin policies and interact with local services. code-review-graph implements HTTP transport security to prevent attackers from exploiting the MCP server running on localhost, ensuring that even if DNS resolution is poisoned, the Origin header validation blocks the malicious request.

### Which hostnames does the middleware explicitly allow?

The `build_http_middleware` function in [`code_review_graph/http_origin_guard.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/http_origin_guard.py) maintains a strict whitelist containing only three entries: **`localhost`**, **`127.0.0.1`**, and **`[::1]`** (the IPv6 loopback address). Any request presenting an Origin header with a different hostname—including domains that resolve to 127.0.0.1 via DNS rebinding—receives an immediate 403 Forbidden response.

### How does the port validation mechanism enhance security?

The middleware compares the port specified in the incoming Origin header against the server's configured listening port passed to `build_http_middleware`. If the ports do not match exactly, the request is rejected. This prevents scenarios where an attacker might exploit a local service running on a different port or attempt cross-port request forgery attacks against the HTTP transport layer.

### Where can I verify the DNS rebinding protection implementation?

The protection logic is implemented in [`code_review_graph/http_origin_guard.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/http_origin_guard.py), specifically within the `build_http_middleware` function. The corresponding test suite in [`tests/test_http_origin_guard.py`](https://github.com/tirth8205/code-review-graph/blob/main/tests/test_http_origin_guard.py) provides executable verification that foreign origins are rejected and loopback origins are accepted, demonstrating the defense against DNS rebinding attacks.