From d2715d2fa1a9e8e6fcad23e6abe39234ed20bbf6 Mon Sep 17 00:00:00 2001 From: Riley Park Date: Mon, 12 Aug 2024 19:45:30 -0700 Subject: [PATCH] Fix incorrect weak ETag assertion Closes gh-33377 --- .../main/java/org/springframework/http/HttpHeaders.java | 7 +++---- .../java/org/springframework/http/HttpHeadersTests.java | 8 +++++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java index 132e47846a09..cb1dee45bbaa 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java +++ b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -1037,9 +1037,8 @@ public long getDate() { */ 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 \""); + Assert.isTrue(etag.startsWith("\"") || etag.startsWith("W/\""), "ETag does not start with W/\" or \""); + Assert.isTrue(etag.endsWith("\""), "ETag does not end with \""); set(ETAG, etag); } else { diff --git a/spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java b/spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java index 0bad35ed16ce..2aa52c5c2d18 100644 --- a/spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java +++ b/spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java @@ -192,11 +192,17 @@ void ipv6Host() { } @Test - void illegalETag() { + void illegalETagWithoutQuotes() { String eTag = "v2.6"; assertThatIllegalArgumentException().isThrownBy(() -> headers.setETag(eTag)); } + @Test + void illegalWeakETagWithoutLeadingQuote() { + String etag = "W/v2.6\""; + assertThatIllegalArgumentException().isThrownBy(() -> headers.setETag(etag)); + } + @Test void ifMatch() { String ifMatch = "\"v2.6\"";