Skip to content

Commit 4db91d6

Browse files
committed
feat: add cors support(default disable)
1 parent 8819c44 commit 4db91d6

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

cmd/server/main.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
openvmv1Connect "github.com/openvm-http/openvm-api/gen/openvm/v1/v1connect"
88
"github.com/openvm-http/openvm-api/internal/interceptor"
99
openvmServer "github.com/openvm-http/openvm-api/internal/service/openvm"
10+
"github.com/rs/cors"
1011
"log"
1112
"net/http"
1213
"os"
@@ -21,6 +22,45 @@ import (
2122
var gitTag string
2223
var dateTime string
2324

25+
func disableCORS() *cors.Cors {
26+
// To let web developers play with the demo service from browsers, we need a
27+
// very permissive CORS setup.
28+
return cors.New(cors.Options{
29+
AllowedMethods: []string{
30+
http.MethodHead,
31+
http.MethodGet,
32+
http.MethodPost,
33+
http.MethodPut,
34+
http.MethodPatch,
35+
http.MethodDelete,
36+
},
37+
AllowOriginFunc: func(_ /* origin */ string) bool {
38+
// Allow all origins, which effectively disables CORS.
39+
return true
40+
},
41+
AllowedHeaders: []string{"*"},
42+
ExposedHeaders: []string{
43+
// Content-Type is in the default safelist.
44+
"Accept",
45+
"Accept-Encoding",
46+
"Accept-Post",
47+
"Connect-Accept-Encoding",
48+
"Connect-Content-Encoding",
49+
"Content-Encoding",
50+
"Grpc-Accept-Encoding",
51+
"Grpc-Encoding",
52+
"Grpc-Message",
53+
"Grpc-Status",
54+
"Grpc-Status-Details-Bin",
55+
},
56+
// Let browsers cache CORS information for longer, which reduces the number
57+
// of preflight requests. Any changes to ExposedHeaders won't take effect
58+
// until the cached data expires. FF caps this value at 24h, and modern
59+
// Chrome caps it at 2h.
60+
MaxAge: int(2 * time.Hour / time.Second),
61+
})
62+
}
63+
2464
func main() {
2565
log.Printf("OpenVM-API %s %s", gitTag, dateTime)
2666
if token := os.Getenv("ACCESS_TOKEN"); token != "" {
@@ -42,10 +82,17 @@ func main() {
4282
))
4383
mux := http.NewServeMux()
4484
mux.Handle("/api/", http.StripPrefix("/api", api))
85+
var httpServerMux http.Handler
86+
if disableCors := os.Getenv("DISABLE_CORS"); disableCors == "YES_I_KNOWN_NOT_SAFE" {
87+
log.Printf("Security Warning: DISABLE_CORS set!\n")
88+
httpServerMux = disableCORS().Handler(mux)
89+
} else {
90+
httpServerMux = mux
91+
}
4592

4693
srv := &http.Server{
4794
Addr: addr,
48-
Handler: h2c.NewHandler(mux, &http2.Server{}),
95+
Handler: h2c.NewHandler(httpServerMux, &http2.Server{}),
4996
}
5097
log.Printf("HTTP server listening on %s\n", addr)
5198
signals := make(chan os.Signal, 1)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
connectrpc.com/connect v1.16.2
77
github.com/openvm-http/govix v1.0.3
88
github.com/openvm-http/govmx/v2 v2.0.1
9+
github.com/rs/cors v1.11.1
910
golang.org/x/net v0.30.0
1011
google.golang.org/protobuf v1.34.2
1112
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ github.com/openvm-http/govix v1.0.3 h1:WC/GroEOEFFc7NIVCZnIX/9xWPfLPaHOcTK/SAjyq
55
github.com/openvm-http/govix v1.0.3/go.mod h1:P22JPIyMAon/IQev5p7d00O9gCq4VV+GW1ES2ccUdcg=
66
github.com/openvm-http/govmx/v2 v2.0.1 h1:Uuglgh7hCoeXB4lJEdcAAD1HAs48NNXnWqKnQmy/pec=
77
github.com/openvm-http/govmx/v2 v2.0.1/go.mod h1:E9iNSSN+upUYUS0FardJPUFc+JGSj+5iHbfg/c4PuLY=
8+
github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
9+
github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
810
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
911
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
1012
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=

0 commit comments

Comments
 (0)