Skip to content

Commit 148222c

Browse files
xegerRaphaël Simon
authored and
Raphaël Simon
committed
Lexically quote routes when generating CORS-support handlers (in case routes contain backslash) (goadesign#1069)
1 parent 98abb7d commit 148222c

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

design/apidsl/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func BasePath(val string) {
161161
// Credentials() // Sets Access-Control-Allow-Credentials header
162162
// })
163163
//
164-
// Origin("/[api|swagger].goa.design/", func() {}) // Define CORS policy with a regular expression
164+
// Origin("/(api|swagger)[.]goa[.]design/", func() {}) // Define CORS policy with a regular expression
165165
func Origin(origin string, dsl func()) {
166166
cors := &design.CORSDefinition{Origin: origin}
167167

goagen/gen_app/writers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ func Mount{{ .Resource }}Controller(service *goa.Service, ctrl {{ .Resource }}Co
699699
initService(service)
700700
var h goa.Handler
701701
{{ $res := .Resource }}{{ if .Origins }}{{ range .PreflightPaths }}{{/*
702-
*/}} service.Mux.Handle("OPTIONS", "{{ . }}", ctrl.MuxHandler("preflight", handle{{ $res }}Origin(cors.HandlePreflight()), nil))
702+
*/}} service.Mux.Handle("OPTIONS", {{ printf "%q" . }}, ctrl.MuxHandler("preflight", handle{{ $res }}Origin(cors.HandlePreflight()), nil))
703703
{{ end }}{{ end }}{{ range .Actions }}{{ $action := . }}
704704
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
705705
// Check if there was an error loading the request

goagen/gen_app/writers_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -946,14 +946,15 @@ var _ = Describe("ControllersWriter", func() {
946946
BeforeEach(func() {
947947
origins = []*design.CORSDefinition{
948948
{
949+
// NB: including backslash to ensure proper escaping
949950
Origin: "here.example.com",
950951
Headers: []string{"X-One", "X-Two"},
951952
Methods: []string{"GET", "POST"},
952953
Exposed: []string{"X-Three"},
953954
Credentials: true,
954955
},
955956
}
956-
preflightPaths = []string{"/public/*filepath"}
957+
preflightPaths = []string{"/public/star\\*star/*filepath"}
957958
})
958959

959960
It("writes the OPTIONS handler code", func() {
@@ -2134,7 +2135,7 @@ type PublicController interface {
21342135
}
21352136
`
21362137

2137-
fileServerOptionsHandler = `service.Mux.Handle("OPTIONS", "/public/*filepath", ctrl.MuxHandler("preflight", handlePublicOrigin(cors.HandlePreflight()), nil))`
2138+
fileServerOptionsHandler = `service.Mux.Handle("OPTIONS", "/public/star\\*star/*filepath", ctrl.MuxHandler("preflight", handlePublicOrigin(cors.HandlePreflight()), nil))`
21382139

21392140
simpleController = `// BottlesController is the controller interface for the Bottles actions.
21402141
type BottlesController interface {

0 commit comments

Comments
 (0)