Make the file upload whitelist configurable and update defaults

This commit is contained in:
tyuXX 2026-10-31 17:35:45 +03:00
parent b8b98994d8
commit e7bc618b9a
6 changed files with 114 additions and 17 deletions

View file

@ -31,5 +31,34 @@
"Username": "openrelayproject", "Username": "openrelayproject",
"Credential": "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"
] ]
} }

View file

@ -88,4 +88,20 @@ public class ServerOptions
public bool RequireAuthForServerInfo { get; set; } = false; public bool RequireAuthForServerInfo { get; set; } = false;
public bool EnableCsrfProtection { get; set; } = true; public bool EnableCsrfProtection { get; set; } = true;
public int MaxWebSocketMessageSizeBytes { get; set; } = 1048576; // 1MB public int MaxWebSocketMessageSizeBytes { get; set; } = 1048576; // 1MB
// File Content Type Filtering
public string FileContentTypeFilterMode { get; set; } = "Whitelist"; // Whitelist or Blacklist
public List<string> 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"
};
} }

View file

@ -114,6 +114,32 @@ public class ServerSettingsManager
options.VapidPrivateKey = priv; 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); Save(options);
return options; return options;
} }

View file

@ -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<string>(
envFileFilters.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries));
}
// Allow CLI overrides e.g. --port 5000 --tls false // Allow CLI overrides e.g. --port 5000 --tls false
for (int i = 0; i < args.Length; i++) 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<string>( serverOptions.StunServers = new System.Collections.Generic.List<string>(
args[i + 1].Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)); 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<string>(
args[i + 1].Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries));
}
} }
builder.Services.AddSingleton(settingsManager); builder.Services.AddSingleton(settingsManager);

View file

@ -15,21 +15,6 @@ public class FileStorageService
private readonly ServerOptions _options; private readonly ServerOptions _options;
private readonly string _filesDirectory; private readonly string _filesDirectory;
// Whitelist of allowed MIME types
private static readonly HashSet<string> 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) public FileStorageService(ChatDatabase db, ServerOptions options)
{ {
_db = db; _db = db;
@ -45,7 +30,28 @@ public class FileStorageService
return false; return false;
} }
return AllowedContentTypes.Contains(contentType.Trim()); string trimmedContentType = contentType.Trim();
var filters = _options.FileContentTypeFilters ?? new List<string>();
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<FileRecord> StoreFileAsync( public async Task<FileRecord> StoreFileAsync(

View file

@ -39,5 +39,9 @@
"InviteTokenExpirationHours": 168, "InviteTokenExpirationHours": 168,
"RequireAuthForServerInfo": false, "RequireAuthForServerInfo": false,
"EnableCsrfProtection": true, "EnableCsrfProtection": true,
"MaxWebSocketMessageSizeBytes": 1048576 "MaxWebSocketMessageSizeBytes": 1048576,
"FileContentTypeFilterMode": "Blacklist",
"FileContentTypeFilters": [
"application/x-msdownload"
]
} }