-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[improve] improve jndi validation #3358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
- Added
validateJmxUrl
to block unsafe protocols and injection patterns - Introduced
isValidHostname
regex checks and port range enforcement in preCheck and at connection time - Hardened connection setup with security properties and timeouts
Comments suppressed due to low confidence (6)
hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/jmx/JmxCollectImpl.java:116
- The disallowedPatterns array includes 'rmi:', which will match the valid 'service:jmx:rmi:' prefix and reject legitimate URLs. Refine this check by excluding the JMX prefix or using a more precise protocol matcher.
String[] disallowedPatterns = { "ldap:", "rmi:", "iiop:", "nis:", "dns:", "corbaname:", "http:", "https:" };
hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/jmx/JmxCollectImpl.java:124
- The check for ':/', which catches '://', is too broad and will flag valid JMX URLs. Limit this pattern detection to only truly unsafe sequences or remove the ':/'.
if (url.contains("${") || url.contains("$[") || url.contains(":#") || url.contains(":/")) {
hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/jmx/JmxCollectImpl.java:142
- The hostname regex requires at least one dot, so single-label hosts like 'localhost' will be rejected. Adjust the pattern to allow single-label hostnames or common local names.
String hostnameRegex = "^([a-zA-Z0-9][-a-zA-Z0-9]*\\.)+[a-zA-Z0-9][-a-zA-Z0-9]*$|^(\\d{1,3}\\.){3}\\d{1,3}$|^([0-9a-fA-F]{0,4}:){2,7}[0-9a-fA-F]{0,4}$";
hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/jmx/JmxCollectImpl.java:100
- [nitpick] Host and port validation logic is duplicated between preCheck and getConnectSession. Extract a shared validation helper to reduce duplication and keep checks consistent.
Assert.isTrue(isValidHostname(host), "Invalid hostname format");
hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/jmx/JmxCollectImpl.java:323
- [nitpick] Logging only the exception message loses stack trace context. Use
log.error("Failed to connect to JMX server", e)
to capture the full exception.
log.error("Failed to connect to JMX server: {}", e.getMessage());
hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/jmx/JmxCollectImpl.java:111
- New validation logic in
validateJmxUrl
andisValidHostname
should have dedicated unit tests covering both allowed and disallowed cases to ensure full test coverage.
private void validateJmxUrl(String url) throws IllegalArgumentException {
...collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/jmx/JmxCollectImpl.java
Outdated
Show resolved
Hide resolved
...collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/jmx/JmxCollectImpl.java
Outdated
Show resolved
Hide resolved
...collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/jmx/JmxCollectImpl.java
Outdated
Show resolved
Hide resolved
…g/apache/hertzbeat/collector/collect/jmx/JmxCollectImpl.java Signed-off-by: tomsun28 <tomsun28@outlook.com>
…g/apache/hertzbeat/collector/collect/jmx/JmxCollectImpl.java Signed-off-by: tomsun28 <tomsun28@outlook.com>
…g/apache/hertzbeat/collector/collect/jmx/JmxCollectImpl.java Signed-off-by: tomsun28 <tomsun28@outlook.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
What's changed?
Enhanced URL Validation
service:jmx:rmi:
protocol${
,$[
, etc.)Hostname and Port Validation
Connection Hardening
Java Compatibility Fixes
instanceof Type var
) with traditional castingChecklist
Add or update API