An insecure direct object reference (IDOR) occurs when a user changes a direct identifier in a request and reaches a resource they should not access. Exposing database IDs in URLs makes this especially easy when identifiers follow predictable patterns. section 29.4.3 section 24.5
Changing a recipe ID
A recipe application displays an edit link such as /Recipes/Edit/120. A user changes it to /Recipes/Edit/121. If the application allows editing or viewing a recipe the user does not own, it has an IDOR vulnerability. Hiding the Edit button does not prevent the user from entering the URL directly. section 29.4.3
Prevent IDOR by enforcing authorization on the requested resource:
- Load the requested entity, determine its ownership or access information, and authorize the current user inside the endpoint handler. section 24.5
- Return a permission-denied error when the user is not authorized; do not rely on hidden or disabled interface elements. section 29.4.3 section 29.5
- Treat GUIDs as a way to make guessing harder, not as a replacement for authorization checks. section 29.4.3
Listing 24.13 The Edit.cshtml page must load the Recipe
public IActionResult OnGet(int id) #1 { var recipe = _service.GetRecipe(id); #2 var createdById = recipe.CreatedById; #2 // Authorize user based on createdById #3 if(isAuthorized) #4 { #4 return View(recipe); #4 } #4 } #1 The id of the recipe to edit is provided by model binding. #2 You must load the Recipe from the database before you know who created it. #3 You must authorize the current user to verify that they’re allowed to edit this specific Recipe. #4 The action method can continue only if the user was authorized.
Shows the endpoint flow: bind the requested ID, load the resource, authorize the user, and continue only after authorization succeeds. section 24.5

