From e7bc618b9a4dc418373dbfd0413c337e716fc67b Mon Sep 17 00:00:00 2001 From: tyuXX Date: Sat, 31 Oct 2026 17:35:45 +0300 Subject: [PATCH] Make the file upload whitelist configurable and update defaults --- serversettings.json | 29 ++++++++++++++ .../Configuration/ServerOptions.cs | 16 ++++++++ .../Configuration/ServerSettingsManager.cs | 26 +++++++++++++ src/GsDChat.Server/Program.cs | 16 ++++++++ .../Services/FileStorageService.cs | 38 +++++++++++-------- src/GsDChat.Server/serversettings.json | 6 ++- 6 files changed, 114 insertions(+), 17 deletions(-) diff --git a/serversettings.json b/serversettings.json index 67a96cd..8d1340a 100644 --- a/serversettings.json +++ b/serversettings.json @@ -31,5 +31,34 @@ "Username": "openrelayproject", "Credential": "openrelayproject" } + ], + "RateLimitRequestsPerMinute": 60, + "MaxConnectionsPerIp": 10, + "MaxGlobalConnections": 100, + "MaxConcurrentSessionsPerUser": 5, + "InviteTokenExpirationHours": 168, + "RequireAuthForServerInfo": false, + "EnableCsrfProtection": true, + "MaxWebSocketMessageSizeBytes": 1048576, + "FileContentTypeFilterMode": "Whitelist", + "FileContentTypeFilters": [ + "image/jpeg", + "image/png", + "image/gif", + "image/webp", + "image/svg+xml", + "application/pdf", + "text/plain", + "text/csv", + "application/octet-stream", + "application/zip", + "application/x-rar-compressed", + "application/x-7z-compressed", + "audio/mpeg", + "audio/wav", + "audio/ogg", + "video/mp4", + "video/webm", + "video/quicktime" ] } diff --git a/src/GsDChat.Server/Configuration/ServerOptions.cs b/src/GsDChat.Server/Configuration/ServerOptions.cs index 0a6382b..a50413b 100644 --- a/src/GsDChat.Server/Configuration/ServerOptions.cs +++ b/src/GsDChat.Server/Configuration/ServerOptions.cs @@ -88,4 +88,20 @@ public class ServerOptions public bool RequireAuthForServerInfo { get; set; } = false; public bool EnableCsrfProtection { get; set; } = true; public int MaxWebSocketMessageSizeBytes { get; set; } = 1048576; // 1MB + + // File Content Type Filtering + public string FileContentTypeFilterMode { get; set; } = "Whitelist"; // Whitelist or Blacklist + public List FileContentTypeFilters { get; set; } = new() + { + // Images + "image/jpeg", "image/png", "image/gif", "image/webp", "image/svg+xml", + // Documents + "application/pdf", "text/plain", "text/csv", "application/octet-stream", + // Archives + "application/zip", "application/x-rar-compressed", "application/x-7z-compressed", + // Audio + "audio/mpeg", "audio/wav", "audio/ogg", + // Video + "video/mp4", "video/webm", "video/quicktime" + }; } diff --git a/src/GsDChat.Server/Configuration/ServerSettingsManager.cs b/src/GsDChat.Server/Configuration/ServerSettingsManager.cs index 06771b2..c1f1f40 100644 --- a/src/GsDChat.Server/Configuration/ServerSettingsManager.cs +++ b/src/GsDChat.Server/Configuration/ServerSettingsManager.cs @@ -114,6 +114,32 @@ public class ServerSettingsManager options.VapidPrivateKey = priv; } + // Ensure file content type filter mode is valid + if (string.IsNullOrWhiteSpace(options.FileContentTypeFilterMode) || + (!string.Equals(options.FileContentTypeFilterMode, "Whitelist", StringComparison.OrdinalIgnoreCase) && + !string.Equals(options.FileContentTypeFilterMode, "Blacklist", StringComparison.OrdinalIgnoreCase))) + { + options.FileContentTypeFilterMode = "Whitelist"; + } + + // Ensure file content type filters list is initialized + if (options.FileContentTypeFilters == null) + { + options.FileContentTypeFilters = new() + { + // Images + "image/jpeg", "image/png", "image/gif", "image/webp", "image/svg+xml", + // Documents + "application/pdf", "text/plain", "text/csv", "application/octet-stream", + // Archives + "application/zip", "application/x-rar-compressed", "application/x-7z-compressed", + // Audio + "audio/mpeg", "audio/wav", "audio/ogg", + // Video + "video/mp4", "video/webm", "video/quicktime" + }; + } + Save(options); return options; } diff --git a/src/GsDChat.Server/Program.cs b/src/GsDChat.Server/Program.cs index 3be0ecb..11c1ec1 100644 --- a/src/GsDChat.Server/Program.cs +++ b/src/GsDChat.Server/Program.cs @@ -95,6 +95,16 @@ if (!string.IsNullOrEmpty(envTurnUrl)) }); } +string? envFileFilterMode = Environment.GetEnvironmentVariable("GSDCHAT_FILE_CONTENT_TYPE_FILTER_MODE"); +if (!string.IsNullOrEmpty(envFileFilterMode)) serverOptions.FileContentTypeFilterMode = envFileFilterMode; + +string? envFileFilters = Environment.GetEnvironmentVariable("GSDCHAT_FILE_CONTENT_TYPE_FILTERS"); +if (!string.IsNullOrEmpty(envFileFilters)) +{ + serverOptions.FileContentTypeFilters = new System.Collections.Generic.List( + envFileFilters.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)); +} + // Allow CLI overrides e.g. --port 5000 --tls false for (int i = 0; i < args.Length; i++) { @@ -115,6 +125,12 @@ for (int i = 0; i < args.Length; i++) serverOptions.StunServers = new System.Collections.Generic.List( args[i + 1].Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)); } + if (args[i] == "--file-content-type-filter-mode" && i + 1 < args.Length) serverOptions.FileContentTypeFilterMode = args[i + 1]; + if (args[i] == "--file-content-type-filters" && i + 1 < args.Length && !string.IsNullOrWhiteSpace(args[i + 1])) + { + serverOptions.FileContentTypeFilters = new System.Collections.Generic.List( + args[i + 1].Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)); + } } builder.Services.AddSingleton(settingsManager); diff --git a/src/GsDChat.Server/Services/FileStorageService.cs b/src/GsDChat.Server/Services/FileStorageService.cs index f7d1c40..af48850 100644 --- a/src/GsDChat.Server/Services/FileStorageService.cs +++ b/src/GsDChat.Server/Services/FileStorageService.cs @@ -15,21 +15,6 @@ public class FileStorageService private readonly ServerOptions _options; private readonly string _filesDirectory; - // Whitelist of allowed MIME types - private static readonly HashSet AllowedContentTypes = new(StringComparer.OrdinalIgnoreCase) - { - // Images - "image/jpeg", "image/png", "image/gif", "image/webp", "image/svg+xml", - // Documents - "application/pdf", "text/plain", "text/csv", "application/octet-stream", - // Archives - "application/zip", "application/x-rar-compressed", "application/x-7z-compressed", - // Audio - "audio/mpeg", "audio/wav", "audio/ogg", - // Video - "video/mp4", "video/webm", "video/quicktime" - }; - public FileStorageService(ChatDatabase db, ServerOptions options) { _db = db; @@ -45,7 +30,28 @@ public class FileStorageService return false; } - return AllowedContentTypes.Contains(contentType.Trim()); + string trimmedContentType = contentType.Trim(); + var filters = _options.FileContentTypeFilters ?? new List(); + + if (filters.Count == 0) + { + // If no filters are configured, allow all content types (or block all depending on default behavior) + // Default to allow all if configuration is empty + return true; + } + + bool isWhitelist = string.Equals(_options.FileContentTypeFilterMode, "Whitelist", StringComparison.OrdinalIgnoreCase); + + if (isWhitelist) + { + // Whitelist mode: only allow content types in the list + return filters.Contains(trimmedContentType, StringComparer.OrdinalIgnoreCase); + } + else + { + // Blacklist mode: block content types in the list + return !filters.Contains(trimmedContentType, StringComparer.OrdinalIgnoreCase); + } } public async Task StoreFileAsync( diff --git a/src/GsDChat.Server/serversettings.json b/src/GsDChat.Server/serversettings.json index 4166636..5538e91 100644 --- a/src/GsDChat.Server/serversettings.json +++ b/src/GsDChat.Server/serversettings.json @@ -39,5 +39,9 @@ "InviteTokenExpirationHours": 168, "RequireAuthForServerInfo": false, "EnableCsrfProtection": true, - "MaxWebSocketMessageSizeBytes": 1048576 + "MaxWebSocketMessageSizeBytes": 1048576, + "FileContentTypeFilterMode": "Blacklist", + "FileContentTypeFilters": [ + "application/x-msdownload" + ] } \ No newline at end of file