HSoni202357 (Community Member) asked a question.

External Control of File Name or Path (CWE ID
73)(2 flaws)

public IActionResult RemoveImage(string ImgPath)

{

  if (!string.IsNullOrEmpty( ImgPath ))

  {

    Regex imageExtensionRegex = new Regex( @"^[\w-_. ]+\.(jpg|jpeg|png)$" );

    if (imageExtensionRegex.IsMatch( ImgPath ) && ImgPath.StartsWith( WebHostEnvironment.WebRootPath ))

    {

      if (System.IO.File.Exists( ImgPath ))

      {

        System.IO.File.Delete( ImgPath );

      }

      return Json( new

      {

        result = "success"

      } );

    }

  }

 

In The Above method i have applied validation using regex , but the flaw is not getting fixed,

 

Please suggest


Topics (4)

No articles found
Loading

Ask the Community

Get answers, share a use case, discuss your favorite features, or get input from the community.