Skip to content
Open
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 middleware/secure.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
package middleware

import (
"fmt"
"strconv"
"strings"

"github.com/labstack/echo/v4"
)
Expand All @@ -16,6 +17,11 @@ type SecureConfig struct {

// XSSProtection provides protection against cross-site scripting attack (XSS)
// by setting the `X-XSS-Protection` header.
//
// NOTE: The X-XSS-Protection header is deprecated in modern browsers.
// Consider using Content-Security-Policy (CSP) header instead for better XSS protection.
// This setting is primarily for backward compatibility with older browsers.
//
// Optional. Default value "1; mode=block".
XSSProtection string `yaml:"xss_protection"`

Expand Down Expand Up @@ -119,14 +125,14 @@ func SecureWithConfig(config SecureConfig) echo.MiddlewareFunc {
res.Header().Set(echo.HeaderXFrameOptions, config.XFrameOptions)
}
if (c.IsTLS() || (req.Header.Get(echo.HeaderXForwardedProto) == "https")) && config.HSTSMaxAge != 0 {
subdomains := ""
directives := []string{"max-age=" + strconv.Itoa(config.HSTSMaxAge)}
if !config.HSTSExcludeSubdomains {
subdomains = "; includeSubdomains"
directives = append(directives, "includeSubdomains")
}
if config.HSTSPreloadEnabled {
subdomains = fmt.Sprintf("%s; preload", subdomains)
directives = append(directives, "preload")
}
res.Header().Set(echo.HeaderStrictTransportSecurity, fmt.Sprintf("max-age=%d%s", config.HSTSMaxAge, subdomains))
res.Header().Set(echo.HeaderStrictTransportSecurity, strings.Join(directives, "; "))
}
if config.ContentSecurityPolicy != "" {
if config.CSPReportOnly {
Expand Down
Loading