console: refuse an import for a wildcard-covered name (#1429)
Fable R3 P3-5, prep for Astra R3 P1 (pending Paul; droppable on its own). The import door answers 409 before reading the body when a wildcard the console knows of covers the host by the edge's rule (exactly one label under the parent): a claim on the parent with Wildcard set, or any name under the apps domain. Static wildcard domains live in the edge's environment and are not seen here. Removal is never refused on these grounds.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+71
−1
internal/console/apidomaincert.go+37 −0internal/console/apidomaincert_test.go+29 −0internal/console/domaincert_copy.go+5 −1
diff --git a/internal/console/apidomaincert.go b/internal/console/apidomaincert.go| index 2b8fd400d..747004c8d 100644 |
| --- a/internal/console/apidomaincert.go |
| +++ b/internal/console/apidomaincert.go |
| @@ -358,6 +358,18 @@ func (s *Server) handleAPIDomainCertImport(w http.ResponseWriter, r *http.Reques |
| return |
| } |
| defer cancel() |
| + // A name a wildcard already covers is served the wildcard, so an import |
| + // for it would never be served (Fable R3 P3-5). Before the body is read. |
| + covered, err := s.certWildcardCovered(ctx, host) |
| + if err != nil { |
| + log.Printf("console: domain cert import %s/%s %s: wildcard parent: %v", acct.Sqid, app, host, err) |
| + jsonError(w, http.StatusBadGateway, certReadFailed) |
| + return |
| + } |
| + if covered { |
| + jsonError(w, http.StatusConflict, certWildcardCovered) |
| + return |
| + } |
| // The body is never logged, and no refusal below repeats any of it. |
| raw, err := io.ReadAll(http.MaxBytesReader(w, r.Body, certImportMaxBody)) |
| if err != nil { |
| @@ -472,3 +484,28 @@ func (s *Server) importedCertSummary(ctx context.Context, c domains.Claim) *cert |
| sum := summarizeCert(c.Host, pair.Leaf) |
| return &sum |
| } |
| + |
| +// certWildcardCovered reports whether a wildcard the console knows of |
| +// covers host, the edge's rule (wildcard.go certFor): host is exactly one |
| +// label under a parent. The parents the console can see are a claim on the |
| +// parent with Wildcard set, and every name under the apps domain (account |
| +// namespaces, whose wildcards the edge derives). Static wildcard domains |
| +// are the edge's own environment and are not seen here. |
| +func (s *Server) certWildcardCovered(ctx context.Context, host string) (bool, error) { |
| + if apps := strings.ToLower(strings.TrimSuffix(s.cfg.AppsDomain, ".")); apps != "" && |
| + (host == apps || strings.HasSuffix(host, "."+apps)) { |
| + return true, nil |
| + } |
| + _, parent, ok := strings.Cut(host, ".") |
| + if !ok || !strings.Contains(parent, ".") { |
| + return false, nil |
| + } |
| + c, err := domains.GetClaim(ctx, s.cfg.Releases, parent) |
| + if errors.Is(err, os.ErrNotExist) { |
| + return false, nil |
| + } |
| + if err != nil { |
| + return false, err |
| + } |
| + return c.Wildcard, nil |
| +} |
diff --git a/internal/console/apidomaincert_test.go b/internal/console/apidomaincert_test.go| index 37c7468b7..45746ebbf 100644 |
| --- a/internal/console/apidomaincert_test.go |
| +++ b/internal/console/apidomaincert_test.go |
| @@ -810,3 +810,32 @@ func TestCertImportClaimWithoutCreatedAtIsNotFoundFirst(t *testing.T) { |
| t.Fatalf("import = %d %s, want 404 %q before validation", code, out, certNoSuchHost) |
| } |
| } |
| + |
| +// TestCertImportRefusesAWildcardCoveredName: a name one label under a |
| +// parent claimed with a wildcard, or under the apps domain, is served the |
| +// wildcard, so an import for it is refused before the body is read. |
| +func TestCertImportRefusesAWildcardCoveredName(t *testing.T) { |
| + d := newCertDoorFixture(t, "code-cert-wildcard-covered") |
| + fx := d.fx |
| + putClaim(t, fx, domains.Claim{Host: "example.com", Account: "bab", App: "titogo", Route: "go.tito.io", Wildcard: true}) |
| + putClaim(t, fx, domains.Claim{Host: "shop.bab.oncarlos.test", Account: "bab", App: "titogo", Route: "go.tito.io"}) |
| + for _, host := range []string{"tickets.example.com", "shop.bab.oncarlos.test"} { |
| + code, _, out := rawReq(t, http.MethodPut, certURL(fx, "bab", "titogo", host), d.paul, |
| + certBody([]byte("not a certificate"), []byte("not a key"), false)) |
| + if code != http.StatusConflict || jsonErrorText(t, out) != certWildcardCovered { |
| + t.Errorf("%s: import = %d %s, want 409 %q", host, code, out, certWildcardCovered) |
| + } |
| + importAbsent(t, fx, host) |
| + } |
| + // Two labels down is not covered by *.example.com. |
| + putClaim(t, fx, domains.Claim{Host: "a.b.example.com", Account: "bab", App: "titogo", Route: "go.tito.io"}) |
| + code, _, out := rawReq(t, http.MethodPut, certURL(fx, "bab", "titogo", "a.b.example.com"), d.paul, |
| + certBody([]byte("not a certificate"), []byte("not a key"), false)) |
| + if code != http.StatusBadRequest { |
| + t.Fatalf("a.b.example.com: import = %d %s, want it to reach validation (400)", code, out) |
| + } |
| + // Removal is never refused on these grounds. |
| + if code, _, out := rawReq(t, http.MethodDelete, certURL(fx, "bab", "titogo", "tickets.example.com"), d.paul, ""); code != http.StatusOK { |
| + t.Fatalf("remove under a wildcard = %d %s, want 200", code, out) |
| + } |
| +} |
diff --git a/internal/console/domaincert_copy.go b/internal/console/domaincert_copy.go| index 648736b45..8399ff981 100644 |
| --- a/internal/console/domaincert_copy.go |
| +++ b/internal/console/domaincert_copy.go |
| @@ -36,7 +36,11 @@ const ( |
| certWrongUsage = "the certificate is not issued for use by web servers" |
| certChainExpired = "a certificate in the chain has expired or is not valid yet" |
| certNotTrusted = "the certificate does not lead to a publicly trusted authority. Include the intermediate certificates after it. Self-signed and private certificates are not accepted" |
| - certWildcardOnly = "the certificate covers this hostname only through a wildcard. Import it again with --allow-wildcard to use it for this hostname" |
| + // certWildcardCovered: the name is under a wildcard the deployment |
| + // serves, which the edge serves ahead of an import. (Not yet reviewed |
| + // by Luna.) |
| + certWildcardCovered = "this hostname is covered by a wildcard certificate already, so an import would not be served" |
| + certWildcardOnly = "the certificate covers this hostname only through a wildcard. Import it again with --allow-wildcard to use it for this hostname" |
| certReadFailed = "could not look up the hostname. Try again" |
| certWriteFailed = "could not save the certificate. Try again" |