spring-projects / spring-projects/spring-security

Make DefaultRequestRejectedHandler Return HTTP 400 by default

Open
#13,081 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

in: web type: breaks-passivity type: enhancement
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

Expected Behavior

DefaultRequestRejectedHandler should return HTTP 400 by default instead of having to implement a custom bean.

Sample request ->

curl --location --request GET 'http://localhost:8080/./'
--header 'Content-Type: application/json'
--header 'Accept-Language: en-us'
--header 'Accept: application/json'

Should return ->

{
"timestamp": "2023-04-24T15:21:47.865+00:00",
"status": 400,
"error": "Internal Server Error",
"path": "/./"
}

Current Behavior

curl --location --request GET 'http://localhost:8080/./'
--header 'Content-Type: application/json'
--header 'Accept-Language: en-us'
--header 'Accept: application/json'

Returns->

{
"timestamp": "2023-04-24T15:21:47.865+00:00",
"status": 500,
"error": "Internal Server Error",
"path": "/./"
}

Context

Currently the DefaultRequestRejectedHandler lets the RequestRejectedException bubble out and return an HTTP 500 and has no logging. I think a more accurate status code is HTTP 400. https://github.com/spring-projects/spring-security/blob/main/web/src/main/java/org/springframework/security/web/firewall/DefaultRequestRejectedHandler.java#L37

I was also unable to use HttpStatusRequestRejectedHandler because the log level used is debug when I think the log level should be error. https://github.com/spring-projects/spring-security/blob/main/web/src/main/java/org/springframework/security/web/firewall/HttpStatusRequestRejectedHandler.java#L59

This required me to implement this workaround. Similar to a recommendation made here https://stackoverflow.com/questions/51788764/how-to-intercept-a-requestrejectedexception-in-spring

@Log4j2
public class CustomRequestRejectedHandler implements RequestRejectedHandler {
  @Override
  public void handle(
      HttpServletRequest request,
      HttpServletResponse response,
      RequestRejectedException requestRejectedException)
      throws IOException, ServletException {
    log.error("Framework rejected request.", requestRejectedException);
    response.sendError(HttpStatus.SC_BAD_REQUEST);
  }
}
  @Bean
  public RequestRejectedHandler requestRejectedHandler() {
    return new CustomRequestRejectedHandler();
  }

I do think this is low priority because there is a pretty simple workaround however I do think the correct status code is HTTP 400 so that should be the default behavior.

As a separate issue I think the log level in HttpStatusRequestRejectedHandler should be changed to error.

I'm also willing to open the Pull Request for this change if you consider this a valid enhancement. Please let me know. Thank you in advance!

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.

Research direction

Start with web/src/main/java/org/springframework/security/web/firewall/DefaultRequestRejectedHandler.java, linked in the issue, and compare it with HttpStatusRequestRejectedHandler.java. Verify the current behavior for a rejected /./ request, then ensure the default handler produces HTTP 400 and decide separately whether the logging-level concern belongs in this change.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
security
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.