java-topology/defects/btcpayserver-0003/patch/btcpayserver-0003.patch
russell@unturf.com c2a1fc15cc undf: assign 928-930; btcpayserver CWE-407 scan (3 defects)
btcpayserver-0001: WalletTransactionInfo.Merge Attachments.Any O(A*B) MEDIUM
btcpayserver-0002: AppService gap-fill series.All O(D*S) LOW-MEDIUM
btcpayserver-0003: StringExtensions.IsValidFileName GetInvalidFileNameChars O(F*I) MEDIUM
2026-03-31 09:39:59 -04:00

26 lines
1.2 KiB
Diff

# UNDF: UNDF-2026-000000930
--- a/BTCPayServer.Abstractions/Extensions/StringExtensions.cs
+++ b/BTCPayServer.Abstractions/Extensions/StringExtensions.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -6,9 +7,16 @@
public static class StringExtensions
{
+ // CWE-407: Path.GetInvalidFileNameChars() allocates a new char[] on every call.
+ // Contains() on char[] is O(I) per character. For a filename of length F, this
+ // is O(F * I) with I = ~41 invalid chars, plus F array allocations.
+ // Fix: cache in a static HashSet<char> for O(1) lookup and zero allocation.
+ private static readonly HashSet<char> InvalidFileNameChars = new HashSet<char>(Path.GetInvalidFileNameChars());
+
public static bool IsValidFileName(this string fileName)
{
- return !fileName.ToCharArray().Any(c => Path.GetInvalidFileNameChars().Contains(c)
+ return !fileName.ToCharArray().Any(c => InvalidFileNameChars.Contains(c)
|| c == Path.AltDirectorySeparatorChar
|| c == Path.DirectorySeparatorChar
|| c == Path.PathSeparator