Skip to content

fix(server): clone TLSConfig in StartTLS instead of mutating caller config - #3250

Open
xgbtxy wants to merge 1 commit into
labstack:masterfrom
xgbtxy:fix/server-clone-tlsconfig-starttls
Open

xgbtxy wants to merge 1 commit into
labstack:masterfrom
xgbtxy:fix/server-clone-tlsconfig-starttls

Conversation

@xgbtxy

@xgbtxy xgbtxy commented Nov 26, 2026 •

Copy link
Copy Markdown

Summary

Clone caller-provided StartConfig.TLSConfig inside StartConfig.StartTLS before setting Certificates, preventing in-place mutation of the caller's *tls.Config and eliminating a data race when multiple StartTLS calls share a base *tls.Config.

Root Cause

StartConfig.StartTLS (server.go) has a value receiver, so callers can reuse a shared base *tls.Config (for example with common MinVersion, CipherSuites, or NextProtos) across multiple servers or listeners. Previously, when sc.TLSConfig != nil, StartTLS wrote:

sc.TLSConfig.Certificates = []tls.Certificate{cer}

directly onto the caller's *tls.Config without calling sc.TLSConfig.Clone(). This:

  1. Mutates the caller's *tls.Config in place (clobbering its Certificates slice).
  2. Triggers a data race under go test -race when two StartTLS calls run concurrently with the same base *tls.Config.

Fix

  • Clone sc.TLSConfig via sc.TLSConfig.Clone() before assigning Certificates in StartConfig.StartTLS (server.go).
  • Add regression tests in server_tls_test.go covering both single-server non-mutation and concurrent StartTLS calls sharing a base *tls.Config under -race.

Verification

  • go test -race -v -count=1 . -run '^TestStartConfig_StartTLS_(DoesNotMutateCallerTLSConfig|SharedTLSConfigConcurrent)$' (fails before the fix with Certificates mutation and a -race data race; passes after the fix).
  • go test -race -count=1 .
  • go vet ./...

Summary by CodeRabbit

  • Bug Fixes
    • Starting a TLS server no longer changes the TLS settings supplied by the caller.
    • Multiple servers can now safely use the same TLS settings without altering the shared configuration.
    • TLS servers continue to start with the existing defaults when no custom settings are provided.

…onfig

StartConfig.StartTLS uses a value receiver, so callers can safely reuse
a base *tls.Config across listeners or servers. Previously, when
sc.TLSConfig was non-nil, StartTLS assigned sc.TLSConfig.Certificates
directly on the caller's pointer without cloning it, which mutated the
caller's *tls.Config and caused a data race under -race when multiple
StartTLS calls shared a base *tls.Config.

Clone sc.TLSConfig via sc.TLSConfig.Clone() before setting Certificates,
and add unit and concurrent -race regression tests in server_tls_test.go.
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: TLS configuration isolation

Layer / File(s) Summary
Clone TLS config and validate server startup
server.go, server_tls_test.go
StartTLS clones a non-nil caller-provided TLS configuration before assigning the loaded certificate. Tests check HTTP 200 responses and verify that the caller’s certificate list remains nil, including when two servers start concurrently with the same configuration.

Change: Bug fix

Merge Risk: Minimal · up to 925c1

StartTLS no longer modifies a TLS configuration supplied by the caller, which removes a data race when several servers share one configuration. The change is small and has regression tests. No merge-blocking risk was identified.

Security architecture risk: Minimal · up to 925c1

StartTLS no longer overwrites certificates in a shared configuration. This removes a source of interference between concurrent server startups without changing how the server selects or opens its listener. The review found no introduced or worsened security risk in this change.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The changed certificate write affects in-process users that share a TLS configuration with StartTLS. One startup can no longer replace the certificate slice on that shared object and thereby affect another consumer.

Trust Boundaries and Controls

  • observed — The ownership boundary is narrowly scoped: StartTLS takes a shallow clone and replaces the clone’s Certificates field. Referenced caller settings are not deep-copied, and a custom listener still controls its own TLS behavior.

Resilience and Maintainability Implications

  • inferred — Invalid certificate input returns before installation. Once installed, the certificate remains local to the startup call even if binding fails, the pre-serve callback rejects startup, or serving ends. No caller-owned certificate state needs restoration before retrying.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c44eb130-31d1-47cf-8095-a46bb916cb8e
Commits

Reviewing files that changed from the base of the PR and between 59aacdd and 925c1ad.

Files selected for processing: 2

  • server.go
  • server_tls_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant