Skip to content

Commit 758e1ad

Browse files
kayukinKonstantin KayukinMHSanaei
authored
Make HSTS policy configurable if https is enabled (#4462)
* Make HSTS policy configurable if https is enabled * refactor(web): gate HSTS at call site so XUI_SKIP_HSTS doesn't drop the Secure cookie flag isDirectHTTPSConfigured was being reused for both the HSTS middleware and the session cookie's Secure flag (web.go:185). Embedding the env-var check inside it meant setting XUI_SKIP_HSTS=true also stripped Secure from session cookies on a real HTTPS server. Split the concerns: keep isDirectHTTPSConfigured honest (cert/key only) and combine it with the env var at the call site for the HSTS middleware only. --------- Co-authored-by: Konstantin Kayukin <t_kkayukin@admarketplace.com> Co-authored-by: Sanaei <ho3ein.sanaei@gmail.com>
1 parent 121b6e0 commit 758e1ad

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ func IsDebug() bool {
5757
return os.Getenv("XUI_DEBUG") == "true"
5858
}
5959

60+
// IsSkipHSTS returns true if skipping HSTS mode is enabled via the XUI_SKIP_HSTS environment variable.
61+
func IsSkipHSTS() bool {
62+
return os.Getenv("XUI_SKIP_HSTS") == "true"
63+
}
64+
6065
// GetBinFolderPath returns the path to the binary folder, defaulting to "bin" if not set via XUI_BIN_FOLDER.
6166
func GetBinFolderPath() string {
6267
binFolderPath := os.Getenv("XUI_BIN_FOLDER")

web/web.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ func (s *Server) initRouter() (*gin.Engine, error) {
154154

155155
engine := gin.Default()
156156
directHTTPS := s.isDirectHTTPSConfigured()
157-
engine.Use(middleware.SecurityHeadersMiddleware(directHTTPS))
157+
sendHSTS := directHTTPS && !config.IsSkipHSTS()
158+
engine.Use(middleware.SecurityHeadersMiddleware(sendHSTS))
158159

159160
webDomain, err := s.settingService.GetWebDomain()
160161
if err != nil {

0 commit comments

Comments
 (0)