Skip to content

Commit

Permalink
CORS + Cookies flags
Browse files Browse the repository at this point in the history
  • Loading branch information
otherview committed Jul 15, 2024
1 parent e5d76aa commit 7180749
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
19 changes: 17 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func New(
enableReqLogger bool,
enableMetrics bool,
logsLimit uint64,
allowedCredsEnabled bool,
allowedOriginEnabled bool,
) (http.HandlerFunc, func()) {
origins := strings.Split(strings.TrimSpace(allowedOrigins), ",")
for i, o := range origins {
Expand Down Expand Up @@ -102,11 +104,24 @@ func New(
}

handler := handlers.CompressHandler(router)
handler = handlers.CORS(
corsOptions := []handlers.CORSOption{
handlers.AllowedOrigins(origins),
handlers.AllowedHeaders([]string{"content-type", "x-genesis-id"}),
handlers.ExposedHeaders([]string{"x-genesis-id", "x-thorest-ver"}),
)(handler)
}

if allowedCredsEnabled {
corsOptions = append(corsOptions, handlers.AllowCredentials())
}

if allowedOriginEnabled {
corsOptions = append(corsOptions, handlers.AllowedOriginValidator(func(origin string) bool {
// Allow all origins by always returning true
return true
}))
}

handler = handlers.CORS(corsOptions...)(handler)

if enableReqLogger {
handler = RequestLoggerHandler(handler, log)
Expand Down
8 changes: 8 additions & 0 deletions cmd/thor/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ var (
Value: 1000,
Usage: "limit the number of logs returned by /logs API",
}
apiAllowedCredsFlag = cli.BoolFlag{
Name: "api-allowed-creds",
Usage: "enables Access-Control-Allow-Credentials header in API response",
}
apiAllowedOriginsFlag = cli.BoolFlag{
Name: "api-allowed-origins",
Usage: "enables Access-Control-Allow-Origin to be set as the Origin request",
}
enableAPILogsFlag = cli.BoolFlag{
Name: "enable-api-logs",
Usage: "enables API requests logging",
Expand Down
8 changes: 8 additions & 0 deletions cmd/thor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func main() {
apiAllowCustomTracerFlag,
enableAPILogsFlag,
apiLogsLimitFlag,
apiAllowedCredsFlag,
apiAllowedOriginsFlag,
verbosityFlag,
maxPeersFlag,
p2pPortFlag,
Expand Down Expand Up @@ -112,6 +114,8 @@ func main() {
apiAllowCustomTracerFlag,
enableAPILogsFlag,
apiLogsLimitFlag,
apiAllowedCredsFlag,
apiAllowedOriginsFlag,
onDemandFlag,
blockInterval,
persistFlag,
Expand Down Expand Up @@ -242,6 +246,8 @@ func defaultAction(ctx *cli.Context) error {
ctx.Bool(enableAPILogsFlag.Name),
ctx.Bool(enableMetricsFlag.Name),
ctx.Uint64(apiLogsLimitFlag.Name),
ctx.Bool(apiAllowedCredsFlag.Name),
ctx.Bool(apiAllowedOriginsFlag.Name),
)
defer func() { log.Info("closing API..."); apiCloser() }()

Expand Down Expand Up @@ -381,6 +387,8 @@ func soloAction(ctx *cli.Context) error {
ctx.Bool(enableAPILogsFlag.Name),
ctx.Bool(enableMetricsFlag.Name),
ctx.Uint64(apiLogsLimitFlag.Name),
ctx.Bool(apiAllowedCredsFlag.Name),
ctx.Bool(apiAllowedOriginsFlag.Name),
)
defer func() { log.Info("closing API..."); apiCloser() }()

Expand Down

0 comments on commit 7180749

Please sign in to comment.