From aa3401f33bb0a6e55324f1f0b67c9946a2df86db Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 23 Aug 2026 02:18:11 -0500 Subject: whitelist log directory --- src/slskd/Files/FileService.cs | 1 + 1 file changed, 1 insertion(+) (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index 62d17f20..6ab2328a 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -61,6 +61,7 @@ namespace slskd.Files { Path.GetFullPath(OptionsMonitor.CurrentValue.Directories.Downloads), Path.GetFullPath(OptionsMonitor.CurrentValue.Directories.Incomplete), + Path.GetFullPath(Program.LogDirectory), }; private ILogger Log { get; } = Serilog.Log.ForContext(); -- cgit v1.2.3 From 96420adbda382a91406c71ecfdddd5878bfb8f9d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 23 Aug 2026 02:18:43 -0500 Subject: rename method for clarity --- src/slskd/Files/API/FilesController.cs | 2 +- src/slskd/Files/FileService.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Files/API/FilesController.cs b/src/slskd/Files/API/FilesController.cs index 8a96d83f..5f30f325 100644 --- a/src/slskd/Files/API/FilesController.cs +++ b/src/slskd/Files/API/FilesController.cs @@ -200,7 +200,7 @@ namespace slskd.Files.API try { - var response = await Files.ListContentsAsync( + var response = await Files.ListDirectoryContentsAsync( directory: requestedDir, enumerationOptions: new EnumerationOptions { diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index 6ab2328a..f348e658 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -301,7 +301,7 @@ namespace slskd.Files /// Thrown if the specified directory has a relative path. /// Thrown if the specified directory does not exist. /// Thrown if the specified root directory is restricted. - public virtual async Task ListContentsAsync(string directory, EnumerationOptions enumerationOptions = null) + public virtual async Task ListDirectoryContentsAsync(string directory, EnumerationOptions enumerationOptions) { ArgumentException.ThrowIfNullOrWhiteSpace(directory); -- cgit v1.2.3 From 1f6c6b1bf962dd03c66f55098704530bb5c4620a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 23 Aug 2026 02:19:09 -0500 Subject: stub function to return file contents --- src/slskd/Files/FileService.cs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index f348e658..a78d4dd4 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -365,6 +365,39 @@ namespace slskd.Files }); } + public virtual Stream GetFileContents(string filename) + { + ArgumentNullException.ThrowIfNullOrWhiteSpace(filename, nameof(filename)); + + if (FileSafety.ContainsTraversalSegments(filename)) + { + Log.Warning("Suspicious attempt to read a file with a filename containing unsafe path segments (one or more of path traversal characters '.' and '..'). Requested file: {File}", filename); + throw new ArgumentException("Filenames containing traversal segments are not allowed"); + } + + // important! we must fully expand the path with GetFullPath() to resolve a given relative directory, like '..' + if (!AllowedDirectories.Any(allowed => filename.StartsWith(allowed + Path.DirectorySeparatorChar))) + { + throw new UnauthorizedException($"Only application-controlled directories can be listed"); + } + + var info = ResolveFileInfo(filename); + + if (!info.Exists) + { + throw new NotFoundException(); + } + + try + { + return new FileStream(filename, FileMode.Open, FileAccess.Read); + } + catch (Exception ex) + { + throw new IOException($"Failed to open file {Path.GetFileName(filename)} for reading: {ex.Message}", ex); + } + } + /// /// Creates a new file with the specified fully qualified and the optional , /// returning a with which the contents of the file can be written. -- cgit v1.2.3 From 8133ee872bfe07e859fc09bf6ea7062c08249ecb Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 23 Aug 2026 18:08:06 -0500 Subject: docs for GetFileContents --- src/slskd/Files/FileService.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index a78d4dd4..c100c7e9 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -365,6 +365,16 @@ namespace slskd.Files }); } + /// + /// Retrieves the contents of the specified as a stream. + /// + /// The file for which contents are to be retrieved. + /// A Stream containing the contents of the file. + /// Thrown if the specified filename is null or contains only whitespace. + /// Thrown if the specified filename contains path traversal segments. + /// Thrown if the specified file is not within an allowed directory. + /// Thrown if the specified file does not exist. + /// Thrown if the file can't be opened for some reason. public virtual Stream GetFileContents(string filename) { ArgumentNullException.ThrowIfNullOrWhiteSpace(filename, nameof(filename)); -- cgit v1.2.3 From e427bc851f09fbda5768ef427de83286ee1b4822 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 24 Aug 2026 17:56:57 -0500 Subject: use r/w fileshare --- src/slskd/Files/FileService.cs | 2 +- src/web/src/components/System/Logs/Live.jsx | 98 ++++++++++++++++++++++++++++ src/web/src/components/System/Logs/index.jsx | 98 ---------------------------- 3 files changed, 99 insertions(+), 99 deletions(-) create mode 100644 src/web/src/components/System/Logs/Live.jsx delete mode 100644 src/web/src/components/System/Logs/index.jsx (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index c100c7e9..e3479210 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -400,7 +400,7 @@ namespace slskd.Files try { - return new FileStream(filename, FileMode.Open, FileAccess.Read); + return new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); } catch (Exception ex) { diff --git a/src/web/src/components/System/Logs/Live.jsx b/src/web/src/components/System/Logs/Live.jsx new file mode 100644 index 00000000..f36258ec --- /dev/null +++ b/src/web/src/components/System/Logs/Live.jsx @@ -0,0 +1,98 @@ +import '../System.css'; +import { createLogsHubConnection } from '../../../lib/hubFactory'; +import { LoaderSegment } from '../../Shared'; +import React, { Component } from 'react'; +import { Table } from 'semantic-ui-react'; + +const initialState = { + connected: false, + logs: [], +}; + +const levels = { + Debug: 'DBG', + Error: 'ERR', + Information: 'INF', + Warning: 'WRN', +}; + +const maxLogs = 500; + +class Logs extends Component { + constructor(props) { + super(props); + + this.state = initialState; + } + + componentDidMount() { + const logsHub = createLogsHubConnection(); + + logsHub.on('buffer', (buffer) => { + this.setState({ + connected: true, + logs: buffer.reverse().slice(0, maxLogs), + }); + }); + + logsHub.on('log', (log) => { + this.setState((previousState) => ({ + connected: true, + logs: [log].concat(previousState.logs).slice(0, maxLogs), + })); + }); + + logsHub.onreconnecting(() => this.setState({ connected: false })); + logsHub.onclose(() => this.setState({ connected: false })); + logsHub.onreconnected(() => this.setState({ connected: true })); + + logsHub.start(); + } + + formatTimestamp = (timestamp) => { + const date = new Date(timestamp); + return `${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`; // eslint-disable-line max-len + }; + + render() { + const { connected, logs } = this.state; + + return ( +
+ {!connected && } + {connected && ( + + + + Timestamp + Level + Message + + + + {logs.map((log) => ( + + {this.formatTimestamp(log.timestamp)} + {levels[log.level] || log.level} + + {log.message} + + + ))} + +
+ )} +
+ ); + } +} + +export default Logs; diff --git a/src/web/src/components/System/Logs/index.jsx b/src/web/src/components/System/Logs/index.jsx deleted file mode 100644 index f36258ec..00000000 --- a/src/web/src/components/System/Logs/index.jsx +++ /dev/null @@ -1,98 +0,0 @@ -import '../System.css'; -import { createLogsHubConnection } from '../../../lib/hubFactory'; -import { LoaderSegment } from '../../Shared'; -import React, { Component } from 'react'; -import { Table } from 'semantic-ui-react'; - -const initialState = { - connected: false, - logs: [], -}; - -const levels = { - Debug: 'DBG', - Error: 'ERR', - Information: 'INF', - Warning: 'WRN', -}; - -const maxLogs = 500; - -class Logs extends Component { - constructor(props) { - super(props); - - this.state = initialState; - } - - componentDidMount() { - const logsHub = createLogsHubConnection(); - - logsHub.on('buffer', (buffer) => { - this.setState({ - connected: true, - logs: buffer.reverse().slice(0, maxLogs), - }); - }); - - logsHub.on('log', (log) => { - this.setState((previousState) => ({ - connected: true, - logs: [log].concat(previousState.logs).slice(0, maxLogs), - })); - }); - - logsHub.onreconnecting(() => this.setState({ connected: false })); - logsHub.onclose(() => this.setState({ connected: false })); - logsHub.onreconnected(() => this.setState({ connected: true })); - - logsHub.start(); - } - - formatTimestamp = (timestamp) => { - const date = new Date(timestamp); - return `${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`; // eslint-disable-line max-len - }; - - render() { - const { connected, logs } = this.state; - - return ( -
- {!connected && } - {connected && ( - - - - Timestamp - Level - Message - - - - {logs.map((log) => ( - - {this.formatTimestamp(log.timestamp)} - {levels[log.level] || log.level} - - {log.message} - - - ))} - -
- )} -
- ); - } -} - -export default Logs; -- cgit v1.2.3 From 400108c9a4a88edb6738309e0b83c9299ca36072 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 2 Oct 2026 19:04:22 -0500 Subject: make enumeration options default as intended --- src/slskd/Files/FileService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index 665fe919..584aab08 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -320,7 +320,7 @@ namespace slskd.Files /// Thrown if the specified directory has a relative path. /// Thrown if the specified directory does not exist. /// Thrown if the specified root directory is restricted. - public virtual async Task ListDirectoryContentsAsync(string directory, EnumerationOptions enumerationOptions) + public virtual async Task ListDirectoryContentsAsync(string directory, EnumerationOptions enumerationOptions = null) { ArgumentException.ThrowIfNullOrWhiteSpace(directory); @@ -352,7 +352,7 @@ namespace slskd.Files try { - var contents = dir.GetFileSystemInfos("*", enumerationOptions); + var contents = dir.GetFileSystemInfos("*", enumerationOptions ?? new EnumerationOptions()); var files = contents .OfType() -- cgit v1.2.3 From c2a9302741e79165978fa91da040b6f7e689cc48 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 3 Oct 2026 13:51:32 -0500 Subject: compensate for late-populated LogDirectory --- src/slskd/Files/FileService.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/slskd/Files/FileService.cs') diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs index 584aab08..70bfc293 100644 --- a/src/slskd/Files/FileService.cs +++ b/src/slskd/Files/FileService.cs @@ -59,10 +59,11 @@ namespace slskd.Files private IEnumerable AllowedDirectories => new[] { - Path.GetFullPath(OptionsMonitor.CurrentValue.Directories.Downloads), - Path.GetFullPath(OptionsMonitor.CurrentValue.Directories.Incomplete), - Path.GetFullPath(Program.LogDirectory), - }; + OptionsMonitor.CurrentValue.Directories.Downloads, + OptionsMonitor.CurrentValue.Directories.Incomplete, + Program.LogDirectory, + }.Where(x => !string.IsNullOrWhiteSpace(x)) + .Select(x => Path.GetFullPath(x)); private ILogger Log { get; } = Serilog.Log.ForContext(); private IOptionsMonitor OptionsMonitor { get; } -- cgit v1.2.3