Skip to content

Commit 2acca94

Browse files
committed
8278186: org.jcp.xml.dsig.internal.dom.Utils.parseIdFromSameDocumentURI throws StringIndexOutOfBoundsException when calling substring method
Backport-of: 1f1db838ab7d427170d59a8b55fdb45c4d80c359
1 parent 40dce20 commit 2acca94

File tree

4 files changed

+75
-9
lines changed

4 files changed

+75
-9
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* under the License.
2222
*/
2323
/*
24-
* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
24+
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
2525
*/
2626
packageorg.jcp.xml.dsig.internal.dom;
2727

@@ -101,7 +101,9 @@ public Data dereference(URIReference uriRef, XMLCryptoContext context)
101101
if (id.startsWith("xpointer(id(")) {
102102
inti1 = id.indexOf('\'');
103103
inti2 = id.indexOf('\'', i1+1);
104-
id = id.substring(i1+1, i2);
104+
if (i1 >= 0 && i2 >= 0) {
105+
id = id.substring(i1 + 1, i2);
106+
}
105107
}
106108

107109
// check if element is registered by Id

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* under the License.
2222
*/
2323
/*
24-
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
24+
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
2525
*/
2626
packageorg.jcp.xml.dsig.internal.dom;
2727

@@ -94,7 +94,9 @@ public static String parseIdFromSameDocumentURI(String uri) {
9494
if (id.startsWith("xpointer(id(")) {
9595
inti1 = id.indexOf('\'');
9696
inti2 = id.indexOf('\'', i1+1);
97-
id = id.substring(i1+1, i2);
97+
if (i1 >= 0 && i2 >= 0) {
98+
id = id.substring(i1 + 1, i2);
99+
}
98100
}
99101
returnid;
100102
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
importjdk.test.lib.Asserts;
25+
importjdk.test.lib.Utils;
26+
importjdk.test.lib.security.XMLUtils;
27+
28+
importjavax.xml.crypto.URIReferenceException;
29+
importjavax.xml.crypto.dsig.XMLSignatureException;
30+
importjava.security.KeyPair;
31+
importjava.security.KeyPairGenerator;
32+
importjava.security.spec.ECGenParameterSpec;
33+
34+
/**
35+
* @test
36+
* @bug 8278186
37+
* @summary reject malformed xpointer(id('a')) gracefully
38+
* @library /test/lib
39+
* @modules java.xml.crypto
40+
*/
41+
publicclassBadXPointer {
42+
43+
publicstaticvoidmain(String[] args) throwsException {
44+
45+
KeyPairGeneratorkpg = KeyPairGenerator.getInstance("EC");
46+
kpg.initialize(newECGenParameterSpec("secp256r1"));
47+
KeyPairkp = kpg.generateKeyPair();
48+
49+
varsigner = XMLUtils.signer(kp.getPrivate(), kp.getPublic());
50+
vardoc = XMLUtils.string2doc("<root/>");
51+
52+
// No enclosing ' for id
53+
Utils.runAndCheckException(
54+
() -> signer.signEnveloping(doc, "a", "#xpointer(id('a))"),
55+
ex -> Asserts.assertTrue(exinstanceofXMLSignatureException
56+
&& ex.getCause() instanceofURIReferenceException
57+
&& ex.getMessage().contains("Could not find a resolver"),
58+
ex.toString()));
59+
}
60+
}

test/lib/jdk/test/lib/security/XMLUtils.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ public static void main(String[] args) throws Exception {
9292
Asserts.assertTrue(v2.validate(s3.sign(d))); // can read KeyInfo
9393
Asserts.assertTrue(v2.secureValidation(false).validate(s3.sign(p.toUri()))); // can read KeyInfo
9494
Asserts.assertTrue(v2.secureValidation(false).baseURI(b).validate(
95-
s3.sign(p.getParent().toUri(), p.getFileName().toUri()))); // can read KeyInfo
95+
s3.sign(p.toAbsolutePath().getParent().toUri(), p.getFileName().toUri()))); // can read KeyInfo
9696
Asserts.assertTrue(v1.validate(s1.sign("text"))); // plain text
9797
Asserts.assertTrue(v1.validate(s1.sign("binary".getBytes()))); // raw data
98+
Asserts.assertTrue(v1.validate(s1.signEnveloping(d, "x", "#x")));
99+
Asserts.assertTrue(v1.validate(s1.signEnveloping(d, "x", "#xpointer(id('x'))")));
98100
}
99101

100102
//////////// CONVERT ////////////
@@ -347,14 +349,14 @@ public Document sign(Document document) throws Exception {
347349
}
348350

349351
// Signs a document in enveloping mode
350-
publicDocumentsignEnveloping(Documentdocument) throwsException {
352+
publicDocumentsignEnveloping(Documentdocument, Stringid, Stringref) throwsException {
351353
DocumentnewDocument = DocumentBuilderFactory.newInstance()
352354
.newDocumentBuilder().newDocument();
353355
FAC.newXMLSignature(
354-
buildSignedInfo(FAC.newReference("#object", dm)),
356+
buildSignedInfo(FAC.newReference(ref, dm)),
355357
buildKeyInfo(),
356358
List.of(FAC.newXMLObject(List.of(newDOMStructure(document.getDocumentElement())),
357-
"object", null, null)),
359+
id, null, null)),
358360
null,
359361
null)
360362
.sign(newDOMSignContext(privateKey, newDocument));
@@ -474,7 +476,7 @@ public boolean validate(Document document) throws Exception {
474476
// If key is not null, any key from the signature will be ignored
475477
publicbooleanvalidate(Documentdocument, PublicKeykey)
476478
throwsException {
477-
NodeListnodeList = document.getElementsByTagName("Signature");
479+
NodeListnodeList = document.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
478480
if (nodeList.getLength() == 1) {
479481
NodesignatureNode = nodeList.item(0);
480482
if (signatureNode != null) {

0 commit comments

Comments
 (0)
close