mirror of
https://github.com/safing/portbase
synced 2025-09-01 10:09:50 +00:00
Use HTTP security headers on all requests
This commit is contained in:
parent
afdb367ada
commit
89fad3d9ca
4 changed files with 38 additions and 20 deletions
|
@ -242,6 +242,14 @@ func authenticateRequest(w http.ResponseWriter, r *http.Request, targetHandler h
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkAuth(w http.ResponseWriter, r *http.Request, authRequired bool) (token *AuthToken, handled bool) {
|
func checkAuth(w http.ResponseWriter, r *http.Request, authRequired bool) (token *AuthToken, handled bool) {
|
||||||
|
// Return highest possible permissions in dev mode.
|
||||||
|
if devMode() {
|
||||||
|
return &AuthToken{
|
||||||
|
Read: PermitSelf,
|
||||||
|
Write: PermitSelf,
|
||||||
|
}, false
|
||||||
|
}
|
||||||
|
|
||||||
// Check for valid API key.
|
// Check for valid API key.
|
||||||
token = checkAPIKey(r)
|
token = checkAPIKey(r)
|
||||||
if token != nil {
|
if token != nil {
|
||||||
|
@ -462,7 +470,12 @@ func deleteSession(sessionKey string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func isReadMethod(method string) bool {
|
func isReadMethod(method string) bool {
|
||||||
return method == http.MethodGet || method == http.MethodHead
|
switch method {
|
||||||
|
case http.MethodGet, http.MethodHead, http.MethodOptions:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseAPIPermission(s string) (Permission, error) {
|
func parseAPIPermission(s string) (Permission, error) {
|
||||||
|
|
|
@ -19,6 +19,8 @@ var (
|
||||||
defaultListenAddress string
|
defaultListenAddress string
|
||||||
|
|
||||||
configuredAPIKeys config.StringArrayOption
|
configuredAPIKeys config.StringArrayOption
|
||||||
|
|
||||||
|
devMode config.BoolOption
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -79,6 +81,8 @@ func registerConfig() error {
|
||||||
}
|
}
|
||||||
configuredAPIKeys = config.GetAsStringArray(CfgAPIKeys, []string{})
|
configuredAPIKeys = config.GetAsStringArray(CfgAPIKeys, []string{})
|
||||||
|
|
||||||
|
devMode = config.Concurrent.GetAsBool(config.CfgDevModeKey, false)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -256,6 +256,9 @@ func (eh *endpointHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
apiRequest.InputData = inputData
|
apiRequest.InputData = inputData
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
// Nothing special to do here.
|
// Nothing special to do here.
|
||||||
|
case http.MethodOptions:
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
return
|
||||||
default:
|
default:
|
||||||
http.Error(w, "Unsupported method for the actions API.", http.StatusMethodNotAllowed)
|
http.Error(w, "Unsupported method for the actions API.", http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
|
@ -308,7 +311,6 @@ func (eh *endpointHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
// Write response.
|
// Write response.
|
||||||
w.Header().Set("Content-Type", apiEndpoint.MimeType+"; charset=utf-8")
|
w.Header().Set("Content-Type", apiEndpoint.MimeType+"; charset=utf-8")
|
||||||
w.Header().Set("Content-Length", strconv.Itoa(len(responseData)))
|
w.Header().Set("Content-Length", strconv.Itoa(len(responseData)))
|
||||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
_, err = w.Write(responseData)
|
_, err = w.Write(responseData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -16,17 +16,6 @@ var (
|
||||||
// gorilla mux
|
// gorilla mux
|
||||||
mainMux = mux.NewRouter()
|
mainMux = mux.NewRouter()
|
||||||
|
|
||||||
// middlewares
|
|
||||||
middlewareHandler = &mwHandler{
|
|
||||||
final: mainMux,
|
|
||||||
handlers: []Middleware{
|
|
||||||
ModuleWorker,
|
|
||||||
LogTracer,
|
|
||||||
RequestLogger,
|
|
||||||
authMiddleware,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// main server and lock
|
// main server and lock
|
||||||
server = &http.Server{}
|
server = &http.Server{}
|
||||||
handlerLock sync.RWMutex
|
handlerLock sync.RWMutex
|
||||||
|
@ -46,18 +35,12 @@ func RegisterHandleFunc(path string, handleFunc func(http.ResponseWriter, *http.
|
||||||
return mainMux.HandleFunc(path, handleFunc)
|
return mainMux.HandleFunc(path, handleFunc)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterMiddleware registers a middle function with the API endoint.
|
|
||||||
func RegisterMiddleware(middleware Middleware) {
|
|
||||||
handlerLock.Lock()
|
|
||||||
defer handlerLock.Unlock()
|
|
||||||
middlewareHandler.handlers = append(middlewareHandler.handlers, middleware)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Serve starts serving the API endpoint.
|
// Serve starts serving the API endpoint.
|
||||||
func Serve() {
|
func Serve() {
|
||||||
// configure server
|
// configure server
|
||||||
server.Addr = listenAddressConfig()
|
server.Addr = listenAddressConfig()
|
||||||
server.Handler = &mainHandler{
|
server.Handler = &mainHandler{
|
||||||
|
// TODO: mainMux should not be modified anymore.
|
||||||
mux: mainMux,
|
mux: mainMux,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,6 +117,22 @@ func (mh *mainHandler) handle(w http.ResponseWriter, r *http.Request) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add security headers.
|
||||||
|
if !devMode() {
|
||||||
|
w.Header().Set(
|
||||||
|
"Content-Security-Policy",
|
||||||
|
"default-src 'self'; "+
|
||||||
|
"style-src 'self' 'unsafe-inline'; "+
|
||||||
|
"img-src 'self' data:",
|
||||||
|
)
|
||||||
|
w.Header().Set("Referrer-Policy", "no-referrer")
|
||||||
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||||
|
w.Header().Set("X-Frame-Options", "deny")
|
||||||
|
w.Header().Set("X-XSS-Protection", "1; mode=block")
|
||||||
|
} else {
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
}
|
||||||
|
|
||||||
// Handle request.
|
// Handle request.
|
||||||
switch {
|
switch {
|
||||||
case handler != nil:
|
case handler != nil:
|
||||||
|
|
Loading…
Add table
Reference in a new issue