From 73d7b56401a805d4b6f2a6a2aa0935d41942a577 Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Tue, 20 Jun 2023 23:01:45 +0100 Subject: [PATCH] Adds endpoint for fetching SSL info --- netlify.toml | 5 +++++ server/lambda/ssl-check.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 server/lambda/ssl-check.js diff --git a/netlify.toml b/netlify.toml index 4f326d4..a5d5f7f 100644 --- a/netlify.toml +++ b/netlify.toml @@ -44,6 +44,11 @@ to = "/.netlify/functions/lighthouse-report" status = 301 force = true +[[redirects]] + from = "/ssl-check" + to = "/.netlify/functions/ssl-check" + status = 301 + force = true # For router history mode, ensure pages land on index [[redirects]] diff --git a/server/lambda/ssl-check.js b/server/lambda/ssl-check.js new file mode 100644 index 0000000..8c95b02 --- /dev/null +++ b/server/lambda/ssl-check.js @@ -0,0 +1,30 @@ +const https = require('https'); + +exports.handler = async function (event, context) { + const { url } = event.queryStringParameters; + + if (!url) { + return { + statusCode: 400, + body: 'url query parameter is required', + }; + } + + return new Promise((resolve, reject) => { + const req = https.request(url, res => { + resolve({ + statusCode: 200, + body: JSON.stringify(res.socket.getPeerCertificate()), + }); + }); + + req.on('error', (error) => { + resolve({ + statusCode: 500, + body: `Error fetching site certificate: ${error.message}`, + }); + }); + + req.end(); + }); +};