apache / apache/grails-core

`GrailsWebRequestFilter` doesn't `clearGrailsWebRequest` when `isIncludeOrForward` && `previous == null`

Open
#13,593 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

### Expected Behavior

We are calling a Grails servlet from another servlet to handle some URLs in a backwards compatible way.

So basically we have:

```kotlin
object : HttpServlet() {
override fun doGet(req: HttpServletRequest, resp: HttpServletResponse) {
val dispatcher = grailsContext.servletContext.getRequestDispatcher()
// Forward to some other path in Grails
dispatcher.forward(req, resp)
}
}
```

I agree this is probably a very uncommon usecase but for us it is the only way.

After `dispatcher.forward(...)` the `RequestContextHolder` should always be empty again, right?

### Actual Behaviour

Actually, `GrailsWebRequestFilter` incorrectly doesn't reset `RequetContextHolder` if the request is an include or forward.

Why? I don't think there really is a reason, it is just a `else` that was never written.

The code currently is:

```java
if(isIncludeOrForward) {
if(previous != null) {
WebUtils.storeGrailsWebRequest(previous);
}
}
else {

WebUtils.clearGrailsWebRequest();
LocaleContextHolder.setLocale(null);
}
```

I think it should be:

```java
if (previous != null) {
WebUtils.storeGrailsWebRequest(previous);
} else {
WebUtils.clearGrailsWebRequest();
}
```

Reason: We must ALWAYS reset the world to how we found it before. So either reset the previous or clear it.

### Steps To Reproduce

_No response_

### Environment Information

_No response_

### Example Application

_No response_

### Version

Latest

Contributor guide

Open the contributing guide

Research direction

Find GrailsWebRequestFilter and inspect its cleanup handling for include or forward requests when the previous web request is null. Trace how WebUtils and RequestContextHolder are restored, then verify that a forwarded request leaves the context empty when no previous request existed.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
backend, web-dev
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.