Skip to content

Commit a26f9db

Browse files
wangweijslowhog
authored andcommittedOct 19, 2021
8263314: Enhance XML Dsig modes
Reviewed-by: rhalade, mschoene, valeriep, mullan
1 parent 895e2bd commit a26f9db

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed
 

‎src/java.xml.crypto/share/classes/org/jcp/xml/dsig/internal/dom/DOMURIDereferencer.java

+22-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
import javax.xml.crypto.*;
3939
import javax.xml.crypto.dom.*;
40+
import java.net.URI;
4041

4142
/**
4243
* DOM-based implementation of URIDereferencer.
@@ -70,9 +71,27 @@ public Data dereference(URIReference uriRef, XMLCryptoContext context)
7071

7172
boolean secVal = Utils.secureValidation(context);
7273

73-
if (secVal && Policy.restrictReferenceUriScheme(uri)) {
74-
throw new URIReferenceException(
75-
"Uri " + uri + " is forbidden when secure validation is enabled");
74+
if (secVal) {
75+
try {
76+
if (Policy.restrictReferenceUriScheme(uri)) {
77+
throw new URIReferenceException(
78+
"URI " + uri + " is forbidden when secure validation is enabled");
79+
}
80+
81+
if (uri != null && !uri.isEmpty() && uri.charAt(0) != '#' && URI.create(uri).getScheme() == null) {
82+
// beseURI will be used to dereference a relative uri
83+
try {
84+
if (Policy.restrictReferenceUriScheme(baseURI)) {
85+
throw new URIReferenceException(
86+
"Base URI " + baseURI + " is forbidden when secure validation is enabled");
87+
}
88+
} catch (IllegalArgumentException e) { // thrown by Policy.restrictReferenceUriScheme
89+
throw new URIReferenceException("Invalid base URI " + baseURI);
90+
}
91+
}
92+
} catch (IllegalArgumentException e) { // thrown by Policy.restrictReferenceUriScheme or URI.create
93+
throw new URIReferenceException("Invalid URI " + uri);
94+
}
7695
}
7796

7897
// Check if same-document URI and already registered on the context

‎test/jdk/javax/xml/crypto/dsig/GenerationTests.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,6 @@ private static void dumpDocument(Document doc, Writer w) throws Exception {
14541454
DOMValidateContext dvc = new DOMValidateContext
14551455
(ks, doc.getDocumentElement());
14561456
File f = new File(DATA_DIR);
1457-
dvc.setBaseURI(f.toURI().toString());
14581457
dvc.setURIDereferencer(httpUd);
14591458

14601459
XMLSignature sig2 = fac.unmarshalXMLSignature(dvc);
@@ -2195,6 +2194,12 @@ public Data dereference(final URIReference ref, XMLCryptoContext ctx)
21952194
(DATA_DIR, uri.substring(uri.lastIndexOf('/'))));
21962195
return new OctetStreamData(fis,ref.getURI(),ref.getType());
21972196
} catch (Exception e) { throw new URIReferenceException(e); }
2197+
} else if (uri.startsWith("certs/")) {
2198+
try {
2199+
FileInputStream fis = new FileInputStream(new File
2200+
(DATA_DIR, uri));
2201+
return new OctetStreamData(fis,ref.getURI(),ref.getType());
2202+
} catch (Exception e) { throw new URIReferenceException(e); }
21982203
}
21992204

22002205
// fallback on builtin deref

0 commit comments

Comments
 (0)
Please sign in to comment.