|
@@ -58,9 +58,12 @@ import org.apache.hadoop.security.UserGroupInformation;
|
|
|
import org.apache.hadoop.security.Krb5AndCertsSslSocketConnector.MODE;
|
|
|
import org.apache.hadoop.security.authorize.AccessControlList;
|
|
|
import org.apache.hadoop.util.ReflectionUtils;
|
|
|
+import org.mortbay.io.Buffer;
|
|
|
import org.mortbay.jetty.Connector;
|
|
|
import org.mortbay.jetty.Handler;
|
|
|
+import org.mortbay.jetty.MimeTypes;
|
|
|
import org.mortbay.jetty.Server;
|
|
|
+import org.mortbay.jetty.handler.ContextHandler;
|
|
|
import org.mortbay.jetty.handler.ContextHandlerCollection;
|
|
|
import org.mortbay.jetty.nio.SelectChannelConnector;
|
|
|
import org.mortbay.jetty.security.SslSocketConnector;
|
|
@@ -750,6 +753,7 @@ public class HttpServer implements FilterContainer {
|
|
|
* all of the servlets resistant to cross-site scripting attacks.
|
|
|
*/
|
|
|
public static class QuotingInputFilter implements Filter {
|
|
|
+ private FilterConfig config;
|
|
|
|
|
|
public static class RequestQuoter extends HttpServletRequestWrapper {
|
|
|
private final HttpServletRequest rawRequest;
|
|
@@ -837,6 +841,7 @@ public class HttpServer implements FilterContainer {
|
|
|
|
|
|
@Override
|
|
|
public void init(FilterConfig config) throws ServletException {
|
|
|
+ this.config = config;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -850,11 +855,29 @@ public class HttpServer implements FilterContainer {
|
|
|
) throws IOException, ServletException {
|
|
|
HttpServletRequestWrapper quoted =
|
|
|
new RequestQuoter((HttpServletRequest) request);
|
|
|
- final HttpServletResponse httpResponse = (HttpServletResponse) response;
|
|
|
- // set the default to UTF-8 so that we don't need to worry about IE7
|
|
|
- // choosing to interpret the special characters as UTF-7
|
|
|
- httpResponse.setContentType("text/html;charset=utf-8");
|
|
|
- chain.doFilter(quoted, response);
|
|
|
+ HttpServletResponse httpResponse = (HttpServletResponse) response;
|
|
|
+
|
|
|
+ String mime = inferMimeType(request);
|
|
|
+ if (mime == null || mime.equals("text/html")) {
|
|
|
+ // no extension or HTML with unspecified encoding, we want to
|
|
|
+ // force HTML with utf-8 encoding
|
|
|
+ // This is to avoid the following security issue:
|
|
|
+ // http://openmya.hacker.jp/hasegawa/security/utf7cs.html
|
|
|
+ httpResponse.setContentType("text/html; charset=utf-8");
|
|
|
+ }
|
|
|
+ chain.doFilter(quoted, httpResponse);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Infer the mime type for the response based on the extension of the request
|
|
|
+ * URI. Returns null if unknown.
|
|
|
+ */
|
|
|
+ private String inferMimeType(ServletRequest request) {
|
|
|
+ String path = ((HttpServletRequest)request).getRequestURI();
|
|
|
+ ContextHandler.SContext sContext = (ContextHandler.SContext)config.getServletContext();
|
|
|
+ MimeTypes mimes = sContext.getContextHandler().getMimeTypes();
|
|
|
+ Buffer mimeBuffer = mimes.getMimeByExtension(path);
|
|
|
+ return (mimeBuffer == null) ? null : mimeBuffer.toString();
|
|
|
}
|
|
|
|
|
|
}
|