An empty required repo identifier built a trailing-empty path segment
(`/api/v1/repos/{owner}/`) and leaked gitea's bare 404. parseArgs now
validates, via reflection, that `repo`/`name` string args are non-empty and
returns a typed ErrValidation naming the field. Optional identifiers (e.g.
code_search's owner-wide fan-out `repo`) opt out with `,omitempty`. `owner` is
already enforced by the allowlist check.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
123 lines
3.6 KiB
Go
123 lines
3.6 KiB
Go
package tools
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"gitea.d-ma.be/mathias/gitea-mcp/internal/gitea"
|
|
"gitea.d-ma.be/mathias/gitea-mcp/internal/registry"
|
|
)
|
|
|
|
// Tool implements registry.Tool.
|
|
type Tool = registry.Tool
|
|
|
|
func textOK(v any) (json.RawMessage, error) {
|
|
return json.Marshal(v)
|
|
}
|
|
|
|
func parseArgs(raw json.RawMessage, dst any) error {
|
|
if len(raw) == 0 {
|
|
if err := json.Unmarshal([]byte("{}"), dst); err != nil {
|
|
return err
|
|
}
|
|
} else if err := json.Unmarshal(normalizeAliases(raw), dst); err != nil {
|
|
return err
|
|
}
|
|
return validateRequiredIdentifiers(dst)
|
|
}
|
|
|
|
// validateRequiredIdentifiers rejects an empty repo identifier at the tool layer
|
|
// before it reaches the client, where it would build a trailing-empty path
|
|
// segment (`/api/v1/repos/{owner}/`) and leak gitea's bare 404 (#37). `repo` and
|
|
// `name` are always required where present — the per-repo identifier tools and
|
|
// the two create tools respectively; `owner` is already enforced by the
|
|
// allowlist check. Returns a typed ErrValidation naming the missing field.
|
|
func validateRequiredIdentifiers(dst any) error {
|
|
v := reflect.ValueOf(dst)
|
|
if v.Kind() != reflect.Ptr || v.IsNil() {
|
|
return nil
|
|
}
|
|
v = v.Elem()
|
|
if v.Kind() != reflect.Struct {
|
|
return nil
|
|
}
|
|
t := v.Type()
|
|
for i := 0; i < t.NumField(); i++ {
|
|
base, opts, _ := strings.Cut(t.Field(i).Tag.Get("json"), ",")
|
|
if base != "repo" && base != "name" {
|
|
continue
|
|
}
|
|
// An optional identifier (e.g. code_search's `repo`, empty => owner-wide
|
|
// fan-out) opts out by carrying `,omitempty`.
|
|
if strings.Contains(opts, "omitempty") {
|
|
continue
|
|
}
|
|
if v.Field(i).Kind() == reflect.String && v.Field(i).String() == "" {
|
|
return fmt.Errorf("%q is required: %w", base, gitea.ErrValidation)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// normalizeAliases reconciles the two spellings of the repo identifier so a tool
|
|
// works whichever the caller sends. `repo` is the canonical, idiomatic name that
|
|
// identifier tools now advertise (#38); `name` is kept as a back-compat alias.
|
|
// The mapping is bidirectional: `name`->`repo` fills the new canonical for old
|
|
// callers, and `repo`->`name` still fills the two create tools (repo_create,
|
|
// create_project_from_template) whose `name` legitimately means "name of the new
|
|
// repo". `index`->`number` is kept for the issue tools whose canonical is
|
|
// `number`. An explicit canonical value always wins over its alias.
|
|
func normalizeAliases(raw json.RawMessage) json.RawMessage {
|
|
var m map[string]json.RawMessage
|
|
if err := json.Unmarshal(raw, &m); err != nil {
|
|
return raw // not a JSON object — leave untouched
|
|
}
|
|
changed := aliasInto(m, "name", "repo")
|
|
changed = aliasInto(m, "repo", "name") || changed
|
|
changed = aliasInto(m, "number", "index") || changed
|
|
if !changed {
|
|
return raw
|
|
}
|
|
if patched, err := json.Marshal(m); err == nil {
|
|
return patched
|
|
}
|
|
return raw
|
|
}
|
|
|
|
// aliasInto copies m[alias] into m[canonical] when canonical is absent or an
|
|
// empty/zero JSON value and alias carries a usable one. An explicit canonical
|
|
// value always wins. Returns true if it wrote canonical.
|
|
func aliasInto(m map[string]json.RawMessage, canonical, alias string) bool {
|
|
av, ok := m[alias]
|
|
if !ok || isEmptyJSON(av) {
|
|
return false
|
|
}
|
|
if cv, ok := m[canonical]; ok && !isEmptyJSON(cv) {
|
|
return false
|
|
}
|
|
m[canonical] = av
|
|
return true
|
|
}
|
|
|
|
func isEmptyJSON(v json.RawMessage) bool {
|
|
switch strings.TrimSpace(string(v)) {
|
|
case "", `""`, "null", "0":
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// capLimit returns a sane page size: 0 or negative → def, > 50 → 50.
|
|
func capLimit(in, def int) int {
|
|
if in <= 0 {
|
|
return def
|
|
}
|
|
if in > 50 {
|
|
return 50
|
|
}
|
|
return in
|
|
}
|