Skip to content

[Bug] NullPointerException will be thrown at request.getCookies() if the request does not contain cookies #15982

@painAdmin

Description

@painAdmin

Pre-check

  • I am sure that all the content I provide is in English.

Search before asking

  • I had searched in the issues and found no similar issues.

Apache Dubbo Component

Java SDK (apache/dubbo)

Dubbo Version

Dubbo:3.2.2, jdk:java version "1.8.0_191", CentOS Linux release 7

Steps to reproduce this issue

背景:使用springboot+dubbo,搭建一个web服务,协议为tri协议,定义controller接口为
Background: Building a web service using Spring Boot and Dubbo, with the tri protocol, and defining the controller interface as follows.

import com.test.IndexApiService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@DubboService
public class IndexApi implements IndexApiService {

    @RequestMapping("/api/index")
    @Override
    public String index(HttpServletRequest request, HttpServletResponse response) {
        return "ok"+getCookie(request,"test");
    }

   /**
       * 根据cookie名字获取value
       * Retrieve value based on cookie name
       */
  public String getCookie(HttpServletRequest request, String name) {
          try {
              Cookie[] cookies = request.getCookies();
              if (cookies != null) {
                  for (Cookie cookie : cookies) {
                      if (name.equals(cookie.getName())) {
                          return cookie.getValue();
                      }
                  }
              }
          } catch (Exception e) {
              logger.error("error,", e);
          }
          return null;
   }
}

复现过程:
Reproduction process:
当请求中无cookie时候,会报NullPointerException 异常,堆栈如下
When the request does not contain cookies, a NullPointerException will be thrown, as shown in the stack trace below.

java.lang.NullPointerException: header
	at io.netty.util.internal.ObjectUtil.checkNotNull(ObjectUtil.java:39)
	at io.netty.handler.codec.http.cookie.ServerCookieDecoder.decode(ServerCookieDecoder.java:89)
	at io.netty.handler.codec.http.cookie.ServerCookieDecoder.decodeAll(ServerCookieDecoder.java:70)
	at org.apache.dubbo.remoting.http12.HttpUtils.decodeCookies(HttpUtils.java:78)
	at org.apache.dubbo.remoting.http12.message.DefaultHttpRequest.cookies(DefaultHttpRequest.java:181)
	at org.apache.dubbo.rpc.protocol.tri.rest.support.servlet.ServletHttpRequestAdapter.getCookies(ServletHttpRequestAdapter.java:79)

What you expected to happen

在org.apache.dubbo.remoting.http12.HttpUtils 中方法:

public static List<HttpCookie> decodeCookies(String value) {
        List<HttpCookie> cookies = new ArrayList<>();
        for (Cookie c : ServerCookieDecoder.LAX.decodeAll(value)) {
            cookies.add(new HttpCookie(c.name(), c.value()));
        }
        return cookies;
    }

ServerCookieDecoder.LAX.decodeAll(value)
这个方法没有兼容value为空的情况,如果会空回报空指针
The method is incompatible with cases where the value is empty, resulting in an error.

Anything else

No response

Are you willing to submit a pull request to fix on your own?

  • Yes I am willing to submit a pull request on my own!

Code of Conduct

Metadata

Metadata

Assignees

Labels

help wantedEverything needs help from contributorstype/bugBugs to being fixed

Type

No type

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions