Skip to content

Commit

Permalink
Fix URLs in virtual endpoint, js-plugin and coprocess (#2138)
Browse files Browse the repository at this point in the history
This PR fixes #2112
  • Loading branch information
furkansenharputlu authored and buger committed Mar 9, 2019
1 parent 2d8cd05 commit 0edfb00
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
5 changes: 3 additions & 2 deletions coprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main
import (
"bytes"
"encoding/json"
"net/url"
"unicode/utf8"

"github.com/Sirupsen/logrus"
Expand Down Expand Up @@ -85,7 +86,7 @@ func (c *CoProcessor) ObjectFromRequest(r *http.Request) *coprocess.Object {
Headers: headers,
SetHeaders: map[string]string{},
DeleteHeaders: []string{},
Url: r.URL.Path,
Url: r.URL.String(),
Params: ProtoMap(r.URL.Query()),
AddParams: map[string]string{},
ExtendedParams: ProtoMap(nil),
Expand Down Expand Up @@ -168,7 +169,7 @@ func (c *CoProcessor) ObjectPostProcess(object *coprocess.Object, r *http.Reques
values.Set(p, v)
}

r.URL.Path = object.Request.Url
r.URL, _ = url.ParseRequestURI(object.Request.Url)
r.URL.RawQuery = values.Encode()
}

Expand Down
7 changes: 5 additions & 2 deletions mw_js_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (d *DynamicMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Reques
SetHeaders: map[string]string{},
DeleteHeaders: []string{},
Body: originalBody,
URL: r.URL.Path,
URL: r.URL.String(),
Params: r.URL.Query(),
AddParams: map[string]string{},
ExtendedParams: map[string][]string{},
Expand Down Expand Up @@ -207,7 +207,10 @@ func (d *DynamicMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Reques
r.Body = ioutil.NopCloser(bytes.NewReader(newRequestData.Request.Body))
}

r.URL.Path = newRequestData.Request.URL
r.URL, err = url.ParseRequestURI(newRequestData.Request.URL)
if err != nil {
return nil, http.StatusOK
}

// Delete and set headers
for _, dh := range newRequestData.Request.DeleteHeaders {
Expand Down
29 changes: 29 additions & 0 deletions mw_js_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,35 @@ func TestTykMakeHTTPRequest(t *testing.T) {

ts.Run(t, test.TestCase{Path: "/sample", BodyMatch: "/api/get?param1=dummy", Code: 200})
})

t.Run("Endpoint with skip cleaning", func(t *testing.T) {
ts.Close()
globalConf := config.Global()
globalConf.HttpServerOptions.SkipURLCleaning = true
globalConf.HttpServerOptions.OverrideDefaults = true
config.SetGlobal(globalConf)

prevSkipClean := defaultTestConfig.HttpServerOptions.OverrideDefaults &&
defaultTestConfig.HttpServerOptions.SkipURLCleaning
testServerRouter.SkipClean(true)
defer testServerRouter.SkipClean(prevSkipClean)

ts := newTykTestServer()
defer ts.Close()
defer resetTestConfig()

buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/sample"
spec.ConfigData = map[string]interface{}{
"base_url": ts.URL,
}
spec.CustomMiddlewareBundle = bundle
}, func(spec *APISpec) {
spec.Proxy.ListenPath = "/api"
})

ts.Run(t, test.TestCase{Path: "/sample/99999-XXXX+%2F%2F+dog+9+fff%C3%A9o+party", BodyMatch: "URI\":\"/sample/99999-XXXX+%2F%2F+dog+9+fff%C3%A9o+party", Code: 200})
})
}

func TestJSVMBase64(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion mw_virtual_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (d *VirtualEndpoint) ServeHTTPForCache(w http.ResponseWriter, r *http.Reque
requestData := RequestObject{
Headers: r.Header,
Body: string(originalBody),
URL: r.URL.Path,
URL: r.URL.String(),
Scheme: scheme,
}

Expand Down

0 comments on commit 0edfb00

Please sign in to comment.