spring-framework: Inconsistent validation order for different inputs and authorization

Spring Boot version: 2.5.4

Consider the following controller:

@RestController
@Validated
public class FooController {

    @PutMapping(
        value    = "/lorem/{loremId}",
        consumes = "text/csv",
        produces = "application/json"
    )
    @PreAuthorize("@authz.isAuthorizedForXyz(#loremId, #ipsumId)")
    public ResponseEntity<List<Bar>> handlePutBar(@PathVariable @Size(max = 16) String loremId,
                                                  @RequestParam(name = "ipsumId") @Size(max = 32) String ipsumId,
                                                  @RequestBody @Valid BarsHolder barsHolder) {
        // ...
    }
}

In this case only barsHolder is validated before the method isAuthorizedForXyz() is called for authorization. Both loremId and ipsumId are validated afterwards, straight before entering the controller logic. Basically, the method used for authorization is receiving unvalidated input here.

(Adding @Valid prior to loremId / ipsumId won’t change behavior…)

Enhancement:

Validate all input parameters before authorization logic is applied as already done for @RequestBody annotated parameters. This allows to have validation logic consistently on the controller method.

Related:

https://github.com/spring-projects/spring-security/issues/6545 https://github.com/spring-projects/spring-boot/issues/10157

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 15 (3 by maintainers)

Most upvoted comments

Thanks for the suggestion. This is out of Spring Boot’s control as it’s Spring Framework that performs the validation of the method arguments. FWIW, I’m surprised by the difference in validation between the @RequestBody-annotated parameter and the @RequestParam-annotated parameters. We’ll transfer this to the Framework team so that they can take a look.

Also curious about any updates, my team is running into the same issue.

It poses a major problem for UUID parameters since the default UUID property editor adds padding for the PreAuthorize method parameter and then afterward runs the validation. So some invalid incoming UUIDs are made “valid” first using padding, authorized incorrectly (in our flow), and then validated uselessly.