Skip to content
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

Merged
merged 5 commits into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/NuGetGallery/App_Code/ViewHelpers.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@using System.Web.Mvc
@using System.Web.Routing
@using System.Web.Mvc.Html
@using Microsoft.Web.Helpers
@using NuGetGallery
Expand Down Expand Up @@ -353,13 +354,17 @@
var cookieService = DependencyResolver.Current.GetService<ICookieComplianceService>();
if (cookieService.CanWriteNonEssentialCookies(Request))
{
var obfuscatedRequest = ObfuscationHelper.ObfuscateRequestUrl(new HttpContextWrapper(HttpContext.Current), RouteTable.Routes);
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', '@propertyId', 'auto');
ga('set', 'anonymizeIp', true);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: indentation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

ga('set', 'page', '@obfuscatedRequest');
ga('set', 'title', '');
ga('send', 'pageview');
</script>
}
Expand Down
27 changes: 27 additions & 0 deletions src/NuGetGallery/Helpers/ObfuscationHelper.cs
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Simplify
if (httpContext?.Request?.Url == null || routes == null)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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('/'));
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: use tertiary operator to reduce code

var route = routes.GetRouteData(httpContext)?.Route as Route;
return route == null ? string.Empty : route.ObfuscateUrlPath(httpContext.Request.Url.AbsolutePath.TrimStart('/'));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

}
}
}
1 change: 1 addition & 0 deletions src/NuGetGallery/NuGetGallery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@
<Compile Include="Filters\UIAuthorizeAttribute.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="Helpers\GravatarHelper.cs" />
<Compile Include="Helpers\ObfuscationHelper.cs" />
<Compile Include="Helpers\PermissionsHelpers.cs" />
<Compile Include="Helpers\RegexEx.cs" />
<Compile Include="Helpers\RouteUrlTemplate.cs" />
Expand Down
72 changes: 72 additions & 0 deletions tests/NuGetGallery.Facts/Helpers/ObfuscationHelperFacts.cs
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()
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

WithNullContextReturnsEmptyString

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}
}
}
}

1 change: 1 addition & 0 deletions tests/NuGetGallery.Facts/NuGetGallery.Facts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@
<Compile Include="Extensions\OrganizationExtensionsFacts.cs" />
<Compile Include="Filters\UIAuthorizeAttributeFacts.cs" />
<Compile Include="Framework\MemberDataHelper.cs" />
<Compile Include="Helpers\ObfuscationHelperFacts.cs" />
<Compile Include="Infrastructure\Authentication\ApiKeyV3Facts.cs" />
<Compile Include="Infrastructure\Authentication\ApiKeyV4Facts.cs" />
<Compile Include="Infrastructure\Authentication\V3HasherTests.cs" />
Expand Down