Skip to content

Commit a3cfdba

Browse files
authored
Merge pull request #45 from delta10/fix/42-options-response-filter-proxy
Make configuring CORS headers more robust
2 parents fc1522f + 8ecd65a commit a3cfdba

3 files changed

Lines changed: 38 additions & 29 deletions

File tree

cmd/filter-proxy/main.go

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ func main() {
4545

4646
if path.Passthrough {
4747
router.PathPrefix(path.Path).HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
48-
if r.Method == http.MethodOptions {
49-
w.Header().Add("Methods", "OPTIONS, GET, HEAD")
50-
writeError(w, http.StatusOK, "options response from filter-proxy")
51-
return
52-
}
53-
5448
client := &http.Client{}
5549

5650
//http: Request.RequestURI can't be set in client requests.
@@ -105,12 +99,6 @@ func main() {
10599
return
106100
}
107101

108-
if r.Method == http.MethodOptions {
109-
w.Header().Add("Methods", "OPTIONS, GET, HEAD")
110-
writeError(w, http.StatusOK, "options response from filter-proxy")
111-
return
112-
}
113-
114102
utils.DelHopHeaders(r.Header)
115103

116104
var bodyFilterParams map[string]interface{}
@@ -309,24 +297,42 @@ func main() {
309297
}
310298
}
311299

312-
var httpHandler http.Handler
300+
// By default allow only https://filter-proxy.local
301+
corsOptions := cors.Options{
302+
AllowedOrigins: []string{
303+
"https://filter-proxy.local",
304+
},
305+
Debug: config.Cors.DebugLogging,
306+
OptionsPassthrough: false,
307+
}
308+
313309
if len(config.Cors.AllowedOrigins) > 0 {
314-
c := cors.New(cors.Options{
315-
AllowedOrigins: config.Cors.AllowedOrigins,
316-
AllowedMethods: config.Cors.AllowedMethods,
317-
AllowedHeaders: config.Cors.AllowedHeaders,
318-
AllowCredentials: config.Cors.AllowCredentials,
319-
AllowPrivateNetwork: config.Cors.AllowPrivateNetwork,
320-
})
321-
322-
httpHandler = c.Handler(router)
323-
} else {
324-
httpHandler = router
310+
corsOptions.AllowedOrigins = config.Cors.AllowedOrigins
325311
}
326312

313+
if len(config.Cors.AllowedMethods) > 0 {
314+
corsOptions.AllowedMethods = config.Cors.AllowedMethods
315+
}
316+
317+
if len(config.Cors.AllowedHeaders) > 0 {
318+
corsOptions.AllowedHeaders = config.Cors.AllowedHeaders
319+
}
320+
321+
if config.Cors.AllowCredentials {
322+
corsOptions.AllowCredentials = config.Cors.AllowCredentials
323+
}
324+
325+
if config.Cors.AllowPrivateNetwork {
326+
corsOptions.AllowPrivateNetwork = config.Cors.AllowPrivateNetwork
327+
}
328+
329+
c := cors.New(corsOptions)
330+
331+
handler := c.Handler(router)
332+
327333
s := &http.Server{
328334
Addr: config.ListenAddress,
329-
Handler: requestLoggingMiddleware(httpHandler),
335+
Handler: requestLoggingMiddleware(handler),
330336
ReadTimeout: 10 * time.Second,
331337
WriteTimeout: 10 * time.Second,
332338
MaxHeaderBytes: 1 << 20,

config.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ listenAddress: localhost:8050
77
authorizationServiceUrl: http://localhost:8000/atlas/api/v1/authorize
88

99
cors:
10-
allowedOrigins: []
11-
allowedMethods: []
12-
allowedHeaders: []
13-
allowCredentials: true
10+
# allowedOrigins: ["http://www.test.nl"]
11+
# allowedMethods: ["GET"]
12+
# allowedHeaders: []
13+
# allowCredentials: true
14+
# allowPrivateNetwork: true
15+
# debugLogging: false
1416

1517
paths:
1618
- path: /api/ows

internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type Cors struct {
4242
AllowedHeaders []string `yaml:"allowedHeaders"`
4343
AllowCredentials bool `yaml:"allowCredentials"`
4444
AllowPrivateNetwork bool `yaml:"allowPrivateNetwork"`
45+
DebugLogging bool `yaml:"debugLogging"`
4546
}
4647

4748
type Config struct {

0 commit comments

Comments
 (0)