From 3c34fcc3bde265d8c98792a8b7d00c61c4bd06e6 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 23 Oct 2026 14:50:57 -0500 Subject: require r/w for conversations --- .../Messaging/API/Controllers/ConversationsController.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/slskd/Messaging/API/Controllers') diff --git a/src/slskd/Messaging/API/Controllers/ConversationsController.cs b/src/slskd/Messaging/API/Controllers/ConversationsController.cs index 7ed0a92c..a1c6038f 100644 --- a/src/slskd/Messaging/API/Controllers/ConversationsController.cs +++ b/src/slskd/Messaging/API/Controllers/ConversationsController.cs @@ -87,7 +87,7 @@ namespace slskd.Messaging.API /// A conversation with the specified username, or a message matching the specified id could not be found. /// [HttpPut("{username}/{id}")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.ReadWriteOrAdministrator)] [ProducesResponseType(200)] [ProducesResponseType(404)] public async Task Acknowledge([FromRoute, UrlEncoded] string username, [FromRoute] int id) @@ -97,7 +97,7 @@ namespace slskd.Messaging.API return Forbid(); } - var message = Messages.Conversations.FindMessageAsync(username, id); + var message = await Messages.Conversations.FindMessageAsync(username, id); if (message == default) { @@ -117,7 +117,7 @@ namespace slskd.Messaging.API /// The request completed successfully. /// A conversation with the specified username could not be found. [HttpPut("{username}")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.ReadWriteOrAdministrator)] [ProducesResponseType(200)] [ProducesResponseType(404)] public async Task AcknowledgeAll([FromRoute, UrlEncoded] string username) @@ -127,7 +127,7 @@ namespace slskd.Messaging.API return Forbid(); } - var conversation = Messages.Conversations.FindAsync(username); + var conversation = await Messages.Conversations.FindAsync(username); if (conversation == default) { @@ -146,7 +146,7 @@ namespace slskd.Messaging.API /// The request completed successfully. /// A conversation with the specified username could not be found. [HttpDelete("{username}")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.ReadWriteOrAdministrator)] [ProducesResponseType(404)] [ProducesResponseType(204)] public async Task Close([FromRoute, UrlEncoded] string username) @@ -156,7 +156,7 @@ namespace slskd.Messaging.API return Forbid(); } - var conversation = Messages.Conversations.FindAsync(username, includeInactive: false); + var conversation = await Messages.Conversations.FindAsync(username, includeInactive: false); if (conversation == default) { @@ -259,7 +259,7 @@ namespace slskd.Messaging.API /// The request completed successfully. /// The specified message is null or empty. [HttpPost("{username}")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.ReadWriteOrAdministrator)] [ProducesResponseType(201)] [ProducesResponseType(400)] public async Task Send([FromRoute, UrlEncoded] string username, [FromBody] string message) -- cgit v1.2.3 From d4447890e5fad1cd942affe051c9479385667465 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 23 Oct 2026 14:54:37 -0500 Subject: perms for more controllers, use paranoid check of api keys --- src/slskd/Core/Security/SecurityService.cs | 7 +++++-- src/slskd/Messaging/API/Controllers/RoomsController.cs | 10 +++++----- src/slskd/Shares/API/Controllers/SharesController.cs | 4 ++-- src/slskd/Transfers/API/Controllers/TransfersController.cs | 14 +++++++------- 4 files changed, 19 insertions(+), 16 deletions(-) (limited to 'src/slskd/Messaging/API/Controllers') diff --git a/src/slskd/Core/Security/SecurityService.cs b/src/slskd/Core/Security/SecurityService.cs index 790f5806..26ed49d2 100644 --- a/src/slskd/Core/Security/SecurityService.cs +++ b/src/slskd/Core/Security/SecurityService.cs @@ -40,6 +40,8 @@ using System.IdentityModel.Tokens.Jwt; using System.Linq; using System.Net; using System.Security.Claims; +using System.Security.Cryptography; +using System.Text; using Microsoft.IdentityModel.Tokens; using NetTools; using Serilog; @@ -121,11 +123,12 @@ public class SecurityService keys = keys.Prepend(new KeyValuePair(nameof(PrimaryApiKey), PrimaryApiKey)); } - var record = keys.FirstOrDefault(k => k.Value.Key == key); + var keyBytes = Encoding.UTF8.GetBytes(key ?? string.Empty); + var record = keys.FirstOrDefault(k => k.Value.Key is not null && CryptographicOperations.FixedTimeEquals(Encoding.UTF8.GetBytes(k.Value.Key), keyBytes)); if (record.Key == null) { - throw new NotFoundException($"Unknown API key beginning with: {key.Substring(0, 4)}"); + throw new NotFoundException("Unknown API key"); } // looks like '::ffff:127.0.0.1'; not compatible with CIDR.Contains(), so convert it back to IPv4 diff --git a/src/slskd/Messaging/API/Controllers/RoomsController.cs b/src/slskd/Messaging/API/Controllers/RoomsController.cs index 3b2fe568..57d3b742 100644 --- a/src/slskd/Messaging/API/Controllers/RoomsController.cs +++ b/src/slskd/Messaging/API/Controllers/RoomsController.cs @@ -127,7 +127,7 @@ namespace slskd.Messaging.API /// The request completed successfully. /// The specified roomName could not be found. [HttpPost("joined/{roomName}/messages")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.ReadWriteOrAdministrator)] [ProducesResponseType(201)] [ProducesResponseType(404)] public async Task SendMessage([FromRoute, UrlEncoded] string roomName, [FromBody] string message) @@ -155,7 +155,7 @@ namespace slskd.Messaging.API /// The request completed successfully. /// The specified roomName could not be found. [HttpPost("joined/{roomName}/ticker")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.ReadWriteOrAdministrator)] [ProducesResponseType(201)] [ProducesResponseType(404)] public async Task SetTicker([FromRoute, UrlEncoded] string roomName, [FromBody] string message) @@ -183,7 +183,7 @@ namespace slskd.Messaging.API /// The request completed successfully. /// The specified roomName could not be found. [HttpPost("joined/{roomName}/members")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.ReadWriteOrAdministrator)] [ProducesResponseType(201)] [ProducesResponseType(404)] public async Task AddRoomMember([FromRoute, UrlEncoded] string roomName, [FromBody] string username) @@ -298,7 +298,7 @@ namespace slskd.Messaging.API /// The request completed successfully. /// The room has already been joined. [HttpPost("joined")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.ReadWriteOrAdministrator)] [ProducesResponseType(typeof(Room), 201)] [ProducesResponseType(304)] public async Task JoinRoom([FromBody] string roomName) @@ -339,7 +339,7 @@ namespace slskd.Messaging.API /// The request completed successfully. /// The room has not been joined. [HttpDelete("joined/{roomName}")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.ReadWriteOrAdministrator)] [ProducesResponseType(204)] [ProducesResponseType(404)] public async Task LeaveRoom([FromRoute, UrlEncoded] string roomName) diff --git a/src/slskd/Shares/API/Controllers/SharesController.cs b/src/slskd/Shares/API/Controllers/SharesController.cs index 76ec4643..64d703bd 100644 --- a/src/slskd/Shares/API/Controllers/SharesController.cs +++ b/src/slskd/Shares/API/Controllers/SharesController.cs @@ -148,7 +148,7 @@ namespace slskd.Shares.API /// A share scan is already in progress. [HttpPut] [Route("")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.ReadWriteOrAdministrator)] [ProducesResponseType(204)] [ProducesResponseType(409)] public IActionResult RescanSharesAsync() @@ -173,7 +173,7 @@ namespace slskd.Shares.API /// A share scan was not in progress. [HttpDelete] [Route("")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.ReadWriteOrAdministrator)] [ProducesResponseType(204)] [ProducesResponseType(404)] public IActionResult CancelShareScan() diff --git a/src/slskd/Transfers/API/Controllers/TransfersController.cs b/src/slskd/Transfers/API/Controllers/TransfersController.cs index 3dd2f124..b3453530 100644 --- a/src/slskd/Transfers/API/Controllers/TransfersController.cs +++ b/src/slskd/Transfers/API/Controllers/TransfersController.cs @@ -89,7 +89,7 @@ namespace slskd.Transfers.API /// The download was cancelled successfully. /// The specified download was not found. [HttpDelete("downloads/{username}/{id}")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.ReadWriteOrAdministrator)] [ProducesResponseType(204)] [ProducesResponseType(404)] public IActionResult CancelDownloadAsync([FromRoute, UrlEncoded, Required] string username, [FromRoute, Required] string id, [FromQuery] bool remove = false) @@ -127,7 +127,7 @@ namespace slskd.Transfers.API /// /// The downloads were removed successfully. [HttpDelete("downloads/all/completed")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.ReadWriteOrAdministrator)] [ProducesResponseType(204)] public IActionResult ClearCompletedDownloads() { @@ -158,7 +158,7 @@ namespace slskd.Transfers.API /// The upload was cancelled successfully. /// The specified upload was not found. [HttpDelete("uploads/{username}/{id}")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.ReadWriteOrAdministrator)] [ProducesResponseType(204)] [ProducesResponseType(404)] public IActionResult CancelUpload([FromRoute, UrlEncoded, Required] string username, [FromRoute, Required] string id, [FromQuery] bool remove = false) @@ -196,7 +196,7 @@ namespace slskd.Transfers.API /// /// The uploads were removed successfully. [HttpDelete("uploads/all/completed")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.ReadWriteOrAdministrator)] [ProducesResponseType(204)] public IActionResult ClearCompletedUploads() { @@ -228,7 +228,7 @@ namespace slskd.Transfers.API /// An unexpected error was encountered. [Obsolete("Will be phased out in future versions; use batches")] [HttpPost("downloads/{username}")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.ReadWriteOrAdministrator)] [ProducesResponseType(201)] [ProducesResponseType(typeof(string), 403)] [ProducesResponseType(typeof(string), 500)] @@ -302,7 +302,7 @@ namespace slskd.Transfers.API /// Request throttled. /// An unexpected error was encountered. [HttpPost("downloads/batches")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.ReadWriteOrAdministrator)] [ProducesResponseType(typeof(EnqueueDownloadBatchResponse), StatusCodes.Status200OK)] [ProducesResponseType(typeof(EnqueueDownloadBatchResponse), StatusCodes.Status201Created)] [ProducesResponseType(typeof(EnqueueDownloadBatchResponse), StatusCodes.Status207MultiStatus)] @@ -582,7 +582,7 @@ namespace slskd.Transfers.API /// The request completed successfully. /// The specified download was not found. [HttpGet("downloads/{username}/{id}/position")] - [Authorize(Policy = AuthPolicy.Any)] + [Authorize(Policy = AuthPolicy.Any, Roles = AuthRole.ReadWriteOrAdministrator)] [ProducesResponseType(typeof(Transfer), 200)] [ProducesResponseType(404)] public async Task GetPlaceInQueueAsync([FromRoute, UrlEncoded, Required] string username, [FromRoute, Required] string id) -- cgit v1.2.3