Skip to content

Instantly share code, notes, and snippets.

@jeremypage
Last active January 31, 2019 10:40
Show Gist options
  • Save jeremypage/2a2af31352625e48d909 to your computer and use it in GitHub Desktop.
Save jeremypage/2a2af31352625e48d909 to your computer and use it in GitHub Desktop.
JavaScript: Check TLS level of browser and remove matching domain links from page if browser does not meet minimum requirement
// domains to check
var checkDomains = ['www.foobar.com', 'www.fubar.com'];
// check all links in DOM for matching domain(s)
var matchingLinks = [];
var documentLinks = document.links;
for (var i = 0; i < documentLinks.length; i++) {
if (checkDomains.indexOf(documentLinks[i].hostname) > -1)
matchingLinks.push(documentLinks[i]);
}
if (matchingLinks.length > 0) {
checkTLS('https://www.howsmyssl.com/a/check?callback=parseTLS', parseTLS);
}
// inserts TLS API script into DOM
function checkTLS(url, callback) {
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = url;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(newScript, s);
};
// interpret response from TLS API
function parseTLS(reponse) {
if (reponse.tls_version.split(' ')[1] < 1.2) {
//disableLinks();
alert('Your browser may not support the minimum security requirements for online payments.\n\nIf you receive an error message when attempting payments, please upgrade to the latest browser version');
}
};
// disable all matching links
function disableLinks() {
for (var i = 0; i < matchingLinks.length; i++) {
var span = document.createElement('span');
if (matchingLinks[i].className)
span.className = matchingLinks[i].className;
if (matchingLinks[i].id)
span.id = matchingLinks[i].id;
span.innerHTML = matchingLinks[i].innerHTML;
span.style.setProperty('text-decoration', 'line-through');
matchingLinks[i].parentNode.replaceChild(span, matchingLinks[i]);
console.log('Disabled link: ' + matchingLinks[i]);
}
}

Check users' browsers before they navigate a link which requires minimum recommended TLS security.

If browser does not meet required TLS level then disable relevant links and give warning. Only checks TLS if matching link domains found.

Uses TLS checking API from www.howsmyssl.com

Add to page with following:

<script src="check-tls.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment