diff --git a/HISTORY.md b/HISTORY.md index edd6c05..e463052 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,8 @@ +unreleased +========== + + * Fix handling of modified headers with invalid dates + 0.5.0 / 2017-02-21 ================== diff --git a/index.js b/index.js index bd9812e..dd49dee 100644 --- a/index.js +++ b/index.js @@ -70,7 +70,7 @@ function fresh (reqHeaders, resHeaders) { // if-modified-since if (modifiedSince) { var lastModified = resHeaders['last-modified'] - var modifiedStale = !lastModified || Date.parse(lastModified) > Date.parse(modifiedSince) + var modifiedStale = !lastModified || !(parseHttpDate(lastModified) <= parseHttpDate(modifiedSince)) if (modifiedStale) { return false @@ -79,3 +79,19 @@ function fresh (reqHeaders, resHeaders) { return true } + +/** + * Parse an HTTP Date into a number. + * + * @param {string} date + * @private + */ + +function parseHttpDate (date) { + var timestamp = date && Date.parse(date) + + // istanbul ignore next: guard against date.js Date.parse patching + return typeof timestamp === 'number' + ? timestamp + : NaN +} diff --git a/test/fresh.js b/test/fresh.js index cd7f72d..f5e66ac 100644 --- a/test/fresh.js +++ b/test/fresh.js @@ -107,15 +107,15 @@ describe('fresh(reqHeaders, resHeaders)', function () { describe('with invalid If-Modified-Since date', function () { it('should be stale', function () { var reqHeaders = { 'if-modified-since': 'foo' } - var resHeaders = { 'modified-since': 'Sat, 01 Jan 2000 00:00:00 GMT' } + var resHeaders = { 'last-modified': 'Sat, 01 Jan 2000 00:00:00 GMT' } assert.ok(!fresh(reqHeaders, resHeaders)) }) }) - describe('with invalid Modified-Since date', function () { + describe('with invalid Last-Modified date', function () { it('should be stale', function () { var reqHeaders = { 'if-modified-since': 'Sat, 01 Jan 2000 00:00:00 GMT' } - var resHeaders = { 'modified-since': 'foo' } + var resHeaders = { 'last-modified': 'foo' } assert.ok(!fresh(reqHeaders, resHeaders)) }) })