How-Tos

How-To: List Every Third-Party Script Your Website Loads

Leave a comment

This week’s Optimizely mess showed how a script loaded from someone else’s server can turn your site into a malware dispenser. Step one is knowing what you load. Here’s a quick inventory from a Linux terminal.

1. Grab the script hosts

SITE=https://example.com
curl -sL "$SITE" | grep -oiE '<script[^>]+src="[^"]+"' \
  | grep -oE 'src="[^"]+"' | cut -d'"' -f2 \
  | awk -F/ '/^https?:/{print $3} /^\/\//{print $3}' | sort | uniq -c | sort -rn

Anything that isn’t your own domain is a third party with the power to run code on your visitors’ browsers. Repeat this for a few key pages, like the home page, a post and the login page.

2. Check for SRI and CSP

curl -sL "$SITE" | grep -oiE '<script[^>]+integrity="[^"]+"' | wc -l
curl -sI "$SITE" | grep -i content-security-policy

Subresource Integrity pins a static file to a hash, so a swapped copy won’t run. It doesn’t fit scripts a vendor changes all the time. For those, use a Content-Security-Policy that lists the hosts you allow, and ask whether you need the script at all.

3. On WordPress

Most third-party scripts come from plugins and themes. Every plugin you remove is one less outside script and one less thing to patch.

God Bless, and Use the Source!

References & Further Reading

Subscribe (RSS) Follow How-Tos via RSS

Leave a note

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.