2021-01-03 11:28:46 +07:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-12-01 20:14:15 +07:00
|
|
|
"io"
|
2021-01-03 11:28:46 +07:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-02-21 06:14:09 +07:00
|
|
|
"strings"
|
2021-01-03 11:28:46 +07:00
|
|
|
"text/template"
|
|
|
|
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/logging"
|
2022-02-06 20:37:28 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/middlewares"
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2021-01-03 11:28:46 +07: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.
|
2022-04-04 14:46:55 +07:00
|
|
|
func ServeTemplatedFile(publicDir, file, assetPath, duoSelfEnrollment, rememberMe, resetPassword, resetPasswordCustomURL, session, theme string, https bool) middlewares.RequestHandler {
|
2021-01-17 06:23:35 +07:00
|
|
|
logger := logging.Logger()
|
|
|
|
|
2021-12-01 10:32:58 +07:00
|
|
|
a, err := assets.Open(publicDir + file)
|
2021-01-03 11:28:46 +07:00
|
|
|
if err != nil {
|
2021-01-17 06:23:35 +07:00
|
|
|
logger.Fatalf("Unable to open %s: %s", file, err)
|
2021-01-03 11:28:46 +07:00
|
|
|
}
|
|
|
|
|
2021-12-01 20:14:15 +07:00
|
|
|
b, err := io.ReadAll(a)
|
2021-01-03 11:28:46 +07:00
|
|
|
if err != nil {
|
2021-01-17 06:23:35 +07:00
|
|
|
logger.Fatalf("Unable to read %s: %s", file, err)
|
2021-01-03 11:28:46 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
tmpl, err := template.New("file").Parse(string(b))
|
|
|
|
if err != nil {
|
2021-01-17 06:23:35 +07:00
|
|
|
logger.Fatalf("Unable to parse %s template: %s", file, err)
|
2021-01-03 11:28:46 +07:00
|
|
|
}
|
|
|
|
|
2022-02-06 20:37:28 +07:00
|
|
|
return func(ctx *middlewares.AutheliaCtx) {
|
2021-08-10 07:31:08 +07:00
|
|
|
base := ""
|
2022-02-06 20:37:28 +07:00
|
|
|
if baseURL := ctx.UserValueBytes(middlewares.UserValueKeyBaseURL); baseURL != nil {
|
2021-08-10 07:31:08 +07:00
|
|
|
base = baseURL.(string)
|
|
|
|
}
|
|
|
|
|
2021-12-01 10:32:58 +07:00
|
|
|
logoOverride := f
|
2021-11-15 15:37:58 +07:00
|
|
|
|
|
|
|
if assetPath != "" {
|
2022-04-04 09:15:26 +07:00
|
|
|
if _, err := os.Stat(filepath.Join(assetPath, logoFile)); err == nil {
|
2021-12-01 10:32:58 +07:00
|
|
|
logoOverride = t
|
2021-11-15 15:37:58 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 07:58:51 +07:00
|
|
|
var scheme = schemeHTTPS
|
2021-10-10 17:19:47 +07:00
|
|
|
|
|
|
|
if !https {
|
2022-02-06 20:37:28 +07:00
|
|
|
proto := string(ctx.XForwardedProto())
|
2021-10-10 17:19:47 +07:00
|
|
|
switch proto {
|
|
|
|
case "":
|
2022-02-06 20:37:28 +07:00
|
|
|
break
|
2022-04-07 07:58:51 +07:00
|
|
|
case schemeHTTP, schemeHTTPS:
|
2021-10-10 17:19:47 +07:00
|
|
|
scheme = proto
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 20:37:28 +07:00
|
|
|
baseURL := scheme + "://" + string(ctx.XForwardedHost()) + base + "/"
|
2021-11-11 16:13:32 +07:00
|
|
|
nonce := utils.RandomString(32, utils.AlphaNumericCharacters, true)
|
2021-01-03 11:28:46 +07: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-22 06:07:06 +07:00
|
|
|
case publicDir == swaggerAssets:
|
2022-02-21 06:14:09 +07: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))
|
|
|
|
case ctx.Configuration.Server.Headers.CSPTemplate != "":
|
|
|
|
ctx.Response.Header.Add("Content-Security-Policy", strings.ReplaceAll(ctx.Configuration.Server.Headers.CSPTemplate, cspNoncePlaceholder, nonce))
|
2021-01-17 06:23:35 +07:00
|
|
|
case os.Getenv("ENVIRONMENT") == dev:
|
2022-02-21 06:14:09 +07:00
|
|
|
ctx.Response.Header.Add("Content-Security-Policy", fmt.Sprintf(cspDefaultDevTemplate, nonce))
|
2021-01-03 11:28:46 +07:00
|
|
|
default:
|
2022-02-21 06:14:09 +07:00
|
|
|
ctx.Response.Header.Add("Content-Security-Policy", fmt.Sprintf(cspDefaultTemplate, nonce))
|
2021-01-03 11:28:46 +07:00
|
|
|
}
|
|
|
|
|
2022-04-04 14:46:55 +07:00
|
|
|
err := tmpl.Execute(ctx.Response.BodyWriter(), struct{ Base, BaseURL, CSPNonce, DuoSelfEnrollment, LogoOverride, RememberMe, ResetPassword, ResetPasswordCustomURL, Session, Theme string }{Base: base, BaseURL: baseURL, CSPNonce: nonce, DuoSelfEnrollment: duoSelfEnrollment, LogoOverride: logoOverride, RememberMe: rememberMe, ResetPassword: resetPassword, ResetPasswordCustomURL: resetPasswordCustomURL, Session: session, Theme: theme})
|
2021-01-03 11:28:46 +07:00
|
|
|
if err != nil {
|
2022-02-06 20:37:28 +07:00
|
|
|
ctx.RequestCtx.Error("an error occurred", 503)
|
2021-01-17 06:23:35 +07:00
|
|
|
logger.Errorf("Unable to execute template: %v", err)
|
2021-01-03 11:28:46 +07:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-05 11:02:07 +07: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" {
|
2022-04-07 07:58:51 +07:00
|
|
|
host = localhost
|
2022-03-25 07:56:23 +07:00
|
|
|
} else if strings.Contains(host, ":") {
|
|
|
|
host = "[" + host + "]"
|
2021-08-05 11:02:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = file.WriteString(fmt.Sprintf(healthCheckEnv, scheme, host, port, path))
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|