Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 3 additions & 21 deletions .github/workflows/go-cross.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,19 @@ jobs:

strategy:
matrix:
go-version: [ 1.16, 1.x ]
go-version: [ 1.24, 1.x ]
os: [ubuntu-latest, macos-latest, windows-latest]

steps:
# https://github.com/marketplace/actions/setup-go-environment
- name: Set up Go ${{ matrix.go-version }}
uses: actions/setup-go@v2
uses: actions/setup-go@v6
with:
go-version: ${{ matrix.go-version }}

# https://github.com/marketplace/actions/checkout
- name: Checkout code
uses: actions/checkout@v2

# https://github.com/marketplace/actions/cache
- name: Cache Go modules
uses: actions/cache@v2
with:
# In order:
# * Module download cache
# * Build cache (Linux)
# * Build cache (Mac)
# * Build cache (Windows)
path: |
~/go/pkg/mod
~/.cache/go-build
~/Library/Caches/go-build
%LocalAppData%\go-build
key: ${{ runner.os }}-${{ matrix.go-version }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-${{ matrix.go-version }}-go-
uses: actions/checkout@v6

- name: Test
run: go test -v -cover ./...
15 changes: 3 additions & 12 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
name: Main Process
runs-on: ubuntu-latest
env:
GO_VERSION: 1.15
GO_VERSION: 1.25
GOLANGCI_LINT_VERSION: v1.41.1
YAEGI_VERSION: v0.9.20
CGO_ENABLED: 0
Expand All @@ -26,26 +26,17 @@ jobs:

# https://github.com/marketplace/actions/setup-go-environment
- name: Set up Go ${{ env.GO_VERSION }}
uses: actions/setup-go@v2
uses: actions/setup-go@v6
with:
go-version: ${{ env.GO_VERSION }}

# https://github.com/marketplace/actions/checkout
- name: Check out code
uses: actions/checkout@v2
uses: actions/checkout@v6
with:
path: go/src/github.com/${{ github.repository }}
fetch-depth: 0

# https://github.com/marketplace/actions/cache
- name: Cache Go modules
uses: actions/cache@v2
with:
path: ${{ github.workspace }}/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-

# https://golangci-lint.run/usage/install#other-ci
- name: Install golangci-lint ${{ env.GOLANGCI_LINT_VERSION }}
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin ${GOLANGCI_LINT_VERSION}
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

[![Build Status](https://github.com/traefik/plugin-blockpath/workflows/Main/badge.svg?branch=master)](https://github.com/traefik/plugin-blockpath/actions)

Block Path is a middleware plugin for [Traefik](https://github.com/traefik/traefik) which sends an HTTP `403 Forbidden`
response when the requested HTTP path matches one the configured [regular expressions](https://github.com/google/re2/wiki/Syntax).
Block Path is a middleware plugin for [Traefik](https://github.com/traefik/traefik) which blocks requests
with a configurable status code (default `403 Forbidden`) when the requested HTTP path matches one of the
configured [regular expressions](https://github.com/google/re2/wiki/Syntax).

## Configuration

Expand Down Expand Up @@ -35,6 +36,7 @@ and uses the `blockpath` middleware plugin to block all HTTP requests with a pat
[http.middlewares]
[http.middlewares.block-foo.plugin.blockpath]
regex = ["^/foo(.*)"]
code = 404 # optional, responds with code 404 instead of 403

[http.services]
[http.services.my-service]
Expand Down
10 changes: 9 additions & 1 deletion blockpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
// Config holds the plugin configuration.
type Config struct {
Regex []string `json:"regex,omitempty"`
Code *int `json:"code,omitempty"` // Optional: Status code to respond with, defaults to 403 Forbidden
}

// CreateConfig creates and initializes the plugin configuration.
Expand All @@ -22,6 +23,7 @@ type blockPath struct {
name string
next http.Handler
regexps []*regexp.Regexp
code int
}

// New creates and returns a plugin instance.
Expand All @@ -37,10 +39,16 @@ func New(_ context.Context, next http.Handler, config *Config, name string) (htt
regexps[i] = re
}

code := http.StatusForbidden
if config.Code != nil {
code = *config.Code
}

return &blockPath{
name: name,
next: next,
regexps: regexps,
code: code,
}, nil
}

Expand All @@ -49,7 +57,7 @@ func (b *blockPath) ServeHTTP(rw http.ResponseWriter, req *http.Request) {

for _, re := range b.regexps {
if re.MatchString(currentPath) {
rw.WriteHeader(http.StatusForbidden)
rw.WriteHeader(b.code)
return
}
}
Expand Down
21 changes: 21 additions & 0 deletions blockpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func TestNew(t *testing.T) {
tests := []struct {
desc string
regexps []string
code *int
expErr bool
}{
{
Expand Down Expand Up @@ -40,9 +41,12 @@ func TestNew(t *testing.T) {
}

func TestServeHTTP(t *testing.T) {
customCode := http.StatusNotFound

tests := []struct {
desc string
regexps []string
code *int
reqPath string
expNextCall bool
expStatusCode int
Expand Down Expand Up @@ -88,12 +92,29 @@ func TestServeHTTP(t *testing.T) {
expNextCall: true,
expStatusCode: http.StatusOK,
},
{
desc: "custom status code (unmatched)",
regexps: []string{"^/api/woof"},
code: &customCode,
reqPath: "/foo/bar",
expNextCall: true,
expStatusCode: http.StatusOK,
},
{
desc: "custom status code (matched)",
regexps: []string{"^/api/woof"},
code: &customCode,
reqPath: "/api/woof",
expNextCall: false,
expStatusCode: customCode,
},
}

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
cfg := &Config{
Regex: test.regexps,
Code: test.code,
}

nextCall := false
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/traefik/plugin-blockpath

go 1.15
go 1.24
Loading