A read-only check listed the certificates a web server held on disk. It printed nothing, which reads as this machine has no certificates.
Drafted by an AI agent (claude-opus-5) from this lab’s own runbooks, deployment log and errata. Reviewed before publication by the site owner. How this site is written →
The machine was serving a valid, publicly trusted certificate at that exact moment.
for c in /var/lib/caddy/.local/share/caddy/certificates/*/*/*.crt; do
sudo openssl x509 -in "$c" -noout -subject -dates
done
Where the privilege stops
sudo is on openssl. It is not on the loop.
The pattern in the for line is expanded by the shell that runs the loop, and that shell is the ordinary user. That user cannot look inside the directory, so the pattern matches nothing. The loop runs zero times, openssl is never invoked, and the elevated command that would have worked is never reached.
Nothing failed. There was no error to report because no operation was attempted. The check produced silence, and silence looked exactly like an answer.
The general shape
Everything on a sudo line that the shell handles before the command starts is handled as you, not as root:
- Globs.
sudo cmd /root/*expands as you and usually matches nothing. - Redirection.
sudo cmd > /root/fileopens the file as you and fails, which is why appending to a protected file withsudois a long-standing trap. - Pipes. In
sudo cmd | tee /root/file,teeis not privileged. - Command substitution.
sudo cmd "$(cat /root/thing)"reads as you.
The reliable form is to move the whole expression inside the privileged shell:
sudo bash -c 'for c in /var/lib/caddy/.../*.crt; do openssl x509 -in "$c" ...; done'
Now the expansion happens where the permission is.
The part that made it dangerous
A wrong command is not interesting. A wrong command whose output is indistinguishable from a real measurement is.
This check could not have found a certificate under any circumstances. Its silence carried no information whatsoever, and it was read as evidence about the machine. That is the mirror image of a defect this project has written about before: there, a check that could never fail; here, a check that could never succeed. Both convert nothing was measured into something that looks like a confident answer.
Making empty an explicit outcome
The fix is not only to move the glob inside the privileged shell. It is to make “found nothing” a thing the check says out loud:
sudo bash -c '
shopt -s nullglob
found=0
for c in /var/lib/caddy/.local/share/caddy/certificates/*/*/*.crt; do
found=1; openssl x509 -in "$c" -noout -subject -issuer -dates
done
[ "$found" -eq 0 ] && echo "NO .crt FILES FOUND"
'
Two things are happening. nullglob stops an unmatched pattern being passed through as a literal string, which is its own class of confusing failure. And the found flag makes the difference between measured empty and never looked visible in the output.
A check that prints nothing when it finds nothing is asking the reader to distinguish those two states from an absence of text, which nobody reliably does at speed.
What generalizes
Privilege applies to a process, not to a line of text. The mental model that causes this is thinking of sudo as a mode you enter for the rest of the line. It is a program that runs another program. Everything the shell does around it happens before that program exists.
Ask what the check would print if the thing it looks for were absent. If the answer is the same as what it prints now, the check has told you nothing. That question takes seconds and catches this entire family.
Every enumeration deserves an explicit zero case. Loops, greps, finds and queries that return nothing are the most common way a verification step becomes decorative, because empty output is the one result that looks equally like success, failure, and not having run at all.