mirror of
https://github.com/0rangebananaspy/authelia.git
synced 2024-09-14 22:47:21 +07:00
74721a9f41
* feat: go:embed static assets Go 1.16 introduced the ability to embed files within a generated binary directly with the go tool chain. This simplifies our dependencies and the significantly improves the development workflow for future developers. Key points to note: Due to the inability to embed files that do not reside within the local package we need to duplicate our `config.template.yml` within `internal/configuration`. To avoid issues with the development workflow empty mock files have been included within `internal/server/public_html`. These are substituted with the respective generated files during the CI/CD and build workflows. * fix(suites): increase ldap suite test timeout * fix(server): fix swagger asset CSP
67 lines
2.3 KiB
Go
67 lines
2.3 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"text/template"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"github.com/authelia/authelia/internal/logging"
|
|
"github.com/authelia/authelia/internal/utils"
|
|
)
|
|
|
|
var alphaNumericRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
|
|
|
// 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.
|
|
func ServeTemplatedFile(publicDir, file, base, rememberMe, resetPassword, session, theme string) fasthttp.RequestHandler {
|
|
logger := logging.Logger()
|
|
|
|
f, err := assets.Open(publicDir + file)
|
|
if err != nil {
|
|
logger.Fatalf("Unable to open %s: %s", file, err)
|
|
}
|
|
|
|
b, err := ioutil.ReadAll(f)
|
|
if err != nil {
|
|
logger.Fatalf("Unable to read %s: %s", file, err)
|
|
}
|
|
|
|
tmpl, err := template.New("file").Parse(string(b))
|
|
if err != nil {
|
|
logger.Fatalf("Unable to parse %s template: %s", file, err)
|
|
}
|
|
|
|
return func(ctx *fasthttp.RequestCtx) {
|
|
nonce := utils.RandomString(32, alphaNumericRunes)
|
|
|
|
switch extension := filepath.Ext(file); extension {
|
|
case ".html":
|
|
ctx.SetContentType("text/html; charset=utf-8")
|
|
default:
|
|
ctx.SetContentType("text/plain; charset=utf-8")
|
|
}
|
|
|
|
switch {
|
|
case publicDir == swaggerAssets:
|
|
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 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))
|
|
default:
|
|
ctx.Response.Header.Add("Content-Security-Policy", fmt.Sprintf("default-src 'self' ; object-src 'none'; style-src 'self' 'nonce-%s'", nonce))
|
|
}
|
|
|
|
err := tmpl.Execute(ctx.Response.BodyWriter(), struct{ Base, CSPNonce, RememberMe, ResetPassword, Session, Theme string }{Base: base, CSPNonce: nonce, RememberMe: rememberMe, ResetPassword: resetPassword, Session: session, Theme: theme})
|
|
if err != nil {
|
|
ctx.Error("An error occurred", 503)
|
|
logger.Errorf("Unable to execute template: %v", err)
|
|
|
|
return
|
|
}
|
|
}
|
|
}
|