diff options
| author | ofernander <oliver@fernander.net> | 2026-04-12 18:40:01 -0700 |
|---|---|---|
| committer | ofernander <oliver@fernander.net> | 2026-04-12 18:40:01 -0700 |
| commit | 7033851e42d1412c34c4022f6a8dd1632c64586e (patch) | |
| tree | 6531d20073126d8659fe4de93d22d7fc2af5f4f3 | |
| parent | 76ccc5c1c8723df586cd4b4fc3d692cb2a7d73f6 (diff) | |
| download | slskd-7033851e42d1412c34c4022f6a8dd1632c64586e.tar.gz | |
feat: add username reg pattern matching to blacklist group
Adds a field to that accepts
a list of regular expressions matched against usernames.
| -rw-r--r-- | config/slskd.example.yml | 2 | ||||
| -rw-r--r-- | docs/config.md | 4 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 16 | ||||
| -rw-r--r-- | src/slskd/Users/UserService.cs | 17 |
4 files changed, 39 insertions, 0 deletions
diff --git a/config/slskd.example.yml b/config/slskd.example.yml index 72aadaed..ee72a2e0 100644 --- a/config/slskd.example.yml +++ b/config/slskd.example.yml @@ -112,6 +112,8 @@ # blacklisted: # members: # - <username to blacklist> +# patterns: +# - <regular expression to match against usernames> # cidrs: # - <CIDR to blacklist, e.g. 255.255.255.255/32> # user_defined: diff --git a/docs/config.md b/docs/config.md index 6ba449f0..1b77a674 100644 --- a/docs/config.md +++ b/docs/config.md @@ -489,6 +489,8 @@ Private and chat room messages from blacklisted users are also ignored. Users can be blacklisted by adding their username to the `members` list. Additionally, users can be blacklisted by IP address, or range of addresses by adding a CIDR entry to the `cidrs` list. +The `patterns` list accepts regular expressions matched against usernames. Any username matching one or more patterns will be blacklisted. This is useful for blocking bots or automated clients that share a common username prefix or suffix — for example, `^user` would block any username starting with `user`, and `_bot$` would block any username ending with `_bot`. Patterns follow the same case sensitivity rules as other user-defined regular expressions in the application — case insensitive by default, controlled by the `flags.case_sensitive_regex` option. + Users added to the blacklist will be blocked from enqueueing any new files. Any existing active or queued transfers will need to be cancelled manually. A managed blacklist file may also be used to achieve the same effects. Read more about this in the [Managed Blacklist](#managed-blacklist) section below. @@ -499,6 +501,8 @@ groups: blacklisted: members: - <username to blacklist> + patterns: + - <regular expression to match against usernames> cidrs: - <CIDR to blacklist, e.g. 255.255.255.255/32> ``` 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?) |
