spring-projects / spring-projects/spring-data-jpa

Add built-in support for Sort field whitelist validation

Open
#4,178 0 comments 2 reactions 1 assignee View on GitHub

@christophstrobl is already working on this.

Since Feb 2, 2026.

status: pending-design-work
Dominant language
Java
Stars
3.3k
Forks
1.6k
PR merge metrics
No merged PRs in 30d

Description

Problem

Currently, when an invalid sort field is passed to a Pageable parameter, Spring Data JPA throws PropertyReferenceException which results in HTTP 500 Internal Server Error. This has several issues:

1. Returns 500 instead of 400/422 - Invalid user input should return a client error, not server error
2. Security concern - Error messages can leak entity structure to clients
3. No whitelist support - All entity fields are allowed by default, but often only a subset should be sortable

Current Behavior

// Request: GET /tasks?sort=nonExistentField,asc

// Results in:
org.springframework.data.mapping.PropertyReferenceException:
No property 'nonExistentField' found for type TaskEntity
// HTTP 500 Internal Server Error

Expected Behavior

  @GetMapping("/tasks")                                                                                                                                                                                                    
  public Page<Task> getTasks(                                                                                                                                                                                              
      @AllowedSortFields({"createdAt", "name", "status"}) Pageable pageable                                                                                                                                                
  ) {                                                                                                                                                                                                                      
      return taskService.findAll(pageable);                                                                                                                                                                                
  }                                                                                                                                                                                                                        
                                                                                                                                                                                                                           
  // Request: GET /tasks?sort=nonExistentField,asc                                                                                                                                                                         
  // HTTP 422 Unprocessable Entity                                                                                                                                                                                         
  // {"error": "invalid-sort-field", "allowed": ["createdAt", "name", "status"]}      

Use Cases

  1. API Security - Restrict sortable fields to prevent information disclosure
  2. Performance - Only allow sorting on indexed columns
  3. Clean API contracts - Document allowed sort fields via annotation

Proposed Solution

Add an annotation like @AllowedSortFields that:

  • Validates sort parameters against a whitelist
  • Returns proper 4xx error with clear message
  • Integrates with HandlerMethodArgumentResolver for Pageable

Workaround

Currently developers must implement custom validation:

private static final Set<String> ALLOWED_SORT_FIELDS = Set.of("createdAt", "name");                                                                                                                                      
                                                                                                                                                                                                                         
public Page<Task> getAll(Pageable pageable) {                                                                                                                                                                            
    pageable.getSort().forEach(order -> {                                                                                                                                                                                
        if (!ALLOWED_SORT_FIELDS.contains(order.getProperty())) {                                                                                                                                                        
            throw new InvalidSortFieldException(order.getProperty());                                                                                                                                                    
        }                                                                                                                                                                                                                
    });                                                                                                                                                                                                                  
    // ...                                                                                                                                                                                                               
}  

This is boilerplate that could be eliminated with framework support.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.