Fix SpEL injection (potential RCE) in DsSpelExpressionProcessor#767
Merged
Fix SpEL injection (potential RCE) in DsSpelExpressionProcessor#767
Conversation
Agent-Logs-Url: https://github.com/baomidou/dynamic-datasource/sessions/db0ccd4f-03f6-4326-8c42-5d5f0a35961a Co-authored-by: huayanYu <16700837+huayanYu@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix SpEL injection vulnerability in dynamic-datasource
Fix SpEL injection (potential RCE) in DsSpelExpressionProcessor
Mar 31, 2026
Member
|
@copilot 正确的修改方案应该提供开关,默认禁用,手动配置了允许则保持原样,用户应该自己意识到手动开启后带来的危险。 |
Agent-Logs-Url: https://github.com/baomidou/dynamic-datasource/sessions/c4f1a2e2-7b0c-4023-b77b-c0a660bd855f Co-authored-by: huayanYu <16700837+huayanYu@users.noreply.github.com>
Contributor
Author
Done in commit
To restore the original (unrestricted) behavior, users explicitly set: spring:
datasource:
dynamic:
aop:
allowed-spel-type-access: trueWarning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
There was a problem hiding this comment.
Pull request overview
Hardens SpEL evaluation in DsSpelExpressionProcessor to reduce the risk of SpEL injection leading to RCE when resolving datasource keys, and exposes an opt-out configuration flag to restore legacy (unsafe) behavior.
Changes:
- Add a default “restricted” mode that blocks SpEL
T(...)type references andnewconstructor expressions during datasource key evaluation. - Introduce
allowedSpelTypeAccessconfiguration (properties + auto-configuration wiring) to opt back into the original unrestricted behavior. - Add unit tests validating default blocking behavior and opt-in behavior.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/processor/DsSpelExpressionProcessor.java | Adds restricted SpEL evaluation mode and an opt-out setter. |
| dynamic-datasource-spring-boot-common/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDatasourceAopProperties.java | Adds allowedSpelTypeAccess property for configuration binding. |
| dynamic-datasource-spring-boot-starter/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceAopConfiguration.java | Wires the new property into DsSpelExpressionProcessor (Boot 2 line). |
| dynamic-datasource-spring-boot3-starter/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceAopConfiguration.java | Wires the new property into DsSpelExpressionProcessor (Boot 3 line). |
| dynamic-datasource-spring-boot4-starter/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceAopConfiguration.java | Wires the new property into DsSpelExpressionProcessor (Boot 4 line). |
| dynamic-datasource-spring-boot3-starter/src/test/java/com/baomidou/dynamic/datasource/common/v3/DsSpelExpressionProcessorSecurityTest.java | Adds tests covering restricted default behavior and opt-in behavior. |
Comment on lines
+99
to
+105
| if (!allowedSpelTypeAccess) { | ||
| // Prevent SpEL injection: block T(...) type references and new instance creation | ||
| context.setTypeLocator(typeName -> { | ||
| throw new EvaluationException("Type access is not allowed in DS SpEL expressions: " + typeName); | ||
| }); | ||
| context.setConstructorResolvers(Collections.emptyList()); | ||
| } |
| * Set to true only if your application genuinely requires such expressions and you | ||
| * fully understand the security risk. | ||
| */ | ||
| private Boolean allowedSpelTypeAccess = false; |
Comment on lines
+61
to
+71
| // T(...) type references must be blocked to prevent SpEL injection / RCE | ||
| assertThrows(EvaluationException.class, () -> | ||
| processor.doDetermineDatasource(invocation, "T(java.lang.Runtime).getRuntime().exec('id')") | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void newInstanceExpressionShouldBeBlockedByDefault() { | ||
| // new Type(...) constructor invocations must also be blocked | ||
| assertThrows(EvaluationException.class, () -> | ||
| processor.doDetermineDatasource(invocation, "new java.lang.ProcessBuilder('id').start()") |
Comment on lines
+69
to
+71
| // new Type(...) constructor invocations must also be blocked | ||
| assertThrows(EvaluationException.class, () -> | ||
| processor.doDetermineDatasource(invocation, "new java.lang.ProcessBuilder('id').start()") |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
DsSpelExpressionProcessorevaluated@DSannotation values usingStandardEvaluationContextwith no restrictions, allowingT(java.lang.Runtime)type references and constructor expressions to execute arbitrary code if an attacker-controlled value reached the SpEL evaluator.Changes
DsSpelExpressionProcessor.java: Harden theStandardEvaluationContextbefore evaluation (enabled by default, opt-out via configuration):TypeLocatorto throwEvaluationExceptionon any type lookup — blocksT(...)expressionsnew SomeClass(...)expressions@beanName), parameter access (#param), and property traversal (#obj.field) are unaffectedallowedSpelTypeAccessfield (defaultfalse) andsetAllowedSpelTypeAccess(boolean)setter allow users to explicitly opt in to the original unrestricted behaviorDynamicDatasourceAopProperties.java: NewallowedSpelTypeAccessproperty (defaultfalse) exposed asspring.datasource.dynamic.aop.allowed-spel-type-access. Set totrueto restore the original behavior — only do this if you fully understand the security implications.DynamicDataSourceAopConfiguration.java(Spring Boot 2, 3, and 4 variants): WireallowedSpelTypeAccessfrom properties intoDsSpelExpressionProcessor.DsSpelExpressionProcessorSecurityTest: Unit tests confirming#paramresolution still works, thatT(java.lang.Runtime).getRuntime().exec('id')andnew java.lang.ProcessBuilder('id').start()both throwEvaluationExceptionby default, and that settingallowedSpelTypeAccess=truelifts the restriction.Warning
Setting
spring.datasource.dynamic.aop.allowed-spel-type-access=truerestores the original unrestricted SpEL evaluation and exposes your application to SpEL injection / RCE if datasource key expressions can be influenced by untrusted input. Only enable this if you genuinely needT(...)ornewexpressions and accept the risk.