diff options
| author | JP Dillingham <jp@dillingham.me> | 2023-01-06 07:50:03 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2023-01-06 07:50:03 -0600 |
| commit | 622718b5ed5c9f8aa6b2f324a1fa4648b481d73c (patch) | |
| tree | 593e2f98cb604d458a86b436ac8da6f0d0ef169c /src/slskd/Relay/API | |
| parent | 6acd4b59a303a41b685d6b75a684c718ac80897c (diff) | |
| parent | 9251a1be5f004f022a7e197112d89dd1972571fd (diff) | |
| download | slskd-622718b5ed5c9f8aa6b2f324a1fa4648b481d73c.tar.gz | |
Merge remote-tracking branch 'origin/master' into relay-docs
Diffstat (limited to 'src/slskd/Relay/API')
| -rw-r--r-- | src/slskd/Relay/API/Controllers/RelayController.cs | 32 | ||||
| -rw-r--r-- | src/slskd/Relay/API/Hubs/RelayHub.cs | 25 |
2 files changed, 45 insertions, 12 deletions
diff --git a/src/slskd/Relay/API/Controllers/RelayController.cs b/src/slskd/Relay/API/Controllers/RelayController.cs index 7183283f..d4fb4810 100644 --- a/src/slskd/Relay/API/Controllers/RelayController.cs +++ b/src/slskd/Relay/API/Controllers/RelayController.cs @@ -82,6 +82,11 @@ namespace slskd.Relay return NoContent(); } + /// <summary> + /// Downloads a file. + /// </summary> + /// <param name="token">The unique identifier for the request.</param> + /// <returns></returns> [HttpGet("downloads/{token}")] [Authorize(Policy = AuthPolicy.ApiKeyOnly)] public IActionResult DownloadFile([FromRoute]string token) @@ -100,11 +105,16 @@ namespace slskd.Relay var credential = Request.Headers["X-Relay-Credential"].FirstOrDefault(); var filename = Request.Headers["X-Relay-Filename"].FirstOrDefault(); - if (!Relay.RegisteredAgents.Any(a => a.Name == agentName)) + if (!Relay.RegisteredAgents.Any(a => a.Name == agentName) || string.IsNullOrEmpty(credential)) { return Unauthorized(); } + if (string.IsNullOrEmpty(filename)) + { + return BadRequest(); + } + Log.Information("Handling file download for token {Token} from a caller claiming to be agent {Agent}", token, agentName); // note: the token remains valid after the validation attempt, unlike most other endpoints. @@ -155,6 +165,11 @@ namespace slskd.Relay var agentName = Request.Headers["X-Relay-Agent"].FirstOrDefault(); var credential = Request.Headers["X-Relay-Credential"].FirstOrDefault(); + if (!Relay.RegisteredAgents.Any(a => a.Name == agentName) || string.IsNullOrEmpty(credential)) + { + return Unauthorized(); + } + Stream stream = default; string filename = default; @@ -179,11 +194,6 @@ namespace slskd.Relay return BadRequest(); } - if (!Relay.RegisteredAgents.Any(a => a.Name == agentName)) - { - return Unauthorized(); - } - Log.Information("Handling file upload for token {Token} from a caller claiming to be agent {Agent}", token, agentName); // agents must encrypt the Id they were given in the request with the secret they share with the controller, and @@ -240,6 +250,11 @@ namespace slskd.Relay var agentName = Request.Headers["X-Relay-Agent"].FirstOrDefault(); var credential = Request.Headers["X-Relay-Credential"].FirstOrDefault(); + if (!Relay.RegisteredAgents.Any(a => a.Name == agentName) || string.IsNullOrEmpty(credential)) + { + return Unauthorized(); + } + IEnumerable<Share> shares; IFormFile database; @@ -254,11 +269,6 @@ namespace slskd.Relay return BadRequest(); } - if (!Relay.RegisteredAgents.Any(a => a.Name == agentName)) - { - return Unauthorized(); - } - if (!Relay.TryValidateShareUploadCredential(token: guid, agentName, credential)) { Log.Warning("Failed to authenticate share upload from caller claiming to be agent {Agent} using token {Token}", agentName, guid); diff --git a/src/slskd/Relay/API/Hubs/RelayHub.cs b/src/slskd/Relay/API/Hubs/RelayHub.cs index d0e9a962..43f49976 100644 --- a/src/slskd/Relay/API/Hubs/RelayHub.cs +++ b/src/slskd/Relay/API/Hubs/RelayHub.cs @@ -15,6 +15,8 @@ // along with this program. If not, see https://www.gnu.org/licenses/. // </copyright> +using Microsoft.Extensions.Options; + namespace slskd.Relay { using System; @@ -24,6 +26,7 @@ namespace slskd.Relay using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.SignalR; + using NetTools; using Serilog; /// <summary> @@ -74,17 +77,21 @@ namespace slskd.Relay /// Initializes a new instance of the <see cref="RelayHub"/> class. /// </summary> /// <param name="relayService"></param> + /// <param name="optionsMonitor"></param> /// <param name="optionsAtStartup"></param> public RelayHub( IRelayService relayService, + IOptionsMonitor<Options> optionsMonitor, OptionsAtStartup optionsAtStartup) { Relay = relayService; + OptionsMonitor = optionsMonitor; OptionsAtStartup = optionsAtStartup; } private ILogger Log { get; } = Serilog.Log.ForContext<RelayService>(); private IRelayService Relay { get; } + private IOptionsMonitor<Options> OptionsMonitor { get; } private OptionsAtStartup OptionsAtStartup { get; } private RelayMode OperationMode => OptionsAtStartup.Relay.Mode.ToEnum<RelayMode>(); private IPAddress RemoteIpAddress => Context.Features.Get<IHttpConnectionFeature>().RemoteIpAddress; @@ -131,9 +138,25 @@ namespace slskd.Relay /// <exception cref="UnauthorizedAccessException">Thrown when the challenge response is invalid.</exception> public void Login(string agent, string challengeResponse) { + OptionsMonitor.CurrentValue.Relay.Agents.TryGetValue(agent, out var agentOptions); + + if (agentOptions == default) + { + Log.Warning("Unauthorized login attempt from unknown Agent {Agent} (connection {Id}) from {IP}", agent, Context.ConnectionId, RemoteIpAddress); + throw new UnauthorizedAccessException(); + } + + if (!agentOptions.Cidr.Split(',') + .Select(cidr => IPAddressRange.Parse(cidr)) + .Any(range => range.Contains(RemoteIpAddress))) + { + Log.Warning("Unauthorized login attempt by Agent {Agent} (connection {Id}); remote IP address {IP} is not within the configured range {CIDR}", agent, Context.ConnectionId, RemoteIpAddress, agentOptions.Cidr); + throw new UnauthorizedAccessException(); + } + if (!Relay.TryValidateAuthenticationCredential(Context.ConnectionId, agent, challengeResponse)) { - Log.Information("Agent connection {Id} from {IP} authentication failed", Context.ConnectionId, RemoteIpAddress); + Log.Warning("Unauthorized login attempt by Agent {Agent} (connection {Id}) from {IP}; authentication failed", agent, Context.ConnectionId, RemoteIpAddress); Relay.TryDeregisterAgent(Context.ConnectionId, out var _); // just in case! throw new UnauthorizedAccessException(); } |
