Skip to content

Commit 9ddad8f

Browse files
committed
handle escaped datetime conversion.
1 parent 87be17b commit 9ddad8f

File tree

3 files changed

+24
-31
lines changed

3 files changed

+24
-31
lines changed

src/Microsoft.IdentityModel.JsonWebTokens/Json/JsonClaimSet.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,17 @@ private static Claim CreateClaimFromJsonElement(string key, string issuer, JsonE
7878
// Json.net recognized DateTime by default.
7979
if (jsonElement.ValueKind == JsonValueKind.String)
8080
{
81-
if (jsonElement.TryGetDateTime(out DateTime dateTimeValue))
82-
return new Claim(key, dateTimeValue.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture), ClaimValueTypes.DateTime, issuer, issuer);
83-
else
81+
try
82+
{
83+
if (jsonElement.TryGetDateTime(out DateTime dateTimeValue))
84+
return new Claim(key, dateTimeValue.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture), ClaimValueTypes.DateTime, issuer, issuer);
85+
else
86+
return new Claim(key, jsonElement.ToString(), ClaimValueTypes.String, issuer, issuer);
87+
}
88+
catch(IndexOutOfRangeException)
89+
{
8490
return new Claim(key, jsonElement.ToString(), ClaimValueTypes.String, issuer, issuer);
91+
}
8592
}
8693
else if (jsonElement.ValueKind == JsonValueKind.Null)
8794
return new Claim(key, string.Empty, JsonClaimValueTypes.JsonNull, issuer, issuer);

test/Microsoft.IdentityModel.JsonWebTokens.Tests/JsonClaimSetTests.cs

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,17 @@
1-
//------------------------------------------------------------------------------
2-
//
3-
// Copyright (c) Microsoft Corporation.
4-
// All rights reserved.
5-
//
6-
// This code is licensed under the MIT License.
7-
//
8-
// Permission is hereby granted, free of charge, to any person obtaining a copy
9-
// of this software and associated documentation files(the "Software"), to deal
10-
// in the Software without restriction, including without limitation the rights
11-
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
12-
// copies of the Software, and to permit persons to whom the Software is
13-
// furnished to do so, subject to the following conditions :
14-
//
15-
// The above copyright notice and this permission notice shall be included in
16-
// all copies or substantial portions of the Software.
17-
//
18-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19-
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20-
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
21-
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22-
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23-
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24-
// THE SOFTWARE.
25-
//
26-
//------------------------------------------------------------------------------
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
273

284
using System;
295
using System.Collections.Generic;
306
using System.Globalization;
317
using System.Reflection;
328
using System.Security.Claims;
33-
using Microsoft.IdentityModel.Json.Linq;
349
using Microsoft.IdentityModel.TestUtils;
10+
using Microsoft.IdentityModel.Tokens;
3511
using Xunit;
3612

3713
#if NET452
14+
using Microsoft.IdentityModel.Json.Linq;
3815
using JsonClaimSet = Microsoft.IdentityModel.JsonWebTokens.JsonClaimSet45;
3916
#endif
4017

test/Microsoft.IdentityModel.JsonWebTokens.Tests/JsonWebTokenTests.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

44
using System;
@@ -573,6 +573,15 @@ public void DateTimeISO8061Claim()
573573
// claim value shouldn't contain any quotes
574574
Assert.DoesNotContain("\"", claimA.Value);
575575
}
576+
577+
[Fact]
578+
public void EscapedClaims()
579+
{
580+
string json = @"{""family_name"":""\u0027\u0027"",""given_name"":""\u0027\u0027"",""name"":""謝京螢""}";
581+
string jsonEncoded = Base64UrlEncoder.Encode("{}") + "." + Base64UrlEncoder.Encode(json) + ".";
582+
JsonWebToken encodedToken = new JsonWebToken(jsonEncoded);
583+
_ = encodedToken.Claims;
584+
}
576585
}
577586

578587
public class ParseTimeValuesTheoryData : TheoryDataBase

0 commit comments

Comments
 (0)