- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: java-security/tainted-xpath
Language: Java
Severity: Warning
Category: Best Practices
CWE: 643
This rule identifies potential security vulnerabilities in your code where an XPath expression may be influenced by data coming from an HTTP request. This could lead to what is known as an XPath Injection attack, where an attacker could manipulate the XPath query to access unauthorized data, or potentially cause other harmful effects.
This rule is crucial because XPath Injection is a severe security risk, similar to SQL Injection. If an attacker can control part of an XPath query, they can alter the query’s logic, leading to unauthorized access or exposure to sensitive data.
Never construct XPath queries using string concatenation with unvalidated input. Instead, always use parameterized queries or sanitize the input before using it in an XPath query. If possible, limit the XPath functionality that your application uses to reduce the attack surface. You can also use APIs that automatically protect against XPath Injection, or use a web application firewall to detect and block attack attempts.
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPath;
public class NonCompliant {
public void doPost(HttpServletRequest request, HttpServletResponse response) {
String param = "<default>";
java.util.Enumeration<String> headers = request.getHeaders("X-Some-Header");
if (headers != null && headers.hasMoreElements()) {
param = headers.nextElement();
}
param = java.net.URLDecoder.decode(param, "UTF-8");
XPathFactory xpf = XPathFactory.newInstance();
XPath xp = xpf.newXPath();
String expression = "/Employees/Employee[@emplid='" + param + "']";
xp.compile(expression).evaluate(xmlDocument);
}
}
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class NonCompliant2 {
@PostMapping("/")
public void handlePost(@RequestHeader("X-Some-Header") String headerValue) {
XPathFactory xpf = XPathFactory.newInstance();
XPath xp = xpf.newXPath();
String expression = "/Employees/Employee[@emplid='" + headerValue + "']";
xp.compile(expression).evaluate(xmlDocument);
}
}