traefik_plugin_filter_json_body

package module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 31, 2026 License: MIT Imports: 8 Imported by: 0

README

traefik-plugin-filter-json-body

A Traefik plugin that filters requests based on JSON body content

Overview

This plugin filters incoming requests based on JSON body content and rejects requests that match specified conditions.

Configuration

Parameters
  • rules: Array of filtering rules (at least one rule is required)
    • path: Request path (exact match, required)
    • method: HTTP method (exact match, required)
    • bodyPath: Path to the target field in JSON body (XPath format, required)
    • bodyValueCondition: Value matching condition (regular expression, required)
Behavior
  • Multiple rules can be specified in the rules array
  • Within each rule, all parameters (path, method, bodyPath, bodyValueCondition) must match for that rule to be considered matched (AND logic)
  • If any one of the rules matches, the request returns 403 Forbidden (OR logic)
  • Requests not matching any rules are passed to the next handler
  • Filtering is skipped in the following cases:
    • Content-Type is not application/json or application/*+json
    • Body size exceeds 10MB
    • Body read error occurs
    • JSON parsing fails
    • Specified bodyPath does not exist in the JSON body

Configuration Examples

Example 1: Reject specific string value
rules:
  - path: /api/test
    method: POST
    bodyPath: key
    bodyValueCondition: ^value$
Example 2: Inspect value in nested object
rules:
  - path: /api/test
    method: POST
    bodyPath: //nestedObject/innerString
    bodyValueCondition: ^inner$
Example 3: Inspect value in object within array
rules:
  - path: /api/test
    method: POST
    bodyPath: //arrayOfObjects/*/objString[text()='obj2']
    bodyValueCondition: ^obj2$

Documentation

Index

Constants

View Source
const MAX_BODY_SIZE = 10 * 1024 * 1024 // 10 MB

Variables

This section is empty.

Functions

func New

func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error)

Types

type Config

type Config struct {
	Rules []Rule `json:"rules,omitempty"`
}

Config the plugin configuration.

func CreateConfig

func CreateConfig() *Config

CreateConfig creates the default plugin configuration.

type FilterJsonBody

type FilterJsonBody struct {
	// contains filtered or unexported fields
}

FilterJsonBody a FilterJsonBody plugin.

func (*FilterJsonBody) ServeHTTP

func (a *FilterJsonBody) ServeHTTP(rw http.ResponseWriter, req *http.Request)

type Rule

type Rule struct {
	Path               string `json:"path,omitempty"`               // only exact match
	Method             string `json:"method,omitempty"`             // only exact match
	BodyPath           string `json:"bodyPath,omitempty"`           // JSON XPath
	BodyValueCondition string `json:"bodyValueCondition,omitempty"` // regex match
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL