Plugin hygiene and attack surface

Six plugins at the top of the tree, 75 installed, and how to keep them current.

Intermediate11 min · lesson 13 of 15
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (16 files, 7 KB): plugins.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-jenkins/plugins.tar.gz && tar -xzf plugins.tar.gz, which creates ~/lab/plugins/. SHA-256: 7190c549f78ef3d027558085159d582096ebdd13f7c4628765a09211f8382f54

Open the Jenkins security advisories and scroll for a while. Most entries are about plugins, and many affect plugins nobody on the team remembers installing, with fixes that wait until someone gets around to clicking "update". Each plugin is code running inside your controller with full access to it. This lesson measures how much code that is on a small server, and how to keep it current without clicking around in production.

How many plugins do you really run?

ubuntu@secopslog-cicd:~/lab/plugins · Jenkins 2.580.2
$ ./up.sh --no-agent
Jenkins 2.580.2 is up on http://127.0.0.1:8080
$ ./jk.sh '/pluginManager/api/json?depth=1&tree=plugins[shortName,dependencies[shortName]]' > /tmp/plugins.json; jq '.plugins | length' /tmp/plugins.json; jq -r '[.plugins[].dependencies[].shortName] as $deps | [.plugins[].shortName | select(. as $p | $deps | index($p) | not)] | join(" ")' /tmp/plugins.json
75 git matrix-auth pipeline-utility-steps timestamper workflow-aggregator ws-cleanup
$ jq -r '.plugins[] | select(.dependencies[].shortName == "git-client") | .shortName' /tmp/plugins.json | sort -u; rm /tmp/plugins.json
git

Seventy-five plugins are installed. The second line lists the ones that nothing else depends on, the top of the tree: six. Everything else came along as a dependency, including things no one here asked for by name. why answers the question to ask before removing anything: git-client is here because git needs it. Before you add a plugin, look at what it brings with it, as you're adding that whole subtree to your attack surface.

Finding updates without touching the server

The plugin manager CLI, inside the image, compares your list with the update center. It doesn't change anything. This script wraps it for a scheduled job:

check-updates.sh
#!/usr/bin/env bash
# Compares controller/plugins.txt with the update center, inside the controller image. Changes nothing.
# The CLI caches update-center data, and the image already holds a copy from when it was built,
# so the cache is cleared first: the check must ask the update center, not repeat old answers.
# It exits non-zero when it can't check, so a scheduled job can't report "0 updates" when it couldn't look.
# UC=URL points it at another update center (the lab uses that to show a failure).
set -euo pipefail
latest=$(docker compose exec -T ${UC:+-e JENKINS_UC="$UC"} controller sh -c \
'rm -rf ~/.cache/jenkins-plugin-management-cli && jenkins-plugin-cli --plugin-file /usr/share/jenkins/ref/plugins.txt --no-download --available-updates --output txt')
[ -n "$latest" ] || { echo "no answer from the update center" >&2; exit 1; }
echo "checked: $(grep -c . <<<"$latest")"
echo "newer than the pin: $(sort controller/plugins.txt | diff - <(sort <<<"$latest") | awk '/^>/{n++} END{print n+0}')"
ubuntu@secopslog-cicd:~/lab/plugins · Jenkins 2.580.2
$ ./check-updates.sh
checked: 75 newer than the pin: 0

The first number is how many plugins were checked, and the second is how many have a newer release than the pin. A number above zero is a to-do list, not an emergency. A check that can't reach the update center must fail, not report zero updates. Point the script at an address where nothing listens:

ubuntu@secopslog-cicd:~/lab/plugins · Jenkins 2.580.2
$ UC=http://127.0.0.1:9 ./check-updates.sh 2>&1 | tail -2 | cut -c1-140; echo "exit status: ${PIPESTATUS[0]}"
Unable to retrieve JSON from http://127.0.0.1:9/update-center.json?version=2.580.2: Connect to 127.0.0.1:9 [/127.0.0.1] failed: Connection r Error getting update center json exit status: 1

Error getting update center json, and the script exits 1. That took a fix. The plugin CLI keeps a cache of update-center data, and building the image fills it. When the first version of this script ran shortly after a rebuild, it answered from that cache and reported checked: 75, newer than the pin: 0 with no update center reachable at all. So the script deletes the cache before each check. Test your own checks the same way, against an address where nothing listens.

Security warnings come from the same CLI:

ubuntu@secopslog-cicd:~/lab/plugins · Jenkins 2.580.2
$ docker compose exec -T controller jenkins-plugin-cli --plugin-file /usr/share/jenkins/ref/plugins.txt --no-download --view-security-warnings 2>&1 | tail -4 | cut -c1-140
Done

Warnings come from the same update-center data, so the same cache applies. Here the check above had just refreshed it. --view-security-warnings prints any published advisory for the plugins in your list, and Done with nothing above it means none today. Run both on a schedule, as a job, and fail or notify if warnings appear. Remember this is point-in-time output, so yours will differ.

Jenkins itself has the same information once it has fetched the update center data:

ubuntu@secopslog-cicd:~/lab/plugins · Jenkins 2.580.2
$ ./jk.sh /pluginManager/checkUpdatesServer -X POST -o /dev/null; sleep 5; ./jk.sh '/pluginManager/api/json?depth=1&tree=plugins[shortName,version,hasUpdate]' | jq -c '[.plugins[] | select(.hasUpdate)] | {withUpdate: length}'
{"withUpdate":0}

To apply an update, the workflow is the one from the install lesson. Change the versions in controller/plugins.txt, rebuild the image, test on a throwaway instance, commit, and roll out. When you move to a new Jenkins LTS, check the plugin list against it before you change FROM: jenkins-plugin-cli --jenkins-version resolves the list for a given core version.

Rolling back is less simple than it looks. The official image copies its plugins into JENKINS_HOME/plugins on the volume. It upgrades a plugin there when the image has a newer version, but it never downgrades one, and it leaves alone any plugin someone updated through the UI. So an older image on the same volume keeps the newer plugins. A rollback means the previous image plus a restore of JENKINS_HOME from before the upgrade, which is another reason to back up first.

A pin that can't resolve

ubuntu@secopslog-cicd:~/lab/plugins · Jenkins 2.580.2
$ docker compose exec -T controller jenkins-plugin-cli --plugins git:0.0.1 --no-download --list 2>&1 | tail -2 | cut -c1-140
Plugin prerequisite not met: Plugin git:0.0.1 has unresolvable dependencies: Unable to resolve dependencies for git
$ ./down.sh
lab stopped and its data removed

A wrong version fails while you're building the image, which is the cheapest place for it to fail. Compare that with finding the problem during a production restart.

HabitWhy
Pin every plugin, including dependenciesBuilds repeat, and each change shows up in review
Check for updates and warnings on a scheduleAdvisories are public, so attackers read them too
Patch security warnings first, the rest in batchesKeeps upgrades small and regular
Remove what nothing usesA plugin you don't have can't have a CVE
Restrict Overall/Administer, so nobody installs plugins from the UIA plugin added through the UI stays on the volume, outside the image and outside review
Watch out
Plugin updates sometimes change on-disk data in a way the old version can't read. Back up before upgrading, and try the upgrade on a restored copy, as the last lesson shows. Also upgrade the Jenkins LTS line regularly, because new plugin releases tend to require a recent core.

Hands-on

In a fresh lab, run ./jk.sh '/pluginManager/api/json?depth=1&tree=plugins[shortName,version,hasUpdate,dependencies[shortName]]' and use jq to find the plugin with the most dependents. If you could remove pipeline-utility-steps from plugins.txt, would anything else in the list depend on it? Check with the why pattern. Finish with ./down.sh.

What you should see: commons-lang3-api and workflow-step-api are tied at 21 dependents each, followed by structs with 16, all libraries no one installs by name. Nothing in the list depends on pipeline-utility-steps, so it's a top-level plugin you could remove from plugins.txt once no Jenkinsfile uses its steps. The dependency list can't tell you about Jenkinsfiles, so search your repos for its steps, such as readJSON, too.

Remember

Six at the top turned into 75 on the server, so count the subtree before adding anything. Check for updates and warnings as a scheduled job that fails when it can't check, and change plugins only by editing plugins.txt and rebuilding the image.

Quick check
01A developer wants a plugin that formats build badges. It would add 11 new dependencies. How should you evaluate the request?
Incorrect — Plugins and their dependencies run inside the controller with full access.
Correct — Each is more code to patch and more advisories to watch, so a small benefit rarely justifies it.
Incorrect — Plugins install on the controller. Agents do not host them.
Incorrect — A disabled plugin provides nothing, so this tests nothing.
02Your scheduled job reports a security warning for a plugin in plugins.txt. What is the right order of work?
Incorrect — This makes the server differ from its image, and it skips a test run.
Incorrect — Advisories are public, so attackers can read them as soon as you can.
Incorrect — Removing a plugin in use without checking dependents can break jobs and data.
Correct — This is fast and repeatable, and you can roll back to the previous image.
03Where does jenkins-plugin-cli catch a pin to a version that doesn't exist?
Correct — In the lab it reported unresolvable dependencies, which is the cheapest moment to find out.
Incorrect — The tool resolves against the update center long before any restart.
Incorrect — Resolution happens without running any build at all.
Incorrect — It failed in the lab rather than substituting another version.

Related