diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-04-20 18:38:51 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-04-20 18:38:51 -0500 |
| commit | 6ead594b1421e37a7ba1dcf0617958263d064fdb (patch) | |
| tree | 18c8688fd57469bd335a5ab3998389d8945d1ee9 /src | |
| parent | 95e85cacce4f189e2bc894f81ff53a267e338381 (diff) | |
| parent | 7033851e42d1412c34c4022f6a8dd1632c64586e (diff) | |
| download | slskd-6ead594b1421e37a7ba1dcf0617958263d064fdb.tar.gz | |
Merge pull request #1699 from ofernander/master
feat: add username reg pattern matching to blacklist group
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Core/Options.cs | 16 | ||||
| -rw-r--r-- | src/slskd/Users/UserService.cs | 17 |
2 files changed, 33 insertions, 0 deletions
diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 7b38143e..dda1b7b5 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1150,6 +1150,14 @@ namespace slskd public string[] Members { get; init; } = Array.Empty<string>(); /// <summary> + /// Gets the list of regular expression patterns matched against usernames. + /// Any username matching one or more patterns will be blacklisted. + /// Follows the same case sensitivity rules as other user-defined regular expressions + /// (case insensitive by default; controlled by the <c>flags.case_sensitive_regex</c> option). + /// </summary> + public string[] Patterns { get; init; } = Array.Empty<string>(); + + /// <summary> /// Gets the list of group CIDRs. /// </summary> public string[] Cidrs { get; init; } = Array.Empty<string>(); @@ -1163,6 +1171,14 @@ namespace slskd { var results = new List<ValidationResult>(); + foreach (var pattern in Patterns ?? Array.Empty<string>()) + { + if (!pattern.IsValidRegex()) + { + results.Add(new ValidationResult($"Blacklist username pattern '{pattern}' is not a valid regular expression")); + } + } + foreach (var cidr in Cidrs ?? Array.Empty<string>()) { try diff --git a/src/slskd/Users/UserService.cs b/src/slskd/Users/UserService.cs index d8b24f4d..31c1e540 100644 --- a/src/slskd/Users/UserService.cs +++ b/src/slskd/Users/UserService.cs @@ -38,6 +38,7 @@ namespace slskd.Users using System.Collections.Generic; using System.Linq; using System.Net; + using System.Text.RegularExpressions; using System.Threading.Tasks; using NetTools; using Serilog; @@ -291,6 +292,8 @@ namespace slskd.Users /// <returns>A value indicating whether the specified user and/or IP are blacklisted.</returns> public bool IsBlacklisted(string username, IPAddress ipAddress = null) { + if (username is null) return false; + var blacklist = OptionsMonitor.CurrentValue.Transfers.Groups.Blacklisted; if (blacklist.Members.Contains(username)) @@ -298,6 +301,20 @@ namespace slskd.Users return true; } + // check username patterns (regex). respects the same case sensitivity flag used by other + // user-defined regular expressions throughout the application. + if (blacklist.Patterns.Length > 0) + { + var regexOptions = OptionsMonitor.CurrentValue.Flags.CaseSensitiveRegEx + ? RegexOptions.None + : RegexOptions.IgnoreCase; + + if (blacklist.Patterns.Any(p => Regex.IsMatch(username, p, regexOptions))) + { + return true; + } + } + // check the user-curated list of blacklisted CIDRs that exists along with the list of // blacklisted usernames. these CIDRs should be one-offs and would not be expected to appear in a // blacklist supplied by a third party (but might?) |
