Skip to content

Commit 128e89b

Browse files
kashikecesarhernandezgt
authored andcommitted
Fix incorrect weak ETag assertion
Closes spring-projectsgh-33377
1 parent 93b3572 commit 128e89b

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

spring-web/src/main/java/org/springframework/http/HttpHeaders.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -783,12 +783,14 @@ public long getDate() {
783783
*/
784784
public void setETag(String etag) {
785785
if (etag != null) {
786-
Assert.isTrue(etag.startsWith("\"") || etag.startsWith("W/"),
787-
"Invalid ETag: does not start with W/ or \"");
788-
Assert.isTrue(etag.endsWith("\""), "Invalid ETag: does not end with \"");
786+
Assert.isTrue(etag.startsWith("\"") || etag.startsWith("W/\""), "ETag does not start with W/\" or \"");
787+
Assert.isTrue(etag.endsWith("\""), "ETag does not end with \"");
788+
set(ETAG, etag);
789789
}
790-
set(ETAG, etag);
791-
}
790+
else {
791+
remove(ETAG);
792+
}
793+
}
792794

793795
/**
794796
* Return the entity tag of the body, as specified by the {@code ETag} header.

spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ public void illegalETag() {
157157
assertEquals("Invalid ETag header", "\"v2.6\"", headers.getFirst("ETag"));
158158
}
159159

160+
@Test(expected = IllegalArgumentException.class)
161+
public void illegalWeakETagWithoutLeadingQuote() {
162+
String etag = "W/v2.6\"";
163+
headers.setETag(etag);
164+
}
165+
160166
@Test
161167
public void ifMatch() {
162168
String ifMatch = "\"v2.6\"";

0 commit comments

Comments
 (0)