UPatel007528 (Community Member) asked a question.

Veracode : CWE 404 Improper Resource Shutdown or Release

public static async Task<string> GetToken(string orgCode, string orgID, string apiURL)

{

  string strAccessToken = string.Empty;

  WebRequest webreq = null;

   

  try

  {

    if (!string.IsNullOrEmpty(orgCode) && !string.IsNullOrEmpty(orgID))

    {

      // Enforce TLS 1.2 only; do not weaken with TLS/SSL3.

      ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

 

      var tokenUri = BuildSafeUri(apiURL, $"Authentication/GetToken?orgCode={WebUtility.UrlEncode(orgCode)}&orgId={WebUtility.UrlEncode(orgID)}");

 

      webreq = WebRequest.Create(tokenUri);

      webreq.Method = "GET";

      webreq.ContentType = "application/json; charset=utf-8";

       

      using (var res = await webreq.GetResponseAsync().ConfigureAwait(false))

      using (var stream = res.GetResponseStream())

      using (var streamReader = new StreamReader(stream))

      {

        strAccessToken = await streamReader.ReadToEndAsync().ConfigureAwait(false);

      }

    }

    else

    {

      cEventLog.AppendError(cMessages.enmMessageType.mToken, "OrgCode or OrgID null while get token", "", GetCurrentMethod().Name);

      return string.Empty;

    }

  }

  catch (WebException webEx)

  {

    cEventLog.AppendError(cMessages.enmMessageType.mToken, "Token request WebException: " + webEx.Message, orgCode, GetCurrentMethod().Name);

    return string.Empty;

  }

  catch (Exception ex)

  {

    cEventLog.AppendError(cMessages.enmMessageType.mToken, ex, orgCode, GetCurrentMethod().Name);

    return string.Empty;

  }

  finally

  {

    // CWE-404 FIX: Explicitly abort the WebRequest to release resources

    if (webreq != null)

    {

      webreq.Abort();

      webreq = null;

    }

  }

   

  return strAccessToken;

}


Topics (8)

No articles found
Loading

Ask the Community

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