Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(http): add flag/config to allow CORS requests, close #229 #293

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

Merged
merged 2 commits into from
Dec 9, 2024
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
6 changes: 6 additions & 0 deletions commands/local_server_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import (

var localWebServerProdWarningMsg = "The local web server is optimized for local development and MUST never be used in a production setup."
var localWebServerTlsKeyLogWarningMsg = "Logging TLS master key is enabled. It means TLS connections between the client and this server will be INSECURE. This is NOT recommended unless you are debugging the connections."
var localWebServerAllowsCORSLogWarningMsg = "Cross-origin resource sharing (CORS) is enabled for all requests.\nYou may want to use https://github.com/nelmio/NelmioCorsBundle to have better control over HTTP headers."

var localServerStartCmd = &console.Command{
Category: "local",
Expand Down Expand Up @@ -83,6 +84,7 @@ var localServerStartCmd = &console.Command{
EnvVars: []string{"SSLKEYLOGFILE"},
},
&console.BoolFlag{Name: "no-workers", Usage: "Do not start workers"},
&console.BoolFlag{Name: "allow-cors", Usage: "Allow Cross-origin resource sharing (CORS) requests"},
},
Action: func(c *console.Context) error {
ui := terminal.SymfonyStyle(terminal.Stdout, terminal.Stdin)
Expand Down Expand Up @@ -188,6 +190,10 @@ var localServerStartCmd = &console.Command{
ui.Warning(localWebServerTlsKeyLogWarningMsg)
}

if config.AllowCORS {
ui.Warning(localWebServerAllowsCORSLogWarningMsg)
}

lw, err := pidFile.LogWriter()
if err != nil {
return err
Expand Down
46 changes: 46 additions & 0 deletions local/http/cors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2021-present Fabien Potencier <[email protected]>
*
* This file is part of Symfony CLI project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package http

import (
"net/http"

"github.com/rs/zerolog"
)

func corsWrapper(h http.Handler, logger zerolog.Logger) http.Handler {
var corsHeaders = []string{"Access-Control-Allow-Origin", "Access-Control-Allow-Methods", "Access-Control-Allow-Headers"}

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, corsHeader := range corsHeaders {
w.Header().Set(corsHeader, "*")
}

h.ServeHTTP(w, r)

for _, corsHeader := range corsHeaders {
if headers, exists := w.Header()[corsHeader]; !exists || len(headers) < 2 {
continue
}

logger.Warn().Msgf(`Multiple entries detected for header "%s". Only one should be set: you should enable CORS handling in the CLI only if the application does not handle them.`, corsHeader)
}
})
}
5 changes: 5 additions & 0 deletions local/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Server struct {
Appversion string
UseGzip bool
TlsKeyLogFile string
AllowCORS bool

httpserver *http.Server
httpsserver *http.Server
Expand Down Expand Up @@ -98,6 +99,10 @@ func (s *Server) Start(errChan chan error) (int, error) {
proxyHandler = gzipWrapper(proxyHandler)
}

if s.AllowCORS {
proxyHandler = corsWrapper(proxyHandler, s.Logger)
}

s.httpserver = &http.Server{
Handler: proxyHandler,
}
Expand Down
4 changes: 4 additions & 0 deletions local/project/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Config struct {
UseGzip bool `yaml:"use_gzip"`
TlsKeyLogFile string `yaml:"tls_key_log_file"`
NoWorkers bool `yaml:"no_workers"`
AllowCORS bool `yaml:"allow_cors"`
}

type FileConfig struct {
Expand Down Expand Up @@ -122,6 +123,9 @@ func NewConfigFromContext(c *console.Context, projectDir string) (*Config, *File
if c.IsSet("no-workers") {
config.NoWorkers = c.Bool("no-workers")
}
if c.IsSet("allow-cors") {
config.AllowCORS = c.Bool("allow-cors")
}

return config, fileConfig, nil
}
Expand Down
1 change: 1 addition & 0 deletions local/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func New(c *Config) (*Project, error) {
UseGzip: c.UseGzip,
Appversion: c.AppVersion,
TlsKeyLogFile: c.TlsKeyLogFile,
AllowCORS: c.AllowCORS,
},
}
if err != nil {
Expand Down
Loading