Skip to content
Open
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
4 changes: 4 additions & 0 deletions src/customer-key-store/Controllers/KeysController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public IActionResult GetKey(string keyName)
{
try
{
ippw.ProtocolVersionValidator.ValidateProtocolVersion(Request);

var publicKey = keyManager.GetPublicKey(GetRequestUri(Request), keyName);

return Ok(publicKey);
Expand All @@ -43,6 +45,8 @@ public IActionResult Decrypt(string keyName, string keyId, [FromBody] ippw.Encry
{
try
{
ippw.ProtocolVersionValidator.ValidateProtocolVersion(Request);

var decryptedData = keyManager.Decrypt(HttpContext.User, keyName, keyId, encryptedData);

return Ok(decryptedData);
Expand Down
42 changes: 42 additions & 0 deletions src/customer-key-store/Models/ProtocolVersionValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace Microsoft.InformationProtection.Web.Models
{
using System;
using System.Globalization;
using Microsoft.Extensions.Primitives;
using Microsoft.InformationProtection.Web.Models.Extensions;

public static class ProtocolVersionValidator
{
private const double MinSupportedVersion = 1.0;
private const double MaxSupportedVersion = 1.0;
private const string ProtocolVersion = "protocol-version";

public static void ValidateProtocolVersion(AspNetCore.Http.HttpRequest request)
{
request.ThrowIfNull(nameof(request));

StringValues values = new StringValues("1.0"); //Older versions of mip were not sending up the protocol, default to 1.0
if(request.Query.ContainsKey(ProtocolVersion))
{
values = request.Query[ProtocolVersion];
}

if(values.Count != 1)
{
throw new ArgumentException("More than one protocol-version header found");
}

if(!double.TryParse(values[0], NumberStyles.Any, CultureInfo.InvariantCulture, out double protocolVersion))
{
throw new ArgumentException("Unable to parse protocol_version: " + values[0]);
}

if(protocolVersion < MinSupportedVersion || protocolVersion > MaxSupportedVersion)
{
throw new ArgumentException("Unsupported protocol_version: " + values[0]);
}
}
}
}
12 changes: 6 additions & 6 deletions src/customer-key-store/Models/RoleAuthorizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public RoleAuthorizer(IConfiguration configuration)
ldapPath = configuration["RoleAuthorizer:LDAPPath"];
}

public static string GetRole(string memberOf)
{
memberOf.ThrowIfNull(nameof(memberOf));
return ParseCN(memberOf);
}

public void AddRole(string role)
{
roles.Add(role);
Expand Down Expand Up @@ -133,11 +139,5 @@ private static string ParseCN(string distinguishedName)

return role.ToString();
}

public static string GetRole(string memberOf)
{
memberOf.ThrowIfNull(nameof(memberOf));
return ParseCN(memberOf);
}
}
}