Skip to content

Commit 38b7732

Browse files
Yuri NesterenkoRealCLanger
authored andcommitted
8270504: Better Xpath expression handling
Reviewed-by: andrew Backport-of: b61a2ca626b1da5e555c50e548b643a2daa396c6
1 parent d19834f commit 38b7732

File tree

27 files changed

+1210
-394
lines changed

27 files changed

+1210
-394
lines changed

src/java.xml/share/classes/com/sun/java_cup/internal/runtime/lr_parser.java

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,6 +26,8 @@
2626

2727
package com.sun.java_cup.internal.runtime;
2828

29+
import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;
30+
import java.util.Arrays;
2931
import java.util.Stack;
3032

3133
/** This class implements a skeleton table driven LR parser. In general,
@@ -134,9 +136,19 @@
134136
* @see com.sun.java_cup.internal.runtime.Symbol
135137
* @see com.sun.java_cup.internal.runtime.virtual_parse_stack
136138
* @author Frank Flannery
139+
*
140+
* @LastModified: Jan 2022
137141
*/
138142

139143
public abstract class lr_parser {
144+
public static final int ID_GROUP = 1;
145+
public static final int ID_OPERATOR = 2;
146+
public static final int ID_TOTAL_OPERATOR = 3;
147+
148+
private boolean isLiteral = false;
149+
private int grpCount = 0;
150+
private int opCount = 0;
151+
private int totalOpCount = 0;
140152

141153
/*-----------------------------------------------------------*/
142154
/*--- Constructor(s) ----------------------------------------*/
@@ -355,8 +367,29 @@ public void user_init() throws java.lang.Exception { }
355367
* the "scan with" clause. Do not recycle objects; every call to
356368
* scan() should return a fresh object.
357369
*/
358-
public Symbol scan() throws java.lang.Exception {
359-
return getScanner().next_token();
370+
public Symbol scan() throws Exception {
371+
Symbol s = getScanner().next_token();
372+
373+
if (s.sym == sym.LPAREN) {
374+
if (!isLiteral) {
375+
grpCount++;
376+
}
377+
opCount++; // function
378+
isLiteral = false;
379+
} else if (contains(sym.OPERATORS, s.sym)) {
380+
opCount++;
381+
isLiteral = false;
382+
}
383+
384+
if (s.sym == sym.Literal || s.sym == sym.QNAME) {
385+
isLiteral = true;
386+
}
387+
388+
return s;
389+
}
390+
391+
private boolean contains(final int[] arr, final int key) {
392+
return Arrays.stream(arr).anyMatch(i -> i == key);
360393
}
361394

362395
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
@@ -552,6 +585,9 @@ public Symbol parse() throws java.lang.Exception
552585

553586
/* do user initialization */
554587
user_init();
588+
isLiteral = false;
589+
grpCount = 0;
590+
opCount = 0;
555591

556592
/* get the first token */
557593
cur_token = scan();
@@ -630,9 +666,29 @@ else if (act == 0)
630666
}
631667
}
632668
}
669+
670+
totalOpCount += opCount;
633671
return lhs_sym;
634672
}
635673

674+
/**
675+
* Returns the count of operators in XPath expressions.
676+
*
677+
* @param id the ID of the count
678+
* @return the count associated with the ID
679+
*/
680+
public int getCount(int id) {
681+
switch (id) {
682+
case ID_GROUP:
683+
return grpCount;
684+
case ID_OPERATOR:
685+
return opCount;
686+
case ID_TOTAL_OPERATOR:
687+
return totalOpCount;
688+
}
689+
return 0;
690+
}
691+
636692
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
637693

