-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: frontend middleware to block matching urls (#3963)
* add new frontend config to blacklist URLs * changelog * rename as deny list and panic on wrong regex * match the complete url * better error message
- Loading branch information
1 parent
7dffd0e
commit 3e20eb2
Showing
6 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package pipeline | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/grafana/tempo/modules/frontend/combiner" | ||
) | ||
|
||
type urlDenylistWare struct { | ||
denyList []*regexp.Regexp | ||
next AsyncRoundTripper[combiner.PipelineResponse] | ||
} | ||
|
||
func NewURLDenyListWare(denyList []string) AsyncMiddleware[combiner.PipelineResponse] { | ||
compiledDenylist := make([]*regexp.Regexp, 0) | ||
for _, v := range denyList { | ||
r, err := regexp.Compile(v) | ||
if err == nil { | ||
compiledDenylist = append(compiledDenylist, r) | ||
} else { | ||
panic(fmt.Sprintf("error compiling query frontend deny list regex: %s", err)) | ||
} | ||
} | ||
|
||
return AsyncMiddlewareFunc[combiner.PipelineResponse](func(next AsyncRoundTripper[combiner.PipelineResponse]) AsyncRoundTripper[combiner.PipelineResponse] { | ||
return &urlDenylistWare{ | ||
next: next, | ||
denyList: compiledDenylist, | ||
} | ||
}) | ||
} | ||
|
||
func (c urlDenylistWare) RoundTrip(req Request) (Responses[combiner.PipelineResponse], error) { | ||
if len(c.denyList) != 0 { | ||
err := c.validateRequest(req.HTTPRequest().URL.String()) | ||
if err != nil { | ||
return NewBadRequest(err), nil | ||
} | ||
} | ||
|
||
return c.next.RoundTrip(req) | ||
} | ||
|
||
func (c urlDenylistWare) validateRequest(url string) error { | ||
for _, v := range c.denyList { | ||
if v.MatchString(url) { | ||
return fmt.Errorf("Invalid request %s. This query has been identified as one that destabilizes our system. Contact your system administrator for more information", url) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package pipeline | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/grafana/tempo/modules/frontend/combiner" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var next = AsyncRoundTripperFunc[combiner.PipelineResponse](func(_ Request) (Responses[combiner.PipelineResponse], error) { | ||
return NewHTTPToAsyncResponse(&http.Response{ | ||
StatusCode: 200, | ||
Body: io.NopCloser(bytes.NewReader([]byte{})), | ||
}), nil | ||
}) | ||
|
||
func TestURLBlackListMiddlewareForEmptyBlackList(t *testing.T) { | ||
regexes := []string{} | ||
roundTrip := NewURLDenyListWare(regexes).Wrap(next) | ||
statusCode := DoRequest(t, "http://localhost:8080/api/v2/traces/123345", roundTrip) | ||
assert.Equal(t, 200, statusCode) | ||
} | ||
|
||
func TestURLBlackListMiddlewarePanicsOnSyntacticallyIncorrectRegex(t *testing.T) { | ||
regexes := []string{"qr/^(.*\\.traces\\/[a-f0-9]{32}$/"} | ||
assert.Panics(t, func() { | ||
NewURLDenyListWare(regexes).Wrap(next) | ||
}) | ||
} | ||
|
||
func TestURLBlackListMiddleware(t *testing.T) { | ||
regexes := []string{ | ||
"^.*v2.*", | ||
} | ||
roundTrip := NewURLDenyListWare(regexes).Wrap(next) | ||
statusCode := DoRequest(t, "http://localhost:9000?param1=a¶m2=b", roundTrip) | ||
assert.Equal(t, 200, statusCode) | ||
|
||
// Blacklisted url | ||
statusCode = DoRequest(t, "http://localhost:8080/api/v2/traces/123345", roundTrip) | ||
assert.Equal(t, 400, statusCode) | ||
} | ||
|
||
func DoRequest(t *testing.T, url string, rt AsyncRoundTripper[combiner.PipelineResponse]) int { | ||
req, _ := http.NewRequest(http.MethodGet, url, nil) | ||
resp, _ := rt.RoundTrip(NewHTTPRequest(req)) | ||
httpResponse, _, err := resp.Next(context.Background()) | ||
require.NoError(t, err) | ||
return httpResponse.HTTPResponse().StatusCode | ||
} |