Skip to content

Commit 5c545da

Browse files
committed
Redefine globalXsltFile as relative to CATALINA_BASE/conf or CATALINA_HOME/conf
git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc7.0.x/trunk@1578637 13f79535-47bb-0310-9956-ffa450edef68
1 parent b697457 commit 5c545da

File tree

3 files changed

+69
-21
lines changed

3 files changed

+69
-21
lines changed

conf/web.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@
8888
<!-- globalXsltFile[null] -->
8989
<!-- -->
9090
<!-- globalXsltFile Site wide configuration version of -->
91-
<!-- localXsltFile This argument is expected -->
92-
<!-- to be a physical file. [null] -->
93-
<!-- -->
94-
<!-- -->
91+
<!-- localXsltFile. This argument must be a -->
92+
<!-- relative path that points to a location below -->
93+
<!-- either $CATALINA_BASE/conf (checked first) -->
94+
<!-- or $CATALINA_BASE/conf (checked second).[null] -->
9595

9696
<servlet>
9797
<servlet-name>default</servlet-name>

java/org/apache/catalina/servlets/DefaultServlet.java

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
18-
1917
package org.apache.catalina.servlets;
2018

2119

@@ -36,6 +34,7 @@
3634
import java.io.StringWriter;
3735
import java.util.ArrayList;
3836
import java.util.Iterator;
37+
import java.util.Locale;
3938
import java.util.StringTokenizer;
4039

4140
import javax.naming.InitialContext;
@@ -1606,20 +1605,24 @@ protected InputStream findXsltInputStream(DirContext directory)
16061605
/* Open and read in file in one fell swoop to reduce chance
16071606
* chance of leaving handle open.
16081607
*/
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);
16161614
byte b[] = new byte[(int)f.length()]; /* danger! */
16171615
fis.read(b);
16181616
return new ByteArrayInputStream(b);
1617+
} finally {
1618+
if (fis != null) {
1619+
try {
1620+
fis.close();
1621+
} catch (IOException ioe) {
1622+
// Ignore
1623+
}
1624+
}
16191625
}
1620-
} finally {
1621-
if (fis!=null)
1622-
fis.close();
16231626
}
16241627
}
16251628

@@ -1628,6 +1631,50 @@ protected InputStream findXsltInputStream(DirContext directory)
16281631
}
16291632

16301633

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+
16311678
// -------------------------------------------------------- protected Methods
16321679

16331680

webapps/docs/default-servlet.xml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,12 @@ The DefaultServlet allows the following initParamters:
110110
<th valign='top'>globalXsltFile</th>
111111
<td valign='top'>
112112
If you wish to customize your directory listing, you
113-
can use an XSL transformation. This value is an absolute
114-
file name which be used for all directory listings.
115-
This can be overridden per context and/or per directory. See
116-
<strong>contextXsltFile</strong> and <strong>localXsltFile</strong>
117-
below. The format of the xml is shown below.
113+
can use an XSL transformation. This value is a relative file name (to
114+
either $CATALINA_BASE/conf/ or $CATALINA_HOME/conf/) which will be used
115+
for all directory listings. This can be overridden per context and/or
116+
per directory. See <strong>contextXsltFile</strong> and
117+
<strong>localXsltFile</strong> below. The format of the xml is shown
118+
below.
118119
</td>
119120
</tr>
120121
<tr>

0 commit comments

Comments
 (0)