nats-io/nats-server · error · errPermTemplateExpansionLimit

%w: %d

Error message

%w: %d

What it means

Same expansion-limit guard as the other errPermTemplateExpansionLimit sites: the running count of expanded subjects would exceed maxPermTemplateSubjectExpansions when multiplying value lists or appending the emitted list, so the error wraps the sentinel with the limit value.

Source

Thrown at server/auth.go:606

			if !hasTags {
				subj := list[i]
				for idx, m := range srcs {
					subj = strings.Replace(subj, m, values[idx][0], -1)
				}
				if IsValidSubject(subj) {
					emittedList = append(emittedList, subj)
				} else if failOnBadSubject {
					return nil, fmt.Errorf("generated invalid subject")
				}
			} else {
				expCount := 1
				for _, v := range values {
					if len(v) == 0 {
						expCount = 0
						break
					}
					if expCount > maxPermTemplateSubjectExpansions/len(v) {
						return nil, fmt.Errorf("%w: %d", errPermTemplateExpansionLimit, maxPermTemplateSubjectExpansions)
					}
					expCount *= len(v)
				}
				if len(emittedList) > maxPermTemplateSubjectExpansions-expCount {
					return nil, fmt.Errorf("%w: %d", errPermTemplateExpansionLimit, maxPermTemplateSubjectExpansions)
				}
				a := nArrayCartesianProduct(values...)
				for _, aa := range a {
					subj := list[i]
					for j := 0; j < len(srcs); j++ {
						subj = strings.Replace(subj, srcs[j], aa[j], -1)
					}
					if IsValidSubject(subj) {
						emittedList = append(emittedList, subj)
					} else if failOnBadSubject {
						return nil, fmt.Errorf("generated invalid subject")
					}
				}

View on GitHub (pinned to 3a66a489d2)

Solutions

  1. Reduce tag combinations used in templated subjects
  2. Use more specific tag prefixes to shrink value lists
  3. Replace wide templates with explicit subject lists
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at server/auth.go:606 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of nats-io/nats-server@3a66a489d2 (2026-09-02). Data as JSON: /api/errors/cf10951266fe77f0. Report an issue: GitHub.