| <!DOCTYPE html> |
| <html> |
| <head> |
| <script src="/js-test-resources/js-test.js"></script> |
| <script src="/cookies/resources/cookie-utilities.js"></script> |
| </head> |
| <body> |
| <script> |
| description("Check that cookies created by JavaScript with max-age or expiry longer than a week get capped to a week."); |
| jsTestIsAsync = true; |
| |
| if (internals) |
| internals.setResourceLoadStatisticsEnabled(true); |
| |
| let passedTests = 0; |
| function checkThatCookieDoesNotExpireAfter(cookieData, maxAgeInSeconds) { |
| let now = new Date(); |
| let maxExpiryDateInMilliseconds = now.getTime() + (maxAgeInSeconds * 1000); |
| |
| if (maxExpiryDateInMilliseconds > cookieData["expires"]) |
| ++passedTests; |
| else |
| testFailed("Cookie named " + cookieData["name"] + " expires in more than " + maxAgeInSeconds + " seconds."); |
| } |
| |
| const twoDaysInSeconds = 2 * 24 * 60 * 60; |
| const shortLivedCookieMaxAge = { name : "shortLivedCookieMaxAge", lifetime : "Max-Age=" + twoDaysInSeconds + ";" }; |
| document.cookie = shortLivedCookieMaxAge.name + "=foobar; " + shortLivedCookieMaxAge.lifetime + " path=/"; |
| |
| const twoDaysAsExpiresDate = createExpiresDateFromMaxAge(twoDaysInSeconds); |
| const shortLivedCookieExpires = { name : "shortLivedCookieExpires", lifetime : "Expires=" + twoDaysAsExpiresDate + ";" }; |
| document.cookie = shortLivedCookieExpires.name + "=foobar; " + shortLivedCookieExpires.lifetime + " path=/"; |
| |
| const oneWeekInSeconds = 7 * 24 * 60 * 60; |
| const twoWeeksInSeconds = 2 * oneWeekInSeconds; |
| const longLivedCookieMaxAge = { name : "longLivedCookieMaxAge", lifetime : "Max-Age=" + twoWeeksInSeconds + ";" }; |
| document.cookie = longLivedCookieMaxAge.name + "=foobar; " + longLivedCookieMaxAge.lifetime + " path=/"; |
| |
| const twoWeeksAsExpiresDate = createExpiresDateFromMaxAge(twoWeeksInSeconds); |
| const longLivedCookieExpires = { name : "longLivedCookieExpires", lifetime : "Expires=" + twoWeeksAsExpiresDate + ";" }; |
| document.cookie = longLivedCookieExpires.name + "=foobar; " + longLivedCookieExpires.lifetime + " path=/"; |
| |
| const overTwoDaysInSeconds = twoDaysInSeconds + 30; |
| const overTwoWeeksInSeconds = twoWeeksInSeconds + 30; |
| if (internals) { |
| let cookies = internals.getCookies(); |
| if (!cookies.length) |
| testFailed("No cookies found."); |
| for (let cookie of cookies) { |
| switch (cookie.name) { |
| case shortLivedCookieMaxAge.name: |
| checkThatCookieDoesNotExpireAfter(cookie, overTwoDaysInSeconds); |
| break; |
| case shortLivedCookieExpires.name: |
| checkThatCookieDoesNotExpireAfter(cookie, overTwoDaysInSeconds); |
| break; |
| case longLivedCookieMaxAge.name: |
| checkThatCookieDoesNotExpireAfter(cookie, overTwoWeeksInSeconds); |
| break; |
| case longLivedCookieExpires.name: |
| checkThatCookieDoesNotExpireAfter(cookie, overTwoWeeksInSeconds); |
| break; |
| } |
| } |
| |
| resetCookiesForCurrentOrigin(); |
| |
| if (passedTests === 4) { |
| testPassed("The two short-lived cookies don't expire after more than " + overTwoDaysInSeconds + " seconds."); |
| testPassed("The two long-lived cookies don't expire after more than " + overTwoWeeksInSeconds + " seconds."); |
| } else |
| testFailed("At least one cookie's expiry attribute was beyond the test thresholds."); |
| } else |
| testFailed("No internals object."); |
| |
| if (internals) |
| internals.setResourceLoadStatisticsEnabled(false); |
| |
| finishJSTest(); |
| </script> |
| </body> |
| </html> |