-
Notifications
You must be signed in to change notification settings - Fork 645
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
Fix issue 1043: Obfuscate AI data on request redirect #5236
Changes from all commits
2a885cb
53311e8
85b1b48
b0c018b
81bb4e2
7b1d98c
62e8928
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,49 @@ | ||
// 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.Collections.Generic; | ||
using System.Linq; | ||
using System.Web.Mvc; | ||
using System.Web.Routing; | ||
|
||
namespace NuGetGallery | ||
{ | ||
public static class RouteExtensions | ||
{ | ||
public struct ObfuscatedMetadata | ||
{ | ||
public int ObfuscatedSegment | ||
{ get; } | ||
|
||
public string ObfuscateValue | ||
{ get; } | ||
|
||
public ObfuscatedMetadata(int obfuscatedSegment, string obfuscateValue) | ||
{ | ||
ObfuscatedSegment = obfuscatedSegment; | ||
ObfuscateValue = obfuscateValue; | ||
} | ||
} | ||
|
||
internal static Dictionary<string, ObfuscatedMetadata> ObfuscatedRouteMap = new Dictionary<string, ObfuscatedMetadata>(); | ||
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: maybe name 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. I think I like more the current name. |
||
|
||
public static void MapRoute(this RouteCollection routes, string name, string url, object defaults, ObfuscatedMetadata obfuscationMetadata) | ||
{ | ||
routes.MapRoute(name, url, defaults); | ||
if (!ObfuscatedRouteMap.ContainsKey(url)) { ObfuscatedRouteMap.Add(url, obfuscationMetadata); } | ||
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: { should be in a new line |
||
} | ||
|
||
public static string ObfuscateUrlPath(this Route route, string urlPath) | ||
{ | ||
var path = route.Url; | ||
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: extra single whitespace? Or maybe it's the 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 |
||
if (!ObfuscatedRouteMap.ContainsKey(path)) | ||
{ | ||
return null; | ||
} | ||
var metadata = ObfuscatedRouteMap[path]; | ||
string[] segments = urlPath.Split('/'); | ||
segments[metadata.ObfuscatedSegment] = metadata.ObfuscateValue; | ||
return string.Join("/", segments); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// 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.Web.Routing; | ||
using Xunit; | ||
|
||
namespace NuGetGallery.Extensions | ||
{ | ||
public class RouteExtensionsFacts | ||
{ | ||
private static string _routeUrl = "test/{user}"; | ||
private static string _url = "test/user1"; | ||
private static int _segment = 1; | ||
private static string _obfuscatedValue = "obfuscatedData"; | ||
|
||
[Fact] | ||
public void MapRouteAddObfuscation() | ||
{ | ||
// Arrange | ||
var routes = new RouteCollection(); | ||
routes.MapRoute("test", _routeUrl, null, new RouteExtensions.ObfuscatedMetadata(_segment, _obfuscatedValue)); | ||
|
||
// Act + Assert | ||
Assert.True(RouteExtensions.ObfuscatedRouteMap.ContainsKey(_routeUrl)); | ||
Assert.Equal(_segment, RouteExtensions.ObfuscatedRouteMap[_routeUrl].ObfuscatedSegment); | ||
Assert.Equal(_obfuscatedValue, RouteExtensions.ObfuscatedRouteMap[_routeUrl].ObfuscateValue); | ||
} | ||
|
||
[Fact] | ||
public void ObfuscateRoutePath_ReturnsNullWhenNotObfuscated() | ||
{ | ||
//Arrange | ||
var urlInput = "newtest/{user}"; | ||
var route = new Route(url: urlInput, routeHandler:null); | ||
|
||
// Act | ||
var obfuscated = route.ObfuscateUrlPath("newtest/user1"); | ||
|
||
//Assert | ||
Assert.Null(obfuscated); | ||
} | ||
|
||
[Fact] | ||
public void ObfuscateRoutePath_CorrectObfuscation() | ||
{ | ||
//Arrange | ||
var routes = new RouteCollection(); | ||
routes.MapRoute("test", _routeUrl, null, new RouteExtensions.ObfuscatedMetadata(_segment, _obfuscatedValue)); | ||
var route = new Route(url: _routeUrl, routeHandler: null); | ||
|
||
// Act | ||
var obfuscated = route.ObfuscateUrlPath(_url); | ||
|
||
//Assert | ||
Assert.Equal($"test/{_obfuscatedValue}", obfuscated); | ||
} | ||
} | ||
} |
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: class could be internal (tests already have internalsVisibleTo)