Skip to content

Commit 7356833

Browse files
authored
public cors (#1419)
1 parent 331eb7e commit 7356833

4 files changed

Lines changed: 17 additions & 40 deletions

File tree

cuhttp/middleware.go

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,29 @@ package cuhttp
33
import (
44
"context"
55
"net/http"
6-
"os"
76
"time"
87

98
"github.com/CAFxX/httpcompression"
109

1110
"github.com/filecoin-project/curio/deps/config"
1211
)
1312

14-
// AllowedCORSOrigins returns the public API CORS allowlist.
15-
// DEV_CURIO_EXTERNAL_URL overrides the default https://{domainName} origin for local dev.
16-
func AllowedCORSOrigins(domainName string) []string {
17-
if devURL := os.Getenv("DEV_CURIO_EXTERNAL_URL"); devURL != "" {
18-
return []string{devURL}
19-
}
20-
return []string{"https://" + domainName}
21-
}
22-
23-
// CORS allows cross-origin API calls from AllowedCORSOrigins (e.g. market and PDP piece uploads).
13+
// CORS allows any origin on the public HTTP API (market, PDP, retrieval).
14+
// Admin UI CORS remains restricted via HTTP.CORSOrigins on the GUI port.
2415
// Exposes the Location header so clients can read redirect targets after upload.
25-
func CORS(allowedOrigins []string) func(http.Handler) http.Handler {
26-
allowed := make(map[string]struct{}, len(allowedOrigins))
27-
for _, origin := range allowedOrigins {
28-
allowed[origin] = struct{}{}
29-
}
30-
31-
return func(next http.Handler) http.Handler {
32-
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
33-
origin := r.Header.Get("Origin")
34-
if origin != "" {
35-
if _, ok := allowed[origin]; ok {
36-
w.Header().Set("Access-Control-Allow-Origin", origin)
37-
w.Header().Set("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, OPTIONS")
38-
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, Accept, Accept-Encoding")
39-
w.Header().Set("Access-Control-Expose-Headers", "Location")
40-
}
41-
}
16+
func CORS(next http.Handler) http.Handler {
17+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
18+
w.Header().Set("Access-Control-Allow-Origin", "*")
19+
w.Header().Set("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, OPTIONS")
20+
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, Accept, Accept-Encoding")
21+
w.Header().Set("Access-Control-Expose-Headers", "Location")
4222

43-
if r.Method == http.MethodOptions {
44-
w.WriteHeader(http.StatusNoContent)
45-
return
46-
}
47-
next.ServeHTTP(w, r)
48-
})
49-
}
23+
if r.Method == http.MethodOptions {
24+
w.WriteHeader(http.StatusNoContent)
25+
return
26+
}
27+
next.ServeHTTP(w, r)
28+
})
5029
}
5130

5231
// SecureHeaders adds standard browser security headers.

cuhttp/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type ServiceDeps struct {
5858
func StartHTTPServer(ctx context.Context, d *deps.Deps, sd *ServiceDeps) error {
5959
cfg := d.Cfg.HTTP
6060

61-
chiRouter := NewRouter(RouterConfig{CSP: cfg.CSP, DomainName: cfg.DomainName})
61+
chiRouter := NewRouter(RouterConfig{CSP: cfg.CSP})
6262

6363
compressionMw, err := Compression(&cfg.CompressionLevels)
6464
if err != nil {

cuhttp/server_common.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ type startTime string
2626
type RouterConfig struct {
2727
// CSP enables secure response headers when non-empty (market server only).
2828
CSP string
29-
// DomainName is used to derive the default allowed CORS origin (https://{DomainName}).
30-
DomainName string
3129
}
3230

3331
// NewRouter builds a chi router with the standard public-server middleware.
@@ -39,7 +37,7 @@ func NewRouter(cfg RouterConfig) *chi.Mux {
3937
if cfg.CSP != "" {
4038
r.Use(SecureHeaders(cfg.CSP))
4139
}
42-
r.Use(CORS(AllowedCORSOrigins(cfg.DomainName)))
40+
r.Use(CORS)
4341
return r
4442
}
4543

pdpnode/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func StartPublic(ctx context.Context, d *Deps, sd *TaskResult) error {
2222
return nil
2323
}
2424

25-
chiRouter := cuhttp.NewRouter(cuhttp.RouterConfig{CSP: cfg.CSP, DomainName: cfg.DomainName})
25+
chiRouter := cuhttp.NewRouter(cuhttp.RouterConfig{CSP: cfg.CSP})
2626
cuhttp.MountStandardRoutes(chiRouter)
2727

2828
if err := MountPublicRoutes(ctx, chiRouter, d, &sd.ServiceDeps); err != nil {

0 commit comments

Comments
 (0)