Skip to content

🐛 bug: enforce CertClientFile for AutoCertManager TLS#4312

Open
gaby wants to merge 5 commits into
mainfrom
fix-autocertmanager-mtls-client-ca-bypass
Open

🐛 bug: enforce CertClientFile for AutoCertManager TLS#4312
gaby wants to merge 5 commits into
mainfrom
fix-autocertmanager-mtls-client-ca-bypass

Conversation

@gaby
Copy link
Copy Markdown
Member

@gaby gaby commented May 23, 2026

Motivation

  • A recent change moved CertClientFile handling into the CertFile/CertKeyFile branch only, which caused AutoCertManager + CertClientFile deployments to silently disable mTLS client-certificate verification.
  • Restoring shared application of the client CA ensures intended mTLS enforcement for all internally-created TLS configs without changing per-branch behavior.

Description

  • Introduced applyClientCert(tlsConfig *tls.Config, certClientFile string) error to centralize reading the client CA file and setting ClientAuth/ClientCAs.
  • Call applyClientCert(...) for any internally-created tls.Config (after the TLS branch switch and before TLSConfigFunc) so the AutoCertManager path honors CertClientFile.
  • Added a regression test Test_Listen_AutoCert_WithClientCertFile to verify AutoCertManager combined with CertClientFile is consumed and returns an error when the client CA file is invalid.

@gaby gaby requested a review from a team as a code owner May 23, 2026 14:15
Copilot AI review requested due to automatic review settings May 23, 2026 14:15
@gaby gaby requested review from ReneWerner87, efectn and sixcolors May 23, 2026 14:15
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 23, 2026

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 24b55d7d-19e2-4903-be85-fba4d1f257e8

📥 Commits

Reviewing files that changed from the base of the PR and between 7170b88 and 9995f8c.

📒 Files selected for processing (2)
  • listen.go
  • listen_test.go

Walkthrough

App.Listen centralizes mTLS client-CA handling by calling new applyClientCert after choosing/cloning tls.Config, removes inline CertClientFile logic from the CertFile branch, and adds tests asserting errors for missing/invalid client CA files and that TLS handler is not attached on error.

Changes

mTLS Client-Certificate Refactoring

Layer / File(s) Summary
TLS branching and tlsHandler declaration
listen.go
Declare tlsHandler outside TLS selection; remove inline CertClientFile handling from the CertFile+CertKeyFile branch while preserving GetCertificate wiring.
applyClientCert integration and helper
listen.go
Add applyClientCert(tlsConfig *tls.Config, certClientFile string) error to read/parse client CA PEM and set tlsConfig.ClientAuth/ClientCAs; call it after tls.Config is finalized and attach tlsHandler only when non-nil.
Client-certificate tests
listen_test.go
Add Test_Listen_AutoCert_WithClientCertFile (table-driven) and Test_Listen_ClientCertErrorDoesNotSetTLSHandler (both parallelized) to assert Listen errors for missing/invalid CertClientFile and verify TLS handler is not set on error.

Sequence Diagram(s)

sequenceDiagram
  participant App
  participant tlsConfig
  participant applyClientCert
  participant FileSystem
  App->>tlsConfig: select or clone base tls.Config
  App->>applyClientCert: applyClientCert(tlsConfig, CertClientFile)
  applyClientCert->>FileSystem: read CertClientFile (PEM)
  applyClientCert->>tlsConfig: set ClientCAs and ClientAuth
  App->>App: SetTLSHandler (if tlsHandler non-nil)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • gofiber/fiber#4024: Both PRs modify listen.go's App.Listen TLS setup to adjust how client CA/client-certificate handling is applied within the constructed tls.Config.

Suggested labels

☢️ Bug, v3

Suggested reviewers

  • sixcolors
  • ReneWerner87
  • efectn

Poem

🐰
I hop between TLS and file, so spry,
I parse the PEM beneath the sky,
ClientCAs gathered, auth set tight,
If something breaks, I stop — that's right,
Then nibble logs beneath moonlight.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the bug fix: enforcing CertClientFile for AutoCertManager TLS to prevent silent mTLS client-certificate verification bypass.
Description check ✅ Passed The description provides clear motivation, specific implementation details, and mentions regression testing, but omits required template sections like 'Changes introduced' checklist and 'Type of change' selection.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-autocertmanager-mtls-client-ca-bypass

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.12.2)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gaby gaby removed the aardvark label May 23, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Fixes a regression where CertClientFile was not enforced when using AutoCertManager, potentially disabling mTLS client-certificate verification for that path.

