-
Notifications
You must be signed in to change notification settings - Fork 762
Store non-sensitive UI state without protection #5434
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Aspire.Dashboard.Model.BrowserStorage; | ||
|
|
||
| public interface ILocalStorage : IBrowserStorage | ||
| { | ||
| Task<StorageResult<T>> GetUnprotectedAsync<T>(string key); | ||
| Task SetUnprotectedAsync<T>(string key, T value); | ||
| } |
37 changes: 35 additions & 2 deletions
37
src/Aspire.Dashboard/Model/BrowserStorage/LocalBrowserStorage.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,46 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Text.Json; | ||
| using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage; | ||
| using Microsoft.JSInterop; | ||
|
|
||
| namespace Aspire.Dashboard.Model.BrowserStorage; | ||
|
|
||
| public class LocalBrowserStorage : BrowserStorageBase, ILocalStorage | ||
| { | ||
| public LocalBrowserStorage(ProtectedLocalStorage protectedLocalStorage) : base(protectedLocalStorage) | ||
| private static readonly JsonSerializerOptions s_options = new() | ||
| { | ||
| PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
| PropertyNameCaseInsensitive = true, | ||
| }; | ||
|
|
||
| private readonly IJSRuntime _jsRuntime; | ||
|
|
||
| public LocalBrowserStorage(IJSRuntime jsRuntime, ProtectedLocalStorage protectedLocalStorage) : base(protectedLocalStorage) | ||
| { | ||
| _jsRuntime = jsRuntime; | ||
| } | ||
|
|
||
| public async Task<StorageResult<T>> GetUnprotectedAsync<T>(string key) | ||
| { | ||
| var json = await GetJsonAsync(key).ConfigureAwait(false); | ||
|
|
||
| return json == null ? | ||
| new StorageResult<T>(false, default) : | ||
| new StorageResult<T>(true, JsonSerializer.Deserialize<T>(json, s_options)); | ||
| } | ||
|
|
||
| public async Task SetUnprotectedAsync<T>(string key, T value) | ||
| { | ||
| var json = JsonSerializer.Serialize(value, s_options); | ||
|
|
||
| await SetJsonAsync(key, json).ConfigureAwait(false); | ||
| } | ||
|
|
||
| private ValueTask SetJsonAsync(string key, string json) | ||
| => _jsRuntime.InvokeVoidAsync("localStorage.setItem", key, json); | ||
|
|
||
| private ValueTask<string?> GetJsonAsync(string key) | ||
| => _jsRuntime.InvokeAsync<string?>("localStorage.getItem", key); | ||
|
Comment on lines
+55
to
+59
Member
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. These two methods return |
||
| } | ||
2 changes: 1 addition & 1 deletion
2
src/Aspire.Dashboard/Model/BrowserStorage/SessionBrowserStorage.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
tests/Aspire.Dashboard.Tests/LocalBrowserStorageTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
| using Aspire.Dashboard.Model.BrowserStorage; | ||
| using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage; | ||
| using Microsoft.AspNetCore.DataProtection; | ||
| using Microsoft.JSInterop; | ||
| using Xunit; | ||
|
|
||
| namespace Aspire.Dashboard.Tests; | ||
|
|
||
| public class LocalBrowserStorageTests | ||
| { | ||
| [Theory] | ||
| [InlineData(123, "123")] | ||
| [InlineData("Hello world", @"""Hello world""")] | ||
| [InlineData(null, "null")] | ||
| public async Task SetUnprotectedAsync_JSInvokedWithJson(object? value, string result) | ||
| { | ||
| // Arrange | ||
| string? identifier = null; | ||
| object?[]? args = null; | ||
|
|
||
| var testJsonRuntime = new TestJSRuntime(); | ||
| testJsonRuntime.OnInvoke = r => | ||
| { | ||
| (identifier, args) = r; | ||
| return default; | ||
| }; | ||
| var localStorage = new LocalBrowserStorage(testJsonRuntime, new ProtectedLocalStorage(testJsonRuntime, new TestDataProtector())); | ||
|
|
||
| // Act | ||
| await localStorage.SetUnprotectedAsync("MyKey", value); | ||
|
|
||
| // Assert | ||
| Assert.Equal("localStorage.setItem", identifier); | ||
| Assert.NotNull(args); | ||
| Assert.Equal("MyKey", args[0]); | ||
| Assert.Equal(result, args[1]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetUnprotectedAsync_HasValue_Success() | ||
| { | ||
| // Arrange | ||
| string? identifier = null; | ||
| object?[]? args = null; | ||
|
|
||
| var testJsonRuntime = new TestJSRuntime(); | ||
| testJsonRuntime.OnInvoke = r => | ||
| { | ||
| (identifier, args) = r; | ||
| return "123"; | ||
| }; | ||
| var localStorage = new LocalBrowserStorage(testJsonRuntime, new ProtectedLocalStorage(testJsonRuntime, new TestDataProtector())); | ||
|
|
||
| // Act | ||
| var result = await localStorage.GetUnprotectedAsync<int>("MyKey"); | ||
|
|
||
| // Assert | ||
| Assert.True(result.Success); | ||
| Assert.Equal(123, result.Value); | ||
| Assert.Equal("localStorage.getItem", identifier); | ||
| Assert.NotNull(args); | ||
| Assert.Equal("MyKey", args[0]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task GetUnprotectedAsync_NoValue_Failure() | ||
| { | ||
| // Arrange | ||
| string? identifier = null; | ||
| object?[]? args = null; | ||
|
|
||
| var testJsonRuntime = new TestJSRuntime(); | ||
| testJsonRuntime.OnInvoke = r => | ||
| { | ||
| (identifier, args) = r; | ||
| return default; | ||
| }; | ||
| var localStorage = new LocalBrowserStorage(testJsonRuntime, new ProtectedLocalStorage(testJsonRuntime, new TestDataProtector())); | ||
|
|
||
| // Act | ||
| var result = await localStorage.GetUnprotectedAsync<int>("MyKey"); | ||
|
|
||
| // Assert | ||
| Assert.False(result.Success); | ||
| Assert.Equal("localStorage.getItem", identifier); | ||
| Assert.NotNull(args); | ||
| Assert.Equal("MyKey", args[0]); | ||
| } | ||
|
|
||
| private sealed class TestJSRuntime : IJSRuntime | ||
| { | ||
| public Func<(string Identifier, object?[]? Args), object?>? OnInvoke { get; set; } | ||
|
|
||
| public ValueTask<TValue> InvokeAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)] TValue>(string identifier, object?[]? args) | ||
| { | ||
| if (OnInvoke?.Invoke((identifier, args)) is TValue result) | ||
| { | ||
| return ValueTask.FromResult(result); | ||
| } | ||
| return default; | ||
| } | ||
|
|
||
| public ValueTask<TValue> InvokeAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)] TValue>(string identifier, CancellationToken cancellationToken, object?[]? args) | ||
| { | ||
| if (OnInvoke?.Invoke((identifier, args)) is TValue result) | ||
| { | ||
| return ValueTask.FromResult(result); | ||
| } | ||
| return default; | ||
| } | ||
| } | ||
|
|
||
| private sealed class TestDataProtector : IDataProtector | ||
| { | ||
| public IDataProtector CreateProtector(string purpose) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| public byte[] Protect(byte[] plaintext) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| public byte[] Unprotect(byte[] protectedData) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.