fabianwennink / IconCaptcha-ASP.NET

IconCaptcha is a fast, simple and user-friendly captcha. - ASP.NET MVC package
MIT License
15 stars 3 forks source link

Fix EmbeddedResource Path in Linux #10

Closed Vu0r1-sec closed 1 month ago

Vu0r1-sec commented 1 month ago

Description of the Bug: While using the library in a project running on Debian, I encountered a 500 Internal Server Error when accessing the endpoint: GET /iconcaptcha?payload=xxxxxxxxx. The issue stems from the fact that the paths for embedded resources are in the form icons\light\icon-{id}.png. On Windows, Path.Combine(iconPath, $"icon-{id}.png") produces the correct path. However, on Linux, the \ characters are replaced by /, which causes path resolution issues.

Exception:

Microsoft.AspNetCore.Server.Kestrel[13] Connection id "0HN6CRV19TEAU", Request id "0HN6CRV19TEAU:00000001": An unhandled exception was thrown by the application. System.ArgumentNullException: Value cannot be null. (Parameter 'data')
    at SkiaSharp.SKImage.FromEncodedData(Stream data)
    at IconCaptcha.IconCaptchaService.CreateImage(Stream file)
    at IconCaptcha.IconCaptchaService.<>c__DisplayClass34_0.<GenerateImage>b__1(Int32 id)
    at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](List`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
    at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
    at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
    at IconCaptcha.IconCaptchaService.GenerateImage(CaptchaChallenge challenge, String iconPath, Stream placeholderStream, Boolean embeddedFiles)
    at IconCaptcha.IconCaptchaService.GetImage(Int64 identifier)
    at IconCaptcha.IconCaptchaService.GetImage(Int64 identifier)
    at IconCaptcha.IconCaptchaMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)
    at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.InterfaceMiddlewareBinder.<>c__DisplayClass2_0.<<CreateMiddleware>b__0>d.MoveNext() 
--- End of stack trace from previous location ---
    at Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.InvokeCore(HttpContext context, PathString matchedPath, PathString remainingPath)
    at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
    at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context) 
    at Program.<>c.<<<Main>$>b__0_6>d.MoveNext() in XXXXXXXXX\Program.cs:line 85
 --- End of stack trace from previous location ---
    at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
    at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
    at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

Solution: Updated the resource path handling to use . as the separator instead of \ or /. This ensures consistent formatting across different operating systems and corrects the resource retrieval process.

Vu0r1-sec commented 1 month ago

Thank you for reviewing this PR! 😊 I appreciate all the hard work that goes into maintaining the project. If you have any questions or need further adjustments, please let me know!

fabianwennink commented 1 month ago

Hi @Vu0r1-sec , thank you for the PR! I'll make sure to review it this weekend.

fabianwennink commented 1 month ago

Hi, I merged your PR. The provided solution appears to work, I just made some minor formatting changes. Thanks again for the PR. I'll push an update to NuGet shortly.

Vu0r1-sec commented 1 month ago

Hi @fabianwennink, Thank you for merging my PR and for the adjustments you made. I'm glad to hear that the solution works as expected. Looking forward to the update on NuGet. Thanks again for your work on this project and for your valuable contributions to the community.