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

Don't serve requests with incorrect or empty Host header #63

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 11 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/kabukky/journey/server"
"github.com/kabukky/journey/structure/methods"
"github.com/kabukky/journey/templates"
"github.com/justinas/alice"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -117,14 +118,16 @@ func main() {
// Start https server
log.Println("Starting https server on port " + httpsPort + "...")
go func() {
err := http.ListenAndServeTLS(httpsPort, filenames.HttpsCertFilename, filenames.HttpsKeyFilename, httpsRouter)
chain := alice.New(server.CheckHost).Then(httpsRouter)
err := http.ListenAndServeTLS(httpsPort, filenames.HttpsCertFilename, filenames.HttpsKeyFilename, chain)
if err != nil {
log.Fatal("Error: Couldn't start the HTTPS server:", err)
}
}()
// Start http server
log.Println("Starting http server on port " + httpPort + "...")
err := http.ListenAndServe(httpPort, httpRouter)
chain := alice.New(server.CheckHost).Then(httpRouter)
err := http.ListenAndServe(httpPort, chain)
if err != nil {
log.Fatal("Error: Couldn't start the HTTP server:", err)
}
Expand All @@ -143,14 +146,16 @@ func main() {
// Start https server
log.Println("Starting https server on port " + httpsPort + "...")
go func() {
err := http.ListenAndServeTLS(httpsPort, filenames.HttpsCertFilename, filenames.HttpsKeyFilename, httpsRouter)
chain := alice.New(server.CheckHost).Then(httpsRouter)
err := http.ListenAndServeTLS(httpsPort, filenames.HttpsCertFilename, filenames.HttpsKeyFilename, chain)
if err != nil {
log.Fatal("Error: Couldn't start the HTTPS server:", err)
}
}()
// Start http server
log.Println("Starting http server on port " + httpPort + "...")
err := http.ListenAndServe(httpPort, httpRouter)
chain := alice.New(server.CheckHost).Then(httpRouter)
err := http.ListenAndServe(httpPort, chain)
if err != nil {
log.Fatal("Error: Couldn't start the HTTP server:", err)
}
Expand All @@ -164,7 +169,8 @@ func main() {
// Start http server
log.Println("Starting server without HTTPS support. Please enable HTTPS in " + filenames.ConfigFilename + " to improve security.")
log.Println("Starting http server on port " + httpPort + "...")
err := http.ListenAndServe(httpPort, httpRouter)
chain := alice.New(server.CheckHost).Then(httpRouter)
err := http.ListenAndServe(httpPort, chain)
if err != nil {
log.Fatal("Error: Couldn't start the HTTP server:", err)
}
Expand Down
36 changes: 36 additions & 0 deletions server/middlewares.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package server

import (
"net"
"net/http"
"net/url"
"github.com/kabukky/journey/configuration"
"log"
"strings"
)

// Generally is not a good idea to serve all requests on the blog IP even with empty or unknown host header.
// The good practice is to serve requests with correct 'Host' header and return 400 otherwise.
// See rfc2616 for details.

func CheckHost(next http.Handler) http.Handler{

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

parsed, err := url.Parse(configuration.Config.Url)
if err != nil {
log.Fatal("Error: Couldn't parse the Config.Url:", err)
}
host, _, _ := net.SplitHostPort(parsed.Host)

if !strings.EqualFold(r.Host, "") {
if (strings.EqualFold(r.Host, host) || strings.EqualFold(r.Host, parsed.Host)) {
next.ServeHTTP(w, r)
return
}
}

http.Error(w, http.StatusText(400), 400)
return
})
}