Skip to content

Commit

Permalink
add -cors.origin flag to "zed serve" (#4334)
Browse files Browse the repository at this point in the history
The -cors.origin flag specifies a CORS allowed origin. The flag may be
repeated.

This change removes the two baked-in allowed origins,
*.observableusercontent.com and localhost, and replaces them with a
default allowed origin of *.  As a consequence, "zed serve" with no
-cors.origin flag behaves like "zed serve -cors.origin '*'".

Closes #4297.
  • Loading branch information
nwt authored Jan 26, 2023
1 parent 21e80a3 commit 58606a0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
4 changes: 4 additions & 0 deletions cmd/zed/serve/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func New(parent charm.Command, f *flag.FlagSet) (charm.Command, error) {
c.conf.Version = cli.Version
c.logflags.SetFlags(f)
f.IntVar(&c.brimfd, "brimfd", -1, "pipe read fd passed by brim to signal brim closure")
f.Func("cors.origin", "CORS allowed origin (may be repeated)", func(s string) error {
c.conf.CORSAllowedOrigins = append(c.conf.CORSAllowedOrigins, s)
return nil
})
f.StringVar(&c.listenAddr, "l", ":9867", "[addr]:port to listen on")
f.StringVar(&c.portFile, "portfile", "", "write listen port to file")
f.StringVar(&c.rootContentFile, "rootcontentfile", "", "file to serve for GET /")
Expand Down
15 changes: 8 additions & 7 deletions service/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ const indexPage = `
</html>`

type Config struct {
Auth AuthConfig
Root *storage.URI
RootContent io.ReadSeeker
Version string
Logger *zap.Logger
Auth AuthConfig
CORSAllowedOrigins []string
Root *storage.URI
RootContent io.ReadSeeker
Version string
Logger *zap.Logger
}

type Core struct {
Expand Down Expand Up @@ -105,7 +106,7 @@ func NewCore(ctx context.Context, conf Config) (*Core, error) {
}

routerAux := mux.NewRouter()
routerAux.Use(corsMiddleware())
routerAux.Use(corsMiddleware(conf.CORSAllowedOrigins))

routerAux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeContent(w, r, "", time.Time{}, conf.RootContent)
Expand All @@ -131,7 +132,7 @@ func NewCore(ctx context.Context, conf Config) (*Core, error) {
routerAPI.Use(requestIDMiddleware())
routerAPI.Use(accessLogMiddleware(conf.Logger))
routerAPI.Use(panicCatchMiddleware(conf.Logger))
routerAPI.Use(corsMiddleware())
routerAPI.Use(corsMiddleware(conf.CORSAllowedOrigins))

c := &Core{
auth: authenticator,
Expand Down
4 changes: 1 addition & 3 deletions service/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ func requestIDMiddleware() mux.MiddlewareFunc {
}
}

var allowedOrigins = []string{"*.observableusercontent.com", "localhost"}

func corsMiddleware() mux.MiddlewareFunc {
func corsMiddleware(allowedOrigins []string) mux.MiddlewareFunc {
return cors.New(cors.Options{
AllowedOrigins: allowedOrigins,
AllowedMethods: []string{
Expand Down
14 changes: 7 additions & 7 deletions service/ztests/curl-cors.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
script: |
source service.sh
LAKE_EXTRA_FLAGS='-cors.origin=http://a -cors.origin=http://*.b' source service.sh
echo === OPTIONS: allowed ===
curl -sD - \
-X OPTIONS \
-H "Access-Control-Request-Method: POST" \
-H "Access-Control-Request-Headers: content-type, authorization" \
-H "Origin: http://test.observableusercontent.com" \
-H "Origin: http://a" \
$ZED_LAKE/query | grep Access-Control-Allow | tr -d '\015'
echo === OPTIONS: not allowed ===
! curl -sD - \
-X OPTIONS \
-H "Access-Control-Request-Method: POST" \
-H "Access-Control-Request-Headers: content-type, authorization" \
-H "Origin: http://adversarialobservableusercontent.com" \
-H "Origin: http://not-a" \
$ZED_LAKE/query | grep Access-Control-Allow
echo === POST: allowed ===
curl -sD - \
-X POST \
-H "Origin: http://test.observableusercontent.com" \
-H "Origin: http://wildcard.b" \
-H "Accept: application/json" \
-d '{"query":"from :pools | *"}' \
$ZED_LAKE/query | grep Access-Control-Allow | tr -d '\015'
echo === POST: not allowed ===
! curl -sD - \
-X POST \
-H "Origin: http://adversarialobservableusercontent.com" \
-H "Origin: http://wildcard.not-b" \
-H "Accept: application/json" \
-d '{"query":"from :pools | *"}' \
$ZED_LAKE/query | grep Access-Control-Allow
Expand All @@ -39,9 +39,9 @@ outputs:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type, Authorization
Access-Control-Allow-Methods: POST
Access-Control-Allow-Origin: http://test.observableusercontent.com
Access-Control-Allow-Origin: http://a
=== OPTIONS: not allowed ===
=== POST: allowed ===
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://test.observableusercontent.com
Access-Control-Allow-Origin: http://wildcard.b
=== POST: not allowed ===

0 comments on commit 58606a0

Please sign in to comment.