2021-01-03 04:28:46 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/logging"
|
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2021-01-03 04:28:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ServeTemplatedFile serves a templated version of a specified file,
|
|
|
|
// this is utilised to pass information between the backend and frontend
|
|
|
|
// and generate a nonce to support a restrictive CSP while using material-ui.
|
2021-12-01 03:32:58 +00:00
|
|
|
func ServeTemplatedFile(publicDir, file, assetPath, duoSelfEnrollment, rememberMe, resetPassword, session, theme string, https bool) fasthttp.RequestHandler {
|
2021-01-16 23:23:35 +00:00
|
|
|
logger := logging.Logger()
|
|
|
|
|
2021-12-01 03:32:58 +00:00
|
|
|
a, err := assets.Open(publicDir + file)
|
2021-01-03 04:28:46 +00:00
|
|
|
if err != nil {
|
2021-01-16 23:23:35 +00:00
|
|
|
logger.Fatalf("Unable to open %s: %s", file, err)
|
2021-01-03 04:28:46 +00:00
|
|
|
}
|
|
|
|
|
2021-12-01 03:32:58 +00:00
|
|
|
b, err := ioutil.ReadAll(a)
|
2021-01-03 04:28:46 +00:00
|
|
|
if err != nil {
|
2021-01-16 23:23:35 +00:00
|
|
|
logger.Fatalf("Unable to read %s: %s", file, err)
|
2021-01-03 04:28:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tmpl, err := template.New("file").Parse(string(b))
|
|
|
|
if err != nil {
|
2021-01-16 23:23:35 +00:00
|
|
|
logger.Fatalf("Unable to parse %s template: %s", file, err)
|
2021-01-03 04:28:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return func(ctx *fasthttp.RequestCtx) {
|
2021-08-10 00:31:08 +00:00
|
|
|
base := ""
|
|
|
|
if baseURL := ctx.UserValue("base_url"); baseURL != nil {
|
|
|
|
base = baseURL.(string)
|
|
|
|
}
|
|
|
|
|
2021-12-01 03:32:58 +00:00
|
|
|
logoOverride := f
|
2021-11-15 08:37:58 +00:00
|
|
|
|
|
|
|
if assetPath != "" {
|
|
|
|
if _, err := os.Stat(assetPath + logoFile); err == nil {
|
2021-12-01 03:32:58 +00:00
|
|
|
logoOverride = t
|
2021-11-15 08:37:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 10:19:47 +00:00
|
|
|
var scheme = "https"
|
|
|
|
|
|
|
|
if !https {
|
|
|
|
proto := string(ctx.Request.Header.Peek(fasthttp.HeaderXForwardedProto))
|
|
|
|
switch proto {
|
|
|
|
case "":
|
|
|
|
scheme = "http"
|
|
|
|
default:
|
|
|
|
scheme = proto
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
baseURL := scheme + "://" + string(ctx.Request.Host()) + base + "/"
|
2021-11-11 09:13:32 +00:00
|
|
|
nonce := utils.RandomString(32, utils.AlphaNumericCharacters, true)
|
2021-01-03 04:28:46 +00:00
|
|
|
|
|
|
|
switch extension := filepath.Ext(file); extension {
|
|
|
|
case ".html":
|
|
|
|
ctx.SetContentType("text/html; charset=utf-8")
|
|
|
|
default:
|
|
|
|
ctx.SetContentType("text/plain; charset=utf-8")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2021-02-21 23:07:06 +00:00
|
|
|
case publicDir == swaggerAssets:
|
2021-01-03 04:28:46 +00:00
|
|
|
ctx.Response.Header.Add("Content-Security-Policy", fmt.Sprintf("base-uri 'self' ; default-src 'self' ; img-src 'self' https://validator.swagger.io data: ; object-src 'none' ; script-src 'self' 'unsafe-inline' 'nonce-%s' ; style-src 'self' 'nonce-%s'", nonce, nonce))
|
2021-01-16 23:23:35 +00:00
|
|
|
case os.Getenv("ENVIRONMENT") == dev:
|
|
|
|
ctx.Response.Header.Add("Content-Security-Policy", fmt.Sprintf("default-src 'self' 'unsafe-eval'; object-src 'none'; style-src 'self' 'nonce-%s'", nonce))
|
2021-01-03 04:28:46 +00:00
|
|
|
default:
|
|
|
|
ctx.Response.Header.Add("Content-Security-Policy", fmt.Sprintf("default-src 'self' ; object-src 'none'; style-src 'self' 'nonce-%s'", nonce))
|
|
|
|
}
|
|
|
|
|
2021-12-01 03:32:58 +00:00
|
|
|
err := tmpl.Execute(ctx.Response.BodyWriter(), struct{ Base, BaseURL, CSPNonce, DuoSelfEnrollment, LogoOverride, RememberMe, ResetPassword, Session, Theme string }{Base: base, BaseURL: baseURL, CSPNonce: nonce, DuoSelfEnrollment: duoSelfEnrollment, LogoOverride: logoOverride, RememberMe: rememberMe, ResetPassword: resetPassword, Session: session, Theme: theme})
|
2021-01-03 04:28:46 +00:00
|
|
|
if err != nil {
|
2021-08-03 23:18:20 +00:00
|
|
|
ctx.Error("an error occurred", 503)
|
2021-01-16 23:23:35 +00:00
|
|
|
logger.Errorf("Unable to execute template: %v", err)
|
2021-01-03 04:28:46 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-05 04:02:07 +00:00
|
|
|
|
|
|
|
func writeHealthCheckEnv(disabled bool, scheme, host, path string, port int) (err error) {
|
|
|
|
if disabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Stat("/app/healthcheck.sh")
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Stat("/app/.healthcheck.env")
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.OpenFile("/app/.healthcheck.env", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
_ = file.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
if host == "0.0.0.0" {
|
|
|
|
host = "localhost"
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = file.WriteString(fmt.Sprintf(healthCheckEnv, scheme, host, port, path))
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|