Skip to content

Commit a656006

Browse files
authored
Handle missing X-Forwarded-For header in TableErrorLog (#9444)
This helps reduce debugging locally when AzureStorage is being used.
1 parent 659510b commit a656006

3 files changed

Lines changed: 63 additions & 5 deletions

File tree

src/NuGetGallery.Core/Infrastructure/TableErrorLog.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public override string Log(Error error)
186186
return pos.ToString(CultureInfo.InvariantCulture);
187187
}
188188

189-
private void Obfuscate(Error error)
189+
public static void Obfuscate(Error error)
190190
{
191191
error.User = string.Empty;
192192
if (error.Form != null)
@@ -218,10 +218,19 @@ private void Obfuscate(Error error)
218218

219219
error.ServerVariables["HTTP_X_NUGET_APIKEY"] = string.Empty;
220220

221-
var forwardedIps = error.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(',');
222-
var obfuscatedIps = forwardedIps.Select(Obfuscator.ObfuscateIp);
223-
224-
error.ServerVariables["HTTP_X_FORWARDED_FOR"] = string.Join(",", obfuscatedIps);
221+
var forwardedIps = error.ServerVariables["HTTP_X_FORWARDED_FOR"]?
222+
.Split(',')
223+
.Select(x => x.Trim())
224+
.Where(x => x.Length > 0)
225+
.ToList();
226+
if (forwardedIps != null)
227+
{
228+
var obfuscatedIps = string.Join(",", forwardedIps.Select(Obfuscator.ObfuscateIp));
229+
if (!string.IsNullOrWhiteSpace(obfuscatedIps))
230+
{
231+
error.ServerVariables["HTTP_X_FORWARDED_FOR"] = obfuscatedIps;
232+
}
233+
}
225234
}
226235
}
227236
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Linq;
5+
using Elmah;
6+
using Xunit;
7+
8+
namespace NuGetGallery.Infrastructure
9+
{
10+
public class TableErrorLogFacts
11+
{
12+
public class TheObfuscateMethod
13+
{
14+
[Fact]
15+
public void HandlesMissingForwardedHeader()
16+
{
17+
// Arrange
18+
var error = new Error();
19+
20+
// Act
21+
TableErrorLog.Obfuscate(error);
22+
23+
// Assert
24+
Assert.DoesNotContain("HTTP_X_FORWARDED_FOR", error.ServerVariables.Keys.Cast<string>());
25+
}
26+
27+
[Theory]
28+
[InlineData("", "")]
29+
[InlineData(",", ",")]
30+
[InlineData(" ", " ")]
31+
[InlineData("127.0.0.1", "127.0.0.0")]
32+
[InlineData("127.1.2.3,127.1.2.4", "127.1.2.0,127.1.2.0")]
33+
[InlineData("127.1.2.3 , 127.1.2.4", "127.1.2.0,127.1.2.0")]
34+
public void ObfuscatesForwardedHeader(string input, string expected)
35+
{
36+
// Arrange
37+
var error = new Error();
38+
error.ServerVariables["HTTP_X_FORWARDED_FOR"] = input;
39+
40+
// Act
41+
TableErrorLog.Obfuscate(error);
42+
43+
// Assert
44+
Assert.Equal(expected, error.ServerVariables["HTTP_X_FORWARDED_FOR"]);
45+
}
46+
}
47+
}
48+
}

tests/NuGetGallery.Core.Facts/NuGetGallery.Core.Facts.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
<Compile Include="Infrastructure\Mail\Messages\SymbolPackageValidationFailedMessageFacts.cs" />
9999
<Compile Include="Infrastructure\Mail\Messages\SymbolPackageValidationTakingTooLongMessageFacts.cs" />
100100
<Compile Include="Infrastructure\Mail\TestMessageServiceConfiguration.cs" />
101+
<Compile Include="Infrastructure\TableErrorLogFacts.cs" />
101102
<Compile Include="Packaging\ManifestValidatorFacts.cs" />
102103
<Compile Include="Packaging\PackageIdValidatorTest.cs" />
103104
<Compile Include="Packaging\PackageMetadataFacts.cs" />

0 commit comments

Comments
 (0)