-
Notifications
You must be signed in to change notification settings - Fork 38.9k
Closed
Labels
in: webIssues in web modules (web, webmvc, webflux, websocket)Issues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancementA general enhancement
Milestone
Description
Affects: v5.3.22
Description
Building a response with etags can result in a NullPointerException if the argument for etag is null. Surprisingly, HttpHeaders.setETag(@Nullable String etag) handles null arguments and is called in ResponseEntity.DefaultBuilder.eTag(String etag).
Sample Code
var response = ResponseEntity
.ok()
.eTag(null)
.body(body);Effected Code
spring-framework/spring-web/src/main/java/org/springframework/http/ResponseEntity.java
Lines 565 to 574 in b72ee5f
| public BodyBuilder eTag(String etag) { | |
| if (!etag.startsWith("\"") && !etag.startsWith("W/\"")) { | |
| etag = "\"" + etag; | |
| } | |
| if (!etag.endsWith("\"")) { | |
| etag = etag + "\""; | |
| } | |
| this.headers.setETag(etag); | |
| return this; | |
| } |
spring-framework/spring-web/src/main/java/org/springframework/http/HttpHeaders.java
Lines 1042 to 1052 in b72ee5f
| public void setETag(@Nullable String etag) { | |
| if (etag != null) { | |
| Assert.isTrue(etag.startsWith("\"") || etag.startsWith("W/"), | |
| "Invalid ETag: does not start with W/ or \""); | |
| Assert.isTrue(etag.endsWith("\""), "Invalid ETag: does not end with \""); | |
| set(ETAG, etag); | |
| } | |
| else { | |
| remove(ETAG); | |
| } | |
| } |
Solution
- Allow
nullstrings - remove etag for
nullstrings (likeHttpHeaders.setETag()already does)
Therefore it should be safe just to call HttpHeaders.setETag() for null values:
public BodyBuilder eTag(String etag) {
if (etag != null && !etag.startsWith("\"") && !etag.startsWith("W/\"")) {
etag = "\"" + etag;
}
if (etag != null && !etag.endsWith("\"")) {
etag = etag + "\"";
}
this.headers.setETag(etag);
return this;
}Metadata
Metadata
Assignees
Labels
in: webIssues in web modules (web, webmvc, webflux, websocket)Issues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancementA general enhancement