Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Use nginx resolved X-Real-IP instead of X-Forwarded-For (#2325)

Leendert de Borst70d33a9f7021
diff --git a/apps/server/Utilities/AliasVault.Auth/IpAddress/IpAddressUtility.cs b/apps/server/Utilities/AliasVault.Auth/IpAddress/IpAddressUtility.cs
index c08f4a13f..8b14dd106 100644
--- a/apps/server/Utilities/AliasVault.Auth/IpAddress/IpAddressUtility.cs
+++ b/apps/server/Utilities/AliasVault.Auth/IpAddress/IpAddressUtility.cs
@@ -11,7 +11,7 @@ using System.Net;
using Microsoft.AspNetCore.Http;
/// <summary>
-/// Ip address utility class to extract IP address from HttpContext.
+/// Ip address utility class to extract the client IP address from HttpContext.
/// </summary>
public static class IpAddressUtility
{
@@ -20,6 +20,11 @@ public static class IpAddressUtility
/// </summary>
public const string AnonymizedIp = "xxx.xxx.xxx.xxx";
+ /// <summary>
+ /// The header which contains the resolved client IP address. This is set by nginx (which ships with AliasVault).
+ /// </summary>
+ private const string RealIpHeader = "X-Real-IP";
+
/// <summary>
/// Extracts the anonymized IP address (IPv4 last octet masked) from the HttpContext for persistence/logging.
/// </summary>
@@ -38,7 +43,7 @@ public static class IpAddressUtility
return string.Empty;
}
- var ipAddress = ExtractRawIpString(httpContext) ?? "0.0.0.0";
+ var ipAddress = ExtractIpAddress(httpContext)?.ToString() ?? "0.0.0.0";
// Anonymize the last octet of the IP address (IPv4 only).
if (ipAddress.Contains('.'))
@@ -71,20 +76,39 @@ public static class IpAddressUtility
return null;
}
- return IPAddress.TryParse(ExtractRawIpString(httpContext), out var parsed) ? parsed : null;
+ return ExtractIpAddress(httpContext);
+ }
+
+ /// <summary>
+ /// Resolves the client IP address for the request.
+ /// </summary>
+ /// <param name="httpContext">HttpContext to extract the IP address from.</param>
+ /// <returns>The parsed IP address, or null when it cannot be determined.</returns>
+ private static IPAddress? ExtractIpAddress(HttpContext httpContext)
+ {
+ if (!IPAddress.TryParse(ExtractRawIpString(httpContext), out var parsed))
+ {
+ return null;
+ }
+
+ return IpRangeUtility.NormalizeAddress(parsed);
}
/// <summary>
- /// Extracts the raw IP address string from the request, honoring the X-Forwarded-For header (first entry) when
- /// present and otherwise falling back to the connection's remote IP address.
+ /// Extracts the raw IP address string from the X-Real-IP header set by nginx, falling back to the connection's
+ /// remote IP address when the header is not set (direct access in development and tests).
/// </summary>
/// <param name="httpContext">HttpContext to extract the IP address from.</param>
/// <returns>The raw IP address string, or null when it cannot be determined.</returns>
private static string? ExtractRawIpString(HttpContext httpContext)
{
- if (httpContext.Request.Headers.TryGetValue("X-Forwarded-For", out var xForwardedFor))
+ if (httpContext.Request.Headers.TryGetValue(RealIpHeader, out var realIp))
{
- return xForwardedFor.ToString().Split(',')[0].Trim();
+ var lastEntry = realIp.ToString().Split(',')[^1].Trim();
+ if (lastEntry.Length > 0)
+ {
+ return lastEntry;
+ }
}
return httpContext.Connection.RemoteIpAddress?.ToString();
diff --git a/apps/server/nginx-443.conf b/apps/server/nginx-443.conf
index f3a6bdeb2..96481fae7 100644
--- a/apps/server/nginx-443.conf
+++ b/apps/server/nginx-443.conf
@@ -21,9 +21,7 @@ http {
server admin:3002 max_fails=1 fail_timeout=5s;
}
- # Preserve any existing X-Forwarded-* headers, this is relevant if AliasVault
- # is running behind another reverse proxy. The set_real_ip_from directives
- # are rendered by entrypoint.sh from TRUSTED_PROXIES (default: RFC1918).
+ # Resolve the real client IP once, here, so the upstream apps don't have to.
__TRUSTED_PROXIES__
real_ip_header X-Forwarded-For;
real_ip_recursive on;
@@ -113,7 +111,7 @@ http {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto https;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Prefix /admin/;
# Rewrite HTTP redirects to HTTPS
@@ -136,7 +134,7 @@ http {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-For $remote_addr;
# Show status page if api is down
proxy_intercept_errors on;
@@ -149,7 +147,7 @@ http {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-For $remote_addr;
# Rewrite HTTP redirects to HTTPS
proxy_redirect http:// https://;
@@ -167,7 +165,7 @@ http {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-For $remote_addr;
proxy_redirect http:// https://;
diff --git a/apps/server/nginx-80-443.conf b/apps/server/nginx-80-443.conf
index 622f58bbc..873f70c95 100644
--- a/apps/server/nginx-80-443.conf
+++ b/apps/server/nginx-80-443.conf
@@ -21,9 +21,7 @@ http {
server admin:3002 max_fails=1 fail_timeout=5s;
}
- # Preserve any existing X-Forwarded-* headers, this is relevant if AliasVault
- # is running behind another reverse proxy. The set_real_ip_from directives
- # are rendered by entrypoint.sh from TRUSTED_PROXIES (default: RFC1918).
+ # Resolve the real client IP once, here, so the upstream apps don't have to.
__TRUSTED_PROXIES__
real_ip_header X-Forwarded-For;
real_ip_recursive on;
@@ -104,7 +102,7 @@ http {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Prefix /admin/;
# Add WebSocket support for Blazor server
@@ -124,7 +122,7 @@ http {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-For $remote_addr;
# Show status page if api is down
proxy_intercept_errors on;
@@ -137,7 +135,7 @@ http {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-For $remote_addr;
# Show status page if client is down
proxy_intercept_errors on;
@@ -152,7 +150,7 @@ http {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-For $remote_addr;
proxy_intercept_errors on;
error_page 502 503 504 =503 /status.html;
diff --git a/dockerfiles/all-in-one/config/nginx-443.conf b/dockerfiles/all-in-one/config/nginx-443.conf
index e71a36e9b..c8e3e8074 100644
--- a/dockerfiles/all-in-one/config/nginx-443.conf
+++ b/dockerfiles/all-in-one/config/nginx-443.conf
@@ -20,9 +20,7 @@ http {
server 127.0.0.1:3002 max_fails=1 fail_timeout=5s;
}
- # Preserve any existing X-Forwarded-* headers, this is relevant if AliasVault
- # is running behind another reverse proxy. The set_real_ip_from directives
- # are rendered by the nginx s6 script from TRUSTED_PROXIES (default: RFC1918).
+ # Resolve the real client IP once, here, so the upstream apps don't have to.
__TRUSTED_PROXIES__
real_ip_header X-Forwarded-For;
real_ip_recursive on;
@@ -102,7 +100,7 @@ http {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Prefix /admin/;
# Add WebSocket support for Blazor server
@@ -122,7 +120,7 @@ http {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-For $remote_addr;
# Show status page if api is down
proxy_intercept_errors on;
@@ -135,7 +133,7 @@ http {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-For $remote_addr;
# Preserve cache headers from upstream client nginx
proxy_hide_header Cache-Control;
@@ -156,7 +154,7 @@ http {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-For $remote_addr;
proxy_hide_header Cache-Control;
proxy_hide_header Pragma;
diff --git a/dockerfiles/all-in-one/config/nginx-80-443.conf b/dockerfiles/all-in-one/config/nginx-80-443.conf
index 82f1108ee..8a5e3e265 100644
--- a/dockerfiles/all-in-one/config/nginx-80-443.conf
+++ b/dockerfiles/all-in-one/config/nginx-80-443.conf
@@ -21,9 +21,7 @@ http {
server 127.0.0.1:3002 max_fails=1 fail_timeout=5s;
}
- # Preserve any existing X-Forwarded-* headers, this is relevant if AliasVault
- # is running behind another reverse proxy. The set_real_ip_from directives
- # are rendered by the nginx s6 script from TRUSTED_PROXIES (default: RFC1918).
+ # Resolve the real client IP once, here, so the upstream apps don't have to.
__TRUSTED_PROXIES__
real_ip_header X-Forwarded-For;
real_ip_recursive on;
@@ -89,7 +87,7 @@ http {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Prefix /admin/;
# Add WebSocket support for Blazor server
@@ -109,7 +107,7 @@ http {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-For $remote_addr;
# Show status page if api is down
proxy_intercept_errors on;
@@ -122,7 +120,7 @@ http {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-For $remote_addr;
# Preserve cache headers from upstream client nginx
proxy_hide_header Cache-Control;
@@ -143,7 +141,7 @@ http {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-For $remote_addr;
proxy_hide_header Cache-Control;
proxy_hide_header Pragma;
@@ -200,7 +198,7 @@ http {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Prefix /admin/;
# Add WebSocket support for Blazor server
@@ -220,7 +218,7 @@ http {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-For $remote_addr;
# Show status page if api is down
proxy_intercept_errors on;
@@ -233,7 +231,7 @@ http {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-For $remote_addr;
# Preserve cache headers from upstream client nginx
proxy_hide_header Cache-Control;
@@ -254,7 +252,7 @@ http {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ proxy_set_header X-Forwarded-For $remote_addr;
proxy_hide_header Cache-Control;
proxy_hide_header Pragma;