From a71146bb006e53f982e078d48e7fc5aaf65c3471 Mon Sep 17 00:00:00 2001 From: thedaemon-wizard <172107842+thedaemon-wizard@users.noreply.github.com> Date: Thu, 3 Dec 2026 10:21:06 +0900 Subject: [PATCH 1/2] wfe: use RFC 8555 problem types for url and CSR identifier mismatches A JWS whose "url" header parameter is not the request URL is now rejected as unauthorized instead of malformed. RFC 8555 Section 6.4: "If the two do not match, then the server MUST reject the request as unauthorized." A finalize request whose CSR identifiers differ from the order's now gets badCSR instead of unauthorized, as Section 7.4 says the problem document SHOULD use for that case. This covers a different or missing DNS name or IP address, and a different number of either. The checks of the inner JWS of a key rollover are unchanged. Fixes #581 --- wfe/wfe.go | 15 +++-- wfe/wfe_test.go | 170 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+), 5 deletions(-) diff --git a/wfe/wfe.go b/wfe/wfe.go index 2e0ef6eb..ee56bfc9 100644 --- a/wfe/wfe.go +++ b/wfe/wfe.go @@ -959,8 +959,10 @@ func (wfe *WebFrontEndImpl) verifyJWS( Host: request.Host, Path: request.RequestURI, } + // RFC 8555 Section 6.4: "If the two do not match, then the server MUST + // reject the request as unauthorized." if expectedURL.String() != headerURL { - return nil, acme.MalformedProblem(fmt.Sprintf( + return nil, acme.UnauthorizedProblem(fmt.Sprintf( "JWS header parameter 'url' incorrect. Expected %q, got %q", expectedURL.String(), headerURL)) } @@ -2218,14 +2220,17 @@ func (wfe *WebFrontEndImpl) FinalizeOrder( //nolint:gocyclo,gocognit csrDNSs := uniqueLowerNames(parsedCSR.DNSNames) csrIPs := uniqueIPs(parsedCSR.IPAddresses) + // RFC 8555 Section 7.4: when the CSR and order identifiers differ, the + // problem document SHOULD use error code "badCSR". + // Check that the CSR has the same number of names as the initial order contained if len(csrDNSs) != len(orderDNSs) { - wfe.sendError(acme.UnauthorizedProblem( + wfe.sendError(acme.BadCSRProblem( "Order includes different number of DNSnames identifiers than CSR specifies"), response) return } if len(csrIPs) != len(orderIPs) { - wfe.sendError(acme.UnauthorizedProblem( + wfe.sendError(acme.BadCSRProblem( "Order includes different number of IP address identifiers than CSR specifies"), response) return } @@ -2233,14 +2238,14 @@ func (wfe *WebFrontEndImpl) FinalizeOrder( //nolint:gocyclo,gocognit // Check that the CSR's names match the order names exactly for i, name := range orderDNSs { if name != csrDNSs[i] { - wfe.sendError(acme.UnauthorizedProblem( + wfe.sendError(acme.BadCSRProblem( fmt.Sprintf("CSR is missing Order domain %q", name)), response) return } } for i, IP := range orderIPs { if !csrIPs[i].Equal(IP) { - wfe.sendError(acme.UnauthorizedProblem( + wfe.sendError(acme.BadCSRProblem( fmt.Sprintf("CSR is missing Order IP %q", IP)), response) return } diff --git a/wfe/wfe_test.go b/wfe/wfe_test.go index 267d49b7..4cdc9d0e 100644 --- a/wfe/wfe_test.go +++ b/wfe/wfe_test.go @@ -1,17 +1,28 @@ package wfe import ( + "crypto/ecdsa" + "crypto/elliptic" + "crypto/rand" + "crypto/tls" + "crypto/x509" "encoding/base64" "encoding/json" "io" "log" + "net" "net/http" "net/http/httptest" "reflect" "strings" "testing" + "time" + + "github.com/go-jose/go-jose/v4" "github.com/letsencrypt/pebble/v2/acme" + "github.com/letsencrypt/pebble/v2/core" + "github.com/letsencrypt/pebble/v2/db" ) func TestNewAccountRejectsUnsupportedSignatureAlgorithm(t *testing.T) { @@ -48,3 +59,162 @@ func TestNewAccountRejectsUnsupportedSignatureAlgorithm(t *testing.T) { t.Errorf("algorithms = %q, want %q", problem.Algorithms, wantAlgorithms) } } + +// signedPOST returns a POST to path whose body is a JWS over payload signed +// by key, with the given "url" header and a fresh nonce from wfe. With an +// empty kid the JWK is embedded instead. +func signedPOST(t *testing.T, wfe *WebFrontEndImpl, key *ecdsa.PrivateKey, path, headerURL, kid string, payload []byte) *http.Request { + t.Helper() + + signingKey := jose.SigningKey{Algorithm: jose.ES256, Key: key} + opts := &jose.SignerOptions{EmbedJWK: kid == ""} + if kid != "" { + signingKey.Key = jose.JSONWebKey{Key: key, KeyID: kid} + } + opts.WithHeader("nonce", wfe.nonce.createNonce()) + opts.WithHeader("url", headerURL) + signer, err := jose.NewSigner(signingKey, opts) + if err != nil { + t.Fatalf("making signer: %v", err) + } + jws, err := signer.Sign(payload) + if err != nil { + t.Fatalf("signing: %v", err) + } + + req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(jws.FullSerialize())) + req.TLS = &tls.ConnectionState{} + req.Header.Set("Content-Type", expectedJWSContentType) + req.Header.Set("User-Agent", "pebble-test") + return req +} + +// problemOf serves req and returns the status code and problem document. +func problemOf(t *testing.T, wfe *WebFrontEndImpl, req *http.Request) (int, acme.ProblemDetails) { + t.Helper() + + recorder := httptest.NewRecorder() + wfe.Handler().ServeHTTP(recorder, req) + var problem acme.ProblemDetails + if err := json.Unmarshal(recorder.Body.Bytes(), &problem); err != nil { + t.Fatalf("unmarshaling problem response %q: %v", recorder.Body.String(), err) + } + return recorder.Code, problem +} + +func newTestKey(t *testing.T) *ecdsa.PrivateKey { + t.Helper() + + key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + t.Fatalf("generating key: %v", err) + } + return key +} + +func TestURLMismatchIsUnauthorized(t *testing.T) { + t.Parallel() + + wfe := &WebFrontEndImpl{ + log: log.New(io.Discard, "", 0), + nonce: newNonceMap(), + } + req := signedPOST(t, wfe, newTestKey(t), newAccountPath, + "https://example.com/not-the-new-account-url", "", []byte(`{"termsOfServiceAgreed":true}`)) + + code, problem := problemOf(t, wfe, req) + + // RFC 8555 Section 6.4: a "url" header parameter that is not the request + // URL MUST be rejected as unauthorized. + if code != http.StatusForbidden { + t.Errorf("response status = %d, want %d", code, http.StatusForbidden) + } + if problem.Type != "urn:ietf:params:acme:error:unauthorized" { + t.Errorf("problem type = %q, want unauthorized (detail: %q)", problem.Type, problem.Detail) + } +} + +func TestFinalizeWithDifferentIdentifiersIsBadCSR(t *testing.T) { + t.Parallel() + + testCases := []struct { + name string + dnsNames []string + ips []net.IP + }{ + {name: "another domain", dnsNames: []string{"other.example.com"}, ips: []net.IP{net.ParseIP("192.0.2.1")}}, + {name: "one more domain", dnsNames: []string{"example.com", "other.example.com"}, ips: []net.IP{net.ParseIP("192.0.2.1")}}, + {name: "another IP address", dnsNames: []string{"example.com"}, ips: []net.IP{net.ParseIP("192.0.2.2")}}, + {name: "no IP address", dnsNames: []string{"example.com"}}, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + memoryStore := db.NewMemoryStore() + wfe := &WebFrontEndImpl{ + log: log.New(io.Discard, "", 0), + nonce: newNonceMap(), + db: memoryStore, + } + accountKey := newTestKey(t) + account := &core.Account{ + Account: acme.Account{Status: acme.StatusValid}, + Key: &jose.JSONWebKey{Key: accountKey.Public()}, + } + if _, err := memoryStore.AddAccount(account); err != nil { + t.Fatalf("adding account: %v", err) + } + // An order for example.com and 192.0.2.1 whose authorizations are + // valid, so that it is ready to finalize. + identifiers := []acme.Identifier{ + {Type: acme.IdentifierDNS, Value: "example.com"}, + {Type: acme.IdentifierIP, Value: "192.0.2.1"}, + } + var authzs []*core.Authorization + for _, ident := range identifiers { + authzs = append(authzs, &core.Authorization{ + Authorization: acme.Authorization{Status: acme.StatusValid, Identifier: ident}, + ExpiresDate: time.Now().Add(time.Hour), + }) + } + order := &core.Order{ + ID: "order-1", + AccountID: account.ID, + Order: acme.Order{Identifiers: identifiers}, + ExpiresDate: time.Now().Add(time.Hour), + AuthorizationObjects: authzs, + } + if _, err := memoryStore.AddOrder(order); err != nil { + t.Fatalf("adding order: %v", err) + } + + csr, err := x509.CreateCertificateRequest(rand.Reader, &x509.CertificateRequest{ + DNSNames: tc.dnsNames, + IPAddresses: tc.ips, + }, newTestKey(t)) + if err != nil { + t.Fatalf("making CSR: %v", err) + } + payload, err := json.Marshal(map[string]string{"csr": base64.RawURLEncoding.EncodeToString(csr)}) + if err != nil { + t.Fatalf("marshaling finalize request: %v", err) + } + path := orderFinalizePath + order.ID + req := signedPOST(t, wfe, accountKey, path, "https://example.com"+path, + "https://example.com"+acctPath+account.ID, payload) + + code, problem := problemOf(t, wfe, req) + + // RFC 8555 Section 7.4: when the CSR and order identifiers differ, + // the problem document SHOULD use error code "badCSR". + if code != http.StatusBadRequest { + t.Errorf("response status = %d, want %d", code, http.StatusBadRequest) + } + if problem.Type != "urn:ietf:params:acme:error:badCSR" { + t.Errorf("problem type = %q, want badCSR (detail: %q)", problem.Type, problem.Detail) + } + }) + } +} From 87074f5321f808d820887490ba2d3156b005d8ae Mon Sep 17 00:00:00 2001 From: thedaemon-wizard <172107842+thedaemon-wizard@users.noreply.github.com> Date: Thu, 3 Dec 2026 11:32:35 +0900 Subject: [PATCH 2/2] wfe: drop the two RFC comments, as review suggested --- wfe/wfe.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/wfe/wfe.go b/wfe/wfe.go index ee56bfc9..cf35d4bf 100644 --- a/wfe/wfe.go +++ b/wfe/wfe.go @@ -959,8 +959,6 @@ func (wfe *WebFrontEndImpl) verifyJWS( Host: request.Host, Path: request.RequestURI, } - // RFC 8555 Section 6.4: "If the two do not match, then the server MUST - // reject the request as unauthorized." if expectedURL.String() != headerURL { return nil, acme.UnauthorizedProblem(fmt.Sprintf( "JWS header parameter 'url' incorrect. Expected %q, got %q", @@ -2220,9 +2218,6 @@ func (wfe *WebFrontEndImpl) FinalizeOrder( //nolint:gocyclo,gocognit csrDNSs := uniqueLowerNames(parsedCSR.DNSNames) csrIPs := uniqueIPs(parsedCSR.IPAddresses) - // RFC 8555 Section 7.4: when the CSR and order identifiers differ, the - // problem document SHOULD use error code "badCSR". - // Check that the CSR has the same number of names as the initial order contained if len(csrDNSs) != len(orderDNSs) { wfe.sendError(acme.BadCSRProblem(