Skip to content
This repository has been archived by the owner on Dec 7, 2020. It is now read-only.

Commit

Permalink
Normalize URL
Browse files Browse the repository at this point in the history
- normal the url before we apply the protection middleware against it
  • Loading branch information
gambol99 committed Mar 18, 2017
1 parent a67c80a commit 0fe4e53
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 115 deletions.
3 changes: 3 additions & 0 deletions forwarding.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import (
// reverseProxyMiddleware is responsible for handles reverse proxy request to the upstream endpoint
func (r *oauthProxy) reverseProxyMiddleware() gin.HandlerFunc {
return func(cx *gin.Context) {
// step: continue the flow
cx.Next()
// step: check its cool to continue
if cx.IsAborted() {
return
}
Expand Down
4 changes: 2 additions & 2 deletions handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ func TestAuthorizationURL(t *testing.T) {
ExpectedCode: http.StatusTemporaryRedirect,
},
{
URL: "/admin/../",
ExpectedURL: "/oauth/authorize?state=L2FkbWluLy4uLw==",
URL: "/help/../admin",
ExpectedURL: "/oauth/authorize?state=L2FkbWlu",
ExpectedCode: http.StatusTemporaryRedirect,
},
{
Expand Down
22 changes: 11 additions & 11 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ limitations under the License.
package main

import (
"bytes"
"fmt"
"regexp"
"strings"
"time"

"github.com/PuerkitoBio/purell"
log "github.com/Sirupsen/logrus"
"github.com/coreos/go-oidc/jose"
"github.com/gin-gonic/gin"
Expand All @@ -34,19 +34,19 @@ const (
cxEnforce = "Enforcing"
)

const normalizeFlags purell.NormalizationFlags = purell.FlagRemoveDotSegments | purell.FlagRemoveDuplicateSlashes

// filterMiddleware is custom filtering for incoming requests
func (r *oauthProxy) filterMiddleware() gin.HandlerFunc {
return func(cx *gin.Context) {
var p rune
var b bytes.Buffer
for _, c := range cx.Request.URL.Path {
if c == '/' && p == '/' {
continue
}
p = c
b.WriteRune(c)
}
cx.Request.URL.Path = b.String()
// step: keep a copy of the original
orig := *cx.Request.URL
// step: normalize the url
purell.NormalizeURL(cx.Request.URL, normalizeFlags)
// step: continue the flow
cx.Next()
// step: place back the original
cx.Request.URL = &orig
}
}

Expand Down
Loading

0 comments on commit 0fe4e53

Please sign in to comment.