Skip to content

The Unified and Autonomous Development Platform

Upgrade Jetty to remove security issues

Robin Shenf7a40e7955a5
diff --git a/core/src/main/java/io/onedev/server/web/websocket/WebSocketFilter.java b/core/src/main/java/io/onedev/server/web/websocket/WebSocketFilter.java
index 528044ed1c..7e3126cde6 100644
--- a/core/src/main/java/io/onedev/server/web/websocket/WebSocketFilter.java
+++ b/core/src/main/java/io/onedev/server/web/websocket/WebSocketFilter.java
@@ -54,9 +54,9 @@ public class WebSocketFilter extends AbstractUpgradeFilter {
@Override
public void init(final boolean isServlet, final FilterConfig filterConfig) throws ServletException {
super.init(isServlet, filterConfig);
-
+
try {
- webSocketFactory = new WebSocketServerFactory(webSocketPolicy);
+ webSocketFactory = new WebSocketServerFactory(getApplication().getServletContext(), webSocketPolicy);
webSocketFactory.setCreator(new WebSocketCreator() {
@Override
diff --git a/core/src/main/java/org/eclipse/jetty/server/Response.java b/core/src/main/java/org/eclipse/jetty/server/Response.java
index 3e47894d7c..580276427d 100644
--- a/core/src/main/java/org/eclipse/jetty/server/Response.java
+++ b/core/src/main/java/org/eclipse/jetty/server/Response.java
@@ -1,6 +1,6 @@
//
// ========================================================================
-// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
+// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
@@ -21,13 +21,13 @@ package org.eclipse.jetty.server;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.channels.IllegalSelectorException;
-import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
-import java.util.Enumeration;
-import java.util.Iterator;
+import java.util.EnumSet;
+import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Collectors;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletOutputStream;
@@ -108,12 +108,15 @@ public class Response implements HttpServletResponse
private Locale _locale;
private MimeTypes.Type _mimeType;
private String _characterEncoding;
- private boolean _explicitEncoding;
+ private EncodingFrom _encodingFrom=EncodingFrom.NOT_SET;
private String _contentType;
private OutputType _outputType = OutputType.NONE;
private ResponseWriter _writer;
private long _contentLength = -1;
+ private enum EncodingFrom { NOT_SET, INFERRED, SET_LOCALE, SET_CONTENT_TYPE, SET_CHARACTER_ENCODING };
+ private static final EnumSet<EncodingFrom> __localeOverride = EnumSet.of(EncodingFrom.NOT_SET,EncodingFrom.INFERRED);
+
public Response(HttpChannel channel, HttpOutput out)
{
@@ -138,7 +141,7 @@ public class Response implements HttpServletResponse
_contentLength = -1;
_out.recycle();
_fields.clear();
- _explicitEncoding=false;
+ _encodingFrom=EncodingFrom.NOT_SET;
}
public HttpOutput getHttpOutput()
@@ -246,10 +249,7 @@ public class Response implements HttpServletResponse
quoteOnlyOrAppend(buf,name,quote_name);
buf.append('=');
-
- // Remember name= part to look for other matching set-cookie
- String name_equals=buf.toString();
-
+
// Append the value
boolean quote_value=isQuoteNeededForCookie(value);
quoteOnlyOrAppend(buf,value,quote_value);
@@ -315,29 +315,9 @@ public class Response implements HttpServletResponse
buf.append(";Comment=");
quoteOnlyOrAppend(buf,comment,isQuoteNeededForCookie(comment));
}
-
- // remove any existing set-cookie fields of same name
- Iterator<HttpField> i=_fields.iterator();
- while (i.hasNext())
- {
- HttpField field=i.next();
- if (field.getHeader()==HttpHeader.SET_COOKIE)
- {
- String val = field.getValue();
- if (val!=null && val.startsWith(name_equals))
- {
- //existing cookie has same name, does it also match domain and path?
- if (((!has_domain && !val.contains("Domain")) || (has_domain && val.contains(domain))) &&
- ((!has_path && !val.contains("Path")) || (has_path && val.contains(path))))
- {
- i.remove();
- }
- }
- }
- }
// add the set cookie
- _fields.add(HttpHeader.SET_COOKIE.toString(), buf.toString());
+ _fields.add(HttpHeader.SET_COOKIE, buf.toString());
// Expire responses with set-cookie headers so they do not get cached.
_fields.put(__EXPIRES_01JAN1970);
@@ -537,8 +517,9 @@ public class Response implements HttpServletResponse
if (isCommitted())
LOG.warn("cannot sendError("+code+", "+message+") response already committed");
-
- resetBuffer();
+ else
+ resetBuffer();
+
_characterEncoding=null;
setHeader(HttpHeader.EXPIRES,null);
setHeader(HttpHeader.LAST_MODIFIED,null);
@@ -662,7 +643,7 @@ public class Response implements HttpServletResponse
*/
/*
- if (!URIUtil.hasScheme(location))
+ if (!URIUtil.hasScheme(location))
{
StringBuilder buf = _channel.getRequest().getRootURL();
if (location.startsWith("/"))
@@ -675,7 +656,7 @@ public class Response implements HttpServletResponse
// relative to request
String path=_channel.getRequest().getRequestURI();
String parent=(path.endsWith("/"))?path:URIUtil.parentPath(path);
- location=URIUtil.canonicalPath(URIUtil.addPaths(parent,location));
+ location=URIUtil.canonicalPath(URIUtil.addEncodedPaths(parent,location));
if (!location.startsWith("/"))
buf.append('/');
}
@@ -686,8 +667,8 @@ public class Response implements HttpServletResponse
location=buf.toString();
}
- */
-
+ */
+
resetBuffer();
setHeader(HttpHeader.LOCATION, location);
setStatus(code);
@@ -865,7 +846,15 @@ public class Response implements HttpServletResponse
public String getCharacterEncoding()
{
if (_characterEncoding == null)
- _characterEncoding = StringUtil.__ISO_8859_1;
+ {
+ String encoding = MimeTypes.getCharsetAssumedFromContentType(_contentType);
+ if (encoding!=null)
+ return encoding;
+ encoding = MimeTypes.getCharsetInferredFromContentType(_contentType);
+ if (encoding!=null)
+ return encoding;
+ return StringUtil.__ISO_8859_1;
+ }
return _characterEncoding;
}
@@ -905,10 +894,14 @@ public class Response implements HttpServletResponse
encoding=_mimeType.getCharsetString();
else
{
- encoding = MimeTypes.inferCharsetFromContentType(_contentType);
+ encoding = MimeTypes.getCharsetAssumedFromContentType(_contentType);
if (encoding == null)
- encoding = StringUtil.__ISO_8859_1;
- setCharacterEncoding(encoding,false);
+ {
+ encoding = MimeTypes.getCharsetInferredFromContentType(_contentType);
+ if (encoding == null)
+ encoding = StringUtil.__ISO_8859_1;
+ setCharacterEncoding(encoding,EncodingFrom.INFERRED);
+ }
}
}
@@ -1024,19 +1017,19 @@ public class Response implements HttpServletResponse
@Override
public void setCharacterEncoding(String encoding)
{
- setCharacterEncoding(encoding,true);
+ setCharacterEncoding(encoding,EncodingFrom.SET_CHARACTER_ENCODING);
}
- private void setCharacterEncoding(String encoding, boolean explicit)
+ private void setCharacterEncoding(String encoding, EncodingFrom from)
{
if (isIncluding() || isWriting())
return;
- if (_outputType == OutputType.NONE && !isCommitted())
+ if (_outputType != OutputType.WRITER && !isCommitted())
{
if (encoding == null)
{
- _explicitEncoding=false;
+ _encodingFrom=EncodingFrom.NOT_SET;
// Clear any encoding.
if (_characterEncoding != null)
@@ -1059,7 +1052,7 @@ public class Response implements HttpServletResponse
else
{
// No, so just add this one to the mimetype
- _explicitEncoding = explicit;
+ _encodingFrom = from;
_characterEncoding = HttpGenerator.__STRICT?encoding:StringUtil.normalizeCharset(encoding);
if (_mimeType!=null)
{
@@ -1109,10 +1102,29 @@ public class Response implements HttpServletResponse
if (charset == null)
{
- if (_characterEncoding != null)
+ switch (_encodingFrom)
{
- _contentType = contentType + ";charset=" + _characterEncoding;
- _mimeType = null;
+ case NOT_SET:
+ break;
+ case INFERRED:
+ case SET_CONTENT_TYPE:
+ if (isWriting())
+ {
+ _mimeType=null;
+ _contentType = _contentType + ";charset=" + _characterEncoding;
+ }
+ else
+ {
+ _encodingFrom=EncodingFrom.NOT_SET;
+ _characterEncoding=null;
+ }
+ break;
+ case SET_LOCALE:
+ case SET_CHARACTER_ENCODING:
+ {
+ _contentType = contentType + ";charset=" + _characterEncoding;
+ _mimeType = null;
+ }
}
}
else if (isWriting() && !charset.equalsIgnoreCase(_characterEncoding))
@@ -1126,7 +1138,7 @@ public class Response implements HttpServletResponse
else
{
_characterEncoding = charset;
- _explicitEncoding = true;
+ _encodingFrom = EncodingFrom.SET_CONTENT_TYPE;
}
if (HttpGenerator.__STRICT || _mimeType==null)
@@ -1137,15 +1149,16 @@ public class Response implements HttpServletResponse
_fields.put(_mimeType.getContentTypeField());
}
}
-
}
@Override
public void setBufferSize(int size)
{
- if (isCommitted() || getContentCount() > 0)
- throw new IllegalStateException("cannot set buffer size on committed response");
- if (size <= 0)
+ if (isCommitted())
+ throw new IllegalStateException("cannot set buffer size after response is in committed state");
+ if (getContentCount() > 0)
+ throw new IllegalStateException("cannot set buffer size after response has " + getContentCount() + " bytes already written");
+ if (size < __MIN_BUFFER_SIZE)
size = __MIN_BUFFER_SIZE;
_out.setBufferSize(size);
}
@@ -1166,14 +1179,24 @@ public class Response implements HttpServletResponse
@Override
public void reset()
{
+ reset(false);
+ }
+
+ public void reset(boolean preserveCookies)
+ {
resetForForward();
_status = 200;
_reason = null;
_contentLength = -1;
+
+ List<HttpField> cookies = preserveCookies
+ ?_fields.stream()
+ .filter(f->f.getHeader()==HttpHeader.SET_COOKIE)
+ .collect(Collectors.toList()):null;
+
_fields.clear();
- String connection = _channel.getRequest().getHeader(HttpHeader.CONNECTION.asString());
-
+ String connection = _channel.getRequest().getHeader(HttpHeader.CONNECTION.asString());
if (connection != null)
{
for (String value: StringUtil.csvSplit(null,connection,0,connection.length()))
@@ -1200,21 +1223,23 @@ public class Response implements HttpServletResponse
}
}
}
- }
- public void reset(boolean preserveCookies)
- {
- if (!preserveCookies)
- reset();
+ if (preserveCookies)
+ cookies.forEach(f->_fields.add(f));
else
{
- ArrayList<String> cookieValues = new ArrayList<String>(5);
- Enumeration<String> vals = _fields.getValues(HttpHeader.SET_COOKIE.asString());
- while (vals.hasMoreElements())
- cookieValues.add(vals.nextElement());
- reset();
- for (String v:cookieValues)
- _fields.add(HttpHeader.SET_COOKIE, v);
+ Request request = getHttpChannel().getRequest();
+ HttpSession session = request.getSession(false);
+ if (session!=null && session.isNew())
+ {
+ SessionManager sm = request.getSessionManager();
+ if (sm!=null)
+ {
+ HttpCookie c=sm.getSessionCookie(session,request.getContextPath(),request.isSecure());
+ if (c!=null)
+ addCookie(c);
+ }
+ }
}
}
@@ -1227,9 +1252,6 @@ public class Response implements HttpServletResponse
@Override
public void resetBuffer()
{
- if (isCommitted())
- throw new IllegalStateException("cannot reset buffer on committed response");
-
_out.resetBuffer();
}
@@ -1277,8 +1299,8 @@ public class Response implements HttpServletResponse
String charset = _channel.getRequest().getContext().getContextHandler().getLocaleEncoding(locale);
- if (charset != null && charset.length() > 0 && !_explicitEncoding)
- setCharacterEncoding(charset,false);
+ if (charset != null && charset.length() > 0 && __localeOverride.contains(_encodingFrom))
+ setCharacterEncoding(charset,EncodingFrom.SET_LOCALE);
}
@Override
diff --git a/pom.xml b/pom.xml
index 059834f494..b7e227a1ee 100644
--- a/pom.xml
+++ b/pom.xml
@@ -231,7 +231,7 @@
<properties>
<launcher.version>1.0.2</launcher.version>
<jersey.version>2.13</jersey.version>
- <jetty.version>9.3.8.v20160314</jetty.version>
+ <jetty.version>9.3.24.v20180605</jetty.version>
<jackson.version>2.4.3</jackson.version>
<servlet.version>3.1.0</servlet.version>
<antlr.version>4.5.1-1</antlr.version>