XML signatures are a method used to ensure the integrity and authenticity of XML documents. However, if XML signatures are not validated securely, it can lead to potential vulnerabilities.

## Why is this an issue?

Before Java 17, XML Digital Signature API does not apply restrictions on XML signature validation unless the application runs with a security manager, which is rare.

## What is the potential impact

By not enforcing secure validation, the XML Digital Signature API is more susceptible to attacks such as signature spoofing and injections.

### Increased Vulnerability to Signature Spoofing

By disabling secure validation, the application becomes more susceptible to signature spoofing attacks. Attackers can potentially manipulate the XML signature in a way that bypasses the validation process, allowing them to forge or tamper with the signature. This can lead to the acceptance of invalid or maliciously modified signatures, compromising the integrity and authenticity of the XML documents.

### Risk of Injection Attacks

Disabling secure validation can expose the application to injection attacks. Attackers can inject malicious code or entities into the XML document, taking advantage of the weakened validation process. In some cases, it can also expose the application to denial-of-service attacks. Attackers can exploit vulnerabilities in the validation process to cause excessive resource consumption or system crashes, leading to service unavailability or disruption.

## How to fix it in Java SE

### Code examples

For versions of Java before 17, secure validation is disabled by default unless the application runs with a security manager, which is rare. It should be enabled explicitly by setting the `org.jcp.xml.dsig.secureValidation` attribute to true with the `javax.xml.crypto.dsig.dom.DOMValidateContext.setProperty` method.

For Java 17 and higher, secure validation is enabled by default.

#### Noncompliant code example

    NodeList signatureElement = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");

    XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
    DOMValidateContext valContext = new DOMValidateContext(new KeyValueKeySelector(), signatureElement.item(0)); // Noncompliant
    XMLSignature signature = fac.unmarshalXMLSignature(valContext);

    boolean signatureValidity = signature.validate(valContext);

#### Compliant solution

    NodeList signatureElement = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");

    XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
    DOMValidateContext valContext = new DOMValidateContext(new KeyValueKeySelector(), signatureElement.item(0));
    valContext.setProperty("org.jcp.xml.dsig.secureValidation", Boolean.TRUE);
    XMLSignature signature = fac.unmarshalXMLSignature(valContext);

    boolean signatureValidity = signature.validate(valContext);

### How does this work?

When XML Signature secure validation mode is enabled, XML Signatures are processed more securely. It enforces a number of restrictionsto to protect from XML Documents that may contain hostile constructs that can cause denial-of-service or other types of security issues.

These restrictions can protect you from XML Signatures that may contain potentially hostile constructs that can cause denial-of-service or other types of security issues.

## Resources

### Documentation

- Oracle Java Documentation - XML Digital Signature API Overview and Tutorial

### Standards

- OWASP - Top 10:2021 A02:2021 - Cryptographic Failures
- OWASP - Top 10 2017 Category A3 - Sensitive Data Exposure
- CWE - CWE-347 - Improper Verification of Cryptographic Signature
- STIG Viewer - Application Security and Development: V-222608 - The application must not be vulnerable to XML-oriented attacks.