Changes:

  • Centralized client CA loading and TLS client-auth configuration in applyClientCert(...).
  • Applied CertClientFile consistently to any internally-created tls.Config (including AutoCertManager).
  • Added a regression test ensuring AutoCertManager + invalid/missing CertClientFile fails.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
listen.go Moves client CA application into a shared helper and applies it for all internal TLS config branches.
listen_test.go Adds a regression test covering AutoCertManager combined with CertClientFile.

Comment thread listen.go Outdated
Comment thread listen.go Outdated
Comment thread listen_test.go
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the client certificate application logic into a dedicated applyClientCert function, allowing mTLS to be configured alongside both manual TLS settings and AutoCertManager. A new test case was added to verify error handling for missing certificate files. Review feedback suggests ensuring that an error is returned if a client certificate file is provided without any TLS configuration to prevent silent security downgrades. Additionally, it is recommended to verify the success of certificate parsing in applyClientCert and to include filenames in error messages for better observability.

Comment thread listen.go
Comment thread listen.go
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@listen.go`:
- Around line 270-271: The code currently calls
clientCertPool.AppendCertsFromPEM(clientCACert) without checking its boolean
return; update the logic around clientCertPool, AppendCertsFromPEM and the usage
of ClientCAs/ClientAuth (RequireAndVerifyClientCert) to validate the return
value and fail early: call ok := clientCertPool.AppendCertsFromPEM(clientCACert)
and if !ok return or propagate a clear error (or log and exit) indicating the
client CA PEM was invalid or contained no certificates so ClientCAs would be
empty; follow the same validation pattern used in internal/tlstest/tls.go to
surface configuration problems instead of letting TLS handshakes fail silently.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 160d708a-85c0-4f01-91bd-f5dd9ab72a5d

📥 Commits

Reviewing files that changed from the base of the PR and between 32cbb28 and 5b405c4.

📒 Files selected for processing (2)
  • listen.go
  • listen_test.go

Comment thread listen.go Outdated
Copilot finished work on behalf of gaby May 23, 2026 16:00
@ReneWerner87 ReneWerner87 added this to v3 May 24, 2026
@ReneWerner87 ReneWerner87 added this to the v3 milestone May 24, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.39%. Comparing base (000ca39) to head (9995f8c).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4312      +/-   ##
==========================================
+ Coverage   91.33%   91.39%   +0.05%     
==========================================
  Files         132      132              
  Lines       13089    13098       +9     
==========================================
+ Hits        11955    11971      +16     
+ Misses        716      710       -6     
+ Partials      418      417       -1     
Flag Coverage Δ
unittests 91.39% <100.00%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot finished work on behalf of gaby May 24, 2026 15:25
@gaby
Copy link
Copy Markdown
Member Author

gaby commented May 24, 2026

@copilot The new tests added will fail on Windows:

=== FAIL: . Test_Listen_AutoCert_WithClientCertFile/invalid_client_CA_pem (0.00s)
listen_test.go:525:
Error Trace: D:/a/fiber/fiber/listen_test.go:525
Error: Error "failed to parse client CA certificate from "C:\\Users\\RUNNER1\\AppData\\Local\\Temp\\Test_Listen_AutoCert_WithClientCertFile1886750659\\001\\client-ca.pem"" does not contain "C:\Users\RUNNER1\AppData\Local\Temp\Test_Listen_AutoCert_WithClientCertFile1886750659\001\client-ca.pem"
Test: Test_Listen_AutoCert_WithClientCertFile/invalid_client_CA_pem

=== FAIL: . Test_Listen_AutoCert_WithClientCertFile (0.00s)

I think we can just check for client-ca.pem instead if the whole path.

Copy link
Copy Markdown
Contributor

Copilot AI commented May 24, 2026

@copilot The new tests added will fail on Windows:

=== FAIL: . Test_Listen_AutoCert_WithClientCertFile/invalid_client_CA_pem (0.00s)
...

Updated in 7170b88 to assert on filepath.Base(invalidClientCAPath) for the invalid-PEM case, so the test no longer depends on Windows path escaping while still proving the error comes from the client CA file.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread listen_test.go
Comment thread listen.go
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

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

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants