From 01a60e7f7eb25010dd35c1b3751906296d4e8733 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 14 Apr 2024 17:59:40 -0500 Subject: guard against malformed lines --- src/slskd/Core/Blacklist.cs | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/slskd/Core/Blacklist.cs b/src/slskd/Core/Blacklist.cs index b91c9384..1f55e1ba 100644 --- a/src/slskd/Core/Blacklist.cs +++ b/src/slskd/Core/Blacklist.cs @@ -133,20 +133,27 @@ public class Blacklist note: we should never get this far without format being one of CIDR, P2P, DAT */ - // CIDR format: 1.2.4.0/24 - if (format == BlacklistFormat.CIDR && !IPAddressRange.TryParse(line, out cidr)) + try { - throw new FormatException($"Failed to parse blacklist line {lineNumber} '{line}' (expected {format} format)"); + // CIDR format: 1.2.4.0/24 + if (format == BlacklistFormat.CIDR && !IPAddressRange.TryParse(line, out cidr)) + { + throw new Exception(); + } + + // P2P format: China Internet Information Center (CNNIC):1.2.4.0-1.2.4.255 + else if (format == BlacklistFormat.P2P && !IPAddressRange.TryParse(line.Split(':')[1], out cidr)) + { + throw new Exception(); + } + + // DAT format: 001.002.004.000 - 001.002.004.255 , 000 , China Internet Information Center (CNNIC) + else if (format == BlacklistFormat.DAT && !IPAddressRange.TryParse(line.Split(",")[0], out cidr)) + { + throw new Exception(); + } } - - // P2P format: China Internet Information Center (CNNIC):1.2.4.0-1.2.4.255 - else if (format == BlacklistFormat.P2P && !IPAddressRange.TryParse(line.Split(':')[1], out cidr)) - { - throw new FormatException($"Failed to parse blacklist line {lineNumber} '{line}' (expected {format} format)"); - } - - // DAT format: 001.002.004.000 - 001.002.004.255 , 000 , China Internet Information Center (CNNIC) - else if (format == BlacklistFormat.DAT && !IPAddressRange.TryParse(line.Split(",")[0], out cidr)) + catch { throw new FormatException($"Failed to parse blacklist line {lineNumber} '{line}' (expected {format} format)"); } -- cgit v1.2.3