638694
/** Write a debugging message to System.err for the debugging version

src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/Parser.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
33
*/
44
/*
55
* Licensed to the Apache Software Foundation (ASF) under one or more
@@ -22,7 +22,6 @@
2222

2323
import com.sun.java_cup.internal.runtime.Symbol;
2424
import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
25-
import com.sun.org.apache.xalan.internal.utils.XMLSecurityManager;
2625
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
2726
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodType;
2827
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
@@ -46,6 +45,7 @@
4645
import jdk.xml.internal.JdkXmlFeatures;
4746
import jdk.xml.internal.JdkXmlUtils;
4847
import jdk.xml.internal.SecuritySupport;
48+
import jdk.xml.internal.XMLSecurityManager;
4949
import org.xml.sax.Attributes;
5050
import org.xml.sax.ContentHandler;
5151
import org.xml.sax.InputSource;
@@ -62,7 +62,7 @@
6262
* @author G. Todd Miller
6363
* @author Morten Jorgensen
6464
* @author Erwin Bolwidt <[email protected]>
65-
* @LastModified: May 2021
65+
* @LastModified: Jan 2022
6666
*/
6767
public class Parser implements Constants, ContentHandler {
6868

@@ -504,8 +504,10 @@ public SyntaxTreeNode parse(InputSource input) {
504504
XMLSecurityManager securityManager =
505505
(XMLSecurityManager)_xsltc.getProperty(JdkConstants.SECURITY_MANAGER);
506506
for (XMLSecurityManager.Limit limit : XMLSecurityManager.Limit.values()) {
507-
lastProperty = limit.apiProperty();
508-
reader.setProperty(lastProperty, securityManager.getLimitValueAsString(limit));
507+
if (limit.isSupported(XMLSecurityManager.Processor.PARSER)) {
508+
lastProperty = limit.apiProperty();
509+
reader.setProperty(lastProperty, securityManager.getLimitValueAsString(limit));
510+
}
509511
}
510512
if (securityManager.printEntityCountInfo()) {
511513
lastProperty = JdkConstants.JDK_DEBUG_LIMIT;
@@ -1169,6 +1171,9 @@ private SyntaxTreeNode parseTopLevel(SyntaxTreeNode parent, String text,
11691171
expression, parent));
11701172
}
11711173
catch (Exception e) {
1174+
if (ErrorMsg.XPATH_LIMIT.equals(e.getMessage())) {
1175+
throw new RuntimeException(ErrorMsg.XPATH_LIMIT);
1176+
}
11721177
if (_xsltc.debug()) e.printStackTrace();
11731178
reportError(ERROR, new ErrorMsg(ErrorMsg.XPATH_PARSER_ERR,
11741179
expression, parent));

src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/XPathParser.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -34,14 +34,21 @@
3434
import java.util.ArrayList;
3535
import java.util.List;
3636
import java.util.Stack;
37+
import jdk.xml.internal.JdkConstants;
38+
import jdk.xml.internal.XMLLimitAnalyzer;
39+
import jdk.xml.internal.XMLSecurityManager;
40+
import jdk.xml.internal.XMLSecurityManager.Limit;
3741

3842
/**
3943
* CUP v0.11b generated parser.
4044
* This class was generated by CUP v0.11b on Nov 12, 2019.
4145
*
42-
* @LastModified: Nov 2019
46+
* @LastModified: Jan 2022
4347
*/
4448
public class XPathParser extends lr_parser {
49+
private int grpLimit = 0;
50+
private int opLimit = 0;
51+
private int totalOpLimit = 0;
4552

4653
/**
4754
* Default constructor.
@@ -953,10 +960,19 @@ public int error_sym() {
953960
*/
954961
public SymbolTable _symbolTable;
955962

963+
private XMLSecurityManager _xmlSM;
964+
private XMLLimitAnalyzer _limitAnalyzer = null;
965+
956966
public XPathParser(Parser parser) {
957967
_parser = parser;
958968
_xsltc = parser.getXSLTC();
959969
_symbolTable = parser.getSymbolTable();
970+
_xmlSM = (XMLSecurityManager)_xsltc.getProperty(JdkConstants.SECURITY_MANAGER);
971+
_limitAnalyzer = new XMLLimitAnalyzer();
972+
// no limits if _xmlSM is null
973+
grpLimit = (_xmlSM != null) ? _xmlSM.getLimit(Limit.XPATH_GROUP_LIMIT) : 0;
974+
opLimit = (_xmlSM != null) ? _xmlSM.getLimit(Limit.XPATH_OP_LIMIT) : 0;
975+
totalOpLimit = (_xmlSM != null) ? _xmlSM.getLimit(Limit.XPATH_TOTALOP_LIMIT) : 0;
960976
}
961977

962978
public int getLineNumber() {
@@ -1101,7 +1117,32 @@ public Symbol parse(String expression, int lineNumber) throws Exception {
11011117
try {
11021118
_expression = expression;
11031119
_lineNumber = lineNumber;
1104-
return super.parse();
1120+
Symbol s = super.parse();
1121+
int grpCount = getCount(ID_GROUP);
1122+
int opCount = getCount(ID_OPERATOR);
1123+
int totalOpCount = getCount(ID_TOTAL_OPERATOR);
1124+
1125+
String errCode = null;
1126+
Object[] params = null;
1127+
if (grpLimit > 0 && grpCount > grpLimit) {
1128+
errCode = ErrorMsg.XPATH_GROUP_LIMIT;
1129+
params = new Object[]{grpCount, grpLimit,
1130+
_xmlSM.getStateLiteral(Limit.XPATH_GROUP_LIMIT)};
1131+
} else if (opLimit > 0 && opCount > opLimit) {
1132+
errCode = ErrorMsg.XPATH_OPERATOR_LIMIT;
1133+
params = new Object[]{opCount, opLimit,
1134+
_xmlSM.getStateLiteral(Limit.XPATH_OP_LIMIT)};
1135+
} else if (totalOpLimit > 0 && totalOpCount > totalOpLimit) {
1136+
errCode = ErrorMsg.XPATH_TOTAL_OPERATOR_LIMIT;
1137+
params = new Object[]{totalOpCount, totalOpLimit,
1138+
_xmlSM.getStateLiteral(Limit.XPATH_TOTALOP_LIMIT)};
1139+
}
1140+
if (errCode != null) {
1141+
_parser.reportError(Constants.FATAL,
1142+
new ErrorMsg(errCode, lineNumber, params));
1143+
throw new RuntimeException(ErrorMsg.XPATH_LIMIT);
1144+
}
1145+
return s;
11051146
} catch (IllegalCharException e) {
11061147
ErrorMsg err = new ErrorMsg(ErrorMsg.ILLEGAL_CHAR_ERR,
11071148
lineNumber, e.getMessage());

src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/XSLTC.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
33
*/
44
/*
55
* Licensed to the Apache Software Foundation (ASF) under one or more
@@ -21,7 +21,6 @@
2121
package com.sun.org.apache.xalan.internal.xsltc.compiler;
2222

2323
import com.sun.org.apache.bcel.internal.classfile.JavaClass;
24-
import com.sun.org.apache.xalan.internal.utils.XMLSecurityManager;
2524
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
2625
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
2726
import com.sun.org.apache.xml.internal.dtm.DTM;
@@ -47,8 +46,8 @@
4746
import javax.xml.catalog.CatalogFeatures;
4847
import jdk.xml.internal.JdkConstants;
4948
import jdk.xml.internal.JdkXmlFeatures;
50-
import jdk.xml.internal.JdkXmlUtils;
5149
import jdk.xml.internal.SecuritySupport;
50+
import jdk.xml.internal.XMLSecurityManager;
5251
import org.xml.sax.InputSource;
5352
import org.xml.sax.XMLReader;
5453

@@ -58,7 +57,7 @@
5857
* @author G. Todd Miller
5958
* @author Morten Jorgensen
6059
* @author John Howard ([email protected])
61-
* @LastModified: May 2021
60+
* @LastModified: Jan 2022
6261
*/
6362
public final class XSLTC {
6463

@@ -505,7 +504,10 @@ else if (systemId != null && !systemId.equals("")) {
505504
}
506505
}
507506
catch (Exception e) {
508-
/*if (_debug)*/ e.printStackTrace();
507+
if (_debug) e.printStackTrace();
508+
if (ErrorMsg.XPATH_LIMIT.equals(e.getMessage())) {
509+
return !_parser.errorsFound();
510+
}
509511
_parser.reportError(Constants.FATAL, new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR, e));
510512
}
511513
catch (Error e) {

src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/sym.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2004, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,9 +25,13 @@
2525

2626
package com.sun.org.apache.xalan.internal.xsltc.compiler;
2727

28+
import java.util.Arrays;
29+
2830
/**
2931
* CUP generated class containing symbol constants.
3032
* This class was generated by CUP v0.10j on Fri Feb 27 13:01:50 PST 2004.
33+
*
34+
* @LastModified: Jan 2022
3135
*/
3236
public class sym {
3337
/* terminals */
@@ -85,4 +89,12 @@ public class sym {
8589
public static final int ATTRIBUTE = 41;
8690
public static final int GT = 19;
8791
public static final int NODE = 31;
92+
/*
93+
AXES: count once at DCOLON,
94+
these axes names are therefore not counted:
95+
NAMESPACE, FOLLOWINGSIBLING, CHILD, DESCENDANTORSELF, DESCENDANT
96+
, PRECEDINGSIBLING, SELF, ANCESTORORSELF, PRECEDING, ANCESTOROR, PARENT, FOLLOWING, ATTRIBUTE
97+
*/
98+
public static final int[] OPERATORS = {GE, SLASH, ATSIGN, LPAREN, DCOLON,
99+
MINUS, STAR, LT, OR, DIV, PLUS, LE, VBAR, MOD, EQ, LBRACK, DOLLAR, NE, GT};
88100
}

src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/util/ErrorMessages.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
33
*/
44
/*
55
* Licensed to the Apache Software Foundation (ASF) under one or more
@@ -24,6 +24,7 @@
2424

2525
/**
2626
* @author Morten Jorgensen
27+
* @LastModified: Jan 2022
2728
*/
2829
public class ErrorMessages extends ListResourceBundle {
2930

@@ -1027,12 +1028,22 @@ public Object[][] getContents()
10271028
"smaller templates."
10281029
},
10291030

1030-
{ErrorMsg.DESERIALIZE_TRANSLET_ERR, "When Java security is enabled, " +
1031-
"support for deserializing TemplatesImpl is disabled." +
1032-
"This can be overridden by setting the jdk.xml.enableTemplatesImplDeserialization" +
1033-
" system property to true."}
1034-
1035-
};
1031+
{ErrorMsg.DESERIALIZE_TRANSLET_ERR, "When Java security is enabled, "
1032+
+ "support for deserializing TemplatesImpl is disabled. This can be "
1033+
+ "overridden by setting the jdk.xml.enableTemplatesImplDeserialization"
1034+
+ " system property to true."},
1035+
1036+
{ErrorMsg.XPATH_GROUP_LIMIT,
1037+
"JAXP0801001: the compiler encountered an XPath expression containing "
1038+
+ "''{0}'' groups that exceeds the ''{1}'' limit set by ''{2}''."},
1039+
1040+
{ErrorMsg.XPATH_OPERATOR_LIMIT,
1041+
"JAXP0801002: the compiler encountered an XPath expression containing "
1042+
+ "''{0}'' operators that exceeds the ''{1}'' limit set by ''{2}''."},
1043+
{ErrorMsg.XPATH_TOTAL_OPERATOR_LIMIT,
1044+
"JAXP0801003: the compiler encountered XPath expressions with an accumulated "
1045+
+ "''{0}'' operators that exceeds the ''{1}'' limit set by ''{2}''."},
1046+
};
10361047

10371048
}
10381049
}

0 commit comments

Comments
 (0)