Skip to content
This repository was archived by the owner on Feb 19, 2025. It is now read-only.

Secure ServerSideSessions page in the same way as Diagnostics #12

Merged
merged 1 commit into from
Sep 27, 2024
Merged
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
################################################################################
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
################################################################################

/.vs
11 changes: 2 additions & 9 deletions Pages/Diagnostics/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,9 @@ public class Index : PageModel

public async Task<IActionResult> OnGet()
{
var localAddresses = new List<string?> { "127.0.0.1", "::1" };
if(HttpContext.Connection.LocalIpAddress != null)
{
localAddresses.Add(HttpContext.Connection.LocalIpAddress.ToString());
}

if (!localAddresses.Contains(HttpContext.Connection.RemoteIpAddress?.ToString()))
{
//Replace with an authorization policy check
if (HttpContext.Connection.IsRemote())
return NotFound();
}

View = new ViewModel(await HttpContext.AuthenticateAsync());

Expand Down
18 changes: 18 additions & 0 deletions Pages/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,22 @@ internal static IActionResult LoadingPage(this PageModel page, string? redirectU

return page.RedirectToPage("/Redirect/Index", new { RedirectUri = redirectUri });
}

/// <summary>
/// Check for a remote connection (non-localhost)
/// </summary>
internal static bool IsRemote(this ConnectionInfo connection)
{
var localAddresses = new List<string?> { "127.0.0.1", "::1" };
if (connection.LocalIpAddress != null)
{
localAddresses.Add(connection.LocalIpAddress.ToString());
}

if (!localAddresses.Contains(connection.RemoteIpAddress?.ToString()))
{
return true;
}
return false;
}
}
11 changes: 10 additions & 1 deletion Pages/ServerSideSessions/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ public IndexModel(ISessionManagementService? sessionManagementService = null)
[BindProperty(SupportsGet = true)]
public string? Prev { get; set; }

public async Task OnGet()
public async Task<ActionResult> OnGet()
{
//Replace with an authorization policy check
if (HttpContext.Connection.IsRemote())
return NotFound();

if (_sessionManagementService != null)
{
UserSessions = await _sessionManagementService.QuerySessionsAsync(new SessionQuery
Expand All @@ -48,13 +52,18 @@ public async Task OnGet()
SubjectId = SubjectIdFilter
});
}
return Page();
}

[BindProperty]
public string? SessionId { get; set; }

public async Task<IActionResult> OnPost()
{
//Replace with an authorization policy check
if (HttpContext.Connection.IsRemote())
return NotFound();

ArgumentNullException.ThrowIfNull(_sessionManagementService);

await _sessionManagementService.RemoveSessionsAsync(new RemoveSessionsContext {
Expand Down