Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add -cors.origin flag to "zed serve" #4334

Merged
merged 3 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that I've removed these baked-in values!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, we need to make sure brim uses this new command argument to add these origins.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'll work fine without them.

We will however need to add https://*.observableusercontent.com on cloud deployments to keep the Observable integration working.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok, good point.


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 ===