Skip to content

Commit bd49fe2

Browse files
committed
Changed CORS to use * origin by default
1 parent c34efe0 commit bd49fe2

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

internal/app/app.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package app
55

66
import (
7-
"cmp"
87
"encoding/json"
98
"errors"
109
"fmt"
@@ -512,13 +511,14 @@ func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
512511
return
513512
}
514513

515-
if a.appConfig.CORS.Setting == "strict" || a.appConfig.CORS.Setting == "lax" {
516-
origin := "*"
517-
if a.appConfig.CORS.Setting == "strict" {
514+
if a.appConfig.CORS.AllowOrigin != "" {
515+
origin := a.appConfig.CORS.AllowOrigin
516+
if a.appConfig.CORS.AllowOrigin == "origin" {
518517
origin = getRequestUrl(r)
519518
}
519+
520520
if r.Method == http.MethodOptions {
521-
w.Header().Set("Access-Control-Allow-Origin", cmp.Or(a.appConfig.CORS.AllowOrigin, origin))
521+
w.Header().Set("Access-Control-Allow-Origin", origin)
522522
w.Header().Set("Access-Control-Allow-Methods", a.appConfig.CORS.AllowMethods)
523523
w.Header().Set("Access-Control-Allow-Headers", a.appConfig.CORS.AllowHeaders)
524524
w.Header().Set("Access-Control-Allow-Credentials", a.appConfig.CORS.AllowCredentials)
@@ -528,7 +528,7 @@ func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
528528
w.WriteHeader(http.StatusNoContent)
529529
return
530530
} else {
531-
w.Header().Set("Access-Control-Allow-Origin", cmp.Or(a.appConfig.CORS.AllowOrigin, origin))
531+
w.Header().Set("Access-Control-Allow-Origin", origin)
532532
w.Header().Set("Access-Control-Allow-Methods", a.appConfig.CORS.AllowMethods)
533533
w.Header().Set("Access-Control-Allow-Headers", a.appConfig.CORS.AllowHeaders)
534534
}

internal/system/clace.default.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ db_connection = "sqlite:$CL_HOME/clace_app.db"
6060
# clace app update-metadata conf --promote cors.allow_methods="GET, POST" /myapp
6161

6262
# CORS related Config
63-
# default setting is strict, which means allow_origin is set to host url. "lax" allows all origins, "*".
64-
# "disabled" means no CORS headers are set. if allow_origin is set, it will be used as the origin value.
65-
cors.setting = "strict"
66-
cors.allow_origin = ""
67-
cors.allow_methods = "GET,POST,PUT,DELETE,PATCH,OPTIONS"
63+
# Default setting is to add CORS headers with * as allow_origin header. If cors.allow_origin is set to empty string,
64+
# no CORS headers are set. If allow_origin is set to "origin", the origin host is used as the allow_origin header.
65+
# For any other value for cors.allow_origin, the specified value is used as the allow_origin header.
66+
cors.allow_origin = "*"
67+
cors.allow_methods = "*"
6868
cors.allow_headers = "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-Requested-With"
6969
cors.allow_credentials = "true"
7070
cors.max_age = "2678400" # 31 days

internal/system/config_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,9 @@ func TestServerConfig(t *testing.T) {
5959
testutil.AssertEqualsString(t, "command", "auto", c.System.ContainerCommand)
6060

6161
// App CORS default Settings
62-
testutil.AssertEqualsString(t, "cors setting", "strict", c.AppConfig.CORS.Setting)
63-
testutil.AssertEqualsString(t, "cors origin", "", c.AppConfig.CORS.AllowOrigin)
62+
testutil.AssertEqualsString(t, "cors origin", "*", c.AppConfig.CORS.AllowOrigin)
6463
testutil.AssertEqualsString(t, "cors headers", "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-Requested-With", c.AppConfig.CORS.AllowHeaders)
65-
testutil.AssertEqualsString(t, "cors methods", "GET,POST,PUT,DELETE,PATCH,OPTIONS", c.AppConfig.CORS.AllowMethods)
64+
testutil.AssertEqualsString(t, "cors methods", "*", c.AppConfig.CORS.AllowMethods)
6665
testutil.AssertEqualsString(t, "cors methods", "true", c.AppConfig.CORS.AllowCredentials)
6766
testutil.AssertEqualsString(t, "cors methods", "2678400", c.AppConfig.CORS.MaxAge)
6867
}

tests/commander/test_versions.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,14 @@ tests:
161161
stderr: "error: version commands not supported for dev app"
162162

163163
# Test CORS
164-
versions0300: # default is strict origin
164+
versions0300: # default is origin is *
165165
command: curl -Iu "admin:qwerty" localhost:25222/versions_local1 | grep -i access-control-allow-origin | cut -f2- -d':'
166-
stdout: "http://localhost:25222"
167-
versions0301: # change to lax setting
168-
command: ../clace app update-metadata conf --promote cors.setting=lax /versions_local1
166+
stdout: "*"
167+
versions0301: # change to "origin" setting
168+
command: ../clace app update-metadata conf --promote cors.allow_origin=origin /versions_local1
169169
versions0302:
170170
command: curl -Iu "admin:qwerty" localhost:25222/versions_local1 | grep -i access-control-allow-origin | cut -f2- -d':'
171-
stdout: "*"
171+
stdout: "http://localhost:25222"
172172
versions0303: # custom origin
173173
command: ../clace app update-metadata conf --promote cors.allow_origin=abc /versions_local1
174174
versions0304:

0 commit comments

Comments
 (0)