From a656006d03712ae4fa655d8cbba6638e81b6e1a6 Mon Sep 17 00:00:00 2001 From: Joel Verhagen Date: Thu, 30 Mar 2023 10:58:58 -0400 Subject: [PATCH] Handle missing X-Forwarded-For header in TableErrorLog (#9444) This helps reduce debugging locally when AzureStorage is being used. --- .../Infrastructure/TableErrorLog.cs | 19 ++++++-- .../Infrastructure/TableErrorLogFacts.cs | 48 +++++++++++++++++++ .../NuGetGallery.Core.Facts.csproj | 1 + 3 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 tests/NuGetGallery.Core.Facts/Infrastructure/TableErrorLogFacts.cs diff --git a/src/NuGetGallery.Core/Infrastructure/TableErrorLog.cs b/src/NuGetGallery.Core/Infrastructure/TableErrorLog.cs index 314f16a220..8cefe9c7f1 100644 --- a/src/NuGetGallery.Core/Infrastructure/TableErrorLog.cs +++ b/src/NuGetGallery.Core/Infrastructure/TableErrorLog.cs @@ -186,7 +186,7 @@ public override string Log(Error error) return pos.ToString(CultureInfo.InvariantCulture); } - private void Obfuscate(Error error) + public static void Obfuscate(Error error) { error.User = string.Empty; if (error.Form != null) @@ -218,10 +218,19 @@ private void Obfuscate(Error error) error.ServerVariables["HTTP_X_NUGET_APIKEY"] = string.Empty; - var forwardedIps = error.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(','); - var obfuscatedIps = forwardedIps.Select(Obfuscator.ObfuscateIp); - - error.ServerVariables["HTTP_X_FORWARDED_FOR"] = string.Join(",", obfuscatedIps); + var forwardedIps = error.ServerVariables["HTTP_X_FORWARDED_FOR"]? + .Split(',') + .Select(x => x.Trim()) + .Where(x => x.Length > 0) + .ToList(); + if (forwardedIps != null) + { + var obfuscatedIps = string.Join(",", forwardedIps.Select(Obfuscator.ObfuscateIp)); + if (!string.IsNullOrWhiteSpace(obfuscatedIps)) + { + error.ServerVariables["HTTP_X_FORWARDED_FOR"] = obfuscatedIps; + } + } } } } \ No newline at end of file diff --git a/tests/NuGetGallery.Core.Facts/Infrastructure/TableErrorLogFacts.cs b/tests/NuGetGallery.Core.Facts/Infrastructure/TableErrorLogFacts.cs new file mode 100644 index 0000000000..31bf7d1aae --- /dev/null +++ b/tests/NuGetGallery.Core.Facts/Infrastructure/TableErrorLogFacts.cs @@ -0,0 +1,48 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Linq; +using Elmah; +using Xunit; + +namespace NuGetGallery.Infrastructure +{ + public class TableErrorLogFacts + { + public class TheObfuscateMethod + { + [Fact] + public void HandlesMissingForwardedHeader() + { + // Arrange + var error = new Error(); + + // Act + TableErrorLog.Obfuscate(error); + + // Assert + Assert.DoesNotContain("HTTP_X_FORWARDED_FOR", error.ServerVariables.Keys.Cast()); + } + + [Theory] + [InlineData("", "")] + [InlineData(",", ",")] + [InlineData(" ", " ")] + [InlineData("127.0.0.1", "127.0.0.0")] + [InlineData("127.1.2.3,127.1.2.4", "127.1.2.0,127.1.2.0")] + [InlineData("127.1.2.3 , 127.1.2.4", "127.1.2.0,127.1.2.0")] + public void ObfuscatesForwardedHeader(string input, string expected) + { + // Arrange + var error = new Error(); + error.ServerVariables["HTTP_X_FORWARDED_FOR"] = input; + + // Act + TableErrorLog.Obfuscate(error); + + // Assert + Assert.Equal(expected, error.ServerVariables["HTTP_X_FORWARDED_FOR"]); + } + } + } +} diff --git a/tests/NuGetGallery.Core.Facts/NuGetGallery.Core.Facts.csproj b/tests/NuGetGallery.Core.Facts/NuGetGallery.Core.Facts.csproj index a72654dc5e..b208be8933 100644 --- a/tests/NuGetGallery.Core.Facts/NuGetGallery.Core.Facts.csproj +++ b/tests/NuGetGallery.Core.Facts/NuGetGallery.Core.Facts.csproj @@ -98,6 +98,7 @@ +