Skip to content

Commit cb54b52

Browse files
committed
[Refactor] tighten resource constraints for XML/XPath collector
1 parent d9403ec commit cb54b52

2 files changed

Lines changed: 18 additions & 18 deletions

File tree

  • hertzbeat-collector

hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImpl.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
public class HttpCollectImpl extends AbstractCollect {
110110

111111
/**
112-
* Pre-compiled regex patterns for dangerous XPath detection.
112+
* Pre-compiled regex patterns for dangerous Xpath detection.
113113
* Compiled once at class load for performance.
114114
*/
115115
private static final List<Pattern> DANGEROUS_XPATH_PATTERNS;
@@ -375,14 +375,14 @@ private void parseResponseBySiteMap(String resp, List<String> aliasFields,
375375
}
376376

377377
/**
378-
* Validates the XPath expression to prevent DoS attacks.
378+
* Validates the Xpath expression to prevent DoS attacks.
379379
* Checks for dangerous patterns that could traverse the entire XML document.
380380
* Uses pre-compiled patterns for performance and case-insensitive matching for security.
381381
*
382-
* @param xpathExpression the XPath expression to validate
382+
* @param xpathExpression the Xpath expression to validate
383383
* @throws IllegalArgumentException if the expression contains dangerous patterns
384384
*/
385-
private void validateXPathExpression(String xpathExpression) throws IllegalArgumentException {
385+
private void validateXpathExpression(String xpathExpression) throws IllegalArgumentException {
386386
if (!StringUtils.hasText(xpathExpression)) {
387387
return;
388388
}
@@ -392,7 +392,7 @@ private void validateXPathExpression(String xpathExpression) throws IllegalArgum
392392
Matcher matcher = pattern.matcher(xpathExpression);
393393
if (matcher.find()) {
394394
throw new IllegalArgumentException(
395-
"XPath expression contains dangerous pattern that may cause DoS: " + pattern.pattern()
395+
"Xpath expression contains dangerous pattern that may cause DoS: " + pattern.pattern()
396396
);
397397
}
398398
}
@@ -403,11 +403,11 @@ private void validateXPathExpression(String xpathExpression) throws IllegalArgum
403403

404404
if (descendantAxisCount > 10 || wildcardCount > 5) {
405405
throw new IllegalArgumentException(
406-
"XPath expression contains too many wildcards or descendant axes, potential DoS risk"
406+
"Xpath expression contains too many wildcards or descendant axes, potential DoS risk"
407407
);
408408
}
409409

410-
log.debug("XPath expression validation passed: {}", xpathExpression);
410+
log.debug("Xpath expression validation passed: {}", xpathExpression);
411411
}
412412

413413
private void parseResponseByXmlPath(String resp, Metrics metrics,
@@ -416,11 +416,11 @@ private void parseResponseByXmlPath(String resp, Metrics metrics,
416416
List<String> aliasFields = metrics.getAliasFields();
417417
String xpathExpression = http.getParseScript();
418418

419-
// Layer 1: Validate XPath expression is not empty
419+
// Layer 1: Validate Xpath expression is not empty
420420
if (!StringUtils.hasText(xpathExpression)) {
421421
log.warn("Http collect parse type is xmlPath, but the xpath expression is empty.");
422422
builder.setCode(CollectRep.Code.FAIL);
423-
builder.setMsg("XPath expression is empty");
423+
builder.setMsg("Xpath expression is empty");
424424
return;
425425
}
426426

@@ -434,11 +434,11 @@ private void parseResponseByXmlPath(String resp, Metrics metrics,
434434
return;
435435
}
436436

437-
// Layer 3: Validate XPath expression for dangerous patterns
437+
// Layer 3: Validate Xpath expression for dangerous patterns
438438
try {
439-
validateXPathExpression(xpathExpression);
439+
validateXpathExpression(xpathExpression);
440440
} catch (IllegalArgumentException e) {
441-
log.warn("XPath expression validation failed: {}", e.getMessage());
441+
log.warn("Xpath expression validation failed: {}", e.getMessage());
442442
builder.setCode(CollectRep.Code.FAIL);
443443
builder.setMsg(e.getMessage());
444444
return;
@@ -466,7 +466,7 @@ private void parseResponseByXmlPath(String resp, Metrics metrics,
466466
NodeList nodeList = (NodeList) xpath.evaluate(xpathExpression, document, XPathConstants.NODESET);
467467

468468
if (nodeList == null || nodeList.getLength() == 0) {
469-
log.debug("XPath expression '{}' returned no nodes.", xpathExpression);
469+
log.debug("Xpath expression '{}' returned no nodes.", xpathExpression);
470470
boolean requestedSummaryFields = aliasFields.stream()
471471
.anyMatch(alias -> NetworkConstants.RESPONSE_TIME.equalsIgnoreCase(alias)
472472
|| CollectorConstants.KEYWORD.equalsIgnoreCase(alias));
@@ -491,7 +491,7 @@ private void parseResponseByXmlPath(String resp, Metrics metrics,
491491
int resultSize = nodeList.getLength();
492492
int maxResults = CollectorConstants.MAX_XPATH_RESULT_NODES;
493493
if (resultSize > maxResults) {
494-
log.warn("XPath expression returned {} nodes, exceeding limit of {}. Processing first {} nodes only.",
494+
log.warn("Xpath expression returned {} nodes, exceeding limit of {}. Processing first {} nodes only.",
495495
resultSize, maxResults, maxResults);
496496
resultSize = maxResults;
497497
}
@@ -510,7 +510,7 @@ private void parseResponseByXmlPath(String resp, Metrics metrics,
510510
String value = (String) xpath.evaluate(alias, node, XPathConstants.STRING);
511511
valueRowBuilder.addColumn(StringUtils.hasText(value) ? value : CommonConstants.NULL_VALUE);
512512
} catch (XPathExpressionException e) {
513-
log.warn("Failed to evaluate XPath '{}' for node [{}]: {}", alias, node.getNodeName(), e.getMessage());
513+
log.warn("Failed to evaluate Xpath '{}' for node [{}]: {}", alias, node.getNodeName(), e.getMessage());
514514
valueRowBuilder.addColumn(CommonConstants.NULL_VALUE);
515515
}
516516
}
@@ -519,7 +519,7 @@ private void parseResponseByXmlPath(String resp, Metrics metrics,
519519
}
520520

521521
} catch (Exception e) {
522-
log.warn("Failed to parse XML response with XPath '{}': {}", xpathExpression, e.getMessage(), e);
522+
log.warn("Failed to parse XML response with Xpath '{}': {}", xpathExpression, e.getMessage(), e);
523523
builder.setCode(CollectRep.Code.FAIL);
524524
builder.setMsg("Failed to parse XML response: " + e.getMessage());
525525
}

hertzbeat-collector/hertzbeat-collector-common/src/main/java/org/apache/hertzbeat/collector/constants/CollectorConstants.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ public interface CollectorConstants extends NetworkConstants {
5555
int MAX_XML_RESPONSE_SIZE = 10 * 1024 * 1024;
5656

5757
/**
58-
* Maximum number of nodes returned by XPath query to prevent excessive resource consumption
58+
* Maximum number of nodes returned by Xpath query to prevent excessive resource consumption
5959
*/
6060
int MAX_XPATH_RESULT_NODES = 1000;
6161

6262
/**
63-
* Dangerous XPath expression patterns that could cause DoS attacks
63+
* Dangerous Xpath expression patterns that could cause DoS attacks
6464
* These patterns match expressions that traverse the entire XML document
6565
*/
6666
String[] DANGEROUS_XPATH_PATTERNS = {

0 commit comments

Comments
 (0)