Skip to content

Commit aadad0c

Browse files
committed
fix(security): restrict default DllImport search to safe directories
Add assembly-wide [DefaultDllImportSearchPaths(AssemblyDirectory | SafeDirectories)] so the runtime's initial native-library resolution (before our DllImportResolver / FixDllNotFoundException fallback kicks in) does not fall through to the current working directory or other unsafe legacy search locations. Resolves a DLL-hijack vector on Windows where libwebp.dll dropped into CWD could be loaded before our trusted runtimes/<rid>/native/ copy. Available since .NET Framework 4.0; honored across all target frameworks (net472, net48, netstandard2.0, net8.0).
1 parent 51b3c6e commit aadad0c

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

src/Imazen.WebP/AssemblyInfo.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Runtime.InteropServices;
2+
3+
// Restrict the default DLL search to the assembly directory and Windows safe
4+
// directories. This prevents the legacy fallback to the current working
5+
// directory (and other unsafe paths) when the runtime resolves libwebp via
6+
// DllImport before our DllImportResolver / FixDllNotFoundException logic runs.
7+
//
8+
// AssemblyDirectory: equivalent to LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR for the
9+
// loading assembly.
10+
// SafeDirectories: equivalent to LOAD_LIBRARY_SEARCH_SYSTEM32 plus the
11+
// user-added DLL directories.
12+
//
13+
// Available since .NET Framework 4.0; honored on .NET Core / .NET 5+ as well.
14+
[assembly: DefaultDllImportSearchPaths(
15+
DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.SafeDirectories)]

0 commit comments

Comments
 (0)