forked from halo-dev/halo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: missing ServerWebExchange in plugin template processor extension (…
…halo-dev#6877) #### What type of PR is this? /kind bug /area core /milestone 2.20.x #### What this PR does / why we need it: 修复由 halo-dev#6680 导致的插件模板处理扩展中无法获取到请求上下文的问题 halo-dev#6680 修复了插件可以在模板处理扩展中通过请求上下文获取到 Halo 的 ApplicationContext 的问题 但这也引入了新的问题就是导致模板处理扩展无法获取到请求上下文,此 PR 通过判断传递给插件的 ITemplateContext 是否为 IWebContext,如果是则包装为 SecureTemplateWebContext 传递给插件,以解决此问题 #### Which issue(s) this PR fixes: Fixes halo-dev#6875 #### Does this PR introduce a user-facing change? ```release-note 修复插件模板处理扩展中无法获取到请求上下文的问题 ```
- Loading branch information
Showing
7 changed files
with
74 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
application/src/main/java/run/halo/app/theme/dialect/SecureTemplateContextWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package run.halo.app.theme.dialect; | ||
|
||
import org.thymeleaf.context.Contexts; | ||
import org.thymeleaf.context.ITemplateContext; | ||
|
||
/** | ||
* Wrap the delegate template context to a secure template context according to whether it is a | ||
* WebContext. | ||
* | ||
* @author guqing | ||
* @since 2.20.4 | ||
*/ | ||
public class SecureTemplateContextWrapper { | ||
|
||
/** | ||
* Wrap the delegate template context to a secure template context. | ||
*/ | ||
static SecureTemplateContext wrap(ITemplateContext delegate) { | ||
if (Contexts.isWebContext(delegate)) { | ||
return new SecureTemplateWebContext(delegate); | ||
} | ||
return new SecureTemplateContext(delegate); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
application/src/main/java/run/halo/app/theme/dialect/SecureTemplateWebContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package run.halo.app.theme.dialect; | ||
|
||
import org.springframework.context.ApplicationContext; | ||
import org.thymeleaf.context.ITemplateContext; | ||
import org.thymeleaf.context.IWebContext; | ||
import org.thymeleaf.web.IWebExchange; | ||
|
||
/** | ||
* Secure template web context. | ||
* <p>It's used to prevent some dangerous variables such as {@link ApplicationContext} from being | ||
* accessed. | ||
* | ||
* @author guqing | ||
* @see SecureTemplateContext | ||
* @since 2.20.4 | ||
*/ | ||
class SecureTemplateWebContext extends SecureTemplateContext implements IWebContext { | ||
private final IWebContext delegate; | ||
|
||
/** | ||
* The delegate must be an instance of IWebContext to create a SecureTemplateWebContext. | ||
*/ | ||
public SecureTemplateWebContext(ITemplateContext delegate) { | ||
super(delegate); | ||
if (delegate instanceof IWebContext webContext) { | ||
this.delegate = webContext; | ||
} else { | ||
throw new IllegalArgumentException("The delegate must be an instance of IWebContext"); | ||
} | ||
} | ||
|
||
@Override | ||
public IWebExchange getExchange() { | ||
return delegate.getExchange(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters