ComfyUI icon indicating copy to clipboard operation
ComfyUI copied to clipboard

fix(security): harden origin middleware against DNS rebinding attacks (#12860)

Open jiangyj545 opened this issue 6 months ago • 2 comments

Summary

Fixes #12860 — The create_origin_only_middleware in server.py is vulnerable to DNS rebinding attacks. An attacker-controlled domain that rebinds to 127.0.0.1 can bypass the existing Host/Origin check because both headers will contain the attacker's domain (which matches), while the actual TCP connection goes to localhost.

Root Cause

The current middleware only checks that Host == Origin for loopback addresses. In a DNS rebinding scenario:

  1. Browser resolves attacker.com → 127.0.0.1 (rebound)
  2. Request sent to http://attacker.com:8188/prompt with Host and Origin both set to attacker.com:8188
  3. Host == Origin check passes → request processed
  4. Attacker can now queue arbitrary ComfyUI workflows

Reference: huntr.com/bounties/f1458e43-64a7-4df2-b71c-9ca453755dc7 (NCC Group PoC)

Changes

1. CSRF Token (per-process, auto-generated)

  • Random uuid4().hex token generated once at first request
  • No config needed, no database, fully backward-compatible

2. Double-DNS-Resolution Check (_dns_rebind_check)

  • Resolves the Host header hostname twice with a 200ms delay
  • If the two IP sets differ → reject (DNS rebinding detected)
  • If any resolved IP is non-loopback → reject
  • Classic defense-in-depth technique (same approach as Chrome Local Network Access)

3. CSRF Validation for State-Changing Requests

  • POST/PUT/DELETE/PATCH requests from loopback origins must include the CSRF token
  • Accepted via X-CSRF-Token header or ?csrf_token= query parameter
  • Returns clear 403 if missing/invalid

4. Token Delivery

  • Every response includes Set-Cookie: comfyui_csrf= (SameSite=Strict, 7-day)
  • Also exposes X-CSRF-Token response header for API/non-browser clients

Backward Compatibility

  • GET/HEAD/OPTIONS: unchanged (no CSRF check for read-only)
  • Non-loopback requests: unchanged (existing external access preserved)
  • Same-origin UI: fully automatic (browser sends cookie transparently)
  • API scripts: read X-CSRF-Token from response header, include in subsequent requests
  • No new dependencies (stdlib only: uuid, socket, ipaddress, time)

Testing Scenarios

Scenario Before After
Normal same-origin UI use works works (auto cookie)
API script with CSRF header works works
DNS rebind attack (cross-site) VULNERABLE 403 blocked
CSRF without DNS rebind VULNERABLE 403 blocked
Cross-site via Sec-Fetch-Site 403 blocked 403 blocked (unchanged)
Host/Origin mismatch 403 blocked 403 blocked (unchanged)

jiangyj545 avatar May 24 '26 04:05 jiangyj545

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds CSRF token and DNS rebinding defense mechanisms to the loopback-origin middleware in server.py. It introduces a per-process CSRF token (_csrf_token) lazily initialized via get_csrf_token(), and a _dns_rebind_check() function that resolves a hostname twice to detect DNS rebinding attacks. The middleware now validates CSRF tokens supplied via X-CSRF-Token header or csrf_token query parameter for state-changing requests (POST/PUT/DELETE/PATCH) from loopback origins, returning 403 on detection of DNS rebinding or invalid tokens. All requests processed by the middleware receive the CSRF token via both comfyui_csrf cookie and X-CSRF-Token response header.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main security improvement: hardening the origin middleware against DNS rebinding attacks, which is the primary objective of the changeset.
Description check ✅ Passed The description is comprehensive and directly related to the changeset, detailing the vulnerability, root cause, implementation approach, backward compatibility considerations, and testing scenarios.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar May 24 '26 04:05 coderabbitai[bot]

Can you tell me which browser is vulnerable to this?

comfyanonymous avatar May 24 '26 22:05 comfyanonymous