certmagic: Corrupted metadata JSON files caused by bug #297
The bug #297 frequently leads to corrupted JSON files when multiple instances mount a shared NFS directory as storage. We’ve encountered numerous cases in production where this causes certificate failures with the error message decoding certificate metadata: invalid character '}' after top-level value, rendering the affected sites completely unusable.
The number of corrupted files is increasing, and I can’t restart the service.
I see the bug fix is in the master branch. Please release a new version that includes this fix. Additionally, how can I identify and repair the already corrupted files among thousands without deleting all files? Alternatively, is there a way to ignore corrupted files during loading?
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 46 (21 by maintainers)
To be exact, 10 per 10 seconds, but effectively the same.
I spent about a dozen hours debugging this yesterday, and I believe my initial comment was incorrect: rather than the bug persisting, I believe I just hadn’t sufficiently cleaned all of the existing corrupt files. There were situations in which there were two trailing bytes at the end of a file (
\n}), and my cleanup script didn’t account for that.I am using NFS, but it does appear to support locking correctly with my current mount options–or, at least, in a way that is compatible with this patch. I’m not a huge fan of NFS and generally don’t trust it, but it should work with this lock/write pattern. I doubt it would ever make sense to officially support NFS given how fickle it is, but the locking code in certmagic is straightforward enough that I should be able to debug and patch it if there are further issues.
The one thing that still has me a little worried is a disconcerting number of requests to the on-demand
askendpoint. The docs make it sound as though that’s to be expected, but it’s accompanied by a large number of log entries related to ARI. I don’t think it’s a bug–it’s probably just a coincidence–but I happened to notice it while troubleshooting.Catching up after the weekend…
Somewhat. From Let’s Encrypt Rate Limits:
Note that it’s per account, per hostname, (and per hour). So, kind of, but if one person’s domain fails to verify, that won’t block others.
Thanks so much for investigating this! And thanks for the follow-up. I’ll see if we can make things clearer.
@bracketforward Good questions.
I think Let’s Encrypt’s rate limits are a good example of this. They have many rate limits, and if you reach them, you could take steps to reduce those failures. On the other hand, they exist to prevent excessive use of resources, so hitting them isn’t “bad” per-se, especially when the domain is truly misconfigured (i.e. trying again won’t help). The only time it’s not good is when it’s a “false positive” like if DNS records have been set but haven’t propagated yet (it’s not really a “false positive” either, I just don’t know a better term).
I’ve seen LE staff reach out to integrators that are having notable difficulty. But it’s always been reasonable IMO.
My browser is crashing so I’ll brb to finish this.
The internal rate limits are pretty generous now, I think it’s 1 per second.
The main thing is the CA sets their rate limits, we just want to avoid slamming CAs with thousands of requests per minute.
@Zenexer
That’s not a bad idea. Maybe DEBUG.
We have specifically had requests – from large integrators – to gate this behind the ask endpoint. It is fast, but it’s expensive. The well-known lookups are a good point, though I haven’t had complaints about that yet (other than, in a way, this thread, I suppose).
If I’m not mistaken, Let’s Encrypt’s rate limiting is much stricter for requests that fail DV–that is, you’re likely to run into rate limiting if you’re requesting a lot of certs and not doing DNS checks on your own.
I’ve since confirmed this: the ask endpoint won’t receive a request.
I think a self-contained example would be helpful here. It should go without saying, but to anyone coming across this discussion: this example does not use best practices; it’s intended to demonstrate a particular issue with minimal code. Do not use this in production.
Caddyfile
docker-compose.yml
In my use case, I’m getting a lot of requests to
/.well-known/acme-challenge/*from various CAs and vulnerability scanners. Neither Caddy nor anything else in my infrastructure was trying to get a certificate, so Caddy doesn’t have any challenge data.Command:
Caddy logs:
For comparison,
curl -sv --connect-to unknown.example:443:127.0.0.1:8443 https://unknown.example/will show a query to theaskendpoint, as willcurl -sv --connect-to known.example:443:127.0.0.1:8443 https://known.example/. The former will end with a TLS alert, while the latter will try to get a cert from LE’s staging endpoint.The most notable log message here is
"error":"no information found to solve challenge for identifier: unknown.example". That’s emitted even without thelogdirective. While trying to debug #303, I didn’t really have any way of determining whether these were the result of concurrency issues, storage issues, or third-parties making bogus requests to/.well-known/acme-challenge/. It turned out to be mostly the latter.As you pointed out,
askisn’t consulted in this scenario–on-demand TLS doesn’t come into play. I wasn’t certain of that at the time.In my case, there were a combination of issues:
askendpoint because certificates weren’t being synchronized. There seemed to be a correlation between theno information found to solve challengeerror messages and theaskendpoint requests. This was probably a coincidence.I knew there were likely multiple issues, which is why debugging was so complicated.
I do think the PR here resolves #303, although it doesn’t fix the already-broken metadata. My only complaint was that the design decisions surrounding
askmade it difficult to gain introspection into Caddy’s logic and determine what was actually happening. In an ideal world without any bugs, the behavior wouldn’t really matter.@aaomidi
I just want to reiterate. This is not okay to do. The problem is that any bad actor can point a wildcard domain to your server and then infinitely make requests like
a.badguy.comthenb.badguy.comto infinity, making your server issue a cert per domain, resulting in a DDOS (rate limits getting hit, or storage exhaustion). That why you must have an allow-list, you cannot trust DNS because it’s not under your control.@Zenexer
Caddy’s internal rate limiting should prevent that from being a concern. It slows itself down if too many attempts are happening.
No, because only a TLS handshake that actually reaches your server (meaning the DNS is already configured correctly) can trigger On-Demand TLS. Any domain which doesn’t point to your server will not renew when the time comes because only a TLS handshake would trigger a renewal attempt.
I’ve never seen any evidence of the usecase you’re describing being a real issue.
To be clear, the only reason I’m bringing up any of this is because it’s made debugging this concurrency issue difficult. If everything worked perfectly as described all the time, it would just be a matter of ignoring the logs–the same as I was already doing.
It’s only a problem because I can’t tell which log entries and ask requests are a result of my Caddy instances trying to get certs (or encountering concurrency issues), versus which log entries are from someone else’s lazy misuse of ACME.
By “wasting PKI resources,” I’m referring to the fact that DV isn’t free, especially at scale. There’s an expectation that integrators should make a reasonable effort to ensure a domain actually points to them (and that an ACME challenge will succeed) prior to trying to get a cert.
I think we’re getting a bit caught up with my specific use case, which isn’t too relevant here. Let’s use the example I provided in a recent comment with two hosting providers.
Correct. I’m not being rate limited and am not proposing that as a concern.
If I follow the instructions in the docs and stop performing DNS checks, then I become Hosting Provider A in my example, and I will get rate limited.
Yes, that was my initial impression. That design strongly disincentivizes ask endpoints from performing sufficient checks. That leads to an ecosystem problem as Caddy becomes more popular.
It invites Caddy users to put themselves in the position of Hosting Provider A above.
Just catching up after feeding the baby and running some kids around… sorry!
I’ve been drafting this reply while several new replies have come in – I wish GitHub would show that someone was replying or at least show the new replies. I feel like the conversation went off-track and got confused by some things, but maybe I did instead. In any case, here’s my attempt to bring it back:
@Zenexer
Thanks for clarifying above. It seems to me you have an extraordinary situation that is not common from what I know of existing large-scale CertMagic deployments. That’s not bad, just something that is worthy of discussion/understanding.
So, in this case, the hosting provider would fail their own ACME challenge, but your server would likely get pinged with a TLS handshake or HTTP request in an attempt to solve the challenge.
The HTTP-01 challenge does not use TLS, so those would not issue a certificate. You’d see junk in your logs, but what’s new (it’s the Internet).
The TLS-ALPN-01 challenge does use TLS, but with a special ALPN value. When it sees a handshake of this sort, it only follows a special code path that serves the challenge solution certificate (if it doesn’t find one it just returns an error and aborts the handshake).
Neither case will initiate an ACME challenge that you end up failing and getting rate limited for. (If they are, that’s a bug I’d like more details, likely in a separate issue.)
@aaomidi
The ask endpoint is only invoked if the client tries to establish a TLS handshake using a domain name it does not have a certificate for (and isn’t itself a challenge handshake). But this endpoint is for the HTTP-01 challenge, which is HTTP-only. Are the servers accessing this plaintext endpoint over HTTPS? That would be the only way this is possible, but is extremely broken / in violation of spec.
@Zenexer’s use case allows people to point their domains to their infrastructure without any registration, etc.
From what I’m understanding, there is no registration or direct user involvement. The only involvement is going to DNS and changing name servers or A records.
@Zenexer What you should do is have your customers register their domain via your app’s settings, and you add it to your DB allow list. Then all the
askendpoint does is compare against that list. That’s how it’s meant to work.It already does that. I am expecting to get certificates for those domains.
Scenario 1: I control
example.com. Large hosting provider used to controlexample.com, and they used to have certificates for it. They keep trying to renew those certificates. My ask endpoint needs to return200, because I need certificates.Scenario 2: I control
example.com. Someone runs Acunetix againstexample.com. For whatever reason, it starts brute forcing random paths with a prefix of/.well-known/acme-challenge/–for example,/.well-known/acme-challenge/xmlrpc.php. I want certificates forexample.com, so the ask endpoint returns200.I do. However, I’m running a free service to which non-technical users can point their domains. They might use my service for a while, decide they don’t like it, and point their domain elsewhere. I can’t trust that the users are going to maintain a list of domains that point to me, so I do have to validate the A/AAAA records before requesting a certificate. The result gets cached in Redis for a while, so subsequent calls to the ask endpoint are very fast.
Yes, mostly for debugging purposes. If I see that multiple Caddy instances are all trying to get certs at the same time, that’s a sign something is amiss. It would likely help when troubleshooting future concurrency issues, but a lightweight plugin could probably serve the same purpose.
I’m still not 100% confident the remaining errors I’m seeing are benign, but I’ll have more data over the next few days.
I’m not seeing any overlap between ARI requests so far. Each one is unique.
If you’re in a hurry, you can make a build yourself with this: