< Back to Blog Overview

SSL Expiry testing with Puppeteer

2020-07-24
SSL Expiry testing with Puppeteer

A great use-case for Headless Testing is testing if your SSL certificates are about to expire.


Most websites nowadays enforce HTTPs, however these HTTPs certificates all have an expiration date. This means that website owners need to make sure that they renew or replace their certificate before the expiration date.


What happens when a certificate expires?

When a certificate expires, users will no longer be able to access your website.
The browser will display a SSL error and will try to prevent the user from continuing to the website.


The error message displayed by the browser will cause your users to leave your website.


The solution

The solution is to use a Puppeteer script to (daily) check the expiry date of your SSL certificate.
You can run this script in a CI/CD pipeline and have it alert you when the certificate is about to expire.


Please see an example on how to use Puppeteer and HeadlessTesting.com's browser grid to do this:

const browser = await puppeteer.connect({
  browserWSEndpoint: 'wss://chrome.headlesstesting.com?token=[TOKEN]'
})
const page = await browser.newPage()
await page.setRequestInterception(true)
page.on("request", (request) => request.continue())
page.on("response", (response) => {
  const contentType = response.headers()["content-type"]

  if (contentType.match("text/html")) {
    const securityDetails = response.securityDetails()
    const expiryDate = securityDetails.validTo() * 1000
    const now = new Date().getTime()
    const daysLeft = Math.min(((expiryDate - now) / 1000) / (60 * 60 * 24))
    if (daysLeft < 5) { 
    	// alert
    }
  }
})

await page.goto('https://headlesstesting.com/')
browser.close()

This script will start a Chrome browser on our grid, go to the website you specified, fetch the SSL certificate and verify if its expiry date is not less than 5 days from now.

HeadlessTesting Logo

Sign up for a Free Trial

Start Headless Testing instantly.

No credit card required!