Skip to content

Commit 92d8503

Browse files
authored
Merge pull request #55 from csfloat/feature/cors
Setup CORS for Extension
2 parents de9d3f3 + 16683bc commit 92d8503

4 files changed

Lines changed: 38 additions & 1 deletion

File tree

config/config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ type Config struct {
2525
}
2626

2727
HTTP struct {
28-
Port string
28+
Port string
29+
AllowedOrigins []string
30+
AllowFirefoxExtensions bool
2931
}
3032

3133
Environment constants.Environment
@@ -74,12 +76,14 @@ func load() Config {
7476
v.SetDefault("Database.PrivateDBName", "private")
7577
v.SetDefault("Database.PublicDBName", "public")
7678
v.SetDefault("HTTP.Port", "80")
79+
v.SetDefault("HTTP.AllowFirefoxExtensions", false)
7780
v.SetDefault("Environment", constants.EnvironmentDevelopment)
7881
v.SetDefault("TrustProxy", false)
7982
v.SetDefault("Ingestors.CSFloat.Enable", false)
8083
v.SetDefault("Ingestors.CSFloat.BaseURL", "https://csfloat.com")
8184

8285
// Need to register environment variables if defaults aren't set
86+
v.BindEnv("HTTP.AllowedOrigins")
8387
v.BindEnv("Ingestors.CSFloat.SecretKey")
8488

8589
// Try to find the root directory, but don't panic if it fails since go.mod doesn't exist in production

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ toolchain go1.24.7
66

77
require (
88
github.com/go-chi/chi/v5 v5.2.4
9+
github.com/go-chi/cors v1.2.2
910
github.com/go-chi/render v1.0.3
1011
github.com/go-viper/mapstructure/v2 v2.4.0
1112
github.com/google/go-cmp v0.7.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S
99
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
1010
github.com/go-chi/chi/v5 v5.2.4 h1:WtFKPHwlywe8Srng8j2BhOD9312j9cGUxG1SP4V2cR4=
1111
github.com/go-chi/chi/v5 v5.2.4/go.mod h1:X7Gx4mteadT3eDOMTsXzmI4/rwUpOwBHLpAfupzFJP0=
12+
github.com/go-chi/cors v1.2.2 h1:Jmey33TE+b+rB7fT8MUy1u0I4L+NARQlK6LhzKPSyQE=
13+
github.com/go-chi/cors v1.2.2/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
1214
github.com/go-chi/render v1.0.3 h1:AsXqd2a1/INaIfUSKq3G5uA8weYx20FOsM7uSoCyyt4=
1315
github.com/go-chi/render v1.0.3/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0=
1416
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=

server/server.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package server
22

33
import (
44
"net/http"
5+
"regexp"
6+
"strings"
57

68
"reverse-watch/api"
79
"reverse-watch/config"
@@ -10,6 +12,7 @@ import (
1012

1113
"github.com/go-chi/chi/v5"
1214
"github.com/go-chi/chi/v5/middleware"
15+
"github.com/go-chi/cors"
1316
)
1417

1518
type Server struct {
@@ -19,6 +22,33 @@ type Server struct {
1922
func New(cfg config.Config, factory repository.Factory) (*Server, error) {
2023
r := chi.NewRouter()
2124

25+
firefoxExtensionOrigin := regexp.MustCompile("^moz-extension://[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
26+
27+
r.Use(cors.Handler(cors.Options{
28+
AllowOriginFunc: func(r *http.Request, origin string) bool {
29+
for _, allowedOrigin := range cfg.HTTP.AllowedOrigins {
30+
if allowedOrigin == origin {
31+
return true
32+
}
33+
}
34+
35+
if cfg.HTTP.AllowFirefoxExtensions {
36+
// Firefox extension IDs are randomly generated for each user.
37+
// Therefore, we're scoping requests made from Firefox extensions to specific endpoints only.
38+
if firefoxExtensionOrigin.MatchString(origin) {
39+
if strings.HasPrefix(r.RequestURI, "/api/v1/users/") {
40+
return true
41+
}
42+
}
43+
}
44+
return false
45+
},
46+
AllowedMethods: []string{"GET", "OPTIONS"},
47+
AllowedHeaders: []string{"Accept", "Content-Type"},
48+
AllowCredentials: true,
49+
MaxAge: 300,
50+
}))
51+
2252
r.Use(middleware.Recoverer)
2353
r.Use(middleware.RequestID)
2454
r.Use(middleware.RealIP)

0 commit comments

Comments
 (0)