Skip to content

Commit c94b2dd

Browse files
authored
[Blazor][Fixes #15399]The Blazor descriptor can contain two consecutive dashes (#15412)
* We Base64 encode the descriptor instead of Base64Url encode it as data protection does with its string overload. * It uses "+/" instead of "-_", both of which are safe inside HTML comments. * The descriptors are not sent in any url, nor are present inside headers or similar, so Base64 encoding them is fine.
1 parent bf846cb commit c94b2dd

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/Components/Server/src/Circuits/ServerComponentDeserializer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Text;
67
using System.Text.Json;
78
using Microsoft.AspNetCore.DataProtection;
89
using Microsoft.Extensions.Logging;
@@ -153,7 +154,9 @@ public bool TryDeserializeComponentDescriptorCollection(string serializedCompone
153154
string unprotected;
154155
try
155156
{
156-
unprotected = _dataProtector.Unprotect(record.Descriptor);
157+
var payload = Convert.FromBase64String(record.Descriptor);
158+
var unprotectedBytes = _dataProtector.Unprotect(payload);
159+
unprotected = Encoding.UTF8.GetString(unprotectedBytes);
157160
}
158161
catch (Exception e)
159162
{

src/Mvc/Mvc.ViewFeatures/src/ServerComponentSerializer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Text;
67
using System.Text.Json;
78
using Microsoft.AspNetCore.Components;
89
using Microsoft.AspNetCore.DataProtection;
@@ -43,7 +44,9 @@ public ServerComponentMarker SerializeInvocation(ServerComponentInvocationSequen
4344
invocationId.Value);
4445

4546
var serializedServerComponent = JsonSerializer.Serialize(serverComponent, ServerComponentSerializationSettings.JsonSerializationOptions);
46-
return (serverComponent.Sequence, _dataProtector.Protect(serializedServerComponent, ServerComponentSerializationSettings.DataExpiration));
47+
var serializedServerComponentBytes = JsonSerializer.SerializeToUtf8Bytes(serverComponent, ServerComponentSerializationSettings.JsonSerializationOptions);
48+
var protectedBytes = _dataProtector.Protect(serializedServerComponentBytes, ServerComponentSerializationSettings.DataExpiration);
49+
return (serverComponent.Sequence, Convert.ToBase64String(protectedBytes));
4750
}
4851

4952
internal IEnumerable<string> GetPreamble(ServerComponentMarker record)

0 commit comments

Comments
 (0)