Skip to content

fix: validate empty trusted_bots at config load time#2215

Merged
lpcox merged 7 commits intomainfrom
fix/trusted-bot-empty-array-validation
Mar 20, 2026
Merged

fix: validate empty trusted_bots at config load time#2215
lpcox merged 7 commits intomainfrom
fix/trusted-bot-empty-array-validation

Conversation

@lpcox
Copy link
Collaborator

@lpcox lpcox commented Mar 20, 2026

Summary

Adds validateTrustedBots() to LoadFromFile so that trusted_bots = [] in TOML config is rejected at parse time per spec §4.1.3.4 ("trustedBots MUST be a non-empty array of strings when present").

Previously empty arrays were only caught downstream in buildStrictLabelAgentPayload. This fixes TestLoadFromFile_WithEmptyTrustedBotsToml.

Changes

  • internal/config/config_core.go: Added validateTrustedBots() function and call in LoadFromFile

Verification

make agent-finished passes — all unit + integration tests green, 0 lint issues.

Add validateTrustedBots() to LoadFromFile to reject trusted_bots = []
at TOML parse time per spec §4.1.3.4. Previously, empty arrays were
only caught later in buildStrictLabelAgentPayload.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 20, 2026 05:12
Copy link
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

This PR enforces the MCP Gateway spec requirement that trusted_bots must be a non-empty list when explicitly present in TOML configs, by validating it during LoadFromFile rather than only failing later during label-agent payload construction.

Changes:

  • Add validateTrustedBots() and invoke it from LoadFromFile.
  • Reject trusted_bots = [] (and blank/whitespace entries) at TOML config load time.

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

Comment on lines +286 to +293
// validateTrustedBots checks that the trusted_bots list conforms to spec §4.1.3.4:
// when present, it must be a non-empty array of non-empty strings.
func validateTrustedBots(bots []string) error {
if bots == nil {
return nil
}
if len(bots) == 0 {
return fmt.Errorf("trusted_bots must be a non-empty array when present (spec §4.1.3.4)")
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

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

validateTrustedBots is the only validation helper in config_core.go, while the rest of the config validations live in internal/config/validation.go (and use logValidation / rules.*). Consider moving this helper into the validation module for consistency and easier reuse across config entrypoints.

Copilot uses AI. Check for mistakes.
Comment on lines +267 to +270
// Validate trusted_bots per spec §4.1.3.4: must be non-empty array when present
if err := validateTrustedBots(cfg.Gateway.TrustedBots); err != nil {
return nil, err
}
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

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

This spec validation is only applied on the TOML load path. The stdin JSON load path currently treats trustedBots: [] as “absent” (it’s not propagated) and does not validate blank/whitespace entries, which is inconsistent with spec §4.1.3.4 as enforced here. Consider calling the same validation from the stdin conversion/validation path so all config sources reject empty arrays when the field is present.

Copilot uses AI. Check for mistakes.
- [x] Investigate CI lint failure (job 67859560031)
- [x] Identify root cause: `internal/config/config_test.go` was not
formatted with gofmt
- [x] Fix: run `gofmt -w internal/config/config_test.go` to format the
file
- [x] Verify lint passes locally

<!-- START COPILOT CODING AGENT SUFFIX -->



<!-- START COPILOT ORIGINAL PROMPT -->



<details>

<summary>Original prompt</summary>

> Fix the failing GitHub Actions workflow lint
> Analyze the workflow logs, identify the root cause of the failure, and
implement a fix.
> Job ID: 67859560031
> Job URL:
https://github.com/github/gh-aw-mcpg/actions/runs/23330039619/job/67859560031


</details>



<!-- START COPILOT CODING AGENT TIPS -->
---

✨ Let Copilot coding agent [set things up for
you](https://github.com/github/gh-aw-mcpg/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot)
— coding agent works faster and does higher quality work when set up for
your repo.
@lpcox
Copy link
Collaborator Author

lpcox commented Mar 20, 2026

@copilot open a new pull request to apply changes based on the comments in this thread

Copy link
Contributor

Copilot AI commented Mar 20, 2026

@lpcox I've opened a new pull request, #2217, to work on those changes. Once the pull request is ready, I'll request review from you.

Copilot AI and others added 2 commits March 20, 2026 05:22
…ath (#2217)

`validateTrustedBots` lived in `config_core.go` while all other
validation helpers live in `validation.go`. The stdin JSON path also
silently dropped `trustedBots: []` instead of rejecting it per spec
§4.1.3.4.

## Changes

- **`validation.go`**: Move `validateTrustedBots` here; call it from
`validateGatewayConfig` so the stdin path enforces the same constraint
as TOML
- **`config_core.go`**: Remove `validateTrustedBots` and unused
`strings` import
- **`config_stdin.go`**: Call `validateTrustedBots` in
`convertStdinConfig` when `TrustedBots != nil` (defense-in-depth);
restructured to eliminate redundant length check
- **`config_stdin_test.go`**: Update `"empty trustedBots not
propagated"` sub-test to expect an error
- **`config_test.go`**: Add `TestLoadFromStdin_WithEmptyTrustedBots`
covering the full `LoadFromStdin` path

Both config sources now consistently reject `trustedBots: []`:

```toml
# TOML — already rejected
[gateway]
trusted_bots = []  # → error: trusted_bots must be a non-empty array when present
```

```json
// JSON stdin — now also rejected
{
  "gateway": {
    "trustedBots": []
  }
}
// → error: minimum 1 items required (schema) / trusted_bots must be non-empty (custom validator)
```

<!-- START COPILOT CODING AGENT TIPS -->
---

⌨️ Start Copilot coding agent tasks without leaving your editor —
available in [VS Code](https://gh.io/cca-vs-code-docs), [Visual
Studio](https://gh.io/cca-visual-studio-docs), [JetBrains
IDEs](https://gh.io/cca-jetbrains-docs) and
[Eclipse](https://gh.io/cca-eclipse-docs).
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.

3 participants