Skip to content

Commit

Permalink
fix: NPE on misformatted Date or Expires header (#2592)
Browse files Browse the repository at this point in the history
* fix: NPE on misformatted Date or Expires header

* fix: null check in date difference

* test: cache control max age with valid and invalid headers

* test: does not need to extend WebClientTestBase
  • Loading branch information
vinceh121 authored Apr 11, 2024
1 parent 212dd22 commit a6d16eb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.time.Duration;
import java.time.Instant;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand Down Expand Up @@ -53,13 +54,15 @@ private CacheControl(MultiMap headers) {
this.vary = headers.get(HttpHeaders.VARY);

if (headers.contains(HttpHeaders.DATE)) {
this.date = DateFormatter.parseHttpDate(headers.get(HttpHeaders.DATE)).toInstant();
Date date = DateFormatter.parseHttpDate(headers.get(HttpHeaders.DATE));
this.date = date == null ? null : date.toInstant();
} else {
this.date = Instant.now();
}

if (headers.contains(HttpHeaders.EXPIRES)) {
this.expires = DateFormatter.parseHttpDate(headers.get(HttpHeaders.EXPIRES)).toInstant();
Date expiresDate = DateFormatter.parseHttpDate(headers.get(HttpHeaders.EXPIRES));
this.expires = expiresDate == null ? null : expiresDate.toInstant();
} else {
this.expires = null;
}
Expand Down Expand Up @@ -150,7 +153,7 @@ private long computeMaxAge() {
return timeDirectives.get(CacheControlDirective.SHARED_MAX_AGE);
} else if (timeDirectives.containsKey(CacheControlDirective.MAX_AGE)) {
return timeDirectives.get(CacheControlDirective.MAX_AGE);
} else if (expires != null) {
} else if (date != null && expires != null) {
return Duration.between(date, expires).getSeconds();
} else {
return Long.MAX_VALUE;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.vertx.ext.web.client.impl.cache;

import static org.junit.Assert.*;

import java.time.Duration;

import org.junit.Test;

import io.vertx.core.MultiMap;

public class CacheControlTest {
@Test
public void testMaxAge() {
MultiMap headers = MultiMap.caseInsensitiveMultiMap();
headers.add("Date", "Tue, 09 Apr 2024 19:10:51 GMT");
headers.add("Expires", "Thu, 09 May 2024 19:10:51 GMT");

CacheControl cc = CacheControl.parse(headers);
assertEquals(Duration.ofDays(30).getSeconds(), cc.getMaxAge());
}

@Test
public void testInvalidHeaders() {
// invalid Date
MultiMap headers = MultiMap.caseInsensitiveMultiMap();
headers.add("Date", "this header doesn't make sense");
headers.add("Expires", "Thu, 09 May 2024 19:10:51 GMT");

CacheControl cc = CacheControl.parse(headers);
assertEquals(Long.MAX_VALUE, cc.getMaxAge());

// invalid Expires
headers = MultiMap.caseInsensitiveMultiMap();
headers.add("Date", "Tue, 09 Apr 2024 19:10:51 GMT");
headers.add("Expires", "this header doesn't make sense");

cc = CacheControl.parse(headers);
assertEquals(Long.MAX_VALUE, cc.getMaxAge());
}
}

0 comments on commit a6d16eb

Please sign in to comment.