browser_aboutCertError_clockSkew.js (11504B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const PREF_SERVICES_SETTINGS_CLOCK_SKEW_SECONDS = 7 "services.settings.clock_skew_seconds"; 8 const PREF_SERVICES_SETTINGS_LAST_FETCHED = 9 "services.settings.last_update_seconds"; 10 11 // Security CertError Felt Privacy set to false 12 add_task(async function checkWrongSystemTimeWarning_feltPrivacyToFalse() { 13 await setSecurityCertErrorsFeltPrivacyToFalse(); 14 async function setUpPage() { 15 let browser; 16 let certErrorLoaded; 17 await BrowserTestUtils.openNewForegroundTab( 18 gBrowser, 19 () => { 20 gBrowser.selectedTab = BrowserTestUtils.addTab( 21 gBrowser, 22 "https://expired.example.com/" 23 ); 24 browser = gBrowser.selectedBrowser; 25 certErrorLoaded = BrowserTestUtils.waitForErrorPage(browser); 26 }, 27 false 28 ); 29 30 info("Loading and waiting for the cert error"); 31 await certErrorLoaded; 32 33 return SpecialPowers.spawn(browser, [], async function () { 34 let doc = content.document; 35 let div = doc.getElementById("errorShortDesc"); 36 let learnMoreLink = doc.getElementById("learnMoreLink"); 37 38 await ContentTaskUtils.waitForCondition( 39 () => div.textContent.includes("update your computer clock"), 40 "Correct error message found" 41 ); 42 43 return { 44 divDisplay: content.getComputedStyle(div).display, 45 text: div.textContent, 46 learnMoreLink: learnMoreLink.href, 47 }; 48 }); 49 } 50 51 // Pretend that we recently updated our kinto clock skew pref 52 Services.prefs.setIntPref( 53 PREF_SERVICES_SETTINGS_LAST_FETCHED, 54 Math.floor(Date.now() / 1000) 55 ); 56 57 // For this test, we want to trick Firefox into believing that 58 // the local system time (as returned by Date.now()) is wrong. 59 // Because we don't want to actually change the local system time, 60 // we will do the following: 61 62 // Take the validity date of our test page (expired.example.com). 63 let expiredDate = new Date("2010/01/05 12:00"); 64 let localDate = Date.now(); 65 66 // Compute the difference between the server date and the correct 67 // local system date. 68 let skew = Math.floor((localDate - expiredDate) / 1000); 69 70 // Make it seem like our reference server agrees that the certificate 71 // date is correct by recording the difference as clock skew. 72 Services.prefs.setIntPref(PREF_SERVICES_SETTINGS_CLOCK_SKEW_SECONDS, skew); 73 74 let localDateFmt = new Intl.DateTimeFormat("en-US", { 75 dateStyle: "medium", 76 }).format(localDate); 77 78 info("Loading a bad cert page with a skewed clock"); 79 let message = await setUpPage(); 80 81 isnot( 82 message.divDisplay, 83 "none", 84 "Wrong time message information is visible" 85 ); 86 ok( 87 message.text.includes("update your computer clock"), 88 "Correct error message found" 89 ); 90 ok( 91 message.text.includes("expired.example.com"), 92 "URL found in error message" 93 ); 94 ok(message.text.includes(localDateFmt), "Correct local date displayed"); 95 ok( 96 message.learnMoreLink.includes("time-errors"), 97 "time-errors in the Learn More URL" 98 ); 99 100 BrowserTestUtils.removeTab(gBrowser.selectedTab); 101 102 Services.prefs.clearUserPref(PREF_SERVICES_SETTINGS_LAST_FETCHED); 103 Services.prefs.clearUserPref(PREF_SERVICES_SETTINGS_CLOCK_SKEW_SECONDS); 104 }); 105 106 add_task(async function checkCertError_feltPrivacyToFalse() { 107 await setSecurityCertErrorsFeltPrivacyToFalse(); 108 async function setUpPage() { 109 gBrowser.selectedTab = BrowserTestUtils.addTab( 110 gBrowser, 111 "https://expired.example.com/" 112 ); 113 let browser = gBrowser.selectedBrowser; 114 let certErrorLoaded = BrowserTestUtils.waitForErrorPage(browser); 115 116 info("Loading and waiting for the cert error"); 117 await certErrorLoaded; 118 119 return SpecialPowers.spawn(browser, [], async function () { 120 let doc = content.document; 121 let el = doc.getElementById("errorWhatToDoText"); 122 await ContentTaskUtils.waitForCondition(() => el.textContent); 123 return el.textContent; 124 }); 125 } 126 127 // The particular error message will be displayed only when clock_skew_seconds is 128 // less or equal to a day and the difference between date.now() and last_fetched is less than 129 // or equal to 5 days. Setting the prefs accordingly. 130 131 Services.prefs.setIntPref( 132 PREF_SERVICES_SETTINGS_LAST_FETCHED, 133 Math.floor(Date.now() / 1000) 134 ); 135 136 let skew = 60 * 60 * 24; 137 Services.prefs.setIntPref(PREF_SERVICES_SETTINGS_CLOCK_SKEW_SECONDS, skew); 138 139 info("Loading a bad cert page"); 140 let message = await setUpPage(); 141 142 ok( 143 message.includes( 144 "The issue is most likely with the website, and there is nothing you can do" + 145 " to resolve it. You can notify the website’s administrator about the problem." 146 ), 147 "Correct error message found" 148 ); 149 150 BrowserTestUtils.removeTab(gBrowser.selectedTab); 151 152 Services.prefs.clearUserPref(PREF_SERVICES_SETTINGS_LAST_FETCHED); 153 Services.prefs.clearUserPref(PREF_SERVICES_SETTINGS_CLOCK_SKEW_SECONDS); 154 }); 155 156 // Security CertError Felt Privacy set to true 157 add_task(async function checkWrongSystemTimeWarning_feltPrivacyToTrue() { 158 await setSecurityCertErrorsFeltPrivacyToTrue(); 159 async function setUpPage() { 160 let browser; 161 let certErrorLoaded; 162 await BrowserTestUtils.openNewForegroundTab( 163 gBrowser, 164 () => { 165 gBrowser.selectedTab = BrowserTestUtils.addTab( 166 gBrowser, 167 "https://expired.example.com/" 168 ); 169 browser = gBrowser.selectedBrowser; 170 certErrorLoaded = BrowserTestUtils.waitForErrorPage(browser); 171 }, 172 false 173 ); 174 175 info("Loading and waiting for the cert error"); 176 await certErrorLoaded; 177 178 return SpecialPowers.spawn(browser, [], async function () { 179 const netErrorCard = 180 content.document.querySelector("net-error-card").wrappedJSObject; 181 await netErrorCard.getUpdateComplete(); 182 183 Assert.ok( 184 netErrorCard.certErrorBodyTitle, 185 "The error page title should exist." 186 ); 187 188 const shortDesc = netErrorCard.certErrorIntro; 189 const advancedButton = netErrorCard.advancedButton; 190 Assert.ok(advancedButton, "The advanced button should exist."); 191 192 Assert.equal( 193 advancedButton.dataset.l10nId, 194 "fp-certerror-advanced-button", 195 "Button should have the 'advanced' l10n ID." 196 ); 197 198 // Perform user button click interaction 199 EventUtils.synthesizeMouseAtCenter(advancedButton, {}, content); 200 201 // Wait for the exception button to be enabled 202 // This ensures that the advanced section is fully loaded 203 await ContentTaskUtils.waitForCondition( 204 () => 205 netErrorCard.exceptionButton && 206 !netErrorCard.exceptionButton.disabled, 207 "Wait for the exception button to be created." 208 ); 209 210 const whatCanYouDo = netErrorCard.whatCanYouDo; 211 Assert.equal( 212 whatCanYouDo.dataset.l10nId, 213 "fp-certerror-expired-what-can-you-do-body", 214 "What can you do section should have fp-certerror-expired-what-can-you-do-body l10n ID." 215 ); 216 217 const whatCanYouDoArgs = JSON.parse(whatCanYouDo.dataset.l10nArgs); 218 Assert.ok( 219 whatCanYouDoArgs.date, 220 "What can you do section should have timestamp." 221 ); 222 223 return { 224 divDisplay: content.getComputedStyle(shortDesc).display, 225 text: shortDesc.textContent, 226 learnMoreLink: netErrorCard.learnMoreLink.href, 227 whatCanYouDoText: whatCanYouDo.textContent, 228 }; 229 }); 230 } 231 232 // Pretend that we recently updated our kinto clock skew pref 233 SpecialPowers.pushPrefEnv({ 234 set: [[PREF_SERVICES_SETTINGS_LAST_FETCHED, Math.floor(Date.now() / 1000)]], 235 }); 236 237 // For this test, we want to trick Firefox into believing that 238 // the local system time (as returned by Date.now()) is wrong. 239 // Because we don't want to actually change the local system time, 240 // we will do the following: 241 242 // Take the validity date of our test page (expired.example.com). 243 let expiredDate = new Date("2010/01/05 12:00"); 244 let localDate = Date.now(); 245 246 // Compute the difference between the server date and the correct 247 // local system date. 248 let skew = Math.floor((localDate - expiredDate) / 1000); 249 250 // Make it seem like our reference server agrees that the certificate 251 // date is correct by recording the difference as clock skew. 252 SpecialPowers.pushPrefEnv({ 253 set: [[PREF_SERVICES_SETTINGS_CLOCK_SKEW_SECONDS, skew]], 254 }); 255 256 let localDateFmt = new Intl.DateTimeFormat("en-US", { 257 year: "numeric", 258 month: "numeric", 259 day: "numeric", 260 }).format(localDate); 261 262 info("Loading a bad cert page with a skewed clock"); 263 let contentData = await setUpPage(); 264 265 Assert.notEqual( 266 contentData.divDisplay, 267 "none", 268 "Wrong time message information is visible" 269 ); 270 271 Assert.ok( 272 contentData.text.includes("expired.example.com"), 273 "URL found in error message" 274 ); 275 276 Assert.ok( 277 contentData.whatCanYouDoText.includes(localDateFmt), 278 "Correct local date displayed" 279 ); 280 Assert.ok( 281 contentData.learnMoreLink.includes("time-errors"), 282 "time-errors in the Learn More URL" 283 ); 284 285 BrowserTestUtils.removeTab(gBrowser.selectedTab); 286 }); 287 288 add_task(async function checkCertError_feltPrivacyToTrue() { 289 await setSecurityCertErrorsFeltPrivacyToTrue(); 290 291 async function setUpPage() { 292 gBrowser.selectedTab = BrowserTestUtils.addTab( 293 gBrowser, 294 "https://expired.example.com/" 295 ); 296 let browser = gBrowser.selectedBrowser; 297 let certErrorLoaded = BrowserTestUtils.waitForErrorPage(browser); 298 299 info("Loading and waiting for the cert error"); 300 await certErrorLoaded; 301 302 return SpecialPowers.spawn(browser, [], async function () { 303 const netErrorCard = 304 content.document.querySelector("net-error-card").wrappedJSObject; 305 await netErrorCard.getUpdateComplete(); 306 307 // Get the advanced container 308 // Perform user button click interaction 309 EventUtils.synthesizeMouseAtCenter( 310 netErrorCard.advancedButton, 311 {}, 312 content 313 ); 314 315 await ContentTaskUtils.waitForCondition( 316 () => 317 netErrorCard.exceptionButton && 318 !netErrorCard.exceptionButton.disabled, 319 "Wait for the exception button to be created." 320 ); 321 322 const whatCanYouDo = netErrorCard.whatCanYouDo; 323 await ContentTaskUtils.waitForCondition(() => whatCanYouDo.textContent); 324 Assert.equal( 325 whatCanYouDo.dataset.l10nId, 326 "fp-certerror-expired-what-can-you-do-body", 327 "Should have the fp-certerror-expired-what-can-you-do-body l10n ID." 328 ); 329 return whatCanYouDo.textContent; 330 }); 331 } 332 333 // The particular error message will be displayed only when clock_skew_seconds is 334 // less or equal to a day and the difference between date.now() and last_fetched is less than 335 // or equal to 5 days. Setting the prefs accordingly. 336 337 let skew = 60 * 60 * 24; 338 SpecialPowers.pushPrefEnv({ 339 set: [ 340 [PREF_SERVICES_SETTINGS_LAST_FETCHED, Math.floor(Date.now() / 1000)], 341 [PREF_SERVICES_SETTINGS_CLOCK_SKEW_SECONDS, skew], 342 ], 343 }); 344 345 info("Loading a bad cert page"); 346 let message = await setUpPage(); 347 348 let localDate = Date.now(); 349 let localDateFmt = new Intl.DateTimeFormat("en-US", { 350 year: "numeric", 351 month: "numeric", 352 day: "numeric", 353 }).format(localDate); 354 355 Assert.ok(message.includes(localDateFmt), "Message has local date displayed"); 356 357 BrowserTestUtils.removeTab(gBrowser.selectedTab); 358 });