From 7b129b69211909a3af9bbeddf0acc1dc3d84e20c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 17 Nov 2022 09:09:50 -0600 Subject: forbid API calls that require server interaction when running in Agent mode --- .../Search/API/Controllers/SearchesController.cs | 39 +++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'src/slskd/Search/API/Controllers') diff --git a/src/slskd/Search/API/Controllers/SearchesController.cs b/src/slskd/Search/API/Controllers/SearchesController.cs index aadf01b2..ae7f42db 100644 --- a/src/slskd/Search/API/Controllers/SearchesController.cs +++ b/src/slskd/Search/API/Controllers/SearchesController.cs @@ -15,12 +15,15 @@ // along with this program. If not, see https://www.gnu.org/licenses/. // +using Microsoft.Extensions.Options; + namespace slskd.Search.API { using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; + using slskd.Relay; using SearchQuery = Soulseek.SearchQuery; using SearchScope = Soulseek.SearchScope; @@ -38,12 +41,16 @@ namespace slskd.Search.API /// Initializes a new instance of the class. /// /// - public SearchesController(ISearchService searchService) + /// + public SearchesController(ISearchService searchService, IOptionsSnapshot optionsSnapshot) { Searches = searchService; + OptionsSnapshot = optionsSnapshot; } private ISearchService Searches { get; } + private IOptionsSnapshot OptionsSnapshot { get; } + private bool IsAgent => OptionsSnapshot.Value.Relay.Mode.ToEnum() == OperationMode.Agent; /// /// Performs a search for the specified . @@ -57,6 +64,11 @@ namespace slskd.Search.API [Authorize(Policy = AuthPolicy.Any)] public async Task Post([FromBody] SearchRequest request) { + if (IsAgent) + { + return Forbid(); + } + if (string.IsNullOrWhiteSpace(request.SearchText)) { return BadRequest("SearchText may not be null or empty"); @@ -94,6 +106,11 @@ namespace slskd.Search.API [Authorize(Policy = AuthPolicy.Any)] public async Task GetById([FromRoute] Guid id, [FromQuery] bool includeResponses = false) { + if (IsAgent) + { + return Forbid(); + } + var search = await Searches.FindAsync(search => search.Id == id, includeResponses); if (search == default) @@ -115,6 +132,11 @@ namespace slskd.Search.API [Authorize(Policy = AuthPolicy.Any)] public async Task GetResponsesById([FromRoute] Guid id) { + if (IsAgent) + { + return Forbid(); + } + var search = await Searches.FindAsync(search => search.Id == id, includeResponses: true); if (search == default) @@ -133,6 +155,11 @@ namespace slskd.Search.API [Authorize(Policy = AuthPolicy.Any)] public async Task GetAll() { + if (IsAgent) + { + return Forbid(); + } + var searches = await Searches.ListAsync(); return Ok(searches); } @@ -150,6 +177,11 @@ namespace slskd.Search.API [ProducesResponseType(304)] public async Task Cancel([FromRoute] Guid id) { + if (IsAgent) + { + return Forbid(); + } + var search = await Searches.FindAsync(search => search.Id == id); if (search == default) @@ -178,6 +210,11 @@ namespace slskd.Search.API [ProducesResponseType(404)] public async Task Delete([FromRoute] Guid id) { + if (IsAgent) + { + return Forbid(); + } + var search = await Searches.FindAsync(search => search.Id == id, includeResponses: true); if (search == default) -- cgit v1.2.3