Skip to content

fix: only trust forwarded IP header from configured trusted proxies#493

Merged
cameri merged 3 commits intocameri:mainfrom
kanishka0411:fix/trusted-proxy-ip-spoofing-v2
Apr 20, 2026
Merged

fix: only trust forwarded IP header from configured trusted proxies#493
cameri merged 3 commits intocameri:mainfrom
kanishka0411:fix/trusted-proxy-ip-spoofing-v2

Conversation

@kanishka0411
Copy link
Copy Markdown
Contributor

Description

Added network.trustedProxies config option. The x-forwarded-for header (or any configured remoteIpHeader) is now only trusted when the request's socket IP is in the trustedProxies list. Otherwise Nostream falls back to the real socket address. Also added a runtime warning when remoteIpHeader is set but trustedProxies is empty.

Related Issue

Fixes #492

Motivation and Context

Previously any client could set x-forwarded-for to any IP and Nostream would blindly trust it. This allowed attackers to spoof their IP and bypass rate limits, IP blacklists, and payment throttling.

How Has This Been Tested?

Added unit tests for:

  • trusted proxy allows forwarded header
  • untrusted proxy falls back to socket address
  • IPv4-mapped proxy address normalization

Screenshots (if appropriate):

Types of changes

  • Non-functional change (docs, style, minor refactor)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my code changes.
  • All new and existing tests passed.

Comment thread src/utils/http.ts
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR mitigates IP spoofing by only trusting x-forwarded-for (or configured network.remoteIpHeader) when the connection comes from an allow-listed proxy IP (network.trustedProxies).

Changes:

  • Added network.trustedProxies setting and updated getRemoteAddress() to require a trusted proxy before using forwarded headers.
  • Added IP normalization for IPv4-mapped proxy socket addresses and improved forwarded-header parsing (trim).
  • Updated unit tests and configuration docs/default settings to include the new option.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/utils/http.ts Implements trusted-proxy gating + IP normalization and warning behavior for forwarded headers.
src/@types/settings.ts Adds trustedProxies?: string[] to the Network settings type.
resources/default-settings.yaml Documents and introduces network.trustedProxies in the default config.
CONFIGURATION.md Documents the new network.trustedProxies setting.
test/unit/utils/http.spec.ts Adds unit tests covering trusted/untrusted proxy behavior and IPv4-mapped normalization.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/utils/http.ts
Comment on lines +38 to +41
const trustedProxies = settings.network?.trustedProxies
if (header && (!Array.isArray(trustedProxies) || trustedProxies.length === 0)) {
console.warn('WARNING: network.remoteIpHeader is set but network.trustedProxies is empty. Forwarded headers will be ignored. Add your proxy IP to network.trustedProxies.')
}
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new warning for network.remoteIpHeader + empty network.trustedProxies runs inside getRemoteAddress(), which is called per request/connection and can spam logs (and add overhead) in misconfigured or default setups. Consider emitting this warning once (e.g., via a module-level "warned" flag) or moving validation to settings load/startup instead of the hot path.

Copilot uses AI. Check for mistakes.
Comment thread src/utils/http.ts Outdated
Comment thread resources/default-settings.yaml Outdated
@cameri
Copy link
Copy Markdown
Owner

cameri commented Apr 18, 2026

@kanishka0411 looks like we have some conflicts now. I'm wondering if we should re-order CONFIGURATION.md list of settings A-Z so there's less conflicts.

@kanishka0411 kanishka0411 force-pushed the fix/trusted-proxy-ip-spoofing-v2 branch from 6892bbd to 6ebff39 Compare April 18, 2026 16:43
@cameri
Copy link
Copy Markdown
Owner

cameri commented Apr 18, 2026

@copilot resolve the merge conflicts in this pull request

@Anshumancanrock Anshumancanrock mentioned this pull request Apr 18, 2026
10 tasks
@cameri
Copy link
Copy Markdown
Owner

cameri commented Apr 18, 2026

@copilot resolve the merge conflicts in this pull request

@kanishka0411 kanishka0411 force-pushed the fix/trusted-proxy-ip-spoofing-v2 branch from 6ebff39 to 0ac3cab Compare April 19, 2026 06:33
@coveralls
Copy link
Copy Markdown
Collaborator

coveralls commented Apr 19, 2026

Coverage Status

coverage: 73.323% (+0.2%) from 73.153% — kanishka0411:fix/trusted-proxy-ip-spoofing-v2 into cameri:main

@kanishka0411 kanishka0411 force-pushed the fix/trusted-proxy-ip-spoofing-v2 branch from 18bffa7 to c638687 Compare April 19, 2026 18:14
@kanishka0411 kanishka0411 force-pushed the fix/trusted-proxy-ip-spoofing-v2 branch from c638687 to 47afc07 Compare April 20, 2026 06:03
@cameri cameri merged commit 5bf1a58 into cameri:main Apr 20, 2026
13 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.

IP spoofing via untrusted x-forwarded-for header

4 participants