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 all 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);
ga('set', 'page', '@obfuscatedRequest');
ga('set', 'title', '');
ga('send', 'pageview');
</script>
}
Expand Down
23 changes: 23 additions & 0 deletions src/NuGetGallery/Helpers/ObfuscationHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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?.Request?.Url == null || routes == null)
{
return string.Empty;
}

var route = routes.GetRouteData(httpContext)?.Route as Route;
return route == null ? string.Empty : route.ObfuscateUrlPath(httpContext.Request.Url.AbsolutePath.TrimStart('/'));
}
}
}
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 WithNullContextReturnsEmptyString()
{
// Assert + Assert
var result = ObfuscationHelper.ObfuscateRequestUrl(null, _currentRoutes);
Assert.Equal("", result);
}

[Fact]
public void WithNullRoutesReturnsEmptyString()
{
//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