diff options
Diffstat (limited to 'src/slskd/Relay/API')
| -rw-r--r-- | src/slskd/Relay/API/Controllers/RelayController.cs | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/src/slskd/Relay/API/Controllers/RelayController.cs b/src/slskd/Relay/API/Controllers/RelayController.cs index 0d2ef9c5..fdc6dbe6 100644 --- a/src/slskd/Relay/API/Controllers/RelayController.cs +++ b/src/slskd/Relay/API/Controllers/RelayController.cs @@ -149,6 +149,14 @@ namespace slskd.Relay return BadRequest(); } + // check the path; the caller has a valid API key but we haven't validated the credential yet, so this is worth + // doing here instead of relying on the CombineSafely() backstop + if (FileSafety.ContainsTraversalSegments(filename)) + { + Log.Warning("Suspicious attempt to download a file containing unsafe path segments (one or more of path traversal characters '.' and '..'). Requested file: {filename}", filename); + return BadRequest(); + } + Log.Information("Handling file download of {Filename} ({Token}) from a caller claiming to be agent {Agent}", filename, token, agentName); // note: the token remains valid after the validation attempt, unlike most other endpoints. @@ -159,7 +167,12 @@ namespace slskd.Relay return Unauthorized(); } - var sourceFile = Path.Combine(OptionsMonitor.CurrentValue.Directories.Downloads, filename); + // if we get this far, the caller has supplied a valid credential and filename, meaning the request + // to download is being made in response to the controller signaling the agent that a download had completed. + // it's not possible for the client to download arbitrary files; credentials are computed from a token, and + // this token is cached in the controller, so only file downloads that the controller solicited are possible + // still, for the abundance of caution, use the CombineSafely method in case the filename has traversal segments + var sourceFile = FileSafety.CombineSafely(OptionsMonitor.CurrentValue.Directories.Downloads, filename); Log.Information("Agent {Agent} authenticated for token {Token}. Sending file {Filename}", agentName, guid, filename); @@ -225,6 +238,14 @@ namespace slskd.Relay throw new ArgumentException("Upload filename is null or empty"); } + // use an abundance of caution; the caller has a valid API key but we haven't validated the credential yet + // this is a really unlikely attack vector and it wouldn't do anything, but we're taking a hard stance on input validation + if (FileSafety.ContainsTraversalSegments(filename)) + { + Log.Warning("Suspicious attempt to upload a file containing unsafe path segments (one or more of path traversal characters '.' and '..'). Requested file: {filename}", filename); + return BadRequest(); + } + if (stream == null || !stream.CanRead) { throw new ArgumentException("Unable to obtain stream from request"); @@ -323,8 +344,8 @@ namespace slskd.Relay return Unauthorized(); } - Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Program.AppName)); - var temp = Path.Combine(Path.GetTempPath(), Program.AppName, $"share_{agentName}_{Path.GetRandomFileName()}.db"); + Directory.CreateDirectory(FileSafety.CombineSafely(Path.GetTempPath(), Program.AppName)); + var temp = FileSafety.CombineSafely(Path.GetTempPath(), Program.AppName, $"share_{agentName.ReplaceInvalidFileNameCharacters()}_{Path.GetRandomFileName()}.db"); try { |
