-
Notifications
You must be signed in to change notification settings - Fork 644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GA - obfuscate ip and user names #5791
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// 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; | ||
using System.Web; | ||
using System.Web.Routing; | ||
|
||
namespace NuGetGallery.Helpers | ||
{ | ||
public class ObfuscationHelper | ||
{ | ||
public static string ObfuscateRequestUrl(HttpContextBase httpContext, RouteCollection routes) | ||
{ | ||
if (httpContext == null || httpContext.Request == null || httpContext.Request.Url == null || routes == null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplify There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||
{ | ||
return string.Empty; | ||
} | ||
|
||
var route = routes.GetRouteData(httpContext)?.Route as Route; | ||
if (route == null) | ||
{ | ||
return string.Empty; | ||
} | ||
return route.ObfuscateUrlPath(httpContext.Request.Url.AbsolutePath.TrimStart('/')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: use tertiary operator to reduce code
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// 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; | ||
using System.Web; | ||
using System.Web.Routing; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace NuGetGallery.Helpers | ||
{ | ||
public class ObfuscationHelperFacts | ||
{ | ||
public class TheObfuscateCurrentRequestUrlFacts | ||
{ | ||
private const string _relativeTestPath = "profiles/user1"; | ||
private RouteCollection _currentRoutes; | ||
|
||
public TheObfuscateCurrentRequestUrlFacts() | ||
{ | ||
if (_currentRoutes == null) | ||
{ | ||
_currentRoutes = new RouteCollection(); | ||
Routes.RegisterApiV2Routes(_currentRoutes); | ||
Routes.RegisterUIRoutes(_currentRoutes); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void NullContext() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: usually test cases have names to describe the expected response in addition to the input e.g.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||
{ | ||
// Assert + Assert | ||
var result = ObfuscationHelper.ObfuscateRequestUrl(null, _currentRoutes); | ||
Assert.Equal("", result); | ||
} | ||
|
||
[Fact] | ||
public void NullRoutes() | ||
{ | ||
//Arrange | ||
var context = GetMockedHttpContext(); | ||
|
||
// Assert + Assert | ||
var result = ObfuscationHelper.ObfuscateRequestUrl(context, null); | ||
Assert.Equal("", result); | ||
} | ||
|
||
[Fact] | ||
public void ValidData() | ||
{ | ||
//Arrange | ||
var context = GetMockedHttpContext(); | ||
|
||
// Assert + Assert | ||
var result = ObfuscationHelper.ObfuscateRequestUrl(context, _currentRoutes); | ||
Assert.Equal("profiles/ObfuscatedUserName", result); | ||
} | ||
|
||
private HttpContextBase GetMockedHttpContext() | ||
{ | ||
var context = new Mock<HttpContextBase>(); | ||
var request = new Mock<HttpRequestBase>(); | ||
context.Setup(ctx => ctx.Request).Returns(request.Object); | ||
|
||
request.Setup(req => req.Url).Returns(new Uri($"https://localhost/{_relativeTestPath}")); | ||
request.Setup(req => req.AppRelativeCurrentExecutionFilePath).Returns($"~/{_relativeTestPath}"); | ||
return context.Object; | ||
} | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: indentation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated