Skip to content

456 x forwarded for header#178

Merged
Stell0 merged 5 commits into
mainfrom
456-x-forwarded-for-header
May 29, 2026
Merged

456 x forwarded for header#178
Stell0 merged 5 commits into
mainfrom
456-x-forwarded-for-header

Conversation

@paolovisintin

@paolovisintin paolovisintin commented May 26, 2026

Copy link
Copy Markdown
Collaborator

Summary

Adds the X-Forwarded-For header to every initial INVITE forwarded downstream to Asterisk, valued with the source IP of the upstream peer (typically the provider). This lets the NethVoice PBX identify IP-based trunks via a match header rule on X-Forwarded-For even when the INVITE reaches Asterisk from the proxy IP instead of directly from the provider.

Refs helpdesk ticket #456.

Changes

  • New route ADD_X_FORWARDED_FOR replaces the dead ADD_X_REAL_IP route (header switched from X-Real-IP to X-Forwarded-For, removed trailing space, removes existing header before inserting to avoid stacking).
  • Invoked in two places, both guarded by is_method("INVITE") && !has_totag() so only initial INVITEs are tagged (no re-INVITEs, no other methods):
    • Main inbound flow in request_route (direction=in), right before RELAY.
    • Inside HANDLE_ALIAS, before the shortcut RELAY. This was needed because handle_ruri_alias() bypasses the main flow whenever the RURI carries an ;alias= parameter — i.e. virtually all real inbound INVITEs going to a registered peer.
  • New Robot test suite tests/02_kamailio_routing.robot with 5 regression assertions on the running config inside the container.

Test plan

Verified end-to-end on lab GNS3 (Vianova FreePBX provider → proxy → NethVoice Asterisk) — pcap evidence available in the helpdesk ticket. Coverage:

  • Inbound provider→PBX with ;alias= (HANDLE_ALIAS path) — INVITE reaching Asterisk carries X-Forwarded-For: <provider-ip>.
  • Outbound PBX→provider — INVITE leaving the proxy does NOT carry the header (correct: direction=out doesn't hit the route).
  • In-dialog requests (ACK with To-tag observed) — not tagged. Same codepath as re-INVITEs (WITHINDLG → loose_route → RELAY bypasses both main flow and HANDLE_ALIAS).
  • Robot tests: 5/5 PASS against lab node.
  • Main flow path without ;alias= parameter — not exercised at runtime (all registered Contacts in lab carry ;alias=); covered by symmetric code path and the Robot static assertions.
  • Re-INVITE in-dialog — not exercised at runtime (no responding softphone in lab to trigger hold/session-refresh); covered by ACK observation and RFC 3261 guarantee that re-INVITEs carry To-tag.

Asterisk match header configuration is out of scope (Nethesis side).

NethServer/dev#7138

Replace dead ADD_X_REAL_IP route with ADD_X_FORWARDED_FOR and wire it into
request_route so every initial INVITE forwarded downstream carries the
original source IP. Lets Asterisk identify IP-based trunks via 'match
header' when the INVITE reaches it from the proxy instead of the provider.

- Renamed route ADD_X_REAL_IP -> ADD_X_FORWARDED_FOR; switched header
  from X-Real-IP to X-Forwarded-For (no trailing space)
- Invoked in request_route for direction=in INVITE with !has_totag()
  (initial dialog only, no re-INVITEs)

Refs helpdesk ticket #456
The route HANDLE_ALIAS shortcuts to RELAY when handle_ruri_alias()
rewrites the RURI (any INVITE with ;alias= param from the provider).
That shortcut skipped the main flow where the X-Forwarded-For header
was being added, so the downstream Asterisk never saw the header.

Verified on lab GNS3 (Vianova FreePBX -> proxy -> NethVoice Asterisk):
the INVITE forwarded to Asterisk now carries 'X-Forwarded-For: <provider-ip>'.
Adds 02_kamailio_routing.robot with 5 regression assertions on the
running kamailio config inside the container:

- route[ADD_X_FORWARDED_FOR] is defined
- it removes existing X-Forwarded-For and inserts one with $si
- route(ADD_X_FORWARDED_FOR) is wired in at >= 2 sites
  (main inbound flow + HANDLE_ALIAS shortcut)
- HANDLE_ALIAS body contains the call (prevents the alias-rewrite
  shortcut from silently skipping the header)
- kamcmd answers (running config parsed cleanly)

Verified against the lab NS8 node: 5/5 passing.
@github-actions

Copy link
Copy Markdown

🔨 PR testing image: ghcr.io/nethesis/nethvoice-proxy:pr-178.3-1.c2859cf

@github-actions

Copy link
Copy Markdown

🔨 PR testing image: ghcr.io/nethesis/nethvoice-proxy:pr-178.6-1.8a77df3

@github-actions

Copy link
Copy Markdown

🔨 PR testing image: ghcr.io/nethesis/nethvoice-proxy:pr-178.10-1.53f51e6

@Stell0 Stell0 merged commit b00a3cd into main May 29, 2026
10 of 12 checks passed
@Stell0 Stell0 deleted the 456-x-forwarded-for-header branch May 29, 2026 07:17
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.

4 participants