logo

Inappropriate coding practices - Unused properties - Csharp


Need

Elimination of unused properties in the application code


Context

  1. Usage of C# 7.0 for modern language features and enhancements
  2. Usage of Microsoft.AspNetCore.Mvc for building web applications using the ASP.NET Core MVC framework
  3. Usage of Microsoft.EntityFrameworkCore for database access and management in .NET applications

Description

Insecure Code Example

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Department { get; set; }
    public string UnusedProperty1 { get; set; }
    public string UnusedProperty2 { get; set; }
}

public class EmployeeController : Controller
{
    private readonly ApplicationDbContext _context;

    public EmployeeController(ApplicationDbContext context)
    {
        _context = context;
    }

    public IActionResult Index()
    {
        var employees = _context.Employees.Select(e => new 
        {
            e.Id,
            e.Name,
            e.Department
        }).ToList();

        return View(employees);
    }
}

In the above code, the `Employee` class defines two properties, `UnusedProperty1` and `UnusedProperty2`, which are not used anywhere in the application. They are not used in any method in the `EmployeeController`, nor are they used in any views or models. These properties add unnecessary complexity to the code and may lead to performance issues. The `EmployeeController`'s `Index` method selects only the `Id`, `Name`, and `Department` properties from the `Employee` entities in the database. The `UnusedProperty1` and `UnusedProperty2` properties are not selected or used in any way, making them unnecessary. This issue can lead to confusion for developers working on the code, as they may not know whether these properties are important or not. Additionally, it can lead to wasted resources, as the application may allocate memory for these properties even though they are not used. In terms of security, while this issue does not directly lead to any known vulnerabilities, it is still a bad practice as it can make the code harder to maintain and understand, increasing the likelihood of bugs and vulnerabilities being introduced in the future.

Steps

  1. Identify and remove the unused properties in the Employee class.
  2. Ensure that the removal of the unused properties does not impact any other parts of the application.
  3. Update the database schema or migration scripts to reflect the changes made to the Employee class.
  4. Update any other code that interacts with the Employee class to remove references to the unused properties.
  5. Test the application to ensure that it functions correctly after removing the unused properties.

Secure Code Example

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Department { get; set; }
}

public class EmployeeController : Controller
{
    private readonly ApplicationDbContext _context;

    public EmployeeController(ApplicationDbContext context)
    {
        _context = context;
    }

    public IActionResult Index()
    {
        var employees = _context.Employees.Select(e => new 
        {
            e.Id,
            e.Name,
            e.Department
        }).ToList();

        return View(employees);
    }
}

The unused properties `UnusedProperty1` and `UnusedProperty2` have been removed from the `Employee` class. This reduces unnecessary complexity in the source code and optimizes the application's performance. The `EmployeeController` class, which interacts with the `Employee` class, has also been updated to reflect these changes. The `Index` method, which retrieves a list of employees, no longer includes the unused properties in its selection. Before deploying these changes, ensure that the removal of these properties does not impact any other parts of the application. Update the database schema or migration scripts to reflect the changes made to the `Employee` class. Test the application thoroughly to ensure that it functions correctly after the removal of the unused properties.


References

  • 391 - Inappropriate coding practices - Unused properties

  • Last updated

    2023/09/18