Skip to content

refactor(config): extract shared validateServerAuth to deduplicate auth validation#3764

Merged
lpcox merged 2 commits intomainfrom
fix/auth-validation-dedup-3561
Apr 14, 2026
Merged

refactor(config): extract shared validateServerAuth to deduplicate auth validation#3764
lpcox merged 2 commits intomainfrom
fix/auth-validation-dedup-3561

Conversation

@lpcox
Copy link
Copy Markdown
Collaborator

@lpcox lpcox commented Apr 14, 2026

Fixes #3561

Problem

Auth validation logic was duplicated between the TOML path (LoadFromFile in config_core.go) and the JSON stdin path (validateStandardServerConfig in validation.go). Both independently checked that auth is only on HTTP servers, then called validateAuthConfig.

Solution

Extracted a shared validateServerAuth(auth, serverType, name, jsonPath) helper that:

  1. Returns nil if auth is nil (no-op)
  2. Rejects auth on non-HTTP servers with a consistent error message
  3. Delegates to validateAuthConfig for HTTP servers

Both paths now call this single helper instead of duplicating the logic.

Changes

File Before After
config_core.go 8-line inline loop body 1-line validateServerAuth call
validation.go (stdio) Separate rules.UnsupportedField error Shared helper
validation.go (http) Inline validateAuthConfig call Shared helper

All existing tests pass unchanged.

…th validation

The TOML path (LoadFromFile in config_core.go) and JSON stdin path
(validateStandardServerConfig in validation.go) both performed the
same auth validation: reject auth on non-HTTP servers, then call
validateAuthConfig. Extract a shared validateServerAuth helper that
handles nil-check, type-check, and delegation in one place.

- config_core.go: replaced 8-line inline loop body with 1-line call
- validation.go (stdio block): replaced structured error with shared helper
- validation.go (http block): replaced inline validateAuthConfig with shared helper

Fixes #3561

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 14, 2026 15:10
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

Refactors configuration validation to deduplicate per-server auth validation across the TOML loader (LoadFromFile) and the JSON-stdin validation path, by introducing a shared validateServerAuth(...) helper.

Changes:

  • Added validateServerAuth(auth, serverType, name, jsonPath) helper in internal/config/validation.go.
  • Updated JSON-stdin standard server validation to call the shared helper instead of inline auth checks.
  • Updated TOML LoadFromFile auth validation loop to delegate to the shared helper.
Show a summary per file
File Description
internal/config/validation.go Introduces validateServerAuth and replaces duplicated auth validation logic in validateStandardServerConfig.
internal/config/config_core.go Replaces inline auth validation in LoadFromFile with a call to validateServerAuth.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 2

Comment on lines +244 to +248
// validateServerAuth validates the auth configuration on any server type,
// rejecting auth on non-HTTP servers and delegating to validateAuthConfig
// for HTTP servers. This is shared by both the TOML (LoadFromFile) and
// JSON stdin (validateStandardServerConfig) paths.
func validateServerAuth(auth *AuthConfig, serverType, name, jsonPath string) error {
Comment thread internal/config/validation.go Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@lpcox lpcox merged commit b370a96 into main Apr 14, 2026
16 checks passed
@lpcox lpcox deleted the fix/auth-validation-dedup-3561 branch April 14, 2026 15:56
lpcox added a commit that referenced this pull request Apr 14, 2026
PR #3764 refactored auth validation into validateServerAuth which uses
rules.UnsupportedField, changing the error message format. Four tests
still expected the old "auth is only supported for HTTP servers" wording.

Updated assertions to match the actual structured error messages.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
lpcox added a commit that referenced this pull request Apr 14, 2026
…#3778)

## Problem

PR #3764 refactored auth validation into a shared `validateServerAuth`
helper that uses `rules.UnsupportedField` for structured errors. This
changed the error message format from the old `"auth is only supported
for HTTP servers"` to a structured `"server type \"stdio\""` with a
suggestion.

Four tests were not updated to match the new format:

- `TestLoadFromFile_AuthOnNonHTTPServerRejected` — expected `"HTTP"`
- `TestConvertStdinServerConfig_ValidationError/stdio_with_auth_block` —
expected old message
- `TestConvertStdinServerConfig_StdioWithAuth` — expected old message in
`valErr.Message`
- `TestValidateAuthConfig/auth_on_stdio_server_is_rejected` — expected
old message

## Fix

Updated all 4 assertions to match the actual structured error messages
from `rules.UnsupportedField`.
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.

[duplicate-code] Duplicate Code Pattern: Auth Validation Logic (TOML vs JSON Stdin Paths)

2 participants