← Back to Pull requests

fix: detect cyclic validation aliases #1675

Open
Harshil-1603 opened this pull request with 57 lines diff across 2 files
View on GitHub
Harshil-1603 Harshil-1603 commented on Oct 21, 2026 14:16 UTC
Author

Fixes Or Enhances

Cyclic aliases currently recurse until Go terminates the process with a stack overflow. Track the aliases on the current expansion path and report a recoverable configuration panic naming the alias and field, consistent with other invalid-tag handling. Reusing an alias in separate branches remains valid; forward registration still works.

Fixes #1673.

  • Tests cover self/indirect cycles, OR alternatives, map keys, Var and Struct, and repeated acyclic aliases.
  • The isolated reproduction terminates with a fatal stack overflow before the fix and recovers afterward. make test (all packages, coverage and race detector) passes on Go 1.27.3. Golangci-lint 2.13.1 passes with Go 1.26.0, matching the lint job; that older linter cannot read Go 1.27 export data. git diff --check passes.

    AI-assisted contribution: OpenAI Codex (GPT-6) implemented and verified this patch.

    Diff: 57 lines (+52 / -5) across 2 files Reviewer: nodivbyzero Review duration: 63524m
    All Vouch review confidence checks passed
    Residual risk 0.04 = Change Risk (0.41) × (1 − Review Confidence (0.89)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.41 · 57 lines diff across 2 files
    0.41
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.91 · 3 substantive comment(s)
    0.91
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    89%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.243
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    4 items
    coveralls coveralls commented on Oct 21, 2026 14:18 UTC
    Contributor

    ![Coverage Status](https://coveralls.io/builds/82505616)

    coverage: 72.575% (+0.006%) from 72.569% — Harshil-1603:fix/cyclic-alias-expansion into go-playground:master

    nodivbyzero nodivbyzero commented on code on Oct 21, 2026 17:55 UTC
    Code Review
    validator_test.go:1
    @@ -795,6 +795,45 @@ func TestAliasTags(t *testing.T) {
     	PanicMatches(t, func() { validate.RegisterAlias("exists!", "gt=5,lt=10") }, "Alias 'exists!' either contains restricted characters or is the same as a restricted tag needed for normal operation")
     }
     
    +func TestCyclicAliases(t *testing.T) {
    +	for _, tc := range []struct {
    +		name    string
    +		aliases map[string]string
    +	}{
    +		{"self", map[string]string{"loop": "loop"}},
    +		{"indirect", map[string]string{"loop": "other", "other": "loop"}},
    +		{"three aliases", map[string]string{"loop": "second", "second": "third", "third": "loop"}},
    +		{"OR alternative", map[string]string{"loop": "required|loop"}},
    +		{"map keys", map[string]string{"loop": "dive,keys,loop,endkeys"}},
    +	} {
    +		t.Run(tc.name, func(t *testing.T) {
    +			v := New()
    +			for alias, tags := range tc.aliases {
    +				v.RegisterAlias(alias, tags)
    +			}
    +			PanicMatches(t, func() { _ = v.Var("value", "loop") }, "Cyclic alias 'loop' on field ''")
    +			PanicMatches(t, func() {
    +				_ = v.Struct(struct {
    +					Value string `validate:"loop"`
    +				}{Value: "value"})
    +			}, "Cyclic alias 'loop' on field 'Value'")
    +			Equal(t, v.Var("value", "required"), nil)
    +		})
    +	}
    +}
    +
    +func TestRepeatedAcyclicAliases(t *testing.T) {
    +	v := New()
    +	v.RegisterAlias("leaf", "min=2")
    +	v.RegisterAlias("left", "leaf")
    +	v.RegisterAlias("right", "leaf")
    +	v.RegisterAlias("both", "left,right")
    +	Equal(t, v.Var("good", "both,both"), nil)
    +	Equal(t, v.Var("good", "leaf|leaf"), nil)
    +	Equal(t, v.Var(map[string]string{"ok": "good"}, "dive,keys,leaf,endkeys,leaf"), nil)
    +	NotEqual(t, v.Var("x", "both"), nil)
    +}
    +

    How about this test?

    func TestCyclicAliasMaskedByCachedTag(t *testing.T) {
    	v := New()
    
    	// Compile and cache the literal tag "base" while it is still acyclic.
    	v.RegisterAlias("base", "required")
    	Equal(t, v.Var("value", "base"), nil)
    
    	// Introduce a cycle: base -> next -> base.
    	v.RegisterAlias("next", "base")
    	v.RegisterAlias("base", "next")
    
    	// This should detect the newly introduced cycle.
    	PanicMatches(t, func() {
    		_ = v.Var("value", "base")
    	}, "Cyclic alias 'base' on field ''")
    }
    

    Harshil-1603 Harshil-1603 commented on code on Oct 21, 2026 21:40 UTC
    Code Review
    validator_test.go:1
    @@ -795,6 +795,45 @@ func TestAliasTags(t *testing.T) {
     	PanicMatches(t, func() { validate.RegisterAlias("exists!", "gt=5,lt=10") }, "Alias 'exists!' either contains restricted characters or is the same as a restricted tag needed for normal operation")
     }
     
    +func TestCyclicAliases(t *testing.T) {
    +	for _, tc := range []struct {
    +		name    string
    +		aliases map[string]string
    +	}{
    +		{"self", map[string]string{"loop": "loop"}},
    +		{"indirect", map[string]string{"loop": "other", "other": "loop"}},
    +		{"three aliases", map[string]string{"loop": "second", "second": "third", "third": "loop"}},
    +		{"OR alternative", map[string]string{"loop": "required|loop"}},
    +		{"map keys", map[string]string{"loop": "dive,keys,loop,endkeys"}},
    +	} {
    +		t.Run(tc.name, func(t *testing.T) {
    +			v := New()
    +			for alias, tags := range tc.aliases {
    +				v.RegisterAlias(alias, tags)
    +			}
    +			PanicMatches(t, func() { _ = v.Var("value", "loop") }, "Cyclic alias 'loop' on field ''")
    +			PanicMatches(t, func() {
    +				_ = v.Struct(struct {
    +					Value string `validate:"loop"`
    +				}{Value: "value"})
    +			}, "Cyclic alias 'loop' on field 'Value'")
    +			Equal(t, v.Var("value", "required"), nil)
    +		})
    +	}
    +}
    +
    +func TestRepeatedAcyclicAliases(t *testing.T) {
    +	v := New()
    +	v.RegisterAlias("leaf", "min=2")
    +	v.RegisterAlias("left", "leaf")
    +	v.RegisterAlias("right", "leaf")
    +	v.RegisterAlias("both", "left,right")
    +	Equal(t, v.Var("good", "both,both"), nil)
    +	Equal(t, v.Var("good", "leaf|leaf"), nil)
    +	Equal(t, v.Var(map[string]string{"ok": "good"}, "dive,keys,leaf,endkeys,leaf"), nil)
    +	NotEqual(t, v.Var("x", "both"), nil)
    +}
    +

    [AI] I reproduced this test: Var keeps using the cached required expansion, so no panic occurs. The same behavior exists on current master; the cache implementation is unchanged by this PR. Re-registering an acyclic alias after validation also retains its earlier cached expansion.

    RegisterAlias currently documents that aliases should be registered before any validation. Changing this behavior would require deciding how alias re-registration interacts with both tag and struct caches, beyond detecting a cycle while compiling tags.

    I suggest keeping this PR scoped to cycle detection during compilation and handling post-validation alias re-registration separately. Would you prefer that broader cache behavior included here? As a control, I checked that a cycle registered before any validation is still detected after an unrelated tag has been cached.

    Reviewers
    Assignees
    Harshil-1603 Harshil-1603
    Labels
    risk: low confidence: 89.4% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 40.8% (MEDIUM) · Higher = larger blast radius
    40.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 89.4% · Lower = rushed or shallow review
    89.4%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 4.3% · Safe to merge
    4.3%