Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
908b5e0
init
MFCaballero Mar 9, 2026
00e175c
init
MFCaballero Mar 9, 2026
5f8807f
Merge branch 'master' of github.com:TykTechnologies/tyk into TT-16767
MFCaballero Mar 9, 2026
f825b1c
fixes
MFCaballero Mar 9, 2026
3d39188
linters
MFCaballero Mar 9, 2026
6ef8286
Merge branch 'master' into TT-16767
MFCaballero Mar 10, 2026
39d1335
update bench
MFCaballero Mar 10, 2026
87ec5c8
Merge branch 'master' into TT-16767
MFCaballero Mar 10, 2026
d97dc15
Merge branch 'master' into TT-16767
edsonmichaque Mar 11, 2026
fa9dfdb
Merge branch 'master' into TT-16767
MFCaballero Mar 11, 2026
5cee70f
simplified comments
MFCaballero Mar 11, 2026
05c6909
Apply suggestion from @andyo-tyk
MFCaballero Mar 12, 2026
df7e005
Apply suggestions from code review
MFCaballero Mar 12, 2026
acf9089
Merge branch 'master' into TT-16767
MFCaballero Mar 12, 2026
d98c0cd
refactor: move error overrides types and methods to apidef to avoid i…
vladzabolotnyi Mar 16, 2026
b2b0220
unify to explicit config naming status_code
MFCaballero Mar 17, 2026
05eb46b
fix format issues
MFCaballero Mar 17, 2026
a1ef045
[TT-16775] Testing Centralised ErrorOverrides Infrastructure (#7878)
MFCaballero Mar 31, 2026
8e3e920
Merge branch 'master' into TT-16767
MFCaballero Mar 31, 2026
6088067
Merge branch 'master' into TT-16767
MFCaballero Apr 1, 2026
9bfdc18
Merge branch 'master' into TT-16767
MFCaballero Apr 2, 2026
bf6cc3d
[TT-16772] Implement Upstream Error Response Overrides (#7896)
MFCaballero Apr 7, 2026
fca75d0
Merge branch 'master' into TT-16767
MFCaballero Apr 7, 2026
6071b1a
Merge branch 'master' into TT-16767
MFCaballero Apr 13, 2026
b259fa8
[TT-16770] Support Template Data & RFC 7807 Context for Validation Er…
buraksezer Apr 13, 2026
36aa606
Merge branch 'master' of github.com:TykTechnologies/tyk into TT-16767
MFCaballero Apr 20, 2026
8842fa5
Merge branch 'TT-16767' of github.com:TykTechnologies/tyk into TT-16767
MFCaballero Apr 20, 2026
9246a6a
Merge branch 'master' into TT-16767
MFCaballero Apr 20, 2026
fbe9f8a
fix sonarqube
MFCaballero Apr 20, 2026
41b4354
Merge branch 'TT-16767' of github.com:TykTechnologies/tyk into TT-16767
MFCaballero Apr 20, 2026
75f0afd
Merge branch 'master' into TT-16767
MFCaballero Apr 20, 2026
0e739a5
fix fmt
MFCaballero Apr 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"github.com/kelseyhightower/envconfig"

"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/internal/errors"
"github.com/TykTechnologies/tyk/internal/otel"
logger "github.com/TykTechnologies/tyk/log"
"github.com/TykTechnologies/tyk/regexp"
Expand Down Expand Up @@ -1339,6 +1340,25 @@
// ```
OverrideMessages map[string]TykError `bson:"override_messages" json:"override_messages"`

// ErrorOverrides allows you to customize the error responses that the Gateway will return to API clients.
// This configuration will be used to override both Gateway-generated errors (e.g. authentication failures, rate limits, validation errors)
// and errors returned by the upstream service (4xx/5xx responses from backend APIs).
// Rules are organized by HTTP status code and can include additional matching criteria.
// These rules will be superseded by any overrides configured in the API definition
//
Comment thread
MFCaballero marked this conversation as resolved.
// Sample Override Setting
// ```
// "error_overrides": {
// "500": [{
// "response": {
// "code": 503,
// "body": "{\"error\": \"Service temporarily unavailable\"}"
// }
// }]
// }
// ```
ErrorOverrides ErrorOverridesMap `json:"error_overrides,omitempty"`

// Cloud flag shows the Gateway runs in Tyk Cloud.
Cloud bool `json:"cloud"`

Expand Down Expand Up @@ -1407,6 +1427,105 @@
Code int `json:"code"`
}

// ErrorMatcher defines additional matching criteria for error overrides.
type ErrorMatcher struct {
// Flag matches against the error classification flag from the request context.
Flag errors.ResponseFlag `json:"flag,omitempty"`

// MessagePattern is a regex pattern to match against the response body.
MessagePattern string `json:"message_pattern,omitempty"`

// BodyField is a JSON path (gjson syntax) to extract a value from the response body.
BodyField string `json:"body_field,omitempty"`

// BodyValue is the expected value at BodyField for the match to succeed.
BodyValue string `json:"body_value,omitempty"`

// CompiledPattern is the pre-compiled regex for MessagePattern.
CompiledPattern *regexp.Regexp `json:"-" ignored:"true"`
}

// Compile compiles the MessagePattern regex if present.
// Should be called after unmarshaling from JSON or YAML.
func (m *ErrorMatcher) Compile() error {
if m.MessagePattern != "" && m.CompiledPattern == nil {
re, err := regexp.Compile(m.MessagePattern)
if err != nil {
return fmt.Errorf("invalid regex pattern %q: %w", m.MessagePattern, err)
}

m.CompiledPattern = re
}

return nil
}

// ErrorResponse defines the override response for error overrides.
type ErrorResponse struct {
// Code is the HTTP status code to return.

Check warning on line 1465 in config/config.go

View check run for this annotation

probelabs / Visor: security

security Issue

The configured error override `Body` is written to the response without a size limit, potentially leading to a denial of service. An administrator could configure an extremely large string in the `Body` field. When the corresponding error is triggered, the gateway will allocate memory for this large body and write it to the response. If triggered frequently, this could lead to memory exhaustion or excessive bandwidth consumption.
Raw output
Implement a validation check to enforce a reasonable size limit on the `ErrorResponse.Body` field when loading the configuration. This could be done in the `CompileErrorOverrides` or a dedicated validation function. A configurable limit with a safe default (e.g., 1MB) would be ideal.
Code int `json:"code"`

// Body is the HTTP response body (literal or inline template).
Body string `json:"body,omitempty"`

// Message is the semantic error message passed to templates as {{.Message}}.
Message string `json:"message,omitempty"`

// Template references an error template file in the templates/ directory.
Template string `json:"template,omitempty"`

// Headers are HTTP headers to include in the response.
Headers map[string]string `json:"headers,omitempty"`
}

// ErrorOverride combines an optional matcher with its response.
type ErrorOverride struct {
// Match contains optional additional matching criteria.
Match *ErrorMatcher `json:"match,omitempty"`

// Response defines the response to return when matched.
Response ErrorResponse `json:"response"`

// compiledBodyTmpl is the pre-compiled text/template for inline Body.
compiledBodyTmpl any `json:"-" ignored:"true"`

// compiledBodyTmplHTML is the pre-compiled html/template for inline Body.
compiledBodyTmplHTML any `json:"-" ignored:"true"`
}

// SetCompiledTemplates stores the pre-compiled templates for inline Body.
func (e *ErrorOverride) SetCompiledTemplates(textTmpl, htmlTmpl any) {
e.compiledBodyTmpl = textTmpl
e.compiledBodyTmplHTML = htmlTmpl
}

// GetCompiledTemplate returns the pre-compiled template for the given content type.
// Returns nil if no inline Body template was compiled (e.g., using file template).
func (e *ErrorOverride) GetCompiledTemplate(isXML bool) interface{} {
if isXML {
return e.compiledBodyTmpl
}

return e.compiledBodyTmplHTML
}

// HasCompiledTemplate returns true if this override has a pre-compiled inline Body template.
func (e *ErrorOverride) HasCompiledTemplate() bool {
return e.compiledBodyTmpl != nil
}

// ErrorOverridesMap maps status codes to their override rules.
type ErrorOverridesMap map[string][]ErrorOverride

// CompiledErrorOverrides provides lookup for error overrides by status code.
type CompiledErrorOverrides struct {
// ByExactCode maps exact status codes to their override rules.
ByExactCode map[int][]*ErrorOverride

// ByPrefix maps status code prefixes to pattern rules.
ByPrefix map[int][]*ErrorOverride
}

// VaultConfig is used to configure the creation of a client
// This is a stripped down version of the config structure in vault's API client
type VaultConfig struct {
Expand Down
Loading
Loading