Adding support to detect bad UTF-8 text message

This commit is contained in:
Joakim Erdfelt 2012-07-10 15:03:47 -07:00
parent 8aa6e5db9b
commit 3ab8a0e5b7
2 changed files with 25 additions and 2 deletions

View File

@ -6,6 +6,8 @@ import java.nio.ByteBuffer;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.Utf8Appendable.NotUtf8Exception;
import org.eclipse.jetty.util.Utf8StringBuilder;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.annotations.WebSocket;
@ -265,7 +267,12 @@ public class WebSocketEventDriver implements Parser.Listener
try
{
BufferUtil.flipToFlush(activeMessage,0);
events.onText.call(websocket,connection,BufferUtil.toUTF8String(activeMessage));
byte data[] = BufferUtil.toArray(activeMessage);
Utf8StringBuilder utf = new Utf8StringBuilder();
// TODO: FIX EVIL COPY
utf.append(data,0,data.length);
events.onText.call(websocket,connection,utf.toString());
}
finally
{
@ -278,6 +285,10 @@ public class WebSocketEventDriver implements Parser.Listener
}
}
}
catch (NotUtf8Exception e)
{
terminateConnection(StatusCode.BAD_PAYLOAD,e.getMessage());
}
catch (CloseException e)
{
terminateConnection(e.getStatusCode(),e.getMessage());

View File

@ -10,6 +10,8 @@ import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.FutureCallback;
import org.eclipse.jetty.util.Utf8Appendable.NotUtf8Exception;
import org.eclipse.jetty.util.Utf8StringBuilder;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.annotations.OnWebSocketConnect;
@ -188,6 +190,16 @@ public class WebSocketServletRFCTest
}
}
@Test(expected = NotUtf8Exception.class)
public void testDetectBadUTF8()
{
byte buf[] = new byte[]
{ (byte)0xC2, (byte)0xC3 };
Utf8StringBuilder utf = new Utf8StringBuilder();
utf.append(buf,0,buf.length);
}
/**
* Test the requirement of issuing
*/
@ -351,7 +363,7 @@ public class WebSocketServletRFCTest
client.expectUpgradeResponse();
byte buf[] = new byte[]
{ (byte)0xC3, 0x28 };
{ (byte)0xC2, (byte)0xC3 };
WebSocketFrame txt = WebSocketFrame.text().setPayload(buf);
ByteBuffer bb = generator.generate(txt);