14
14
* See the License for the specific language governing permissions and
15
15
* limitations under the License.
16
16
*/
17
-
18
-
19
17
package org .apache .catalina .servlets ;
20
18
21
19
36
34
import java .io .StringWriter ;
37
35
import java .util .ArrayList ;
38
36
import java .util .Iterator ;
37
+ import java .util .Locale ;
39
38
import java .util .StringTokenizer ;
40
39
41
40
import javax .naming .InitialContext ;
@@ -1606,20 +1605,24 @@ protected InputStream findXsltInputStream(DirContext directory)
1606
1605
/* Open and read in file in one fell swoop to reduce chance
1607
1606
* chance of leaving handle open.
1608
1607
*/
1609
- if (globalXsltFile !=null ) {
1610
- FileInputStream fis = null ;
1611
-
1612
- try {
1613
- File f = new File (globalXsltFile );
1614
- if (f .exists ()){
1615
- fis =new FileInputStream (f );
1608
+ if (globalXsltFile != null ) {
1609
+ File f = validateGlobalXsltFile ();
1610
+ if (f != null && f .exists ()){
1611
+ FileInputStream fis = null ;
1612
+ try {
1613
+ fis = new FileInputStream (f );
1616
1614
byte b [] = new byte [(int )f .length ()]; /* danger! */
1617
1615
fis .read (b );
1618
1616
return new ByteArrayInputStream (b );
1617
+ } finally {
1618
+ if (fis != null ) {
1619
+ try {
1620
+ fis .close ();
1621
+ } catch (IOException ioe ) {
1622
+ // Ignore
1623
+ }
1624
+ }
1619
1625
}
1620
- } finally {
1621
- if (fis !=null )
1622
- fis .close ();
1623
1626
}
1624
1627
}
1625
1628
@@ -1628,6 +1631,50 @@ protected InputStream findXsltInputStream(DirContext directory)
1628
1631
}
1629
1632
1630
1633
1634
+ private File validateGlobalXsltFile () {
1635
+
1636
+ File result = null ;
1637
+ String base = System .getProperty (Globals .CATALINA_BASE_PROP );
1638
+
1639
+ if (base != null ) {
1640
+ File baseConf = new File (base , "conf" );
1641
+ result = validateGlobalXsltFile (baseConf );
1642
+ }
1643
+
1644
+ if (result == null ) {
1645
+ String home = System .getProperty (Globals .CATALINA_HOME_PROP );
1646
+ if (home != null ) {
1647
+ File homeConf = new File (home , "conf" );
1648
+ result = validateGlobalXsltFile (homeConf );
1649
+ }
1650
+ }
1651
+
1652
+ return result ;
1653
+ }
1654
+
1655
+
1656
+ private File validateGlobalXsltFile (File base ) {
1657
+ File candidate = new File (base , globalXsltFile );
1658
+
1659
+ // First check that the resulting path is under the provided base
1660
+ try {
1661
+ if (!candidate .getCanonicalPath ().startsWith (base .getCanonicalPath ())) {
1662
+ return null ;
1663
+ }
1664
+ } catch (IOException ioe ) {
1665
+ return null ;
1666
+ }
1667
+
1668
+ // Next check that an .xlt or .xslt file has been specified
1669
+ String nameLower = candidate .getName ().toLowerCase (Locale .ENGLISH );
1670
+ if (!nameLower .endsWith (".xslt" ) && !nameLower .endsWith (".xlt" )) {
1671
+ return null ;
1672
+ }
1673
+
1674
+ return candidate ;
1675
+ }
1676
+
1677
+
1631
1678
// -------------------------------------------------------- protected Methods
1632
1679
1633
1680
0 commit comments