Insecure direct object references

Insecure direct object references in C#

Description

Check that the caller may view the requested profile or account. A user ID from the request identifies a record but does not establish ownership.

Potential impact

Another user’s personal or payment details, administrative records or internal identifiers may be exposed.

Remediation

Authenticate the caller and check ownership on the server before retrieving the record. If administrators need access to other accounts, authorize that access with a separate policy.

Examples

Before

csharp
[HttpGet("profile/{id}")]
public IActionResult GetProfile(int id)
{
    return Ok(repository.Find(id));
}

After

csharp
[Authorize]
[HttpGet("profile/{id}")]
public IActionResult GetProfile(int id)
{
    if (id != CurrentUserId()) return Forbid();
    return Ok(repository.Find(id));
}

Implement CurrentUserId() using the validated principal. The example returns Forbid() for a different id and looks up the record only when the IDs match.

References