View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.http.websocketx;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.ChannelFutureListener;
20  import io.netty.channel.ChannelHandlerContext;
21  import io.netty.channel.ChannelInboundHandlerAdapter;
22  
23  /**
24   *
25   */
26  public class Utf8FrameValidator extends ChannelInboundHandlerAdapter {
27  
28      private final boolean closeOnProtocolViolation;
29  
30      private int fragmentedFramesCount;
31      private Utf8Validator utf8Validator;
32  
33      public Utf8FrameValidator() {
34          this(true);
35      }
36  
37      public Utf8FrameValidator(boolean closeOnProtocolViolation) {
38          this.closeOnProtocolViolation = closeOnProtocolViolation;
39      }
40  
41      // See https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.
42      private static boolean isControlFrame(WebSocketFrame frame) {
43          return frame instanceof CloseWebSocketFrame ||
44                  frame instanceof PingWebSocketFrame ||
45                  frame instanceof PongWebSocketFrame;
46      }
47  
48      @Override
49      public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
50          if (msg instanceof WebSocketFrame) {
51              WebSocketFrame frame = (WebSocketFrame) msg;
52  
53              try {
54                  // Processing for possible fragmented messages for text and binary
55                  // frames
56                  if (frame.isFinalFragment()) {
57                      // Control frames are allowed between fragments
58                      // See https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.
59                      if (!isControlFrame(frame)) {
60  
61                          // Final frame of the sequence.
62                          fragmentedFramesCount = 0;
63  
64                          // Check text for UTF8 correctness
65                          if (frame instanceof TextWebSocketFrame ||
66                                  (utf8Validator != null && utf8Validator.isChecking())) {
67                              // Check UTF-8 correctness for this payload
68                              checkUTF8String(frame.content());
69  
70                              // This does a second check to make sure UTF-8
71                              // correctness for entire text message
72                              utf8Validator.finish();
73                          }
74                      }
75                  } else {
76                      // Not final frame so we can expect more frames in the
77                      // fragmented sequence
78                      if (fragmentedFramesCount == 0) {
79                          // First text or binary frame for a fragmented set
80                          if (frame instanceof TextWebSocketFrame) {
81                              checkUTF8String(frame.content());
82                          }
83                      } else {
84                          // Subsequent frames - only check if init frame is text
85                          if (utf8Validator != null && utf8Validator.isChecking()) {
86                              checkUTF8String(frame.content());
87                          }
88                      }
89  
90                      // Increment counter
91                      fragmentedFramesCount++;
92                  }
93              } catch (CorruptedWebSocketFrameException e) {
94                  protocolViolation(ctx, frame, e);
95              }
96          }
97  
98          super.channelRead(ctx, msg);
99      }
100 
101     private void checkUTF8String(ByteBuf buffer) {
102         if (utf8Validator == null) {
103             utf8Validator = new Utf8Validator();
104         }
105         utf8Validator.check(buffer);
106     }
107 
108     private void protocolViolation(ChannelHandlerContext ctx, WebSocketFrame frame,
109                                    CorruptedWebSocketFrameException ex) {
110         frame.release();
111         if (closeOnProtocolViolation && ctx.channel().isOpen()) {
112             WebSocketCloseStatus closeStatus = ex.closeStatus();
113             String reasonText = ex.getMessage();
114             if (reasonText == null) {
115                 reasonText = closeStatus.reasonText();
116             }
117 
118             CloseWebSocketFrame closeFrame = new CloseWebSocketFrame(closeStatus.code(), reasonText);
119             ctx.writeAndFlush(closeFrame).addListener(ChannelFutureListener.CLOSE);
120         }
121 
122         throw ex;
123     }
124 
125     @Override
126     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
127         super.exceptionCaught(ctx, cause);
128     }
129 }